Search in sources :

Example 91 with WritableRaster

use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.

the class DrawImageBgTest method main.

public static void main(String[] args) {
    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    if (gc.getColorModel().getPixelSize() <= 8) {
        System.out.println("8-bit color model, test considered passed");
        return;
    }
    /*
         * Set up images:
         * 1.) VolatileImge for rendering to,
         * 2.) BufferedImage for reading back the contents of the VI
         * 3.) The image triggering the problem
         */
    VolatileImage vImg = null;
    BufferedImage readBackBImg;
    // create a BITMASK ICM such that the transparent color is
    // tr. black (and it's the first in the color map so a buffered image
    // created with this ICM is transparent
    byte[] r = { 0x00, (byte) 0xff };
    byte[] g = { 0x00, (byte) 0xff };
    byte[] b = { 0x00, (byte) 0xff };
    IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);
    WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);
    BufferedImage tImg = new BufferedImage(icm, wr, false, null);
    do {
        if (vImg == null || vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
            vImg = gc.createCompatibleVolatileImage(tImg.getWidth(), tImg.getHeight());
        }
        Graphics viG = vImg.getGraphics();
        viG.setColor(Color.red);
        viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
        viG.drawImage(tImg, 0, 0, Color.green, null);
        viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
        viG.drawImage(tImg, 0, 0, Color.white, null);
        readBackBImg = vImg.getSnapshot();
    } while (vImg.contentsLost());
    for (int x = 0; x < readBackBImg.getWidth(); x++) {
        for (int y = 0; y < readBackBImg.getHeight(); y++) {
            int currPixel = readBackBImg.getRGB(x, y);
            if (currPixel != Color.white.getRGB()) {
                String fileName = "DrawImageBgTest.png";
                try {
                    ImageIO.write(readBackBImg, "png", new File(fileName));
                    System.err.println("Dumped image to " + fileName);
                } catch (IOException ex) {
                }
                throw new RuntimeException("Test Failed: found wrong color: 0x" + Integer.toHexString(currPixel));
            }
        }
    }
    System.out.println("Test Passed.");
}
Also used : Graphics(java.awt.Graphics) VolatileImage(java.awt.image.VolatileImage) WritableRaster(java.awt.image.WritableRaster) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) GraphicsConfiguration(java.awt.GraphicsConfiguration) IndexColorModel(java.awt.image.IndexColorModel)

Example 92 with WritableRaster

use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.

the class EffectUtils method setPixels.

/**
     * <p>Writes a rectangular area of pixels in the destination
     * <code>BufferedImage</code>. Calling this method on
     * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
     * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
     *
     * @param img the destination image
     * @param x the x location at which to start storing pixels
     * @param y the y location at which to start storing pixels
     * @param w the width of the rectangle of pixels to store
     * @param h the height of the rectangle of pixels to store
     * @param pixels an array of pixels, stored as integers
     * @throws IllegalArgumentException is <code>pixels</code> is non-null and
     *   of length &lt; w*h
     */
public static void setPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" + " >= w*h");
    }
    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster)

Example 93 with WritableRaster

use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.

the class InnerShadowEffect method applyEffect.

/**
     * Apply the effect to the src image generating the result . The result image may or may not contain the source
     * image depending on what the effect type is.
     *
     * @param src The source image for applying the effect to
     * @param dst The dstination image to paint effect result into. If this is null then a new image will be created
     * @param w   The width of the src image to apply effect to, this allow the src and dst buffers to be bigger than
     *            the area the need effect applied to it
     * @param h   The height of the src image to apply effect to, this allow the src and dst buffers to be bigger than
     *            the area the need effect applied to it
     * @return Image with the result of the effect
     */
BufferedImage applyEffect(BufferedImage src, BufferedImage dst, int w, int h) {
    if (src == null || src.getType() != BufferedImage.TYPE_INT_ARGB) {
        throw new IllegalArgumentException("Effect only works with " + "source images of type BufferedImage.TYPE_INT_ARGB.");
    }
    if (dst != null && dst.getType() != BufferedImage.TYPE_INT_ARGB) {
        throw new IllegalArgumentException("Effect only works with " + "destination images of type BufferedImage.TYPE_INT_ARGB.");
    }
    // calculate offset
    double trangleAngle = Math.toRadians(angle - 90);
    int offsetX = (int) (Math.sin(trangleAngle) * distance);
    int offsetY = (int) (Math.cos(trangleAngle) * distance);
    // clac expanded size
    int tmpOffX = offsetX + size;
    int tmpOffY = offsetX + size;
    int tmpW = w + offsetX + size + size;
    int tmpH = h + offsetX + size;
    // create tmp buffers
    int[] lineBuf = getArrayCache().getTmpIntArray(w);
    byte[] srcAlphaBuf = getArrayCache().getTmpByteArray1(tmpW * tmpH);
    Arrays.fill(srcAlphaBuf, (byte) 0xFF);
    byte[] tmpBuf1 = getArrayCache().getTmpByteArray2(tmpW * tmpH);
    byte[] tmpBuf2 = getArrayCache().getTmpByteArray3(tmpW * tmpH);
    // extract src image alpha channel and inverse and offset
    Raster srcRaster = src.getRaster();
    for (int y = 0; y < h; y++) {
        int dy = (y + tmpOffY);
        int offset = dy * tmpW;
        srcRaster.getDataElements(0, y, w, 1, lineBuf);
        for (int x = 0; x < w; x++) {
            int dx = x + tmpOffX;
            srcAlphaBuf[offset + dx] = (byte) ((255 - ((lineBuf[x] & 0xFF000000) >>> 24)) & 0xFF);
        }
    }
    // blur
    float[] kernel = EffectUtils.createGaussianKernel(size * 2);
    // horizontal pass
    EffectUtils.blur(srcAlphaBuf, tmpBuf2, tmpW, tmpH, kernel, size * 2);
    // vertical pass
    EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size * 2);
    //rescale
    float spread = Math.min(1 / (1 - (0.01f * this.spread)), 255);
    for (int i = 0; i < tmpBuf1.length; i++) {
        int val = (int) (((int) tmpBuf1[i] & 0xFF) * spread);
        tmpBuf1[i] = (val > 255) ? (byte) 0xFF : (byte) val;
    }
    // create color image with shadow color and greyscale image as alpha
    if (dst == null)
        dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    WritableRaster shadowRaster = dst.getRaster();
    int red = color.getRed(), green = color.getGreen(), blue = color.getBlue();
    for (int y = 0; y < h; y++) {
        int srcY = y + tmpOffY;
        int offset = srcY * tmpW;
        int shadowOffset = (srcY - offsetY) * tmpW;
        for (int x = 0; x < w; x++) {
            int srcX = x + tmpOffX;
            int origianlAlphaVal = 255 - ((int) srcAlphaBuf[offset + srcX] & 0xFF);
            int shadowVal = (int) tmpBuf1[shadowOffset + (srcX - offsetX)] & 0xFF;
            int alphaVal = Math.min(origianlAlphaVal, shadowVal);
            lineBuf[x] = ((byte) alphaVal & 0xFF) << 24 | red << 16 | green << 8 | blue;
        }
        shadowRaster.setDataElements(0, y, w, 1, lineBuf);
    }
    return dst;
}
Also used : WritableRaster(java.awt.image.WritableRaster) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Example 94 with WritableRaster

use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.

the class EffectUtils method setPixels.

/**
     * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
     * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
     * will unmanage the image.</p>
     *
     * @param img    the destination image
     * @param x      the x location at which to start storing pixels
     * @param y      the y location at which to start storing pixels
     * @param w      the width of the rectangle of pixels to store
     * @param h      the height of the rectangle of pixels to store
     * @param pixels an array of pixels, stored as integers
     * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
     */
static void setPixels(BufferedImage img, int x, int y, int w, int h, byte[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }
    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster)

Example 95 with WritableRaster

use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.

the class LCMSTransform method colorConvert.

public void colorConvert(BufferedImage src, BufferedImage dst) {
    LCMSImageLayout srcIL, dstIL;
    try {
        if (!dst.getColorModel().hasAlpha()) {
            dstIL = LCMSImageLayout.createImageLayout(dst);
            if (dstIL != null) {
                srcIL = LCMSImageLayout.createImageLayout(src);
                if (srcIL != null) {
                    doTransform(srcIL, dstIL);
                    return;
                }
            }
        }
    } catch (ImageLayoutException e) {
        throw new CMMException("Unable to convert images");
    }
    Raster srcRas = src.getRaster();
    WritableRaster dstRas = dst.getRaster();
    ColorModel srcCM = src.getColorModel();
    ColorModel dstCM = dst.getColorModel();
    int w = src.getWidth();
    int h = src.getHeight();
    int srcNumComp = srcCM.getNumColorComponents();
    int dstNumComp = dstCM.getNumColorComponents();
    int precision = 8;
    float maxNum = 255.0f;
    for (int i = 0; i < srcNumComp; i++) {
        if (srcCM.getComponentSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    for (int i = 0; i < dstNumComp; i++) {
        if (dstCM.getComponentSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    float[] srcMinVal = new float[srcNumComp];
    float[] srcInvDiffMinMax = new float[srcNumComp];
    ColorSpace cs = srcCM.getColorSpace();
    for (int i = 0; i < srcNumComp; i++) {
        srcMinVal[i] = cs.getMinValue(i);
        srcInvDiffMinMax[i] = maxNum / (cs.getMaxValue(i) - srcMinVal[i]);
    }
    cs = dstCM.getColorSpace();
    float[] dstMinVal = new float[dstNumComp];
    float[] dstDiffMinMax = new float[dstNumComp];
    for (int i = 0; i < dstNumComp; i++) {
        dstMinVal[i] = cs.getMinValue(i);
        dstDiffMinMax[i] = (cs.getMaxValue(i) - dstMinVal[i]) / maxNum;
    }
    boolean dstHasAlpha = dstCM.hasAlpha();
    boolean needSrcAlpha = srcCM.hasAlpha() && dstHasAlpha;
    float[] dstColor;
    if (dstHasAlpha) {
        dstColor = new float[dstNumComp + 1];
    } else {
        dstColor = new float[dstNumComp];
    }
    if (precision == 8) {
        byte[] srcLine = new byte[w * srcNumComp];
        byte[] dstLine = new byte[w * dstNumComp];
        Object pixel;
        float[] color;
        float[] alpha = null;
        if (needSrcAlpha) {
            alpha = new float[w];
        }
        int idx;
        // TODO check for src npixels = dst npixels
        try {
            srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(1), getNumInComponents());
            dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
        } catch (ImageLayoutException e) {
            throw new CMMException("Unable to convert images");
        }
        // process each scanline
        for (int y = 0; y < h; y++) {
            // convert src scanline
            pixel = null;
            color = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                pixel = srcRas.getDataElements(x, y, pixel);
                color = srcCM.getNormalizedComponents(pixel, color, 0);
                for (int i = 0; i < srcNumComp; i++) {
                    srcLine[idx++] = (byte) ((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] + 0.5f);
                }
                if (needSrcAlpha) {
                    alpha[x] = color[srcNumComp];
                }
            }
            // color convert srcLine to dstLine
            doTransform(srcIL, dstIL);
            // convert dst scanline
            pixel = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                for (int i = 0; i < dstNumComp; i++) {
                    dstColor[i] = ((float) (dstLine[idx++] & 0xff)) * dstDiffMinMax[i] + dstMinVal[i];
                }
                if (needSrcAlpha) {
                    dstColor[dstNumComp] = alpha[x];
                } else if (dstHasAlpha) {
                    dstColor[dstNumComp] = 1.0f;
                }
                pixel = dstCM.getDataElements(dstColor, 0, pixel);
                dstRas.setDataElements(x, y, pixel);
            }
        }
    } else {
        short[] srcLine = new short[w * srcNumComp];
        short[] dstLine = new short[w * dstNumComp];
        Object pixel;
        float[] color;
        float[] alpha = null;
        if (needSrcAlpha) {
            alpha = new float[w];
        }
        int idx;
        try {
            srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(2), getNumInComponents() * 2);
            dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(2), getNumOutComponents() * 2);
        } catch (ImageLayoutException e) {
            throw new CMMException("Unable to convert images");
        }
        // process each scanline
        for (int y = 0; y < h; y++) {
            // convert src scanline
            pixel = null;
            color = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                pixel = srcRas.getDataElements(x, y, pixel);
                color = srcCM.getNormalizedComponents(pixel, color, 0);
                for (int i = 0; i < srcNumComp; i++) {
                    srcLine[idx++] = (short) ((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] + 0.5f);
                }
                if (needSrcAlpha) {
                    alpha[x] = color[srcNumComp];
                }
            }
            // color convert srcLine to dstLine
            doTransform(srcIL, dstIL);
            // convert dst scanline
            pixel = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                for (int i = 0; i < dstNumComp; i++) {
                    dstColor[i] = ((float) (dstLine[idx++] & 0xffff)) * dstDiffMinMax[i] + dstMinVal[i];
                }
                if (needSrcAlpha) {
                    dstColor[dstNumComp] = alpha[x];
                } else if (dstHasAlpha) {
                    dstColor[dstNumComp] = 1.0f;
                }
                pixel = dstCM.getDataElements(dstColor, 0, pixel);
                dstRas.setDataElements(x, y, pixel);
            }
        }
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace) ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) CMMException(java.awt.color.CMMException) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel)

Aggregations

WritableRaster (java.awt.image.WritableRaster)110 BufferedImage (java.awt.image.BufferedImage)74 ColorModel (java.awt.image.ColorModel)33 DirectColorModel (java.awt.image.DirectColorModel)20 IndexColorModel (java.awt.image.IndexColorModel)19 DataBufferByte (java.awt.image.DataBufferByte)17 Point (java.awt.Point)15 Raster (java.awt.image.Raster)15 Rectangle (java.awt.Rectangle)13 Graphics2D (java.awt.Graphics2D)12 ComponentColorModel (java.awt.image.ComponentColorModel)12 DataBufferInt (java.awt.image.DataBufferInt)10 SampleModel (java.awt.image.SampleModel)9 DataBuffer (java.awt.image.DataBuffer)8 IOException (java.io.IOException)8 ColorSpace (java.awt.color.ColorSpace)7 Color (java.awt.Color)6 Iterator (java.util.Iterator)6 AffineTransform (java.awt.geom.AffineTransform)5 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)5