Search in sources :

Example 1 with SampleModel

use of java.awt.image.SampleModel 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 SampleModel

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

the class PngDitDepthTest method main.

public static void main(String[] args) throws IIOInvalidTreeException {
    // getting the writer for the png format
    Iterator iter = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = (ImageWriter) iter.next();
    // creating a color model
    ColorModel colorModel = ColorModel.getRGBdefault();
    // creating a sample model
    SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480);
    // creating a default metadata object
    IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null);
    String formatName = metaData.getNativeMetadataFormatName();
    // first call
    Node metaDataNode = metaData.getAsTree(formatName);
    try {
        metaData.setFromTree(formatName, metaDataNode);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // second call (bitdepht is already set to an invalid value)
    metaDataNode = metaData.getAsTree(formatName);
    metaData.setFromTree(formatName, metaDataNode);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException)

Example 3 with SampleModel

use of java.awt.image.SampleModel 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 4 with SampleModel

use of java.awt.image.SampleModel 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 5 with SampleModel

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

the class D3DSurfaceData method getRaster.

public synchronized Raster getRaster(int x, int y, int w, int h) {
    if (wrn == null) {
        DirectColorModel dcm = (DirectColorModel) getColorModel();
        SampleModel smHw;
        int dataType = 0;
        int scanStride = width;
        if (dcm.getPixelSize() > 16) {
            dataType = DataBuffer.TYPE_INT;
        } else {
            // 15, 16
            dataType = DataBuffer.TYPE_USHORT;
        }
        // note that we have to use the surface width and height here,
        // not the passed w,h
        smHw = new SinglePixelPackedSampleModel(dataType, width, height, scanStride, dcm.getMasks());
        DataBuffer dbn = new D3DDataBufferNative(this, dataType, width, height);
        wrn = WritableRasterNative.createNativeRaster(smHw, dbn);
    }
    return wrn;
}
Also used : SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) DirectColorModel(java.awt.image.DirectColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) DataBuffer(java.awt.image.DataBuffer)

Aggregations

SampleModel (java.awt.image.SampleModel)97 ColorModel (java.awt.image.ColorModel)43 ComponentSampleModel (java.awt.image.ComponentSampleModel)38 Point (java.awt.Point)33 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)33 DataBuffer (java.awt.image.DataBuffer)32 Rectangle (java.awt.Rectangle)30 IndexColorModel (java.awt.image.IndexColorModel)29 BufferedImage (java.awt.image.BufferedImage)27 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)26 WritableRaster (java.awt.image.WritableRaster)26 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)23 DataBufferByte (java.awt.image.DataBufferByte)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ComponentColorModel (java.awt.image.ComponentColorModel)17 DataBufferInt (java.awt.image.DataBufferInt)17 ColorSpace (java.awt.color.ColorSpace)15 Raster (java.awt.image.Raster)15 DataBufferUShort (java.awt.image.DataBufferUShort)13 BandedSampleModel (java.awt.image.BandedSampleModel)12