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