use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.
the class ImageRepresentation method setPixels.
public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pix, int off, int scansize) {
int lineOff = off;
int poff;
if (src != null) {
src.checkSecurity(null, false);
}
// REMIND: What if the model doesn't fit in default color model?
synchronized (this) {
if (bimage == null) {
if (cmodel == null) {
cmodel = model;
}
createBufferedImage();
}
int[] storage = new int[w];
int yoff;
int pixel;
if (cmodel instanceof IndexColorModel) {
// REMIND: Right now we don't support writing back into ICM
// images.
convertToRGB();
}
if ((model == cmodel) && (biRaster instanceof IntegerComponentRaster)) {
IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;
if (off == 0 && scansize == w) {
iraster.setDataElements(x, y, w, h, pix);
} else {
// Need to pack the data
for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
System.arraycopy(pix, lineOff, storage, 0, w);
iraster.setDataElements(x, yoff, w, 1, storage);
}
}
} else {
if (model.getTransparency() != model.OPAQUE && cmodel.getTransparency() == cmodel.OPAQUE) {
convertToRGB();
}
if (isDefaultBI) {
IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;
int[] data = iraster.getDataStorage();
if (cmodel.equals(model)) {
int sstride = iraster.getScanlineStride();
int doff = y * sstride + x;
for (yoff = 0; yoff < h; yoff++, lineOff += scansize) {
System.arraycopy(pix, lineOff, data, doff, w);
doff += sstride;
}
// Note: manual modification of pixels, mark the
// raster as changed
iraster.markDirty();
} else {
for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
poff = lineOff;
for (int i = 0; i < w; i++) {
storage[i] = model.getRGB(pix[poff++]);
}
iraster.setDataElements(x, yoff, w, 1, storage);
}
}
availinfo |= ImageObserver.SOMEBITS;
} else {
Object tmp = null;
for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
poff = lineOff;
for (int xoff = x; xoff < x + w; xoff++) {
pixel = model.getRGB(pix[poff++]);
tmp = cmodel.getDataElements(pixel, tmp);
biRaster.setDataElements(xoff, yoff, tmp);
}
}
availinfo |= ImageObserver.SOMEBITS;
}
}
}
// the region
if (((availinfo & ImageObserver.FRAMEBITS) == 0)) {
newInfo(image, ImageObserver.SOMEBITS, x, y, w, h);
}
}
use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.
the class BufferedBufImgOps method isLookupOpValid.
/**************************** LookupOp support ******************************/
public static boolean isLookupOpValid(LookupOp lop, BufferedImage srcImg) {
LookupTable table = lop.getTable();
int numComps = table.getNumComponents();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new IllegalArgumentException("LookupOp cannot be " + "performed on an indexed image");
}
if (numComps != 1 && numComps != srcCM.getNumComponents() && numComps != srcCM.getNumColorComponents()) {
throw new IllegalArgumentException("Number of arrays in the " + " lookup table (" + numComps + ") is not compatible with" + " the src image: " + srcImg);
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB && csType != ColorSpace.TYPE_GRAY) {
// Not prepared to deal with other color spaces
return false;
}
if (numComps == 2 || numComps > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
// these restrictions here.
if (table instanceof ByteLookupTable) {
byte[][] data = ((ByteLookupTable) table).getTable();
for (int i = 1; i < data.length; i++) {
if (data[i].length > 256 || data[i].length != data[i - 1].length) {
return false;
}
}
} else if (table instanceof ShortLookupTable) {
short[][] data = ((ShortLookupTable) table).getTable();
for (int i = 1; i < data.length; i++) {
if (data[i].length > 256 || data[i].length != data[i - 1].length) {
return false;
}
}
} else {
return false;
}
return true;
}
use of java.awt.image.IndexColorModel 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.IndexColorModel in project jdk8u_jdk by JetBrains.
the class PNGImageWriter method encodePass.
private void encodePass(ImageOutputStream os, RenderedImage image, int xOffset, int yOffset, int xSkip, int ySkip) throws IOException {
int minX = sourceXOffset;
int minY = sourceYOffset;
int width = sourceWidth;
int height = sourceHeight;
// Adjust offsets and skips based on source subsampling factors
xOffset *= periodX;
xSkip *= periodX;
yOffset *= periodY;
ySkip *= periodY;
// Early exit if no data for this pass
int hpixels = (width - xOffset + xSkip - 1) / xSkip;
int vpixels = (height - yOffset + ySkip - 1) / ySkip;
if (hpixels == 0 || vpixels == 0) {
return;
}
// Convert X offset and skip from pixels to samples
xOffset *= numBands;
xSkip *= numBands;
// Create row buffers
int samplesPerByte = 8 / metadata.IHDR_bitDepth;
int numSamples = width * numBands;
int[] samples = new int[numSamples];
int bytesPerRow = hpixels * numBands;
if (metadata.IHDR_bitDepth < 8) {
bytesPerRow = (bytesPerRow + samplesPerByte - 1) / samplesPerByte;
} else if (metadata.IHDR_bitDepth == 16) {
bytesPerRow *= 2;
}
IndexColorModel icm_gray_alpha = null;
if (metadata.IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA && image.getColorModel() instanceof IndexColorModel) {
// reserve space for alpha samples
bytesPerRow *= 2;
// will be used to calculate alpha value for the pixel
icm_gray_alpha = (IndexColorModel) image.getColorModel();
}
currRow = new byte[bytesPerRow + bpp];
prevRow = new byte[bytesPerRow + bpp];
filteredRows = new byte[5][bytesPerRow + bpp];
int bitDepth = metadata.IHDR_bitDepth;
for (int row = minY + yOffset; row < minY + height; row += ySkip) {
Rectangle rect = new Rectangle(minX, row, width, 1);
Raster ras = image.getData(rect);
if (sourceBands != null) {
ras = ras.createChild(minX, row, width, 1, minX, row, sourceBands);
}
ras.getPixels(minX, row, width, 1, samples);
if (image.getColorModel().isAlphaPremultiplied()) {
WritableRaster wr = ras.createCompatibleWritableRaster();
wr.setPixels(wr.getMinX(), wr.getMinY(), wr.getWidth(), wr.getHeight(), samples);
image.getColorModel().coerceData(wr, false);
wr.getPixels(wr.getMinX(), wr.getMinY(), wr.getWidth(), wr.getHeight(), samples);
}
// Reorder palette data if necessary
int[] paletteOrder = metadata.PLTE_order;
if (paletteOrder != null) {
for (int i = 0; i < numSamples; i++) {
samples[i] = paletteOrder[samples[i]];
}
}
// leave first 'bpp' bytes zero
int count = bpp;
int pos = 0;
int tmp = 0;
switch(bitDepth) {
case 1:
case 2:
case 4:
// Image can only have a single band
int mask = samplesPerByte - 1;
for (int s = xOffset; s < numSamples; s += xSkip) {
byte val = scale0[samples[s]];
tmp = (tmp << bitDepth) | val;
if ((pos++ & mask) == mask) {
currRow[count++] = (byte) tmp;
tmp = 0;
pos = 0;
}
}
// Left shift the last byte
if ((pos & mask) != 0) {
tmp <<= ((8 / bitDepth) - pos) * bitDepth;
currRow[count++] = (byte) tmp;
}
break;
case 8:
if (numBands == 1) {
for (int s = xOffset; s < numSamples; s += xSkip) {
currRow[count++] = scale0[samples[s]];
if (icm_gray_alpha != null) {
currRow[count++] = scale0[icm_gray_alpha.getAlpha(0xff & samples[s])];
}
}
} else {
for (int s = xOffset; s < numSamples; s += xSkip) {
for (int b = 0; b < numBands; b++) {
currRow[count++] = scale[b][samples[s + b]];
}
}
}
break;
case 16:
for (int s = xOffset; s < numSamples; s += xSkip) {
for (int b = 0; b < numBands; b++) {
currRow[count++] = scaleh[b][samples[s + b]];
currRow[count++] = scalel[b][samples[s + b]];
}
}
break;
}
// Perform filtering
int filterType = rowFilter.filterRow(metadata.IHDR_colorType, currRow, prevRow, filteredRows, bytesPerRow, bpp);
os.write(filterType);
os.write(filteredRows[filterType], bpp, bytesPerRow);
// Swap current and previous rows
byte[] swap = currRow;
currRow = prevRow;
prevRow = swap;
pixelsDone += hpixels;
processImageProgress(100.0F * pixelsDone / totalPixels);
// processWriteAborted will be called later
if (abortRequested()) {
return;
}
}
}
use of java.awt.image.IndexColorModel in project vcell by virtualcell.
the class ROIMultiPaintManager method getContrastIndexColorModel.
public static IndexColorModel getContrastIndexColorModel() {
if (indexColorModel == null) {
int[] cmap = new int[256];
for (int i = 0; i < OverlayEditorPanelJAI.CONTRAST_COLORS.length; i += 1) {
cmap[i] = OverlayEditorPanelJAI.CONTRAST_COLORS[i].getRGB();
if (i == 0) {
cmap[i] = new Color(0, 0, 0, 0).getRGB();
}
}
indexColorModel = new java.awt.image.IndexColorModel(8, cmap.length, cmap, 0, false, /*false means NOT USE alpha*/
-1, /*NO transparent single pixel*/
java.awt.image.DataBuffer.TYPE_BYTE);
}
return indexColorModel;
}
Aggregations