Example usage for java.awt.image BufferedImage getRGB

List of usage examples for java.awt.image BufferedImage getRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRGB.

Prototype

public int getRGB(int x, int y) 

Source Link

Document

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Usage

From source file:org.apache.pdfbox.rendering.TestPDFToImage.java

/**
 * Get the difference between two images, identical colors are set to white, differences are
 * xored, the highest bit of each color is reset to avoid colors that are too light
 *
 * @param bim1//from w  w  w  .j a  v  a 2 s  .  c o  m
 * @param bim2
 * @return If the images are different, the function returns a diff image If the images are
 * identical, the function returns null If the size is different, a black border on the botton
 * and the right is created
 *
 * @throws IOException
 */
private BufferedImage diffImages(BufferedImage bim1, BufferedImage bim2) throws IOException {
    int minWidth = Math.min(bim1.getWidth(), bim2.getWidth());
    int minHeight = Math.min(bim1.getHeight(), bim2.getHeight());
    int maxWidth = Math.max(bim1.getWidth(), bim2.getWidth());
    int maxHeight = Math.max(bim1.getHeight(), bim2.getHeight());
    BufferedImage bim3 = null;
    if (minWidth != maxWidth || minHeight != maxHeight) {
        bim3 = createEmptyDiffImage(minWidth, minHeight, maxWidth, maxHeight);
    }
    for (int x = 0; x < minWidth; ++x) {
        for (int y = 0; y < minHeight; ++y) {
            int rgb1 = bim1.getRGB(x, y);
            int rgb2 = bim2.getRGB(x, y);
            if (rgb1 != rgb2
                    // don't bother about differences of 1 color step
                    && (Math.abs((rgb1 & 0xFF) - (rgb2 & 0xFF)) > 1
                            || Math.abs(((rgb1 >> 8) & 0xFF) - ((rgb2 >> 8) & 0xFF)) > 1
                            || Math.abs(((rgb1 >> 16) & 0xFF) - ((rgb2 >> 16) & 0xFF)) > 1)) {
                if (bim3 == null) {
                    bim3 = createEmptyDiffImage(minWidth, minHeight, maxWidth, maxHeight);
                }
                int r = Math.abs((rgb1 & 0xFF) - (rgb2 & 0xFF));
                int g = Math.abs((rgb1 & 0xFF00) - (rgb2 & 0xFF00));
                int b = Math.abs((rgb1 & 0xFF0000) - (rgb2 & 0xFF0000));
                bim3.setRGB(x, y, 0xFFFFFF - (r | g | b));
            } else {
                if (bim3 != null) {
                    bim3.setRGB(x, y, Color.WHITE.getRGB());
                }
            }
        }
    }
    return bim3;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage rgb2gray(BufferedImage img) {
    if (img.getType() == BufferedImage.TYPE_INT_ARGB || img.getType() == BufferedImage.TYPE_INT_RGB) {
        int w, h;
        w = img.getWidth();/*from  w w  w  .  j ava  2 s .  c om*/
        h = img.getHeight();
        BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

        //----------------------------v3.0----------------------
        ColorModel cm = img.getColorModel();
        int pixel, gr;
        int r, g, b;
        WritableRaster raster = out.getRaster();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                gr = (int) Math.round(((double) r + (double) g + (double) b) / 3.0);
                raster.setSample(x, y, 0, gr);
            }
        }
        return out;
    }
    return img;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGBA(BufferedImage img) {
    int w, h, a, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_ARGB) {
        w = img.getWidth();//w w  w .  ja  va  2 s  . co m
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[4];
        WritableRaster[] rasters = new WritableRaster[4];
        for (int i = 0; i < 4; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                a = cm.getAlpha(pixel);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[3].setSample(x, y, 0, a);
                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGB(BufferedImage img) {

    //--------------------v2.0--------------------------------------
    int w, h, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_RGB) {
        w = img.getWidth();//from ww  w. j  ava2  s.  c  o  m
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[3];
        WritableRaster[] rasters = new WritableRaster[3];
        for (int i = 0; i < 3; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:com.hygenics.imaging.ImageCleanup.java

/**
 * Private method that performs the sharpen
 *//*from w w w .j  a  va 2 s  .  c o  m*/
public byte[] sharpen(byte[] ibytes, int weight, String format) {
    /*
     * Kernel is |-1|-1|-1| |-1|weight|-1||-1|-1|-1|
     */
    try {
        InputStream is = new ByteArrayInputStream(ibytes);
        BufferedImage proxyimage = ImageIO.read(is);
        // a secondary image for storing new outcomes
        BufferedImage image2 = new BufferedImage(proxyimage.getWidth(), proxyimage.getHeight(),
                BufferedImage.TYPE_BYTE_GRAY);

        // image width and height
        int width = proxyimage.getWidth();
        int height = proxyimage.getHeight();
        int r = 0;
        int g = 0;
        int b = 0;
        Color c = null;
        for (int x = 1; x < width - 1; x++) {
            for (int y = 1; y < height - 1; y++) {
                // sharpen the image using the kernel (center is c5)
                Color c00 = new Color(proxyimage.getRGB(x - 1, y - 1));
                Color c01 = new Color(proxyimage.getRGB(x - 1, y));
                Color c02 = new Color(proxyimage.getRGB(x - 1, y + 1));
                Color c10 = new Color(proxyimage.getRGB(x, y - 1));
                Color c11 = new Color(proxyimage.getRGB(x, y));
                Color c12 = new Color(proxyimage.getRGB(x, y + 1));
                Color c20 = new Color(proxyimage.getRGB(x + 1, y - 1));
                Color c21 = new Color(proxyimage.getRGB(x + 1, y));
                Color c22 = new Color(proxyimage.getRGB(x + 1, y + 1));

                // apply the kernel for r
                r = -c00.getRed() - c01.getRed() - c02.getRed() - c10.getRed() + (weight * c11.getRed())
                        - c12.getRed() - c20.getRed() - c21.getRed() - c22.getRed();

                // apply the kernel for g
                g = c00.getGreen() - c01.getGreen() - c02.getGreen() - c10.getGreen()
                        + (weight * c11.getGreen()) - c12.getGreen() - c20.getGreen() - c21.getGreen()
                        - c22.getGreen();

                // apply the transformation for b
                b = c00.getBlue() - c01.getBlue() - c02.getBlue() - c10.getBlue() + (weight * c11.getBlue())
                        - c12.getBlue() - c20.getBlue() - c21.getBlue() - c22.getBlue();

                // set the new rgb values
                r = Math.min(255, Math.max(0, r));
                g = Math.min(255, Math.max(0, g));
                b = Math.min(255, Math.max(0, b));

                c = new Color(r, g, b);

                // set the new mask colors in the new image
                image2.setRGB(x, y, c.getRGB());

            }
        }

        // add the new values back to the original image
        Color cmask = null;
        Color corig = null;
        for (int x = 1; x < width - 1; x++) {
            for (int y = 1; y < height - 1; y++) {
                // get the 2 colors
                cmask = new Color(image2.getRGB(x, y));
                corig = new Color(proxyimage.getRGB(x, y));

                // add the new values
                r = cmask.getRed() + corig.getRed();
                g = cmask.getGreen() + corig.getGreen();
                b = cmask.getBlue() + corig.getBlue();

                // set the new rgb values
                r = Math.min(255, Math.max(0, r));
                g = Math.min(255, Math.max(0, g));
                b = Math.min(255, Math.max(0, b));

                proxyimage.setRGB(x, y, new Color(r, g, b).getRGB());
            }
        }

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            ImageIO.write(proxyimage, format, baos);
            ibytes = baos.toByteArray();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return ibytes;
}

From source file:ome.services.blitz.impl.RoiI.java

public void uploadMask_async(final AMD_IRoi_uploadMask __cb, final long imageId, final int z, final int t,
        final byte[] bytes, final Current __current) throws ServerError {

    final IceMapper mapper = new IceMapper(IceMapper.VOID);

    runnableCall(__current, new Adapter(__cb, __current, mapper, factory.getExecutor(), factory.principal,
            new SimpleWork(this, "uploadMask", bytes) {

                @Transactional(readOnly = false)
                public Object doWork(Session session, ServiceFactory sf) {
                    IUpdate update = sf.getUpdateService();

                    ome.model.core.Image image;
                    ome.model.roi.Roi roi;
                    ByteArrayInputStream s = new ByteArrayInputStream(bytes);
                    IQuery query = sf.getQueryService();
                    IObject o = query.findByQuery(
                            "from Image as i left outer join " + "fetch i.pixels as p where i.id = " + imageId,
                            null);//  ww w .j a  v  a  2s .  c o m

                    try {
                        image = (ome.model.core.Image) o;
                        BufferedImage inputImage = ImageIO.read(s);
                        Map<Integer, MaskClass> map = new HashMap<Integer, MaskClass>();
                        MaskClass mask;
                        int value;
                        for (int x = 0; x < inputImage.getWidth(); x++)
                            for (int y = 0; y < inputImage.getHeight(); y++) {
                                value = inputImage.getRGB(x, y);
                                if (value == Color.black.getRGB())
                                    continue;
                                if (!map.containsKey(value)) {
                                    mask = new MaskClass(value);
                                    map.put(value, mask);
                                } else
                                    mask = map.get(value);
                                mask.add(new Point(x, y));
                            }
                        Iterator<Integer> maskIterator = map.keySet().iterator();
                        while (maskIterator.hasNext()) {
                            int colour = maskIterator.next();
                            mask = map.get(colour);
                            roi = new ome.model.roi.Roi();
                            roi.setImage(image);
                            ome.model.roi.Mask toSaveMask = mask.asMaskI(z, t);
                            roi.addShape(toSaveMask);
                            ome.model.roi.Roi newROI = update.saveAndReturnObject(roi);
                        }
                        return null;
                    } catch (Exception e) {
                        __cb.ice_exception(e);
                    }
                    return null;
                }
            }));
}

From source file:oct.analysis.application.comp.EZWorker.java

/**
 * Recursively search for a contour to the right of the supplied starting
 * point. If the contour returned contains the starting point then the
 * contour traced back to the start point rather than towards the edge of
 * the image.//from   w  ww.  ja v a  2 s .c om
 *
 * @param searchPoint point to search from
 * @param startPoint start search point
 * @param contourList list to add the contour points to
 * @param sharpOCT OCT to find the contour in
 * @return the next point in the contour after the search point
 */
public Point findContourRight(Point searchPoint, Cardinality searchDirection, Point startPoint,
        Cardinality startDirection, LinkedList<Point> contourList, BufferedImage sharpOCT, int depth)
        throws InterruptedException {
    if (debug) {
        publish(searchPoint);
        Thread.sleep(debug_sleep);
    }
    depth++;
    if (depth > depthThreshold) {
        //the recursive search has gone awry, we must terminate it before causing the stack to overflow
        return null;
    }
    Point nextPoint;
    Cardinality nextDirection;
    switch (searchDirection) {
    case SOUTH:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x + 1, searchPoint.y)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.EAST;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x + 1, searchPoint.y + 1)) == 0) {
            nextPoint = new Point(searchPoint.x + 1, searchPoint.y + 1);
            nextDirection = Cardinality.WEST;
        } else {
            nextPoint = new Point(searchPoint.x + 1, searchPoint.y);
            nextDirection = Cardinality.SOUTH;
        }
        break;
    case EAST:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x, searchPoint.y - 1)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.NORTH;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x + 1, searchPoint.y - 1)) == 0) {
            nextPoint = new Point(searchPoint.x + 1, searchPoint.y - 1);
            nextDirection = Cardinality.SOUTH;
        } else {
            nextPoint = new Point(searchPoint.x, searchPoint.y - 1);
            nextDirection = Cardinality.EAST;
        }
        break;
    case WEST:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x, searchPoint.y + 1)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.SOUTH;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x - 1, searchPoint.y + 1)) == 0) {
            nextPoint = new Point(searchPoint.x - 1, searchPoint.y + 1);
            nextDirection = Cardinality.NORTH;
        } else {
            nextPoint = new Point(searchPoint.x, searchPoint.y + 1);
            nextDirection = Cardinality.WEST;
        }
        break;
    case NORTH:
        if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x - 1, searchPoint.y)) > 0) {
            nextPoint = new Point(searchPoint);
            nextDirection = Cardinality.WEST;
        } else if (Util.calculateGrayScaleValue(sharpOCT.getRGB(searchPoint.x - 1, searchPoint.y - 1)) == 0) {
            nextPoint = new Point(searchPoint.x - 1, searchPoint.y - 1);
            nextDirection = Cardinality.EAST;
        } else {
            nextPoint = new Point(searchPoint.x - 1, searchPoint.y);
            nextDirection = Cardinality.NORTH;
        }
        break;
    default:
        //will never happen, just placed in to make code compile
        nextPoint = new Point(searchPoint);
        nextDirection = Cardinality.EAST;
        break;
    }
    if (!((nextPoint.equals(startPoint) && nextDirection == startDirection) || nextPoint.y < 100
            || nextPoint.y > sharpOCT.getHeight() - 20 || nextPoint.x <= 20
            || nextPoint.x >= sharpOCT.getWidth() - 20)) {
        contourList.add(findContourRight(nextPoint, nextDirection, startPoint, startDirection, contourList,
                sharpOCT, depth));
    }
    return nextPoint;
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

private static BufferedImage shiftHue(BufferedImage img, ImageConfiguration imageConfiguration) {
    int height = img.getHeight();
    int width = img.getWidth();
    int imageType = 0;
    if (imageConfiguration.getColorType() == COLOR_TYPE.MONOCHROM) {
        imageType = BufferedImage.TYPE_BYTE_BINARY;
    } else if (imageConfiguration.getColorType() == COLOR_TYPE.GRAY) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    } else if (imageConfiguration.getColorType() == COLOR_TYPE.RGB) {
        imageType = BufferedImage.TYPE_INT_RGB;
    } else {/*from   w w w  .ja va  2 s  .co  m*/
        imageType = BufferedImage.TYPE_INT_ARGB;
    }

    BufferedImage finalThresholdImage = new BufferedImage(width, height, imageType);

    Float hueDelta = null;
    Float saturationDelta = null;
    Float brightnessDelta = null;
    if (imageConfiguration.getRgb() != null) {
        hueDelta = degreeToPercent(imageConfiguration.getRgb().getHSB()[0]);
        saturationDelta = imageConfiguration.getRgb().getHSB()[1];
        brightnessDelta = imageConfiguration.getRgb().getHSB()[2];
    } else {
        hueDelta = imageConfiguration.getAdjHue();
        saturationDelta = imageConfiguration.getAdjSaturation();
        brightnessDelta = imageConfiguration.getAdjBrightness();
    }

    if (hueDelta == null) {
        hueDelta = 0f;
    }
    if (saturationDelta == null) {
        saturationDelta = 0f;
    }
    if (brightnessDelta == null) {
        brightnessDelta = 0f;
    }
    for (int x = 0; x < width; x++) {
        try {
            for (int y = 0; y < height; y++) {
                int rgb = img.getRGB(x, y);
                Color color = new Color(rgb);
                float[] hsb = getHsb(imageConfiguration, color);
                hsb = applyDeltas(imageConfiguration, hueDelta, saturationDelta, brightnessDelta, hsb);
                hsb = replaceColors(imageConfiguration, color, hsb);
                int newRGB = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
                finalThresholdImage.setRGB(x, y, newRGB);
                if (imageType == BufferedImage.TYPE_INT_ARGB) {
                    setAlpha(finalThresholdImage, x, y, rgb);
                    finalThresholdImage = makeColorsTransparent(finalThresholdImage, imageConfiguration,
                            new Color(newRGB));
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    return finalThresholdImage;
}

From source file:org.pentaho.di.core.gui.SwingDirectGC.java

private void drawImage(SwingUniversalImage img, int centerX, int centerY, double angle, int imageSize) {
    if (isDrawingPixelatedImages() && img.isBitmap()) {
        BufferedImage bi = img.getAsBitmapForSize(imageSize, imageSize, angle);

        int offx = centerX + xOffset - bi.getWidth() / 2;
        int offy = centerY + yOffset - bi.getHeight() / 2;
        for (int x = 0; x < bi.getWidth(observer); x++) {
            for (int y = 0; y < bi.getHeight(observer); y++) {
                int rgb = bi.getRGB(x, y);
                gc.setColor(new Color(rgb));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(offx + x, offy + y, offx + x, offy + y);
            }/*from   w  w w .j a  v  a2 s  . com*/
        }
    } else {
        gc.setBackground(Color.white);
        gc.clearRect(centerX, centerY, imageSize, imageSize);
        img.drawToGraphics(gc, centerX, centerY, imageSize, imageSize, angle);
    }
}

From source file:gr.iti.mklab.reveal.forensics.maps.dwnoisevar.DWNoiseVarExtractor.java

public void getNoiseMap() throws IOException {

    BufferedImage img = inputImage;

    int imWidth, imHeight;
    if (img.getWidth() % 2 == 0) {
        imWidth = img.getWidth();//from   ww  w.j  a  va2  s  .  c o  m
    } else {
        imWidth = (img.getWidth() - 1);
    }
    if (img.getHeight() % 2 == 0) {
        imHeight = img.getHeight();
    } else {
        imHeight = (img.getHeight() - 1);
    }

    int columnFilterScale = (int) (Math.log(imHeight) / Math.log(2)) - 1;
    int rowFilterScale = (int) (Math.log(imWidth) / Math.log(2)) - 1;

    double[][] imgYAsArray = new double[imWidth][imHeight];
    double[][] filteredImgYAsArray = new double[imWidth][imHeight / 2];
    double[][] doubleFilteredImgYAsArray = new double[imWidth / 2][imHeight / 2];
    double[] imgColumn, imgRow;
    Color tmpcolor;
    double R, G, B;

    for (int ii = 0; ii < imWidth; ii++) {
        for (int jj = 0; jj < imHeight; jj++) {
            tmpcolor = new Color(img.getRGB(ii, jj));
            R = tmpcolor.getRed();
            G = tmpcolor.getGreen();
            B = tmpcolor.getBlue();
            imgYAsArray[ii][jj] = 0.2989 * R + 0.5870 * G + 0.1140 * B;
        }
    }

    double[] waveletColumn;
    RealMatrix rm = new Array2DRowRealMatrix(imgYAsArray);
    for (int ii = 0; ii < imWidth; ii++) {
        try {
            imgColumn = new double[imHeight];
            imgColumn = rm.getRow(ii);
            //Long startTime1 = System.currentTimeMillis();  
            waveletColumn = DWT.transform(imgColumn, Wavelet.Daubechies, 8, columnFilterScale,
                    DWT.Direction.forward);
            System.arraycopy(waveletColumn, imHeight / 2, filteredImgYAsArray[ii], 0, imHeight / 2);
        } catch (Exception ex) {
            Logger.getLogger(DWNoiseVarExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    double[] waveletRow;
    RealMatrix rm2 = new Array2DRowRealMatrix(filteredImgYAsArray);
    for (int jj = 0; jj < imHeight / 2; jj++) {
        try {
            imgRow = new double[imWidth];
            imgRow = rm2.getColumn(jj);
            waveletRow = DWT.transform(imgRow, Wavelet.Daubechies, 8, rowFilterScale, DWT.Direction.forward);
            for (int ii = 0; ii < imWidth / 2; ii++) {
                doubleFilteredImgYAsArray[ii][jj] = waveletRow[ii + imWidth / 2];
            }
        } catch (Exception ex) {
            Logger.getLogger(DWNoiseVarExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    int blockSize = 8;
    double[][] blockNoiseVar = Util.blockNoiseVar(doubleFilteredImgYAsArray, blockSize);

    int medianFilterSize = 7;
    if (medianFilterSize > blockNoiseVar.length) {
        medianFilterSize = blockNoiseVar.length - 1;
    }
    if (medianFilterSize > blockNoiseVar[0].length) {
        medianFilterSize = blockNoiseVar[0].length - 1;
    }
    if (medianFilterSize < 5) {
        minNoiseValue = 0;
        maxNoiseValue = 0;
        noiseMap = new float[1][1];
        noiseMap[0][0] = 0;
        double[][] artificialOutput = new double[1][1];
        artificialOutput[0][0] = 0;
        BufferedImage outputImage = Util.visualizeWithJet(artificialOutput);
        displaySurface = outputImage;
        return;
    }

    float[][] outBlockMap = Util.medianFilterSingleChannelImage(blockNoiseVar, medianFilterSize);

    minNoiseValue = Util.minDouble2DArray(outBlockMap);
    maxNoiseValue = Util.maxDouble2DArray(outBlockMap);
    noiseMap = outBlockMap;
    double[][] normalizedMap = Util.normalizeIm(outBlockMap);
    BufferedImage outputImage = Util.visualizeWithJet(normalizedMap);
    // output
    displaySurface = outputImage;
}