Path: blob/master/test/jdk/javax/imageio/plugins/tiff/TIFFDirectoryWriteReadTest.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* @bug 814902826* @author a.stepanov27* @summary a simple write-read test for TIFFDirectory28* @run main TIFFDirectoryWriteReadTest29*/3031import java.awt.*;32import java.awt.color.*;33import java.awt.image.BufferedImage;34import java.io.*;35import javax.imageio.*;36import javax.imageio.metadata.*;37import javax.imageio.stream.*;38import javax.imageio.plugins.tiff.*;394041public class TIFFDirectoryWriteReadTest {4243private final static String FILENAME = "test.tiff";44private final static int SZ = 100;45private final static Color C = Color.RED;4647private static final String COPYRIGHT[] = {"Copyright 123ABC.."};48private static final String DESCRIPTION[] = {"Test Image", "Description"};49private static final String SOFTWARE[] = {"test", "software", "123"};5051private static final long RES_X[][] = {{2, 1}}, RES_Y[][] = {{1, 1}};5253private static final byte[] ICC_PROFILE =54ICC_ProfileRGB.getInstance(ColorSpace.CS_sRGB).getData();555657private ImageWriter getTIFFWriter() {5859java.util.Iterator<ImageWriter> writers =60ImageIO.getImageWritersByFormatName("TIFF");61if (!writers.hasNext()) {62throw new RuntimeException("No writers available for TIFF format");63}64return writers.next();65}6667private ImageReader getTIFFReader() {6869java.util.Iterator<ImageReader> readers =70ImageIO.getImageReadersByFormatName("TIFF");71if (!readers.hasNext()) {72throw new RuntimeException("No readers available for TIFF format");73}74return readers.next();75}7677private void addASCIIField(TIFFDirectory d,78String name,79String data[],80int num) {8182d.addTIFFField(new TIFFField(83new TIFFTag(name, num, 1 << TIFFTag.TIFF_ASCII),84TIFFTag.TIFF_ASCII, data.length, data));85}8687private void checkASCIIField(TIFFDirectory d,88String what,89String data[],90int num) {9192String notFound = what + " field was not found";93check(d.containsTIFFField(num), notFound);94TIFFField f = d.getTIFFField(num);95check(f.getType() == TIFFTag.TIFF_ASCII, "field type != ASCII");96check(f.getCount() == data.length, "invalid " + what + " data count");97for (int i = 0; i < data.length; i++) {98check(f.getValueAsString(i).equals(data[i]),99"invalid " + what + " data");100}101}102103private void writeImage() throws Exception {104105OutputStream s = new BufferedOutputStream(new FileOutputStream(FILENAME));106try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {107108ImageWriter writer = getTIFFWriter();109writer.setOutput(ios);110111BufferedImage img = new BufferedImage(112SZ, SZ, BufferedImage.TYPE_INT_RGB);113Graphics g = img.getGraphics();114g.setColor(C);115g.fillRect(0, 0, SZ, SZ);116g.dispose();117118IIOMetadata metadata = writer.getDefaultImageMetadata(119new ImageTypeSpecifier(img), writer.getDefaultWriteParam());120121TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);122123addASCIIField(dir, "Copyright",124COPYRIGHT, BaselineTIFFTagSet.TAG_COPYRIGHT);125126addASCIIField(dir, "ImageDescription",127DESCRIPTION, BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION);128129addASCIIField(dir, "Software",130SOFTWARE, BaselineTIFFTagSet.TAG_SOFTWARE);131132dir.addTIFFField(new TIFFField(133new TIFFTag("XResolution", BaselineTIFFTagSet.TAG_X_RESOLUTION,1341 << TIFFTag.TIFF_RATIONAL), TIFFTag.TIFF_RATIONAL, 1, RES_X));135dir.addTIFFField(new TIFFField(136new TIFFTag("YResolution", BaselineTIFFTagSet.TAG_Y_RESOLUTION,1371 << TIFFTag.TIFF_RATIONAL), TIFFTag.TIFF_RATIONAL, 1, RES_Y));138139dir.addTIFFField(new TIFFField(140new TIFFTag("ICC Profile", BaselineTIFFTagSet.TAG_ICC_PROFILE,1411 << TIFFTag.TIFF_UNDEFINED),142TIFFTag.TIFF_UNDEFINED, ICC_PROFILE.length, ICC_PROFILE));143144IIOMetadata data = dir.getAsMetadata();145writer.write(new IIOImage(img, null, data));146147ios.flush();148writer.dispose();149}150s.close();151}152153154155private void readAndCheckImage() throws Exception {156157ImageReader reader = getTIFFReader();158159ImageInputStream s = ImageIO.createImageInputStream(new File(FILENAME));160reader.setInput(s);161162int ni = reader.getNumImages(true);163check(ni == 1, "invalid number of images");164165// check image166BufferedImage img = reader.read(0);167check(img.getWidth() == SZ && img.getHeight() == SZ,168"invalid image size");169170Color c = new Color(img.getRGB(SZ / 2, SZ / 2));171check(C.equals(c), "invalid image color");172173IIOMetadata metadata = reader.readAll(0, null).getMetadata();174TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);175176reader.dispose();177s.close();178179// ===== perform tag checks =====180181checkASCIIField(dir, "copyright", COPYRIGHT,182BaselineTIFFTagSet.TAG_COPYRIGHT);183184checkASCIIField(dir, "description", DESCRIPTION,185BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION);186187checkASCIIField(dir, "software", SOFTWARE,188BaselineTIFFTagSet.TAG_SOFTWARE);189190TIFFField f = dir.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);191check(f.getCount() == 1, "invalid width field count");192int w = f.getAsInt(0);193check(w == SZ, "invalid width");194195f = dir.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_LENGTH);196check(f.getCount() == 1, "invalid height field count");197int h = f.getAsInt(0);198check(h == SZ, "invalid height");199200f = dir.getTIFFField(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);201// RGB: 3 x 8 bits for R, G and B components202int bps[] = f.getAsInts();203check((f.getCount() == 3) && (bps.length == 3), "invalid BPS count");204for (int b: bps) { check(b == 8, "invalid bits per sample"); }205206// RGB: PhotometricInterpretation = 2207f = dir.getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);208check(f.getCount() == 1, "invalid count");209check(f.getAsInt(0) == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_RGB,210"invalid photometric interpretation value");211212String rat = " resolution must be rational";213f = dir.getTIFFField(BaselineTIFFTagSet.TAG_X_RESOLUTION);214check(f.getType() == TIFFTag.TIFF_RATIONAL, "x" + rat);215check(f.getCount() == 1 &&216f.getAsInt(0) == (int) (RES_X[0][0] / RES_X[0][1]),217"invalid x resolution");218219f = dir.getTIFFField(BaselineTIFFTagSet.TAG_Y_RESOLUTION);220check(f.getType() == TIFFTag.TIFF_RATIONAL, "y" + rat);221check(f.getCount() == 1 &&222f.getAsInt(0) == (int) (RES_Y[0][0] / RES_Y[0][1]),223"invalid y resolution");224225f = dir.getTIFFField(BaselineTIFFTagSet.TAG_ICC_PROFILE);226check(f.getType() == TIFFTag.TIFF_UNDEFINED,227"invalid ICC profile field type");228int cnt = f.getCount();229byte icc[] = f.getAsBytes();230check((cnt == ICC_PROFILE.length) && (cnt == icc.length),231"invalid ICC profile");232for (int i = 0; i < cnt; i++) {233check(icc[i] == ICC_PROFILE[i], "invalid ICC profile");234}235}236237public void run() {238239try {240writeImage();241readAndCheckImage();242} catch (Exception e) {243throw new RuntimeException(e);244}245246}247248private void check(boolean ok, String msg) {249if (!ok) { throw new RuntimeException(msg); }250}251252public static void main(String[] args) {253(new TIFFDirectoryWriteReadTest()).run();254}255}256257258