private static IndexColorModel buildBoardingColorMap() { byte[] r = new byte[256]; byte[] g = new byte[256]; byte[] b = new byte[256]; byte[] a = new byte[256]; Arrays.fill(a, (byte) 80); g[0] = (byte) 255; b[1] = (byte) 255; r[2] = (byte) 255; g[2] = (byte) 255; r[3] = (byte) 255; a[255] = 0; return new IndexColorModel(8, 256, r, g, b, a); }
return new IndexColorModel(8, 256, vals[0], vals[1], vals[2], vals[3]);
private static BufferedImage rgbaToIndexedBufferedImage(final BufferedImage sourceBufferedImage) { final BufferedImage indexedImage = new BufferedImage( sourceBufferedImage.getWidth(), sourceBufferedImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); final ColorModel cm = indexedImage.getColorModel(); final IndexColorModel icm = (IndexColorModel) cm; final int size = icm.getMapSize(); final byte[] reds = new byte[size]; final byte[] greens = new byte[size]; final byte[] blues = new byte[size]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); final WritableRaster raster = indexedImage.getRaster(); final int pixel = raster.getSample(0, 0, 0); final IndexColorModel resultIcm = new IndexColorModel(8, size, reds, greens, blues, pixel); final BufferedImage resultIndexedImage = new BufferedImage(resultIcm, raster, sourceBufferedImage.isAlphaPremultiplied(), null); resultIndexedImage.getGraphics().drawImage(sourceBufferedImage, 0, 0, null); return resultIndexedImage; }
a[i] = (byte)palette[i].getAlpha(); return new IndexColorModel(8, 256, r, g, b, a);
while ((colors.size() - 1) >> bitCount != 0) bitCount *= 2; IndexColorModel cm = new IndexColorModel(bitCount, colors.size(), cmap, 0, DataBuffer.TYPE_BYTE, null);
IndexColorModel getIndexColorModel() { if (colorModel == null) { int[] rgb = toInterleavedRGB(colors); colorModel = new IndexColorModel(8, rgb.length, rgb, 0, false, -1, DataBuffer.TYPE_BYTE); } return colorModel; }
public IndexColorModel getEGAPalette() { // Test for CGA modes if (isCGAVideoMode4() || isCGAVideoMode5() || isCGAVideoMode6()) { return CGAColorModel.create(palette, bitsPerPixel); } // Test if we should use a default B/W palette if (bitsPerPixel == 1 && channels == 1 && (version < PCX.VERSION_2_X_WINDOWS || isDummyPalette())) { return MONOCHROME; } int bits = channels * bitsPerPixel; return new IndexColorModel(bits, Math.min(16, 1 << bits), palette, 0, false); }
@Test public void testCreateByte() { int[] colors = createIntLut(1 << 8); IndexColorModel colorModel = new IndexColorModel(8, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE); new DiscreteAlphaIndexColorModel(colorModel); }
@Test public void testCreateUShort() { int[] colors = createIntLut(1 << 16); IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT); new DiscreteAlphaIndexColorModel(colorModel); }
@Test public void testCreateBufferedImageBinary() { IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE); ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm); BufferedImage image = spec.createBufferedImage(2, 2); assertNotNull(image); assertEquals(BufferedImage.TYPE_BYTE_BINARY, image.getType()); assertEquals(cm, image.getColorModel()); }
@Test public void testCreateBufferedImageIndexed() { IndexColorModel cm = new IndexColorModel(8, 256, new int[256], 0, false, -1, DataBuffer.TYPE_BYTE); ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm); BufferedImage image = spec.createBufferedImage(2, 2); assertNotNull(image); assertEquals(BufferedImage.TYPE_BYTE_INDEXED, image.getType()); assertEquals(cm, image.getColorModel()); } }
@Test public void testCreateFromIndexedColorModel1to8() { for (int bits = 1; bits <= 8; bits <<= 1) { int[] colors = createIntLut(1 << bits); IndexColorModel colorModel = new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE); assertEquals( IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel), ImageTypeSpecifiers.createFromIndexColorModel(colorModel) ); } }
@Test public void testCreateIndexedIntArray1to8() { for (int bits = 1; bits <= 8; bits <<= 1) { int[] colors = createIntLut(1 << bits); assertEquals( IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE)), ImageTypeSpecifiers.createIndexed(colors, false, -1, bits, DataBuffer.TYPE_BYTE) ); } }
@Test public void testCreateFromIndexedColorModel16() { int[] colors = createIntLut(1 << 16); IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT); assertEquals( IndexedImageTypeSpecifier.createFromIndexColorModel(colorModel), ImageTypeSpecifiers.createFromIndexColorModel(colorModel) ); }
@Test public void testHashCode() { IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE); ImageTypeSpecifier spec = IndexedImageTypeSpecifier.createFromIndexColorModel(cm); ImageTypeSpecifier other = IndexedImageTypeSpecifier.createFromIndexColorModel(cm); ImageTypeSpecifier different = IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE)); // TODO: There is still a problem that IndexColorModel does not override hashCode, // so any model with the same number of bits, transparency, and transfer type will have same hash assertEquals(spec.hashCode(), other.hashCode()); assertFalse(spec.hashCode() == different.hashCode()); }
@Test public void testCreateIndexedIntArray16() { int[] colors = createIntLut(1 << 16); assertEquals( IndexedImageTypeSpecifier.createFromIndexColorModel(new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT)), ImageTypeSpecifiers.createIndexed(colors, false, -1, 16, DataBuffer.TYPE_USHORT) ); }
@Test public void testCreateDiscreteAlphaIndexedFromIndexColorModel8() { int[] colors = createIntLut(1 << 8); IndexColorModel colorModel = new IndexColorModel(8, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE); assertEquals( new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1)), ImageTypeSpecifiers.createFromIndexColorModel(colorModel) ); }
@Test public void testCreateDiscreteAlphaIndexedFromIndexColorModel16() { int[] colors = createIntLut(1 << 16); IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT); assertEquals( new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1)), ImageTypeSpecifiers.createFromIndexColorModel(colorModel) ); }
@Test public void testCreateCompatibleRaster() { int[] colors = createIntLut(1 << 8); IndexColorModel icm = new IndexColorModel(8, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE); ColorModel colorModel = new DiscreteAlphaIndexColorModel(icm); WritableRaster raster = colorModel.createCompatibleWritableRaster(3, 2); assertNotNull(raster); assertEquals(3, raster.getWidth()); assertEquals(2, raster.getHeight()); assertTrue(colorModel.isCompatibleRaster(raster)); assertThat(raster, CoreMatchers.is(WritableRaster.class)); // Specific subclasses are in sun.awt package assertThat(raster.getTransferType(), CoreMatchers.equalTo(DataBuffer.TYPE_BYTE)); }
@Test public void testGetAlpha() { int[] colors = createIntLut(1 << 8); IndexColorModel icm = new IndexColorModel(8, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE); DiscreteAlphaIndexColorModel colorModel = new DiscreteAlphaIndexColorModel(icm); assertEquals(0x45, colorModel.getAlpha(0x45)); assertEquals(0x45, colorModel.getAlpha(new byte[] {0x01, 0x45})); for (int i = 1; i < colors.length; i++) { assertEquals(i, colorModel.getAlpha(i)); assertEquals(i, colorModel.getAlpha(new byte[]{(byte) 0xff, (byte) i})); } }