--- old/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java 2018-04-11 16:00:27.662796544 +0530 +++ new/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java 2018-04-11 16:00:27.178796544 +0530 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -651,11 +651,11 @@ int colorType = metadata.IHDR_colorType & 0x3; int chunkType = metadata.bKGD_colorType; + int chunkRed = metadata.bKGD_red; + int chunkGreen = metadata.bKGD_green; + int chunkBlue = metadata.bKGD_blue; // Special case: image is RGB(A) and chunk is Gray // Promote chunk contents to RGB - int chunkRed = metadata.bKGD_red; - int chunkGreen = metadata.bKGD_red; - int chunkBlue = metadata.bKGD_red; if (colorType == PNGImageReader.PNG_COLOR_RGB && chunkType == PNGImageReader.PNG_COLOR_GRAY) { // Make a gray bKGD chunk look like RGB --- /dev/null 2018-04-10 11:25:39.758703000 +0530 +++ new/test/jdk/javax/imageio/plugins/png/VerifyRGBValuesFromBKGDChunk.java 2018-04-11 16:00:28.390796544 +0530 @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6574555 + * @summary Test verifies that PNGImageWriter encodes the R, G, B + * values of bKGD chunk properly. + * @run main VerifyRGBValuesFromBKGDChunk + */ + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Iterator; +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriter; +import javax.imageio.ImageReader; +import javax.imageio.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.metadata.IIOMetadataNode; +import javax.imageio.IIOImage; +import javax.imageio.stream.ImageInputStream; +import javax.imageio.stream.ImageOutputStream; + +public class VerifyRGBValuesFromBKGDChunk { + + public static void main(String[] args) throws IOException { + ImageOutputStream ios = null; + ImageInputStream iis = null; + File output = null; + try { + int width = 1; + int height = 1; + BufferedImage img = new + BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); + Iterator iterWriter = + ImageIO.getImageWritersBySuffix("png"); + ImageWriter writer = iterWriter.next(); + + ImageWriteParam param = writer.getDefaultWriteParam(); + ImageTypeSpecifier specifier = + ImageTypeSpecifier. + createFromBufferedImageType(BufferedImage.TYPE_INT_BGR); + IIOMetadata encodeMetadata = + writer.getDefaultImageMetadata(specifier, param); + + // Write png image with bKGD chunk + IIOMetadataNode bKGD_rgb = new IIOMetadataNode("bKGD_RGB"); + String red = "100"; + String green = "150"; + String blue = "200"; + bKGD_rgb.setAttribute("red", red); + bKGD_rgb.setAttribute("green", green); + bKGD_rgb.setAttribute("blue", blue); + + IIOMetadataNode bKGD = new IIOMetadataNode("bKGD"); + bKGD.appendChild(bKGD_rgb); + IIOMetadataNode encodeRoot = + new IIOMetadataNode("javax_imageio_png_1.0"); + encodeRoot.appendChild(bKGD); + + encodeMetadata.mergeTree("javax_imageio_png_1.0", encodeRoot); + + String dir = System.getProperty("test.src"); + String sep = System.getProperty("file.separator"); + String filePath = dir + sep; + File directory = new File(filePath); + output = File.createTempFile("output", "png", directory); + ios = ImageIO.createImageOutputStream(output); + writer.setOutput(ios); + + writer.write(encodeMetadata, + new IIOImage(img, null, encodeMetadata), param); + writer.dispose(); + + // Get bKGD chunk from image and verify the values + iis = ImageIO.createImageInputStream(output); + Iterator iterReader = + ImageIO.getImageReadersBySuffix("png"); + ImageReader reader = iterReader.next(); + reader.setInput(iis, false, false); + + IIOMetadata decodeMetadata = reader.getImageMetadata(0); + IIOMetadataNode decodeRoot = + (IIOMetadataNode) decodeMetadata. + getAsTree("javax_imageio_png_1.0"); + bKGD_rgb = (IIOMetadataNode) + decodeRoot.getElementsByTagName("bKGD_RGB").item(0); + + if (!(red.equals(bKGD_rgb.getAttribute("red")) && + green.equals(bKGD_rgb.getAttribute("green")) && + blue.equals(bKGD_rgb.getAttribute("blue")))) { + throw new RuntimeException("bKGD RGB values are not stored" + + " properly"); + } + } finally { + if (ios != null) { + ios.close(); + } + if (iis != null) { + iis.close(); + } + if (output != null) { + Files.delete(output.toPath()); + } + } + } +} +