下面列出了java.awt.image.IndexColorModel#getRGB() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
public static void main(String args[]) {
IndexColorModel cm =
new IndexColorModel(8, 1, new byte[]{(byte) 128}, new byte[]{(byte) 128}, new byte[]{(byte) 128});
cm.getTransparency();
cm.getComponentSize();
cm.isAlphaPremultiplied();
cm.hasAlpha();
cm.isAlphaPremultiplied();
cm.getTransferType();
cm.getPixelSize();
cm.getComponentSize();
cm.getNumComponents();
cm.getNumColorComponents();
cm.getRed(20);
cm.getGreen(20);
cm.getBlue(20);
cm.getAlpha(20);
cm.getRGB(20);
cm.isAlphaPremultiplied();
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
protected BufferedImage createTestImage(int numColors) {
IndexColorModel icm = createTestICM(numColors);
int w = numColors * 10;
int h = 20;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g = img.createGraphics();
for (int i = 0; i < numColors; i++) {
int rgb = icm.getRGB(i);
//System.out.printf("pixel %d, rgb %x\n", i, rgb);
g.setColor(new Color(rgb));
g.fillRect(i * 10, 0, w - i * 10, h);
}
g.dispose();
return img;
}
/**
* Determines if a specified color map has more than one color.
* Note that we ignore any variation in alpha.
*/
private static boolean isMultiColor(ColorMapped cm) {
ColorMap cmap = cm.getColorMap();
IndexColorModel icm = cmap.getColorModel();
int n = icm.getMapSize();
int rgb = icm.getRGB(0)&0x00ffffff;
for (int i=1; i<n; ++i)
if (rgb!=(icm.getRGB(i)&0x00ffffff))
return true;
return false;
}
/**
* Creates a color table from a <code>ColorIndexModel</code>.
*
* @param i
* this needs to be a byte-based <code>ColorIndexModel</code>.
*/
public GifColorTable(IndexColorModel i) {
this.icm = i;
int size = i.getMapSize();
if (size == 0)
throw new RuntimeException();
// if size is not an exact power
// of 2, we must round it up to one...
int realSize;
if (size == 2) {
realSize = 2;
} else if (size <= 4) {
realSize = 4;
} else if (size <= 8) {
realSize = 8;
} else if (size <= 16) {
realSize = 16;
} else if (size <= 32) {
realSize = 32;
} else if (size <= 64) {
realSize = 64;
} else if (size <= 128) {
realSize = 128;
} else if (size <= 256) {
realSize = 256;
} else {
throw new RuntimeException(
"The number of colors in a GIF color table must be between 2 and 256.");
}
data = new byte[3 * realSize];
for (int a = 0; a < size; a++) {
int rgb = i.getRGB(a);
data[3 * a] = (byte) ((rgb >> 16) & 0xFF);
data[3 * a + 1] = (byte) ((rgb >> 8) & 0xFF);
data[3 * a + 2] = (byte) (rgb & 0xFF);
}
}
/**
* Writes the colour map resulting from the source <tt>IndexColorModel</tt>.
* @param icm the source <tt>IndexColorModel</tt>
* @param out the output to which the colour map will be written
* @throws java.io.IOException if an error occurs
*/
public static void writeColorMap(IndexColorModel icm, net.sf.image4j.io.LittleEndianOutputStream out) throws IOException {
int mapSize = icm.getMapSize();
for (int i = 0; i < mapSize; i++) {
int rgb = icm.getRGB(i);
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb) &0xFF;
out.writeByte(b);
out.writeByte(g);
out.writeByte(r);
out.writeByte(0);
}
}
/**
* Returns {@code true} if the given color models are equal. The {@link ColorModel} class
* defines an {@code equals} method, but as of Java 6 that method does not compare every
* attributes. For example it does not compare the color space and the transfer type, so
* we have to compare them here.
*
* @param cm1 the first color model.
* @param cm2 the second color model.
* @return {@code true} if the two color models are equal.
*/
private static boolean equals(final ColorModel cm1, final ColorModel cm2) {
if (cm1 == cm2) {
return true;
}
if (cm1 != null && cm1.equals(cm2) &&
cm1.getClass().equals(cm2.getClass()) &&
cm1.getTransferType() == cm2.getTransferType() &&
Objects.equals(cm1.getColorSpace(), cm2.getColorSpace()))
{
if (cm1 instanceof IndexColorModel) {
final IndexColorModel icm1 = (IndexColorModel) cm1;
final IndexColorModel icm2 = (IndexColorModel) cm2;
final int size = icm1.getMapSize();
if (icm2.getMapSize() == size &&
icm1.getTransparentPixel() == icm2.getTransparentPixel() &&
Objects.equals(icm1.getValidPixels(), icm2.getValidPixels()))
{
for (int i=0; i<size; i++) {
if (icm1.getRGB(i) != icm2.getRGB(i)) {
return false;
}
}
}
if (cm1 instanceof MultiBandsIndexColorModel) {
final MultiBandsIndexColorModel micm1 = (MultiBandsIndexColorModel) cm1;
final MultiBandsIndexColorModel micm2 = (MultiBandsIndexColorModel) cm2;
if (micm1.numBands != micm2.numBands || micm1.visibleBand != micm2.visibleBand) {
return false;
}
}
}
return true;
}
return false;
}
public BufferedImage getQuantizedImage(Image img) {
// 32 bit ico file already loaded as BufferedImage
if (img instanceof BufferedImage) {
return (BufferedImage) img;
} else {
int width = img.getWidth(null);
int height = img.getHeight(null);
int[][] data = new int[width][height];
int[] pixelbuffer = new int[width * height];
PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixelbuffer, 0, width);
try {
grabber.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
throw new RuntimeException("Can't load the image provided", e);
}
for (int i = 0; i < pixelbuffer.length; i++) {
data[i % width][i / width] = pixelbuffer[i];
}
int[][] savedata = new int[width][height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
savedata[x][y] = data[x][y];
int[] palette = net.charabia.util.codec.Quantize.quantizeImage(data, 255);
byte[] cmap = new byte[256 * 4];
for (int i = 0; i < palette.length; i++) {
// System.out.println(" i= " + (i));
cmap[(i * 4)] = (byte) ((palette[i] >> 16) & 0xFF);
cmap[(i * 4) + 1] = (byte) ((palette[i] >> 8) & 0xFF);
cmap[(i * 4) + 2] = (byte) (palette[i] & 0xFF);
cmap[(i * 4) + 3] = (byte) 0xFF;
}
IndexColorModel colmodel = new IndexColorModel(8, palette.length, cmap, 0, true, 0);
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//
// The normal manner of quantizing would be to run
// result.setRGB(0,0, width, height, pixelbuffer, 0, width);
// where result is a BufferedImage of
// BufferedImage.TYPE_BYTE_INDEXED type. Unfortunately, I
// couldn't make it work. So, here is a work-around that
// should work similarly.
//
java.util.Hashtable set = new java.util.Hashtable();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int alpha = (savedata[x][y] >> 24) & 0xFF;
if (alpha == 0) {
result.setRGB(x, y, 0);
// System.out.print(".");
} else {
int rgb = colmodel.getRGB(data[x][y]);
rgb |= 0xFF000000;
set.put(new Integer(rgb), new Integer(rgb));
result.setRGB(x, y, rgb);
// System.out.print("*");
}
}
// System.out.println("");
}
return result;
}
}