Example usage for java.awt.image WritableRaster setPixels

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

Introduction

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

Prototype

public void setPixels(int x, int y, int w, int h, double[] dArray) 

Source Link

Document

Sets all samples for a rectangle of pixels from a double array containing one sample per array element.

Usage

From source file:Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;/*ww w  .  j  av  a2  s .c om*/
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                pixelArray[0] = rgb.red;
                pixelArray[1] = rgb.green;
                pixelArray[2] = rgb.blue;
                raster.setPixels(x, y, 1, 1, pixelArray);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

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   w w  w  .j av  a  2 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: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  ww  .  j a v a2 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:Engine.WorldMap.java

private void updateImageLabel(int[] pixels, int labelId) {
    miniMapImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = miniMapImage.getRaster();
    raster.setPixels(0, 0, width, height, pixels);
    //JLabel label = labelsArray.get(labelId);
    //label.setIcon(new ImageIcon(image));
}

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

/**
 * Convert image profile from Ycck to Cmyk
 * @param raster/*from w ww.j  a  v a2 s  . 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:org.apache.fop.visual.BitmapComparator.java

/**
 * Builds a new BufferedImage that is the difference between the two input images
 * @param ref the reference bitmap/* w ww.j  a va2  s  . c om*/
 * @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;
}

From source file:GraphicsUtil.java

public static void copyData_FALLBACK(Raster src, WritableRaster dst) {
    // System.out.println("Fallback copyData");

    int x0 = dst.getMinX();
    if (x0 < src.getMinX())
        x0 = src.getMinX();/*from   w ww. ja  v  a 2s.  c  o  m*/

    int y0 = dst.getMinY();
    if (y0 < src.getMinY())
        y0 = src.getMinY();

    int x1 = dst.getMinX() + dst.getWidth() - 1;
    if (x1 > src.getMinX() + src.getWidth() - 1)
        x1 = src.getMinX() + src.getWidth() - 1;

    int y1 = dst.getMinY() + dst.getHeight() - 1;
    if (y1 > src.getMinY() + src.getHeight() - 1)
        y1 = src.getMinY() + src.getHeight() - 1;

    int width = x1 - x0 + 1;
    int[] data = null;

    for (int y = y0; y <= y1; y++) {
        data = src.getPixels(x0, y, width, 1, data);
        dst.setPixels(x0, y, width, 1, data);
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.TriangleBasedShadingContext.java

@Override
public final Raster getRaster(int x, int y, int w, int h) {
    WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
    int[] data = new int[w * h * 4];
    if (!isDataEmpty() || getBackground() != null) {
        for (int row = 0; row < h; row++) {
            int currentY = y + row;
            if (bboxRect != null && (currentY < minBBoxY || currentY > maxBBoxY)) {
                continue;
            }//from w  w w .ja  v  a  2 s.  com
            for (int col = 0; col < w; col++) {
                int currentX = x + col;
                if (bboxRect != null && (currentX < minBBoxX || currentX > maxBBoxX)) {
                    continue;
                }
                Point p = new Point(currentX, currentY);
                int value;
                if (pixelTable.containsKey(p)) {
                    value = pixelTable.get(p);
                } else {
                    if (getBackground() == null) {
                        continue;
                    }
                    value = getRgbBackground();
                }
                int index = (row * w + col) * 4;
                data[index] = value & 255;
                value >>= 8;
                data[index + 1] = value & 255;
                value >>= 8;
                data[index + 2] = value & 255;
                data[index + 3] = 255;
            }
        }
    }
    raster.setPixels(0, 0, w, h, data);
    return raster;
}

From source file:GraphicsUtil.java

/**
 * Copies data from one bufferedImage to another paying attention
 * to the state of AlphaPreMultiplied./*from   w  w w .j  av a  2 s  .  c om*/
 *
 * @param src The source
 * @param srcRect The Rectangle of source data to be copied
 * @param dst The destination
 * @param destP The Place for the upper left corner of srcRect in dst.
 */
public static void copyData(BufferedImage src, Rectangle srcRect, BufferedImage dst, Point destP) {

    /*
     if (srcCS != dstCS)
    throw new IllegalArgumentException
        ("Images must be in the same ColorSpace in order "+
         "to copy Data between them");
     */
    boolean srcAlpha = src.getColorModel().hasAlpha();
    boolean dstAlpha = dst.getColorModel().hasAlpha();

    // System.out.println("Src has: " + srcAlpha +
    //                    " is: " + src.isAlphaPremultiplied());
    //
    // System.out.println("Dst has: " + dstAlpha +
    //                    " is: " + dst.isAlphaPremultiplied());

    if (srcAlpha == dstAlpha)
        if (!srcAlpha || src.isAlphaPremultiplied() == dst.isAlphaPremultiplied()) {
            // They match one another so just copy everything...
            copyData(src.getRaster(), dst.getRaster());
            return;
        }

    // System.out.println("Using Slow CopyData");

    int[] pixel = null;
    Raster srcR = src.getRaster();
    WritableRaster dstR = dst.getRaster();
    int bands = dstR.getNumBands();

    int dx = destP.x - srcRect.x;
    int dy = destP.y - srcRect.y;

    int w = srcRect.width;
    int x0 = srcRect.x;
    int y0 = srcRect.y;
    int y1 = y0 + srcRect.height - 1;

    if (!srcAlpha) {
        // Src has no alpha dest does so set alpha to 1.0 everywhere.
        // System.out.println("Add Alpha");
        int[] oPix = new int[bands * w];
        int out = (w * bands) - 1; // The 2 skips alpha channel
        while (out >= 0) {
            // Fill alpha channel with 255's
            oPix[out] = 255;
            out -= bands;
        }

        int b, in;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = w * (bands - 1) - 1;
            out = (w * bands) - 2; // The 2 skips alpha channel on last pix
            switch (bands) {
            case 4:
                while (in >= 0) {
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    out--;
                }
                break;
            default:
                while (in >= 0) {
                    for (b = 0; b < bands - 1; b++)
                        oPix[out--] = pixel[in--];
                    out--;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else if (dstAlpha && dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to multiply it for dst.
        // System.out.println("Mult Case");
        int a, b, alpha, in, fpNorm = (1 << 24) / 255, pt5 = 1 << 23;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = bands * w - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= 4;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= bands;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (dstAlpha && !dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to divide it out for dst.
        // System.out.println("Div Case");
        int a, b, ialpha, in, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands * w) - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= 4;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= bands;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (src.isAlphaPremultiplied()) {
        int[] oPix = new int[bands * w];
        // Src has alpha dest does not so unpremult and store...
        // System.out.println("Remove Alpha, Div Case");
        int a, b, ialpha, in, out, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands + 1) * w - 1;
            out = (bands * w) - 1;
            while (in >= 0) {
                a = pixel[in];
                in--;
                if (a > 0) {
                    if (a < 255) {
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands; b++)
                            oPix[out--] = (pixel[in--] * ialpha + pt5) >>> 16;
                    } else
                        for (b = 0; b < bands; b++)
                            oPix[out--] = pixel[in--];
                } else {
                    in -= bands;
                    for (b = 0; b < bands; b++)
                        oPix[out--] = 255;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else {
        // Src has unpremult alpha, dest does not have alpha,
        // just copy the color channels over.
        Rectangle dstRect = new Rectangle(destP.x, destP.y, srcRect.width, srcRect.height);
        for (int b = 0; b < bands; b++)
            copyBand(srcR, srcRect, b, dstR, dstRect, b);
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.AxialShadingContext.java

/**
 * {@inheritDoc}/*from   w  ww  . jav a2  s .c  om*/
 */
public Raster getRaster(int x, int y, int w, int h) {
    // create writable raster
    WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
    float[] input = new float[1];
    int[] data = new int[w * h * 3];
    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            double inputValue = x1x0 * (x + i - coords[0]);
            inputValue += y1y0 * (y + j - coords[1]);
            inputValue /= denom;
            // input value is out of range
            if (inputValue < domain[0]) {
                // the shading has to be extended if extend[0] == true
                if (extend[0]) {
                    inputValue = domain[0];
                } else {
                    continue;
                }
            }
            // input value is out of range
            else if (inputValue > domain[1]) {
                // the shading has to be extended if extend[1] == true
                if (extend[1]) {
                    inputValue = domain[1];
                } else {
                    continue;
                }
            }
            input[0] = (float) (domain[0] + (d1d0 * inputValue));
            float[] values = null;
            try {
                values = function.eval(input);
            } catch (IOException exception) {
                LOG.error("error while processing a function", exception);
            }
            int index = (j * w + i) * 3;
            // convert color values from shading colorspace to RGB 
            if (shadingColorSpace != null) {
                values = shadingColorSpace.toRGB(values);
            }
            data[index] = (int) (values[0] * 255);
            data[index + 1] = (int) (values[1] * 255);
            data[index + 2] = (int) (values[2] * 255);
        }
    }
    raster.setPixels(0, 0, w, h, data);
    return raster;
}