Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8351108

ImageIO.write(..) fails with exception when writing JPEG with IndexColorModel

XMLWordPrintable

    • b15
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      This is an adaptation of issue 1083 from the twelvemonkeys github project, where the stacktrace resembles:

      ```
      javax.imageio.IIOException: Bogus input colorspace
          at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
          at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1035)
          at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:385)
          at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:175)
          at java.desktop/javax.imageio.ImageWriter.write(ImageWriter.java:613)
          at java.desktop/javax.imageio.ImageIO.doWrite(ImageIO.java:1632)
          at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1558)
      ```

      If you call `ImageIO.write(image, "jpeg", file)` for a BufferedImage with a non-opaque IndexColorModel: then an IIOException is thrown.

      The expected behavior is for `ImageIO.write(..)` to return false.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All 3 image types (OPAQUE, BITMASK, TRANSLUCENT) should pass
      ACTUAL -
      Only OPAQUE passes; BITMASK and TRANSLUCENT fail.

      ---------- BEGIN SOURCE ----------
      import javax.imageio.ImageIO;
      import java.awt.*;
      import java.awt.image.BufferedImage;
      import java.awt.image.IndexColorModel;
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;

      public class JpegWriterWriteNonOpaqueIndexColorModelTest {
          public static void main(String[] args) throws IOException {
              boolean b1 = testJpegWriter(Transparency.OPAQUE, "OPAQUE", true);
              boolean b2 = testJpegWriter(Transparency.BITMASK, "BITMASK", false);
              boolean b3 = testJpegWriter(Transparency.TRANSLUCENT, "TRANSLUCENT", false);
              if (!(b1 && b2 && b3))
                  throw new Error("Test failed");
          }

          private static boolean testJpegWriter(int imageType, String name, boolean expectedWriteReturnValue) {
              System.out.println();
              System.out.println("TESTING " + name);
              try {
                  byte[] gray = new byte[256];
                  for (int a = 0; a < gray.length; a++) {
                      gray[a] = (byte) a;
                  }

                  byte[] alpha = new byte[256];
                  if (imageType == Transparency.OPAQUE || imageType == Transparency.BITMASK) {
                      for (int a = 0; a < alpha.length; a++) {
                          alpha[a] = -1;
                      }
                      if (imageType == Transparency.BITMASK)
                          alpha[0] = 0;
                  } else if (imageType == Transparency.TRANSLUCENT) {
                      for (int a = 0; a < alpha.length; a++) {
                          alpha[a] = (byte) a;
                      }
                  }
                  IndexColorModel indexColorModel = new IndexColorModel(8, 256, gray, gray, gray, alpha);
                  System.out.println("colorModel.getTransparency() = " + indexColorModel.getTransparency());
                  BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_INDEXED, indexColorModel);
                  boolean result = ImageIO.write(bi, "jpg", new ByteArrayOutputStream());
                  if (result != expectedWriteReturnValue)
                      throw new Error("ImageIO.write(..) returned " + result + " but we expected " + expectedWriteReturnValue);
                  System.out.println("Tested passed");
                  return true;
              } catch(Exception e) {
                  System.err.println(name + " test failed");
                  e.printStackTrace();
                  return false;
              }
          }
      }

      ---------- END SOURCE ----------

            jdv Jayathirth D V
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: