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