This discussion is archived
2 Replies Latest reply: Nov 5, 2012 8:19 AM by 29594 RSS

Error saving TIF image with JPEG compression (IndexColorModel)

29594 Newbie
Currently Being Moderated
Hi all,
i recently came upon the following error:
 javax.imageio.IIOException: Metadata components != number of destination bands
     at com.sun.imageio.plugins.jpeg.JPEGImageWriter.checkSOFBands(JPEGImageWriter.java:1279)
     at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:694)
     at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:339)
     at com.sun.media.imageioimpl.plugins.tiff.TIFFBaseJPEGCompressor.encode(TIFFBaseJPEGCompressor.java:489)
     at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.writeTile(TIFFImageWriter.java:1835)
     at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.write(TIFFImageWriter.java:2686)
trying to save a tif file with jpeg compression. The code used is this:
  public static void main(String[] args) throws Exception{
    String s = "c:/sample images/gray_bug/graybug.tif";
    BufferedImage bi = ImageIO.read(new File(s));
    File outFile = new File(s + "-out.tif");
    
    ImageWriter writer = (ImageWriter)ImageIO.getImageWritersByFormatName("TIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(outFile);
    ios.setByteOrder(ByteOrder.LITTLE_ENDIAN);
    writer.setOutput(ios);
    TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.ENGLISH);
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    writeParam.setCompressionType("JPEG");
    writeParam.setCompressionQuality(1f);
    IIOImage iioImage = new IIOImage(bi, null, null);
    writer.write(null, iioImage, writeParam);      
    writer.reset(); 
    writer.dispose();
    ios.flush();
    ios.close();
  }
The test image that creates the problem can be found here:
http://s11.postimage.org/7wl195w8z/graybug.png

Any ideas what might be wrong?
I am using imageio 1.2-pre-dr-b04 and JDK 1.6.

TIA,
Costas
  • 1. Re: Error saving TIF image with JPEG compression (IndexColorModel)
    gimbal2 Guru
    Currently Being Moderated
    I can't help you but here's an exercise for you. When you get an exception you can't explain yourself, try posting it in Google. With a catch: remove anything from the error message that is specific to you like a classname or a specific value.

    In this case there is nothing specific in the error message so try a search for "javax.imageio.IIOException: Metadata components != number of destination bands". One thing is for sure: you're not the first to run into this error.
  • 2. Re: Error saving TIF image with JPEG compression (IndexColorModel)
    29594 Newbie
    Currently Being Moderated
    Thanks, already did that before posting here, but I can't find any information (although Google now brings this question in top 3 results...)

    I traced the code and see where it comes from, yet i have absolutely no idea why it happens. Strange thing is that saving the BufferedImage as JPG works!
    Costas

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points