test/jdk/javax/imageio/MultiReadTest.java
changeset 47216 71c04702a3d5
parent 35693 5bdca98e8410
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2014, 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  * @bug     8041465
       
    27  * @summary Test verifies that standard ImageIO plugins handle
       
    28  *          multiple reads correctly.
       
    29  *
       
    30  * @run     main MultiReadTest
       
    31  */
       
    32 
       
    33 import java.awt.Color;
       
    34 import java.awt.Graphics2D;
       
    35 import java.awt.Rectangle;
       
    36 import java.awt.image.BufferedImage;
       
    37 import java.io.File;
       
    38 import java.io.IOException;
       
    39 import javax.imageio.ImageIO;
       
    40 import javax.imageio.ImageReadParam;
       
    41 import javax.imageio.ImageReader;
       
    42 import javax.imageio.stream.ImageInputStream;
       
    43 
       
    44 public class MultiReadTest {
       
    45 
       
    46     private static final File pwd = new File(".");
       
    47 
       
    48     private static final Color srcColor = Color.red;
       
    49     private static final int width = 200;
       
    50     private static final int height = 200;
       
    51 
       
    52     private static final int max = 5;
       
    53 
       
    54     public static void main(String[] args) throws IOException {
       
    55         final String[] formats = { "bmp", "png", "gif", "jpg", "tif" };
       
    56 
       
    57         for (String f : formats) {
       
    58             test(f);
       
    59         }
       
    60     }
       
    61 
       
    62     private static void test(String format) throws IOException {
       
    63         System.out.println("Format: " + format);
       
    64 
       
    65         BufferedImage src = createSrc();
       
    66 
       
    67         ImageInputStream iis = prepareInput(src, format);
       
    68 
       
    69         ImageReader reader = ImageIO.getImageReaders(iis).next();
       
    70 
       
    71         reader.setInput(iis);
       
    72 
       
    73         ImageReadParam p = reader.getDefaultReadParam();
       
    74         int cnt = 0;
       
    75         do {
       
    76             System.out.println("cnt: " + cnt);
       
    77             p.setSourceRegion(new Rectangle(width / 4, height / 4,
       
    78                                             width / 2, height / 2));
       
    79 
       
    80             BufferedImage dst = reader.read(0, p);
       
    81 
       
    82             final Color c = new Color(dst.getRGB(10, 10), true);
       
    83 
       
    84             if (!sameColor(c, srcColor)) {
       
    85                 throw new RuntimeException(
       
    86                     String.format("Test failed: read color 0x%X\n",
       
    87                                   c.getRGB()));
       
    88             }
       
    89         } while (++cnt < max);
       
    90     }
       
    91 
       
    92     private static boolean sameColor(Color c1, Color c2) {
       
    93         final float delta = 0.1f;
       
    94 
       
    95         float[] rgb1 = new float[4];
       
    96         float[] rgb2 = new float[4];
       
    97 
       
    98         rgb1 = c1.getRGBComponents(rgb1);
       
    99         rgb2 = c2.getRGBComponents(rgb2);
       
   100 
       
   101         for (int i = 0; i < rgb1.length; i++) {
       
   102             if (Math.abs(rgb1[i] - rgb2[i]) > delta) {
       
   103                 return false;
       
   104             }
       
   105         }
       
   106         return true;
       
   107     }
       
   108 
       
   109     private static BufferedImage createSrc() {
       
   110         BufferedImage img = new BufferedImage(width, height,
       
   111                 BufferedImage.TYPE_INT_RGB);
       
   112 
       
   113         Graphics2D g = img.createGraphics();
       
   114         g.setColor(srcColor);
       
   115         g.fillRect(0, 0, width, height);
       
   116         g.dispose();
       
   117 
       
   118         return img;
       
   119     }
       
   120 
       
   121     private static ImageInputStream prepareInput(BufferedImage src, String format) throws IOException {
       
   122         File f = File.createTempFile("src_", "." + format, pwd);
       
   123 
       
   124         if (ImageIO.write(src, format, f)) {
       
   125             return ImageIO.createImageInputStream(f);
       
   126         }
       
   127         return null;
       
   128     }
       
   129 }