Path: blob/master/test/jdk/javax/imageio/plugins/png/PngMultipleImageReadTest.java
41155 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 819143126* @summary Test verifies that whether we can use same PNGImageReader instance27* to read multiple images or not. It also verifies whether28* imageStartPosition in PNGImageReader is updated properly when we29* use same PNGImageReader instance to read multiple images.30* @run main PngMultipleImageReadTest31*/3233import java.awt.Color;34import java.awt.Graphics2D;35import java.awt.image.BufferedImage;36import java.awt.image.IndexColorModel;37import java.io.File;38import java.io.IOException;39import java.nio.file.Files;40import javax.imageio.ImageIO;41import javax.imageio.ImageReadParam;42import javax.imageio.ImageReader;43import javax.imageio.stream.ImageInputStream;4445public class PngMultipleImageReadTest {4647private static final ImageReader PNG_READER =48ImageIO.getImageReadersByMIMEType("image/png").next();4950public static void main(String[] args) throws IOException {5152/*53* First we create a PNG image without palette so that the IDAT54* start position in the stream is at some position 'x'.55*/56BufferedImage imageWithoutPalette =57new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);58Graphics2D g1 = imageWithoutPalette.createGraphics();59g1.setColor(Color.WHITE);60g1.fillRect(0, 0, 20, 20);61g1.dispose();62// write and read the image without palette63writeAndReadImage(imageWithoutPalette);6465/*66* We create another PNG image with PLTE(palette) chunk so that67* now the IDAT start position is at some 'x + y'.68*/69IndexColorModel cm = new IndexColorModel(703,711,72new byte[]{10}, // r73new byte[]{10}, // g74new byte[]{10}); // b75BufferedImage imageWithPalette = new BufferedImage(7610, 10,77BufferedImage.TYPE_BYTE_INDEXED,78cm);79Graphics2D g2 = imageWithPalette.createGraphics();80g2.setColor(Color.BLACK);81g2.fillRect(0, 0, 10, 10);82g2.dispose();83// write and read the image with palette84writeAndReadImage(imageWithPalette);85}8687private static void writeAndReadImage(BufferedImage image)88throws IOException {89File output = File.createTempFile("output", ".png");90ImageInputStream stream = null;91try {92ImageIO.write(image, "png", output);9394stream = ImageIO.createImageInputStream(output);95ImageReadParam param = PNG_READER.getDefaultReadParam();96PNG_READER.setInput(stream, true, true);97PNG_READER.read(0, param);98} finally {99if (stream != null) {100stream.close();101}102Files.delete(output.toPath());103}104}105}106107108109