Path: blob/master/test/jdk/javax/imageio/plugins/tiff/MultiPageTest/MultiPageTest.java
41159 views
/*1* Copyright (c) 2016, 2017, 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* @library /test/lib26*27* @bug 814577628* @author a.stepanov29* @summary A simple write-read test for the multi-page tiff.30* Create the file programmaticaly, then do some simple checks31* (number of pages, sizes, colors). Use -Dseed=X to set32* the random generator seed.33*34* @build jdk.test.lib.RandomFactory35* @run main MultiPageTest36* @key randomness37*/383940import java.awt.*;41import java.awt.image.*;42import java.io.*;4344import java.util.*;4546import javax.imageio.*;47import javax.imageio.stream.*;4849import jdk.test.lib.RandomFactory;505152public class MultiPageTest {5354private final String fileName;5556private final int NUM_IMAGES = 51;5758private final static Random rnd = RandomFactory.getRandom();5960private final int w[], h[];61private final Color colors[];62private final int BLACK_SIZE = 100;6364private final int imageType;656667public MultiPageTest(int type, String tName) {6869imageType = type;70fileName = "test__" + tName + ".tif";7172w = new int[NUM_IMAGES + 4];73h = new int[NUM_IMAGES + 4];7475for (int i = 2; i < NUM_IMAGES + 2; i++) {76w[i] = 10 + rnd.nextInt(21);77h[i] = 10 + rnd.nextInt(21);78}7980w[0] = BLACK_SIZE; h[0] = BLACK_SIZE;81w[1] = BLACK_SIZE; h[1] = BLACK_SIZE;82w[NUM_IMAGES + 2] = BLACK_SIZE; h[NUM_IMAGES + 2] = BLACK_SIZE;83w[NUM_IMAGES + 3] = BLACK_SIZE; h[NUM_IMAGES + 3] = BLACK_SIZE;848586colors = new Color[NUM_IMAGES + 4];87for (int i = 2; i < NUM_IMAGES + 2; ++i) {88colors[i] = new Color(89rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));90}9192colors[0] = Color.black;93colors[1] = Color.black;94colors[NUM_IMAGES + 2] = Color.black;95colors[NUM_IMAGES + 3] = Color.black;96}979899private ImageWriter getTIFFWriter() {100101Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("TIFF");102if (!writers.hasNext()) {103throw new RuntimeException("No writers available for " + fileName);104}105return writers.next();106}107108private ImageReader getTIFFReader() {109110Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("TIFF");111if (!readers.hasNext()) {112throw new RuntimeException("No readers available for " + fileName);113}114return readers.next();115}116117118private void createImage() throws Exception {119120OutputStream s = new BufferedOutputStream(new FileOutputStream(fileName));121try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {122123ImageWriter writer = getTIFFWriter();124writer.setOutput(ios);125126Graphics g;127128BufferedImage blackImg =129new BufferedImage(BLACK_SIZE, BLACK_SIZE, imageType);130g = blackImg.getGraphics();131g.setColor(Color.black);132g.fillRect(0, 0, BLACK_SIZE, BLACK_SIZE);133134writer.prepareWriteSequence(null);135136for (int i = 2; i < NUM_IMAGES + 2; i++) {137BufferedImage img = new BufferedImage(w[i], h[i], imageType);138139g = img.getGraphics();140g.setColor(colors[i]);141g.fillRect(0, 0, w[i], h[i]);142143writer.writeToSequence(new IIOImage(img, null, null), null);144}145146writer.endWriteSequence();147148// check: insert to the beginning149writer.writeInsert(0, new IIOImage(blackImg, null, null), null);150151// check: insert to non-zero position152writer.writeInsert(1, new IIOImage(blackImg, null, null), null);153154// check: append to the end by index155writer.writeInsert(NUM_IMAGES + 2,156new IIOImage(blackImg, null, null), null);157158// check: append to the end using index "-1"159writer.writeInsert(-1, new IIOImage(blackImg, null, null), null);160161ios.flush();162writer.dispose();163}164s.close();165}166167168169private void readAndCheckImage() throws Exception {170171ImageReader reader = getTIFFReader();172173ImageInputStream s = ImageIO.createImageInputStream(new File(fileName));174reader.setInput(s);175176177// check number of pages178if ((NUM_IMAGES + 4) != reader.getNumImages(true)) {179throw new RuntimeException("invalid number of images!");180}181182// check colors / sizes183for (int i = 0; i < NUM_IMAGES + 4; i++) {184185BufferedImage img = reader.read(i);186187int imw = w[i], imh = h[i];188189if ( (img.getWidth() != imw) || (img.getHeight() != imh) ) {190throw new RuntimeException("NOK: size(" + i + ")");191}192193Color194c1 = new Color(img.getRGB(0, 0)),195c2 = new Color(img.getRGB(imw / 2, imh / 2)),196c3 = new Color(img.getRGB(imw - 1, imh - 1));197if (! (c1.equals(colors[i]) && c1.equals(c2) && c1.equals(c3) ) ) {198throw new RuntimeException("NOK: color(" + i + ")");199}200}201202reader.dispose();203s.close();204}205206public void doTest() throws Exception {207createImage();208readAndCheckImage();209}210211public static void main(String[] args) throws Exception {212213int types[] = new int[]{214BufferedImage.TYPE_INT_RGB,215BufferedImage.TYPE_INT_ARGB,216BufferedImage.TYPE_INT_ARGB_PRE,217BufferedImage.TYPE_INT_BGR,218BufferedImage.TYPE_3BYTE_BGR,219BufferedImage.TYPE_4BYTE_ABGR,220BufferedImage.TYPE_4BYTE_ABGR_PRE221};222223String names[] = new String[]{224"TYPE_INT_RGB",225"TYPE_INT_ARGB",226"TYPE_INT_ARGB_PRE",227"TYPE_INT_BGR",228"TYPE_3BYTE_BGR",229"TYPE_4BYTE_ABGR",230"TYPE_4BYTE_ABGR_PRE"231};232233for (int i = 0; i < types.length; i++) {234System.out.println("image type: " + names[i]);235(new MultiPageTest(types[i], names[i])).doTest();236}237}238}239240241