1 /*
   2  * Copyright (c) 2016, 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  *
  27  * @bug     8144991 8150154
  28  * @ignore  8150154
  29  * @author  a.stepanov
  30  * @summary Check if repeating image writing doesn't fail
  31  *          (particularly, no AIOOB occurs)
  32  *
  33  * @run     main RepeatingWriteTest
  34  */
  35 
  36 
  37 import java.awt.*;
  38 import java.awt.image.BufferedImage;
  39 import java.io.*;
  40 import javax.imageio.*;
  41 import javax.imageio.stream.*;
  42 import java.util.Iterator;
  43 
  44 public class RepeatingWriteTest {
  45 
  46     private static final int TYPES[] = new int[]{
  47            BufferedImage.TYPE_INT_RGB,        BufferedImage.TYPE_INT_ARGB,
  48            BufferedImage.TYPE_INT_ARGB_PRE,   BufferedImage.TYPE_INT_BGR,
  49            BufferedImage.TYPE_3BYTE_BGR,      BufferedImage.TYPE_4BYTE_ABGR,
  50            BufferedImage.TYPE_4BYTE_ABGR_PRE, BufferedImage.TYPE_BYTE_GRAY,
  51            BufferedImage.TYPE_USHORT_GRAY,    BufferedImage.TYPE_BYTE_BINARY,
  52            BufferedImage.TYPE_BYTE_INDEXED,   BufferedImage.TYPE_USHORT_565_RGB,
  53            BufferedImage.TYPE_USHORT_555_RGB};
  54 
  55     private static final String NAMES[] = new String[]{
  56            "TYPE_INT_RGB",        "TYPE_INT_ARGB",
  57            "TYPE_INT_ARGB_PRE",   "TYPE_INT_BGR",
  58            "TYPE_3BYTE_BGR",      "TYPE_4BYTE_ABGR",
  59            "TYPE_4BYTE_ABGR_PRE", "TYPE_BYTE_GRAY",
  60            "TYPE_USHORT_GRAY",    "TYPE_BYTE_BINARY",
  61            "TYPE_BYTE_INDEXED",   "TYPE_USHORT_565_RGB",
  62            "TYPE_USHORT_555_RGB"};
  63 
  64     private static final int SZ = 100;
  65 
  66     private static ImageWriter getWriter(String format) {
  67         Iterator<ImageWriter> writers =
  68             ImageIO.getImageWritersByFormatName(format);
  69         if (!writers.hasNext()) {
  70             throw new RuntimeException("no writers available for " + format);
  71         }
  72         return writers.next();
  73     }
  74 
  75     private static ImageReader getReader(String format) {
  76         Iterator<ImageReader> readers =
  77             ImageIO.getImageReadersByFormatName(format);
  78         if (!readers.hasNext()) {
  79             throw new RuntimeException("no readers available for " + format);
  80         }
  81         return readers.next();
  82     }
  83 
  84     private static class ImageCheckException extends Exception {
  85         public ImageCheckException(String msg) { super(msg); }
  86     }
  87 
  88     private final static Color C = Color.WHITE;
  89     private final String format;
  90 
  91     public RepeatingWriteTest(String format) { this.format = format; }
  92 
  93     private void checkImage(String fileName) throws Exception {
  94         ImageReader reader = getReader(format);
  95         ImageInputStream iis = ImageIO.createImageInputStream(new File(fileName));
  96         reader.setInput(iis);
  97         BufferedImage img = reader.read(0);
  98         Color c = new Color(img.getRGB(SZ / 2, SZ / 2));
  99         if (img.getWidth()  != SZ ||
 100             img.getHeight() != SZ ||
 101             !c.equals(C)) { throw new ImageCheckException(fileName); }
 102     }
 103 
 104     private void doTest(int i, int j) throws Exception {
 105 
 106         String f1 = "test-1-" + NAMES[i] + "." + format;
 107         String f2 = "test-2-" + NAMES[j] + "." + format;
 108 
 109         ImageWriter writer = getWriter(format);
 110 
 111         // --- write 1st image ---
 112         OutputStream s = new BufferedOutputStream(new FileOutputStream(f1));
 113         ImageOutputStream ios = ImageIO.createImageOutputStream(s);
 114         writer.setOutput(ios);
 115 
 116         BufferedImage img = new BufferedImage(SZ, SZ, TYPES[i]);
 117         Graphics g = img.getGraphics();
 118         g.setColor(C);
 119         g.fillRect(0, 0, SZ, SZ);
 120         g.dispose();
 121 
 122         writer.write(img);
 123         ios.flush();
 124         s.close();
 125 
 126         // --- write 2nd image ---
 127         s = new BufferedOutputStream(new FileOutputStream(f2));
 128         ios = ImageIO.createImageOutputStream(s);
 129         writer.setOutput(ios);
 130 
 131         img = new BufferedImage(SZ, SZ, TYPES[j]);
 132         g = img.getGraphics();
 133         g.setColor(C);
 134         g.fillRect(0, 0, SZ, SZ);
 135         g.dispose();
 136 
 137         writer.write(img);
 138         ios.flush();
 139         s.close();
 140 
 141         // --- check files ---
 142         checkImage(f1);
 143         checkImage(f2);
 144     }
 145 
 146     public static void main(String args[]) throws Exception {
 147 
 148         int n = TYPES.length;
 149         int nAIOOB = 0, nChecksFailed = 0;
 150 
 151         String formats[] = {"gif", "png", "tiff"};
 152         for (String f: formats) {
 153             System.out.println("\nformat: " + f);
 154             RepeatingWriteTest test = new RepeatingWriteTest(f);
 155             for (int i = 0; i < n; ++i) {
 156                 for (int j = 0; j < n; ++j) {
 157                     try {
 158                         test.doTest(i, j);
 159                     } catch (ArrayIndexOutOfBoundsException e) {
 160                         System.err.println(f + ": AIOOB for pair (" +
 161                             NAMES[i] + ", " + NAMES[j] + ")");
 162                         nAIOOB++;
 163                     } catch (ImageCheckException e) {
 164                         System.err.println(f + 
 165                             ": image check failed for " + e.getMessage());
 166                         nChecksFailed++;
 167                     }
 168                 }
 169             }
 170         }
 171 
 172         if (nAIOOB > 0 || nChecksFailed > 0) {
 173             throw new RuntimeException("test failed: " + nAIOOB + " AIOOBs, " +
 174                 nChecksFailed + " image checks failed");
 175         }
 176     }
 177 }