1 /*
   2  * Copyright (c) 2017, 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     8191431
  27  * @summary Test verifies that whether we can use same PNGImageReader instance
  28  *          to read multiple images or not. It also verifies whether
  29  *          imageStartPosition in PNGImageReader is updated properly when we
  30  *          use same PNGImageReader instance to read multiple images.
  31  * @run     main PngMultipleImageReadTest
  32  */
  33 
  34 import java.awt.Color;
  35 import java.awt.Graphics2D;
  36 import java.awt.image.BufferedImage;
  37 import java.awt.image.IndexColorModel;
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.nio.file.Files;
  41 import javax.imageio.ImageIO;
  42 import javax.imageio.ImageReadParam;
  43 import javax.imageio.ImageReader;
  44 import javax.imageio.stream.ImageInputStream;
  45 
  46 public class PngMultipleImageReadTest {
  47 
  48     private static final ImageReader PNG_READER =
  49             ImageIO.getImageReadersByMIMEType("image/png").next();
  50 
  51     public static void main(String[] args) throws IOException {
  52 
  53         /*
  54          * First we create a PNG image without palette so that the IDAT
  55          * start position in the stream is at some position 'x'.
  56          */
  57         BufferedImage imageWithoutPalette =
  58                 new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
  59         Graphics2D g1 = imageWithoutPalette.createGraphics();
  60         g1.setColor(Color.WHITE);
  61         g1.fillRect(0, 0, 20, 20);
  62         g1.dispose();
  63         writeAndReadImageWithoutPalette(imageWithoutPalette);
  64 
  65         /*
  66          * We create another PNG image with PLTE(palette) chunk so that
  67          * now the IDAT start position is at some 'x + y'.
  68          */
  69         IndexColorModel cm = new IndexColorModel(
  70                 3,
  71                 1,
  72                 new byte[]{10}, // r
  73                 new byte[]{10}, // g
  74                 new byte[]{10}); // b
  75         BufferedImage imageWithPalette = new BufferedImage(
  76                 10, 10,
  77                 BufferedImage.TYPE_BYTE_INDEXED,
  78                 cm);
  79         Graphics2D g2 = imageWithPalette.createGraphics();
  80         g2.setColor(Color.BLACK);
  81         g2.fillRect(0, 0, 10, 10);
  82         g2.dispose();
  83         writeAndReadImageWithPalette(imageWithPalette);
  84     }
  85 
  86     private static void writeAndReadImageWithoutPalette(BufferedImage image)
  87             throws IOException {
  88         File output = File.createTempFile("output", ".png");
  89         ImageInputStream stream = null;
  90         try {
  91             ImageIO.write(image, "png", output);
  92 
  93             stream = ImageIO.createImageInputStream(output);
  94             ImageReadParam param = PNG_READER.getDefaultReadParam();
  95             PNG_READER.setInput(stream, true, true);
  96             PNG_READER.read(0, param);
  97         } finally {
  98             if (stream != null) {
  99                 stream.close();
 100             }
 101             Files.delete(output.toPath());
 102         }
 103     }
 104 
 105     private static void writeAndReadImageWithPalette(BufferedImage image)
 106             throws IOException {
 107         File output = File.createTempFile("output", ".png");
 108         ImageInputStream stream = null;
 109         try {
 110             ImageIO.write(image, "png", output);
 111 
 112             stream = ImageIO.createImageInputStream(output);
 113             ImageReadParam param = PNG_READER.getDefaultReadParam();
 114             PNG_READER.setInput(stream, true, true);
 115             PNG_READER.read(0, param);
 116         } finally {
 117             if (stream != null) {
 118                 stream.close();
 119             }
 120             Files.delete(output.toPath());
 121         }
 122     }
 123 }
 124