KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > codec > tiff > TIFFField


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.codec.tiff;
19
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * A class representing a field in a TIFF 6.0 Image File Directory.
24  *
25  * <p> The TIFF file format is described in more detail in the
26  * comments for the TIFFDescriptor class.
27  *
28  * <p> A field in a TIFF Image File Directory (IFD). A field is defined
29  * as a sequence of values of identical data type. TIFF 6.0 defines
30  * 12 data types, which are mapped internally onto the Java datatypes
31  * byte, int, long, float, and double.
32  *
33  * <p><b> This class is not a committed part of the JAI API. It may
34  * be removed or changed in future releases of JAI.</b>
35  *
36  * @see TIFFDirectory
37  */

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

127     public TIFFField(int tag, int type, int count, Object JavaDoc data) {
128         this.tag = tag;
129         this.type = type;
130         this.count = count;
131         this.data = data;
132     }
133
134     /**
135      * Returns the tag number, between 0 and 65535.
136      */

137     public int getTag() {
138         return tag;
139     }
140
141     /**
142      * Returns the type of the data stored in the IFD.
143      * For a TIFF6.0 file, the value will equal one of the
144      * TIFF_ constants defined in this class. For future
145      * revisions of TIFF, higher values are possible.
146      *
147      */

148     public int getType() {
149         return type;
150     }
151
152     /**
153      * Returns the number of elements in the IFD.
154      */

155     public int getCount() {
156         return count;
157     }
158
159     /**
160      * Returns the data as an uninterpreted array of bytes.
161      * The type of the field must be one of TIFF_BYTE, TIFF_SBYTE,
162      * or TIFF_UNDEFINED;
163      *
164      * <p> For data in TIFF_BYTE format, the application must take
165      * care when promoting the data to longer integral types
166      * to avoid sign extension.
167      *
168      * <p> A ClassCastException will be thrown if the field is not
169      * of type TIFF_BYTE, TIFF_SBYTE, or TIFF_UNDEFINED.
170      */

171     public byte[] getAsBytes() {
172         return (byte[])data;
173     }
174
175     /**
176      * Returns TIFF_SHORT data as an array of chars (unsigned 16-bit
177      * integers).
178      *
179      * <p> A ClassCastException will be thrown if the field is not
180      * of type TIFF_SHORT.
181      */

182     public char[] getAsChars() {
183         return (char[])data;
184     }
185
186     /**
187      * Returns TIFF_SSHORT data as an array of shorts (signed 16-bit
188      * integers).
189      *
190      * <p> A ClassCastException will be thrown if the field is not
191      * of type TIFF_SSHORT.
192      */

193     public short[] getAsShorts() {
194         return (short[])data;
195     }
196
197     /**
198      * Returns TIFF_SLONG data as an array of ints (signed 32-bit
199      * integers).
200      *
201      * <p> A ClassCastException will be thrown if the field is not
202      * of type TIFF_SLONG.
203      */

204     public int[] getAsInts() {
205         return (int[])data;
206     }
207
208     /**
209      * Returns TIFF_LONG data as an array of longs (signed 64-bit
210      * integers).
211      *
212      * <p> A ClassCastException will be thrown if the field is not
213      * of type TIFF_LONG.
214      */

215     public long[] getAsLongs() {
216         return (long[])data;
217     }
218
219     /**
220      * Returns TIFF_FLOAT data as an array of floats.
221      *
222      * <p> A ClassCastException will be thrown if the field is not
223      * of type TIFF_FLOAT.
224      */

225     public float[] getAsFloats() {
226         return (float[])data;
227     }
228
229     /**
230      * Returns TIFF_DOUBLE data as an array of doubles.
231      *
232      * <p> A ClassCastException will be thrown if the field is not
233      * of type TIFF_DOUBLE.
234      */

235     public double[] getAsDoubles() {
236         return (double[])data;
237     }
238
239     /**
240      * Returns TIFF_SRATIONAL data as an array of 2-element arrays of ints.
241      *
242      * <p> A ClassCastException will be thrown if the field is not
243      * of type TIFF_SRATIONAL.
244      */

245     public int[][] getAsSRationals() {
246         return (int[][])data;
247     }
248
249     /**
250      * Returns TIFF_RATIONAL data as an array of 2-element arrays of longs.
251      *
252      * <p> A ClassCastException will be thrown if the field is not
253      * of type TIFF_RATTIONAL.
254      */

255     public long[][] getAsRationals() {
256         return (long[][])data;
257     }
258
259     /**
260      * Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
261      * TIFF_SSHORT, or TIFF_SLONG format as an int.
262      *
263      * <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned;
264      * that is, no sign extension will take place and the returned
265      * value will be in the range [0, 255]. TIFF_SBYTE data will
266      * be returned in the range [-128, 127].
267      *
268      * <p> A ClassCastException will be thrown if the field is not of
269      * type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
270      * TIFF_SSHORT, or TIFF_SLONG.
271      */

272     public int getAsInt(int index) {
273         switch (type) {
274         case TIFF_BYTE: case TIFF_UNDEFINED:
275             return ((byte[])data)[index] & 0xff;
276         case TIFF_SBYTE:
277             return ((byte[])data)[index];
278         case TIFF_SHORT:
279             return ((char[])data)[index] & 0xffff;
280         case TIFF_SSHORT:
281             return ((short[])data)[index];
282         case TIFF_SLONG:
283             return ((int[])data)[index];
284         default:
285             throw new ClassCastException JavaDoc();
286         }
287     }
288
289     /**
290      * Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
291      * TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG format as a long.
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, TIFF_SLONG, or TIFF_LONG.
301      */

302     public long getAsLong(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         case TIFF_LONG:
315             return ((long[])data)[index];
316         default:
317             throw new ClassCastException JavaDoc();
318         }
319     }
320     
321     /**
322      * Returns data in any numerical format as a float. Data in
323      * TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by
324      * dividing the numerator into the denominator using
325      * double-precision arithmetic and then truncating to single
326      * precision. Data in TIFF_SLONG, TIFF_LONG, or TIFF_DOUBLE
327      * format may suffer from truncation.
328      *
329      * <p> A ClassCastException will be thrown if the field is
330      * of type TIFF_UNDEFINED or TIFF_ASCII.
331      */

332     public float getAsFloat(int index) {
333         switch (type) {
334         case TIFF_BYTE:
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         case TIFF_FLOAT:
347             return ((float[])data)[index];
348         case TIFF_DOUBLE:
349             return (float)((double[])data)[index];
350         case TIFF_SRATIONAL:
351             int[] ivalue = getAsSRational(index);
352             return (float)((double)ivalue[0]/ivalue[1]);
353         case TIFF_RATIONAL:
354             long[] lvalue = getAsRational(index);
355             return (float)((double)lvalue[0]/lvalue[1]);
356         default:
357             throw new ClassCastException JavaDoc();
358         }
359     }
360
361     /**
362      * Returns data in any numerical format as a float. Data in
363      * TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by
364      * dividing the numerator into the denominator using
365      * double-precision arithmetic.
366      *
367      * <p> A ClassCastException will be thrown if the field is of
368      * type TIFF_UNDEFINED or TIFF_ASCII.
369      */

370     public double getAsDouble(int index) {
371         switch (type) {
372         case TIFF_BYTE:
373             return ((byte[])data)[index] & 0xff;
374         case TIFF_SBYTE:
375             return ((byte[])data)[index];
376         case TIFF_SHORT:
377             return ((char[])data)[index] & 0xffff;
378         case TIFF_SSHORT:
379             return ((short[])data)[index];
380         case TIFF_SLONG:
381             return ((int[])data)[index];
382         case TIFF_LONG:
383             return ((long[])data)[index];
384         case TIFF_FLOAT:
385             return ((float[])data)[index];
386         case TIFF_DOUBLE:
387             return ((double[])data)[index];
388         case TIFF_SRATIONAL:
389             int[] ivalue = getAsSRational(index);
390             return (double)ivalue[0]/ivalue[1];
391         case TIFF_RATIONAL:
392             long[] lvalue = getAsRational(index);
393             return (double)lvalue[0]/lvalue[1];
394         default:
395             throw new ClassCastException JavaDoc();
396         }
397     }
398
399     /**
400      * Returns a TIFF_ASCII data item as a String.
401      *
402      * <p> A ClassCastException will be thrown if the field is not
403      * of type TIFF_ASCII.
404      */

405     public String JavaDoc getAsString(int index) {
406         return ((String JavaDoc[])data)[index];
407     }
408
409     /**
410      * Returns a TIFF_SRATIONAL data item as a two-element array
411      * of ints.
412      *
413      * <p> A ClassCastException will be thrown if the field is not
414      * of type TIFF_SRATIONAL.
415      */

416     public int[] getAsSRational(int index) {
417         return ((int[][])data)[index];
418     }
419
420     /**
421      * Returns a TIFF_RATIONAL data item as a two-element array
422      * of ints.
423      *
424      * <p> A ClassCastException will be thrown if the field is not
425      * of type TIFF_RATIONAL.
426      */

427     public long[] getAsRational(int index) {
428         return ((long[][])data)[index];
429     }
430
431     /**
432      * Compares this <code>TIFFField</code> with another
433      * <code>TIFFField</code> by comparing the tags.
434      *
435      * <p><b>Note: this class has a natural ordering that is inconsistent
436      * with <code>equals()</code>.</b>
437      *
438      * @throws IllegalArgumentException if the parameter is <code>null</code>.
439      * @throws ClassCastException if the parameter is not a
440      * <code>TIFFField</code>.
441      */

442     public int compareTo(Object JavaDoc o) {
443         if(o == null) {
444             throw new IllegalArgumentException JavaDoc();
445         }
446
447         int oTag = ((TIFFField)o).getTag();
448
449         if(tag < oTag) {
450             return -1;
451         } else if(tag > oTag) {
452             return 1;
453         } else {
454             return 0;
455         }
456     }
457 }
458
Popular Tags