001/* 002 * Copyright 2003-2008 by Paulo Soares. 003 * 004 * This code was originally released in 2001 by SUN (see class 005 * com.sun.media.imageio.plugins.tiff.TIFFField.java) 006 * using the BSD license in a specific wording. In a mail dating from 007 * January 23, 2008, Brian Burkhalter (@sun.com) gave us permission 008 * to use the code under the following version of the BSD license: 009 * 010 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. 011 * 012 * Redistribution and use in source and binary forms, with or without 013 * modification, are permitted provided that the following conditions 014 * are met: 015 * 016 * - Redistribution of source code must retain the above copyright 017 * notice, this list of conditions and the following disclaimer. 018 * 019 * - Redistribution in binary form must reproduce the above copyright 020 * notice, this list of conditions and the following disclaimer in 021 * the documentation and/or other materials provided with the 022 * distribution. 023 * 024 * Neither the name of Sun Microsystems, Inc. or the names of 025 * contributors may be used to endorse or promote products derived 026 * from this software without specific prior written permission. 027 * 028 * This software is provided "AS IS," without a warranty of any 029 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 030 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 031 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 032 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 033 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 034 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS 035 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 036 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, 037 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND 038 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR 039 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 040 * POSSIBILITY OF SUCH DAMAGES. 041 * 042 * You acknowledge that this software is not designed or intended for 043 * use in the design, construction, operation or maintenance of any 044 * nuclear facility. 045 */ 046package com.itextpdf.text.pdf.codec; 047 048import java.io.Serializable; 049 050/** 051 * A class representing a field in a TIFF 6.0 Image File Directory. 052 * 053 * <p> The TIFF file format is described in more detail in the 054 * comments for the TIFFDescriptor class. 055 * 056 * <p> A field in a TIFF Image File Directory (IFD). A field is defined 057 * as a sequence of values of identical data type. TIFF 6.0 defines 058 * 12 data types, which are mapped internally onto the Java data types 059 * byte, int, long, float, and double. 060 * 061 * <p><b> This class is not a committed part of the JAI API. It may 062 * be removed or changed in future releases of JAI.</b> 063 * 064 * @see TIFFDirectory 065 */ 066public class TIFFField extends Object implements Comparable<TIFFField>, Serializable { 067 068 private static final long serialVersionUID = 9088332901412823834L; 069 070 /** Flag for 8 bit unsigned integers. */ 071 public static final int TIFF_BYTE = 1; 072 073 /** Flag for null-terminated ASCII strings. */ 074 public static final int TIFF_ASCII = 2; 075 076 /** Flag for 16 bit unsigned integers. */ 077 public static final int TIFF_SHORT = 3; 078 079 /** Flag for 32 bit unsigned integers. */ 080 public static final int TIFF_LONG = 4; 081 082 /** Flag for pairs of 32 bit unsigned integers. */ 083 public static final int TIFF_RATIONAL = 5; 084 085 /** Flag for 8 bit signed integers. */ 086 public static final int TIFF_SBYTE = 6; 087 088 /** Flag for 8 bit uninterpreted bytes. */ 089 public static final int TIFF_UNDEFINED = 7; 090 091 /** Flag for 16 bit signed integers. */ 092 public static final int TIFF_SSHORT = 8; 093 094 /** Flag for 32 bit signed integers. */ 095 public static final int TIFF_SLONG = 9; 096 097 /** Flag for pairs of 32 bit signed integers. */ 098 public static final int TIFF_SRATIONAL = 10; 099 100 /** Flag for 32 bit IEEE floats. */ 101 public static final int TIFF_FLOAT = 11; 102 103 /** Flag for 64 bit IEEE doubles. */ 104 public static final int TIFF_DOUBLE = 12; 105 106 /** The tag number. */ 107 int tag; 108 109 /** The tag type. */ 110 int type; 111 112 /** The number of data items present in the field. */ 113 int count; 114 115 /** The field data. */ 116 Object data; 117 118 /** The default constructor. */ 119 TIFFField() {} 120 121 /** 122 * Constructs a TIFFField with arbitrary data. The data 123 * parameter must be an array of a Java type appropriate for the 124 * type of the TIFF field. Since there is no available 32-bit 125 * unsigned data type, long is used. The mapping between types is 126 * as follows: 127 * 128 * <table border=1> 129 * <tr> 130 * <th> TIFF type </th> <th> Java type </th> 131 * <tr> 132 * <td><tt>TIFF_BYTE</tt></td> <td><tt>byte</tt></td> 133 * <tr> 134 * <td><tt>TIFF_ASCII</tt></td> <td><tt>String</tt></td> 135 * <tr> 136 * <td><tt>TIFF_SHORT</tt></td> <td><tt>char</tt></td> 137 * <tr> 138 * <td><tt>TIFF_LONG</tt></td> <td><tt>long</tt></td> 139 * <tr> 140 * <td><tt>TIFF_RATIONAL</tt></td> <td><tt>long[2]</tt></td> 141 * <tr> 142 * <td><tt>TIFF_SBYTE</tt></td> <td><tt>byte</tt></td> 143 * <tr> 144 * <td><tt>TIFF_UNDEFINED</tt></td> <td><tt>byte</tt></td> 145 * <tr> 146 * <td><tt>TIFF_SSHORT</tt></td> <td><tt>short</tt></td> 147 * <tr> 148 * <td><tt>TIFF_SLONG</tt></td> <td><tt>int</tt></td> 149 * <tr> 150 * <td><tt>TIFF_SRATIONAL</tt></td> <td><tt>int[2]</tt></td> 151 * <tr> 152 * <td><tt>TIFF_FLOAT</tt></td> <td><tt>float</tt></td> 153 * <tr> 154 * <td><tt>TIFF_DOUBLE</tt></td> <td><tt>double</tt></td> 155 * </table> 156 */ 157 public TIFFField(int tag, int type, int count, Object data) { 158 this.tag = tag; 159 this.type = type; 160 this.count = count; 161 this.data = data; 162 } 163 164 /** 165 * Returns the tag number, between 0 and 65535. 166 */ 167 public int getTag() { 168 return tag; 169 } 170 171 /** 172 * Returns the type of the data stored in the IFD. 173 * For a TIFF6.0 file, the value will equal one of the 174 * TIFF_ constants defined in this class. For future 175 * revisions of TIFF, higher values are possible. 176 * 177 */ 178 public int getType() { 179 return type; 180 } 181 182 /** 183 * Returns the number of elements in the IFD. 184 */ 185 public int getCount() { 186 return count; 187 } 188 189 /** 190 * Returns the data as an uninterpreted array of bytes. 191 * The type of the field must be one of TIFF_BYTE, TIFF_SBYTE, 192 * or TIFF_UNDEFINED; 193 * 194 * <p> For data in TIFF_BYTE format, the application must take 195 * care when promoting the data to longer integral types 196 * to avoid sign extension. 197 * 198 * <p> A ClassCastException will be thrown if the field is not 199 * of type TIFF_BYTE, TIFF_SBYTE, or TIFF_UNDEFINED. 200 */ 201 public byte[] getAsBytes() { 202 return (byte[])data; 203 } 204 205 /** 206 * Returns TIFF_SHORT data as an array of chars (unsigned 16-bit 207 * integers). 208 * 209 * <p> A ClassCastException will be thrown if the field is not 210 * of type TIFF_SHORT. 211 */ 212 public char[] getAsChars() { 213 return (char[])data; 214 } 215 216 /** 217 * Returns TIFF_SSHORT data as an array of shorts (signed 16-bit 218 * integers). 219 * 220 * <p> A ClassCastException will be thrown if the field is not 221 * of type TIFF_SSHORT. 222 */ 223 public short[] getAsShorts() { 224 return (short[])data; 225 } 226 227 /** 228 * Returns TIFF_SLONG data as an array of ints (signed 32-bit 229 * integers). 230 * 231 * <p> A ClassCastException will be thrown if the field is not 232 * of type TIFF_SLONG. 233 */ 234 public int[] getAsInts() { 235 return (int[])data; 236 } 237 238 /** 239 * Returns TIFF_LONG data as an array of longs (signed 64-bit 240 * integers). 241 * 242 * <p> A ClassCastException will be thrown if the field is not 243 * of type TIFF_LONG. 244 */ 245 public long[] getAsLongs() { 246 return (long[])data; 247 } 248 249 /** 250 * Returns TIFF_FLOAT data as an array of floats. 251 * 252 * <p> A ClassCastException will be thrown if the field is not 253 * of type TIFF_FLOAT. 254 */ 255 public float[] getAsFloats() { 256 return (float[])data; 257 } 258 259 /** 260 * Returns TIFF_DOUBLE data as an array of doubles. 261 * 262 * <p> A ClassCastException will be thrown if the field is not 263 * of type TIFF_DOUBLE. 264 */ 265 public double[] getAsDoubles() { 266 return (double[])data; 267 } 268 269 /** 270 * Returns TIFF_SRATIONAL data as an array of 2-element arrays of ints. 271 * 272 * <p> A ClassCastException will be thrown if the field is not 273 * of type TIFF_SRATIONAL. 274 */ 275 public int[][] getAsSRationals() { 276 return (int[][])data; 277 } 278 279 /** 280 * Returns TIFF_RATIONAL data as an array of 2-element arrays of longs. 281 * 282 * <p> A ClassCastException will be thrown if the field is not 283 * of type TIFF_RATTIONAL. 284 */ 285 public long[][] getAsRationals() { 286 return (long[][])data; 287 } 288 289 /** 290 * Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT, 291 * TIFF_SSHORT, or TIFF_SLONG format as an int. 292 * 293 * <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned; 294 * that is, no sign extension will take place and the returned 295 * value will be in the range [0, 255]. TIFF_SBYTE data will 296 * be returned in the range [-128, 127]. 297 * 298 * <p> A ClassCastException will be thrown if the field is not of 299 * type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT, 300 * TIFF_SSHORT, or TIFF_SLONG. 301 */ 302 public int getAsInt(int index) { 303 switch (type) { 304 case TIFF_BYTE: case TIFF_UNDEFINED: 305 return ((byte[])data)[index] & 0xff; 306 case TIFF_SBYTE: 307 return ((byte[])data)[index]; 308 case TIFF_SHORT: 309 return ((char[])data)[index] & 0xffff; 310 case TIFF_SSHORT: 311 return ((short[])data)[index]; 312 case TIFF_SLONG: 313 return ((int[])data)[index]; 314 default: 315 throw new ClassCastException(); 316 } 317 } 318 319 /** 320 * Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT, 321 * TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG format as a long. 322 * 323 * <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned; 324 * that is, no sign extension will take place and the returned 325 * value will be in the range [0, 255]. TIFF_SBYTE data will 326 * be returned in the range [-128, 127]. 327 * 328 * <p> A ClassCastException will be thrown if the field is not of 329 * type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT, 330 * TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG. 331 */ 332 public long getAsLong(int index) { 333 switch (type) { 334 case TIFF_BYTE: case TIFF_UNDEFINED: 335 return ((byte[])data)[index] & 0xff; 336 case TIFF_SBYTE: 337 return ((byte[])data)[index]; 338 case TIFF_SHORT: 339 return ((char[])data)[index] & 0xffff; 340 case TIFF_SSHORT: 341 return ((short[])data)[index]; 342 case TIFF_SLONG: 343 return ((int[])data)[index]; 344 case TIFF_LONG: 345 return ((long[])data)[index]; 346 default: 347 throw new ClassCastException(); 348 } 349 } 350 351 /** 352 * Returns data in any numerical format as a float. Data in 353 * TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by 354 * dividing the numerator into the denominator using 355 * double-precision arithmetic and then truncating to single 356 * precision. Data in TIFF_SLONG, TIFF_LONG, or TIFF_DOUBLE 357 * format may suffer from truncation. 358 * 359 * <p> A ClassCastException will be thrown if the field is 360 * of type TIFF_UNDEFINED or TIFF_ASCII. 361 */ 362 public float getAsFloat(int index) { 363 switch (type) { 364 case TIFF_BYTE: 365 return ((byte[])data)[index] & 0xff; 366 case TIFF_SBYTE: 367 return ((byte[])data)[index]; 368 case TIFF_SHORT: 369 return ((char[])data)[index] & 0xffff; 370 case TIFF_SSHORT: 371 return ((short[])data)[index]; 372 case TIFF_SLONG: 373 return ((int[])data)[index]; 374 case TIFF_LONG: 375 return ((long[])data)[index]; 376 case TIFF_FLOAT: 377 return ((float[])data)[index]; 378 case TIFF_DOUBLE: 379 return (float)((double[])data)[index]; 380 case TIFF_SRATIONAL: 381 int[] ivalue = getAsSRational(index); 382 return (float)((double)ivalue[0]/ivalue[1]); 383 case TIFF_RATIONAL: 384 long[] lvalue = getAsRational(index); 385 return (float)((double)lvalue[0]/lvalue[1]); 386 default: 387 throw new ClassCastException(); 388 } 389 } 390 391 /** 392 * Returns data in any numerical format as a float. Data in 393 * TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by 394 * dividing the numerator into the denominator using 395 * double-precision arithmetic. 396 * 397 * <p> A ClassCastException will be thrown if the field is of 398 * type TIFF_UNDEFINED or TIFF_ASCII. 399 */ 400 public double getAsDouble(int index) { 401 switch (type) { 402 case TIFF_BYTE: 403 return ((byte[])data)[index] & 0xff; 404 case TIFF_SBYTE: 405 return ((byte[])data)[index]; 406 case TIFF_SHORT: 407 return ((char[])data)[index] & 0xffff; 408 case TIFF_SSHORT: 409 return ((short[])data)[index]; 410 case TIFF_SLONG: 411 return ((int[])data)[index]; 412 case TIFF_LONG: 413 return ((long[])data)[index]; 414 case TIFF_FLOAT: 415 return ((float[])data)[index]; 416 case TIFF_DOUBLE: 417 return ((double[])data)[index]; 418 case TIFF_SRATIONAL: 419 int[] ivalue = getAsSRational(index); 420 return (double)ivalue[0]/ivalue[1]; 421 case TIFF_RATIONAL: 422 long[] lvalue = getAsRational(index); 423 return (double)lvalue[0]/lvalue[1]; 424 default: 425 throw new ClassCastException(); 426 } 427 } 428 429 /** 430 * Returns a TIFF_ASCII data item as a String. 431 * 432 * <p> A ClassCastException will be thrown if the field is not 433 * of type TIFF_ASCII. 434 */ 435 public String getAsString(int index) { 436 return ((String[])data)[index]; 437 } 438 439 /** 440 * Returns a TIFF_SRATIONAL data item as a two-element array 441 * of ints. 442 * 443 * <p> A ClassCastException will be thrown if the field is not 444 * of type TIFF_SRATIONAL. 445 */ 446 public int[] getAsSRational(int index) { 447 return ((int[][])data)[index]; 448 } 449 450 /** 451 * Returns a TIFF_RATIONAL data item as a two-element array 452 * of ints. 453 * 454 * <p> A ClassCastException will be thrown if the field is not 455 * of type TIFF_RATIONAL. 456 */ 457 public long[] getAsRational(int index) { 458 if (type == TIFF_LONG) 459 return getAsLongs(); 460 return ((long[][])data)[index]; 461 } 462 463 /** 464 * Compares this <code>TIFFField</code> with another 465 * <code>TIFFField</code> by comparing the tags. 466 * 467 * <p><b>Note: this class has a natural ordering that is inconsistent 468 * with <code>equals()</code>.</b> 469 * 470 * @throws IllegalArgumentException if the parameter is <code>null</code>. 471 */ 472 public int compareTo(TIFFField o) { 473 if(o == null) { 474 throw new IllegalArgumentException(); 475 } 476 477 int oTag = o.getTag(); 478 479 if(tag < oTag) { 480 return -1; 481 } else if(tag > oTag) { 482 return 1; 483 } else { 484 return 0; 485 } 486 } 487}