KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > TIFFField


1 /*
2  * Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * -Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * -Redistribution in binary form must reproduct the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
15  * be used to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * This software is provided "AS IS," without a warranty of any kind. ALL
19  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
20  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
21  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
22  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
23  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
24  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
25  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
26  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
27  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * You acknowledge that Software is not designed,licensed or intended for use in
31  * the design, construction, operation or maintenance of any nuclear facility.
32  */

33 package com.lowagie.text.pdf.codec;
34
35 import java.io.Serializable JavaDoc;
36
37 /**
38  * A class representing a field in a TIFF 6.0 Image File Directory.
39  *
40  * <p> The TIFF file format is described in more detail in the
41  * comments for the TIFFDescriptor class.
42  *
43  * <p> A field in a TIFF Image File Directory (IFD). A field is defined
44  * as a sequence of values of identical data type. TIFF 6.0 defines
45  * 12 data types, which are mapped internally onto the Java datatypes
46  * byte, int, long, float, and double.
47  *
48  * <p><b> This class is not a committed part of the JAI API. It may
49  * be removed or changed in future releases of JAI.</b>
50  *
51  * @see TIFFDirectory
52  */

53 public class TIFFField extends Object JavaDoc implements Comparable JavaDoc, Serializable JavaDoc {
54
55     private static final long serialVersionUID = 9088332901412823834L;
56
57     /** Flag for 8 bit unsigned integers. */
58     public static final int TIFF_BYTE = 1;
59
60     /** Flag for null-terminated ASCII strings. */
61     public static final int TIFF_ASCII = 2;
62
63     /** Flag for 16 bit unsigned integers. */
64     public static final int TIFF_SHORT = 3;
65
66     /** Flag for 32 bit unsigned integers. */
67     public static final int TIFF_LONG = 4;
68
69     /** Flag for pairs of 32 bit unsigned integers. */
70     public static final int TIFF_RATIONAL = 5;
71
72     /** Flag for 8 bit signed integers. */
73     public static final int TIFF_SBYTE = 6;
74
75     /** Flag for 8 bit uninterpreted bytes. */
76     public static final int TIFF_UNDEFINED = 7;
77
78     /** Flag for 16 bit signed integers. */
79     public static final int TIFF_SSHORT = 8;
80
81     /** Flag for 32 bit signed integers. */
82     public static final int TIFF_SLONG = 9;
83
84     /** Flag for pairs of 32 bit signed integers. */
85     public static final int TIFF_SRATIONAL = 10;
86
87     /** Flag for 32 bit IEEE floats. */
88     public static final int TIFF_FLOAT = 11;
89
90     /** Flag for 64 bit IEEE doubles. */
91     public static final int TIFF_DOUBLE = 12;
92
93     /** The tag number. */
94     int tag;
95
96     /** The tag type. */
97     int type;
98
99     /** The number of data items present in the field. */
100     int count;
101
102     /** The field data. */
103     Object JavaDoc data;
104     
105     /** The default constructor. */
106     TIFFField() {}
107
108     /**
109      * Constructs a TIFFField with arbitrary data. The data
110      * parameter must be an array of a Java type appropriate for the
111      * type of the TIFF field. Since there is no available 32-bit
112      * unsigned datatype, long is used. The mapping between types is
113      * as follows:
114      *
115      * <table border=1>
116      * <tr>
117      * <th> TIFF type </th> <th> Java type </th>
118      * <tr>
119      * <td><tt>TIFF_BYTE</tt></td> <td><tt>byte</tt></td>
120      * <tr>
121      * <td><tt>TIFF_ASCII</tt></td> <td><tt>String</tt></td>
122      * <tr>
123      * <td><tt>TIFF_SHORT</tt></td> <td><tt>char</tt></td>
124      * <tr>
125      * <td><tt>TIFF_LONG</tt></td> <td><tt>long</tt></td>
126      * <tr>
127      * <td><tt>TIFF_RATIONAL</tt></td> <td><tt>long[2]</tt></td>
128      * <tr>
129      * <td><tt>TIFF_SBYTE</tt></td> <td><tt>byte</tt></td>
130      * <tr>
131      * <td><tt>TIFF_UNDEFINED</tt></td> <td><tt>byte</tt></td>
132      * <tr>
133      * <td><tt>TIFF_SSHORT</tt></td> <td><tt>short</tt></td>
134      * <tr>
135      * <td><tt>TIFF_SLONG</tt></td> <td><tt>int</tt></td>
136      * <tr>
137      * <td><tt>TIFF_SRATIONAL</tt></td> <td><tt>int[2]</tt></td>
138      * <tr>
139      * <td><tt>TIFF_FLOAT</tt></td> <td><tt>float</tt></td>
140      * <tr>
141      * <td><tt>TIFF_DOUBLE</tt></td> <td><tt>double</tt></td>
142      * </table>
143      */

144     public TIFFField(int tag, int type, int count, Object JavaDoc data) {
145         this.tag = tag;
146         this.type = type;
147         this.count = count;
148         this.data = data;
149     }
150
151     /**
152      * Returns the tag number, between 0 and 65535.
153      */

154     public int getTag() {
155         return tag;
156     }
157
158     /**
159      * Returns the type of the data stored in the IFD.
160      * For a TIFF6.0 file, the value will equal one of the
161      * TIFF_ constants defined in this class. For future
162      * revisions of TIFF, higher values are possible.
163      *
164      */

165     public int getType() {
166         return type;
167     }
168
169     /**
170      * Returns the number of elements in the IFD.
171      */

172     public int getCount() {
173         return count;
174     }
175
176     /**
177      * Returns the data as an uninterpreted array of bytes.
178      * The type of the field must be one of TIFF_BYTE, TIFF_SBYTE,
179      * or TIFF_UNDEFINED;
180      *
181      * <p> For data in TIFF_BYTE format, the application must take
182      * care when promoting the data to longer integral types
183      * to avoid sign extension.
184      *
185      * <p> A ClassCastException will be thrown if the field is not
186      * of type TIFF_BYTE, TIFF_SBYTE, or TIFF_UNDEFINED.
187      */

188     public byte[] getAsBytes() {
189         return (byte[])data;
190     }
191
192     /**
193      * Returns TIFF_SHORT data as an array of chars (unsigned 16-bit
194      * integers).
195      *
196      * <p> A ClassCastException will be thrown if the field is not
197      * of type TIFF_SHORT.
198      */

199     public char[] getAsChars() {
200         return (char[])data;
201     }
202
203     /**
204      * Returns TIFF_SSHORT data as an array of shorts (signed 16-bit
205      * integers).
206      *
207      * <p> A ClassCastException will be thrown if the field is not
208      * of type TIFF_SSHORT.
209      */

210     public short[] getAsShorts() {
211         return (short[])data;
212     }
213
214     /**
215      * Returns TIFF_SLONG data as an array of ints (signed 32-bit
216      * integers).
217      *
218      * <p> A ClassCastException will be thrown if the field is not
219      * of type TIFF_SLONG.
220      */

221     public int[] getAsInts() {
222         return (int[])data;
223     }
224
225     /**
226      * Returns TIFF_LONG data as an array of longs (signed 64-bit
227      * integers).
228      *
229      * <p> A ClassCastException will be thrown if the field is not
230      * of type TIFF_LONG.
231      */

232     public long[] getAsLongs() {
233         return (long[])data;
234     }
235
236     /**
237      * Returns TIFF_FLOAT data as an array of floats.
238      *
239      * <p> A ClassCastException will be thrown if the field is not
240      * of type TIFF_FLOAT.
241      */

242     public float[] getAsFloats() {
243         return (float[])data;
244     }
245
246     /**
247      * Returns TIFF_DOUBLE data as an array of doubles.
248      *
249      * <p> A ClassCastException will be thrown if the field is not
250      * of type TIFF_DOUBLE.
251      */

252     public double[] getAsDoubles() {
253         return (double[])data;
254     }
255
256     /**
257      * Returns TIFF_SRATIONAL data as an array of 2-element arrays of ints.
258      *
259      * <p> A ClassCastException will be thrown if the field is not
260      * of type TIFF_SRATIONAL.
261      */

262     public int[][] getAsSRationals() {
263         return (int[][])data;
264     }
265
266     /**
267      * Returns TIFF_RATIONAL data as an array of 2-element arrays of longs.
268      *
269      * <p> A ClassCastException will be thrown if the field is not
270      * of type TIFF_RATTIONAL.
271      */

272     public long[][] getAsRationals() {
273         return (long[][])data;
274     }
275
276     /**
277      * Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
278      * TIFF_SSHORT, or TIFF_SLONG format as an int.
279      *
280      * <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned;
281      * that is, no sign extension will take place and the returned
282      * value will be in the range [0, 255]. TIFF_SBYTE data will
283      * be returned in the range [-128, 127].
284      *
285      * <p> A ClassCastException will be thrown if the field is not of
286      * type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
287      * TIFF_SSHORT, or TIFF_SLONG.
288      */

289     public int getAsInt(int index) {
290         switch (type) {
291         case TIFF_BYTE: case TIFF_UNDEFINED:
292             return ((byte[])data)[index] & 0xff;
293         case TIFF_SBYTE:
294             return ((byte[])data)[index];
295         case TIFF_SHORT:
296             return ((char[])data)[index] & 0xffff;
297         case TIFF_SSHORT:
298             return ((short[])data)[index];
299         case TIFF_SLONG:
300             return ((int[])data)[index];
301         default:
302             throw new ClassCastException JavaDoc();
303         }
304     }
305
306     /**
307      * Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
308      * TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG format as a long.
309      *
310      * <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned;
311      * that is, no sign extension will take place and the returned
312      * value will be in the range [0, 255]. TIFF_SBYTE data will
313      * be returned in the range [-128, 127].
314      *
315      * <p> A ClassCastException will be thrown if the field is not of
316      * type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
317      * TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG.
318      */

319     public long getAsLong(int index) {
320         switch (type) {
321         case TIFF_BYTE: case TIFF_UNDEFINED:
322             return ((byte[])data)[index] & 0xff;
323         case TIFF_SBYTE:
324             return ((byte[])data)[index];
325         case TIFF_SHORT:
326             return ((char[])data)[index] & 0xffff;
327         case TIFF_SSHORT:
328             return ((short[])data)[index];
329         case TIFF_SLONG:
330             return ((int[])data)[index];
331         case TIFF_LONG:
332             return ((long[])data)[index];
333         default:
334             throw new ClassCastException JavaDoc();
335         }
336     }
337     
338     /**
339      * Returns data in any numerical format as a float. Data in
340      * TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by
341      * dividing the numerator into the denominator using
342      * double-precision arithmetic and then truncating to single
343      * precision. Data in TIFF_SLONG, TIFF_LONG, or TIFF_DOUBLE
344      * format may suffer from truncation.
345      *
346      * <p> A ClassCastException will be thrown if the field is
347      * of type TIFF_UNDEFINED or TIFF_ASCII.
348      */

349     public float getAsFloat(int index) {
350         switch (type) {
351         case TIFF_BYTE:
352             return ((byte[])data)[index] & 0xff;
353         case TIFF_SBYTE:
354             return ((byte[])data)[index];
355         case TIFF_SHORT:
356             return ((char[])data)[index] & 0xffff;
357         case TIFF_SSHORT:
358             return ((short[])data)[index];
359         case TIFF_SLONG:
360             return ((int[])data)[index];
361         case TIFF_LONG:
362             return ((long[])data)[index];
363         case TIFF_FLOAT:
364             return ((float[])data)[index];
365         case TIFF_DOUBLE:
366             return (float)((double[])data)[index];
367         case TIFF_SRATIONAL:
368             int[] ivalue = getAsSRational(index);
369             return (float)((double)ivalue[0]/ivalue[1]);
370         case TIFF_RATIONAL:
371             long[] lvalue = getAsRational(index);
372             return (float)((double)lvalue[0]/lvalue[1]);
373         default:
374             throw new ClassCastException JavaDoc();
375         }
376     }
377
378     /**
379      * Returns data in any numerical format as a float. Data in
380      * TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by
381      * dividing the numerator into the denominator using
382      * double-precision arithmetic.
383      *
384      * <p> A ClassCastException will be thrown if the field is of
385      * type TIFF_UNDEFINED or TIFF_ASCII.
386      */

387     public double getAsDouble(int index) {
388         switch (type) {
389         case TIFF_BYTE:
390             return ((byte[])data)[index] & 0xff;
391         case TIFF_SBYTE:
392             return ((byte[])data)[index];
393         case TIFF_SHORT:
394             return ((char[])data)[index] & 0xffff;
395         case TIFF_SSHORT:
396             return ((short[])data)[index];
397         case TIFF_SLONG:
398             return ((int[])data)[index];
399         case TIFF_LONG:
400             return ((long[])data)[index];
401         case TIFF_FLOAT:
402             return ((float[])data)[index];
403         case TIFF_DOUBLE:
404             return ((double[])data)[index];
405         case TIFF_SRATIONAL:
406             int[] ivalue = getAsSRational(index);
407             return (double)ivalue[0]/ivalue[1];
408         case TIFF_RATIONAL:
409             long[] lvalue = getAsRational(index);
410             return (double)lvalue[0]/lvalue[1];
411         default:
412             throw new ClassCastException JavaDoc();
413         }
414     }
415
416     /**
417      * Returns a TIFF_ASCII data item as a String.
418      *
419      * <p> A ClassCastException will be thrown if the field is not
420      * of type TIFF_ASCII.
421      */

422     public String JavaDoc getAsString(int index) {
423         return ((String JavaDoc[])data)[index];
424     }
425
426     /**
427      * Returns a TIFF_SRATIONAL data item as a two-element array
428      * of ints.
429      *
430      * <p> A ClassCastException will be thrown if the field is not
431      * of type TIFF_SRATIONAL.
432      */

433     public int[] getAsSRational(int index) {
434         return ((int[][])data)[index];
435     }
436
437     /**
438      * Returns a TIFF_RATIONAL data item as a two-element array
439      * of ints.
440      *
441      * <p> A ClassCastException will be thrown if the field is not
442      * of type TIFF_RATIONAL.
443      */

444     public long[] getAsRational(int index) {
445         if (type == TIFF_LONG)
446             return getAsLongs();
447         return ((long[][])data)[index];
448     }
449
450     /**
451      * Compares this <code>TIFFField</code> with another
452      * <code>TIFFField</code> by comparing the tags.
453      *
454      * <p><b>Note: this class has a natural ordering that is inconsistent
455      * with <code>equals()</code>.</b>
456      *
457      * @throws IllegalArgumentException if the parameter is <code>null</code>.
458      * @throws ClassCastException if the parameter is not a
459      * <code>TIFFField</code>.
460      */

461     public int compareTo(Object JavaDoc o) {
462         if(o == null) {
463             throw new IllegalArgumentException JavaDoc();
464         }
465
466         int oTag = ((TIFFField)o).getTag();
467
468         if(tag < oTag) {
469             return -1;
470         } else if(tag > oTag) {
471             return 1;
472         } else {
473             return 0;
474         }
475     }
476 }
477
Popular Tags