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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* 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 8152183 8148454
* @author a.stepanov
* @summary check that TIFFields are derived properly for multi-page tiff
* @run main MultiPageImageTIFFFieldTest
*/
import java.awt.*;
import java.awt.color.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import javax.imageio.plugins.tiff.*;
public class MultiPageImageTIFFFieldTest {
private final static String FILENAME = "test.tiff";
private final static int W1 = 20, H1 = 40, W2 = 100, H2 = 15;
private final static Color C1 = Color.BLACK, C2 = Color.RED;
private final static int N_WIDTH = BaselineTIFFTagSet.TAG_IMAGE_WIDTH;
private final static int N_HEIGHT = BaselineTIFFTagSet.TAG_IMAGE_LENGTH;
private static final String DESCRIPTION_1[] = {"Description-1", "abc ABC"};
private static final String DESCRIPTION_2[] = {"Description-2", "1-2-3"};
private final static int N_DESCRIPTION =
BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION;
private final static String EXIF_DATA_1[] = {"2001:01:01 00:00:01"};
private final static String EXIF_DATA_2[] = {"2002:02:02 00:00:02"};
private final static int N_EXIF = ExifTIFFTagSet.TAG_DATE_TIME_ORIGINAL;
private final static String GPS_DATA[] = {
ExifGPSTagSet.STATUS_MEASUREMENT_IN_PROGRESS};
private final static int N_GPS = ExifGPSTagSet.TAG_GPS_STATUS;
private final static short FAX_DATA =
FaxTIFFTagSet.CLEAN_FAX_DATA_ERRORS_UNCORRECTED;
private final static int N_FAX = FaxTIFFTagSet.TAG_CLEAN_FAX_DATA;
private static final byte[] ICC_PROFILE_2 =
ICC_ProfileRGB.getInstance(ColorSpace.CS_sRGB).getData();
private static final int N_ICC = BaselineTIFFTagSet.TAG_ICC_PROFILE;
private static final int N_BPS = BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE;
private static final int
COMPRESSION_1 = BaselineTIFFTagSet.COMPRESSION_DEFLATE,
COMPRESSION_2 = BaselineTIFFTagSet.COMPRESSION_LZW;
private static final int N_COMPRESSION = BaselineTIFFTagSet.TAG_COMPRESSION;
private static final int
GRAY_1 = BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO,
GRAY_2 = BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO,
RGB = BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_RGB;
private static final int N_PHOTO =
BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION;
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 addASCIIField(TIFFDirectory d,
String name,
String data[],
int num) {
d.addTIFFField(new TIFFField(
new TIFFTag(name, num, 1 << TIFFTag.TIFF_ASCII),
TIFFTag.TIFF_ASCII, data.length, data));
}
private void checkASCIIField(TIFFDirectory d,
String what,
String data[],
int num) {
String notFound = what + " field was not found";
check(d.containsTIFFField(num), notFound);
TIFFField f = d.getTIFFField(num);
check(f.getType() == TIFFTag.TIFF_ASCII, "field type != ASCII");
check(f.getCount() == data.length, "invalid " + what + " data count");
for (int i = 0; i < data.length; i++) {
check(f.getValueAsString(i).equals(data[i]),
"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 img1 =
new BufferedImage(W1, H1, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = img1.getGraphics();
g.setColor(C1);
g.fillRect(0, 0, W1, H1);
g.dispose();
BufferedImage img2 =
new BufferedImage(W2, H2, BufferedImage.TYPE_INT_RGB);
g = img2.getGraphics();
g.setColor(C2);
g.fillRect(0, 0, W2, H2);
g.dispose();
ImageWriteParam param1 = writer.getDefaultWriteParam();
param1.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param1.setCompressionType("Deflate");
param1.setCompressionQuality(0.5f);
ImageWriteParam param2 = writer.getDefaultWriteParam();
param2.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param2.setCompressionType("LZW");
param2.setCompressionQuality(0.5f);
IIOMetadata
md1 = writer.getDefaultImageMetadata(
new ImageTypeSpecifier(img1), param1),
md2 = writer.getDefaultImageMetadata(
new ImageTypeSpecifier(img2), param2);
TIFFDirectory
dir1 = TIFFDirectory.createFromMetadata(md1),
dir2 = TIFFDirectory.createFromMetadata(md2);
addASCIIField(dir1, "ImageDescription", DESCRIPTION_1, N_DESCRIPTION);
addASCIIField(dir2, "ImageDescription", DESCRIPTION_2, N_DESCRIPTION);
addASCIIField(dir1, "GPSStatus", GPS_DATA, N_GPS);
addASCIIField(dir2, "GPSStatus", GPS_DATA, N_GPS);
addASCIIField(dir1, "DateTimeOriginal", EXIF_DATA_1, N_EXIF);
addASCIIField(dir2, "DateTimeOriginal", EXIF_DATA_2, N_EXIF);
TIFFTag faxTag = new TIFFTag(
"CleanFaxData", N_FAX, 1 << TIFFTag.TIFF_SHORT);
dir1.addTIFFField(new TIFFField(faxTag, FAX_DATA));
dir2.addTIFFField(new TIFFField(faxTag, FAX_DATA));
dir2.addTIFFField(new TIFFField(
new TIFFTag("ICC Profile", N_ICC, 1 << TIFFTag.TIFF_UNDEFINED),
TIFFTag.TIFF_UNDEFINED, ICC_PROFILE_2.length, ICC_PROFILE_2));
writer.prepareWriteSequence(null);
writer.writeToSequence(
new IIOImage(img1, null, dir1.getAsMetadata()), param1);
writer.writeToSequence(
new IIOImage(img2, null, dir2.getAsMetadata()), param2);
writer.endWriteSequence();
ios.flush();
writer.dispose();
}
s.close();
}
private void checkBufferedImages(BufferedImage im1, BufferedImage im2) {
check(im1.getWidth() == W1, "invalid width for image 1");
check(im1.getHeight() == H1, "invalid height for image 1");
check(im2.getWidth() == W2, "invalid width for image 2");
check(im2.getHeight() == H2, "invalid height for image 2");
Color
c1 = new Color(im1.getRGB(W1 / 2, H1 / 2)),
c2 = new Color(im2.getRGB(W2 / 2, H2 / 2));
check(c1.equals(C1), "invalid image 1 color");
check(c2.equals(C2), "invalid image 2 color");
}
private void readAndCheckImage() throws Exception {
ImageReader reader = getTIFFReader();
ImageInputStream s = ImageIO.createImageInputStream(new File(FILENAME));
reader.setInput(s, false, false);
int ni = reader.getNumImages(true);
check(ni == 2, "invalid number of images");
// check TIFFImageReadParam for multipage image
TIFFImageReadParam
param1 = new TIFFImageReadParam(), param2 = new TIFFImageReadParam();
param1.addAllowedTagSet(ExifTIFFTagSet.getInstance());
param1.addAllowedTagSet(ExifGPSTagSet.getInstance());
param2.addAllowedTagSet(ExifTIFFTagSet.getInstance());
param2.addAllowedTagSet(GeoTIFFTagSet.getInstance());
// FaxTIFFTagSet is allowed by default
param2.removeAllowedTagSet(FaxTIFFTagSet.getInstance());
// read images and metadata
IIOImage i1 = reader.readAll(0, param1), i2 = reader.readAll(1, param2);
BufferedImage
bi1 = (BufferedImage) i1.getRenderedImage(),
bi2 = (BufferedImage) i2.getRenderedImage();
// check rendered images, just in case
checkBufferedImages(bi1, bi2);
TIFFDirectory
dir1 = TIFFDirectory.createFromMetadata(i1.getMetadata()),
dir2 = TIFFDirectory.createFromMetadata(i2.getMetadata());
// check ASCII fields
checkASCIIField(
dir1, "image 1 description", DESCRIPTION_1, N_DESCRIPTION);
checkASCIIField(
dir2, "image 2 description", DESCRIPTION_2, N_DESCRIPTION);
checkASCIIField(dir1, "image 1 datetime", EXIF_DATA_1, N_EXIF);
checkASCIIField(dir2, "image 2 datetime", EXIF_DATA_2, N_EXIF);
// check sizes
TIFFField f = dir1.getTIFFField(N_WIDTH);
check((f.getCount() == 1) && (f.getAsInt(0) == W1),
"invalid width field for image 1");
f = dir2.getTIFFField(N_WIDTH);
check((f.getCount() == 1) && (f.getAsInt(0) == W2),
"invalid width field for image 2");
f = dir1.getTIFFField(N_HEIGHT);
check((f.getCount() == 1) && (f.getAsInt(0) == H1),
"invalid height field for image 1");
f = dir2.getTIFFField(N_HEIGHT);
check((f.getCount() == 1) && (f.getAsInt(0) == H2),
"invalid height field for image 2");
// check fax data
check(dir1.containsTIFFField(N_FAX), "image 2 TIFF directory " +
"must contain clean fax data");
f = dir1.getTIFFField(N_FAX);
check(
(f.getCount() == 1) && f.isIntegral() && (f.getAsInt(0) == FAX_DATA),
"invalid clean fax data");
check(!dir2.containsTIFFField(N_FAX), "image 2 TIFF directory " +
"must not contain fax fields");
// check GPS data
checkASCIIField(dir1, "GPS status", GPS_DATA, N_GPS);
check(!dir2.containsTIFFField(N_GPS), "image 2 TIFF directory " +
"must not contain GPS fields");
// check ICC profile data
check(!dir1.containsTIFFField(N_ICC), "image 1 TIFF directory "
+ "must not contain ICC Profile field");
check(dir2.containsTIFFField(N_ICC), "image 2 TIFF directory "
+ "must contain ICC Profile field");
f = dir2.getTIFFField(N_ICC);
check(f.getType() == TIFFTag.TIFF_UNDEFINED,
"invalid ICC profile field type");
int cnt = f.getCount();
byte icc[] = f.getAsBytes();
check((cnt == ICC_PROFILE_2.length) && (cnt == icc.length),
"invalid ICC profile");
for (int i = 0; i < cnt; i++) {
check(icc[i] == ICC_PROFILE_2[i], "invalid ICC profile data");
}
// check component sizes
check(dir1.getTIFFField(N_BPS).isIntegral() &&
dir2.getTIFFField(N_BPS).isIntegral(),
"invalid bits per sample type");
int sz1[] = bi1.getColorModel().getComponentSize(),
sz2[] = bi2.getColorModel().getComponentSize(),
bps1[] = dir1.getTIFFField(N_BPS).getAsInts(),
bps2[] = dir2.getTIFFField(N_BPS).getAsInts();
check((bps1.length == sz1.length) && (bps2.length == sz2.length),
"invalid component size count");
for (int i = 0; i < bps1.length; i++) {
check(bps1[i] == sz1[i], "image 1: invalid bits per sample data");
}
for (int i = 0; i < bps2.length; i++) {
check(bps2[i] == sz2[i], "image 2: invalid bits per sample data");
}
// check compression data
check(dir1.containsTIFFField(N_COMPRESSION) &&
dir2.containsTIFFField(N_COMPRESSION),
"compression info lost");
f = dir1.getTIFFField(N_COMPRESSION);
check(f.isIntegral() && (f.getCount() == 1) &&
(f.getAsInt(0) == COMPRESSION_1), "invalid image 1 compression data");
f = dir2.getTIFFField(N_COMPRESSION);
check(f.isIntegral() && (f.getCount() == 1) &&
(f.getAsInt(0) == COMPRESSION_2), "invalid image 2 compression data");
// check photometric interpretation
f = dir1.getTIFFField(N_PHOTO);
check(f.isIntegral() && (f.getCount() == 1) &&
((f.getAsInt(0) == GRAY_1) || (f.getAsInt(0) == GRAY_2)),
"invalid photometric interpretation for image 1");
f = dir2.getTIFFField(N_PHOTO);
check(f.isIntegral() && (f.getCount() == 1) && (f.getAsInt(0) == RGB),
"invalid photometric interpretation for image 2");
}
public void run() {
try {
writeImage();
readAndCheckImage();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void check(boolean ok, String msg) {
if (!ok) { throw new RuntimeException(msg); }
}
public static void main(String[] args) {
(new MultiPageImageTIFFFieldTest()).run();
}
}
|