--- old/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java 2018-03-28 13:31:07.098900000 +0530 +++ new/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java 2018-03-28 13:31:06.590900000 +0530 @@ -698,10 +698,12 @@ readHeader(); /* - * Optimization: We can skip the remaining metadata if the - * ignoreMetadata flag is set, and only if this is not a palette - * image (in that case, we need to read the metadata to get the - * tRNS chunk, which is needed for the getImageTypes() method). + * Optimization: We can skip reading metadata if ignoreMetadata + * flag is set and colorType is not PNG_COLOR_PALETTE. But we need + * to parse only the tRNS chunk even in the case where IHDR colortype + * is not PNG_COLOR_PALETTE, because we need tRNS chunk transparent + * pixel information for PNG_COLOR_RGB while storing the pixel data + * in decodePass(). */ int colorType = metadata.IHDR_colorType; if (ignoreMetadata && colorType != PNG_COLOR_PALETTE) { @@ -717,10 +719,19 @@ int chunkType = stream.readInt(); if (chunkType == IDAT_TYPE) { - // We've reached the image data + // We've reached the first IDAT chunk position stream.skipBytes(-8); imageStartPosition = stream.getStreamPosition(); + /* + * According to PNG specification tRNS chunk must + * precede the first IDAT chunk. So we can stop + * reading metadata. + */ break; + } else if (chunkType == tRNS_TYPE) { + parse_tRNS_chunk(chunkLength); + // After parsing tRNS chunk we will skip 4 CRC bytes + stream.skipBytes(4); } else { // Skip the chunk plus the 4 CRC bytes that follow stream.skipBytes(chunkLength + 4); @@ -1074,6 +1085,32 @@ int bytesPerRow = (bitsPerRow + 7) / 8; int eltsPerRow = (bitDepth == 16) ? bytesPerRow/2 : bytesPerRow; + WritableRaster passRow; + if (isTransparentPixelPresent()) { + /* + * When we have tRNS chunk for image type PNG_COLOR_RGB, we need + * extra alpha channel to represent transparency. In getImageTypes() + * we create a 4 channel destination image, so we need to calculate + * destEltsPerRow and create appropriate Raster. + */ + int destBands = 4; + int destBytesPerPixel = (bitDepth == 16) ? 2 : 1; + destBytesPerPixel *= destBands; + int destBitsPerRow = + Math.multiplyExact((destBands * bitDepth), passWidth); + int destBytesPerRow = (destBitsPerRow + 7) / 8; + int destEltsPerRow = + (bitDepth == 16) ? destBytesPerRow/2 : destBytesPerRow; + + // Create a 1-row tall Raster to hold the data + passRow = createRaster(passWidth, 1, destBands, + destEltsPerRow, bitDepth); + } else { + // Create a 1-row tall Raster to hold the data + passRow = createRaster(passWidth, 1, inputBands, + eltsPerRow, bitDepth); + } + // If no pixels need updating, just skip the input data if (updateWidth == 0) { for (int srcY = 0; srcY < passHeight; srcY++) { @@ -1109,11 +1146,6 @@ byte[] curr = new byte[bytesPerRow]; byte[] prior = new byte[bytesPerRow]; - // Create a 1-row tall Raster to hold the data - WritableRaster passRow = createRaster(passWidth, 1, inputBands, - eltsPerRow, - bitDepth); - // Create an array suitable for holding one pixel int[] ps = passRow.getPixel(0, 0, (int[])null); @@ -1238,15 +1270,70 @@ filter + ")!"); } - // Copy data into passRow byte by byte - if (bitDepth < 16) { - System.arraycopy(curr, 0, byteData, 0, bytesPerRow); + /* + * Copy data into passRow byte by byte. In case of colortype + * PNG_COLOR_RGB if we have transparent pixel information from + * tRNS chunk we need to consider that and store proper information + * in alpha channel. + */ + if (isTransparentPixelPresent()) { + if (bitDepth < 16) { + int srcidx = 0; + int destidx = 0; + for (int i = 0; i < passWidth; i++) { + + byteData[destidx] = curr[srcidx]; + byteData[destidx + 1] = curr[srcidx + 1]; + byteData[destidx + 2] = curr[srcidx + 2]; + + if (curr[srcidx] == (byte)metadata.tRNS_red && + curr[srcidx + 1] == (byte)metadata.tRNS_green && + curr[srcidx + 2] == (byte)metadata.tRNS_blue) + { + byteData[destidx + 3] = (byte)0; + } else { + byteData[destidx + 3] = (byte)255; + } + srcidx += 3; + destidx += 4; + } + } else { + int srcidx = 0; + int destidx = 0; + for (int i = 0; i < passWidth; i++) { + short red = (short) + ((curr[srcidx] << 8) | (curr[srcidx + 1] & 0xff)); + short green = (short) + ((curr[srcidx + 2] << 8) | (curr[srcidx + 3] & 0xff)); + short blue = (short) + ((curr[srcidx + 4] << 8) | (curr[srcidx + 5] & 0xff)); + + shortData[destidx] = red; + shortData[destidx + 1] = green; + shortData[destidx + 2] = blue; + + if (red == (short)metadata.tRNS_red && + green == (short)metadata.tRNS_green && + blue == (short)metadata.tRNS_blue) + { + shortData[destidx + 3] = (short)0; + } else { + shortData[destidx + 3] = (short)65535; + } + srcidx += 6; + destidx += 4; + } + } } else { - int idx = 0; - for (int j = 0; j < eltsPerRow; j++) { - shortData[j] = + if (bitDepth < 16) { + System.arraycopy(curr, 0, byteData, 0, bytesPerRow); + } else { + int idx = 0; + for (int j = 0; j < eltsPerRow; j++) { + shortData[j] = (short)((curr[idx] << 8) | (curr[idx + 1] & 0xff)); - idx += 2; + idx += 2; + } } } @@ -1422,9 +1509,16 @@ // how many bands are in the image, so perform checking // of the read param. int colorType = metadata.IHDR_colorType; - checkReadParamBandSettings(param, - inputBandsForColorType[colorType], - theImage.getSampleModel().getNumBands()); + if (isTransparentPixelPresent()) { + checkReadParamBandSettings(param, + 4, + theImage.getSampleModel().getNumBands()); + + } else { + checkReadParamBandSettings(param, + inputBandsForColorType[colorType], + theImage.getSampleModel().getNumBands()); + } clearAbortRequest(); processImageStarted(0); @@ -1448,6 +1542,19 @@ } } + private boolean isTransparentPixelPresent(){ + /* + * In case of non-indexed PNG_COLOR_RGB images if we have tRNS chunk, + * we need to consider transparent pixel values and store alpha channel + * also. If user explicitly provides ImageReadParam with + * destinationBands as 3 when we have tRNS chunk, only then we will + * ignore tRNS chunk values. + */ + return ((destinationBands == null || + destinationBands.length == 4) && + metadata.tRNS_colorType == PNG_COLOR_RGB); + } + public int getNumImages(boolean allowSearch) throws IIOException { if (stream == null) { throw new IllegalStateException("No input source set!"); @@ -1514,7 +1621,18 @@ break; case PNG_COLOR_RGB: + readMetadata(); // Need tRNS chunk + + /* + * In case of PNG_COLOR_RGB colortype if we have transparent + * pixel information in tRNS chunk we create destination + * image with 4 channels. + */ if (bitDepth == 8) { + if (isTransparentPixelPresent()) { + l.add(ImageTypeSpecifier.createFromBufferedImageType( + BufferedImage.TYPE_4BYTE_ABGR)); + } // some standard types of buffered images // which can be used as destination l.add(ImageTypeSpecifier.createFromBufferedImageType( @@ -1527,6 +1645,18 @@ BufferedImage.TYPE_INT_BGR)); } + if (isTransparentPixelPresent()) { + rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB); + bandOffsets = new int[4]; + bandOffsets[0] = 0; + bandOffsets[1] = 1; + bandOffsets[2] = 2; + bandOffsets[3] = 3; + + l.add(ImageTypeSpecifier. + createInterleaved(rgb, bandOffsets, + dataType, true, false)); + } // Component R, G, B rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB); bandOffsets = new int[3]; --- old/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGMetadata.java 2018-03-28 13:31:08.054900000 +0530 +++ new/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGMetadata.java 2018-03-28 13:31:07.722900000 +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 @@ -268,7 +268,7 @@ // If external (non-PNG sourced) data has red = green = blue, // always store it as gray and promote when writing public boolean tRNS_present; - public int tRNS_colorType; // PNG_COLOR_GRAY, _RGB, or _PALETTE + public int tRNS_colorType = -1; // PNG_COLOR_GRAY, _RGB, or _PALETTE public byte[] tRNS_alpha; // May have fewer entries than PLTE_red, etc. public int tRNS_gray; public int tRNS_red; --- /dev/null 2018-03-28 12:32:01.581694000 +0530 +++ new/test/jdk/javax/imageio/plugins/png/Read8BitPNGWithTRNSChunk.java 2018-03-28 13:31:08.578900000 +0530 @@ -0,0 +1,120 @@ +/* + * 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 6788458 + * @summary Test verifies that PNGImageReader takes tRNS chunk values + * into consideration while reading non-indexed 8 bit PNG image. + * @run main Read8BitPNGWithTRNSChunk + */ + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.awt.Color; +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.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.metadata.IIOMetadataNode; +import javax.imageio.stream.ImageOutputStream; +import javax.imageio.IIOImage; + +public final class Read8BitPNGWithTRNSChunk { + + public static void main(String[] args) throws IOException { + String dir = System.getProperty("test.src"); + String sep = System.getProperty("file.separator"); + String filePath = dir + sep; + File directory = new File(filePath); + File output = File.createTempFile("output", "png", directory); + directory.delete(); + ImageOutputStream ios = ImageIO.createImageOutputStream(output); + try { + int width = 2; + int height = 1; + // Create 8 bit PNG image + BufferedImage img = + new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); + Graphics2D g2D = img.createGraphics(); + + // transparent first pixel + g2D.setColor(Color.WHITE); + g2D.fillRect(0, 0, 1, 1); + // non-transparent second pixel + g2D.setColor(Color.RED); + g2D.fillRect(1, 0, 1, 1); + g2D.dispose(); + + Iterator iterWriter = + ImageIO.getImageWritersBySuffix("png"); + ImageWriter writer = iterWriter.next(); + + ImageWriteParam param = writer.getDefaultWriteParam(); + ImageTypeSpecifier specifier = + ImageTypeSpecifier. + createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR); + IIOMetadata metadata = + writer.getDefaultImageMetadata(specifier, param); + + // Create tRNS node and merge it with default metadata + IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB"); + tRNS_rgb.setAttribute("red", "255"); + tRNS_rgb.setAttribute("green", "255"); + tRNS_rgb.setAttribute("blue", "255"); + + IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); + tRNS.appendChild(tRNS_rgb); + IIOMetadataNode root = + new IIOMetadataNode("javax_imageio_png_1.0"); + root.appendChild(tRNS); + + metadata.mergeTree("javax_imageio_png_1.0", root); + + writer.setOutput(ios); + + writer.write(metadata, new IIOImage(img, null, metadata), param); + + // Read 8 bit PNG RGB image with tRNS chunk + BufferedImage display_img = ImageIO.read(output); + // Verify alpha values present in first & second pixel + Color firstPixel = new Color(display_img.getRGB(0, 0), true); + Color secondPixel = new Color(display_img.getRGB(1, 0), true); + if (firstPixel.getAlpha() != 0 || + secondPixel.getAlpha() != 255) + { + throw new RuntimeException("PNGImageReader is not using" + + " transparent pixel information from tRNS chunk properly"); + } + } finally { + ios.close(); + Files.delete(output.toPath()); + } + } +} + --- /dev/null 2018-03-28 12:32:01.581694000 +0530 +++ new/test/jdk/javax/imageio/plugins/png/Read16BitPNGWithTRNSChunk.java 2018-03-28 13:31:09.546900000 +0530 @@ -0,0 +1,143 @@ +/* + * 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 6788458 + * @summary Test verifies that PNGImageReader takes tRNS chunk values + * into consideration while reading non-indexed 16 bit PNG image. + * @run main Read16BitPNGWithTRNSChunk + */ + +import java.awt.color.ColorSpace; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.ComponentColorModel; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferUShort; +import java.awt.image.WritableRaster; +import java.awt.image.Raster; +import java.awt.Color; +import java.awt.Transparency; +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.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.metadata.IIOMetadataNode; +import javax.imageio.stream.ImageOutputStream; +import javax.imageio.IIOImage; + +public final class Read16BitPNGWithTRNSChunk { + + public static void main(String[] args) throws IOException { + String dir = System.getProperty("test.src"); + String sep = System.getProperty("file.separator"); + String filePath = dir + sep; + File directory = new File(filePath); + File output = File.createTempFile("output", "png", directory); + directory.delete(); + ImageOutputStream ios = ImageIO.createImageOutputStream(output); + try { + // Create 16 bit PNG image + int height = 1; + int width = 2; + int numBands = 3; + int shortArrayLength = width * height * numBands; + short[] pixelData = new short[shortArrayLength]; + // transparent first pixel + pixelData[0] = (short)0xffff; + pixelData[1] = (short)0xffff; + pixelData[2] = (short)0xffff; + // non-transparent second pixel + pixelData[3] = (short)0xffff; + pixelData[4] = (short)0xffff; + pixelData[5] = (short)0xfffe; + + DataBuffer buffer = new DataBufferUShort(pixelData, shortArrayLength); + + int[] bandOffset = {0, 1 ,2}; + WritableRaster ras = + Raster.createInterleavedRaster(buffer, width, height, + width * numBands, numBands, bandOffset, null); + + int nBits[] = {16, 16 ,16}; + ColorModel colorModel = new + ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + nBits, false, false, Transparency.OPAQUE, + DataBuffer.TYPE_USHORT); + BufferedImage img = new BufferedImage(colorModel, ras, + false, null); + + Iterator iterWriter = + ImageIO.getImageWritersBySuffix("png"); + ImageWriter writer = iterWriter.next(); + + ImageWriteParam param = writer.getDefaultWriteParam(); + ImageTypeSpecifier specifier = + ImageTypeSpecifier. + createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR); + IIOMetadata metadata = + writer.getDefaultImageMetadata(specifier, param); + + // Create tRNS node and merge it with default metadata + IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB"); + tRNS_rgb.setAttribute("red", "65535"); + tRNS_rgb.setAttribute("green", "65535"); + tRNS_rgb.setAttribute("blue", "65535"); + + IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); + tRNS.appendChild(tRNS_rgb); + IIOMetadataNode root = + new IIOMetadataNode("javax_imageio_png_1.0"); + root.appendChild(tRNS); + + metadata.mergeTree("javax_imageio_png_1.0", root); + + writer.setOutput(ios); + + writer.write(metadata, new IIOImage(img, null, metadata), param); + + // Read 16 bit PNG RGB image with tRNS chunk + BufferedImage display_img = ImageIO.read(output); + // Verify alpha values present in first & second pixel + Color firstPixel = new Color(display_img.getRGB(0, 0), true); + Color secondPixel = new Color(display_img.getRGB(1, 0), true); + if (firstPixel.getAlpha() != 0 || + secondPixel.getAlpha() != 255) + { + throw new RuntimeException("PNGImageReader is not using" + + " transparent pixel information from tRNS chunk properly"); + } + } finally { + ios.close(); + Files.delete(output.toPath()); + } + + } +} +