Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert BufferedImage from DirectColorModel to IndexColorModel

87 views
Skip to first unread message

Philipp

unread,
Dec 10, 2006, 3:24:22 PM12/10/06
to
Hello
I want to be able to save to gif using ImageIO. For this I need to
convert my TYPE_INT_RGB BufferedImage to a TYPE_BYTE_INDEXED BufferedImage.

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

hiwa

unread,
Dec 10, 2006, 6:43:27 PM12/10/06
to
Make an indexed BufferedImage and draw the original RGB BufferedImage
onto it.

Philipp

unread,
Dec 11, 2006, 12:24:55 PM12/11/06
to

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--]

Thomas Fritsch

unread,
Dec 11, 2006, 12:56:24 PM12/11/06
to
Philipp schrieb:

> hiwa wrote:
>
>> Philipp wrote:
>>
>>> Hello
>>> I want to be able to save to gif using ImageIO. For this I need to
>>> convert my TYPE_INT_RGB BufferedImage to a TYPE_BYTE_INDEXED
>>> BufferedImage.
>>>
>>> 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
>>
>> Make an indexed BufferedImage and draw the original RGB BufferedImage
>> onto it.
>>
>
> Hello and thanks for the answer.
> I tried the following which throws an exception. What do you mean by
> "draw onto it"?:

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

Philipp

unread,
Dec 12, 2006, 4:00:57 AM12/12/06
to
Thomas Fritsch wrote:
> Philipp schrieb:
>> hiwa wrote:
>>
>>> Philipp wrote:
>>>
>>>> Hello
>>>> I want to be able to save to gif using ImageIO. For this I need to
>>>> convert my TYPE_INT_RGB BufferedImage to a TYPE_BYTE_INDEXED
>>>> BufferedImage.
>
> I think he means something like this:
> BufferedImage rgbImage = ...;
> BufferedImage indexedImage = new BufferedImage(...,
> BufferedImage.TYPE_BYTE_INDEXED, ...);
> Graphics2D g = indexImage.createGraphics();
> g.drawImage(rgbImage, ...);
>

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)

0 new messages