Path: blob/master/test/jdk/javax/imageio/plugins/shared/RepeatingWriteTest.java
41153 views
/*1* Copyright (c) 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*26* @bug 8144991 815015427* @author a.stepanov28* @summary Check if repeating image writing doesn't fail29* (particularly, no AIOOB occurs)30*31* @run main RepeatingWriteTest32*/333435import java.awt.*;36import java.awt.image.BufferedImage;37import java.io.*;38import javax.imageio.*;39import javax.imageio.stream.*;40import java.util.Iterator;41import java.util.Locale;42import javax.imageio.plugins.bmp.BMPImageWriteParam;43import javax.imageio.plugins.jpeg.JPEGImageWriteParam;4445public class RepeatingWriteTest {4647private static final int TYPES[] = new int[]{48BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB,49BufferedImage.TYPE_INT_ARGB_PRE, BufferedImage.TYPE_INT_BGR,50BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR,51BufferedImage.TYPE_4BYTE_ABGR_PRE, BufferedImage.TYPE_BYTE_GRAY,52BufferedImage.TYPE_USHORT_GRAY, BufferedImage.TYPE_BYTE_BINARY,53BufferedImage.TYPE_BYTE_INDEXED, BufferedImage.TYPE_USHORT_565_RGB,54BufferedImage.TYPE_USHORT_555_RGB};5556private static final String NAMES[] = new String[]{57"TYPE_INT_RGB", "TYPE_INT_ARGB",58"TYPE_INT_ARGB_PRE", "TYPE_INT_BGR",59"TYPE_3BYTE_BGR", "TYPE_4BYTE_ABGR",60"TYPE_4BYTE_ABGR_PRE", "TYPE_BYTE_GRAY",61"TYPE_USHORT_GRAY", "TYPE_BYTE_BINARY",62"TYPE_BYTE_INDEXED", "TYPE_USHORT_565_RGB",63"TYPE_USHORT_555_RGB"};6465private static final int SZ1 = 200, SZ2 = 100;66private static final Color C1 = Color.BLACK, C2 = Color.WHITE;6768private static ImageWriter getWriter(String format) {69Iterator<ImageWriter> writers =70ImageIO.getImageWritersByFormatName(format);71if (!writers.hasNext()) {72throw new RuntimeException("no writers available for " + format);73}74return writers.next();75}7677private static ImageReader getReader(String format) {78Iterator<ImageReader> readers =79ImageIO.getImageReadersByFormatName(format);80if (!readers.hasNext()) {81throw new RuntimeException("no readers available for " + format);82}83return readers.next();84}8586private static class ImageCheckException extends Exception {87public ImageCheckException(String msg) { super(msg); }88}8990private final String format;9192public RepeatingWriteTest(String format) { this.format = format; }9394private void checkImage(String fileName, boolean is1st) throws Exception {9596Color cRef = is1st ? C1 : C2;97int szRef = is1st ? SZ1 : SZ2;9899ImageReader reader = getReader(format);100ImageInputStream iis = ImageIO.createImageInputStream(new File(fileName));101reader.setInput(iis);102BufferedImage img = reader.read(0);103Color c = new Color(img.getRGB(szRef / 2, szRef / 2));104int w = img.getWidth(), h = img.getHeight();105106if (w != szRef || h != szRef) {107throw new ImageCheckException(fileName +108": invalid image size " + w + " x " + h +109" expected " + szRef + " x " + szRef);110}111112if (!c.equals(cRef)) {113throw new ImageCheckException(fileName +114": invalid image color " + c +115" expected " + cRef);116}117}118119private void doTest(int i, int j) throws Exception {120121String pair = NAMES[i] + " " + NAMES[j];122123// some type checks: avoid IO exceptions124if ((format.equals("jpeg") || format.equals("bmp")) &&125(pair.contains("USHORT_GRAY") ||126pair.contains("ARGB") || pair.contains("ABGR"))) {127return;128}129130// If JDK-8163323 is fixed this if block should be removed.131if (format.equals("tiff")132&& (NAMES[i].equals("TYPE_USHORT_555_RGB")133|| NAMES[i].equals("TYPE_USHORT_565_RGB"))134&& (NAMES[j].equals("TYPE_USHORT_555_RGB")135|| NAMES[j].equals("TYPE_USHORT_565_RGB"))) {136return;137}138139String f1 = "test-1-" + NAMES[i] + "." + format;140String f2 = "test-2-" + NAMES[j] + "." + format;141142ImageWriter writer = getWriter(format);143144// --- write 1st image ---145OutputStream s = new BufferedOutputStream(new FileOutputStream(f1));146ImageOutputStream ios = ImageIO.createImageOutputStream(s);147writer.setOutput(ios);148149BufferedImage img = new BufferedImage(SZ1, SZ1, TYPES[i]);150Graphics g = img.getGraphics();151g.setColor(C1);152g.fillRect(0, 0, SZ1, SZ1);153g.dispose();154155if (format.equals("jpeg")) {156writer.write(null, new IIOImage(img, null, null),157new JPEGImageWriteParam(Locale.getDefault()));158} if (format.equals("bmp")) {159writer.write(null, new IIOImage(img, null, null),160new BMPImageWriteParam());161} else {162writer.write(img);163}164ios.flush();165s.close();166167// --- write 2nd image ---168s = new BufferedOutputStream(new FileOutputStream(f2));169ios = ImageIO.createImageOutputStream(s);170writer.setOutput(ios);171172img = new BufferedImage(SZ2, SZ2, TYPES[j]);173g = img.getGraphics();174g.setColor(C2);175g.fillRect(0, 0, SZ2, SZ2);176g.dispose();177178if (format.equals("jpeg")) {179writer.write(null, new IIOImage(img, null, null),180new JPEGImageWriteParam(Locale.getDefault()));181} if (format.equals("bmp")) {182writer.write(null, new IIOImage(img, null, null),183new BMPImageWriteParam());184} else {185writer.write(img);186}187ios.flush();188s.close();189190// --- check files ---191checkImage(f1, true);192checkImage(f2, false);193}194195public static void main(String args[]) throws Exception {196197198int n = TYPES.length;199int nAIOOB = 0, nChecksFailed = 0;200201String formats[] = {"bmp", "jpeg", "gif", "png", "tiff"};202203for (String f: formats) {204System.out.println("\nformat: " + f);205RepeatingWriteTest test = new RepeatingWriteTest(f);206for (int i = 0; i < n; ++i) {207for (int j = 0; j < n; ++j) {208try {209test.doTest(i, j);210} catch (ArrayIndexOutOfBoundsException e) {211System.err.println(f + ": AIOOB for pair " +212NAMES[i] + ", " + NAMES[j] + ": " + e.getMessage());213nAIOOB++;214} catch (ImageCheckException e) {215System.err.println(f +216": image check failed for " + e.getMessage());217nChecksFailed++;218}219}220}221}222223if (nAIOOB > 0 || nChecksFailed > 0) {224throw new RuntimeException("test failed: " + nAIOOB + " AIOOBs, " +225nChecksFailed + " image check failures");226}227}228}229230231