Example usage for java.awt.image WritableRaster setPixel

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

Introduction

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

Prototype

public void setPixel(int x, int y, double[] dArray) 

Source Link

Document

Sets a pixel in the DataBuffer using a double array of samples for input.

Usage

From source file:Main.java

private static BufferedImage colorImage(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();
    WritableRaster raster = image.getRaster();

    for (int xx = 0; xx < width; xx++) {
        for (int yy = 0; yy < height; yy++) {
            int[] pixels = raster.getPixel(xx, yy, (int[]) null);
            pixels[0] = 0;/*ww  w.  j a  v  a 2 s.c o m*/
            pixels[1] = 255;
            pixels[2] = 255;
            raster.setPixel(xx, yy, pixels);
        }
    }
    return image;
}

From source file:tilt.image.Blob.java

/**
 * Actually set a black in the given raster
 * @param wr the raster to set a black pixel in
 * @param loc the location of the pixel//ww  w  .  j a  v a2s . c om
 */
public static void addBlackPixel(WritableRaster wr, Point loc) {
    int[] iArray = new int[1];
    wr.setPixel(loc.x, loc.y, iArray);
}

From source file:org.eclipse.swt.snippets.Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;/*from w  w  w . j  a  v a  2 s  . com*/
    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);
        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);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        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:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Applies the given transparency mask to a buffered image. Always creates a
 * new buffered image containing an alpha channel. Copies the old image into
 * the new one and then sets the alpha pixels according to the given mask.
 * /*ww  w. j  av  a 2s. c  o m*/
 * @param img the old image
 * @param mask the alpha mask
 * @return the new image with alpha channel applied
 * @throws IllegalArgumentException if the image's size does not match the
 *             mask's size
 */
public static BufferedImage applyTransparencyMask(BufferedImage img, ImageData mask) {
    if (mask.width != img.getWidth() || mask.height != img.getHeight()) {
        throw new IllegalArgumentException("Image size does not match the mask size");
    }

    // copy image and also convert to RGBA
    BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = result.getGraphics();
    g.drawImage(img, 0, 0, null);

    WritableRaster alphaRaster = result.getAlphaRaster();
    int alpha0[] = new int[] { 0 };
    int alpha255[] = new int[] { 255 };
    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            alphaRaster.setPixel(x, y, mask.getPixel(x, y) == 0 ? alpha0 : alpha255);
        }
    }

    return result;
}

From source file:ml.hsv.java

public static void HSV2File(hsv hsvImage[][], int width, int height) throws IOException //store img output as hsv2file.png
{
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int RGB = Color.HSBtoRGB(hsvImage[i][j].h, hsvImage[i][j].s, hsvImage[i][j].v);
            Color c = new Color(RGB);
            int temp[] = { c.getRed(), c.getGreen(), c.getBlue() };
            raster.setPixel(j, i, temp);
        }/* w w w.ja v  a  2s.  c  o m*/
    }
    ImageIO.write(image, "PNG",
            new File("/home/shinchan/FinalProject/PaperImplementation/Eclipse/ML/output/hsv2file.png"));
}

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Convert a SWT Image to a {@link BufferedImage}
 * // w w  w  .j av  a2 s  .com
 * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"}
 * 
 * @param data the SWT {@link ImageData}
 * @param applyAlphaMask true if the image data's alpha mask should be
 *            applied to the result image (if there is any). This method
 *            calls {@link #applyTransparencyMask(BufferedImage, ImageData)}
 *            for that purpose.
 * @return the AWT {@link BufferedImage}
 */
public static BufferedImage convertToAWT(ImageData data, boolean applyAlphaMask) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;

    BufferedImage result;
    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);
        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);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        result = 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);
            }
        }
        result = bufferedImage;
    }

    if (data.getTransparencyType() == SWT.TRANSPARENCY_MASK && applyAlphaMask) {
        result = applyTransparencyMask(result, data.getTransparencyMask());
    }
    return result;
}

From source file:org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java

/**
 * Returns an ARGB image filled with the given paint and using the given image as a mask.
 * @param paint the paint to fill the visible portions of the image with
 * @return a masked image filled with the given paint
 * @throws IOException if the image cannot be read
 * @throws IllegalStateException if the image is not a stencil.
 *//*from  w  w w  . ja  v  a2s . co  m*/
public static BufferedImage getStencilImage(PDImage pdImage, Paint paint) throws IOException {
    // get mask (this image)
    BufferedImage mask = getRGBImage(pdImage, null);

    // compose to ARGB
    BufferedImage masked = new BufferedImage(mask.getWidth(), mask.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = masked.createGraphics();

    // draw the mask
    //g.drawImage(mask, 0, 0, null);

    // fill with paint using src-in
    //g.setComposite(AlphaComposite.SrcIn);
    g.setPaint(paint);
    g.fillRect(0, 0, mask.getWidth(), mask.getHeight());
    g.dispose();

    // set the alpha
    int width = masked.getWidth();
    int height = masked.getHeight();
    WritableRaster raster = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    final float[] transparent = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            if (alphaPixel[0] == 255) {
                raster.setPixel(x, y, transparent);
            }
        }
    }

    return masked;
}

From source file:org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java

private static BufferedImage applyColorKeyMask(BufferedImage image, BufferedImage mask) throws IOException {
    int width = image.getWidth();
    int height = image.getHeight();

    // compose to ARGB
    BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    WritableRaster src = image.getRaster();
    WritableRaster dest = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    float[] rgb = new float[3];
    float[] rgba = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            src.getPixel(x, y, rgb);//from   ww  w.j a va  2 s .  c o m

            rgba[0] = rgb[0];
            rgba[1] = rgb[1];
            rgba[2] = rgb[2];
            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            rgba[3] = 255 - alphaPixel[0];

            dest.setPixel(x, y, rgba);
        }
    }

    return masked;
}

From source file:GraphicsUtil.java

public static void divideAlpha(WritableRaster wr) {
    if (is_BYTE_COMP_Data(wr.getSampleModel()))
        divide_BYTE_COMP_Data(wr);
    else if (is_INT_PACK_Data(wr.getSampleModel(), true))
        divide_INT_PACK_Data(wr);
    else {/*w ww  .j a va  2 s .co m*/
        int x0, x1, y0, y1, a, b;
        float ialpha;
        int bands = wr.getNumBands();
        int[] pixel = null;

        x0 = wr.getMinX();
        x1 = x0 + wr.getWidth();
        y0 = wr.getMinY();
        y1 = y0 + wr.getHeight();
        for (int y = y0; y < y1; y++)
            for (int x = x0; x < x1; x++) {
                pixel = wr.getPixel(x, y, pixel);
                a = pixel[bands - 1];
                if ((a > 0) && (a < 255)) {
                    ialpha = 255 / (float) a;
                    for (b = 0; b < bands - 1; b++)
                        pixel[b] = (int) (pixel[b] * ialpha + 0.5f);
                    wr.setPixel(x, y, pixel);
                }
            }
    }
}

From source file:GraphicsUtil.java

public static void multiplyAlpha(WritableRaster wr) {
    if (is_BYTE_COMP_Data(wr.getSampleModel()))
        mult_BYTE_COMP_Data(wr);//  ww w. j  ava2s  . c  o  m
    else if (is_INT_PACK_Data(wr.getSampleModel(), true))
        mult_INT_PACK_Data(wr);
    else {
        int[] pixel = null;
        int bands = wr.getNumBands();
        float norm = 1f / 255f;
        int x0, x1, y0, y1, a, b;
        float alpha;
        x0 = wr.getMinX();
        x1 = x0 + wr.getWidth();
        y0 = wr.getMinY();
        y1 = y0 + wr.getHeight();
        for (int y = y0; y < y1; y++)
            for (int x = x0; x < x1; x++) {
                pixel = wr.getPixel(x, y, pixel);
                a = pixel[bands - 1];
                if ((a >= 0) && (a < 255)) {
                    alpha = a * norm;
                    for (b = 0; b < bands - 1; b++)
                        pixel[b] = (int) (pixel[b] * alpha + 0.5f);
                    wr.setPixel(x, y, pixel);
                }
            }
    }
}