Convert BufferedImage from DirectColorModel to IndexColorModel

Discussion in 'Java' started by Philipp, Dec 10, 2006.

  1. Philipp

    Philipp Guest

    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
    Philipp, Dec 10, 2006
    #1
    1. Advertising

  2. Philipp

    hiwa Guest

    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.
    hiwa, Dec 10, 2006
    #2
    1. Advertising

  3. Philipp

    Philipp Guest

    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"?:

    [--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--]
    Philipp, Dec 11, 2006
    #3
  4. 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
    Thomas Fritsch, Dec 11, 2006
    #4
  5. Philipp

    Philipp Guest

    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)
    Philipp, Dec 12, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bernhard Messerer
    Replies:
    1
    Views:
    669
    Roedy Green
    Aug 14, 2003
  2. Michael C. Gilson
    Replies:
    4
    Views:
    946
    Ken DeLong
    Aug 31, 2003
  3. Martin Chan
    Replies:
    0
    Views:
    564
    Martin Chan
    Feb 17, 2004
  4. bonkura_
    Replies:
    3
    Views:
    2,208
    Roedy Green
    Oct 22, 2005
  5. Replies:
    1
    Views:
    1,784
Loading...

Share This Page