Path: blob/master/test/jdk/javax/imageio/MultiReadTest.java
41145 views
/*1* Copyright (c) 2014, 2016, 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 804146526* @summary Test verifies that standard ImageIO plugins handle27* multiple reads correctly.28*29* @run main MultiReadTest30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.Rectangle;35import java.awt.image.BufferedImage;36import java.io.File;37import java.io.IOException;38import javax.imageio.ImageIO;39import javax.imageio.ImageReadParam;40import javax.imageio.ImageReader;41import javax.imageio.stream.ImageInputStream;4243public class MultiReadTest {4445private static final File pwd = new File(".");4647private static final Color srcColor = Color.red;48private static final int width = 200;49private static final int height = 200;5051private static final int max = 5;5253public static void main(String[] args) throws IOException {54final String[] formats = { "bmp", "png", "gif", "jpg", "tif" };5556for (String f : formats) {57test(f);58}59}6061private static void test(String format) throws IOException {62System.out.println("Format: " + format);6364BufferedImage src = createSrc();6566ImageInputStream iis = prepareInput(src, format);6768ImageReader reader = ImageIO.getImageReaders(iis).next();6970reader.setInput(iis);7172ImageReadParam p = reader.getDefaultReadParam();73int cnt = 0;74do {75System.out.println("cnt: " + cnt);76p.setSourceRegion(new Rectangle(width / 4, height / 4,77width / 2, height / 2));7879BufferedImage dst = reader.read(0, p);8081final Color c = new Color(dst.getRGB(10, 10), true);8283if (!sameColor(c, srcColor)) {84throw new RuntimeException(85String.format("Test failed: read color 0x%X\n",86c.getRGB()));87}88} while (++cnt < max);89}9091private static boolean sameColor(Color c1, Color c2) {92final float delta = 0.1f;9394float[] rgb1 = new float[4];95float[] rgb2 = new float[4];9697rgb1 = c1.getRGBComponents(rgb1);98rgb2 = c2.getRGBComponents(rgb2);99100for (int i = 0; i < rgb1.length; i++) {101if (Math.abs(rgb1[i] - rgb2[i]) > delta) {102return false;103}104}105return true;106}107108private static BufferedImage createSrc() {109BufferedImage img = new BufferedImage(width, height,110BufferedImage.TYPE_INT_RGB);111112Graphics2D g = img.createGraphics();113g.setColor(srcColor);114g.fillRect(0, 0, width, height);115g.dispose();116117return img;118}119120private static ImageInputStream prepareInput(BufferedImage src, String format) throws IOException {121File f = File.createTempFile("src_", "." + format, pwd);122123if (ImageIO.write(src, format, f)) {124return ImageIO.createImageInputStream(f);125}126return null;127}128}129130131