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

Incorrect value of Metadata "Transparency"-"Alpha" written for PNG format file

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      When attribute "isAlphaPremultiplied" of image is true and image has alpha channel, the value of "Alpha" is always written as "nonpremultipled" in standard Meta data "Transparency" Node in PNG format file. Please notice this is also a typo in codes~

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Make a image as AlphaPremultiplied, like using code" image.coerceData(true);"
      2) Write the image as PNG file.
      3) Check the meta data of this image file, like using tool "exiftool"
      4) For comparison, write same image as Tif file and check its meta data.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected logic is:
      1) When attribute "isAlphaPremultiplied" of image is true and image has alpha channel, the value of "Alpha" should be written as "premultiplied" in standard Meta data "Transparency" Node.
      2) When this attribute is false and image has alpha channel, the value should be written as "nonpremultiplied".
      3) When image has not alpha channel , the value should be "None".
      ACTUAL -
      When image has alpha channel, the value "Alpha" is always "nonpremultipled" (Typo too) when save as PNG format.
      Meanwhile, correct meta data is written when save as Tif format.


      ---------- BEGIN SOURCE ----------
      I checked source codes and think following codes may need improvement.
      Package "com.sun.imageio.plugins.png", Class "PNGMetadata", Method "getStandardTransparencyNode":
      ----------------------------------------------------------------
              IIOMetadataNode transparency_node =
                  new IIOMetadataNode("Transparency");
              IIOMetadataNode node = null; // scratch node

              node = new IIOMetadataNode("Alpha");
              boolean hasAlpha =
                  (IHDR_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) ||
                  (IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) ||
                  (IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE &&
                   tRNS_present &&
                   (tRNS_colorType == IHDR_colorType) &&
                   (tRNS_alpha != null));
              node.setAttribute("value", hasAlpha ? "nonpremultipled" : "none"); <--- bug here! typo too!
              transparency_node.appendChild(node);
      ----------------------------------------------------------------


      Codes of Tif format look correct.
      Package "com.sun.imageio.plugins.tiff", Class "TIFFImageMetadata", Method "getStandardTransparencyNode":
      ----------------------------------------------------------------
              IIOMetadataNode transparency_node =
                  new IIOMetadataNode("Transparency");
              IIOMetadataNode node = null; // scratch node

              TIFFField f;

              node = new IIOMetadataNode("Alpha");
              String value = "none";

              f = getTIFFField(BaselineTIFFTagSet.TAG_EXTRA_SAMPLES);
              if(f != null) {
                  int[] extraSamples = f.getAsInts();
                  for(int i = 0; i < extraSamples.length; i++) {
                      if(extraSamples[i] ==
                         BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA) {
                          value = "premultiplied";
                          break;
                      } else if(extraSamples[i] ==
                                BaselineTIFFTagSet.EXTRA_SAMPLES_UNASSOCIATED_ALPHA) {
                          value = "nonpremultiplied";
                          break;
                      }
                  }
              }
      ----------------------------------------------------------------
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No way to set correct value even try following codes:
      -------------------------------------------------------------------
       IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
                  if (metaData != null && !metaData.isReadOnly()
                          && metaData.isStandardMetadataFormatSupported()) {
                      try {
                          String standardFormat = IIOMetadataFormatImpl.standardMetadataFormatName; // "javax_imageio_1.0"
                          IIOMetadataNode standardTree = (IIOMetadataNode) metaData.getAsTree(standardFormat);
                          NodeList TransparencyNode = standardTree.getElementsByTagName("Transparency");
                          IIOMetadataNode Transparency, Alpha;
                          if (TransparencyNode != null && TransparencyNode.getLength() > 0) {
                              Transparency = (IIOMetadataNode) TransparencyNode.item(0);
                          } else {
                              Transparency = new IIOMetadataNode("Transparency");
                              standardTree.appendChild(Transparency);
                          }
                          Alpha = new IIOMetadataNode("Alpha");
                          Alpha.setAttribute("value", "premultiplied");
                          Transparency.appendChild(Alpha);
                          metaData.mergeTree(standardFormat, standardTree);
                      } catch (Exception e) {
                          logger.error(e.toString());
                      }
                  }
                  try (ImageOutputStream out = ImageIO.createImageOutputStream(file)) {
                      writer.setOutput(out);
                      writer.write(metaData, new IIOImage(image, null, metaData), param);
                      out.flush();
                  }
                  writer.dispose();

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: