use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class BMPImageWriter method canEncodeImage.
protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
ImageWriterSpi spi = this.getOriginatingProvider();
if (!spi.canEncodeImage(imgType)) {
return false;
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
}
if (compressionType == BI_RLE8 && bpp != 8) {
// only 8bpp images can be encoded as BI_RLE8
return false;
}
if (bpp == 16) {
/*
* Technically we expect that we may be able to
* encode only some of SinglePixelPackedSampleModel
* images here.
*
* In addition we should take into account following:
*
* 1. BI_RGB case, according to the MSDN description:
*
* The bitmap has a maximum of 2^16 colors. If the
* biCompression member of the BITMAPINFOHEADER is BI_RGB,
* the bmiColors member of BITMAPINFO is NULL. Each WORD
* in the bitmap array represents a single pixel. The
* relative intensities of red, green, and blue are
* represented with five bits for each color component.
*
* 2. BI_BITFIELDS case, according ot the MSDN description:
*
* Windows 95/98/Me: When the biCompression member is
* BI_BITFIELDS, the system supports only the following
* 16bpp color masks: A 5-5-5 16-bit image, where the blue
* mask is 0x001F, the green mask is 0x03E0, and the red mask
* is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
* is 0x001F, the green mask is 0x07E0, and the red mask is
* 0xF800.
*/
boolean canUseRGB = false;
boolean canUseBITFIELDS = false;
SampleModel sm = imgType.getSampleModel();
if (sm instanceof SinglePixelPackedSampleModel) {
int[] sizes = ((SinglePixelPackedSampleModel) sm).getSampleSize();
canUseRGB = true;
canUseBITFIELDS = true;
for (int i = 0; i < sizes.length; i++) {
canUseRGB &= (sizes[i] == 5);
canUseBITFIELDS &= ((sizes[i] == 5) || (i == 1 && sizes[i] == 6));
}
}
return (((compressionType == BI_RGB) && canUseRGB) || ((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
}
return true;
}
use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class BMPImageWriterSpi method canEncodeImage.
public boolean canEncodeImage(ImageTypeSpecifier type) {
int dataType = type.getSampleModel().getDataType();
if (dataType < DataBuffer.TYPE_BYTE || dataType > DataBuffer.TYPE_INT)
return false;
SampleModel sm = type.getSampleModel();
int numBands = sm.getNumBands();
if (!(numBands == 1 || numBands == 3))
return false;
if (numBands == 1 && dataType != DataBuffer.TYPE_BYTE)
return false;
if (dataType > DataBuffer.TYPE_BYTE && !(sm instanceof SinglePixelPackedSampleModel))
return false;
return true;
}
use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method imageIsContiguous.
/**
* Returns whether the image has contiguous data across rows.
*/
public static final boolean imageIsContiguous(RenderedImage image) {
SampleModel sm;
if (image instanceof BufferedImage) {
WritableRaster ras = ((BufferedImage) image).getRaster();
sm = ras.getSampleModel();
} else {
sm = image.getSampleModel();
}
if (sm instanceof ComponentSampleModel) {
// Ensure image rows samples are stored contiguously
// in a single bank.
ComponentSampleModel csm = (ComponentSampleModel) sm;
if (csm.getPixelStride() != csm.getNumBands()) {
return false;
}
int[] bandOffsets = csm.getBandOffsets();
for (int i = 0; i < bandOffsets.length; i++) {
if (bandOffsets[i] != i) {
return false;
}
}
int[] bankIndices = csm.getBankIndices();
for (int i = 0; i < bandOffsets.length; i++) {
if (bankIndices[i] != 0) {
return false;
}
}
return true;
}
// pixel stride.
return ImageUtil.isBinary(sm);
}
use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method setUnpackedBinaryData.
/**
* Copies data into the packed array of the <code>Raster</code>
* from an array of unpacked data of the form returned by
* <code>getUnpackedBinaryData()</code>.
*
* <p> If the data are binary, then the target bit will be set if
* and only if the corresponding byte is non-zero.
*
* @throws IllegalArgumentException if <code>isBinary()</code> returns
* <code>false</code> with the <code>SampleModel</code> of the
* supplied <code>Raster</code> as argument.
*/
public static void setUnpackedBinaryData(byte[] bdata, WritableRaster raster, Rectangle rect) {
SampleModel sm = raster.getSampleModel();
if (!isBinary(sm)) {
throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
}
int rectX = rect.x;
int rectY = rect.y;
int rectWidth = rect.width;
int rectHeight = rect.height;
DataBuffer dataBuffer = raster.getDataBuffer();
int dx = rectX - raster.getSampleModelTranslateX();
int dy = rectY - raster.getSampleModelTranslateY();
MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
int lineStride = mpp.getScanlineStride();
int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
int bitOffset = mpp.getBitOffset(dx);
int k = 0;
if (dataBuffer instanceof DataBufferByte) {
byte[] data = ((DataBufferByte) dataBuffer).getData();
for (int y = 0; y < rectHeight; y++) {
int bOffset = eltOffset * 8 + bitOffset;
for (int x = 0; x < rectWidth; x++) {
if (bdata[k++] != (byte) 0) {
data[bOffset / 8] |= (byte) (0x00000001 << (7 - bOffset & 7));
}
bOffset++;
}
eltOffset += lineStride;
}
} else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
for (int y = 0; y < rectHeight; y++) {
int bOffset = eltOffset * 16 + bitOffset;
for (int x = 0; x < rectWidth; x++) {
if (bdata[k++] != (byte) 0) {
data[bOffset / 16] |= (short) (0x00000001 << (15 - bOffset % 16));
}
bOffset++;
}
eltOffset += lineStride;
}
} else if (dataBuffer instanceof DataBufferInt) {
int[] data = ((DataBufferInt) dataBuffer).getData();
for (int y = 0; y < rectHeight; y++) {
int bOffset = eltOffset * 32 + bitOffset;
for (int x = 0; x < rectWidth; x++) {
if (bdata[k++] != (byte) 0) {
data[bOffset / 32] |= (int) (0x00000001 << (31 - bOffset % 32));
}
bOffset++;
}
eltOffset += lineStride;
}
}
}
use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class ImageTypeSpecifier method createBufferedImage.
/**
* Creates a <code>BufferedImage</code> with a given width and
* height according to the specification embodied in this object.
*
* @param width the desired width of the returned
* <code>BufferedImage</code>.
* @param height the desired height of the returned
* <code>BufferedImage</code>.
*
* @return a new <code>BufferedImage</code>
*
* @exception IllegalArgumentException if either <code>width</code> or
* <code>height</code> are negative or zero.
* @exception IllegalArgumentException if the product of
* <code>width</code> and <code>height</code> is greater than
* <code>Integer.MAX_VALUE</code>, or if the number of array
* elements needed to store the image is greater than
* <code>Integer.MAX_VALUE</code>.
*/
public BufferedImage createBufferedImage(int width, int height) {
try {
SampleModel sampleModel = getSampleModel(width, height);
WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), new Hashtable());
} catch (NegativeArraySizeException e) {
// Exception most likely thrown from a DataBuffer constructor
throw new IllegalArgumentException("Array size > Integer.MAX_VALUE!");
}
}
Aggregations