1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8149028
* @author a.stepanov
* @summary check TIFFDirectory manipulation
* by means of TIFFImageReadParam
* @run main TIFFImageReadParamTest
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.plugins.tiff.*;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
public class TIFFImageReadParamTest {
private final static String FILENAME = "test.tiff";
private final static int SZ = 100;
private final static Color C = Color.RED;
private final static String GEO_DATA = "test params";
private final static int GEO_N = GeoTIFFTagSet.TAG_GEO_ASCII_PARAMS;
private final static String EXIF_DATA = "2000:01:01 00:00:01";
private final static int EXIF_N = ExifTIFFTagSet.TAG_DATE_TIME_ORIGINAL;
private final static String GPS_DATA =
ExifGPSTagSet.STATUS_MEASUREMENT_IN_PROGRESS;
private final static int GPS_N = ExifGPSTagSet.TAG_GPS_STATUS;
private final static short FAX_DATA =
FaxTIFFTagSet.CLEAN_FAX_DATA_ERRORS_UNCORRECTED;
private final static int FAX_N = FaxTIFFTagSet.TAG_CLEAN_FAX_DATA;
private ImageWriter getTIFFWriter() {
java.util.Iterator<ImageWriter> writers =
ImageIO.getImageWritersByFormatName("TIFF");
if (!writers.hasNext()) {
throw new RuntimeException("No writers available for TIFF format");
}
return writers.next();
}
private ImageReader getTIFFReader() {
java.util.Iterator<ImageReader> readers =
ImageIO.getImageReadersByFormatName("TIFF");
if (!readers.hasNext()) {
throw new RuntimeException("No readers available for TIFF format");
}
return readers.next();
}
private void check(boolean ok, String msg) {
if (!ok) { throw new RuntimeException(msg); }
}
private void addASCIIField(TIFFDirectory d,
String name,
String data,
int num) {
String a[] = {data};
d.addTIFFField(new TIFFField(
new TIFFTag(name, num, 1 << TIFFTag.TIFF_ASCII),
TIFFTag.TIFF_ASCII, 1, a));
}
private void checkASCIIValue(TIFFDirectory d,
String what,
String data,
int num) {
TIFFField f = d.getTIFFField(num);
check(f.getType() == TIFFTag.TIFF_ASCII, "field type != ASCII");
check(f.getCount() == 1, "invalid " + what + " data count");
check(f.getValueAsString(0).equals(data),
"invalid " + what + " data");
}
private void writeImage() throws Exception {
OutputStream s = new BufferedOutputStream(new FileOutputStream(FILENAME));
try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {
ImageWriter writer = getTIFFWriter();
writer.setOutput(ios);
BufferedImage img =
new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(C);
g.fillRect(0, 0, SZ, SZ);
g.dispose();
IIOMetadata metadata = writer.getDefaultImageMetadata(
new ImageTypeSpecifier(img), writer.getDefaultWriteParam());
TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);
// add some extension tags
addASCIIField(dir, "GeoAsciiParamsTag", GEO_DATA, GEO_N);
addASCIIField(dir, "DateTimeOriginal", EXIF_DATA, EXIF_N);
addASCIIField(dir, "GPSStatus", GPS_DATA, GPS_N);
dir.addTIFFField(new TIFFField(new TIFFTag(
"CleanFaxData", FAX_N, 1 << TIFFTag.TIFF_SHORT), FAX_DATA));
IIOMetadata data = dir.getAsMetadata();
writer.write(new IIOImage(img, null, data));
ios.flush();
writer.dispose();
}
}
private void checkImage(BufferedImage img) {
check(img.getWidth() == SZ, "invalid image width");
check(img.getHeight() == SZ, "invalid image height");
Color c = new Color(img.getRGB(SZ / 2, SZ / 2));
check(c.equals(C), "invalid image color");
}
private TIFFDirectory getDir(TIFFTagSet[] add,
TIFFTagSet[] remove) throws Exception {
ImageReader reader = getTIFFReader();
ImageInputStream s = ImageIO.createImageInputStream(new File(FILENAME));
reader.setInput(s, false, false);
int ni = reader.getNumImages(true);
check(ni == 1, "invalid number of images: " + ni);
TIFFImageReadParam param = new TIFFImageReadParam();
for (TIFFTagSet ts: add) { param.addAllowedTagSet(ts); }
for (TIFFTagSet ts: remove) { param.removeAllowedTagSet(ts); }
IIOImage img = reader.readAll(0, param);
// just in case, check image
checkImage((BufferedImage) img.getRenderedImage());
IIOMetadata metadata = img.getMetadata();
TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);
reader.dispose();
s.close();
return dir;
}
private void simpleChecks() {
TIFFImageReadParam param = new TIFFImageReadParam();
java.util.List<TIFFTagSet> allowed = param.getAllowedTagSets();
// see docs
check(allowed.contains(BaselineTIFFTagSet.getInstance()),
"must contain BaselineTIFFTagSet");
check(allowed.contains(FaxTIFFTagSet.getInstance()),
"must contain FaxTIFFTagSet");
check(allowed.contains(ExifParentTIFFTagSet.getInstance()),
"must contain ExifParentTIFFTagSet");
check(allowed.contains(GeoTIFFTagSet.getInstance()),
"must contain GeoTIFFTagSet");
TIFFTagSet gps = ExifGPSTagSet.getInstance();
param.addAllowedTagSet(gps);
check(param.getAllowedTagSets().contains(gps),
"must contain ExifGPSTagSet");
param.removeAllowedTagSet(gps);
check(!param.getAllowedTagSets().contains(gps),
"must not contain ExifGPSTagSet");
// check that repeating remove goes properly
param.removeAllowedTagSet(gps);
boolean ok = false;
try { param.addAllowedTagSet(null); }
catch (IllegalArgumentException e) { ok = true; }
check(ok, "must not be able to add null tag set");
ok = false;
try { param.removeAllowedTagSet(null); }
catch (IllegalArgumentException e) { ok = true; }
check(ok, "must not be able to remove null tag set");
}
private void run() {
simpleChecks();
try {
writeImage();
TIFFTagSet
empty[] = {},
geo[] = { GeoTIFFTagSet.getInstance() },
exif[] = { ExifTIFFTagSet.getInstance() },
gps[] = { ExifGPSTagSet.getInstance() },
fax[] = { FaxTIFFTagSet.getInstance() };
// default param state
TIFFDirectory dir = getDir(empty, empty);
// Geo and Fax are default allowed tag sets
check(dir.containsTIFFField(GEO_N), "must contain Geo field");
checkASCIIValue(dir, "Geo", GEO_DATA, GEO_N);
check(dir.containsTIFFField(FAX_N), "must contain Fax field");
check(
(dir.getTIFFField(FAX_N).getCount() == 1) &&
(dir.getTIFFField(FAX_N).getAsInt(0) == FAX_DATA),
"invalid Fax field value");
// corresponding tag sets are non-default
check(!dir.containsTIFFField(EXIF_N), "must not contain Geo field");
check(!dir.containsTIFFField(GPS_N), "must not contain GPS field");
// remove Fax
dir = getDir(empty, fax);
check(!dir.containsTIFFField(FAX_N), "must not contain Fax field");
// add EXIF, remove Geo
dir = getDir(exif, geo);
check(dir.containsTIFFField(EXIF_N), "must contain EXIF field");
checkASCIIValue(dir, "EXIF", EXIF_DATA, EXIF_N);
check(!dir.containsTIFFField(GEO_N), "must not contain Geo field");
// add GPS
dir = getDir(gps, empty);
check(dir.containsTIFFField(GPS_N), "must contain GPS field");
checkASCIIValue(dir, "GPS", GPS_DATA, GPS_N);
} catch (Exception e) { throw new RuntimeException(e); }
}
public static void main(String[] args) {
(new TIFFImageReadParamTest()).run();
}
}
|