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

Writing a JPEG is not gamma-correct

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      It does not seem to be system-specific.

      A DESCRIPTION OF THE PROBLEM :
      By default, the JPEG writer subsamples chroma 4:1:1, and does not perform gamma-correction while subsampling. This can lead to surprising results, aka, destroy the visible image.

      This can be easily seen with the gamma_dalai_lama_gray.jpg example from http://www.ericbrasseur.org/gamma.html.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Download gamma_dalai_lama_gray.jpg
      2. Compile provided .java file
      3. Run resulting binary
      4. Look at output.jpg

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Resulting image looks like original image.
      ACTUAL -
      Resulting image is completely gray.

      ---------- BEGIN SOURCE ----------
      public class BugExample {
        public static void main(String[] args) throws Exception {
          BufferedImage image = ImageIO.read(new File("gamma_dalai_lama_gray.jpg"));
          JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
          jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          jpegParams.setCompressionQuality(1f);
          jpegParams.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
          ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
          writer.setOutput(ImageIO.createImageOutputStream(new File("output.jpg")));
          writer.write(null, new IIOImage(image, null, null), jpegParams);
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There is no public API for doing so, but it can be worked around by setting HsamplingFactor and VsamplingFactor on the metadata specified on the IIOImage, e.g., by round-tripping the metadata through the XML representation, and overwriting the corresponding nodes.

      See https://stackoverflow.com/questions/14149739/disable-java-imageio-chroma-subsampling

      FREQUENCY : always


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

              Created:
              Updated: