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

Incorrectly writes out PNG with color type 2 and a transparency chunk

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Attempting to load then write out a png with a color type of 2 and a transparency chunk results in the transparency chunk having a size of 0xFFFFFFFF and breaks the image. It seems that upon loading then writing, java attempts to convert the color type from 2 to 6 (true color to true color + alpha). Though, this results in producing a bad png.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I have added code in the Test Case Code to reproduce. A png image with a color type of 2 and a transparency chunk is also necessary. Create a java class with the following main method to reproduce. I can provide one if needed.


         

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It is expected that the image written is able to be viewed as normal
      ACTUAL -
      The image cannot be viewed due to the fact that the transparency chunk has a size of 0xFFFFFFFF, thus corrupting the entire file

      ---------- BEGIN SOURCE ----------
       public static void main(String[] args) {
              if(args.length != 2) {
                  System.out.println("Provide input and output files as parameters");
                  return;
              }

              String inputFile = args[0];
              String outputFile = args[1];

              try (
                      InputStream input = new FileInputStream(inputFile);
                      OutputStream output = new FileOutputStream(outputFile);
              ) {
                  ImageInputStream imageInputStream = ImageIO.createImageInputStream(input);
                  Iterator<ImageReader> imageReader = ImageIO.getImageReaders(imageInputStream);
                  IIOImage image = null;
                  if (imageReader.hasNext()) {
                      ImageReader reader = imageReader.next();
                      reader.setInput(imageInputStream, true);
                      IIOMetadata metadata = reader.getImageMetadata(0);
                      BufferedImage bufferedImage = reader.read(0);
                      Iterator<ImageWriter> imageWriter = ImageIO.getImageWritersByFormatName("png");
                      ImageWriter writer = imageWriter.next();

                      image = new IIOImage(bufferedImage, null, metadata);
                      ImageOutputStream ios = ImageIO.createImageOutputStream(output);
                      writer.setOutput(ios);
                      writer.write(reader.getStreamMetadata(), image, null);
                      ios.flush();
                  }
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: