Search in sources :

Example 51 with WritableRaster

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

the class OffScreenImageSource method sendPixels.

private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width * numDataElements];
    boolean needToCvt = true;
    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);
        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y = 0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0, width);
            }
        } else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y = 0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x = 0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0, width);
            }
        } else if (dataType == DataBuffer.TYPE_SHORT || dataType == DataBuffer.TYPE_INT) {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y = 0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
            }
        }
    } else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch(dataType) {
            case DataBuffer.TYPE_INT:
                for (int y = 0; y < height; y++) {
                    raster.getDataElements(0, y, width, 1, scanline);
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
                }
                break;
            case DataBuffer.TYPE_BYTE:
                byte[] bscanline = new byte[width];
                for (int y = 0; y < height; y++) {
                    raster.getDataElements(0, y, width, 1, bscanline);
                    for (int x = 0; x < width; x++) {
                        scanline[x] = bscanline[x] & 0xff;
                    }
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
                }
                break;
            case DataBuffer.TYPE_USHORT:
                short[] sscanline = new short[width];
                for (int y = 0; y < height; y++) {
                    raster.getDataElements(0, y, width, 1, sscanline);
                    for (int x = 0; x < width; x++) {
                        scanline[x] = sscanline[x] & 0xffff;
                    }
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
                }
                break;
            default:
                needToCvt = true;
        }
    }
    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0, width);
        }
    }
}
Also used : ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 52 with WritableRaster

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

the class ImageRepresentation method getOpaqueRGBImage.

public BufferedImage getOpaqueRGBImage() {
    if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) {
        int w = bimage.getWidth();
        int h = bimage.getHeight();
        int size = w * h;
        // Note that we steal the data array here, but only for reading...
        DataBufferInt db = (DataBufferInt) biRaster.getDataBuffer();
        int[] pixels = SunWritableRaster.stealData(db, 0);
        for (int i = 0; i < size; i++) {
            if ((pixels[i] >>> 24) != 0xff) {
                return bimage;
            }
        }
        ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
        int[] bandmasks = { 0x00ff0000, 0x0000ff00, 0x000000ff };
        WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null);
        try {
            BufferedImage opImage = createImage(opModel, opRaster, false, null);
            return opImage;
        } catch (Exception e) {
            return bimage;
        }
    }
    return bimage;
}
Also used : DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) AWTException(java.awt.AWTException)

Example 53 with WritableRaster

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

the class CGLGraphicsConfig method createAcceleratedImage.

@Override
public Image createAcceleratedImage(Component target, int width, int height) {
    ColorModel model = getColorModel(Transparency.OPAQUE);
    WritableRaster wr = model.createCompatibleWritableRaster(width, height);
    return new OffScreenImage(target, model, wr, model.isAlphaPremultiplied());
}
Also used : DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) OffScreenImage(sun.awt.image.OffScreenImage)

Example 54 with WritableRaster

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

the class CGLGraphicsConfig method createCompatibleImage.

@Override
public BufferedImage createCompatibleImage(int width, int height) {
    ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    WritableRaster raster = model.createCompatibleWritableRaster(width, height);
    return new BufferedImage(model, raster, model.isAlphaPremultiplied(), null);
}
Also used : DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) BufferedImage(java.awt.image.BufferedImage)

Example 55 with WritableRaster

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

the class JPEGImageWriter method grabPixels.

/**
     * Put the scanline y of the source ROI view Raster into the
     * 1-line Raster for writing.  This handles ROI and band
     * rearrangements, and expands indexed images.  Subsampling is
     * done in the native code.
     * This is called by the native code.
     */
private void grabPixels(int y) {
    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset, sourceYOffset + y, sourceWidth, 1, 0, 0, new int[] { 0 });
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB = (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine, forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset, sourceYOffset + y, sourceWidth, 1, 0, 0, srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(), sourceLine.getWidth(), sourceLine.getHeight(), data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(), sourceLine.getWidth(), sourceLine.getHeight(), data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(), wr.getWidth(), wr.getHeight(), 0, 0, srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y % 8 == 0)) {
        // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

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