use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class LCMSTransform method colorConvert.
public void colorConvert(Raster src, WritableRaster dst) {
LCMSImageLayout srcIL, dstIL;
dstIL = LCMSImageLayout.createImageLayout(dst);
if (dstIL != null) {
srcIL = LCMSImageLayout.createImageLayout(src);
if (srcIL != null) {
doTransform(srcIL, dstIL);
return;
}
}
// Can't pass src and dst directly to CMM, so process per scanline
SampleModel srcSM = src.getSampleModel();
SampleModel dstSM = dst.getSampleModel();
int srcTransferType = src.getTransferType();
int dstTransferType = dst.getTransferType();
int w = src.getWidth();
int h = src.getHeight();
int srcNumBands = src.getNumBands();
int dstNumBands = dst.getNumBands();
int precision = 8;
float maxNum = 255.0f;
for (int i = 0; i < srcNumBands; i++) {
if (srcSM.getSampleSize(i) > 8) {
precision = 16;
maxNum = 65535.0f;
}
}
for (int i = 0; i < dstNumBands; i++) {
if (dstSM.getSampleSize(i) > 8) {
precision = 16;
maxNum = 65535.0f;
}
}
float[] srcScaleFactor = new float[srcNumBands];
float[] dstScaleFactor = new float[dstNumBands];
for (int i = 0; i < srcNumBands; i++) {
if (srcTransferType == DataBuffer.TYPE_SHORT) {
srcScaleFactor[i] = maxNum / 32767.0f;
} else {
srcScaleFactor[i] = maxNum / ((float) ((1 << srcSM.getSampleSize(i)) - 1));
}
}
for (int i = 0; i < dstNumBands; i++) {
if (dstTransferType == DataBuffer.TYPE_SHORT) {
dstScaleFactor[i] = 32767.0f / maxNum;
} else {
dstScaleFactor[i] = ((float) ((1 << dstSM.getSampleSize(i)) - 1)) / maxNum;
}
}
int ys = src.getMinY();
int yd = dst.getMinY();
int xs, xd;
int sample;
if (precision == 8) {
byte[] srcLine = new byte[w * srcNumBands];
byte[] dstLine = new byte[w * dstNumBands];
int idx;
// TODO check for src npixels = dst npixels
try {
srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(1), getNumInComponents());
dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
} catch (ImageLayoutException e) {
throw new CMMException("Unable to convert rasters");
}
// process each scanline
for (int y = 0; y < h; y++, ys++, yd++) {
// get src scanline
xs = src.getMinX();
idx = 0;
for (int x = 0; x < w; x++, xs++) {
for (int i = 0; i < srcNumBands; i++) {
sample = src.getSample(xs, ys, i);
srcLine[idx++] = (byte) ((sample * srcScaleFactor[i]) + 0.5f);
}
}
// color convert srcLine to dstLine
doTransform(srcIL, dstIL);
// store dst scanline
xd = dst.getMinX();
idx = 0;
for (int x = 0; x < w; x++, xd++) {
for (int i = 0; i < dstNumBands; i++) {
sample = (int) (((dstLine[idx++] & 0xff) * dstScaleFactor[i]) + 0.5f);
dst.setSample(xd, yd, i, sample);
}
}
}
} else {
short[] srcLine = new short[w * srcNumBands];
short[] dstLine = new short[w * dstNumBands];
int idx;
try {
srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(2), getNumInComponents() * 2);
dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(2), getNumOutComponents() * 2);
} catch (ImageLayoutException e) {
throw new CMMException("Unable to convert rasters");
}
// process each scanline
for (int y = 0; y < h; y++, ys++, yd++) {
// get src scanline
xs = src.getMinX();
idx = 0;
for (int x = 0; x < w; x++, xs++) {
for (int i = 0; i < srcNumBands; i++) {
sample = src.getSample(xs, ys, i);
srcLine[idx++] = (short) ((sample * srcScaleFactor[i]) + 0.5f);
}
}
// color convert srcLine to dstLine
doTransform(srcIL, dstIL);
// store dst scanline
xd = dst.getMinX();
idx = 0;
for (int x = 0; x < w; x++, xd++) {
for (int i = 0; i < dstNumBands; i++) {
sample = (int) (((dstLine[idx++] & 0xffff) * dstScaleFactor[i]) + 0.5f);
dst.setSample(xd, yd, i, sample);
}
}
}
}
}
use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method setPackedBinaryData.
/**
* Sets the supplied <code>Raster</code>'s data from an array
* of packed binary data of the form returned by
* <code>getPackedBinaryData()</code>.
*
* @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 setPackedBinaryData(byte[] binaryDataArray, 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 b = 0;
if (bitOffset == 0) {
if (dataBuffer instanceof DataBufferByte) {
byte[] data = ((DataBufferByte) dataBuffer).getData();
if (data == binaryDataArray) {
// Optimal case: simply return.
return;
}
int stride = (rectWidth + 7) / 8;
int offset = 0;
for (int y = 0; y < rectHeight; y++) {
System.arraycopy(binaryDataArray, offset, data, eltOffset, stride);
offset += stride;
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 xRemaining = rectWidth;
int i = eltOffset;
while (xRemaining > 8) {
data[i++] = (short) (((binaryDataArray[b++] & 0xFF) << 8) | (binaryDataArray[b++] & 0xFF));
xRemaining -= 16;
}
if (xRemaining > 0) {
data[i++] = (short) ((binaryDataArray[b++] & 0xFF) << 8);
}
eltOffset += lineStride;
}
} else if (dataBuffer instanceof DataBufferInt) {
int[] data = ((DataBufferInt) dataBuffer).getData();
for (int y = 0; y < rectHeight; y++) {
int xRemaining = rectWidth;
int i = eltOffset;
while (xRemaining > 24) {
data[i++] = (int) (((binaryDataArray[b++] & 0xFF) << 24) | ((binaryDataArray[b++] & 0xFF) << 16) | ((binaryDataArray[b++] & 0xFF) << 8) | (binaryDataArray[b++] & 0xFF));
xRemaining -= 32;
}
int shift = 24;
while (xRemaining > 0) {
data[i] |= (int) ((binaryDataArray[b++] & 0xFF) << shift);
shift -= 8;
xRemaining -= 8;
}
eltOffset += lineStride;
}
}
} else {
// bitOffset != 0
int stride = (rectWidth + 7) / 8;
int offset = 0;
if (dataBuffer instanceof DataBufferByte) {
byte[] data = ((DataBufferByte) dataBuffer).getData();
if ((bitOffset & 7) == 0) {
for (int y = 0; y < rectHeight; y++) {
System.arraycopy(binaryDataArray, offset, data, eltOffset, stride);
offset += stride;
eltOffset += lineStride;
}
} else {
// bitOffset % 8 != 0
int rightShift = bitOffset & 7;
int leftShift = 8 - rightShift;
int leftShift8 = 8 + leftShift;
int mask = (byte) (255 << leftShift);
int mask1 = (byte) ~mask;
for (int y = 0; y < rectHeight; y++) {
int i = eltOffset;
int xRemaining = rectWidth;
while (xRemaining > 0) {
byte datum = binaryDataArray[b++];
if (xRemaining > leftShift8) {
// when all the bits in this BYTE will be set
// into the data buffer.
data[i] = (byte) ((data[i] & mask) | ((datum & 0xFF) >>> rightShift));
data[++i] = (byte) ((datum & 0xFF) << leftShift);
} else if (xRemaining > leftShift) {
// All the "leftShift" high bits will be set
// into the data buffer. But not all the
// "rightShift" low bits will be set.
data[i] = (byte) ((data[i] & mask) | ((datum & 0xFF) >>> rightShift));
i++;
data[i] = (byte) ((data[i] & mask1) | ((datum & 0xFF) << leftShift));
} else {
// Less than "leftShift" high bits will be set.
int remainMask = (1 << leftShift - xRemaining) - 1;
data[i] = (byte) ((data[i] & (mask | remainMask)) | (datum & 0xFF) >>> rightShift & ~remainMask);
}
xRemaining -= 8;
}
eltOffset += lineStride;
}
}
} else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
int rightShift = bitOffset & 7;
int leftShift = 8 - rightShift;
int leftShift16 = 16 + leftShift;
int mask = (short) (~(255 << leftShift));
int mask1 = (short) (65535 << leftShift);
int mask2 = (short) ~mask1;
for (int y = 0; y < rectHeight; y++) {
int bOffset = bitOffset;
int xRemaining = rectWidth;
for (int x = 0; x < rectWidth; x += 8, bOffset += 8, xRemaining -= 8) {
int i = eltOffset + (bOffset >> 4);
int mod = bOffset & 15;
int datum = binaryDataArray[b++] & 0xFF;
if (mod <= 8) {
// This BYTE is set into one SHORT
if (xRemaining < 8) {
// Mask the bits to be set.
datum &= 255 << 8 - xRemaining;
}
data[i] = (short) ((data[i] & mask) | (datum << leftShift));
} else if (xRemaining > leftShift16) {
// This BYTE will be set into two SHORTs
data[i] = (short) ((data[i] & mask1) | ((datum >>> rightShift) & 0xFFFF));
data[++i] = (short) ((datum << leftShift) & 0xFFFF);
} else if (xRemaining > leftShift) {
// This BYTE will be set into two SHORTs;
// But not all the low bits will be set into SHORT
data[i] = (short) ((data[i] & mask1) | ((datum >>> rightShift) & 0xFFFF));
i++;
data[i] = (short) ((data[i] & mask2) | ((datum << leftShift) & 0xFFFF));
} else {
// Only some of the high bits will be set into
// SHORTs
int remainMask = (1 << leftShift - xRemaining) - 1;
data[i] = (short) ((data[i] & (mask1 | remainMask)) | ((datum >>> rightShift) & 0xFFFF & ~remainMask));
}
}
eltOffset += lineStride;
}
} else if (dataBuffer instanceof DataBufferInt) {
int[] data = ((DataBufferInt) dataBuffer).getData();
int rightShift = bitOffset & 7;
int leftShift = 8 - rightShift;
int leftShift32 = 32 + leftShift;
int mask = 0xFFFFFFFF << leftShift;
int mask1 = ~mask;
for (int y = 0; y < rectHeight; y++) {
int bOffset = bitOffset;
int xRemaining = rectWidth;
for (int x = 0; x < rectWidth; x += 8, bOffset += 8, xRemaining -= 8) {
int i = eltOffset + (bOffset >> 5);
int mod = bOffset & 31;
int datum = binaryDataArray[b++] & 0xFF;
if (mod <= 24) {
// This BYTE is set into one INT
int shift = 24 - mod;
if (xRemaining < 8) {
// Mask the bits to be set.
datum &= 255 << 8 - xRemaining;
}
data[i] = (data[i] & (~(255 << shift))) | (datum << shift);
} else if (xRemaining > leftShift32) {
// All the bits of this BYTE will be set into two INTs
data[i] = (data[i] & mask) | (datum >>> rightShift);
data[++i] = datum << leftShift;
} else if (xRemaining > leftShift) {
// This BYTE will be set into two INTs;
// But not all the low bits will be set into INT
data[i] = (data[i] & mask) | (datum >>> rightShift);
i++;
data[i] = (data[i] & mask1) | (datum << leftShift);
} else {
// Only some of the high bits will be set into INT
int remainMask = (1 << leftShift - xRemaining) - 1;
data[i] = (data[i] & (mask | remainMask)) | (datum >>> rightShift & ~remainMask);
}
}
eltOffset += lineStride;
}
}
}
}
use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method getPackedBinaryData.
/**
* For the case of binary data (<code>isBinary()</code> returns
* <code>true</code>), return the binary data as a packed byte array.
* The data will be packed as eight bits per byte with no bit offset,
* i.e., the first bit in each image line will be the left-most of the
* first byte of the line. The line stride in bytes will be
* <code>(int)((getWidth()+7)/8)</code>. The length of the returned
* array will be the line stride multiplied by <code>getHeight()</code>
*
* @return the binary data as a packed array of bytes with zero offset
* of <code>null</code> if the data are not binary.
* @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 byte[] getPackedBinaryData(Raster 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 numBytesPerRow = (rectWidth + 7) / 8;
if (dataBuffer instanceof DataBufferByte && eltOffset == 0 && bitOffset == 0 && numBytesPerRow == lineStride && ((DataBufferByte) dataBuffer).getData().length == numBytesPerRow * rectHeight) {
return ((DataBufferByte) dataBuffer).getData();
}
byte[] binaryDataArray = new byte[numBytesPerRow * rectHeight];
int b = 0;
if (bitOffset == 0) {
if (dataBuffer instanceof DataBufferByte) {
byte[] data = ((DataBufferByte) dataBuffer).getData();
int stride = numBytesPerRow;
int offset = 0;
for (int y = 0; y < rectHeight; y++) {
System.arraycopy(data, eltOffset, binaryDataArray, offset, stride);
offset += stride;
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 xRemaining = rectWidth;
int i = eltOffset;
while (xRemaining > 8) {
short datum = data[i++];
binaryDataArray[b++] = (byte) ((datum >>> 8) & 0xFF);
binaryDataArray[b++] = (byte) (datum & 0xFF);
xRemaining -= 16;
}
if (xRemaining > 0) {
binaryDataArray[b++] = (byte) ((data[i] >>> 8) & 0XFF);
}
eltOffset += lineStride;
}
} else if (dataBuffer instanceof DataBufferInt) {
int[] data = ((DataBufferInt) dataBuffer).getData();
for (int y = 0; y < rectHeight; y++) {
int xRemaining = rectWidth;
int i = eltOffset;
while (xRemaining > 24) {
int datum = data[i++];
binaryDataArray[b++] = (byte) ((datum >>> 24) & 0xFF);
binaryDataArray[b++] = (byte) ((datum >>> 16) & 0xFF);
binaryDataArray[b++] = (byte) ((datum >>> 8) & 0xFF);
binaryDataArray[b++] = (byte) (datum & 0xFF);
xRemaining -= 32;
}
int shift = 24;
while (xRemaining > 0) {
binaryDataArray[b++] = (byte) ((data[i] >>> shift) & 0xFF);
shift -= 8;
xRemaining -= 8;
}
eltOffset += lineStride;
}
}
} else {
// bitOffset != 0
if (dataBuffer instanceof DataBufferByte) {
byte[] data = ((DataBufferByte) dataBuffer).getData();
if ((bitOffset & 7) == 0) {
int stride = numBytesPerRow;
int offset = 0;
for (int y = 0; y < rectHeight; y++) {
System.arraycopy(data, eltOffset, binaryDataArray, offset, stride);
offset += stride;
eltOffset += lineStride;
}
} else {
// bitOffset % 8 != 0
int leftShift = bitOffset & 7;
int rightShift = 8 - leftShift;
for (int y = 0; y < rectHeight; y++) {
int i = eltOffset;
int xRemaining = rectWidth;
while (xRemaining > 0) {
if (xRemaining > rightShift) {
binaryDataArray[b++] = (byte) (((data[i++] & 0xFF) << leftShift) | ((data[i] & 0xFF) >>> rightShift));
} else {
binaryDataArray[b++] = (byte) ((data[i] & 0xFF) << leftShift);
}
xRemaining -= 8;
}
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 = bitOffset;
for (int x = 0; x < rectWidth; x += 8, bOffset += 8) {
int i = eltOffset + bOffset / 16;
int mod = bOffset % 16;
int left = data[i] & 0xFFFF;
if (mod <= 8) {
binaryDataArray[b++] = (byte) (left >>> (8 - mod));
} else {
int delta = mod - 8;
int right = data[i + 1] & 0xFFFF;
binaryDataArray[b++] = (byte) ((left << delta) | (right >>> (16 - delta)));
}
}
eltOffset += lineStride;
}
} else if (dataBuffer instanceof DataBufferInt) {
int[] data = ((DataBufferInt) dataBuffer).getData();
for (int y = 0; y < rectHeight; y++) {
int bOffset = bitOffset;
for (int x = 0; x < rectWidth; x += 8, bOffset += 8) {
int i = eltOffset + bOffset / 32;
int mod = bOffset % 32;
int left = data[i];
if (mod <= 24) {
binaryDataArray[b++] = (byte) (left >>> (24 - mod));
} else {
int delta = mod - 24;
int right = data[i + 1];
binaryDataArray[b++] = (byte) ((left << delta) | (right >>> (32 - delta)));
}
}
eltOffset += lineStride;
}
}
}
return binaryDataArray;
}
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;
}
Aggregations