Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Convert BufferedImage from DirectColorModel to IndexColorModel

Thread view: 
Philipp - 10 Dec 2006 20:24 GMT
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 - 10 Dec 2006 23:43 GMT
> Hello
> I want to be able to save to gif using ImageIO. For this I need to
[quoted text clipped - 6 lines]
>
> Thanks for your answers Phil
Make an indexed BufferedImage and draw the original RGB BufferedImage
onto it.
Philipp - 11 Dec 2006 17:24 GMT
>> Hello
>> I want to be able to save to gif using ImageIO. For this I need to
[quoted text clipped - 8 lines]
> 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"?:

[--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 - 11 Dec 2006 17:56 GMT
Philipp schrieb:

>>> Hello
>>> I want to be able to save to gif using ImageIO. For this I need to
[quoted text clipped - 14 lines]
> 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, ...);

Signature

Thomas

Philipp - 12 Dec 2006 09:00 GMT
> Philipp schrieb:
>>
[quoted text clipped - 9 lines]
>   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)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.