1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6574555
  27  * @summary Test verifies that PNGImageWriter encodes the R, G, B
  28  *          values of bKGD chunk properly.
  29  * @run     main VerifyRGBValuesFromBKGDChunk
  30  */
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.nio.file.Files;
  36 import java.util.Iterator;
  37 import javax.imageio.ImageTypeSpecifier;
  38 import javax.imageio.ImageWriter;
  39 import javax.imageio.ImageReader;
  40 import javax.imageio.ImageIO;
  41 import javax.imageio.ImageWriteParam;
  42 import javax.imageio.metadata.IIOMetadata;
  43 import javax.imageio.metadata.IIOMetadataNode;
  44 import javax.imageio.IIOImage;
  45 import javax.imageio.stream.ImageInputStream;
  46 import javax.imageio.stream.ImageOutputStream;
  47 
  48 public class VerifyRGBValuesFromBKGDChunk {
  49 
  50     public static void main(String[] args) throws IOException {
  51         ImageOutputStream ios = null;
  52         ImageInputStream iis = null;
  53         File output = null;
  54         try {
  55             int width = 1;
  56             int height = 1;
  57             BufferedImage img = new
  58                 BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
  59             Iterator<ImageWriter> iterWriter =
  60                 ImageIO.getImageWritersBySuffix("png");
  61             ImageWriter writer = iterWriter.next();
  62 
  63             ImageWriteParam param = writer.getDefaultWriteParam();
  64             ImageTypeSpecifier specifier =
  65                 ImageTypeSpecifier.
  66                     createFromBufferedImageType(BufferedImage.TYPE_INT_BGR);
  67             IIOMetadata encodeMetadata =
  68                 writer.getDefaultImageMetadata(specifier, param);
  69 
  70             // Write png image with bKGD chunk
  71             IIOMetadataNode bKGD_rgb = new IIOMetadataNode("bKGD_RGB");
  72             String red = "100";
  73             String green = "150";
  74             String blue = "200";
  75             bKGD_rgb.setAttribute("red", red);
  76             bKGD_rgb.setAttribute("green", green);
  77             bKGD_rgb.setAttribute("blue", blue);
  78 
  79             IIOMetadataNode bKGD = new IIOMetadataNode("bKGD");
  80             bKGD.appendChild(bKGD_rgb);
  81             IIOMetadataNode encodeRoot =
  82                 new IIOMetadataNode("javax_imageio_png_1.0");
  83             encodeRoot.appendChild(bKGD);
  84 
  85             encodeMetadata.mergeTree("javax_imageio_png_1.0", encodeRoot);
  86 
  87             String dir = System.getProperty("test.src");
  88             String sep = System.getProperty("file.separator");
  89             String filePath = dir + sep;
  90             File directory = new File(filePath);
  91             output = File.createTempFile("output", "png", directory);
  92             ios = ImageIO.createImageOutputStream(output);
  93             writer.setOutput(ios);
  94 
  95             writer.write(encodeMetadata,
  96                          new IIOImage(img, null, encodeMetadata), param);
  97             writer.dispose();
  98 
  99             // Get bKGD chunk from image and verify the values
 100             iis = ImageIO.createImageInputStream(output);
 101             Iterator<ImageReader> iterReader =
 102                 ImageIO.getImageReadersBySuffix("png");
 103             ImageReader reader = iterReader.next();
 104             reader.setInput(iis, false, false);
 105 
 106             IIOMetadata decodeMetadata = reader.getImageMetadata(0);
 107             IIOMetadataNode decodeRoot =
 108                 (IIOMetadataNode) decodeMetadata.
 109                     getAsTree("javax_imageio_png_1.0");
 110             bKGD_rgb = (IIOMetadataNode)
 111                 decodeRoot.getElementsByTagName("bKGD_RGB").item(0);
 112 
 113             if (!(red.equals(bKGD_rgb.getAttribute("red")) &&
 114                   green.equals(bKGD_rgb.getAttribute("green")) &&
 115                   blue.equals(bKGD_rgb.getAttribute("blue")))) {
 116                 throw new RuntimeException("bKGD RGB values are not stored" +
 117                 " properly");
 118             }
 119         } finally {
 120             if (ios != null) {
 121                 ios.close();
 122             }
 123             if (iis != null) {
 124                 iis.close();
 125             }
 126             if (output != null) {
 127                 Files.delete(output.toPath());
 128             }
 129         }
 130     }
 131 }
 132