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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations