How can this be done using the quantization algorithms discussed in
javax.media.jai.operator.ColorQuantizerDescriptor.
(I don't seem to understand how all these image operations works).
Thanks for your answers Phil
Hello and thanks for the answer.
I tried the following which throws an exception. What do you mean by
"draw onto it"?:
[--code--]
BufferedImage bi = sv.getBufferedImage();
BufferedImage indBI = new BufferedImage(bi.getHeight(), bi.getWidth(),
BufferedImage.TYPE_BYTE_INDEXED);
bi.copyData(indBI.getRaster());
return ImageIO.write(indBI, fileType, file);
[--end--]
The copyData line throws the following:
[--stack trace--]
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [I
at
sun.awt.image.ByteInterleavedRaster.setDataElements(ByteInterleavedRaster.java:660)
at java.awt.image.BufferedImage.copyData(BufferedImage.java:1449)
at io.BitmapRecorder.writeBitmapFile(BitmapRecorder.java:25)
[--end--]
I think he means something like this:
BufferedImage rgbImage = ...;
BufferedImage indexedImage = new BufferedImage(...,
BufferedImage.TYPE_BYTE_INDEXED, ...);
Graphics2D g = indexImage.createGraphics();
g.drawImage(rgbImage, ...);
--
Thomas
Hello and thank you for the comment which brought me closer to the
solution I look for.
I tried the following (1) which works as it outputs a gif file, but
a) I think to use RescaleOp as a color conversion tool is ugly
b) the colors output are really ugly (especially because my image has
only 256 colors, so it should be indexable exactly). Can I improve this
with the rendering hints?
I tried other ways which do not work:
(2) ColorConvertOp: I dont know the colorspace to use.
(3) ColorQuantizerDescriptor: This is prob the way to go. Don't know why
it doesn't work. Can you point me in the right direction?
Thanks again Phil
--- [code] (OK works, but colors are ugly) ---
BufferedImage rgbImage = sv.getBufferedImage(); // is of TYPE_INT_RGB
BufferedImage indexedImage = new BufferedImage(rgbImage.getWidth(),
rgbImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g = indexedImage.createGraphics();
RenderingHints hints = new RenderingHints(RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_ENABLE);
RescaleOp op = new RescaleOp(1.0f, 0.0f, hints); // do nothing!
g.drawImage(rgbImage,op,1,1);
---- 2 ----
BufferedImage rgbImage = sv.getBufferedImage(); // is of TYPE_INT_RGB
BufferedImage indexedImage = new BufferedImage(rgbImage.getHeight(),
rgbImage.getWidth(), BufferedImage.TYPE_BYTE_INDEXED);
ColorConvertOp op = new ColorConvertOp(null);
indexedImage = op.filter(rgbImage, indexedImage);
ImageIO.write(indexedImage, "gif", file);
This throws:
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: Destination ColorSpace is undefined
at java.awt.image.ColorConvertOp.ICCBIFilter(ColorConvertOp.java:290)
at java.awt.image.ColorConvertOp.filter(ColorConvertOp.java:262)
at io.BitmapRecorder.writeBitmapFile(BitmapRecorder.java:58)
---- 3 ----
BufferedImage rgbImage = sv.getBufferedImage(); // is of TYPE_INT_RGB
PlanarImage indexedImage =
ColorQuantizerDescriptor.create(rgbImage,ColorQuantizerDescriptor.MEDIANCUT,256,32768,null,1,1,null);
ImageIO.write(indexedImage, "gif", file);
This throws:
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: The specified ColorModel is
incompatible with the image SampleModel.
at javax.media.jai.PlanarImage.setImageLayout(PlanarImage.java:541)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
at javax.media.jai.RenderedOp.getColorModel(RenderedOp.java:2253)
at javax.imageio.ImageTypeSpecifier.<init>(ImageTypeSpecifier.java:226)
at
javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:1031)
at javax.imageio.ImageIO.write(ImageIO.java:1439)
at javax.imageio.ImageIO.write(ImageIO.java:1488)
at io.BitmapRecorder.writeBitmapFile(BitmapRecorder.java:65)