1 /*
   2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.imageio.plugins.tiff;
  26 
  27 import java.util.StringTokenizer;
  28 import org.w3c.dom.NamedNodeMap;
  29 import org.w3c.dom.Node;
  30 import com.sun.imageio.plugins.tiff.TIFFFieldNode;
  31 import com.sun.imageio.plugins.tiff.TIFFIFD;
  32 
  33 /**
  34  * A class representing a field in a TIFF 6.0 Image File Directory.
  35  *
  36  * <p> A field in a TIFF Image File Directory (IFD) is defined as a
  37  * tag number accompanied by a sequence of values of identical data type.
  38  * TIFF 6.0 defines 12 data types; a 13th type {@code IFD} is
  39  * defined in TIFF Tech Note 1 of TIFF Specification Supplement 1. These
  40  * TIFF data types are referred to by Java constants and mapped internally
  41  * onto Java language data types and type names as follows:
  42  *
  43  * <br>
  44  * <br>
  45  * <table border="1">
  46  * <caption>TIFF Data Type to Java Data Type Mapping</caption>
  47  *
  48  * <tr>
  49  * <th>
  50  * <b>TIFF Data Type</b>
  51  * </th>
  52  * <th>
  53  * <b>Java Constant</b>
  54  * </th>
  55  * <th>
  56  * <b>Java Data Type</b>
  57  * </th>
  58  * <th>
  59  * <b>Java Type Name</b>
  60  * </th>
  61  * </tr>
  62  *
  63  * <tr>
  64  * <td>
  65  * <tt>BYTE</tt>
  66  * </td>
  67  * <td>
  68  * {@link TIFFTag#TIFF_BYTE}
  69  * </td>
  70  * <td>
  71  * {@code byte}
  72  * </td>
  73  * <td>
  74  * {@code "Byte"}
  75  * </td>
  76  * </tr>
  77  *
  78  * <tr>
  79  * <td>
  80  * <tt>ASCII</tt>
  81  * </td>
  82  * <td>
  83  * {@link TIFFTag#TIFF_ASCII}
  84  * </td>
  85  * <td>
  86  * {@code String}
  87  * </td>
  88  * <td>
  89  * {@code "Ascii"}
  90  * </td>
  91  * </tr>
  92  *
  93  * <tr>
  94  * <td>
  95  * <tt>SHORT</tt>
  96  * </td>
  97  * <td>
  98  * {@link TIFFTag#TIFF_SHORT}
  99  * </td>
 100  * <td>
 101  * {@code char}
 102  * </td>
 103  * <td>
 104  * {@code "Short"}
 105  * </td>
 106  * </tr>
 107  *
 108  * <tr>
 109  * <td>
 110  * <tt>LONG</tt>
 111  * </td>
 112  * <td>
 113  * {@link TIFFTag#TIFF_LONG}
 114  * </td>
 115  * <td>
 116  * {@code long}
 117  * </td>
 118  * <td>
 119  * {@code "Long"}
 120  * </td>
 121  * </tr>
 122  *
 123  * <tr>
 124  * <td>
 125  * <tt>RATIONAL</tt>
 126  * </td>
 127  * <td>
 128  * {@link TIFFTag#TIFF_RATIONAL}
 129  * </td>
 130  * <td>
 131  * {@code long[2]} {numerator, denominator}
 132  * </td>
 133  * <td>
 134  * {@code "Rational"}
 135  * </td>
 136  * </tr>
 137  *
 138  * <tr>
 139  * <td>
 140  * <tt>SBYTE</tt>
 141  * </td>
 142  * <td>
 143  * {@link TIFFTag#TIFF_SBYTE}
 144  * </td>
 145  * <td>
 146  * {@code byte}
 147  * </td>
 148  * <td>
 149  * {@code "SByte"}
 150  * </td>
 151  * </tr>
 152  *
 153  * <tr>
 154  * <td>
 155  * <tt>UNDEFINED</tt>
 156  * </td>
 157  * <td>
 158  * {@link TIFFTag#TIFF_UNDEFINED}
 159  * </td>
 160  * <td>
 161  * {@code byte}
 162  * </td>
 163  * <td>
 164  * {@code "Undefined"}
 165  * </td>
 166  * </tr>
 167  *
 168  * <tr>
 169  * <td>
 170  * <tt>SSHORT</tt>
 171  * </td>
 172  * <td>
 173  * {@link TIFFTag#TIFF_SSHORT}
 174  * </td>
 175  * <td>
 176  * {@code short}
 177  * </td>
 178  * <td>
 179  * {@code "SShort"}
 180  * </td>
 181  * </tr>
 182  *
 183  * <tr>
 184  * <td>
 185  * <tt>SLONG</tt>
 186  * </td>
 187  * <td>
 188  * {@link TIFFTag#TIFF_SLONG}
 189  * </td>
 190  * <td>
 191  * {@code int}
 192  * </td>
 193  * <td>
 194  * {@code "SLong"}
 195  * </td>
 196  * </tr>
 197  *
 198  * <tr>
 199  * <td>
 200  * <tt>SRATIONAL</tt>
 201  * </td>
 202  * <td>
 203  * {@link TIFFTag#TIFF_SRATIONAL}
 204  * </td>
 205  * <td>
 206  * {@code int[2]} {numerator, denominator}
 207  * </td>
 208  * <td>
 209  * {@code "SRational"}
 210  * </td>
 211  * </tr>
 212  *
 213  * <tr>
 214  * <td>
 215  * <tt>FLOAT</tt>
 216  * </td>
 217  * <td>
 218  * {@link TIFFTag#TIFF_FLOAT}
 219  * </td>
 220  * <td>
 221  * {@code float}
 222  * </td>
 223  * <td>
 224  * {@code "Float"}
 225  * </td>
 226  * </tr>
 227  *
 228  * <tr>
 229  * <td>
 230  * <tt>DOUBLE</tt>
 231  * </td>
 232  * <td>
 233  * {@link TIFFTag#TIFF_DOUBLE}
 234  * </td>
 235  * <td>
 236  * {@code double}
 237  * </td>
 238  * <td>
 239  * {@code "Double"}
 240  * </td>
 241  * </tr>
 242  *
 243  * <tr>
 244  * <td>
 245  * <tt>IFD</tt>
 246  * </td>
 247  * <td>
 248  * {@link TIFFTag#TIFF_IFD_POINTER}
 249  * </td>
 250  * <td>
 251  * {@code long}
 252  * </td>
 253  * <td>
 254  * {@code "IFDPointer"}
 255  * </td>
 256  * </tr>
 257  *
 258  * </table>
 259  *
 260  * @since 9
 261  * @see   TIFFDirectory
 262  * @see   TIFFTag
 263  */
 264 public final class TIFFField implements Cloneable {
 265 
 266     private static final long MAX_UINT32 = 0xffffffffL;
 267 
 268     private static final String[] TYPE_NAMES = {
 269         null,
 270         "Byte", "Ascii", "Short", "Long", "Rational",
 271         "SByte", "Undefined", "SShort", "SLong", "SRational",
 272         "Float", "Double", "IFDPointer"
 273     };
 274 
 275     private static final boolean[] IS_INTEGRAL = {
 276         false,
 277         true, false, true, true, false,
 278         true, true, true, true, false,
 279         false, false, false
 280     };
 281 
 282     /** The tag. */
 283     private TIFFTag tag;
 284 
 285     /** The tag number. */
 286     private int tagNumber;
 287 
 288     /** The tag type. */
 289     private int type;
 290 
 291     /** The number of data items present in the field. */
 292     private int count;
 293 
 294     /** The field data. */
 295     private Object data;
 296 
 297     /** The IFD contents if available. This will usually be a TIFFIFD. */
 298     private TIFFDirectory dir;
 299 
 300     /** The default constructor. */
 301     private TIFFField() {}
 302 
 303     private static String getAttribute(Node node, String attrName) {
 304         NamedNodeMap attrs = node.getAttributes();
 305         return attrs.getNamedItem(attrName).getNodeValue();
 306     }
 307 
 308     private static void initData(Node node,
 309                                  int[] otype, int[] ocount, Object[] odata) {
 310         int type;
 311         int count;
 312         Object data = null;
 313 
 314         String typeName = node.getNodeName();
 315         typeName = typeName.substring(4);
 316         typeName = typeName.substring(0, typeName.length() - 1);
 317         type = TIFFField.getTypeByName(typeName);
 318         if (type == -1) {
 319             throw new IllegalArgumentException("typeName = " + typeName);
 320         }
 321 
 322         Node child = node.getFirstChild();
 323 
 324         count = 0;
 325         while (child != null) {
 326             String childTypeName = child.getNodeName().substring(4);
 327             if (!typeName.equals(childTypeName)) {
 328                 // warning
 329             }
 330 
 331             ++count;
 332             child = child.getNextSibling();
 333         }
 334 
 335         if (count > 0) {
 336             data = createArrayForType(type, count);
 337             child = node.getFirstChild();
 338             int idx = 0;
 339             while (child != null) {
 340                 String value = getAttribute(child, "value");
 341 
 342                 String numerator, denominator;
 343                 int slashPos;
 344 
 345                 switch (type) {
 346                 case TIFFTag.TIFF_ASCII:
 347                     ((String[])data)[idx] = value;
 348                     break;
 349                 case TIFFTag.TIFF_BYTE:
 350                 case TIFFTag.TIFF_SBYTE:
 351                     ((byte[])data)[idx] =
 352                         (byte)Integer.parseInt(value);
 353                     break;
 354                 case TIFFTag.TIFF_SHORT:
 355                     ((char[])data)[idx] =
 356                         (char)Integer.parseInt(value);
 357                     break;
 358                 case TIFFTag.TIFF_SSHORT:
 359                     ((short[])data)[idx] =
 360                         (short)Integer.parseInt(value);
 361                     break;
 362                 case TIFFTag.TIFF_SLONG:
 363                     ((int[])data)[idx] =
 364                         Integer.parseInt(value);
 365                     break;
 366                 case TIFFTag.TIFF_LONG:
 367                 case TIFFTag.TIFF_IFD_POINTER:
 368                     ((long[])data)[idx] =
 369                         Long.parseLong(value);
 370                     break;
 371                 case TIFFTag.TIFF_FLOAT:
 372                     ((float[])data)[idx] =
 373                         Float.parseFloat(value);
 374                     break;
 375                 case TIFFTag.TIFF_DOUBLE:
 376                     ((double[])data)[idx] =
 377                         Double.parseDouble(value);
 378                     break;
 379                 case TIFFTag.TIFF_SRATIONAL:
 380                     slashPos = value.indexOf("/");
 381                     numerator = value.substring(0, slashPos);
 382                     denominator = value.substring(slashPos + 1);
 383 
 384                     ((int[][])data)[idx] = new int[2];
 385                     ((int[][])data)[idx][0] =
 386                         Integer.parseInt(numerator);
 387                     ((int[][])data)[idx][1] =
 388                         Integer.parseInt(denominator);
 389                     break;
 390                 case TIFFTag.TIFF_RATIONAL:
 391                     slashPos = value.indexOf("/");
 392                     numerator = value.substring(0, slashPos);
 393                     denominator = value.substring(slashPos + 1);
 394 
 395                     ((long[][])data)[idx] = new long[2];
 396                     ((long[][])data)[idx][0] =
 397                         Long.parseLong(numerator);
 398                     ((long[][])data)[idx][1] =
 399                         Long.parseLong(denominator);
 400                     break;
 401                 default:
 402                     // error
 403                 }
 404 
 405                 idx++;
 406                 child = child.getNextSibling();
 407             }
 408         }
 409 
 410         otype[0] = type;
 411         ocount[0] = count;
 412         odata[0] = data;
 413     }
 414 
 415     /**
 416      * Creates a {@code TIFFField} from a TIFF native image
 417      * metadata node. If the value of the {@code "number"} attribute
 418      * of the node is not found in {@code tagSet} then a new
 419      * {@code TIFFTag} with name {@code TIFFTag.UNKNOWN_TAG_NAME}
 420      * will be created and assigned to the field.
 421      *
 422      * @param tagSet The {@code TIFFTagSet} to which the
 423      * {@code TIFFTag} of the field belongs.
 424      * @param node A native TIFF image metadata {@code TIFFField} node.
 425      * @throws IllegalArgumentException If the {@code Node} parameter content
 426      * does not adhere to the {@code TIFFField} element structure defined by
 427      * the <a href="../../metadata/doc-files/tiff_metadata.html#ImageMetadata">
 428      * TIFF native image metadata format specification</a>, or if the
 429      * combination of node attributes and data is not legal per the
 430      * {@link #TIFFField(TIFFTag,int,int,Object)} constructor specification.
 431      * Note that a cause might be set on such an exception.
 432      * @return A new {@code TIFFField}.
 433      */
 434     public static TIFFField createFromMetadataNode(TIFFTagSet tagSet,
 435                                                    Node node) {
 436         if (node == null) {
 437             // This method is specified to throw only IllegalArgumentExceptions
 438             // so we create an IAE with a NullPointerException as its cause.
 439             throw new IllegalArgumentException(new NullPointerException
 440                 ("node == null!"));
 441         }
 442         String name = node.getNodeName();
 443         if (!name.equals("TIFFField")) {
 444             throw new IllegalArgumentException("!name.equals(\"TIFFField\")");
 445         }
 446 
 447         int tagNumber = Integer.parseInt(getAttribute(node, "number"));
 448         TIFFTag tag = null;
 449         if (tagSet != null) {
 450             tag = tagSet.getTag(tagNumber);
 451         }
 452 
 453         int type = TIFFTag.TIFF_UNDEFINED;
 454         int count = 0;
 455         Object data = null;
 456 
 457         Node child = node.getFirstChild();
 458         if (child != null) {
 459             String typeName = child.getNodeName();
 460             if (typeName.equals("TIFFUndefined")) {
 461                 String values = getAttribute(child, "value");
 462                 StringTokenizer st = new StringTokenizer(values, ",");
 463                 count = st.countTokens();
 464 
 465                 byte[] bdata = new byte[count];
 466                 for (int i = 0; i < count; i++) {
 467                     bdata[i] = (byte)Integer.parseInt(st.nextToken());
 468                 }
 469 
 470                 type = TIFFTag.TIFF_UNDEFINED;
 471                 data = bdata;
 472             } else {
 473                 int[] otype = new int[1];
 474                 int[] ocount = new int[1];
 475                 Object[] odata = new Object[1];
 476 
 477                 initData(node.getFirstChild(), otype, ocount, odata);
 478                 type = otype[0];
 479                 count = ocount[0];
 480                 data = odata[0];
 481             }
 482         } else if (tag != null) {
 483             int t = TIFFTag.MAX_DATATYPE;
 484             while(t >= TIFFTag.MIN_DATATYPE && !tag.isDataTypeOK(t)) {
 485                 t--;
 486             }
 487             type = t;
 488         }
 489 
 490         if (tag == null) {
 491             tag = new TIFFTag(TIFFTag.UNKNOWN_TAG_NAME, tagNumber, 1 << type);
 492         }
 493 
 494         TIFFField field;
 495         try {
 496             field = new TIFFField(tag, type, count, data);
 497         } catch (NullPointerException npe) {
 498             // This method is specified to throw only IllegalArgumentExceptions
 499             // so we catch the NullPointerException and set it as the cause of
 500             // the IAE which is thrown.
 501             throw new IllegalArgumentException(npe);
 502         }
 503 
 504         return field;
 505     }
 506 
 507     /**
 508      * Constructs a {@code TIFFField} with arbitrary data. The
 509      * {@code type} parameter must be a value for which
 510      * {@link TIFFTag#isDataTypeOK tag.isDataTypeOK()}
 511      * returns {@code true}. The {@code data} parameter must
 512      * be an array of a Java type appropriate for the type of the TIFF
 513      * field.
 514      *
 515      * <p>Note that the value (data) of the {@code TIFFField}
 516      * will always be the actual field value regardless of the number of
 517      * bytes required for that value. This is the case despite the fact
 518      * that the TIFF <i>IFD Entry</i> corresponding to the field may
 519      * actually contain the offset to the value of the field rather than
 520      * the value itself (the latter occurring if and only if the
 521      * value fits into 4 bytes). In other words, the value of the
 522      * field will already have been read from the TIFF stream. (An exception
 523      * to this case may occur when the field represents the contents of a
 524      * non-baseline IFD. In that case the data will be a {@code long[]}
 525      * containing the offset to the IFD and the {@code TIFFDirectory}
 526      * returned by {@link #getDirectory()} will be its contents.)
 527      *
 528      * @param tag The tag to associated with this field.
 529      * @param type One of the {@code TIFFTag.TIFF_*} constants
 530      * indicating the data type of the field as written to the TIFF stream.
 531      * @param count The number of data values.
 532      * @param data The actual data content of the field.
 533      *
 534      * @throws NullPointerException if {@code tag == null}.
 535      * @throws IllegalArgumentException if {@code type} is not
 536      * one of the {@code TIFFTag.TIFF_*} data type constants.
 537      * @throws IllegalArgumentException if {@code type} is an unacceptable
 538      * data type for the supplied {@code TIFFTag}.
 539      * @throws IllegalArgumentException if {@code count < 0}.
 540      * @throws IllegalArgumentException if {@code count < 1}
 541      * and {@code type} is {@code TIFF_RATIONAL} or
 542      * {@code TIFF_SRATIONAL}.
 543      * @throws IllegalArgumentException if {@code count != 1}
 544      * and {@code type} is {@code TIFF_IFD_POINTER}.
 545      * @throws NullPointerException if {@code data == null}.
 546      * @throws IllegalArgumentException if {@code data} is an instance of
 547      * a class incompatible with the specified type.
 548      * @throws IllegalArgumentException if the size of the data array is wrong.
 549      * @throws IllegalArgumentException if the type of the data array is
 550      * {@code TIFF_LONG}, {@code TIFF_RATIONAL}, or {@code TIFF_IFD_POINTER}
 551      * and any of the elements is negative or greater than {@code 0xffffffff}.
 552      */
 553     public TIFFField(TIFFTag tag, int type, int count, Object data) {
 554         if(tag == null) {
 555             throw new NullPointerException("tag == null!");
 556         } else if(type < TIFFTag.MIN_DATATYPE || type > TIFFTag.MAX_DATATYPE) {
 557             throw new IllegalArgumentException("Unknown data type "+type);
 558         } else if(!tag.isDataTypeOK(type)) {
 559             throw new IllegalArgumentException("Illegal data type " + type
 560                 + " for " + tag.getName() + " tag");
 561         } else if(count < 0) {
 562             throw new IllegalArgumentException("count < 0!");
 563         } else if((type == TIFFTag.TIFF_RATIONAL
 564                    || type == TIFFTag.TIFF_SRATIONAL)
 565                   && count < 1) {
 566             throw new IllegalArgumentException
 567                 ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1");
 568         } else if (type == TIFFTag.TIFF_IFD_POINTER && count != 1) {
 569             throw new IllegalArgumentException
 570                 ("Type is TIFF_IFD_POINTER count != 1");
 571         } else if(data == null) {
 572             throw new NullPointerException("data == null!");
 573         }
 574 
 575         boolean isDataArrayCorrect = false;
 576 
 577         switch (type) {
 578         case TIFFTag.TIFF_BYTE:
 579         case TIFFTag.TIFF_SBYTE:
 580         case TIFFTag.TIFF_UNDEFINED:
 581             isDataArrayCorrect = data instanceof byte[]
 582                 && ((byte[])data).length == count;
 583             break;
 584         case TIFFTag.TIFF_ASCII:
 585             isDataArrayCorrect = data instanceof String[]
 586                 && ((String[])data).length == count;
 587             break;
 588         case TIFFTag.TIFF_SHORT:
 589             isDataArrayCorrect = data instanceof char[]
 590                 && ((char[])data).length == count;
 591             break;
 592         case TIFFTag.TIFF_LONG:
 593             isDataArrayCorrect = data instanceof long[]
 594                 && ((long[])data).length == count;
 595             if (isDataArrayCorrect) {
 596                 for (long datum : (long[])data) {
 597                     if (datum < 0) {
 598                         throw new IllegalArgumentException
 599                             ("Negative value supplied for TIFF_LONG");
 600                     }
 601                     if (datum > MAX_UINT32) {
 602                         throw new IllegalArgumentException
 603                             ("Too large value supplied for TIFF_LONG");
 604                     }
 605                 }
 606             }
 607             break;
 608         case TIFFTag.TIFF_IFD_POINTER:
 609             isDataArrayCorrect = data instanceof long[]
 610                 && ((long[])data).length == 1;
 611             if (((long[])data)[0] < 0) {
 612                 throw new IllegalArgumentException
 613                     ("Negative value supplied for TIFF_IFD_POINTER");
 614             }
 615             if (((long[])data)[0] > MAX_UINT32) {
 616                 throw new IllegalArgumentException
 617                     ("Too large value supplied for TIFF_IFD_POINTER");
 618             }
 619             break;
 620         case TIFFTag.TIFF_RATIONAL:
 621             isDataArrayCorrect = data instanceof long[][]
 622                 && ((long[][])data).length == count;
 623             if (isDataArrayCorrect) {
 624                 for (long[] datum : (long[][])data) {
 625                     if (datum.length != 2) {
 626                         isDataArrayCorrect = false;
 627                         break;
 628                     }
 629                     if (datum[0] < 0 || datum[1] < 0) {
 630                         throw new IllegalArgumentException
 631                             ("Negative value supplied for TIFF_RATIONAL");
 632                     }
 633                     if (datum[0] > MAX_UINT32 || datum[1] > MAX_UINT32) {
 634                         throw new IllegalArgumentException
 635                             ("Too large value supplied for TIFF_RATIONAL");
 636                     }
 637                 }
 638             }
 639             break;
 640         case TIFFTag.TIFF_SSHORT:
 641             isDataArrayCorrect = data instanceof short[]
 642                 && ((short[])data).length == count;
 643             break;
 644         case TIFFTag.TIFF_SLONG:
 645             isDataArrayCorrect = data instanceof int[]
 646                 && ((int[])data).length == count;
 647             break;
 648         case TIFFTag.TIFF_SRATIONAL:
 649             isDataArrayCorrect = data instanceof int[][]
 650                 && ((int[][])data).length == count;
 651             if (isDataArrayCorrect) {
 652                 for (int[] datum : (int[][])data) {
 653                     if (datum.length != 2) {
 654                         isDataArrayCorrect = false;
 655                         break;
 656                     }
 657                 }
 658             }
 659             break;
 660         case TIFFTag.TIFF_FLOAT:
 661             isDataArrayCorrect = data instanceof float[]
 662                 && ((float[])data).length == count;
 663             break;
 664         case TIFFTag.TIFF_DOUBLE:
 665             isDataArrayCorrect = data instanceof double[]
 666                 && ((double[])data).length == count;
 667             break;
 668         default:
 669             throw new IllegalArgumentException("Unknown data type "+type);
 670         }
 671 
 672         if (!isDataArrayCorrect) {
 673             throw new IllegalArgumentException
 674                 ("Illegal class or length for data array");
 675         }
 676 
 677         this.tag = tag;
 678         this.tagNumber = tag.getNumber();
 679         this.type = type;
 680         this.count = count;
 681         this.data = data;
 682     }
 683 
 684     /**
 685      * Constructs a data array using {@link #createArrayForType
 686      * createArrayForType()} and invokes
 687      * {@link #TIFFField(TIFFTag,int,int,Object)} with the supplied
 688      * parameters and the created array.
 689      *
 690      * @param tag The tag to associated with this field.
 691      * @param type One of the {@code TIFFTag.TIFF_*} constants
 692      * indicating the data type of the field as written to the TIFF stream.
 693      * @param count The number of data values.
 694      * @throws NullPointerException if {@code tag == null}.
 695      * @throws IllegalArgumentException if {@code type} is not
 696      * one of the {@code TIFFTag.TIFF_*} data type constants.
 697      * @throws IllegalArgumentException if {@code type} is an unacceptable
 698      * data type for the supplied {@code TIFFTag}.
 699      * @throws IllegalArgumentException if {@code count < 0}.
 700      * @see #TIFFField(TIFFTag,int,int,Object)
 701      */
 702     public TIFFField(TIFFTag tag, int type, int count) {
 703         this(tag, type, count, createArrayForType(type, count));
 704     }
 705 
 706     /**
 707      * Constructs a {@code TIFFField} with a single non-negative integral
 708      * value. The field will have type {@link TIFFTag#TIFF_SHORT TIFF_SHORT}
 709      * if {@code value} is in {@code [0,0xffff]}, and type
 710      * {@link TIFFTag#TIFF_LONG TIFF_LONG} if {@code value} is in
 711      * {@code [0x10000,0xffffffff]}. The count of the field will be unity.
 712      *
 713      * @param tag The tag to associate with this field.
 714      * @param value The value to associate with this field.
 715      * @throws NullPointerException if {@code tag == null}.
 716      * @throws IllegalArgumentException if {@code value} is not in
 717      * {@code [0,0xffffffff]}.
 718      * @throws IllegalArgumentException if {@code value} is in
 719      * {@code [0,0xffff]} and {@code TIFF_SHORT} is an unacceptable type
 720      * for the {@code TIFFTag}, or if {@code value} is in
 721      * {@code [0x10000,0xffffffff]} and {@code TIFF_LONG} is an unacceptable
 722      * type for the {@code TIFFTag}.
 723      */
 724     public TIFFField(TIFFTag tag, long value) {
 725         if(tag == null) {
 726             throw new NullPointerException("tag == null!");
 727         }
 728         if (value < 0) {
 729             throw new IllegalArgumentException("value < 0!");
 730         }
 731         if (value > MAX_UINT32) {
 732             throw new IllegalArgumentException("value > 0xffffffff!");
 733         }
 734 
 735         this.tag = tag;
 736         this.tagNumber = tag.getNumber();
 737         this.count = 1;
 738 
 739         if (value < 65536) {
 740             if (!tag.isDataTypeOK(TIFFTag.TIFF_SHORT)) {
 741                 throw new IllegalArgumentException("Illegal data type "
 742                     + getTypeName(TIFFTag.TIFF_SHORT) + " for tag "
 743                     + "\"" + tag.getName() + "\"");
 744             }
 745             this.type = TIFFTag.TIFF_SHORT;
 746             char[] cdata = new char[1];
 747             cdata[0] = (char)value;
 748             this.data = cdata;
 749         } else {
 750             if (!tag.isDataTypeOK(TIFFTag.TIFF_LONG)) {
 751                 throw new IllegalArgumentException("Illegal data type "
 752                     + getTypeName(TIFFTag.TIFF_LONG) + " for tag "
 753                     + "\"" + tag.getName() + "\"");
 754             }
 755             this.type = TIFFTag.TIFF_LONG;
 756             long[] ldata = new long[1];
 757             ldata[0] = value;
 758             this.data = ldata;
 759         }
 760     }
 761 
 762     /**
 763      * Constructs a {@code TIFFField} with an IFD offset and contents.
 764      * The offset will be stored as the data of this field as
 765      * {@code long[] {offset}}. The directory will not be cloned. The count
 766      * of the field will be unity.
 767      *
 768      * @param tag The tag to associated with this field.
 769      * @param type One of the constants {@code TIFFTag.TIFF_LONG} or
 770      * {@code TIFFTag.TIFF_IFD_POINTER}.
 771      * @param offset The IFD offset.
 772      * @param dir The directory.
 773      *
 774      * @throws NullPointerException if {@code tag == null}.
 775      * @throws IllegalArgumentException if {@code type} is an unacceptable
 776      * data type for the supplied {@code TIFFTag}.
 777      * @throws IllegalArgumentException if {@code type} is neither
 778      * {@code TIFFTag.TIFF_LONG} nor {@code TIFFTag.TIFF_IFD_POINTER}.
 779      * @throws IllegalArgumentException if {@code offset <= 0}.
 780      * @throws NullPointerException if {@code dir == null}.
 781      *
 782      * @see #TIFFField(TIFFTag,int,int,Object)
 783      */
 784     public TIFFField(TIFFTag tag, int type, long offset, TIFFDirectory dir) {
 785         if (tag == null) {
 786             throw new NullPointerException("tag == null!");
 787         } else if (type < TIFFTag.MIN_DATATYPE || type > TIFFTag.MAX_DATATYPE) {
 788             throw new IllegalArgumentException("Unknown data type "+type);
 789         } else if (!tag.isDataTypeOK(type)) {
 790             throw new IllegalArgumentException("Illegal data type " + type
 791                 + " for " + tag.getName() + " tag");
 792         } else if (type != TIFFTag.TIFF_LONG
 793                    && type != TIFFTag.TIFF_IFD_POINTER) {
 794             throw new IllegalArgumentException("type " + type
 795                 + " is neither TIFFTag.TIFF_LONG nor TIFFTag.TIFF_IFD_POINTER");
 796         } else if (offset <= 0) {
 797             throw new IllegalArgumentException("offset " + offset
 798                 + " is non-positive");
 799         } else if (dir == null) {
 800             throw new NullPointerException("dir == null");
 801         }
 802 
 803         this.tag = tag;
 804         this.tagNumber = tag.getNumber();
 805         this.type = type;
 806         this.count = 1;
 807         this.data = new long[] {offset};
 808 
 809         this.dir = dir;
 810     }
 811 
 812     /**
 813      * Retrieves the tag associated with this field.
 814      *
 815      * @return The associated {@code TIFFTag}.
 816      */
 817     public TIFFTag getTag() {
 818         return tag;
 819     }
 820 
 821     /**
 822      * Retrieves the tag number in the range {@code [0,65535]}.
 823      *
 824      * @return The tag number.
 825      */
 826     public int getTagNumber() {
 827         return tagNumber;
 828     }
 829 
 830     /**
 831      * Returns the type of the data stored in the field.  For a TIFF 6.0
 832      * stream, the value will equal one of the {@code TIFFTag.TIFF_*}
 833      * constants. For future revisions of TIFF, higher values are possible.
 834      *
 835      * @return The data type of the field value.
 836      */
 837     public int getType() {
 838         return type;
 839     }
 840 
 841     /**
 842      * Returns the name of the supplied data type constant.
 843      *
 844      * @param dataType One of the {@code TIFFTag.TIFF_*} constants
 845      * indicating the data type of the field as written to the TIFF stream.
 846      * @return The type name corresponding to the supplied type constant.
 847      * @throws IllegalArgumentException if {@code dataType} is not
 848      * one of the {@code TIFFTag.TIFF_*} data type constants.
 849      */
 850     public static String getTypeName(int dataType) {
 851         if (dataType < TIFFTag.MIN_DATATYPE ||
 852             dataType > TIFFTag.MAX_DATATYPE) {
 853             throw new IllegalArgumentException("Unknown data type "+dataType);
 854         }
 855 
 856         return TYPE_NAMES[dataType];
 857     }
 858 
 859     /**
 860      * Returns the data type constant corresponding to the supplied data
 861      * type name. If the name is unknown {@code -1} will be returned.
 862      *
 863      * @param typeName The type name.
 864      * @return One of the {@code TIFFTag.TIFF_*} constants or
 865      * {@code -1} if the name is not recognized.
 866      */
 867     public static int getTypeByName(String typeName) {
 868         for (int i = TIFFTag.MIN_DATATYPE; i <= TIFFTag.MAX_DATATYPE; i++) {
 869             if (typeName.equals(TYPE_NAMES[i])) {
 870                 return i;
 871             }
 872         }
 873 
 874         return -1;
 875     }
 876 
 877     /**
 878      * Creates an array appropriate for the indicated data type.
 879      *
 880      * @param dataType One of the {@code TIFFTag.TIFF_*} data type
 881      * constants.
 882      * @param count The number of values in the array.
 883      * @return An array appropriate for the specified data type.
 884      *
 885      * @throws IllegalArgumentException if {@code dataType} is not
 886      * one of the {@code TIFFTag.TIFF_*} data type constants.
 887      * @throws IllegalArgumentException if {@code count < 0}.
 888      */
 889     public static Object createArrayForType(int dataType, int count) {
 890         if(count < 0) {
 891             throw new IllegalArgumentException("count < 0!");
 892         }
 893         switch (dataType) {
 894         case TIFFTag.TIFF_BYTE:
 895         case TIFFTag.TIFF_SBYTE:
 896         case TIFFTag.TIFF_UNDEFINED:
 897             return new byte[count];
 898         case TIFFTag.TIFF_ASCII:
 899             return new String[count];
 900         case TIFFTag.TIFF_SHORT:
 901             return new char[count];
 902         case TIFFTag.TIFF_LONG:
 903         case TIFFTag.TIFF_IFD_POINTER:
 904             return new long[count];
 905         case TIFFTag.TIFF_RATIONAL:
 906             return new long[count][2];
 907         case TIFFTag.TIFF_SSHORT:
 908             return new short[count];
 909         case TIFFTag.TIFF_SLONG:
 910             return new int[count];
 911         case TIFFTag.TIFF_SRATIONAL:
 912             return new int[count][2];
 913         case TIFFTag.TIFF_FLOAT:
 914             return new float[count];
 915         case TIFFTag.TIFF_DOUBLE:
 916             return new double[count];
 917         default:
 918             throw new IllegalArgumentException("Unknown data type "+dataType);
 919         }
 920     }
 921 
 922     /**
 923      * Returns the {@code TIFFField} as a node named either
 924      * <tt>"TIFFField"</tt> or <tt>"TIFFIFD"</tt> as described in the
 925      * TIFF native image metadata specification. The node will be named
 926      * <tt>"TIFFIFD"</tt> if and only if {@link #hasDirectory()} returns
 927      * {@code true} and the field's type is either {@link TIFFTag#TIFF_LONG}
 928      * or {@link TIFFTag#TIFF_IFD_POINTER}.
 929      *
 930      * @return a {@code Node} named <tt>"TIFFField"</tt> or
 931      * <tt>"TIFFIFD"</tt>.
 932      */
 933     public Node getAsNativeNode() {
 934         return new TIFFFieldNode(this);
 935     }
 936 
 937     /**
 938      * Indicates whether the value associated with the field is of
 939      * integral data type.
 940      *
 941      * @return Whether the field type is integral.
 942      */
 943     public boolean isIntegral() {
 944         return IS_INTEGRAL[type];
 945     }
 946 
 947     /**
 948      * Returns the number of data items present in the field.  For
 949      * {@code TIFFTag.TIFF_ASCII} fields, the value returned is the
 950      * number of {@code String}s, not the total length of the
 951      * data as in the file representation.
 952      *
 953      * @return The number of data items present in the field.
 954      */
 955     public int getCount() {
 956         return count;
 957     }
 958 
 959     /**
 960      * Returns a reference to the data object associated with the field.
 961      *
 962      * @return The data object of the field.
 963      */
 964     public Object getData() {
 965         return data;
 966     }
 967 
 968     /**
 969      * Returns the data as an uninterpreted array of
 970      * {@code byte}s.  The type of the field must be one of
 971      * {@code TIFFTag.TIFF_BYTE}, {@code TIFF_SBYTE}, or
 972      * {@code TIFF_UNDEFINED}.
 973      *
 974      * <p> For data in {@code TIFFTag.TIFF_BYTE} format, the application
 975      * must take care when promoting the data to longer integral types
 976      * to avoid sign extension.
 977      *
 978      * @throws ClassCastException if the field is not of type
 979      * {@code TIFF_BYTE}, {@code TIFF_SBYTE}, or
 980      * {@code TIFF_UNDEFINED}.
 981      * @return The data as an uninterpreted array of bytes.
 982      */
 983     public byte[] getAsBytes() {
 984         return (byte[])data;
 985     }
 986 
 987     /**
 988      * Returns {@code TIFFTag.TIFF_SHORT} data as an array of
 989      * {@code char}s (unsigned 16-bit integers).
 990      *
 991      * @throws ClassCastException if the field is not of type
 992      * {@code TIFF_SHORT}.
 993      * @return The data as an array of {@code char}s.
 994      */
 995     public char[] getAsChars() {
 996         return (char[])data;
 997     }
 998 
 999     /**
1000      * Returns {@code TIFFTag.TIFF_SSHORT} data as an array of
1001      * {@code short}s (signed 16-bit integers).
1002      *
1003      * @throws ClassCastException if the field is not of type
1004      * {@code TIFF_SSHORT}.
1005      * @return The data as an array of {@code short}s.
1006      */
1007     public short[] getAsShorts() {
1008         return (short[])data;
1009     }
1010 
1011     /**
1012      * Returns {@code TIFFTag.TIFF_SLONG} data as an array of
1013      * {@code int}s (signed 32-bit integers).
1014      *
1015      * @throws ClassCastException if the field is not of type
1016      * {@code TIFF_SHORT}, {@code TIFF_SSHORT}, or
1017      * {@code TIFF_SLONG}.
1018      * @return The data as an array of {@code int}s.
1019      */
1020     public int[] getAsInts() {
1021         if (data instanceof int[]) {
1022             return (int[])data;
1023         } else if (data instanceof char[]){
1024             char[] cdata = (char[])data;
1025             int[] idata = new int[cdata.length];
1026             for (int i = 0; i < cdata.length; i++) {
1027                 idata[i] = cdata[i] & 0xffff;
1028             }
1029             return idata;
1030         } else if (data instanceof short[]){
1031             short[] sdata = (short[])data;
1032             int[] idata = new int[sdata.length];
1033             for (int i = 0; i < sdata.length; i++) {
1034                 idata[i] = (int)sdata[i];
1035             }
1036             return idata;
1037         } else {
1038             throw new ClassCastException("Data not char[], short[], or int[]!");
1039         }
1040     }
1041 
1042     /**
1043      * Returns {@code TIFFTag.TIFF_LONG} or
1044      * {@code TIFF_IFD_POINTER} data as an array of
1045      * {@code long}s (signed 64-bit integers).
1046      *
1047      * @throws ClassCastException if the field is not of type
1048      * {@code TIFF_LONG} or {@code TIFF_IFD_POINTER}.
1049      * @return The data as an array of {@code long}s.
1050      */
1051     public long[] getAsLongs() {
1052         return (long[])data;
1053     }
1054 
1055     /**
1056      * Returns {@code TIFFTag.TIFF_FLOAT} data as an array of
1057      * {@code float}s (32-bit floating-point values).
1058      *
1059      * @throws ClassCastException if the field is not of type
1060      * {@code TIFF_FLOAT}.
1061      * @return The data as an array of {@code float}s.
1062      */
1063     public float[] getAsFloats() {
1064         return (float[])data;
1065     }
1066 
1067     /**
1068      * Returns {@code TIFFTag.TIFF_DOUBLE} data as an array of
1069      * {@code double}s (64-bit floating-point values).
1070      *
1071      * @throws ClassCastException if the field is not of type
1072      * {@code TIFF_DOUBLE}.
1073      * @return The data as an array of {@code double}s.
1074      */
1075     public double[] getAsDoubles() {
1076         return (double[])data;
1077     }
1078 
1079     /**
1080      * Returns {@code TIFFTag.TIFF_SRATIONAL} data as an array of
1081      * 2-element arrays of {@code int}s.
1082      *
1083      * @throws ClassCastException if the field is not of type
1084      * {@code TIFF_SRATIONAL}.
1085      * @return The data as an array of signed rationals.
1086      */
1087     public int[][] getAsSRationals() {
1088         return (int[][])data;
1089     }
1090 
1091     /**
1092      * Returns {@code TIFFTag.TIFF_RATIONAL} data as an array of
1093      * 2-element arrays of {@code long}s.
1094      *
1095      * @throws ClassCastException if the field is not of type
1096      * {@code TIFF_RATIONAL}.
1097      * @return The data as an array of unsigned rationals.
1098      */
1099     public long[][] getAsRationals() {
1100         return (long[][])data;
1101     }
1102 
1103     /**
1104      * Returns data in any format as an {@code int}.
1105      *
1106      * <p> {@code TIFFTag.TIFF_BYTE} values are treated as unsigned; that
1107      * is, no sign extension will take place and the returned value
1108      * will be in the range [0, 255].  {@code TIFF_SBYTE} data
1109      * will be returned in the range [-128, 127].
1110      *
1111      * <p> A {@code TIFF_UNDEFINED} value is treated as though
1112      * it were a {@code TIFF_BYTE}.
1113      *
1114      * <p> Data in {@code TIFF_SLONG}, {@code TIFF_LONG},
1115      * {@code TIFF_FLOAT}, {@code TIFF_DOUBLE} or
1116      * {@code TIFF_IFD_POINTER} format are simply cast to
1117      * {@code int} and may suffer from truncation.
1118      *
1119      * <p> Data in {@code TIFF_SRATIONAL} or
1120      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1121      * numerator into the denominator using double-precision
1122      * arithmetic and then casting to {@code int}.  Loss of
1123      * precision and truncation may occur.
1124      *
1125      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1126      * the {@code Double.parseDouble} method, with the result
1127      * case to {@code int}.
1128      *
1129      * @param index The index of the data.
1130      * @return The data at the given index as an {@code int}.
1131      */
1132     public int getAsInt(int index) {
1133         switch (type) {
1134         case TIFFTag.TIFF_BYTE:
1135         case TIFFTag.TIFF_UNDEFINED:
1136             return ((byte[])data)[index] & 0xff;
1137         case TIFFTag.TIFF_SBYTE:
1138             return ((byte[])data)[index];
1139         case TIFFTag.TIFF_SHORT:
1140             return ((char[])data)[index] & 0xffff;
1141         case TIFFTag.TIFF_SSHORT:
1142             return ((short[])data)[index];
1143         case TIFFTag.TIFF_SLONG:
1144             return ((int[])data)[index];
1145         case TIFFTag.TIFF_LONG:
1146         case TIFFTag.TIFF_IFD_POINTER:
1147             return (int)((long[])data)[index];
1148         case TIFFTag.TIFF_FLOAT:
1149             return (int)((float[])data)[index];
1150         case TIFFTag.TIFF_DOUBLE:
1151             return (int)((double[])data)[index];
1152         case TIFFTag.TIFF_SRATIONAL:
1153             int[] ivalue = getAsSRational(index);
1154             return (int)((double)ivalue[0]/ivalue[1]);
1155         case TIFFTag.TIFF_RATIONAL:
1156             long[] lvalue = getAsRational(index);
1157             return (int)((double)lvalue[0]/lvalue[1]);
1158         case TIFFTag.TIFF_ASCII:
1159              String s = ((String[])data)[index];
1160              return (int)Double.parseDouble(s);
1161         default:
1162             throw new ClassCastException(); // should never happen
1163         }
1164     }
1165 
1166     /**
1167      * Returns data in any format as a {@code long}.
1168      *
1169      * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data
1170      * are treated as unsigned; that is, no sign extension will take
1171      * place and the returned value will be in the range [0, 255].
1172      * {@code TIFF_SBYTE} data will be returned in the range
1173      * [-128, 127].
1174      *
1175      * <p> Data in {@code TIFF_FLOAT} and {@code TIFF_DOUBLE} are
1176      * simply cast to {@code long} and may suffer from truncation.
1177      *
1178      * <p> Data in {@code TIFF_SRATIONAL} or
1179      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1180      * numerator into the denominator using double-precision
1181      * arithmetic and then casting to {@code long}.  Loss of
1182      * precision and truncation may occur.
1183      *
1184      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1185      * the {@code Double.parseDouble} method, with the result
1186      * cast to {@code long}.
1187      *
1188      * @param index The index of the data.
1189      * @return The data at the given index as a {@code long}.
1190      */
1191     public long getAsLong(int index) {
1192         switch (type) {
1193         case TIFFTag.TIFF_BYTE:
1194         case TIFFTag.TIFF_UNDEFINED:
1195             return ((byte[])data)[index] & 0xff;
1196         case TIFFTag.TIFF_SBYTE:
1197             return ((byte[])data)[index];
1198         case TIFFTag.TIFF_SHORT:
1199             return ((char[])data)[index] & 0xffff;
1200         case TIFFTag.TIFF_SSHORT:
1201             return ((short[])data)[index];
1202         case TIFFTag.TIFF_SLONG:
1203             return ((int[])data)[index];
1204         case TIFFTag.TIFF_LONG:
1205         case TIFFTag.TIFF_IFD_POINTER:
1206             return ((long[])data)[index];
1207         case TIFFTag.TIFF_FLOAT:
1208             return (long)((float[])data)[index];
1209         case TIFFTag.TIFF_DOUBLE:
1210             return (long)((double[])data)[index];
1211         case TIFFTag.TIFF_SRATIONAL:
1212             int[] ivalue = getAsSRational(index);
1213             return (long)((double)ivalue[0]/ivalue[1]);
1214         case TIFFTag.TIFF_RATIONAL:
1215             long[] lvalue = getAsRational(index);
1216             return (long)((double)lvalue[0]/lvalue[1]);
1217         case TIFFTag.TIFF_ASCII:
1218              String s = ((String[])data)[index];
1219              return (long)Double.parseDouble(s);
1220         default:
1221             throw new ClassCastException(); // should never happen
1222         }
1223     }
1224 
1225     /**
1226      * Returns data in any format as a {@code float}.
1227      *
1228      * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data
1229      * are treated as unsigned; that is, no sign extension will take
1230      * place and the returned value will be in the range [0, 255].
1231      * {@code TIFF_SBYTE} data will be returned in the range
1232      * [-128, 127].
1233      *
1234      * <p> Data in {@code TIFF_SLONG}, {@code TIFF_LONG},
1235      * {@code TIFF_DOUBLE}, or {@code TIFF_IFD_POINTER} format are
1236      * simply cast to {@code float} and may suffer from
1237      * truncation.
1238      *
1239      * <p> Data in {@code TIFF_SRATIONAL} or
1240      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1241      * numerator into the denominator using double-precision
1242      * arithmetic and then casting to {@code float}.
1243      *
1244      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1245      * the {@code Double.parseDouble} method, with the result
1246      * cast to {@code float}.
1247      *
1248      * @param index The index of the data.
1249      * @return The data at the given index as a {@code float}.
1250      */
1251     public float getAsFloat(int index) {
1252         switch (type) {
1253         case TIFFTag.TIFF_BYTE:
1254         case TIFFTag.TIFF_UNDEFINED:
1255             return ((byte[])data)[index] & 0xff;
1256         case TIFFTag.TIFF_SBYTE:
1257             return ((byte[])data)[index];
1258         case TIFFTag.TIFF_SHORT:
1259             return ((char[])data)[index] & 0xffff;
1260         case TIFFTag.TIFF_SSHORT:
1261             return ((short[])data)[index];
1262         case TIFFTag.TIFF_SLONG:
1263             return ((int[])data)[index];
1264         case TIFFTag.TIFF_LONG:
1265         case TIFFTag.TIFF_IFD_POINTER:
1266             return ((long[])data)[index];
1267         case TIFFTag.TIFF_FLOAT:
1268             return ((float[])data)[index];
1269         case TIFFTag.TIFF_DOUBLE:
1270             return (float)((double[])data)[index];
1271         case TIFFTag.TIFF_SRATIONAL:
1272             int[] ivalue = getAsSRational(index);
1273             return (float)((double)ivalue[0]/ivalue[1]);
1274         case TIFFTag.TIFF_RATIONAL:
1275             long[] lvalue = getAsRational(index);
1276             return (float)((double)lvalue[0]/lvalue[1]);
1277         case TIFFTag.TIFF_ASCII:
1278              String s = ((String[])data)[index];
1279              return (float)Double.parseDouble(s);
1280         default:
1281             throw new ClassCastException(); // should never happen
1282         }
1283     }
1284 
1285     /**
1286      * Returns data in any format as a {@code double}.
1287      *
1288      * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data
1289      * are treated as unsigned; that is, no sign extension will take
1290      * place and the returned value will be in the range [0, 255].
1291      * {@code TIFF_SBYTE} data will be returned in the range
1292      * [-128, 127].
1293      *
1294      * <p> Data in {@code TIFF_SRATIONAL} or
1295      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1296      * numerator into the denominator using double-precision
1297      * arithmetic.
1298      *
1299      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1300      * the {@code Double.parseDouble} method.
1301      *
1302      * @param index The index of the data.
1303      * @return The data at the given index as a {@code double}.
1304      */
1305     public double getAsDouble(int index) {
1306         switch (type) {
1307         case TIFFTag.TIFF_BYTE:
1308         case TIFFTag.TIFF_UNDEFINED:
1309             return ((byte[])data)[index] & 0xff;
1310         case TIFFTag.TIFF_SBYTE:
1311             return ((byte[])data)[index];
1312         case TIFFTag.TIFF_SHORT:
1313             return ((char[])data)[index] & 0xffff;
1314         case TIFFTag.TIFF_SSHORT:
1315             return ((short[])data)[index];
1316         case TIFFTag.TIFF_SLONG:
1317             return ((int[])data)[index];
1318         case TIFFTag.TIFF_LONG:
1319         case TIFFTag.TIFF_IFD_POINTER:
1320             return ((long[])data)[index];
1321         case TIFFTag.TIFF_FLOAT:
1322             return ((float[])data)[index];
1323         case TIFFTag.TIFF_DOUBLE:
1324             return ((double[])data)[index];
1325         case TIFFTag.TIFF_SRATIONAL:
1326             int[] ivalue = getAsSRational(index);
1327             return (double)ivalue[0]/ivalue[1];
1328         case TIFFTag.TIFF_RATIONAL:
1329             long[] lvalue = getAsRational(index);
1330             return (double)lvalue[0]/lvalue[1];
1331         case TIFFTag.TIFF_ASCII:
1332              String s = ((String[])data)[index];
1333              return Double.parseDouble(s);
1334         default:
1335             throw new ClassCastException(); // should never happen
1336         }
1337     }
1338 
1339     /**
1340      * Returns a {@code TIFFTag.TIFF_ASCII} value as a
1341      * {@code String}.
1342      *
1343      * @throws ClassCastException if the field is not of type
1344      * {@code TIFF_ASCII}.
1345      *
1346      * @param index The index of the data.
1347      * @return The data at the given index as a {@code String}.
1348      */
1349     public String getAsString(int index) {
1350         return ((String[])data)[index];
1351     }
1352 
1353     /**
1354      * Returns a {@code TIFFTag.TIFF_SRATIONAL} data item as a
1355      * two-element array of {@code int}s.
1356      *
1357      * @param index The index of the data.
1358      * @return The data at the given index as a signed rational.
1359      * @throws ClassCastException if the field is not of type
1360      * {@code TIFF_SRATIONAL}.
1361      */
1362     public int[] getAsSRational(int index) {
1363         return ((int[][])data)[index];
1364     }
1365 
1366     /**
1367      * Returns a TIFFTag.TIFF_RATIONAL data item as a two-element array
1368      * of ints.
1369      *
1370      * @param index The index of the data.
1371      * @return The data at the given index as an unsigned rational.
1372      * @throws ClassCastException if the field is not of type
1373      * {@code TIFF_RATIONAL}.
1374      */
1375     public long[] getAsRational(int index) {
1376         return ((long[][])data)[index];
1377     }
1378 
1379 
1380     /**
1381      * Returns a {@code String} containing a human-readable
1382      * version of the data item.  Data of type
1383      * {@code TIFFTag.TIFF_RATIONAL} or {@code TIFF_SRATIONAL} are
1384      * represented as a pair of integers separated by a
1385      * {@code '/'} character.  If the numerator of a
1386      * {@code TIFFTag.TIFF_RATIONAL} or {@code TIFF_SRATIONAL} is an integral
1387      * multiple of the denominator, then the value is represented as
1388      * {@code "q/1"} where {@code q} is the quotient of the numerator and
1389      * denominator.
1390      *
1391      * @param index The index of the data.
1392      * @return The data at the given index as a {@code String}.
1393      * @throws ClassCastException if the field is not of one of the
1394      * legal field types.
1395      */
1396     public String getValueAsString(int index) {
1397         switch (type) {
1398         case TIFFTag.TIFF_ASCII:
1399             return ((String[])data)[index];
1400         case TIFFTag.TIFF_BYTE:
1401         case TIFFTag.TIFF_UNDEFINED:
1402             return Integer.toString(((byte[])data)[index] & 0xff);
1403         case TIFFTag.TIFF_SBYTE:
1404             return Integer.toString(((byte[])data)[index]);
1405         case TIFFTag.TIFF_SHORT:
1406             return Integer.toString(((char[])data)[index] & 0xffff);
1407         case TIFFTag.TIFF_SSHORT:
1408             return Integer.toString(((short[])data)[index]);
1409         case TIFFTag.TIFF_SLONG:
1410             return Integer.toString(((int[])data)[index]);
1411         case TIFFTag.TIFF_LONG:
1412         case TIFFTag.TIFF_IFD_POINTER:
1413             return Long.toString(((long[])data)[index]);
1414         case TIFFTag.TIFF_FLOAT:
1415             return Float.toString(((float[])data)[index]);
1416         case TIFFTag.TIFF_DOUBLE:
1417             return Double.toString(((double[])data)[index]);
1418         case TIFFTag.TIFF_SRATIONAL:
1419             int[] ivalue = getAsSRational(index);
1420             String srationalString;
1421             if(ivalue[1] != 0 && ivalue[0] % ivalue[1] == 0) {
1422                 // If the denominator is a non-zero integral divisor
1423                 // of the numerator then convert the fraction to be
1424                 // with respect to a unity denominator.
1425                 srationalString =
1426                     Integer.toString(ivalue[0] / ivalue[1]) + "/1";
1427             } else {
1428                 // Use the values directly.
1429                 srationalString =
1430                     Integer.toString(ivalue[0]) +
1431                     "/" +
1432                     Integer.toString(ivalue[1]);
1433             }
1434             return srationalString;
1435         case TIFFTag.TIFF_RATIONAL:
1436             long[] lvalue = getAsRational(index);
1437             String rationalString;
1438             if(lvalue[1] != 0L && lvalue[0] % lvalue[1] == 0) {
1439                 // If the denominator is a non-zero integral divisor
1440                 // of the numerator then convert the fraction to be
1441                 // with respect to a unity denominator.
1442                 rationalString =
1443                     Long.toString(lvalue[0] / lvalue[1]) + "/1";
1444             } else {
1445                 // Use the values directly.
1446                 rationalString =
1447                     Long.toString(lvalue[0]) +
1448                     "/" +
1449                     Long.toString(lvalue[1]);
1450             }
1451             return rationalString;
1452         default:
1453             throw new ClassCastException(); // should never happen
1454         }
1455     }
1456 
1457     /**
1458      * Returns whether the field has a {@code TIFFDirectory}.
1459      *
1460      * @return true if and only if getDirectory() returns non-null.
1461      */
1462     public boolean hasDirectory() {
1463         return getDirectory() != null;
1464     }
1465 
1466     /**
1467      * Returns the associated {@code TIFFDirectory}, if available. If no
1468      * directory is set, then {@code null} will be returned.
1469      *
1470      * @return the TIFFDirectory instance or null.
1471      */
1472     public TIFFDirectory getDirectory() {
1473         return dir;
1474     }
1475 
1476     /**
1477      * Clones the field and all the information contained therein.
1478      *
1479      * @return A clone of this {@code TIFFField}.
1480      * @throws CloneNotSupportedException if the instance cannot be cloned.
1481      */
1482     @Override
1483     public TIFFField clone() throws CloneNotSupportedException {
1484         TIFFField field = (TIFFField)super.clone();
1485 
1486         Object fieldData;
1487         switch (type) {
1488         case TIFFTag.TIFF_BYTE:
1489         case TIFFTag.TIFF_UNDEFINED:
1490         case TIFFTag.TIFF_SBYTE:
1491             fieldData = ((byte[])data).clone();
1492             break;
1493         case TIFFTag.TIFF_SHORT:
1494             fieldData = ((char[])data).clone();
1495             break;
1496         case TIFFTag.TIFF_SSHORT:
1497             fieldData = ((short[])data).clone();
1498             break;
1499         case TIFFTag.TIFF_SLONG:
1500             fieldData = ((int[])data).clone();
1501             break;
1502         case TIFFTag.TIFF_LONG:
1503         case TIFFTag.TIFF_IFD_POINTER:
1504             fieldData = ((long[])data).clone();
1505             break;
1506         case TIFFTag.TIFF_FLOAT:
1507             fieldData = ((float[])data).clone();
1508             break;
1509         case TIFFTag.TIFF_DOUBLE:
1510             fieldData = ((double[])data).clone();
1511             break;
1512         case TIFFTag.TIFF_SRATIONAL:
1513             fieldData = ((int[][])data).clone();
1514             break;
1515         case TIFFTag.TIFF_RATIONAL:
1516             fieldData = ((long[][])data).clone();
1517             break;
1518         case TIFFTag.TIFF_ASCII:
1519             fieldData = ((String[])data).clone();
1520             break;
1521         default:
1522             throw new ClassCastException(); // should never happen
1523         }
1524 
1525         field.tag = tag;
1526         field.tagNumber = tagNumber;
1527         field.type = type;
1528         field.count = count;
1529         field.data = fieldData;
1530         field.dir = dir != null ? dir.clone() : null;
1531 
1532         return field;
1533     }
1534 }