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 4294967295}.
 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.
 709      * The field will have type
 710      * {@link TIFFTag#TIFF_SHORT  TIFF_SHORT} if
 711      * {@code val < 65536} and type
 712      * {@link TIFFTag#TIFF_LONG TIFF_LONG} otherwise.  The count
 713      * of the field will be unity.
 714      *
 715      * @param tag The tag to associate with this field.
 716      * @param value The value to associate with this field.
 717      * @throws NullPointerException if {@code tag == null}.
 718      * @throws IllegalArgumentException if the derived type is unacceptable
 719      * for the supplied {@code TIFFTag}.
 720      * @throws IllegalArgumentException if {@code value < 0}.
 721      */
 722     public TIFFField(TIFFTag tag, int value) {
 723         if(tag == null) {
 724             throw new NullPointerException("tag == null!");
 725         }
 726         if (value < 0) {
 727             throw new IllegalArgumentException("value < 0!");
 728         }
 729 
 730         this.tag = tag;
 731         this.tagNumber = tag.getNumber();
 732         this.count = 1;
 733 
 734         if (value < 65536) {
 735             if (!tag.isDataTypeOK(TIFFTag.TIFF_SHORT)) {
 736                 throw new IllegalArgumentException("Illegal data type "
 737                     + TIFFTag.TIFF_SHORT + " for " + tag.getName() + " tag");
 738             }
 739             this.type = TIFFTag.TIFF_SHORT;
 740             char[] cdata = new char[1];
 741             cdata[0] = (char)value;
 742             this.data = cdata;
 743         } else {
 744             if (!tag.isDataTypeOK(TIFFTag.TIFF_LONG)) {
 745                 throw new IllegalArgumentException("Illegal data type "
 746                     + TIFFTag.TIFF_LONG + " for " + tag.getName() + " tag");
 747             }
 748             this.type = TIFFTag.TIFF_LONG;
 749             long[] ldata = new long[1];
 750             ldata[0] = value;
 751             this.data = ldata;
 752         }
 753     }
 754 
 755     /**
 756      * Constructs a {@code TIFFField} with an IFD offset and contents.
 757      * The offset will be stored as the data of this field as
 758      * {@code long[] {offset}}. The directory will not be cloned. The count
 759      * of the field will be unity.
 760      *
 761      * @param tag The tag to associated with this field.
 762      * @param type One of the constants {@code TIFFTag.TIFF_LONG} or
 763      * {@code TIFFTag.TIFF_IFD_POINTER}.
 764      * @param offset The IFD offset.
 765      * @param dir The directory.
 766      *
 767      * @throws NullPointerException if {@code tag == null}.
 768      * @throws IllegalArgumentException if {@code type} is an unacceptable
 769      * data type for the supplied {@code TIFFTag}.
 770      * @throws IllegalArgumentException if {@code type} is neither
 771      * {@code TIFFTag.TIFF_LONG} nor {@code TIFFTag.TIFF_IFD_POINTER}.
 772      * @throws IllegalArgumentException if {@code offset <= 0}.
 773      * @throws NullPointerException if {@code dir == null}.
 774      *
 775      * @see #TIFFField(TIFFTag,int,int,Object)
 776      */
 777     public TIFFField(TIFFTag tag, int type, long offset, TIFFDirectory dir) {
 778         if (tag == null) {
 779             throw new NullPointerException("tag == null!");
 780         } else if (type < TIFFTag.MIN_DATATYPE || type > TIFFTag.MAX_DATATYPE) {
 781             throw new IllegalArgumentException("Unknown data type "+type);
 782         } else if (!tag.isDataTypeOK(type)) {
 783             throw new IllegalArgumentException("Illegal data type " + type
 784                 + " for " + tag.getName() + " tag");
 785         } else if (type != TIFFTag.TIFF_LONG
 786                    && type != TIFFTag.TIFF_IFD_POINTER) {
 787             throw new IllegalArgumentException("type " + type
 788                 + " is neither TIFFTag.TIFF_LONG nor TIFFTag.TIFF_IFD_POINTER");
 789         } else if (offset <= 0) {
 790             throw new IllegalArgumentException("offset " + offset
 791                 + " is non-positive");
 792         } else if (dir == null) {
 793             throw new NullPointerException("dir == null");
 794         }
 795 
 796         this.tag = tag;
 797         this.tagNumber = tag.getNumber();
 798         this.type = type;
 799         this.count = 1;
 800         this.data = new long[] {offset};
 801 
 802         this.dir = dir;
 803     }
 804 
 805     /**
 806      * Retrieves the tag associated with this field.
 807      *
 808      * @return The associated {@code TIFFTag}.
 809      */
 810     public TIFFTag getTag() {
 811         return tag;
 812     }
 813 
 814     /**
 815      * Retrieves the tag number in the range {@code [0,65535]}.
 816      *
 817      * @return The tag number.
 818      */
 819     public int getTagNumber() {
 820         return tagNumber;
 821     }
 822 
 823     /**
 824      * Returns the type of the data stored in the field.  For a TIFF 6.0
 825      * stream, the value will equal one of the {@code TIFFTag.TIFF_*}
 826      * constants. For future revisions of TIFF, higher values are possible.
 827      *
 828      * @return The data type of the field value.
 829      */
 830     public int getType() {
 831         return type;
 832     }
 833 
 834     /**
 835      * Returns the name of the supplied data type constant.
 836      *
 837      * @param dataType One of the {@code TIFFTag.TIFF_*} constants
 838      * indicating the data type of the field as written to the TIFF stream.
 839      * @return The type name corresponding to the supplied type constant.
 840      * @throws IllegalArgumentException if {@code dataType} is not
 841      * one of the {@code TIFFTag.TIFF_*} data type constants.
 842      */
 843     public static String getTypeName(int dataType) {
 844         if (dataType < TIFFTag.MIN_DATATYPE ||
 845             dataType > TIFFTag.MAX_DATATYPE) {
 846             throw new IllegalArgumentException("Unknown data type "+dataType);
 847         }
 848 
 849         return TYPE_NAMES[dataType];
 850     }
 851 
 852     /**
 853      * Returns the data type constant corresponding to the supplied data
 854      * type name. If the name is unknown {@code -1} will be returned.
 855      *
 856      * @param typeName The type name.
 857      * @return One of the {@code TIFFTag.TIFF_*} constants or
 858      * {@code -1} if the name is not recognized.
 859      */
 860     public static int getTypeByName(String typeName) {
 861         for (int i = TIFFTag.MIN_DATATYPE; i <= TIFFTag.MAX_DATATYPE; i++) {
 862             if (typeName.equals(TYPE_NAMES[i])) {
 863                 return i;
 864             }
 865         }
 866 
 867         return -1;
 868     }
 869 
 870     /**
 871      * Creates an array appropriate for the indicated data type.
 872      *
 873      * @param dataType One of the {@code TIFFTag.TIFF_*} data type
 874      * constants.
 875      * @param count The number of values in the array.
 876      * @return An array appropriate for the specified data type.
 877      *
 878      * @throws IllegalArgumentException if {@code dataType} is not
 879      * one of the {@code TIFFTag.TIFF_*} data type constants.
 880      * @throws IllegalArgumentException if {@code count < 0}.
 881      */
 882     public static Object createArrayForType(int dataType, int count) {
 883         if(count < 0) {
 884             throw new IllegalArgumentException("count < 0!");
 885         }
 886         switch (dataType) {
 887         case TIFFTag.TIFF_BYTE:
 888         case TIFFTag.TIFF_SBYTE:
 889         case TIFFTag.TIFF_UNDEFINED:
 890             return new byte[count];
 891         case TIFFTag.TIFF_ASCII:
 892             return new String[count];
 893         case TIFFTag.TIFF_SHORT:
 894             return new char[count];
 895         case TIFFTag.TIFF_LONG:
 896         case TIFFTag.TIFF_IFD_POINTER:
 897             return new long[count];
 898         case TIFFTag.TIFF_RATIONAL:
 899             return new long[count][2];
 900         case TIFFTag.TIFF_SSHORT:
 901             return new short[count];
 902         case TIFFTag.TIFF_SLONG:
 903             return new int[count];
 904         case TIFFTag.TIFF_SRATIONAL:
 905             return new int[count][2];
 906         case TIFFTag.TIFF_FLOAT:
 907             return new float[count];
 908         case TIFFTag.TIFF_DOUBLE:
 909             return new double[count];
 910         default:
 911             throw new IllegalArgumentException("Unknown data type "+dataType);
 912         }
 913     }
 914 
 915     /**
 916      * Returns the {@code TIFFField} as a node named either
 917      * <tt>"TIFFField"</tt> or <tt>"TIFFIFD"</tt> as described in the
 918      * TIFF native image metadata specification. The node will be named
 919      * <tt>"TIFFIFD"</tt> if and only if {@link #hasDirectory()} returns
 920      * {@code true} and the field's type is either {@link TIFFTag#TIFF_LONG}
 921      * or {@link TIFFTag#TIFF_IFD_POINTER}.
 922      *
 923      * @return a {@code Node} named <tt>"TIFFField"</tt> or
 924      * <tt>"TIFFIFD"</tt>.
 925      */
 926     public Node getAsNativeNode() {
 927         return new TIFFFieldNode(this);
 928     }
 929 
 930     /**
 931      * Indicates whether the value associated with the field is of
 932      * integral data type.
 933      *
 934      * @return Whether the field type is integral.
 935      */
 936     public boolean isIntegral() {
 937         return IS_INTEGRAL[type];
 938     }
 939 
 940     /**
 941      * Returns the number of data items present in the field.  For
 942      * {@code TIFFTag.TIFF_ASCII} fields, the value returned is the
 943      * number of {@code String}s, not the total length of the
 944      * data as in the file representation.
 945      *
 946      * @return The number of data items present in the field.
 947      */
 948     public int getCount() {
 949         return count;
 950     }
 951 
 952     /**
 953      * Returns a reference to the data object associated with the field.
 954      *
 955      * @return The data object of the field.
 956      */
 957     public Object getData() {
 958         return data;
 959     }
 960 
 961     /**
 962      * Returns the data as an uninterpreted array of
 963      * {@code byte}s.  The type of the field must be one of
 964      * {@code TIFFTag.TIFF_BYTE}, {@code TIFF_SBYTE}, or
 965      * {@code TIFF_UNDEFINED}.
 966      *
 967      * <p> For data in {@code TIFFTag.TIFF_BYTE} format, the application
 968      * must take care when promoting the data to longer integral types
 969      * to avoid sign extension.
 970      *
 971      * @throws ClassCastException if the field is not of type
 972      * {@code TIFF_BYTE}, {@code TIFF_SBYTE}, or
 973      * {@code TIFF_UNDEFINED}.
 974      * @return The data as an uninterpreted array of bytes.
 975      */
 976     public byte[] getAsBytes() {
 977         return (byte[])data;
 978     }
 979 
 980     /**
 981      * Returns {@code TIFFTag.TIFF_SHORT} data as an array of
 982      * {@code char}s (unsigned 16-bit integers).
 983      *
 984      * @throws ClassCastException if the field is not of type
 985      * {@code TIFF_SHORT}.
 986      * @return The data as an array of {@code char}s.
 987      */
 988     public char[] getAsChars() {
 989         return (char[])data;
 990     }
 991 
 992     /**
 993      * Returns {@code TIFFTag.TIFF_SSHORT} data as an array of
 994      * {@code short}s (signed 16-bit integers).
 995      *
 996      * @throws ClassCastException if the field is not of type
 997      * {@code TIFF_SSHORT}.
 998      * @return The data as an array of {@code short}s.
 999      */
1000     public short[] getAsShorts() {
1001         return (short[])data;
1002     }
1003 
1004     /**
1005      * Returns {@code TIFFTag.TIFF_SLONG} data as an array of
1006      * {@code int}s (signed 32-bit integers).
1007      *
1008      * @throws ClassCastException if the field is not of type
1009      * {@code TIFF_SHORT}, {@code TIFF_SSHORT}, or
1010      * {@code TIFF_SLONG}.
1011      * @return The data as an array of {@code int}s.
1012      */
1013     public int[] getAsInts() {
1014         if (data instanceof int[]) {
1015             return (int[])data;
1016         } else if (data instanceof char[]){
1017             char[] cdata = (char[])data;
1018             int[] idata = new int[cdata.length];
1019             for (int i = 0; i < cdata.length; i++) {
1020                 idata[i] = cdata[i] & 0xffff;
1021             }
1022             return idata;
1023         } else if (data instanceof short[]){
1024             short[] sdata = (short[])data;
1025             int[] idata = new int[sdata.length];
1026             for (int i = 0; i < sdata.length; i++) {
1027                 idata[i] = (int)sdata[i];
1028             }
1029             return idata;
1030         } else {
1031             throw new ClassCastException("Data not char[], short[], or int[]!");
1032         }
1033     }
1034 
1035     /**
1036      * Returns {@code TIFFTag.TIFF_LONG} or
1037      * {@code TIFF_IFD_POINTER} data as an array of
1038      * {@code long}s (signed 64-bit integers).
1039      *
1040      * @throws ClassCastException if the field is not of type
1041      * {@code TIFF_LONG} or {@code TIFF_IFD_POINTER}.
1042      * @return The data as an array of {@code long}s.
1043      */
1044     public long[] getAsLongs() {
1045         return (long[])data;
1046     }
1047 
1048     /**
1049      * Returns {@code TIFFTag.TIFF_FLOAT} data as an array of
1050      * {@code float}s (32-bit floating-point values).
1051      *
1052      * @throws ClassCastException if the field is not of type
1053      * {@code TIFF_FLOAT}.
1054      * @return The data as an array of {@code float}s.
1055      */
1056     public float[] getAsFloats() {
1057         return (float[])data;
1058     }
1059 
1060     /**
1061      * Returns {@code TIFFTag.TIFF_DOUBLE} data as an array of
1062      * {@code double}s (64-bit floating-point values).
1063      *
1064      * @throws ClassCastException if the field is not of type
1065      * {@code TIFF_DOUBLE}.
1066      * @return The data as an array of {@code double}s.
1067      */
1068     public double[] getAsDoubles() {
1069         return (double[])data;
1070     }
1071 
1072     /**
1073      * Returns {@code TIFFTag.TIFF_SRATIONAL} data as an array of
1074      * 2-element arrays of {@code int}s.
1075      *
1076      * @throws ClassCastException if the field is not of type
1077      * {@code TIFF_SRATIONAL}.
1078      * @return The data as an array of signed rationals.
1079      */
1080     public int[][] getAsSRationals() {
1081         return (int[][])data;
1082     }
1083 
1084     /**
1085      * Returns {@code TIFFTag.TIFF_RATIONAL} data as an array of
1086      * 2-element arrays of {@code long}s.
1087      *
1088      * @throws ClassCastException if the field is not of type
1089      * {@code TIFF_RATIONAL}.
1090      * @return The data as an array of unsigned rationals.
1091      */
1092     public long[][] getAsRationals() {
1093         return (long[][])data;
1094     }
1095 
1096     /**
1097      * Returns data in any format as an {@code int}.
1098      *
1099      * <p> {@code TIFFTag.TIFF_BYTE} values are treated as unsigned; that
1100      * is, no sign extension will take place and the returned value
1101      * will be in the range [0, 255].  {@code TIFF_SBYTE} data
1102      * will be returned in the range [-128, 127].
1103      *
1104      * <p> A {@code TIFF_UNDEFINED} value is treated as though
1105      * it were a {@code TIFF_BYTE}.
1106      *
1107      * <p> Data in {@code TIFF_SLONG}, {@code TIFF_LONG},
1108      * {@code TIFF_FLOAT}, {@code TIFF_DOUBLE} or
1109      * {@code TIFF_IFD_POINTER} format are simply cast to
1110      * {@code int} and may suffer from truncation.
1111      *
1112      * <p> Data in {@code TIFF_SRATIONAL} or
1113      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1114      * numerator into the denominator using double-precision
1115      * arithmetic and then casting to {@code int}.  Loss of
1116      * precision and truncation may occur.
1117      *
1118      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1119      * the {@code Double.parseDouble} method, with the result
1120      * case to {@code int}.
1121      *
1122      * @param index The index of the data.
1123      * @return The data at the given index as an {@code int}.
1124      */
1125     public int getAsInt(int index) {
1126         switch (type) {
1127         case TIFFTag.TIFF_BYTE:
1128         case TIFFTag.TIFF_UNDEFINED:
1129             return ((byte[])data)[index] & 0xff;
1130         case TIFFTag.TIFF_SBYTE:
1131             return ((byte[])data)[index];
1132         case TIFFTag.TIFF_SHORT:
1133             return ((char[])data)[index] & 0xffff;
1134         case TIFFTag.TIFF_SSHORT:
1135             return ((short[])data)[index];
1136         case TIFFTag.TIFF_SLONG:
1137             return ((int[])data)[index];
1138         case TIFFTag.TIFF_LONG:
1139         case TIFFTag.TIFF_IFD_POINTER:
1140             return (int)((long[])data)[index];
1141         case TIFFTag.TIFF_FLOAT:
1142             return (int)((float[])data)[index];
1143         case TIFFTag.TIFF_DOUBLE:
1144             return (int)((double[])data)[index];
1145         case TIFFTag.TIFF_SRATIONAL:
1146             int[] ivalue = getAsSRational(index);
1147             return (int)((double)ivalue[0]/ivalue[1]);
1148         case TIFFTag.TIFF_RATIONAL:
1149             long[] lvalue = getAsRational(index);
1150             return (int)((double)lvalue[0]/lvalue[1]);
1151         case TIFFTag.TIFF_ASCII:
1152              String s = ((String[])data)[index];
1153              return (int)Double.parseDouble(s);
1154         default:
1155             throw new ClassCastException(); // should never happen
1156         }
1157     }
1158 
1159     /**
1160      * Returns data in any format as a {@code long}.
1161      *
1162      * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data
1163      * are treated as unsigned; that is, no sign extension will take
1164      * place and the returned value will be in the range [0, 255].
1165      * {@code TIFF_SBYTE} data will be returned in the range
1166      * [-128, 127].
1167      *
1168      * <p> Data in {@code TIFF_FLOAT} and {@code TIFF_DOUBLE} are
1169      * simply cast to {@code long} and may suffer from truncation.
1170      *
1171      * <p> Data in {@code TIFF_SRATIONAL} or
1172      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1173      * numerator into the denominator using double-precision
1174      * arithmetic and then casting to {@code long}.  Loss of
1175      * precision and truncation may occur.
1176      *
1177      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1178      * the {@code Double.parseDouble} method, with the result
1179      * cast to {@code long}.
1180      *
1181      * @param index The index of the data.
1182      * @return The data at the given index as a {@code long}.
1183      */
1184     public long getAsLong(int index) {
1185         switch (type) {
1186         case TIFFTag.TIFF_BYTE:
1187         case TIFFTag.TIFF_UNDEFINED:
1188             return ((byte[])data)[index] & 0xff;
1189         case TIFFTag.TIFF_SBYTE:
1190             return ((byte[])data)[index];
1191         case TIFFTag.TIFF_SHORT:
1192             return ((char[])data)[index] & 0xffff;
1193         case TIFFTag.TIFF_SSHORT:
1194             return ((short[])data)[index];
1195         case TIFFTag.TIFF_SLONG:
1196             return ((int[])data)[index];
1197         case TIFFTag.TIFF_LONG:
1198         case TIFFTag.TIFF_IFD_POINTER:
1199             return ((long[])data)[index];
1200         case TIFFTag.TIFF_FLOAT:
1201             return (long)((float[])data)[index];
1202         case TIFFTag.TIFF_DOUBLE:
1203             return (long)((double[])data)[index];
1204         case TIFFTag.TIFF_SRATIONAL:
1205             int[] ivalue = getAsSRational(index);
1206             return (long)((double)ivalue[0]/ivalue[1]);
1207         case TIFFTag.TIFF_RATIONAL:
1208             long[] lvalue = getAsRational(index);
1209             return (long)((double)lvalue[0]/lvalue[1]);
1210         case TIFFTag.TIFF_ASCII:
1211              String s = ((String[])data)[index];
1212              return (long)Double.parseDouble(s);
1213         default:
1214             throw new ClassCastException(); // should never happen
1215         }
1216     }
1217 
1218     /**
1219      * Returns data in any format as a {@code float}.
1220      *
1221      * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data
1222      * are treated as unsigned; that is, no sign extension will take
1223      * place and the returned value will be in the range [0, 255].
1224      * {@code TIFF_SBYTE} data will be returned in the range
1225      * [-128, 127].
1226      *
1227      * <p> Data in {@code TIFF_SLONG}, {@code TIFF_LONG},
1228      * {@code TIFF_DOUBLE}, or {@code TIFF_IFD_POINTER} format are
1229      * simply cast to {@code float} and may suffer from
1230      * truncation.
1231      *
1232      * <p> Data in {@code TIFF_SRATIONAL} or
1233      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1234      * numerator into the denominator using double-precision
1235      * arithmetic and then casting to {@code float}.
1236      *
1237      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1238      * the {@code Double.parseDouble} method, with the result
1239      * cast to {@code float}.
1240      *
1241      * @param index The index of the data.
1242      * @return The data at the given index as a {@code float}.
1243      */
1244     public float getAsFloat(int index) {
1245         switch (type) {
1246         case TIFFTag.TIFF_BYTE:
1247         case TIFFTag.TIFF_UNDEFINED:
1248             return ((byte[])data)[index] & 0xff;
1249         case TIFFTag.TIFF_SBYTE:
1250             return ((byte[])data)[index];
1251         case TIFFTag.TIFF_SHORT:
1252             return ((char[])data)[index] & 0xffff;
1253         case TIFFTag.TIFF_SSHORT:
1254             return ((short[])data)[index];
1255         case TIFFTag.TIFF_SLONG:
1256             return ((int[])data)[index];
1257         case TIFFTag.TIFF_LONG:
1258         case TIFFTag.TIFF_IFD_POINTER:
1259             return ((long[])data)[index];
1260         case TIFFTag.TIFF_FLOAT:
1261             return ((float[])data)[index];
1262         case TIFFTag.TIFF_DOUBLE:
1263             return (float)((double[])data)[index];
1264         case TIFFTag.TIFF_SRATIONAL:
1265             int[] ivalue = getAsSRational(index);
1266             return (float)((double)ivalue[0]/ivalue[1]);
1267         case TIFFTag.TIFF_RATIONAL:
1268             long[] lvalue = getAsRational(index);
1269             return (float)((double)lvalue[0]/lvalue[1]);
1270         case TIFFTag.TIFF_ASCII:
1271              String s = ((String[])data)[index];
1272              return (float)Double.parseDouble(s);
1273         default:
1274             throw new ClassCastException(); // should never happen
1275         }
1276     }
1277 
1278     /**
1279      * Returns data in any format as a {@code double}.
1280      *
1281      * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data
1282      * are treated as unsigned; that is, no sign extension will take
1283      * place and the returned value will be in the range [0, 255].
1284      * {@code TIFF_SBYTE} data will be returned in the range
1285      * [-128, 127].
1286      *
1287      * <p> Data in {@code TIFF_SRATIONAL} or
1288      * {@code TIFF_RATIONAL} format are evaluated by dividing the
1289      * numerator into the denominator using double-precision
1290      * arithmetic.
1291      *
1292      * <p> Data in {@code TIFF_ASCII} format will be parsed as by
1293      * the {@code Double.parseDouble} method.
1294      *
1295      * @param index The index of the data.
1296      * @return The data at the given index as a {@code double}.
1297      */
1298     public double getAsDouble(int index) {
1299         switch (type) {
1300         case TIFFTag.TIFF_BYTE:
1301         case TIFFTag.TIFF_UNDEFINED:
1302             return ((byte[])data)[index] & 0xff;
1303         case TIFFTag.TIFF_SBYTE:
1304             return ((byte[])data)[index];
1305         case TIFFTag.TIFF_SHORT:
1306             return ((char[])data)[index] & 0xffff;
1307         case TIFFTag.TIFF_SSHORT:
1308             return ((short[])data)[index];
1309         case TIFFTag.TIFF_SLONG:
1310             return ((int[])data)[index];
1311         case TIFFTag.TIFF_LONG:
1312         case TIFFTag.TIFF_IFD_POINTER:
1313             return ((long[])data)[index];
1314         case TIFFTag.TIFF_FLOAT:
1315             return ((float[])data)[index];
1316         case TIFFTag.TIFF_DOUBLE:
1317             return ((double[])data)[index];
1318         case TIFFTag.TIFF_SRATIONAL:
1319             int[] ivalue = getAsSRational(index);
1320             return (double)ivalue[0]/ivalue[1];
1321         case TIFFTag.TIFF_RATIONAL:
1322             long[] lvalue = getAsRational(index);
1323             return (double)lvalue[0]/lvalue[1];
1324         case TIFFTag.TIFF_ASCII:
1325              String s = ((String[])data)[index];
1326              return Double.parseDouble(s);
1327         default:
1328             throw new ClassCastException(); // should never happen
1329         }
1330     }
1331 
1332     /**
1333      * Returns a {@code TIFFTag.TIFF_ASCII} value as a
1334      * {@code String}.
1335      *
1336      * @throws ClassCastException if the field is not of type
1337      * {@code TIFF_ASCII}.
1338      *
1339      * @param index The index of the data.
1340      * @return The data at the given index as a {@code String}.
1341      */
1342     public String getAsString(int index) {
1343         return ((String[])data)[index];
1344     }
1345 
1346     /**
1347      * Returns a {@code TIFFTag.TIFF_SRATIONAL} data item as a
1348      * two-element array of {@code int}s.
1349      *
1350      * @param index The index of the data.
1351      * @return The data at the given index as a signed rational.
1352      * @throws ClassCastException if the field is not of type
1353      * {@code TIFF_SRATIONAL}.
1354      */
1355     public int[] getAsSRational(int index) {
1356         return ((int[][])data)[index];
1357     }
1358 
1359     /**
1360      * Returns a TIFFTag.TIFF_RATIONAL data item as a two-element array
1361      * of ints.
1362      *
1363      * @param index The index of the data.
1364      * @return The data at the given index as an unsigned rational.
1365      * @throws ClassCastException if the field is not of type
1366      * {@code TIFF_RATIONAL}.
1367      */
1368     public long[] getAsRational(int index) {
1369         return ((long[][])data)[index];
1370     }
1371 
1372 
1373     /**
1374      * Returns a {@code String} containing a human-readable
1375      * version of the data item.  Data of type
1376      * {@code TIFFTag.TIFF_RATIONAL} or {@code TIFF_SRATIONAL} are
1377      * represented as a pair of integers separated by a
1378      * {@code '/'} character.  If the numerator of a
1379      * {@code TIFFTag.TIFF_RATIONAL} or {@code TIFF_SRATIONAL} is an integral
1380      * multiple of the denominator, then the value is represented as
1381      * {@code "q/1"} where {@code q} is the quotient of the numerator and
1382      * denominator.
1383      *
1384      * @param index The index of the data.
1385      * @return The data at the given index as a {@code String}.
1386      * @throws ClassCastException if the field is not of one of the
1387      * legal field types.
1388      */
1389     public String getValueAsString(int index) {
1390         switch (type) {
1391         case TIFFTag.TIFF_ASCII:
1392             return ((String[])data)[index];
1393         case TIFFTag.TIFF_BYTE:
1394         case TIFFTag.TIFF_UNDEFINED:
1395             return Integer.toString(((byte[])data)[index] & 0xff);
1396         case TIFFTag.TIFF_SBYTE:
1397             return Integer.toString(((byte[])data)[index]);
1398         case TIFFTag.TIFF_SHORT:
1399             return Integer.toString(((char[])data)[index] & 0xffff);
1400         case TIFFTag.TIFF_SSHORT:
1401             return Integer.toString(((short[])data)[index]);
1402         case TIFFTag.TIFF_SLONG:
1403             return Integer.toString(((int[])data)[index]);
1404         case TIFFTag.TIFF_LONG:
1405         case TIFFTag.TIFF_IFD_POINTER:
1406             return Long.toString(((long[])data)[index]);
1407         case TIFFTag.TIFF_FLOAT:
1408             return Float.toString(((float[])data)[index]);
1409         case TIFFTag.TIFF_DOUBLE:
1410             return Double.toString(((double[])data)[index]);
1411         case TIFFTag.TIFF_SRATIONAL:
1412             int[] ivalue = getAsSRational(index);
1413             String srationalString;
1414             if(ivalue[1] != 0 && ivalue[0] % ivalue[1] == 0) {
1415                 // If the denominator is a non-zero integral divisor
1416                 // of the numerator then convert the fraction to be
1417                 // with respect to a unity denominator.
1418                 srationalString =
1419                     Integer.toString(ivalue[0] / ivalue[1]) + "/1";
1420             } else {
1421                 // Use the values directly.
1422                 srationalString =
1423                     Integer.toString(ivalue[0]) +
1424                     "/" +
1425                     Integer.toString(ivalue[1]);
1426             }
1427             return srationalString;
1428         case TIFFTag.TIFF_RATIONAL:
1429             long[] lvalue = getAsRational(index);
1430             String rationalString;
1431             if(lvalue[1] != 0L && lvalue[0] % lvalue[1] == 0) {
1432                 // If the denominator is a non-zero integral divisor
1433                 // of the numerator then convert the fraction to be
1434                 // with respect to a unity denominator.
1435                 rationalString =
1436                     Long.toString(lvalue[0] / lvalue[1]) + "/1";
1437             } else {
1438                 // Use the values directly.
1439                 rationalString =
1440                     Long.toString(lvalue[0]) +
1441                     "/" +
1442                     Long.toString(lvalue[1]);
1443             }
1444             return rationalString;
1445         default:
1446             throw new ClassCastException(); // should never happen
1447         }
1448     }
1449 
1450     /**
1451      * Returns whether the field has a {@code TIFFDirectory}.
1452      *
1453      * @return true if and only if getDirectory() returns non-null.
1454      */
1455     public boolean hasDirectory() {
1456         return getDirectory() != null;
1457     }
1458 
1459     /**
1460      * Returns the associated {@code TIFFDirectory}, if available. If no
1461      * directory is set, then {@code null} will be returned.
1462      *
1463      * @return the TIFFDirectory instance or null.
1464      */
1465     public TIFFDirectory getDirectory() {
1466         return dir;
1467     }
1468 
1469     /**
1470      * Clones the field and all the information contained therein.
1471      *
1472      * @return A clone of this {@code TIFFField}.
1473      * @throws CloneNotSupportedException if the instance cannot be cloned.
1474      */
1475     @Override
1476     public TIFFField clone() throws CloneNotSupportedException {
1477         TIFFField field = (TIFFField)super.clone();
1478 
1479         Object fieldData;
1480         switch (type) {
1481         case TIFFTag.TIFF_BYTE:
1482         case TIFFTag.TIFF_UNDEFINED:
1483         case TIFFTag.TIFF_SBYTE:
1484             fieldData = ((byte[])data).clone();
1485             break;
1486         case TIFFTag.TIFF_SHORT:
1487             fieldData = ((char[])data).clone();
1488             break;
1489         case TIFFTag.TIFF_SSHORT:
1490             fieldData = ((short[])data).clone();
1491             break;
1492         case TIFFTag.TIFF_SLONG:
1493             fieldData = ((int[])data).clone();
1494             break;
1495         case TIFFTag.TIFF_LONG:
1496         case TIFFTag.TIFF_IFD_POINTER:
1497             fieldData = ((long[])data).clone();
1498             break;
1499         case TIFFTag.TIFF_FLOAT:
1500             fieldData = ((float[])data).clone();
1501             break;
1502         case TIFFTag.TIFF_DOUBLE:
1503             fieldData = ((double[])data).clone();
1504             break;
1505         case TIFFTag.TIFF_SRATIONAL:
1506             fieldData = ((int[][])data).clone();
1507             break;
1508         case TIFFTag.TIFF_RATIONAL:
1509             fieldData = ((long[][])data).clone();
1510             break;
1511         case TIFFTag.TIFF_ASCII:
1512             fieldData = ((String[])data).clone();
1513             break;
1514         default:
1515             throw new ClassCastException(); // should never happen
1516         }
1517 
1518         field.tag = tag;
1519         field.tagNumber = tagNumber;
1520         field.type = type;
1521         field.count = count;
1522         field.data = fieldData;
1523         field.dir = dir != null ? dir.clone() : null;
1524 
1525         return field;
1526     }
1527 }