use of java.awt.image.MultiPixelPackedSampleModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method getTileSize.
public static long getTileSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
if (sm instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sm;
return (mppsm.getScanlineStride() * mppsm.getHeight() + (mppsm.getDataBitOffset() + elementSize - 1) / elementSize) * ((elementSize + 7) / 8);
} else if (sm instanceof ComponentSampleModel) {
ComponentSampleModel csm = (ComponentSampleModel) sm;
int[] bandOffsets = csm.getBandOffsets();
int maxBandOff = bandOffsets[0];
for (int i = 1; i < bandOffsets.length; i++) maxBandOff = Math.max(maxBandOff, bandOffsets[i]);
long size = 0;
int pixelStride = csm.getPixelStride();
int scanlineStride = csm.getScanlineStride();
if (maxBandOff >= 0)
size += maxBandOff + 1;
if (pixelStride > 0)
size += pixelStride * (sm.getWidth() - 1);
if (scanlineStride > 0)
size += scanlineStride * (sm.getHeight() - 1);
int[] bankIndices = csm.getBankIndices();
maxBandOff = bankIndices[0];
for (int i = 1; i < bankIndices.length; i++) maxBandOff = Math.max(maxBandOff, bankIndices[i]);
return size * (maxBandOff + 1) * ((elementSize + 7) / 8);
} else if (sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sm;
long size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) + sppsm.getWidth();
return size * ((elementSize + 7) / 8);
}
return 0;
}
use of java.awt.image.MultiPixelPackedSampleModel in project jdk8u_jdk by JetBrains.
the class ImageUtil method createColorModel.
public static ColorModel createColorModel(ColorSpace colorSpace, SampleModel sampleModel) {
ColorModel colorModel = null;
if (sampleModel == null) {
throw new IllegalArgumentException(I18N.getString("ImageUtil1"));
}
int numBands = sampleModel.getNumBands();
if (numBands < 1 || numBands > 4) {
return null;
}
int dataType = sampleModel.getDataType();
if (sampleModel instanceof ComponentSampleModel) {
if (dataType < DataBuffer.TYPE_BYTE || //dataType == DataBuffer.TYPE_SHORT ||
dataType > DataBuffer.TYPE_DOUBLE) {
return null;
}
if (colorSpace == null)
colorSpace = numBands <= 2 ? ColorSpace.getInstance(ColorSpace.CS_GRAY) : ColorSpace.getInstance(ColorSpace.CS_sRGB);
boolean useAlpha = (numBands == 2) || (numBands == 4);
int transparency = useAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
boolean premultiplied = false;
int dataTypeSize = DataBuffer.getDataTypeSize(dataType);
int[] bits = new int[numBands];
for (int i = 0; i < numBands; i++) {
bits[i] = dataTypeSize;
}
colorModel = new ComponentColorModel(colorSpace, bits, useAlpha, premultiplied, transparency, dataType);
} else if (sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
int[] bitMasks = sppsm.getBitMasks();
int rmask = 0;
int gmask = 0;
int bmask = 0;
int amask = 0;
numBands = bitMasks.length;
if (numBands <= 2) {
rmask = gmask = bmask = bitMasks[0];
if (numBands == 2) {
amask = bitMasks[1];
}
} else {
rmask = bitMasks[0];
gmask = bitMasks[1];
bmask = bitMasks[2];
if (numBands == 4) {
amask = bitMasks[3];
}
}
int[] sampleSize = sppsm.getSampleSize();
int bits = 0;
for (int i = 0; i < sampleSize.length; i++) {
bits += sampleSize[i];
}
if (colorSpace == null)
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
colorModel = new DirectColorModel(colorSpace, bits, rmask, gmask, bmask, amask, false, sampleModel.getDataType());
} else if (sampleModel instanceof MultiPixelPackedSampleModel) {
int bits = ((MultiPixelPackedSampleModel) sampleModel).getPixelBitStride();
int size = 1 << bits;
byte[] comp = new byte[size];
for (int i = 0; i < size; i++) comp[i] = (byte) (255 * i / (size - 1));
colorModel = new IndexColorModel(bits, size, comp, comp, comp);
}
return colorModel;
}
use of java.awt.image.MultiPixelPackedSampleModel in project jdk8u_jdk by JetBrains.
the class BMPImageReader method decodeRLE4.
private void decodeRLE4(int imSize, int padding, byte[] values, byte[] bdata) throws IOException {
byte[] val = new byte[width];
int count = 0, l = 0;
int value;
boolean flag = false;
int lineNo = isBottomUp ? height - 1 : 0;
int lineStride = ((MultiPixelPackedSampleModel) sampleModel).getScanlineStride();
int finished = 0;
while (count != imSize) {
value = values[count++] & 0xFF;
if (value == 0) {
// Absolute mode
switch(values[count++] & 0xFF) {
case 0:
// End-of-scanline marker
if (lineNo >= sourceRegion.y && lineNo < sourceRegion.y + sourceRegion.height) {
if (noTransform) {
int pos = lineNo * (width + 1 >> 1);
for (int i = 0, j = 0; i < width >> 1; i++) bdata[pos++] = (byte) ((val[j++] << 4) | val[j++]);
if ((width & 1) == 1)
bdata[pos] |= val[width - 1] << 4;
processImageUpdate(bi, 0, lineNo, destinationRegion.width, 1, 1, 1, new int[] { 0 });
finished++;
} else if ((lineNo - sourceRegion.y) % scaleY == 0) {
int currentLine = (lineNo - sourceRegion.y) / scaleY + destinationRegion.y;
int pos = currentLine * lineStride;
pos += destinationRegion.x >> 1;
int shift = (1 - (destinationRegion.x & 1)) << 2;
for (int i = sourceRegion.x; i < sourceRegion.x + sourceRegion.width; i += scaleX) {
bdata[pos] |= val[i] << shift;
shift += 4;
if (shift == 4) {
pos++;
}
shift &= 7;
}
processImageUpdate(bi, 0, currentLine, destinationRegion.width, 1, 1, 1, new int[] { 0 });
finished++;
}
}
processImageProgress(100.0F * finished / destinationRegion.height);
lineNo += isBottomUp ? -1 : 1;
l = 0;
if (abortRequested()) {
flag = true;
}
break;
case 1:
// End-of-RLE marker
flag = true;
break;
case 2:
// delta or vector marker
int xoff = values[count++] & 0xFF;
int yoff = values[count] & 0xFF;
// Move to the position xoff, yoff down
l += xoff + yoff * width;
break;
default:
int end = values[count - 1] & 0xFF;
for (int i = 0; i < end; i++) {
val[l++] = (byte) (((i & 1) == 0) ? (values[count] & 0xf0) >> 4 : (values[count++] & 0x0f));
}
// increment count, so do it now.
if ((end & 1) == 1) {
count++;
}
// an extra padding byte will be present, so skip that.
if ((((int) Math.ceil(end / 2)) & 1) == 1) {
count++;
}
break;
}
} else {
// Encoded mode
int[] alternate = { (values[count] & 0xf0) >> 4, values[count] & 0x0f };
for (int i = 0; (i < value) && (l < width); i++) {
val[l++] = (byte) alternate[i & 1];
}
count++;
}
// If End-of-RLE data, then exit the while loop
if (flag) {
break;
}
}
}
use of java.awt.image.MultiPixelPackedSampleModel in project jdk8u_jdk by JetBrains.
the class BMPImageReader method readHeader.
/**
* Process the image header.
*
* @exception IllegalStateException if source stream is not set.
*
* @exception IOException if image stream is corrupted.
*
* @exception IllegalArgumentException if the image stream does not contain
* a BMP image, or if a sample model instance to describe the
* image can not be created.
*/
protected void readHeader() throws IOException, IllegalArgumentException {
if (gotHeader)
return;
if (iis == null) {
throw new IllegalStateException("Input source not set!");
}
int profileData = 0, profileSize = 0;
this.metadata = new BMPMetadata();
iis.mark();
// read and check the magic marker
byte[] marker = new byte[2];
iis.read(marker);
if (marker[0] != 0x42 || marker[1] != 0x4d)
throw new IllegalArgumentException(I18N.getString("BMPImageReader1"));
// Read file size
bitmapFileSize = iis.readUnsignedInt();
// skip the two reserved fields
iis.skipBytes(4);
// Offset to the bitmap from the beginning
bitmapOffset = iis.readUnsignedInt();
// End File Header
// Start BitmapCoreHeader
long size = iis.readUnsignedInt();
if (size == 12) {
width = iis.readShort();
height = iis.readShort();
} else {
width = iis.readInt();
height = iis.readInt();
}
metadata.width = width;
metadata.height = height;
int planes = iis.readUnsignedShort();
bitsPerPixel = iis.readUnsignedShort();
//metadata.colorPlane = planes;
metadata.bitsPerPixel = (short) bitsPerPixel;
// As BMP always has 3 rgb bands, except for Version 5,
// which is bgra
numBands = 3;
if (size == 12) {
// Windows 2.x and OS/2 1.x
metadata.bmpVersion = VERSION_2;
// Classify the image type
if (bitsPerPixel == 1) {
imageType = VERSION_2_1_BIT;
} else if (bitsPerPixel == 4) {
imageType = VERSION_2_4_BIT;
} else if (bitsPerPixel == 8) {
imageType = VERSION_2_8_BIT;
} else if (bitsPerPixel == 24) {
imageType = VERSION_2_24_BIT;
}
// Read in the palette
int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 3);
int sizeOfPalette = numberOfEntries * 3;
palette = new byte[sizeOfPalette];
iis.readFully(palette, 0, sizeOfPalette);
metadata.palette = palette;
metadata.paletteSize = numberOfEntries;
} else {
compression = iis.readUnsignedInt();
imageSize = iis.readUnsignedInt();
long xPelsPerMeter = iis.readInt();
long yPelsPerMeter = iis.readInt();
long colorsUsed = iis.readUnsignedInt();
long colorsImportant = iis.readUnsignedInt();
metadata.compression = (int) compression;
metadata.xPixelsPerMeter = (int) xPelsPerMeter;
metadata.yPixelsPerMeter = (int) yPelsPerMeter;
metadata.colorsUsed = (int) colorsUsed;
metadata.colorsImportant = (int) colorsImportant;
if (size == 40) {
// Windows 3.x and Windows NT
switch((int) compression) {
case BI_JPEG:
case BI_PNG:
metadata.bmpVersion = VERSION_3;
imageType = VERSION_3_XP_EMBEDDED;
break;
// No compression
case BI_RGB:
// 8-bit RLE compression
case BI_RLE8:
case // 4-bit RLE compression
BI_RLE4:
// Read in the palette
if (bitmapOffset < (size + 14)) {
throw new IIOException(I18N.getString("BMPImageReader7"));
}
int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
int sizeOfPalette = numberOfEntries * 4;
palette = new byte[sizeOfPalette];
iis.readFully(palette, 0, sizeOfPalette);
metadata.palette = palette;
metadata.paletteSize = numberOfEntries;
if (bitsPerPixel == 1) {
imageType = VERSION_3_1_BIT;
} else if (bitsPerPixel == 4) {
imageType = VERSION_3_4_BIT;
} else if (bitsPerPixel == 8) {
imageType = VERSION_3_8_BIT;
} else if (bitsPerPixel == 24) {
imageType = VERSION_3_24_BIT;
} else if (bitsPerPixel == 16) {
imageType = VERSION_3_NT_16_BIT;
redMask = 0x7C00;
greenMask = 0x3E0;
// 0x1F;
blueMask = (1 << 5) - 1;
metadata.redMask = redMask;
metadata.greenMask = greenMask;
metadata.blueMask = blueMask;
} else if (bitsPerPixel == 32) {
imageType = VERSION_3_NT_32_BIT;
redMask = 0x00FF0000;
greenMask = 0x0000FF00;
blueMask = 0x000000FF;
metadata.redMask = redMask;
metadata.greenMask = greenMask;
metadata.blueMask = blueMask;
}
metadata.bmpVersion = VERSION_3;
break;
case BI_BITFIELDS:
if (bitsPerPixel == 16) {
imageType = VERSION_3_NT_16_BIT;
} else if (bitsPerPixel == 32) {
imageType = VERSION_3_NT_32_BIT;
}
// BitsField encoding
redMask = (int) iis.readUnsignedInt();
greenMask = (int) iis.readUnsignedInt();
blueMask = (int) iis.readUnsignedInt();
metadata.redMask = redMask;
metadata.greenMask = greenMask;
metadata.blueMask = blueMask;
if (colorsUsed != 0) {
// there is a palette
sizeOfPalette = (int) colorsUsed * 4;
palette = new byte[sizeOfPalette];
iis.readFully(palette, 0, sizeOfPalette);
metadata.palette = palette;
metadata.paletteSize = (int) colorsUsed;
}
metadata.bmpVersion = VERSION_3_NT;
break;
default:
throw new IIOException(I18N.getString("BMPImageReader2"));
}
} else if (size == 108 || size == 124) {
// Windows 4.x BMP
if (size == 108)
metadata.bmpVersion = VERSION_4;
else if (size == 124)
metadata.bmpVersion = VERSION_5;
// rgb masks, valid only if comp is BI_BITFIELDS
redMask = (int) iis.readUnsignedInt();
greenMask = (int) iis.readUnsignedInt();
blueMask = (int) iis.readUnsignedInt();
// Only supported for 32bpp BI_RGB argb
alphaMask = (int) iis.readUnsignedInt();
long csType = iis.readUnsignedInt();
int redX = iis.readInt();
int redY = iis.readInt();
int redZ = iis.readInt();
int greenX = iis.readInt();
int greenY = iis.readInt();
int greenZ = iis.readInt();
int blueX = iis.readInt();
int blueY = iis.readInt();
int blueZ = iis.readInt();
long gammaRed = iis.readUnsignedInt();
long gammaGreen = iis.readUnsignedInt();
long gammaBlue = iis.readUnsignedInt();
if (size == 124) {
metadata.intent = iis.readInt();
profileData = iis.readInt();
profileSize = iis.readInt();
iis.skipBytes(4);
}
metadata.colorSpace = (int) csType;
if (csType == LCS_CALIBRATED_RGB) {
// All the new fields are valid only for this case
metadata.redX = redX;
metadata.redY = redY;
metadata.redZ = redZ;
metadata.greenX = greenX;
metadata.greenY = greenY;
metadata.greenZ = greenZ;
metadata.blueX = blueX;
metadata.blueY = blueY;
metadata.blueZ = blueZ;
metadata.gammaRed = (int) gammaRed;
metadata.gammaGreen = (int) gammaGreen;
metadata.gammaBlue = (int) gammaBlue;
}
// Read in the palette
int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
int sizeOfPalette = numberOfEntries * 4;
palette = new byte[sizeOfPalette];
iis.readFully(palette, 0, sizeOfPalette);
metadata.palette = palette;
metadata.paletteSize = numberOfEntries;
switch((int) compression) {
case BI_JPEG:
case BI_PNG:
if (size == 108) {
imageType = VERSION_4_XP_EMBEDDED;
} else if (size == 124) {
imageType = VERSION_5_XP_EMBEDDED;
}
break;
default:
if (bitsPerPixel == 1) {
imageType = VERSION_4_1_BIT;
} else if (bitsPerPixel == 4) {
imageType = VERSION_4_4_BIT;
} else if (bitsPerPixel == 8) {
imageType = VERSION_4_8_BIT;
} else if (bitsPerPixel == 16) {
imageType = VERSION_4_16_BIT;
if ((int) compression == BI_RGB) {
redMask = 0x7C00;
greenMask = 0x3E0;
blueMask = 0x1F;
}
} else if (bitsPerPixel == 24) {
imageType = VERSION_4_24_BIT;
} else if (bitsPerPixel == 32) {
imageType = VERSION_4_32_BIT;
if ((int) compression == BI_RGB) {
redMask = 0x00FF0000;
greenMask = 0x0000FF00;
blueMask = 0x000000FF;
}
}
metadata.redMask = redMask;
metadata.greenMask = greenMask;
metadata.blueMask = blueMask;
metadata.alphaMask = alphaMask;
}
} else {
throw new IIOException(I18N.getString("BMPImageReader3"));
}
}
if (height > 0) {
// bottom up image
isBottomUp = true;
} else {
// top down image
isBottomUp = false;
height = Math.abs(height);
}
// Reset Image Layout so there's only one tile.
//Define the color space
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
if (metadata.colorSpace == PROFILE_LINKED || metadata.colorSpace == PROFILE_EMBEDDED) {
iis.mark();
iis.skipBytes(profileData - size);
byte[] profile = new byte[profileSize];
iis.readFully(profile, 0, profileSize);
iis.reset();
try {
if (metadata.colorSpace == PROFILE_LINKED && isLinkedProfileAllowed() && !isUncOrDevicePath(profile)) {
String path = new String(profile, "windows-1252");
colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(path));
} else {
colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(profile));
}
} catch (Exception e) {
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
}
if (bitsPerPixel == 0 || compression == BI_JPEG || compression == BI_PNG) {
// the colorModel and sampleModel will be initialzed
// by the reader of embedded image
colorModel = null;
sampleModel = null;
} else if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
// When number of bitsPerPixel is <= 8, we use IndexColorModel.
numBands = 1;
if (bitsPerPixel == 8) {
int[] bandOffsets = new int[numBands];
for (int i = 0; i < numBands; i++) {
bandOffsets[i] = numBands - 1 - i;
}
sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
} else {
// 1 and 4 bit pixels can be stored in a packed format.
sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerPixel);
}
// Create IndexColorModel from the palette.
byte[] r, g, b;
if (imageType == VERSION_2_1_BIT || imageType == VERSION_2_4_BIT || imageType == VERSION_2_8_BIT) {
size = palette.length / 3;
if (size > 256) {
size = 256;
}
int off;
r = new byte[(int) size];
g = new byte[(int) size];
b = new byte[(int) size];
for (int i = 0; i < (int) size; i++) {
off = 3 * i;
b[i] = palette[off];
g[i] = palette[off + 1];
r[i] = palette[off + 2];
}
} else {
size = palette.length / 4;
if (size > 256) {
size = 256;
}
int off;
r = new byte[(int) size];
g = new byte[(int) size];
b = new byte[(int) size];
for (int i = 0; i < size; i++) {
off = 4 * i;
b[i] = palette[off];
g[i] = palette[off + 1];
r[i] = palette[off + 2];
}
}
if (ImageUtil.isIndicesForGrayscale(r, g, b))
colorModel = ImageUtil.createColorModel(null, sampleModel);
else
colorModel = new IndexColorModel(bitsPerPixel, (int) size, r, g, b);
} else if (bitsPerPixel == 16) {
numBands = 3;
sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, width, height, new int[] { redMask, greenMask, blueMask });
colorModel = new DirectColorModel(colorSpace, 16, redMask, greenMask, blueMask, 0, false, DataBuffer.TYPE_USHORT);
} else if (bitsPerPixel == 32) {
numBands = alphaMask == 0 ? 3 : 4;
// The number of bands in the SampleModel is determined by
// the length of the mask array passed in.
int[] bitMasks = numBands == 3 ? new int[] { redMask, greenMask, blueMask } : new int[] { redMask, greenMask, blueMask, alphaMask };
sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, bitMasks);
colorModel = new DirectColorModel(colorSpace, 32, redMask, greenMask, blueMask, alphaMask, false, DataBuffer.TYPE_INT);
} else {
numBands = 3;
// Create SampleModel
int[] bandOffsets = new int[numBands];
for (int i = 0; i < numBands; i++) {
bandOffsets[i] = numBands - 1 - i;
}
sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
colorModel = ImageUtil.createColorModel(colorSpace, sampleModel);
}
originalSampleModel = sampleModel;
originalColorModel = colorModel;
// Reset to the start of bitmap; then jump to the
//start of image data
iis.reset();
iis.skipBytes(bitmapOffset);
gotHeader = true;
}
use of java.awt.image.MultiPixelPackedSampleModel in project jdk8u_jdk by JetBrains.
the class BMPImageReader method read1Bit.
// Deal with 1 Bit images using IndexColorModels
private void read1Bit(byte[] bdata) throws IOException {
int bytesPerScanline = (width + 7) / 8;
int padding = bytesPerScanline % 4;
if (padding != 0) {
padding = 4 - padding;
}
int lineLength = bytesPerScanline + padding;
if (noTransform) {
int j = isBottomUp ? (height - 1) * bytesPerScanline : 0;
for (int i = 0; i < height; i++) {
if (abortRequested()) {
break;
}
iis.readFully(bdata, j, bytesPerScanline);
iis.skipBytes(padding);
j += isBottomUp ? -bytesPerScanline : bytesPerScanline;
processImageUpdate(bi, 0, i, destinationRegion.width, 1, 1, 1, new int[] { 0 });
processImageProgress(100.0F * i / destinationRegion.height);
}
} else {
byte[] buf = new byte[lineLength];
int lineStride = ((MultiPixelPackedSampleModel) sampleModel).getScanlineStride();
if (isBottomUp) {
int lastLine = sourceRegion.y + (destinationRegion.height - 1) * scaleY;
iis.skipBytes(lineLength * (height - 1 - lastLine));
} else
iis.skipBytes(lineLength * sourceRegion.y);
int skipLength = lineLength * (scaleY - 1);
// cache the values to avoid duplicated computation
int[] srcOff = new int[destinationRegion.width];
int[] destOff = new int[destinationRegion.width];
int[] srcPos = new int[destinationRegion.width];
int[] destPos = new int[destinationRegion.width];
for (int i = destinationRegion.x, x = sourceRegion.x, j = 0; i < destinationRegion.x + destinationRegion.width; i++, j++, x += scaleX) {
srcPos[j] = x >> 3;
srcOff[j] = 7 - (x & 7);
destPos[j] = i >> 3;
destOff[j] = 7 - (i & 7);
}
int k = destinationRegion.y * lineStride;
if (isBottomUp)
k += (destinationRegion.height - 1) * lineStride;
for (int j = 0, y = sourceRegion.y; j < destinationRegion.height; j++, y += scaleY) {
if (abortRequested())
break;
iis.read(buf, 0, lineLength);
for (int i = 0; i < destinationRegion.width; i++) {
//get the bit and assign to the data buffer of the raster
int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
bdata[k + destPos[i]] |= v << destOff[i];
}
k += isBottomUp ? -lineStride : lineStride;
iis.skipBytes(skipLength);
processImageUpdate(bi, 0, j, destinationRegion.width, 1, 1, 1, new int[] { 0 });
processImageProgress(100.0F * j / destinationRegion.height);
}
}
}
Aggregations