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 startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 

Source Link

Document

Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image data.

Usage

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight) {
    if (targetWidth == 0) {
        targetWidth = 1;/* w w w .  ja v  a 2  s . co m*/
    }
    if (targetHeight == 0) {
        targetHeight = 1;
    }

    if (targetHeight > 1 && targetWidth > 1) {
        throw new Wrong9PatchException();
    }

    int w = image.getWidth();
    int h = image.getHeight();
    int[] data = image.getRGB(0, 0, w, h, null, 0, w);
    int[] newData = new int[targetWidth * targetHeight];

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            newData[y * targetWidth + x] = 0x00;
        }
    }

    List<Integer> startPositions = new ArrayList<Integer>();
    List<Integer> endPositions = new ArrayList<Integer>();

    boolean inBlock = false;
    if (targetHeight == 1) {
        for (int x = 0; x < w; x++) {
            if ((0xff000000 & data[x]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(x);
                }
            } else if (inBlock) {
                endPositions.add(x - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(w - 1);
        }
    } else {
        for (int y = 0; y < h; y++) {
            if ((0xff000000 & data[y]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(y);
                }
            } else if (inBlock) {
                endPositions.add(y - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(h - 1);
        }
    }
    try {
        SplineInterpolator interpolator = new SplineInterpolator();
        PolynomialSplineFunction function = interpolator.interpolate(
                new double[] { 0f, 1f, Math.max(w - 1, h - 1) },
                new double[] { 0f, 1f, Math.max(targetHeight - 1, targetWidth - 1) });
        for (int i = 0; i < startPositions.size(); i++) {
            int start = startPositions.get(i);
            int end = endPositions.get(i);
            for (int j = (int) function.value(start); j <= (int) function.value(end); j++) {
                newData[j] = 0xff000000;
            }
        }

        BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth);
        return img;
    } catch (Exception e) {
        Logger.getInstance(ImageUtils.class).error("resizeBorder", e);
    }

    return null;
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

private static BufferedImage generateBordersImage(BufferedImage source, int trimmedWidth, int trimmedHeight)
        throws IOException {
    BufferedImage finalBorder = UIUtil.createImage(trimmedWidth + 2, trimmedHeight + 2,
            BufferedImage.TYPE_INT_ARGB);
    int cutW = source.getWidth() - 2;
    int cutH = source.getHeight() - 2;

    // left border
    BufferedImage leftBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    leftBorder.setRGB(0, 0, 1, cutH, source.getRGB(0, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(leftBorder);//from  w ww. j  a v  a  2s.c om
    leftBorder = resizeBorder(leftBorder, 1, trimmedHeight);
    finalBorder.setRGB(0, 1, 1, trimmedHeight, leftBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // right border
    BufferedImage rightBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    rightBorder.setRGB(0, 0, 1, cutH, source.getRGB(cutW + 1, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(rightBorder);
    rightBorder = resizeBorder(rightBorder, 1, trimmedHeight);
    finalBorder.setRGB(trimmedWidth + 1, 1, 1, trimmedHeight,
            rightBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // top border
    BufferedImage topBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    topBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, 0, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(topBorder);
    topBorder = resizeBorder(topBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, 0, trimmedWidth, 1, topBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0,
            trimmedWidth);

    // bottom border
    BufferedImage bottomBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    bottomBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, cutH + 1, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(bottomBorder);
    bottomBorder = resizeBorder(bottomBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, trimmedHeight + 1, trimmedWidth, 1,
            bottomBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth);

    return finalBorder;
}

From source file:org.jtrfp.trcl.core.Texture.java

public static ByteBuffer RGBA8FromPNG(BufferedImage image, int startX, int startY, int sizeX, int sizeY) {
    //int color;//from w ww  .j ava 2s.  co  m
    ByteBuffer buf = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * 4);
    final int[] row = new int[image.getWidth()];
    for (int y = startY; y < startY + sizeY; y++) {
        image.getRGB(0, y, image.getWidth(), 1, row, 0, image.getWidth());
        for (int color : row) {
            buf.put((byte) ((color & 0x00FF0000) >> 16));
            buf.put((byte) ((color & 0x0000FF00) >> 8));
            buf.put((byte) (color & 0x000000FF));
            buf.put((byte) ((color & 0xFF000000) >> 24));
        } // end for(x)
    } // end for(y)
    buf.clear();// Rewind
    return buf;
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static BufferedImage generateBordersImage(BufferedImage source, int trimmedWidth, int trimmedHeight)
        throws Wrong9PatchException, IOException {
    BufferedImage finalBorder = UIUtil.createImage(trimmedWidth + 2, trimmedHeight + 2,
            BufferedImage.TYPE_INT_ARGB);
    int cutW = source.getWidth() - 2;
    int cutH = source.getHeight() - 2;

    // left border
    BufferedImage leftBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    leftBorder.setRGB(0, 0, 1, cutH, source.getRGB(0, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(leftBorder);//  w w w  .  j  ava 2s.  co m
    leftBorder = resizeBorder(leftBorder, 1, trimmedHeight);
    finalBorder.setRGB(0, 1, 1, trimmedHeight, leftBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // right border
    BufferedImage rightBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    rightBorder.setRGB(0, 0, 1, cutH, source.getRGB(cutW + 1, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(rightBorder);
    rightBorder = resizeBorder(rightBorder, 1, trimmedHeight);
    finalBorder.setRGB(trimmedWidth + 1, 1, 1, trimmedHeight,
            rightBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // top border
    BufferedImage topBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    topBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, 0, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(topBorder);
    topBorder = resizeBorder(topBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, 0, trimmedWidth, 1, topBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0,
            trimmedWidth);

    // bottom border
    BufferedImage bottomBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    bottomBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, cutH + 1, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(bottomBorder);
    bottomBorder = resizeBorder(bottomBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, trimmedHeight + 1, trimmedWidth, 1,
            bottomBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth);

    return finalBorder;
}

From source file:TextureByReference.java

public static void flipImage(BufferedImage bImage) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    int[] tempArray = new int[width * height];
    int y2 = 0;/* w  w  w .  ja  v  a 2s .c o m*/
    for (int y = height - 1; y >= 0; y--) {
        for (int x = 0; x < width; x++) {
            tempArray[y2 * width + x] = rgbArray[y * width + x];
        }
        y2++;
    }
    bImage.setRGB(0, 0, width, height, tempArray, 0, width);
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the PSNR between two files/*from   w w w  . j a  va 2 s . c  o m*/
 * @param pOne first image to compare
 * @param pTwo second image to compare
 * @return calculated psnr
 */
public static double calcPSNR(final File pOne, final File pTwo) {
    BufferedImage imageOne = null;
    try {
        imageOne = Imaging.getBufferedImage(pOne);
    } catch (IOException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0,
            imageOne.getWidth());
    final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY
            || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY);
    imageOne = null;

    BufferedImage imageTwo = null;
    try {
        imageTwo = Imaging.getBufferedImage(pTwo);
    } catch (IOException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0,
            imageTwo.getWidth());
    imageTwo = null;

    final double psnr = calcPSNR(oneA, twoA, greyscale);

    return psnr;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the SSIM between two files//from   w w  w. j a v a2  s  .co  m
 * @param pOne first image to compare
 * @param pTwo second image to compare
 * @param pHeatMapFilename ssim heat map image filename (can be null)
 * @param pMin list for return value - ssim minimum (can be null)
 * @param pVariance list for return value - ssim variance (can be null)
 * @return calculated ssim
 */
public static double calcSSIM(final File pOne, final File pTwo, final String pHeatMapFilename,
        List<Double> pMin, List<Double> pVariance) {

    BufferedImage imageOne = null;
    try {
        imageOne = Imaging.getBufferedImage(pOne);
    } catch (IOException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0,
            imageOne.getWidth());
    final int width = imageOne.getWidth();
    final int height = imageOne.getHeight();
    final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY
            || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY);
    imageOne = null;

    BufferedImage imageTwo = null;
    try {
        imageTwo = Imaging.getBufferedImage(pTwo);
    } catch (IOException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0,
            imageTwo.getWidth());
    imageTwo = null;

    final double ssim = calcSSIM(oneA, twoA, width, height, greyscale, pHeatMapFilename, pMin, pVariance);

    return ssim;
}

From source file:TextureByReference.java

public static BufferedImage convertImage(BufferedImage bImage, int type) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    BufferedImage newImage = new BufferedImage(width, height, type);
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    newImage.setRGB(0, 0, width, height, rgbArray, 0, width);
    return newImage;
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

public static BufferedImage resizeNinePatchImage(ImageInformation information) throws IOException {
    BufferedImage image = ImageIO.read(information.getImageFile());
    if (MathUtils.floatEquals(information.getFactor(), 1f)) {
        return image;
    }/*from   w  w w.ja  va  2 s .  c om*/

    BufferedImage trimmedImage = trim9PBorder(image);
    ImageInformation trimmedImageInformation = ImageInformation.newBuilder(information)
            .setExportName(getExportName("trimmed", information.getExportName())).build();
    saveImageTempFile(trimmedImage, trimmedImageInformation);
    trimmedImage = resizeNormalImage(trimmedImage, trimmedImageInformation);
    saveImageTempFile(trimmedImage, ImageInformation.newBuilder(trimmedImageInformation)
            .setExportName(getExportName("trimmedResized", information.getExportName())).build());

    BufferedImage borderImage;

    int w = trimmedImage.getWidth();
    int h = trimmedImage.getHeight();

    try {
        borderImage = generateBordersImage(image, w, h);
    } catch (Exception e) {
        return null;
    }

    int[] rgbArray = new int[w * h];
    trimmedImage.getRGB(0, 0, w, h, rgbArray, 0, w);
    borderImage.setRGB(1, 1, w, h, rgbArray, 0, w);

    return borderImage;
}

From source file:org.uva.itast.blended.omr.OMRUtils.java

/**
   * Writes out a single PNG which is three times the width of the input image, containing from left
   * to right: the original image, the row sampling monochrome version, and the 2D sampling
   * monochrome version./*from  w  w w  .  ja va 2  s  . c  o  m*/
   */
public static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) {
    // TODO: Update to compare different Binarizer implementations.
    String inputName = uri.getPath();
    if (inputName.contains(".mono.png")) {
        return;
    }

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int stride = width * 3;
    int[] pixels = new int[stride * height];

    // The original image
    int[] argb = new int[width];
    for (int y = 0; y < height; y++) {
        image.getRGB(0, y, width, 1, argb, 0, width);
        System.arraycopy(argb, 0, pixels, y * stride, width);
    }

    // Row sampling
    BitArray row = new BitArray(width);
    for (int y = 0; y < height; y++) {
        try {
            row = bitmap.getBlackRow(y, row);
        } catch (NotFoundException nfe) {
            // If fetching the row failed, draw a red line and keep going.
            int offset = y * stride + width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = 0xffff0000;
            }
            continue;
        }

        int offset = y * stride + width;
        for (int x = 0; x < width; x++) {
            if (row.get(x)) {
                pixels[offset + x] = 0xff000000;
            } else {
                pixels[offset + x] = 0xffffffff;
            }
        }
    }

    // 2D sampling
    try {
        for (int y = 0; y < height; y++) {
            BitMatrix matrix = bitmap.getBlackMatrix();
            int offset = y * stride + width * 2;
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[offset + x] = 0xff000000;
                } else {
                    pixels[offset + x] = 0xffffffff;
                }
            }
        }
    } catch (NotFoundException nfe) {
    }

    writeResultImage(stride, height, pixels, uri, inputName, ".mono.png");
}