1 18 package org.apache.batik.ext.awt.image.codec.tiff; 19 20 import java.io.Serializable ; 21 22 38 public class TIFFField extends Object implements Comparable , Serializable { 39 40 41 public static final int TIFF_BYTE = 1; 42 43 44 public static final int TIFF_ASCII = 2; 45 46 47 public static final int TIFF_SHORT = 3; 48 49 50 public static final int TIFF_LONG = 4; 51 52 53 public static final int TIFF_RATIONAL = 5; 54 55 56 public static final int TIFF_SBYTE = 6; 57 58 59 public static final int TIFF_UNDEFINED = 7; 60 61 62 public static final int TIFF_SSHORT = 8; 63 64 65 public static final int TIFF_SLONG = 9; 66 67 68 public static final int TIFF_SRATIONAL = 10; 69 70 71 public static final int TIFF_FLOAT = 11; 72 73 74 public static final int TIFF_DOUBLE = 12; 75 76 77 int tag; 78 79 80 int type; 81 82 83 int count; 84 85 86 Object data; 87 88 89 TIFFField() {} 90 91 127 public TIFFField(int tag, int type, int count, Object data) { 128 this.tag = tag; 129 this.type = type; 130 this.count = count; 131 this.data = data; 132 } 133 134 137 public int getTag() { 138 return tag; 139 } 140 141 148 public int getType() { 149 return type; 150 } 151 152 155 public int getCount() { 156 return count; 157 } 158 159 171 public byte[] getAsBytes() { 172 return (byte[])data; 173 } 174 175 182 public char[] getAsChars() { 183 return (char[])data; 184 } 185 186 193 public short[] getAsShorts() { 194 return (short[])data; 195 } 196 197 204 public int[] getAsInts() { 205 return (int[])data; 206 } 207 208 215 public long[] getAsLongs() { 216 return (long[])data; 217 } 218 219 225 public float[] getAsFloats() { 226 return (float[])data; 227 } 228 229 235 public double[] getAsDoubles() { 236 return (double[])data; 237 } 238 239 245 public int[][] getAsSRationals() { 246 return (int[][])data; 247 } 248 249 255 public long[][] getAsRationals() { 256 return (long[][])data; 257 } 258 259 272 public int getAsInt(int index) { 273 switch (type) { 274 case TIFF_BYTE: case TIFF_UNDEFINED: 275 return ((byte[])data)[index] & 0xff; 276 case TIFF_SBYTE: 277 return ((byte[])data)[index]; 278 case TIFF_SHORT: 279 return ((char[])data)[index] & 0xffff; 280 case TIFF_SSHORT: 281 return ((short[])data)[index]; 282 case TIFF_SLONG: 283 return ((int[])data)[index]; 284 default: 285 throw new ClassCastException (); 286 } 287 } 288 289 302 public long getAsLong(int index) { 303 switch (type) { 304 case TIFF_BYTE: case TIFF_UNDEFINED: 305 return ((byte[])data)[index] & 0xff; 306 case TIFF_SBYTE: 307 return ((byte[])data)[index]; 308 case TIFF_SHORT: 309 return ((char[])data)[index] & 0xffff; 310 case TIFF_SSHORT: 311 return ((short[])data)[index]; 312 case TIFF_SLONG: 313 return ((int[])data)[index]; 314 case TIFF_LONG: 315 return ((long[])data)[index]; 316 default: 317 throw new ClassCastException (); 318 } 319 } 320 321 332 public float getAsFloat(int index) { 333 switch (type) { 334 case TIFF_BYTE: 335 return ((byte[])data)[index] & 0xff; 336 case TIFF_SBYTE: 337 return ((byte[])data)[index]; 338 case TIFF_SHORT: 339 return ((char[])data)[index] & 0xffff; 340 case TIFF_SSHORT: 341 return ((short[])data)[index]; 342 case TIFF_SLONG: 343 return ((int[])data)[index]; 344 case TIFF_LONG: 345 return ((long[])data)[index]; 346 case TIFF_FLOAT: 347 return ((float[])data)[index]; 348 case TIFF_DOUBLE: 349 return (float)((double[])data)[index]; 350 case TIFF_SRATIONAL: 351 int[] ivalue = getAsSRational(index); 352 return (float)((double)ivalue[0]/ivalue[1]); 353 case TIFF_RATIONAL: 354 long[] lvalue = getAsRational(index); 355 return (float)((double)lvalue[0]/lvalue[1]); 356 default: 357 throw new ClassCastException (); 358 } 359 } 360 361 370 public double getAsDouble(int index) { 371 switch (type) { 372 case TIFF_BYTE: 373 return ((byte[])data)[index] & 0xff; 374 case TIFF_SBYTE: 375 return ((byte[])data)[index]; 376 case TIFF_SHORT: 377 return ((char[])data)[index] & 0xffff; 378 case TIFF_SSHORT: 379 return ((short[])data)[index]; 380 case TIFF_SLONG: 381 return ((int[])data)[index]; 382 case TIFF_LONG: 383 return ((long[])data)[index]; 384 case TIFF_FLOAT: 385 return ((float[])data)[index]; 386 case TIFF_DOUBLE: 387 return ((double[])data)[index]; 388 case TIFF_SRATIONAL: 389 int[] ivalue = getAsSRational(index); 390 return (double)ivalue[0]/ivalue[1]; 391 case TIFF_RATIONAL: 392 long[] lvalue = getAsRational(index); 393 return (double)lvalue[0]/lvalue[1]; 394 default: 395 throw new ClassCastException (); 396 } 397 } 398 399 405 public String getAsString(int index) { 406 return ((String [])data)[index]; 407 } 408 409 416 public int[] getAsSRational(int index) { 417 return ((int[][])data)[index]; 418 } 419 420 427 public long[] getAsRational(int index) { 428 return ((long[][])data)[index]; 429 } 430 431 442 public int compareTo(Object o) { 443 if(o == null) { 444 throw new IllegalArgumentException (); 445 } 446 447 int oTag = ((TIFFField)o).getTag(); 448 449 if(tag < oTag) { 450 return -1; 451 } else if(tag > oTag) { 452 return 1; 453 } else { 454 return 0; 455 } 456 } 457 } 458 | Popular Tags |