Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: non_com/media/jai/codec/TIFFField.java


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 non_com.media.jai.codec;
34  import java.io.Serializable;
35  
36  /**
37   *  A class representing a field in a TIFF 6.0 Image File Directory. <p>
38   *
39   *  The TIFF file format is described in more detail in the comments for the
40   *  TIFFDescriptor class. <p>
41   *
42   *  A field in a TIFF Image File Directory (IFD). A field is defined as a
43   *  sequence of values of identical data type. TIFF 6.0 defines 12 data types,
44   *  which are mapped internally onto the Java datatypes byte, int, long, float,
45   *  and double. <p>
46   *
47   *  <b> This class is not a committed part of the JAI API. It may be removed or
48   *  changed in future releases of JAI.</b>
49   *
50   * @see        javax.media.jai.operator.TIFFDescriptor
51   * @see        TIFFDirectory
52   */
53  public class TIFFField extends Object implements Comparable, Serializable {
54  
55    /**
56     *  Flag for 8 bit unsigned integers.
57     */
58    public final static int TIFF_BYTE = 1;
59  
60    /**
61     *  Flag for null-terminated ASCII strings.
62     */
63    public final static int TIFF_ASCII = 2;
64  
65    /**
66     *  Flag for 16 bit unsigned integers.
67     */
68    public final static int TIFF_SHORT = 3;
69  
70    /**
71     *  Flag for 32 bit unsigned integers.
72     */
73    public final static int TIFF_LONG = 4;
74  
75    /**
76     *  Flag for pairs of 32 bit unsigned integers.
77     */
78    public final static int TIFF_RATIONAL = 5;
79  
80    /**
81     *  Flag for 8 bit signed integers.
82     */
83    public final static int TIFF_SBYTE = 6;
84  
85    /**
86     *  Flag for 8 bit uninterpreted bytes.
87     */
88    public final static int TIFF_UNDEFINED = 7;
89  
90    /**
91     *  Flag for 16 bit signed integers.
92     */
93    public final static int TIFF_SSHORT = 8;
94  
95    /**
96     *  Flag for 32 bit signed integers.
97     */
98    public final static int TIFF_SLONG = 9;
99  
100   /**
101    *  Flag for pairs of 32 bit signed integers.
102    */
103   public final static int TIFF_SRATIONAL = 10;
104 
105   /**
106    *  Flag for 32 bit IEEE floats.
107    */
108   public final static int TIFF_FLOAT = 11;
109 
110   /**
111    *  Flag for 64 bit IEEE doubles.
112    */
113   public final static int TIFF_DOUBLE = 12;
114 
115   /**
116    *  The tag number.
117    */
118   int tag;
119 
120   /**
121    *  The tag type.
122    */
123   int type;
124 
125   /**
126    *  The number of data items present in the field.
127    */
128   int count;
129 
130   /**
131    *  The field data.
132    */
133   Object data;
134 
135 
136   /**
137    *  Constructs a TIFFField with arbitrary data. The data parameter must be an
138    *  array of a Java type appropriate for the type of the TIFF field. Since
139    *  there is no available 32-bit unsigned datatype, long is used. The mapping
140    *  between types is as follows:
141    *  <tableborder=1>
142    *
143    *    <tr>
144    *
145    *      <th>
146    *        TIFF type
147    *      </th>
148    *
149    *      <th>
150    *        Java type
151    *      </th>
152    *
153    *      <tr>
154    *
155    *        <td>
156    *          <tt>TIFF_BYTE</tt>
157    *        </td>
158    *
159    *        <td>
160    *          <tt>byte</tt>
161    *        </td>
162    *
163    *        <tr>
164    *
165    *          <td>
166    *            <tt>TIFF_ASCII</tt>
167    *          </td>
168    *
169    *          <td>
170    *            <tt>String</tt>
171    *          </td>
172    *
173    *          <tr>
174    *
175    *            <td>
176    *              <tt>TIFF_SHORT</tt>
177    *            </td>
178    *
179    *            <td>
180    *              <tt>char</tt>
181    *            </td>
182    *
183    *            <tr>
184    *
185    *              <td>
186    *                <tt>TIFF_LONG</tt>
187    *              </td>
188    *
189    *              <td>
190    *                <tt>long</tt>
191    *              </td>
192    *
193    *              <tr>
194    *
195    *                <td>
196    *                  <tt>TIFF_RATIONAL</tt>
197    *                </td>
198    *
199    *                <td>
200    *                  <tt>long[2]</tt>
201    *                </td>
202    *
203    *                <tr>
204    *
205    *                  <td>
206    *                    <tt>TIFF_SBYTE</tt>
207    *                  </td>
208    *
209    *                  <td>
210    *                    <tt>byte</tt>
211    *                  </td>
212    *
213    *                  <tr>
214    *
215    *                    <td>
216    *                      <tt>TIFF_UNDEFINED</tt>
217    *                    </td>
218    *
219    *                    <td>
220    *                      <tt>byte</tt>
221    *                    </td>
222    *
223    *                    <tr>
224    *
225    *                      <td>
226    *                        <tt>TIFF_SSHORT</tt>
227    *                      </td>
228    *
229    *                      <td>
230    *                        <tt>short</tt>
231    *                      </td>
232    *
233    *                      <tr>
234    *
235    *                        <td>
236    *                          <tt>TIFF_SLONG</tt>
237    *                        </td>
238    *
239    *                        <td>
240    *                          <tt>int</tt>
241    *                        </td>
242    *
243    *                        <tr>
244    *
245    *                          <td>
246    *                            <tt>TIFF_SRATIONAL</tt>
247    *                          </td>
248    *
249    *                          <td>
250    *                            <tt>int[2]</tt>
251    *                          </td>
252    *
253    *                          <tr>
254    *
255    *                            <td>
256    *                              <tt>TIFF_FLOAT</tt>
257    *                            </td>
258    *
259    *                            <td>
260    *                              <tt>float</tt>
261    *                            </td>
262    *
263    *                            <tr>
264    *
265    *                              <td>
266    *                                <tt>TIFF_DOUBLE</tt>
267    *                              </td>
268    *
269    *                              <td>
270    *                                <tt>double</tt>
271    *                              </td>
272    *
273    *                            </table>
274    *
275    *
276    * @param  tag    Description of Parameter
277    * @param  type   Description of Parameter
278    * @param  count  Description of Parameter
279    * @param  data   Description of Parameter
280    */
281   public TIFFField(int tag, int type, int count, Object data) {
282     this.tag = tag;
283     this.type = type;
284     this.count = count;
285     this.data = data;
286   }
287 
288 
289   /**
290    *  The default constructor.
291    */
292   TIFFField() {
293   }
294 
295 
296   /**
297    *  Returns the tag number, between 0 and 65535.
298    *
299    * @return    The Tag value
300    */
301   public int getTag() {
302     return tag;
303   }
304 
305 
306   /**
307    *  Returns the type of the data stored in the IFD. For a TIFF6.0 file, the
308    *  value will equal one of the TIFF_ constants defined in this class. For
309    *  future revisions of TIFF, higher values are possible.
310    *
311    * @return    The Type value
312    */
313   public int getType() {
314     return type;
315   }
316 
317 
318   /**
319    *  Returns the number of elements in the IFD.
320    *
321    * @return    The Count value
322    */
323   public int getCount() {
324     return count;
325   }
326 
327 
328   /**
329    *  Returns the data as an uninterpreted array of bytes. The type of the field
330    *  must be one of TIFF_BYTE, TIFF_SBYTE, or TIFF_UNDEFINED; <p>
331    *
332    *  For data in TIFF_BYTE format, the application must take care when
333    *  promoting the data to longer integral types to avoid sign extension. <p>
334    *
335    *  A ClassCastException will be thrown if the field is not of type TIFF_BYTE,
336    *  TIFF_SBYTE, or TIFF_UNDEFINED.
337    *
338    * @return    The AsBytes value
339    */
340   public byte[] getAsBytes() {
341     return (byte[]) data;
342   }
343 
344 
345   /**
346    *  Returns TIFF_SHORT data as an array of chars (unsigned 16-bit integers).
347    *  <p>
348    *
349    *  A ClassCastException will be thrown if the field is not of type
350    *  TIFF_SHORT.
351    *
352    * @return    The AsChars value
353    */
354   public char[] getAsChars() {
355     return (char[]) data;
356   }
357 
358 
359   /**
360    *  Returns TIFF_SSHORT data as an array of shorts (signed 16-bit integers).
361    *  <p>
362    *
363    *  A ClassCastException will be thrown if the field is not of type
364    *  TIFF_SSHORT.
365    *
366    * @return    The AsShorts value
367    */
368   public short[] getAsShorts() {
369     return (short[]) data;
370   }
371 
372 
373   /**
374    *  Returns TIFF_SLONG data as an array of ints (signed 32-bit integers). <p>
375    *
376    *  A ClassCastException will be thrown if the field is not of type
377    *  TIFF_SLONG.
378    *
379    * @return    The AsInts value
380    */
381   public int[] getAsInts() {
382     return (int[]) data;
383   }
384 
385 
386   /**
387    *  Returns TIFF_LONG data as an array of longs (signed 64-bit integers). <p>
388    *
389    *  A ClassCastException will be thrown if the field is not of type TIFF_LONG.
390    *
391    * @return    The AsLongs value
392    */
393   public long[] getAsLongs() {
394     return (long[]) data;
395   }
396 
397 
398   /**
399    *  Returns TIFF_FLOAT data as an array of floats. <p>
400    *
401    *  A ClassCastException will be thrown if the field is not of type
402    *  TIFF_FLOAT.
403    *
404    * @return    The AsFloats value
405    */
406   public float[] getAsFloats() {
407     return (float[]) data;
408   }
409 
410 
411   /**
412    *  Returns TIFF_DOUBLE data as an array of doubles. <p>
413    *
414    *  A ClassCastException will be thrown if the field is not of type
415    *  TIFF_DOUBLE.
416    *
417    * @return    The AsDoubles value
418    */
419   public double[] getAsDoubles() {
420     return (double[]) data;
421   }
422 
423 
424   /**
425    *  Returns TIFF_SRATIONAL data as an array of 2-element arrays of ints. <p>
426    *
427    *  A ClassCastException will be thrown if the field is not of type
428    *  TIFF_SRATIONAL.
429    *
430    * @return    The AsSRationals value
431    */
432   public int[][] getAsSRationals() {
433     return (int[][]) data;
434   }
435 
436 
437   /**
438    *  Returns TIFF_RATIONAL data as an array of 2-element arrays of longs. <p>
439    *
440    *  A ClassCastException will be thrown if the field is not of type
441    *  TIFF_RATTIONAL.
442    *
443    * @return    The AsRationals value
444    */
445   public long[][] getAsRationals() {
446     return (long[][]) data;
447   }
448 
449 
450   /**
451    *  Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
452    *  TIFF_SSHORT, or TIFF_SLONG format as an int. <p>
453    *
454    *  TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned; that is, no
455    *  sign extension will take place and the returned value will be in the range
456    *  [0, 255]. TIFF_SBYTE data will be returned in the range [-128, 127]. <p>
457    *
458    *  A ClassCastException will be thrown if the field is not of type TIFF_BYTE,
459    *  TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT, TIFF_SSHORT, or TIFF_SLONG.
460    *
461    * @param  index  Description of Parameter
462    * @return        The AsInt value
463    */
464   public int getAsInt(int index) {
465     switch (type) {
466       case TIFF_BYTE:
467       case TIFF_UNDEFINED:
468         return ((byte[]) data)[index] & 0xff;
469       case TIFF_SBYTE:
470         return ((byte[]) data)[index];
471       case TIFF_SHORT:
472         return ((char[]) data)[index] & 0xffff;
473       case TIFF_SSHORT:
474         return ((short[]) data)[index];
475       case TIFF_SLONG:
476         return ((int[]) data)[index];
477       default:
478         throw new ClassCastException();
479     }
480   }
481 
482 
483   /**
484    *  Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
485    *  TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG format as a long. <p>
486    *
487    *  TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned; that is, no
488    *  sign extension will take place and the returned value will be in the range
489    *  [0, 255]. TIFF_SBYTE data will be returned in the range [-128, 127]. <p>
490    *
491    *  A ClassCastException will be thrown if the field is not of type TIFF_BYTE,
492    *  TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT, TIFF_SSHORT, TIFF_SLONG, or
493    *  TIFF_LONG.
494    *
495    * @param  index  Description of Parameter
496    * @return        The AsLong value
497    */
498   public long getAsLong(int index) {
499     switch (type) {
500       case TIFF_BYTE:
501       case TIFF_UNDEFINED:
502         return ((byte[]) data)[index] & 0xff;
503       case TIFF_SBYTE:
504         return ((byte[]) data)[index];
505       case TIFF_SHORT:
506         return ((char[]) data)[index] & 0xffff;
507       case TIFF_SSHORT:
508         return ((short[]) data)[index];
509       case TIFF_SLONG:
510         return ((int[]) data)[index];
511       case TIFF_LONG:
512         return ((long[]) data)[index];
513       default:
514         throw new ClassCastException();
515     }
516   }
517 
518 
519   /**
520    *  Returns data in any numerical format as a float. Data in TIFF_SRATIONAL or
521    *  TIFF_RATIONAL format are evaluated by dividing the numerator into the
522    *  denominator using double-precision arithmetic and then truncating to
523    *  single precision. Data in TIFF_SLONG, TIFF_LONG, or TIFF_DOUBLE format may
524    *  suffer from truncation. <p>
525    *
526    *  A ClassCastException will be thrown if the field is of type TIFF_UNDEFINED
527    *  or TIFF_ASCII.
528    *
529    * @param  index  Description of Parameter
530    * @return        The AsFloat value
531    */
532   public float getAsFloat(int index) {
533     switch (type) {
534       case TIFF_BYTE:
535         return ((byte[]) data)[index] & 0xff;
536       case TIFF_SBYTE:
537         return ((byte[]) data)[index];
538       case TIFF_SHORT:
539         return ((char[]) data)[index] & 0xffff;
540       case TIFF_SSHORT:
541         return ((short[]) data)[index];
542       case TIFF_SLONG:
543         return ((int[]) data)[index];
544       case TIFF_LONG:
545         return ((long[]) data)[index];
546       case TIFF_FLOAT:
547         return ((float[]) data)[index];
548       case TIFF_DOUBLE:
549         return (float) ((double[]) data)[index];
550       case TIFF_SRATIONAL:
551         int[] ivalue = getAsSRational(index);
552         return (float) ((double) ivalue[0] / ivalue[1]);
553       case TIFF_RATIONAL:
554         long[] lvalue = getAsRational(index);
555         return (float) ((double) lvalue[0] / lvalue[1]);
556       default:
557         throw new ClassCastException();
558     }
559   }
560 
561 
562   /**
563    *  Returns data in any numerical format as a float. Data in TIFF_SRATIONAL or
564    *  TIFF_RATIONAL format are evaluated by dividing the numerator into the
565    *  denominator using double-precision arithmetic. <p>
566    *
567    *  A ClassCastException will be thrown if the field is of type TIFF_UNDEFINED
568    *  or TIFF_ASCII.
569    *
570    * @param  index  Description of Parameter
571    * @return        The AsDouble value
572    */
573   public double getAsDouble(int index) {
574     switch (type) {
575       case TIFF_BYTE:
576         return ((byte[]) data)[index] & 0xff;
577       case TIFF_SBYTE:
578         return ((byte[]) data)[index];
579       case TIFF_SHORT:
580         return ((char[]) data)[index] & 0xffff;
581       case TIFF_SSHORT:
582         return ((short[]) data)[index];
583       case TIFF_SLONG:
584         return ((int[]) data)[index];
585       case TIFF_LONG:
586         return ((long[]) data)[index];
587       case TIFF_FLOAT:
588         return ((float[]) data)[index];
589       case TIFF_DOUBLE:
590         return ((double[]) data)[index];
591       case TIFF_SRATIONAL:
592         int[] ivalue = getAsSRational(index);
593         return (double) ivalue[0] / ivalue[1];
594       case TIFF_RATIONAL:
595         long[] lvalue = getAsRational(index);
596         return (double) lvalue[0] / lvalue[1];
597       default:
598         throw new ClassCastException();
599     }
600   }
601 
602 
603   /**
604    *  Returns a TIFF_ASCII data item as a String. <p>
605    *
606    *  A ClassCastException will be thrown if the field is not of type
607    *  TIFF_ASCII.
608    *
609    * @param  index  Description of Parameter
610    * @return        The AsString value
611    */
612   public String getAsString(int index) {
613     return ((String[]) data)[index];
614   }
615 
616 
617   /**
618    *  Returns a TIFF_SRATIONAL data item as a two-element array of ints. <p>
619    *
620    *  A ClassCastException will be thrown if the field is not of type
621    *  TIFF_SRATIONAL.
622    *
623    * @param  index  Description of Parameter
624    * @return        The AsSRational value
625    */
626   public int[] getAsSRational(int index) {
627     return ((int[][]) data)[index];
628   }
629 
630 
631   /**
632    *  Returns a TIFF_RATIONAL data item as a two-element array of ints. <p>
633    *
634    *  A ClassCastException will be thrown if the field is not of type
635    *  TIFF_RATIONAL.
636    *
637    * @param  index  Description of Parameter
638    * @return        The AsRational value
639    */
640   public long[] getAsRational(int index) {
641     return ((long[][]) data)[index];
642   }
643 
644 
645   /**
646    *  Compares this <code>TIFFField</code> with another <code>TIFFField</code>
647    *  by comparing the tags. <p>
648    *
649    *  <b>Note: this class has a natural ordering that is inconsistent with
650    *  <code>equals()</code>.</b>
651    *
652    * @param  o                          Description of Parameter
653    * @return                            Description of the Returned Value
654    * @throws  IllegalArgumentException  if the parameter is <code>null</code>.
655    * @throws  ClassCastException        if the parameter is not a <code>TIFFField</code>
656    *      .
657    */
658   public int compareTo(Object o) {
659     if (o == null) {
660       throw new IllegalArgumentException();
661     }
662 
663     int oTag = ((TIFFField) o).getTag();
664 
665     if (tag < oTag) {
666       return -1;
667     }
668     else if (tag > oTag) {
669       return 1;
670     }
671     else {
672       return 0;
673     }
674   }
675 }