Example usage for java.awt.image WritableRaster getPixels

List of usage examples for java.awt.image WritableRaster getPixels

Introduction

In this page you can find the example usage for java.awt.image WritableRaster getPixels.

Prototype

public int[] getPixels(int x, int y, int w, int h, int[] iArray) 

Source Link

Document

Returns an int array containing all samples for a rectangle of pixels, one sample per array element.

Usage

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Invert pixel color if the image has a adobe marker ... not used now
 * @param raster/*from  www  .ja  va2  s.c  om*/
 */
@SuppressWarnings("unused")
private static void convertInvertedColors(WritableRaster raster) {
    int height = raster.getHeight();
    int width = raster.getWidth();
    int stride = width * 4;
    int[] pixelRow = new int[stride];
    for (int h = 0; h < height; h++) {
        raster.getPixels(0, h, width, 1, pixelRow);
        for (int x = 0; x < stride; x++)
            pixelRow[x] = 255 - pixelRow[x];
        raster.setPixels(0, h, width, 1, pixelRow);
    }
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Convert image profile from Ycck to Cmyk
 * @param raster/*from   ww w.ja v a  2s.  c o  m*/
 */
private static void convertYcckToCmyk(WritableRaster raster) {
    int height = raster.getHeight();
    int width = raster.getWidth();
    int stride = width * 4;
    int[] pixelRow = new int[stride];
    for (int h = 0; h < height; h++) {
        raster.getPixels(0, h, width, 1, pixelRow);

        for (int x = 0; x < stride; x += 4) {
            int y = pixelRow[x];
            int cb = pixelRow[x + 1];
            int cr = pixelRow[x + 2];

            int c = (int) (y + 1.402 * cr - 178.956);
            int m = (int) (y - 0.34414 * cb - 0.71414 * cr + 135.95984);
            y = (int) (y + 1.772 * cb - 226.316);

            if (c < 0)
                c = 0;
            else if (c > 255)
                c = 255;
            if (m < 0)
                m = 0;
            else if (m > 255)
                m = 255;
            if (y < 0)
                y = 0;
            else if (y > 255)
                y = 255;

            pixelRow[x] = 255 - c;
            pixelRow[x + 1] = 255 - m;
            pixelRow[x + 2] = 255 - y;
        }
        raster.setPixels(0, h, width, 1, pixelRow);
    }
}

From source file:tilt.image.Blob.java

public static float setToWhite(WritableRaster wr, Rectangle bounds) {
    int blackPixels = 0;
    int yEnd = bounds.height + bounds.y;
    int xEnd = bounds.width + bounds.x;
    // set all to white
    int[] iArray = new int[bounds.width];
    int[] dirty = new int[bounds.width];
    for (int x = 0; x < bounds.width; x++)
        iArray[x] = 255;//w  w  w  .  j  a va 2  s.co m
    for (int y = bounds.y; y < yEnd; y++) {
        wr.getPixels(bounds.x, y, bounds.width, 1, dirty);
        for (int i = 0; i < dirty.length; i++)
            if (dirty[i] == 0)
                blackPixels++;
        wr.setPixels(bounds.x, y, bounds.width, 1, iArray);
    }
    return (float) blackPixels / (float) (wr.getWidth() * wr.getHeight());
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Builds a new BufferedImage that is the difference between the two input images
 * @param ref the reference bitmap//from   w ww .  j av  a2 s .  c  o  m
 * @param gen the newly generated bitmap
 * @return the diff bitmap
 */
public static BufferedImage buildDiffImage(BufferedImage ref, BufferedImage gen) {
    BufferedImage diff = new BufferedImage(ref.getWidth(), ref.getHeight(), BufferedImage.TYPE_INT_ARGB);
    WritableRaster refWR = ref.getRaster();
    WritableRaster genWR = gen.getRaster();
    WritableRaster dstWR = diff.getRaster();

    boolean refPre = ref.isAlphaPremultiplied();
    if (!refPre) {
        ColorModel cm = ref.getColorModel();
        cm = GraphicsUtil.coerceData(refWR, cm, true);
        ref = new BufferedImage(cm, refWR, true, null);
    }
    boolean genPre = gen.isAlphaPremultiplied();
    if (!genPre) {
        ColorModel cm = gen.getColorModel();
        cm = GraphicsUtil.coerceData(genWR, cm, true);
        gen = new BufferedImage(cm, genWR, true, null);
    }

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

    int y, i, val;
    int[] refPix = null;
    int[] genPix = null;
    for (y = 0; y < h; y++) {
        refPix = refWR.getPixels(0, y, w, 1, refPix);
        genPix = genWR.getPixels(0, y, w, 1, genPix);
        for (i = 0; i < refPix.length; i++) {
            // val = ((genPix[i] - refPix[i]) * 5) + 128;
            val = ((refPix[i] - genPix[i]) * 10) + 128;
            if ((val & 0xFFFFFF00) != 0) {
                if ((val & 0x80000000) != 0) {
                    val = 0;
                } else {
                    val = 255;
                }
            }
            genPix[i] = val;
        }
        dstWR.setPixels(0, y, w, 1, genPix);
    }

    if (!genPre) {
        ColorModel cm = gen.getColorModel();
        cm = GraphicsUtil.coerceData(genWR, cm, false);
    }

    if (!refPre) {
        ColorModel cm = ref.getColorModel();
        cm = GraphicsUtil.coerceData(refWR, cm, false);
    }

    return diff;
}