IndexColorModelpublic class IndexColorModel extends ColorModel The Class IndexColorModel represents a color model in which the color values
of the pixels are read from a palette. |
Fields Summary |
---|
private int[] | colorMapThe color map. | private int | mapSizeThe map size. | private int | transparentIndexThe transparent index. | private boolean | grayPaletteThe gray palette. | private BigInteger | validBitsThe valid bits. | private static final int | CACHESIZEThe Constant CACHESIZE. | private final int[] | cachetableThe cachetable. | private int | nextInsertIdxThe next insert idx. | private int | totalInsertedThe total inserted. |
Constructors Summary |
---|
public IndexColorModel(int bits, int size, int[] cmap, int start, int transferType, BigInteger validBits)Instantiates a new index color model. // Number of inserted values into Cache table
super(bits, IndexColorModel.createBits(true), ColorSpace.getInstance(ColorSpace.CS_sRGB),
true, false, Transparency.OPAQUE, validateTransferType(transferType));
if (size < 1) {
// awt.264=Size of the color map is less than 1
throw new IllegalArgumentException(Messages.getString("awt.264")); //$NON-NLS-1$
}
mapSize = size;
colorMap = new int[mapSize];
transparentIndex = -1;
if (validBits != null) {
for (int i = 0; i < mapSize; i++) {
if (!validBits.testBit(i)) {
this.validBits = validBits;
}
break;
}
}
transparency = Transparency.OPAQUE;
int alphaMask = 0xff000000;
int alpha = 0;
for (int i = 0; i < mapSize; i++, start++) {
colorMap[i] = cmap[start];
alpha = cmap[start] & alphaMask;
if (alpha == alphaMask) {
continue;
}
if (alpha == 0) {
if (transparentIndex < 0) {
transparentIndex = i;
}
if (transparency == Transparency.OPAQUE) {
transparency = Transparency.BITMASK;
}
} else if (alpha != alphaMask && transparency != Transparency.TRANSLUCENT) {
transparency = Transparency.TRANSLUCENT;
}
}
checkPalette();
| public IndexColorModel(int bits, int size, int[] cmap, int start, boolean hasalpha, int trans, int transferType)Instantiates a new index color model.
super(bits, IndexColorModel.createBits(hasalpha || (trans >= 0)), ColorSpace
.getInstance(ColorSpace.CS_sRGB), (hasalpha || (trans >= 0)), false,
Transparency.OPAQUE, validateTransferType(transferType));
if (size < 1) {
// awt.264=Size of the color map is less than 1
throw new IllegalArgumentException(Messages.getString("awt.264")); //$NON-NLS-1$
}
mapSize = size;
colorMap = new int[mapSize];
if (trans >= 0 && trans < mapSize) {
transparentIndex = trans;
transparency = Transparency.BITMASK;
} else {
transparentIndex = -1;
transparency = Transparency.OPAQUE;
}
int alphaMask = 0xff000000;
int alpha = 0;
for (int i = 0; i < mapSize; i++, start++) {
if (transparentIndex == i) {
colorMap[i] = cmap[start] & 0x00ffffff;
continue;
}
if (hasalpha) {
alpha = cmap[start] & alphaMask;
colorMap[i] = cmap[start];
if (alpha == alphaMask) {
continue;
}
if (alpha == 0) {
if (trans < 0) {
trans = i;
}
if (transparency == Transparency.OPAQUE) {
transparency = Transparency.BITMASK;
}
} else if (alpha != 0 && transparency != Transparency.TRANSLUCENT) {
transparency = Transparency.TRANSLUCENT;
}
} else {
colorMap[i] = alphaMask | cmap[start];
}
}
checkPalette();
| public IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b, byte[] a)Instantiates a new index color model by building the color map from
arrays of red, green, blue, and alpha values.
super(bits, IndexColorModel.createBits(true), ColorSpace.getInstance(ColorSpace.CS_sRGB),
true, false, Transparency.OPAQUE, validateTransferType(ColorModel
.getTransferType(bits)));
createColorMap(size, r, g, b, a, -1);
checkPalette();
| public IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b, int trans)Instantiates a new index color model by building the color map from
arrays of red, green, and blue values.
super(bits, IndexColorModel.createBits((trans >= 0)), ColorSpace
.getInstance(ColorSpace.CS_sRGB), (trans >= 0), false, Transparency.OPAQUE,
validateTransferType(ColorModel.getTransferType(bits)));
createColorMap(size, r, g, b, null, trans);
checkPalette();
| public IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b)Instantiates a new index color model by building the color map from
arrays of red, green, and blue values.
super(bits, IndexColorModel.createBits(false), ColorSpace.getInstance(ColorSpace.CS_sRGB),
false, false, Transparency.OPAQUE, validateTransferType(ColorModel
.getTransferType(bits)));
createColorMap(size, r, g, b, null, -1);
checkPalette();
| public IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasalpha, int trans)Instantiates a new index color model.
super(bits, IndexColorModel.createBits(hasalpha || (trans >= 0)), ColorSpace
.getInstance(ColorSpace.CS_sRGB), (hasalpha || (trans >= 0)), false,
Transparency.OPAQUE, validateTransferType(ColorModel.getTransferType(bits)));
if (size < 1) {
// awt.264=Size of the color map is less than 1
throw new IllegalArgumentException(Messages.getString("awt.264")); //$NON-NLS-1$
}
mapSize = size;
colorMap = new int[mapSize];
transparentIndex = -1;
transparency = Transparency.OPAQUE;
int alpha = 0xff000000;
for (int i = 0; i < mapSize; i++) {
colorMap[i] = (cmap[start++] & 0xff) << 16 | (cmap[start++] & 0xff) << 8
| (cmap[start++] & 0xff);
if (trans == i) {
if (transparency == Transparency.OPAQUE) {
transparency = Transparency.BITMASK;
}
if (hasalpha) {
start++;
}
continue;
}
if (hasalpha) {
alpha = cmap[start++] & 0xff;
if (alpha == 0) {
if (transparency == Transparency.OPAQUE) {
transparency = Transparency.BITMASK;
if (trans < 0) {
trans = i;
}
}
} else {
if (alpha != 0xff && transparency != Transparency.TRANSLUCENT) {
transparency = Transparency.TRANSLUCENT;
}
}
alpha <<= 24;
}
colorMap[i] |= alpha;
}
if (trans >= 0 && trans < mapSize) {
transparentIndex = trans;
}
checkPalette();
| public IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasalpha)Instantiates a new index color model.
this(bits, size, cmap, start, hasalpha, -1);
|
Methods Summary |
---|
private void | checkPalette()This method checking, if Color Map has Gray palette.
grayPalette = false;
if (transparency > Transparency.OPAQUE) {
return;
}
int rgb = 0;
for (int i = 0; i < mapSize; i++) {
rgb = colorMap[i];
if (((rgb >> 16) & 0xff) != ((rgb >> 8) & 0xff) || ((rgb >> 8) & 0xff) != (rgb & 0xff)) {
return;
}
}
grayPalette = true;
| public java.awt.image.BufferedImage | convertToIntDiscrete(java.awt.image.Raster raster, boolean forceARGB)Converts an image from indexed to RGB format.
if (!isCompatibleRaster(raster)) {
// awt.265=The raster argument is not compatible with this
// IndexColorModel
throw new IllegalArgumentException(Messages.getString("awt.265")); //$NON-NLS-1$
}
ColorModel model;
if (forceARGB || transparency == Transparency.TRANSLUCENT) {
model = ColorModel.getRGBdefault();
} else if (transparency == Transparency.BITMASK) {
model = new DirectColorModel(25, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x01000000);
} else {
model = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
}
int w = raster.getWidth();
int h = raster.getHeight();
WritableRaster distRaster = model.createCompatibleWritableRaster(w, h);
int minX = raster.getMinX();
int minY = raster.getMinY();
Object obj = null;
int pixels[] = null;
for (int i = 0; i < h; i++, minY++) {
obj = raster.getDataElements(minX, minY, w, 1, obj);
if (obj instanceof byte[]) {
byte ba[] = (byte[])obj;
if (pixels == null) {
pixels = new int[ba.length];
}
for (int j = 0; j < ba.length; j++) {
pixels[j] = colorMap[ba[j] & 0xff];
}
} else if (obj instanceof short[]) {
short sa[] = (short[])obj;
if (pixels == null) {
pixels = new int[sa.length];
}
for (int j = 0; j < sa.length; j++) {
pixels[j] = colorMap[sa[j] & 0xffff];
}
}
if (obj instanceof int[]) {
int ia[] = (int[])obj;
if (pixels == null) {
pixels = new int[ia.length];
}
for (int j = 0; j < ia.length; j++) {
pixels[j] = colorMap[ia[j]];
}
}
distRaster.setDataElements(0, i, w, 1, pixels);
}
return new BufferedImage(model, distRaster, false, null);
| private static int[] | createBits(boolean hasAlpha)Creates the bits.
int numChannels;
if (hasAlpha) {
numChannels = 4;
} else {
numChannels = 3;
}
int bits[] = new int[numChannels];
for (int i = 0; i < numChannels; i++) {
bits[i] = 8;
}
return bits;
| private void | createColorMap(int size, byte[] r, byte[] g, byte[] b, byte[] a, int trans)Creates the color map.
if (size < 1) {
// awt.264=Size of the color map is less than 1
throw new IllegalArgumentException(Messages.getString("awt.264")); //$NON-NLS-1$
}
mapSize = size;
colorMap = new int[mapSize];
if (trans >= 0 && trans < mapSize) {
transparency = Transparency.BITMASK;
transparentIndex = trans;
} else {
transparency = Transparency.OPAQUE;
transparentIndex = -1;
}
int alpha = 0;
for (int i = 0; i < mapSize; i++) {
colorMap[i] = ((r[i] & 0xff) << 16) | ((g[i] & 0xff) << 8) | (b[i] & 0xff);
if (trans == i) {
continue;
}
if (a == null) {
colorMap[i] |= 0xff000000;
} else {
alpha = a[i] & 0xff;
if (alpha == 0xff) {
colorMap[i] |= 0xff000000;
} else if (alpha == 0) {
if (transparency == Transparency.OPAQUE) {
transparency = Transparency.BITMASK;
}
if (transparentIndex < 0) {
transparentIndex = i;
}
} else {
colorMap[i] |= (a[i] & 0xff) << 24;
if (transparency != Transparency.TRANSLUCENT) {
transparency = Transparency.TRANSLUCENT;
}
}
}
}
| public java.awt.image.SampleModel | createCompatibleSampleModel(int w, int h)
if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) {
return new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, pixel_bits);
}
int bandOffsets[] = new int[1];
bandOffsets[0] = 0;
return new ComponentSampleModel(transferType, w, h, 1, w, bandOffsets);
| public java.awt.image.WritableRaster | createCompatibleWritableRaster(int w, int h)
WritableRaster raster;
if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) {
raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, w, h, 1, pixel_bits, null);
} else if (pixel_bits <= 8) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, 1, null);
} else if (pixel_bits <= 16) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT, w, h, 1, null);
} else {
// awt.266=The number of bits in a pixel is greater than 16
throw new UnsupportedOperationException(Messages.getString("awt.266")); //$NON-NLS-1$
}
return raster;
| private java.lang.Object | createDataObject(int colorMapIdx, java.lang.Object pixel)Construction an array pixel representation.
if (pixel == null) {
switch (transferType) {
case DataBuffer.TYPE_BYTE:
byte[] ba = new byte[1];
ba[0] = (byte)colorMapIdx;
pixel = ba;
break;
case DataBuffer.TYPE_USHORT:
short[] sa = new short[1];
sa[0] = (short)colorMapIdx;
pixel = sa;
break;
default:
// awt.267=The transferType is invalid
throw new UnsupportedOperationException(Messages.getString("awt.267")); //$NON-NLS-1$
}
} else if (pixel instanceof byte[] && transferType == DataBuffer.TYPE_BYTE) {
byte ba[] = (byte[])pixel;
ba[0] = (byte)colorMapIdx;
pixel = ba;
} else if (pixel instanceof short[] && transferType == DataBuffer.TYPE_USHORT) {
short[] sa = (short[])pixel;
sa[0] = (short)colorMapIdx;
pixel = sa;
} else if (pixel instanceof int[]) {
int ia[] = (int[])pixel;
ia[0] = colorMapIdx;
pixel = ia;
} else {
// awt.268=The pixel is not a primitive array of type transferType
throw new ClassCastException(Messages.getString("awt.268")); //$NON-NLS-1$
}
return pixel;
| public void | finalize()
// TODO: implement
return;
| public final int | getAlpha(int pixel)
return (colorMap[pixel] >> 24) & 0xff;
| public final void | getAlphas(byte[] a)Gets the alpha component of the color map.
for (int i = 0; i < mapSize; i++) {
a[i] = (byte)(colorMap[i] >> 24);
}
| public final int | getBlue(int pixel)
return colorMap[pixel] & 0xff;
| public final void | getBlues(byte[] b)Gets the blue component of the color map.
for (int i = 0; i < mapSize; i++) {
b[i] = (byte)colorMap[i];
}
| public int[] | getComponentSize()
return bits.clone();
| public int[] | getComponents(java.lang.Object pixel, int[] components, int offset)
int pixIdx = -1;
if (pixel instanceof byte[]) {
byte ba[] = (byte[])pixel;
pixIdx = ba[0] & 0xff;
} else if (pixel instanceof short[]) {
short sa[] = (short[])pixel;
pixIdx = sa[0] & 0xffff;
} else if (pixel instanceof int[]) {
int ia[] = (int[])pixel;
pixIdx = ia[0];
} else {
// awt.219=This transferType is not supported by this color model
throw new UnsupportedOperationException(Messages.getString("awt.219")); //$NON-NLS-1$
}
return getComponents(pixIdx, components, offset);
| public int[] | getComponents(int pixel, int[] components, int offset)
if (components == null) {
components = new int[offset + numComponents];
}
components[offset + 0] = getRed(pixel);
components[offset + 1] = getGreen(pixel);
components[offset + 2] = getBlue(pixel);
if (hasAlpha && (components.length - offset) > 3) {
components[offset + 3] = getAlpha(pixel);
}
return components;
| public int | getDataElement(int[] components, int offset)
int rgb = (components[offset] << 16) | (components[offset + 1]) << 8
| components[offset + 2];
if (hasAlpha) {
rgb |= components[offset + 3] << 24;
} else {
rgb |= 0xff000000;
}
int pixel;
switch (transferType) {
case DataBuffer.TYPE_BYTE:
byte ba[] = (byte[])getDataElements(rgb, null);
pixel = ba[0] & 0xff;
break;
case DataBuffer.TYPE_USHORT:
short sa[] = (short[])getDataElements(rgb, null);
pixel = sa[0] & 0xffff;
break;
default:
// awt.267=The transferType is invalid
throw new UnsupportedOperationException(Messages.getString("awt.267")); //$NON-NLS-1$
}
return pixel;
| public java.lang.Object | getDataElements(int[] components, int offset, java.lang.Object pixel)
int rgb = (components[offset] << 16) | (components[offset + 1]) << 8
| components[offset + 2];
if (hasAlpha) {
rgb |= components[offset + 3] << 24;
} else {
rgb |= 0xff000000;
}
return getDataElements(rgb, pixel);
| public synchronized java.lang.Object | getDataElements(int rgb, java.lang.Object pixel)
int red = (rgb >> 16) & 0xff;
int green = (rgb >> 8) & 0xff;
int blue = rgb & 0xff;
int alpha = rgb >>> 24;
int pixIdx = 0;
for (int i = 0; i < totalInserted; i++) {
int idx = i * 2;
if (rgb == cachetable[idx]) {
return createDataObject(cachetable[idx + 1], pixel);
}
}
if (!hasAlpha && grayPalette) {
int grey = (red * 77 + green * 150 + blue * 29 + 128) >>> 8;
int minError = 255;
int error = 0;
for (int i = 0; i < mapSize; i++) {
error = Math.abs((colorMap[i] & 0xff) - grey);
if (error < minError) {
pixIdx = i;
if (error == 0) {
break;
}
minError = error;
}
}
} else if (alpha == 0 && transparentIndex > -1) {
pixIdx = transparentIndex;
} else {
int minAlphaError = 255;
int minError = 195075; // 255^2 + 255^2 + 255^2
int alphaError;
int error = 0;
for (int i = 0; i < mapSize; i++) {
int pix = colorMap[i];
if (rgb == pix) {
pixIdx = i;
break;
}
alphaError = Math.abs(alpha - (pix >>> 24));
if (alphaError <= minAlphaError) {
minAlphaError = alphaError;
int buf = ((pix >> 16) & 0xff) - red;
error = buf * buf;
if (error < minError) {
buf = ((pix >> 8) & 0xff) - green;
error += buf * buf;
if (error < minError) {
buf = (pix & 0xff) - blue;
error += buf * buf;
if (error < minError) {
pixIdx = i;
minError = error;
}
}
}
}
}
}
cachetable[nextInsertIdx] = rgb;
cachetable[nextInsertIdx + 1] = pixIdx;
nextInsertIdx = (nextInsertIdx + 2) % (CACHESIZE * 2);
if (totalInserted < CACHESIZE) {
totalInserted++;
}
return createDataObject(pixIdx, pixel);
| public final int | getGreen(int pixel)
return (colorMap[pixel] >> 8) & 0xff;
| public final void | getGreens(byte[] g)Gets the green component of the color map.
for (int i = 0; i < mapSize; i++) {
g[i] = (byte)(colorMap[i] >> 8);
}
| public final int | getMapSize()Gets the size of the color map.
return mapSize;
| public final int | getRGB(int pixel)
return colorMap[pixel];
| public final void | getRGBs(int[] rgb)Gets the color map.
System.arraycopy(colorMap, 0, rgb, 0, mapSize);
| public final int | getRed(int pixel)
return (colorMap[pixel] >> 16) & 0xff;
| public final void | getReds(byte[] r)Gets the red component of the color map.
for (int i = 0; i < mapSize; i++) {
r[i] = (byte)(colorMap[i] >> 16);
}
| public int | getTransparency()
return transparency;
| public final int | getTransparentPixel()Gets the index that represents the transparent pixel.
return transparentIndex;
| public java.math.BigInteger | getValidPixels()Gets the valid pixels.
return validBits;
| public boolean | isCompatibleRaster(java.awt.image.Raster raster)
int sampleSize = raster.getSampleModel().getSampleSize(0);
return (raster.getTransferType() == transferType && raster.getNumBands() == 1 && (1 << sampleSize) >= mapSize);
| public boolean | isCompatibleSampleModel(java.awt.image.SampleModel sm)
if (sm == null) {
return false;
}
if (!(sm instanceof MultiPixelPackedSampleModel) && !(sm instanceof ComponentSampleModel)) {
return false;
}
if (sm.getTransferType() != transferType) {
return false;
}
if (sm.getNumBands() != 1) {
return false;
}
return true;
| boolean | isGrayPallete()Checks if is gray palette.
return grayPalette;
| public boolean | isValid(int pixel)Checks if the specified pixel is valid for this color model.
if (validBits == null) {
return (pixel >= 0 && pixel < mapSize);
}
return (pixel < mapSize && validBits.testBit(pixel));
| public boolean | isValid()Checks if this color model validates pixels.
return (validBits == null);
| public java.lang.String | toString()
// The output format based on 1.5 release behaviour.
// It could be reveled such way:
// BufferedImage bi = new BufferedImage(1, 1,
// BufferedImage.TYPE_BYTE_INDEXED);
// ColorModel cm = bi.getColorModel();
// System.out.println(cm.toString());
String str = "IndexColorModel: #pixel_bits = " + pixel_bits + //$NON-NLS-1$
" numComponents = " + numComponents + " color space = " + cs + //$NON-NLS-1$ //$NON-NLS-2$
" transparency = "; //$NON-NLS-1$
if (transparency == Transparency.OPAQUE) {
str = str + "Transparency.OPAQUE"; //$NON-NLS-1$
} else if (transparency == Transparency.BITMASK) {
str = str + "Transparency.BITMASK"; //$NON-NLS-1$
} else {
str = str + "Transparency.TRANSLUCENT"; //$NON-NLS-1$
}
str = str + " transIndex = " + transparentIndex + " has alpha = " + //$NON-NLS-1$ //$NON-NLS-2$
hasAlpha + " isAlphaPre = " + isAlphaPremultiplied; //$NON-NLS-1$
return str;
| private static int | validateTransferType(int transferType)Validate transfer type.
if (transferType != DataBuffer.TYPE_BYTE && transferType != DataBuffer.TYPE_USHORT) {
// awt.269=The transferType is not one of DataBuffer.TYPE_BYTE or
// DataBuffer.TYPE_USHORT
throw new IllegalArgumentException(Messages.getString("awt.269")); //$NON-NLS-1$
}
return transferType;
|
|