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         // write and read the image without palette
  64         writeAndReadImage(imageWithoutPalette);
  65 
  66         /*
  67          * We create another PNG image with PLTE(palette) chunk so that
  68          * now the IDAT start position is at some 'x + y'.
  69          */
  70         IndexColorModel cm = new IndexColorModel(
  71                 3,
  72                 1,
  73                 new byte[]{10}, // r
  74                 new byte[]{10}, // g
  75                 new byte[]{10}); // b
  76         BufferedImage imageWithPalette = new BufferedImage(
  77                 10, 10,
  78                 BufferedImage.TYPE_BYTE_INDEXED,
  79                 cm);
  80         Graphics2D g2 = imageWithPalette.createGraphics();
  81         g2.setColor(Color.BLACK);
  82         g2.fillRect(0, 0, 10, 10);
  83         g2.dispose();
  84         // write and read the image with palette
  85         writeAndReadImage(imageWithPalette);
  86     }
  87 
  88     private static void writeAndReadImage(BufferedImage image)
  89             throws IOException {
  90         File output = File.createTempFile("output", ".png");
  91         ImageInputStream stream = null;
  92         try {
  93             ImageIO.write(image, "png", output);
  94 
  95             stream = ImageIO.createImageInputStream(output);
  96             ImageReadParam param = PNG_READER.getDefaultReadParam();
  97             PNG_READER.setInput(stream, true, true);
  98             PNG_READER.read(0, param);
  99         } finally {
 100             if (stream != null) {
 101                 stream.close();
 102             }
 103             Files.delete(output.toPath());
 104         }
 105     }
 106 }
 107