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 class TIFFField implements Cloneable { 265 266 private static final String[] typeNames = { 267 null, 268 "Byte", "Ascii", "Short", "Long", "Rational", 269 "SByte", "Undefined", "SShort", "SLong", "SRational", 270 "Float", "Double", "IFDPointer" 271 }; 272 273 private static final boolean[] isIntegral = { 274 false, 275 true, false, true, true, false, 276 true, true, true, true, false, 277 false, false, false 278 }; 279 280 /** The tag. */ 281 private TIFFTag tag; 282 283 /** The tag number. */ 284 private int tagNumber; 285 286 /** The tag type. */ 287 private int type; 288 289 /** The number of data items present in the field. */ 290 private int count; 291 292 /** The field data. */ 293 private Object data; 294 295 /** The IFD contents if available. This will usually be a TIFFIFD. */ 296 private TIFFDirectory dir; 297 298 /** The default constructor. */ 299 private TIFFField() {} 300 301 private static String getAttribute(Node node, String attrName) { 302 NamedNodeMap attrs = node.getAttributes(); 303 return attrs.getNamedItem(attrName).getNodeValue(); 304 } 305 306 private static void initData(Node node, 307 int[] otype, int[] ocount, Object[] odata) { 308 int type; 309 int count; 310 Object data = null; 311 312 String typeName = node.getNodeName(); 313 typeName = typeName.substring(4); 314 typeName = typeName.substring(0, typeName.length() - 1); 315 type = TIFFField.getTypeByName(typeName); 316 if (type == -1) { 317 throw new IllegalArgumentException("typeName = " + typeName); 318 } 319 320 Node child = node.getFirstChild(); 321 322 count = 0; 323 while (child != null) { 324 String childTypeName = child.getNodeName().substring(4); 325 if (!typeName.equals(childTypeName)) { 326 // warning 327 } 328 329 ++count; 330 child = child.getNextSibling(); 331 } 332 333 if (count > 0) { 334 data = createArrayForType(type, count); 335 child = node.getFirstChild(); 336 int idx = 0; 337 while (child != null) { 338 String value = getAttribute(child, "value"); 339 340 String numerator, denominator; 341 int slashPos; 342 343 switch (type) { 344 case TIFFTag.TIFF_ASCII: 345 ((String[])data)[idx] = value; 346 break; 347 case TIFFTag.TIFF_BYTE: 348 case TIFFTag.TIFF_SBYTE: 349 ((byte[])data)[idx] = 350 (byte)Integer.parseInt(value); 351 break; 352 case TIFFTag.TIFF_SHORT: 353 ((char[])data)[idx] = 354 (char)Integer.parseInt(value); 355 break; 356 case TIFFTag.TIFF_SSHORT: 357 ((short[])data)[idx] = 358 (short)Integer.parseInt(value); 359 break; 360 case TIFFTag.TIFF_SLONG: 361 ((int[])data)[idx] = 362 Integer.parseInt(value); 363 break; 364 case TIFFTag.TIFF_LONG: 365 case TIFFTag.TIFF_IFD_POINTER: 366 ((long[])data)[idx] = 367 Long.parseLong(value); 368 break; 369 case TIFFTag.TIFF_FLOAT: 370 ((float[])data)[idx] = 371 Float.parseFloat(value); 372 break; 373 case TIFFTag.TIFF_DOUBLE: 374 ((double[])data)[idx] = 375 Double.parseDouble(value); 376 break; 377 case TIFFTag.TIFF_SRATIONAL: 378 slashPos = value.indexOf("/"); 379 numerator = value.substring(0, slashPos); 380 denominator = value.substring(slashPos + 1); 381 382 ((int[][])data)[idx] = new int[2]; 383 ((int[][])data)[idx][0] = 384 Integer.parseInt(numerator); 385 ((int[][])data)[idx][1] = 386 Integer.parseInt(denominator); 387 break; 388 case TIFFTag.TIFF_RATIONAL: 389 slashPos = value.indexOf("/"); 390 numerator = value.substring(0, slashPos); 391 denominator = value.substring(slashPos + 1); 392 393 ((long[][])data)[idx] = new long[2]; 394 ((long[][])data)[idx][0] = 395 Long.parseLong(numerator); 396 ((long[][])data)[idx][1] = 397 Long.parseLong(denominator); 398 break; 399 default: 400 // error 401 } 402 403 idx++; 404 child = child.getNextSibling(); 405 } 406 } 407 408 otype[0] = type; 409 ocount[0] = count; 410 odata[0] = data; 411 } 412 413 /** 414 * Creates a {@code TIFFField} from a TIFF native image 415 * metadata node. If the value of the <tt>"number"</tt> attribute 416 * of the node is not found in {@code tagSet} then a new 417 * {@code TIFFTag} with name {@code TIFFTag.UNKNOWN_TAG_NAME} 418 * will be created and assigned to the field. 419 * 420 * @param tagSet The {@code TIFFTagSet} to which the 421 * {@code TIFFTag} of the field belongs. 422 * @param node A native TIFF image metadata {@code TIFFField} node. 423 * @throws NullPointerException if {@code node} is 424 * {@code null}. 425 * @throws IllegalArgumentException if the name of the node is not 426 * {@code "TIFFField"}. 427 * @throws NullPointerException if the node does not contain any data. 428 * @throws IllegalArgumentException if the combination of node attributes 429 * and data is not legal per the {@link #TIFFField(TIFFTag,int,int,Object)} 430 * constructor specification. 431 * @return A new {@code TIFFField}. 432 */ 433 public static TIFFField createFromMetadataNode(TIFFTagSet tagSet, 434 Node node) { 435 if (node == null) { 436 throw new NullPointerException("node == null!"); 437 } 438 String name = node.getNodeName(); 439 if (!name.equals("TIFFField")) { 440 throw new IllegalArgumentException("!name.equals(\"TIFFField\")"); 441 } 442 443 int tagNumber = Integer.parseInt(getAttribute(node, "number")); 444 TIFFTag tag = null; 445 if (tagSet != null) { 446 tag = tagSet.getTag(tagNumber); 447 } 448 449 int type = TIFFTag.TIFF_UNDEFINED; 450 int count = 0; 451 Object data = null; 452 453 Node child = node.getFirstChild(); 454 if (child != null) { 455 String typeName = child.getNodeName(); 456 if (typeName.equals("TIFFUndefined")) { 457 String values = getAttribute(child, "value"); 458 StringTokenizer st = new StringTokenizer(values, ","); 459 count = st.countTokens(); 460 461 byte[] bdata = new byte[count]; 462 for (int i = 0; i < count; i++) { 463 bdata[i] = (byte)Integer.parseInt(st.nextToken()); 464 } 465 466 type = TIFFTag.TIFF_UNDEFINED; 467 data = bdata; 468 } else { 469 int[] otype = new int[1]; 470 int[] ocount = new int[1]; 471 Object[] odata = new Object[1]; 472 473 initData(node.getFirstChild(), otype, ocount, odata); 474 type = otype[0]; 475 count = ocount[0]; 476 data = odata[0]; 477 } 478 } else if (tag != null) { 479 int t = TIFFTag.MAX_DATATYPE; 480 while(t >= TIFFTag.MIN_DATATYPE && !tag.isDataTypeOK(t)) { 481 t--; 482 } 483 type = t; 484 } 485 486 if (tag == null) { 487 tag = new TIFFTag(TIFFTag.UNKNOWN_TAG_NAME, tagNumber, 1 << type); 488 } 489 490 return new TIFFField(tag, type, count, data); 491 } 492 493 /** 494 * Constructs a {@code TIFFField} with arbitrary data. The 495 * {@code type} parameter must be a value for which 496 * {@link TIFFTag#isDataTypeOK tag.isDataTypeOK()} 497 * returns {@code true}. The {@code data} parameter must 498 * be an array of a Java type appropriate for the type of the TIFF 499 * field. 500 * 501 * <p>Note that the value (data) of the {@code TIFFField} 502 * will always be the actual field value regardless of the number of 503 * bytes required for that value. This is the case despite the fact 504 * that the TIFF <i>IFD Entry</i> corresponding to the field may 505 * actually contain the offset to the value of the field rather than 506 * the value itself (the latter occurring if and only if the 507 * value fits into 4 bytes). In other words, the value of the 508 * field will already have been read from the TIFF stream. (An exception 509 * to this case may occur when the field represents the contents of a 510 * non-baseline IFD. In that case the data will be a {@code long[]} 511 * containing the offset to the IFD and the {@code TIFFDirectory} 512 * returned by {@link #getDirectory()} will be its contents.) 513 * 514 * @param tag The tag to associated with this field. 515 * @param type One of the {@code TIFFTag.TIFF_*} constants 516 * indicating the data type of the field as written to the TIFF stream. 517 * @param count The number of data values. 518 * @param data The actual data content of the field. 519 * 520 * @throws NullPointerException if {@code tag == null}. 521 * @throws IllegalArgumentException if {@code type} is not 522 * one of the {@code TIFFTag.TIFF_*} data type constants. 523 * @throws IllegalArgumentException if {@code type} is an unacceptable 524 * data type for the supplied {@code TIFFTag}. 525 * @throws IllegalArgumentException if {@code count < 0}. 526 * @throws IllegalArgumentException if {@code count < 1} 527 * and {@code type} is {@code TIFF_RATIONAL} or 528 * {@code TIFF_SRATIONAL}. 529 * @throws IllegalArgumentException if {@code count ≠ 1} 530 * and {@code type} is {@code TIFF_IFD_POINTER}. 531 * @throws NullPointerException if {@code data == null}. 532 * @throws IllegalArgumentException if {@code data} is an instance of 533 * a class incompatible with the specified type. 534 * @throws IllegalArgumentException if the size of the data array is wrong. 535 */ 536 public TIFFField(TIFFTag tag, int type, int count, Object data) { 537 if(tag == null) { 538 throw new NullPointerException("tag == null!"); 539 } else if(type < TIFFTag.MIN_DATATYPE || type > TIFFTag.MAX_DATATYPE) { 540 throw new IllegalArgumentException("Unknown data type "+type); 541 } else if(!tag.isDataTypeOK(type)) { 542 throw new IllegalArgumentException("Illegal data type " + type 543 + " for " + tag.getName() + " tag"); 544 } else if(count < 0) { 545 throw new IllegalArgumentException("count < 0!"); 546 } else if((type == TIFFTag.TIFF_RATIONAL 547 || type == TIFFTag.TIFF_SRATIONAL) 548 && count < 1) { 549 throw new IllegalArgumentException 550 ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1"); 551 } else if (type == TIFFTag.TIFF_IFD_POINTER && count != 1) { 552 throw new IllegalArgumentException 553 ("Type is TIFF_IFD_POINTER count != 1"); 554 } else if(data == null) { 555 throw new NullPointerException("data == null!"); 556 } 557 558 boolean isDataArrayCorrect = false; 559 560 switch (type) { 561 case TIFFTag.TIFF_BYTE: 562 case TIFFTag.TIFF_SBYTE: 563 case TIFFTag.TIFF_UNDEFINED: 564 isDataArrayCorrect = data instanceof byte[] 565 && ((byte[])data).length == count; 566 break; 567 case TIFFTag.TIFF_ASCII: 568 isDataArrayCorrect = data instanceof String[] 569 && ((String[])data).length == count; 570 break; 571 case TIFFTag.TIFF_SHORT: 572 isDataArrayCorrect = data instanceof char[] 573 && ((char[])data).length == count; 574 break; 575 case TIFFTag.TIFF_LONG: 576 isDataArrayCorrect = data instanceof long[] 577 && ((long[])data).length == count; 578 break; 579 case TIFFTag.TIFF_IFD_POINTER: 580 isDataArrayCorrect = data instanceof long[] 581 && ((long[])data).length == 1; 582 break; 583 case TIFFTag.TIFF_RATIONAL: 584 isDataArrayCorrect = data instanceof long[][] 585 && ((long[][])data).length == count 586 && ((long[][])data)[0].length == 2; 587 break; 588 case TIFFTag.TIFF_SSHORT: 589 isDataArrayCorrect = data instanceof short[] 590 && ((short[])data).length == count; 591 break; 592 case TIFFTag.TIFF_SLONG: 593 isDataArrayCorrect = data instanceof int[] 594 && ((int[])data).length == count; 595 break; 596 case TIFFTag.TIFF_SRATIONAL: 597 isDataArrayCorrect = data instanceof int[][] 598 && ((int[][])data).length == count 599 && ((int[][])data)[0].length == 2; 600 break; 601 case TIFFTag.TIFF_FLOAT: 602 isDataArrayCorrect = data instanceof float[] 603 && ((float[])data).length == count; 604 break; 605 case TIFFTag.TIFF_DOUBLE: 606 isDataArrayCorrect = data instanceof double[] 607 && ((double[])data).length == count; 608 break; 609 default: 610 throw new IllegalArgumentException("Unknown data type "+type); 611 } 612 613 if (!isDataArrayCorrect) { 614 throw new IllegalArgumentException 615 ("Illegal class or length for data array"); 616 } 617 618 this.tag = tag; 619 this.tagNumber = tag.getNumber(); 620 this.type = type; 621 this.count = count; 622 this.data = data; 623 } 624 625 /** 626 * Constructs a data array using {@link #createArrayForType 627 * createArrayForType()} and invokes 628 * {@link #TIFFField(TIFFTag,int,int,Object)} with the supplied 629 * parameters and the created array. 630 * 631 * @param tag The tag to associated with this field. 632 * @param type One of the {@code TIFFTag.TIFF_*} constants 633 * indicating the data type of the field as written to the TIFF stream. 634 * @param count The number of data values. 635 * @throws NullPointerException if {@code tag == null}. 636 * @throws IllegalArgumentException if {@code type} is not 637 * one of the {@code TIFFTag.TIFF_*} data type constants. 638 * @throws IllegalArgumentException if {@code type} is an unacceptable 639 * data type for the supplied {@code TIFFTag}. 640 * @throws IllegalArgumentException if {@code count < 0}. 641 * @see #TIFFField(TIFFTag,int,int,Object) 642 */ 643 public TIFFField(TIFFTag tag, int type, int count) { 644 this(tag, type, count, createArrayForType(type, count)); 645 } 646 647 /** 648 * Constructs a {@code TIFFField} with a single non-negative integral 649 * value. 650 * The field will have type 651 * {@link TIFFTag#TIFF_SHORT TIFF_SHORT} if 652 * {@code val < 65536} and type 653 * {@link TIFFTag#TIFF_LONG TIFF_LONG} otherwise. The count 654 * of the field will be unity. 655 * 656 * @param tag The tag to associate with this field. 657 * @param value The value to associate with this field. 658 * @throws NullPointerException if {@code tag == null}. 659 * @throws IllegalArgumentException if the derived type is unacceptable 660 * for the supplied {@code TIFFTag}. 661 * @throws IllegalArgumentException if {@code value < 0}. 662 */ 663 public TIFFField(TIFFTag tag, int value) { 664 if(tag == null) { 665 throw new NullPointerException("tag == null!"); 666 } 667 if (value < 0) { 668 throw new IllegalArgumentException("value < 0!"); 669 } 670 671 this.tag = tag; 672 this.tagNumber = tag.getNumber(); 673 this.count = 1; 674 675 if (value < 65536) { 676 if (!tag.isDataTypeOK(TIFFTag.TIFF_SHORT)) { 677 throw new IllegalArgumentException("Illegal data type " 678 + TIFFTag.TIFF_SHORT + " for " + tag.getName() + " tag"); 679 } 680 this.type = TIFFTag.TIFF_SHORT; 681 char[] cdata = new char[1]; 682 cdata[0] = (char)value; 683 this.data = cdata; 684 } else { 685 if (!tag.isDataTypeOK(TIFFTag.TIFF_LONG)) { 686 throw new IllegalArgumentException("Illegal data type " 687 + TIFFTag.TIFF_LONG + " for " + tag.getName() + " tag"); 688 } 689 this.type = TIFFTag.TIFF_LONG; 690 long[] ldata = new long[1]; 691 ldata[0] = value; 692 this.data = ldata; 693 } 694 } 695 696 /** 697 * Constructs a {@code TIFFField} with an IFD offset and contents. 698 * The offset will be stored as the data of this field as 699 * {@code long[] {offset}}. The directory will not be cloned. The count 700 * of the field will be unity. 701 * 702 * @param tag The tag to associated with this field. 703 * @param type One of the constants {@code TIFFTag.TIFF_LONG} or 704 * {@code TIFFTag.TIFF_IFD_POINTER}. 705 * @param offset The IFD offset. 706 * @param dir The directory. 707 * 708 * @throws NullPointerException if {@code tag == null}. 709 * @throws IllegalArgumentException if {@code type} is neither 710 * {@code TIFFTag.TIFF_LONG} nor {@code TIFFTag.TIFF_IFD_POINTER}. 711 * @throws IllegalArgumentException if {@code type} is an unacceptable 712 * data type for the supplied {@code TIFFTag}. 713 * @throws IllegalArgumentException if {@code offset} is non-positive. 714 * @throws NullPointerException if {@code dir == null}. 715 * 716 * @see #TIFFField(TIFFTag,int,int,Object) 717 */ 718 public TIFFField(TIFFTag tag, int type, long offset, TIFFDirectory dir) { 719 this(tag, type, 1, new long[] {offset}); 720 if (type != TIFFTag.TIFF_LONG && type != TIFFTag.TIFF_IFD_POINTER) { 721 throw new IllegalArgumentException("type " + type 722 + " is neither TIFFTag.TIFF_LONG nor TIFFTag.TIFF_IFD_POINTER"); 723 } else if (offset <= 0) { 724 throw new IllegalArgumentException("offset " + offset 725 + " is non-positive"); 726 } else if (dir == null) { 727 throw new NullPointerException("dir == null"); 728 } 729 this.dir = dir; 730 } 731 732 /** 733 * Retrieves the tag associated with this field. 734 * 735 * @return The associated {@code TIFFTag}. 736 */ 737 public TIFFTag getTag() { 738 return tag; 739 } 740 741 /** 742 * Retrieves the tag number in the range {@code [0, 65535]}. 743 * 744 * @return The tag number. 745 */ 746 public int getTagNumber() { 747 return tagNumber; 748 } 749 750 /** 751 * Returns the type of the data stored in the field. For a TIFF 6.0 752 * stream, the value will equal one of the {@code TIFFTag.TIFF_*} 753 * constants. For future revisions of TIFF, higher values are possible. 754 * 755 * @return The data type of the field value. 756 */ 757 public int getType() { 758 return type; 759 } 760 761 /** 762 * Returns the name of the supplied data type constant. 763 * 764 * @param dataType One of the {@code TIFFTag.TIFF_*} constants 765 * indicating the data type of the field as written to the TIFF stream. 766 * @return The type name corresponding to the supplied type constant. 767 * @throws IllegalArgumentException if {@code dataType} is not 768 * one of the {@code TIFFTag.TIFF_*} data type constants. 769 */ 770 public static String getTypeName(int dataType) { 771 if (dataType < TIFFTag.MIN_DATATYPE || 772 dataType > TIFFTag.MAX_DATATYPE) { 773 throw new IllegalArgumentException("Unknown data type "+dataType); 774 } 775 776 return typeNames[dataType]; 777 } 778 779 /** 780 * Returns the data type constant corresponding to the supplied data 781 * type name. If the name is unknown {@code -1} will be returned. 782 * 783 * @param typeName The type name. 784 * @return One of the {@code TIFFTag.TIFF_*} constants or 785 * {@code -1} if the name is not recognized. 786 */ 787 public static int getTypeByName(String typeName) { 788 for (int i = TIFFTag.MIN_DATATYPE; i <= TIFFTag.MAX_DATATYPE; i++) { 789 if (typeName.equals(typeNames[i])) { 790 return i; 791 } 792 } 793 794 return -1; 795 } 796 797 /** 798 * Creates an array appropriate for the indicated data type. 799 * 800 * @param dataType One of the {@code TIFFTag.TIFF_*} data type 801 * constants. 802 * @param count The number of values in the array. 803 * @return An array appropriate for the specified data type. 804 * 805 * @throws IllegalArgumentException if {@code dataType} is not 806 * one of the {@code TIFFTag.TIFF_*} data type constants. 807 * @throws IllegalArgumentException if {@code count < 0}. 808 */ 809 public static Object createArrayForType(int dataType, int count) { 810 if(count < 0) { 811 throw new IllegalArgumentException("count < 0!"); 812 } 813 switch (dataType) { 814 case TIFFTag.TIFF_BYTE: 815 case TIFFTag.TIFF_SBYTE: 816 case TIFFTag.TIFF_UNDEFINED: 817 return new byte[count]; 818 case TIFFTag.TIFF_ASCII: 819 return new String[count]; 820 case TIFFTag.TIFF_SHORT: 821 return new char[count]; 822 case TIFFTag.TIFF_LONG: 823 case TIFFTag.TIFF_IFD_POINTER: 824 return new long[count]; 825 case TIFFTag.TIFF_RATIONAL: 826 return new long[count][2]; 827 case TIFFTag.TIFF_SSHORT: 828 return new short[count]; 829 case TIFFTag.TIFF_SLONG: 830 return new int[count]; 831 case TIFFTag.TIFF_SRATIONAL: 832 return new int[count][2]; 833 case TIFFTag.TIFF_FLOAT: 834 return new float[count]; 835 case TIFFTag.TIFF_DOUBLE: 836 return new double[count]; 837 default: 838 throw new IllegalArgumentException("Unknown data type "+dataType); 839 } 840 } 841 842 /** 843 * Returns the {@code TIFFField} as a node named either 844 * <tt>"TIFFField"</tt> or <tt>"TIFFIFD"</tt> as described in the 845 * TIFF native image metadata specification. The node will be named 846 * <tt>"TIFFIFD"</tt> if and only if {@link #hasDirectory()} returns 847 * {@code true} and the field's type is either {@link TIFFTag#TIFF_LONG} 848 * or {@link TIFFTag#TIFF_IFD_POINTER}. 849 * 850 * @return a {@code Node} named <tt>"TIFFField"</tt> or 851 * <tt>"TIFFIFD"</tt>. 852 */ 853 public Node getAsNativeNode() { 854 return new TIFFFieldNode(this); 855 } 856 857 /** 858 * Indicates whether the value associated with the field is of 859 * integral data type. 860 * 861 * @return Whether the field type is integral. 862 */ 863 public boolean isIntegral() { 864 return isIntegral[type]; 865 } 866 867 /** 868 * Returns the number of data items present in the field. For 869 * {@code TIFFTag.TIFF_ASCII} fields, the value returned is the 870 * number of {@code String}s, not the total length of the 871 * data as in the file representation. 872 * 873 * @return The number of data items present in the field. 874 */ 875 public int getCount() { 876 return count; 877 } 878 879 /** 880 * Returns a reference to the data object associated with the field. 881 * 882 * @return The data object of the field. 883 */ 884 public Object getData() { 885 return data; 886 } 887 888 /** 889 * Returns the data as an uninterpreted array of 890 * {@code byte}s. The type of the field must be one of 891 * {@code TIFFTag.TIFF_BYTE}, {@code TIFF_SBYTE}, or 892 * {@code TIFF_UNDEFINED}. 893 * 894 * <p> For data in {@code TIFFTag.TIFF_BYTE} format, the application 895 * must take care when promoting the data to longer integral types 896 * to avoid sign extension. 897 * 898 * @throws ClassCastException if the field is not of type 899 * {@code TIFF_BYTE}, {@code TIFF_SBYTE}, or 900 * {@code TIFF_UNDEFINED}. 901 * @return The data as an uninterpreted array of bytes. 902 */ 903 public byte[] getAsBytes() { 904 return (byte[])data; 905 } 906 907 /** 908 * Returns {@code TIFFTag.TIFF_SHORT} data as an array of 909 * {@code char}s (unsigned 16-bit integers). 910 * 911 * @throws ClassCastException if the field is not of type 912 * {@code TIFF_SHORT}. 913 * @return The data as an array of {@code char}s. 914 */ 915 public char[] getAsChars() { 916 return (char[])data; 917 } 918 919 /** 920 * Returns {@code TIFFTag.TIFF_SSHORT} data as an array of 921 * {@code short}s (signed 16-bit integers). 922 * 923 * @throws ClassCastException if the field is not of type 924 * {@code TIFF_SSHORT}. 925 * @return The data as an array of {@code short}s. 926 */ 927 public short[] getAsShorts() { 928 return (short[])data; 929 } 930 931 /** 932 * Returns {@code TIFFTag.TIFF_SLONG} data as an array of 933 * {@code int}s (signed 32-bit integers). 934 * 935 * @throws ClassCastException if the field is not of type 936 * {@code TIFF_SHORT}, {@code TIFF_SSHORT}, or 937 * {@code TIFF_SLONG}. 938 * @return The data as an array of {@code int}s. 939 */ 940 public int[] getAsInts() { 941 if (data instanceof int[]) { 942 return (int[])data; 943 } else if (data instanceof char[]){ 944 char[] cdata = (char[])data; 945 int[] idata = new int[cdata.length]; 946 for (int i = 0; i < cdata.length; i++) { 947 idata[i] = cdata[i] & 0xffff; 948 } 949 return idata; 950 } else if (data instanceof short[]){ 951 short[] sdata = (short[])data; 952 int[] idata = new int[sdata.length]; 953 for (int i = 0; i < sdata.length; i++) { 954 idata[i] = (int)sdata[i]; 955 } 956 return idata; 957 } else { 958 throw new ClassCastException("Data not char[], short[], or int[]!"); 959 } 960 } 961 962 /** 963 * Returns {@code TIFFTag.TIFF_LONG} or 964 * {@code TIFF_IFD_POINTER} data as an array of 965 * {@code long}s (signed 64-bit integers). 966 * 967 * @throws ClassCastException if the field is not of type 968 * {@code TIFF_LONG} or {@code TIFF_IFD_POINTER}. 969 * @return The data as an array of {@code long}s. 970 */ 971 public long[] getAsLongs() { 972 return (long[])data; 973 } 974 975 /** 976 * Returns {@code TIFFTag.TIFF_FLOAT} data as an array of 977 * {@code float}s (32-bit floating-point values). 978 * 979 * @throws ClassCastException if the field is not of type 980 * {@code TIFF_FLOAT}. 981 * @return The data as an array of {@code float}s. 982 */ 983 public float[] getAsFloats() { 984 return (float[])data; 985 } 986 987 /** 988 * Returns {@code TIFFTag.TIFF_DOUBLE} data as an array of 989 * {@code double}s (64-bit floating-point values). 990 * 991 * @throws ClassCastException if the field is not of type 992 * {@code TIFF_DOUBLE}. 993 * @return The data as an array of {@code double}s. 994 */ 995 public double[] getAsDoubles() { 996 return (double[])data; 997 } 998 999 /** 1000 * Returns {@code TIFFTag.TIFF_SRATIONAL} data as an array of 1001 * 2-element arrays of {@code int}s. 1002 * 1003 * @throws ClassCastException if the field is not of type 1004 * {@code TIFF_SRATIONAL}. 1005 * @return The data as an array of signed rationals. 1006 */ 1007 public int[][] getAsSRationals() { 1008 return (int[][])data; 1009 } 1010 1011 /** 1012 * Returns {@code TIFFTag.TIFF_RATIONAL} data as an array of 1013 * 2-element arrays of {@code long}s. 1014 * 1015 * @throws ClassCastException if the field is not of type 1016 * {@code TIFF_RATIONAL}. 1017 * @return The data as an array of unsigned rationals. 1018 */ 1019 public long[][] getAsRationals() { 1020 return (long[][])data; 1021 } 1022 1023 /** 1024 * Returns data in any format as an {@code int}. 1025 * 1026 * <p> {@code TIFFTag.TIFF_BYTE} values are treated as unsigned; that 1027 * is, no sign extension will take place and the returned value 1028 * will be in the range [0, 255]. {@code TIFF_SBYTE} data 1029 * will be returned in the range [-128, 127]. 1030 * 1031 * <p> A {@code TIFF_UNDEFINED} value is treated as though 1032 * it were a {@code TIFF_BYTE}. 1033 * 1034 * <p> Data in {@code TIFF_SLONG}, {@code TIFF_LONG}, 1035 * {@code TIFF_FLOAT}, {@code TIFF_DOUBLE} or 1036 * {@code TIFF_IFD_POINTER} format are simply cast to 1037 * {@code int} and may suffer from truncation. 1038 * 1039 * <p> Data in {@code TIFF_SRATIONAL} or 1040 * {@code TIFF_RATIONAL} format are evaluated by dividing the 1041 * numerator into the denominator using double-precision 1042 * arithmetic and then casting to {@code int}. Loss of 1043 * precision and truncation may occur. 1044 * 1045 * <p> Data in {@code TIFF_ASCII} format will be parsed as by 1046 * the {@code Double.parseDouble} method, with the result 1047 * case to {@code int}. 1048 * 1049 * @param index The index of the data. 1050 * @return The data at the given index as an {@code int}. 1051 */ 1052 public int getAsInt(int index) { 1053 switch (type) { 1054 case TIFFTag.TIFF_BYTE: 1055 case TIFFTag.TIFF_UNDEFINED: 1056 return ((byte[])data)[index] & 0xff; 1057 case TIFFTag.TIFF_SBYTE: 1058 return ((byte[])data)[index]; 1059 case TIFFTag.TIFF_SHORT: 1060 return ((char[])data)[index] & 0xffff; 1061 case TIFFTag.TIFF_SSHORT: 1062 return ((short[])data)[index]; 1063 case TIFFTag.TIFF_SLONG: 1064 return ((int[])data)[index]; 1065 case TIFFTag.TIFF_LONG: 1066 case TIFFTag.TIFF_IFD_POINTER: 1067 return (int)((long[])data)[index]; 1068 case TIFFTag.TIFF_FLOAT: 1069 return (int)((float[])data)[index]; 1070 case TIFFTag.TIFF_DOUBLE: 1071 return (int)((double[])data)[index]; 1072 case TIFFTag.TIFF_SRATIONAL: 1073 int[] ivalue = getAsSRational(index); 1074 return (int)((double)ivalue[0]/ivalue[1]); 1075 case TIFFTag.TIFF_RATIONAL: 1076 long[] lvalue = getAsRational(index); 1077 return (int)((double)lvalue[0]/lvalue[1]); 1078 case TIFFTag.TIFF_ASCII: 1079 String s = ((String[])data)[index]; 1080 return (int)Double.parseDouble(s); 1081 default: 1082 throw new ClassCastException(); // should never happen 1083 } 1084 } 1085 1086 /** 1087 * Returns data in any format as a {@code long}. 1088 * 1089 * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data 1090 * are treated as unsigned; that is, no sign extension will take 1091 * place and the returned value will be in the range [0, 255]. 1092 * {@code TIFF_SBYTE} data will be returned in the range 1093 * [-128, 127]. 1094 * 1095 * <p> Data in {@code TIFF_FLOAT} and {@code TIFF_DOUBLE} are 1096 * simply cast to {@code long} and may suffer from truncation. 1097 * 1098 * <p> Data in {@code TIFF_SRATIONAL} or 1099 * {@code TIFF_RATIONAL} format are evaluated by dividing the 1100 * numerator into the denominator using double-precision 1101 * arithmetic and then casting to {@code long}. Loss of 1102 * precision and truncation may occur. 1103 * 1104 * <p> Data in {@code TIFF_ASCII} format will be parsed as by 1105 * the {@code Double.parseDouble} method, with the result 1106 * cast to {@code long}. 1107 * 1108 * @param index The index of the data. 1109 * @return The data at the given index as a {@code long}. 1110 */ 1111 public long getAsLong(int index) { 1112 switch (type) { 1113 case TIFFTag.TIFF_BYTE: 1114 case TIFFTag.TIFF_UNDEFINED: 1115 return ((byte[])data)[index] & 0xff; 1116 case TIFFTag.TIFF_SBYTE: 1117 return ((byte[])data)[index]; 1118 case TIFFTag.TIFF_SHORT: 1119 return ((char[])data)[index] & 0xffff; 1120 case TIFFTag.TIFF_SSHORT: 1121 return ((short[])data)[index]; 1122 case TIFFTag.TIFF_SLONG: 1123 return ((int[])data)[index]; 1124 case TIFFTag.TIFF_LONG: 1125 case TIFFTag.TIFF_IFD_POINTER: 1126 return ((long[])data)[index]; 1127 case TIFFTag.TIFF_FLOAT: 1128 return (long)((float[])data)[index]; 1129 case TIFFTag.TIFF_DOUBLE: 1130 return (long)((double[])data)[index]; 1131 case TIFFTag.TIFF_SRATIONAL: 1132 int[] ivalue = getAsSRational(index); 1133 return (long)((double)ivalue[0]/ivalue[1]); 1134 case TIFFTag.TIFF_RATIONAL: 1135 long[] lvalue = getAsRational(index); 1136 return (long)((double)lvalue[0]/lvalue[1]); 1137 case TIFFTag.TIFF_ASCII: 1138 String s = ((String[])data)[index]; 1139 return (long)Double.parseDouble(s); 1140 default: 1141 throw new ClassCastException(); // should never happen 1142 } 1143 } 1144 1145 /** 1146 * Returns data in any format as a {@code float}. 1147 * 1148 * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data 1149 * are treated as unsigned; that is, no sign extension will take 1150 * place and the returned value will be in the range [0, 255]. 1151 * {@code TIFF_SBYTE} data will be returned in the range 1152 * [-128, 127]. 1153 * 1154 * <p> Data in {@code TIFF_SLONG}, {@code TIFF_LONG}, 1155 * {@code TIFF_DOUBLE}, or {@code TIFF_IFD_POINTER} format are 1156 * simply cast to {@code float} and may suffer from 1157 * truncation. 1158 * 1159 * <p> Data in {@code TIFF_SRATIONAL} or 1160 * {@code TIFF_RATIONAL} format are evaluated by dividing the 1161 * numerator into the denominator using double-precision 1162 * arithmetic and then casting to {@code float}. 1163 * 1164 * <p> Data in {@code TIFF_ASCII} format will be parsed as by 1165 * the {@code Double.parseDouble} method, with the result 1166 * cast to {@code float}. 1167 * 1168 * @param index The index of the data. 1169 * @return The data at the given index as a {@code float}. 1170 */ 1171 public float getAsFloat(int index) { 1172 switch (type) { 1173 case TIFFTag.TIFF_BYTE: 1174 case TIFFTag.TIFF_UNDEFINED: 1175 return ((byte[])data)[index] & 0xff; 1176 case TIFFTag.TIFF_SBYTE: 1177 return ((byte[])data)[index]; 1178 case TIFFTag.TIFF_SHORT: 1179 return ((char[])data)[index] & 0xffff; 1180 case TIFFTag.TIFF_SSHORT: 1181 return ((short[])data)[index]; 1182 case TIFFTag.TIFF_SLONG: 1183 return ((int[])data)[index]; 1184 case TIFFTag.TIFF_LONG: 1185 case TIFFTag.TIFF_IFD_POINTER: 1186 return ((long[])data)[index]; 1187 case TIFFTag.TIFF_FLOAT: 1188 return ((float[])data)[index]; 1189 case TIFFTag.TIFF_DOUBLE: 1190 return (float)((double[])data)[index]; 1191 case TIFFTag.TIFF_SRATIONAL: 1192 int[] ivalue = getAsSRational(index); 1193 return (float)((double)ivalue[0]/ivalue[1]); 1194 case TIFFTag.TIFF_RATIONAL: 1195 long[] lvalue = getAsRational(index); 1196 return (float)((double)lvalue[0]/lvalue[1]); 1197 case TIFFTag.TIFF_ASCII: 1198 String s = ((String[])data)[index]; 1199 return (float)Double.parseDouble(s); 1200 default: 1201 throw new ClassCastException(); // should never happen 1202 } 1203 } 1204 1205 /** 1206 * Returns data in any format as a {@code double}. 1207 * 1208 * <p> {@code TIFFTag.TIFF_BYTE} and {@code TIFF_UNDEFINED} data 1209 * are treated as unsigned; that is, no sign extension will take 1210 * place and the returned value will be in the range [0, 255]. 1211 * {@code TIFF_SBYTE} data will be returned in the range 1212 * [-128, 127]. 1213 * 1214 * <p> Data in {@code TIFF_SRATIONAL} or 1215 * {@code TIFF_RATIONAL} format are evaluated by dividing the 1216 * numerator into the denominator using double-precision 1217 * arithmetic. 1218 * 1219 * <p> Data in {@code TIFF_ASCII} format will be parsed as by 1220 * the {@code Double.parseDouble} method. 1221 * 1222 * @param index The index of the data. 1223 * @return The data at the given index as a {@code double}. 1224 */ 1225 public double getAsDouble(int index) { 1226 switch (type) { 1227 case TIFFTag.TIFF_BYTE: 1228 case TIFFTag.TIFF_UNDEFINED: 1229 return ((byte[])data)[index] & 0xff; 1230 case TIFFTag.TIFF_SBYTE: 1231 return ((byte[])data)[index]; 1232 case TIFFTag.TIFF_SHORT: 1233 return ((char[])data)[index] & 0xffff; 1234 case TIFFTag.TIFF_SSHORT: 1235 return ((short[])data)[index]; 1236 case TIFFTag.TIFF_SLONG: 1237 return ((int[])data)[index]; 1238 case TIFFTag.TIFF_LONG: 1239 case TIFFTag.TIFF_IFD_POINTER: 1240 return ((long[])data)[index]; 1241 case TIFFTag.TIFF_FLOAT: 1242 return ((float[])data)[index]; 1243 case TIFFTag.TIFF_DOUBLE: 1244 return ((double[])data)[index]; 1245 case TIFFTag.TIFF_SRATIONAL: 1246 int[] ivalue = getAsSRational(index); 1247 return (double)ivalue[0]/ivalue[1]; 1248 case TIFFTag.TIFF_RATIONAL: 1249 long[] lvalue = getAsRational(index); 1250 return (double)lvalue[0]/lvalue[1]; 1251 case TIFFTag.TIFF_ASCII: 1252 String s = ((String[])data)[index]; 1253 return Double.parseDouble(s); 1254 default: 1255 throw new ClassCastException(); // should never happen 1256 } 1257 } 1258 1259 /** 1260 * Returns a {@code TIFFTag.TIFF_ASCII} value as a 1261 * {@code String}. 1262 * 1263 * @throws ClassCastException if the field is not of type 1264 * {@code TIFF_ASCII}. 1265 * 1266 * @param index The index of the data. 1267 * @return The data at the given index as a {@code String}. 1268 */ 1269 public String getAsString(int index) { 1270 return ((String[])data)[index]; 1271 } 1272 1273 /** 1274 * Returns a {@code TIFFTag.TIFF_SRATIONAL} data item as a 1275 * two-element array of {@code int}s. 1276 * 1277 * @param index The index of the data. 1278 * @return The data at the given index as a signed rational. 1279 * @throws ClassCastException if the field is not of type 1280 * {@code TIFF_SRATIONAL}. 1281 */ 1282 public int[] getAsSRational(int index) { 1283 return ((int[][])data)[index]; 1284 } 1285 1286 /** 1287 * Returns a TIFFTag.TIFF_RATIONAL data item as a two-element array 1288 * of ints. 1289 * 1290 * @param index The index of the data. 1291 * @return The data at the given index as an unsigned rational. 1292 * @throws ClassCastException if the field is not of type 1293 * {@code TIFF_RATIONAL}. 1294 */ 1295 public long[] getAsRational(int index) { 1296 return ((long[][])data)[index]; 1297 } 1298 1299 1300 /** 1301 * Returns a {@code String} containing a human-readable 1302 * version of the data item. Data of type 1303 * {@code TIFFTag.TIFF_RATIONAL} or {@code TIFF_SRATIONAL} are 1304 * represented as a pair of integers separated by a 1305 * {@code '/'} character. If the numerator of a 1306 * {@code TIFFTag.TIFF_RATIONAL} or {@code TIFF_SRATIONAL} is an integral 1307 * multiple of the denominator, then the value is represented as 1308 * {@code "q/1"} where {@code q} is the quotient of the numerator and 1309 * denominator. 1310 * 1311 * @param index The index of the data. 1312 * @return The data at the given index as a {@code String}. 1313 * @throws ClassCastException if the field is not of one of the 1314 * legal field types. 1315 */ 1316 public String getValueAsString(int index) { 1317 switch (type) { 1318 case TIFFTag.TIFF_ASCII: 1319 return ((String[])data)[index]; 1320 case TIFFTag.TIFF_BYTE: 1321 case TIFFTag.TIFF_UNDEFINED: 1322 return Integer.toString(((byte[])data)[index] & 0xff); 1323 case TIFFTag.TIFF_SBYTE: 1324 return Integer.toString(((byte[])data)[index]); 1325 case TIFFTag.TIFF_SHORT: 1326 return Integer.toString(((char[])data)[index] & 0xffff); 1327 case TIFFTag.TIFF_SSHORT: 1328 return Integer.toString(((short[])data)[index]); 1329 case TIFFTag.TIFF_SLONG: 1330 return Integer.toString(((int[])data)[index]); 1331 case TIFFTag.TIFF_LONG: 1332 case TIFFTag.TIFF_IFD_POINTER: 1333 return Long.toString(((long[])data)[index]); 1334 case TIFFTag.TIFF_FLOAT: 1335 return Float.toString(((float[])data)[index]); 1336 case TIFFTag.TIFF_DOUBLE: 1337 return Double.toString(((double[])data)[index]); 1338 case TIFFTag.TIFF_SRATIONAL: 1339 int[] ivalue = getAsSRational(index); 1340 String srationalString; 1341 if(ivalue[1] != 0 && ivalue[0] % ivalue[1] == 0) { 1342 // If the denominator is a non-zero integral divisor 1343 // of the numerator then convert the fraction to be 1344 // with respect to a unity denominator. 1345 srationalString = 1346 Integer.toString(ivalue[0] / ivalue[1]) + "/1"; 1347 } else { 1348 // Use the values directly. 1349 srationalString = 1350 Integer.toString(ivalue[0]) + 1351 "/" + 1352 Integer.toString(ivalue[1]); 1353 } 1354 return srationalString; 1355 case TIFFTag.TIFF_RATIONAL: 1356 long[] lvalue = getAsRational(index); 1357 String rationalString; 1358 if(lvalue[1] != 0L && lvalue[0] % lvalue[1] == 0) { 1359 // If the denominator is a non-zero integral divisor 1360 // of the numerator then convert the fraction to be 1361 // with respect to a unity denominator. 1362 rationalString = 1363 Long.toString(lvalue[0] / lvalue[1]) + "/1"; 1364 } else { 1365 // Use the values directly. 1366 rationalString = 1367 Long.toString(lvalue[0]) + 1368 "/" + 1369 Long.toString(lvalue[1]); 1370 } 1371 return rationalString; 1372 default: 1373 throw new ClassCastException(); // should never happen 1374 } 1375 } 1376 1377 /** 1378 * Returns whether the field has a {@code TIFFDirectory}. 1379 * 1380 * @return true if and only if getDirectory() returns non-null. 1381 */ 1382 public boolean hasDirectory() { 1383 return getDirectory() != null; 1384 } 1385 1386 /** 1387 * Returns the associated {@code TIFFDirectory}, if available. If no 1388 * directory is set, then {@code null} will be returned. 1389 * 1390 * @return the TIFFDirectory instance or null. 1391 */ 1392 public TIFFDirectory getDirectory() { 1393 return dir; 1394 } 1395 1396 /** 1397 * Clones the field and all the information contained therein. 1398 * 1399 * @return A clone of this {@code TIFFField}. 1400 * @throws CloneNotSupportedException if the instance cannot be cloned. 1401 */ 1402 @Override 1403 public TIFFField clone() throws CloneNotSupportedException { 1404 TIFFField field = (TIFFField)super.clone(); 1405 1406 Object fieldData; 1407 switch (type) { 1408 case TIFFTag.TIFF_BYTE: 1409 case TIFFTag.TIFF_UNDEFINED: 1410 case TIFFTag.TIFF_SBYTE: 1411 fieldData = ((byte[])data).clone(); 1412 break; 1413 case TIFFTag.TIFF_SHORT: 1414 fieldData = ((char[])data).clone(); 1415 break; 1416 case TIFFTag.TIFF_SSHORT: 1417 fieldData = ((short[])data).clone(); 1418 break; 1419 case TIFFTag.TIFF_SLONG: 1420 fieldData = ((int[])data).clone(); 1421 break; 1422 case TIFFTag.TIFF_LONG: 1423 case TIFFTag.TIFF_IFD_POINTER: 1424 fieldData = ((long[])data).clone(); 1425 break; 1426 case TIFFTag.TIFF_FLOAT: 1427 fieldData = ((float[])data).clone(); 1428 break; 1429 case TIFFTag.TIFF_DOUBLE: 1430 fieldData = ((double[])data).clone(); 1431 break; 1432 case TIFFTag.TIFF_SRATIONAL: 1433 fieldData = ((int[][])data).clone(); 1434 break; 1435 case TIFFTag.TIFF_RATIONAL: 1436 fieldData = ((long[][])data).clone(); 1437 break; 1438 case TIFFTag.TIFF_ASCII: 1439 fieldData = ((String[])data).clone(); 1440 break; 1441 default: 1442 throw new ClassCastException(); // should never happen 1443 } 1444 1445 field.tag = tag; 1446 field.tagNumber = tagNumber; 1447 field.type = type; 1448 field.count = count; 1449 field.data = fieldData; 1450 field.dir = dir != null ? dir.clone() : null; 1451 1452 return field; 1453 } 1454 } --- EOF ---