Path: blob/master/test/jdk/javax/imageio/plugins/tiff/TIFFImageReadParamTest.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 check TIFFDirectory manipulation28* by means of TIFFImageReadParam29* @run main TIFFImageReadParamTest30*/313233import java.awt.Color;34import java.awt.Graphics;35import java.awt.image.BufferedImage;36import java.io.*;37import javax.imageio.*;38import javax.imageio.metadata.IIOMetadata;39import javax.imageio.plugins.tiff.*;40import javax.imageio.stream.ImageInputStream;41import javax.imageio.stream.ImageOutputStream;4243public class TIFFImageReadParamTest {4445private final static String FILENAME = "test.tiff";46private final static int SZ = 100;47private final static Color C = Color.RED;4849private final static String GEO_DATA = "test params";50private final static int GEO_N = GeoTIFFTagSet.TAG_GEO_ASCII_PARAMS;5152private final static String EXIF_DATA = "2000:01:01 00:00:01";53private final static int EXIF_N = ExifTIFFTagSet.TAG_DATE_TIME_ORIGINAL;5455private final static String GPS_DATA =56ExifGPSTagSet.STATUS_MEASUREMENT_IN_PROGRESS;57private final static int GPS_N = ExifGPSTagSet.TAG_GPS_STATUS;5859private final static short FAX_DATA =60FaxTIFFTagSet.CLEAN_FAX_DATA_ERRORS_UNCORRECTED;61private final static int FAX_N = FaxTIFFTagSet.TAG_CLEAN_FAX_DATA;6263private ImageWriter getTIFFWriter() {6465java.util.Iterator<ImageWriter> writers =66ImageIO.getImageWritersByFormatName("TIFF");67if (!writers.hasNext()) {68throw new RuntimeException("No writers available for TIFF format");69}70return writers.next();71}7273private ImageReader getTIFFReader() {7475java.util.Iterator<ImageReader> readers =76ImageIO.getImageReadersByFormatName("TIFF");77if (!readers.hasNext()) {78throw new RuntimeException("No readers available for TIFF format");79}80return readers.next();81}8283private void check(boolean ok, String msg) {84if (!ok) { throw new RuntimeException(msg); }85}8687private void addASCIIField(TIFFDirectory d,88String name,89String data,90int num) {9192String a[] = {data};93d.addTIFFField(new TIFFField(94new TIFFTag(name, num, 1 << TIFFTag.TIFF_ASCII),95TIFFTag.TIFF_ASCII, 1, a));96}9798private void checkASCIIValue(TIFFDirectory d,99String what,100String data,101int num) {102103TIFFField f = d.getTIFFField(num);104check(f.getType() == TIFFTag.TIFF_ASCII, "field type != ASCII");105check(f.getCount() == 1, "invalid " + what + " data count");106check(f.getValueAsString(0).equals(data),107"invalid " + what + " data");108}109110111private void writeImage() throws Exception {112113OutputStream s = new BufferedOutputStream(new FileOutputStream(FILENAME));114try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {115ImageWriter writer = getTIFFWriter();116writer.setOutput(ios);117118BufferedImage img =119new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);120Graphics g = img.getGraphics();121g.setColor(C);122g.fillRect(0, 0, SZ, SZ);123g.dispose();124125IIOMetadata metadata = writer.getDefaultImageMetadata(126new ImageTypeSpecifier(img), writer.getDefaultWriteParam());127128TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);129130// add some extension tags131addASCIIField(dir, "GeoAsciiParamsTag", GEO_DATA, GEO_N);132addASCIIField(dir, "DateTimeOriginal", EXIF_DATA, EXIF_N);133addASCIIField(dir, "GPSStatus", GPS_DATA, GPS_N);134135dir.addTIFFField(new TIFFField(new TIFFTag(136"CleanFaxData", FAX_N, 1 << TIFFTag.TIFF_SHORT), FAX_DATA));137138IIOMetadata data = dir.getAsMetadata();139140writer.write(new IIOImage(img, null, data));141142ios.flush();143writer.dispose();144}145}146147private void checkImage(BufferedImage img) {148149check(img.getWidth() == SZ, "invalid image width");150check(img.getHeight() == SZ, "invalid image height");151Color c = new Color(img.getRGB(SZ / 2, SZ / 2));152check(c.equals(C), "invalid image color");153}154155private TIFFDirectory getDir(TIFFTagSet[] add,156TIFFTagSet[] remove) throws Exception {157158ImageReader reader = getTIFFReader();159160ImageInputStream s = ImageIO.createImageInputStream(new File(FILENAME));161reader.setInput(s, false, false);162163int ni = reader.getNumImages(true);164check(ni == 1, "invalid number of images: " + ni);165166TIFFImageReadParam param = new TIFFImageReadParam();167for (TIFFTagSet ts: add) { param.addAllowedTagSet(ts); }168for (TIFFTagSet ts: remove) { param.removeAllowedTagSet(ts); }169170IIOImage img = reader.readAll(0, param);171172// just in case, check image173checkImage((BufferedImage) img.getRenderedImage());174175IIOMetadata metadata = img.getMetadata();176TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);177178reader.dispose();179s.close();180181return dir;182}183184private void simpleChecks() {185186TIFFImageReadParam param = new TIFFImageReadParam();187188java.util.List<TIFFTagSet> allowed = param.getAllowedTagSets();189190// see docs191check(allowed.contains(BaselineTIFFTagSet.getInstance()),192"must contain BaselineTIFFTagSet");193check(allowed.contains(FaxTIFFTagSet.getInstance()),194"must contain FaxTIFFTagSet");195check(allowed.contains(ExifParentTIFFTagSet.getInstance()),196"must contain ExifParentTIFFTagSet");197check(allowed.contains(GeoTIFFTagSet.getInstance()),198"must contain GeoTIFFTagSet");199200TIFFTagSet gps = ExifGPSTagSet.getInstance();201param.addAllowedTagSet(gps);202check(param.getAllowedTagSets().contains(gps),203"must contain ExifGPSTagSet");204205param.removeAllowedTagSet(gps);206check(!param.getAllowedTagSets().contains(gps),207"must not contain ExifGPSTagSet");208209// check that repeating remove goes properly210param.removeAllowedTagSet(gps);211212boolean ok = false;213try { param.addAllowedTagSet(null); }214catch (IllegalArgumentException e) { ok = true; }215check(ok, "must not be able to add null tag set");216217ok = false;218try { param.removeAllowedTagSet(null); }219catch (IllegalArgumentException e) { ok = true; }220check(ok, "must not be able to remove null tag set");221}222223private void run() {224225simpleChecks();226227try {228229writeImage();230231TIFFTagSet232empty[] = {},233geo[] = { GeoTIFFTagSet.getInstance() },234exif[] = { ExifTIFFTagSet.getInstance() },235gps[] = { ExifGPSTagSet.getInstance() },236fax[] = { FaxTIFFTagSet.getInstance() };237238// default param state239TIFFDirectory dir = getDir(empty, empty);240// Geo and Fax are default allowed tag sets241check(dir.containsTIFFField(GEO_N), "must contain Geo field");242checkASCIIValue(dir, "Geo", GEO_DATA, GEO_N);243check(dir.containsTIFFField(FAX_N), "must contain Fax field");244check(245(dir.getTIFFField(FAX_N).getCount() == 1) &&246(dir.getTIFFField(FAX_N).getAsInt(0) == FAX_DATA),247"invalid Fax field value");248249// corresponding tag sets are non-default250check(!dir.containsTIFFField(EXIF_N), "must not contain Geo field");251check(!dir.containsTIFFField(GPS_N), "must not contain GPS field");252253// remove Fax254dir = getDir(empty, fax);255check(!dir.containsTIFFField(FAX_N), "must not contain Fax field");256257// add EXIF, remove Geo258dir = getDir(exif, geo);259check(dir.containsTIFFField(EXIF_N), "must contain EXIF field");260checkASCIIValue(dir, "EXIF", EXIF_DATA, EXIF_N);261check(!dir.containsTIFFField(GEO_N), "must not contain Geo field");262263// add GPS264dir = getDir(gps, empty);265check(dir.containsTIFFField(GPS_N), "must contain GPS field");266checkASCIIValue(dir, "GPS", GPS_DATA, GPS_N);267268} catch (Exception e) { throw new RuntimeException(e); }269}270271public static void main(String[] args) {272(new TIFFImageReadParamTest()).run();273}274}275276277