Search in sources :

Example 1 with DataBuffer

use of java.awt.image.DataBuffer in project jna by java-native-access.

the class RasterRangesUtils method outputOccupiedRanges.

/**
     * Outputs ranges of occupied pixels.
     * In a raster that has an alpha layer, a pixel is occupied if its alpha value is not null.
     * In a raster without alpha layer, a pixel is occupied if it is not completely black.
     * @param raster image to be segmented in non black or non-transparent ranges
     * @param out destination of the non null ranges
     * @return true if the output succeeded, false otherwise
     */
public static boolean outputOccupiedRanges(Raster raster, RangesOutput out) {
    Rectangle bounds = raster.getBounds();
    SampleModel sampleModel = raster.getSampleModel();
    boolean hasAlpha = sampleModel.getNumBands() == 4;
    // Try to use the underlying data array directly for a few common raster formats
    if (raster.getParent() == null && bounds.x == 0 && bounds.y == 0) {
        // No support for subraster (as obtained with Image.getSubimage(...))
        DataBuffer data = raster.getDataBuffer();
        if (data.getNumBanks() == 1) {
            if (sampleModel instanceof MultiPixelPackedSampleModel) {
                MultiPixelPackedSampleModel packedSampleModel = (MultiPixelPackedSampleModel) sampleModel;
                if (packedSampleModel.getPixelBitStride() == 1) {
                    // TYPE_BYTE_BINARY
                    return outputOccupiedRangesOfBinaryPixels(((DataBufferByte) data).getData(), bounds.width, bounds.height, out);
                }
            } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
                if (sampleModel.getDataType() == DataBuffer.TYPE_INT) {
                    // TYPE_INT_ARGB, TYPE_INT_ARGB_PRE, TYPE_INT_BGR or TYPE_INT_RGB
                    return outputOccupiedRanges(((DataBufferInt) data).getData(), bounds.width, bounds.height, hasAlpha ? 0xff000000 : 0xffffff, out);
                }
            // TODO could easily handle cases of TYPE_USHORT_GRAY and TYPE_BYTE_GRAY.
            }
        }
    }
    // Fallback behaviour : copy pixels of raster
    int[] pixels = raster.getPixels(0, 0, bounds.width, bounds.height, (int[]) null);
    return outputOccupiedRanges(pixels, bounds.width, bounds.height, hasAlpha ? 0xff000000 : 0xffffff, out);
}
Also used : SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) Rectangle(java.awt.Rectangle) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBuffer(java.awt.image.DataBuffer)

Example 2 with DataBuffer

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

the class ImageFactory method createCCMImage.

public static BufferedImage createCCMImage(int cs, int dataType) {
    ColorSpace cSpace = ColorSpace.getInstance(cs);
    ComponentColorModel ccm = null;
    if (dataType == DataBuffer.TYPE_INT) {
        ccm = new ComponentColorModel(cSpace, ((cs == ColorSpace.CS_GRAY) ? new int[] { 8 } : new int[] { 8, 8, 8 }), false, false, Transparency.OPAQUE, dataType);
    } else {
        ccm = new ComponentColorModel(cSpace, false, false, Transparency.OPAQUE, dataType);
    }
    SampleModel sm = ccm.createCompatibleSampleModel(WIDTH, HEIGHT);
    WritableRaster raster = ccm.createCompatibleWritableRaster(WIDTH, HEIGHT);
    DataBuffer data = raster.getDataBuffer();
    fillCCM(data, sm, cSpace);
    return new BufferedImage(ccm, raster, false, null);
}
Also used : SampleModel(java.awt.image.SampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 3 with DataBuffer

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

the class ImageFactory method createDCMImage.

public static BufferedImage createDCMImage(int type, int cs) {
    if (type == BufferedImage.TYPE_INT_RGB && cs == ColorSpace.CS_sRGB) {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        DataBuffer data = image.getData().getDataBuffer();
        fill(image);
        return image;
    }
    ColorSpace cSpace = ColorSpace.getInstance(cs);
    DirectColorModel dcm = null;
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            dcm = new DirectColorModel(cSpace, 32, rMask, gMask, bMask, aMask, false, DataBuffer.TYPE_INT);
            break;
        case BufferedImage.TYPE_INT_RGB:
            dcm = new DirectColorModel(cSpace, 24, rMask, gMask, bMask, 0, false, DataBuffer.TYPE_INT);
            break;
        case BufferedImage.TYPE_INT_BGR:
            dcm = new DirectColorModel(cSpace, 24, rMask, gMask, bMask, 0, false, DataBuffer.TYPE_INT);
            break;
        case BufferedImage.TYPE_USHORT_555_RGB:
            dcm = new DirectColorModel(cSpace, 15, r555Mask, g555Mask, b555Mask, 0, false, DataBuffer.TYPE_USHORT);
            break;
        case BufferedImage.TYPE_USHORT_565_RGB:
            dcm = new DirectColorModel(cSpace, 16, r565Mask, g565Mask, b565Mask, 0, false, DataBuffer.TYPE_USHORT);
            break;
    }
    SampleModel sm = dcm.createCompatibleSampleModel(WIDTH, HEIGHT);
    WritableRaster raster = dcm.createCompatibleWritableRaster(WIDTH, HEIGHT);
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            fillDCM(raster.getDataBuffer(), sm, cSpace.getType());
            break;
        case BufferedImage.TYPE_INT_RGB:
            fillDCM(raster.getDataBuffer(), sm, cSpace.getType());
            break;
        case BufferedImage.TYPE_INT_BGR:
            fillDCM(raster.getDataBuffer(), sm, cSpace.getType());
            break;
        case BufferedImage.TYPE_USHORT_555_RGB:
            fillDCM(raster.getDataBuffer(), sm, cSpace.getType(), 5, 5, 5);
            break;
        case BufferedImage.TYPE_USHORT_565_RGB:
            fillDCM(raster.getDataBuffer(), sm, cSpace.getType(), 5, 6, 5);
            break;
    }
    return new BufferedImage(dcm, raster, false, null);
}
Also used : SampleModel(java.awt.image.SampleModel) ColorSpace(java.awt.color.ColorSpace) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 4 with DataBuffer

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

the class IncorrectAlphaConversionBicubic method makeUnmanagedBI.

private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc, int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) DataBuffer(java.awt.image.DataBuffer)

Example 5 with DataBuffer

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

the class UnmanagedDrawImagePerformance method makeUnmanagedBI.

private static BufferedImage makeUnmanagedBI(final int type) {
    final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBuffer (java.awt.image.DataBuffer)26 DataBufferInt (java.awt.image.DataBufferInt)15 SampleModel (java.awt.image.SampleModel)14 DataBufferByte (java.awt.image.DataBufferByte)12 BufferedImage (java.awt.image.BufferedImage)11 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)10 DataBufferShort (java.awt.image.DataBufferShort)9 Point (java.awt.Point)8 WritableRaster (java.awt.image.WritableRaster)8 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 DataBufferUShort (java.awt.image.DataBufferUShort)5 DirectColorModel (java.awt.image.DirectColorModel)4 Graphics2D (java.awt.Graphics2D)3 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 Rectangle (java.awt.Rectangle)2 ColorSpace (java.awt.color.ColorSpace)2 ColorModel (java.awt.image.ColorModel)2 Raster (java.awt.image.Raster)2 ByteBuffer (java.nio.ByteBuffer)2