Changeset 5964


Ignore:
Timestamp:
02/26/10 06:32:47 (10 years ago)
Author:
callan
Message:

#466 Converting IDF types to a Java 1.5+ enum

Location:
branches/cleanup/components/bio-formats/src/loci/formats/tiff
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/cleanup/components/bio-formats/src/loci/formats/tiff/IFD.java

    r5958 r5964  
    5353  private static final Logger LOGGER = LoggerFactory.getLogger(IFD.class); 
    5454 
    55   // TODO: Investigate using Java 1.5 enums instead of int enumerations. 
    56   //       http://javahowto.blogspot.com/2008/04/java-enum-examples.html 
    57  
    5855  // non-IFD tags (for internal use) 
    5956  public static final int LITTLE_ENDIAN = 0; 
    6057  public static final int BIG_TIFF = 1; 
    61  
    62   // IFD types 
    63   public static final int BYTE = 1; 
    64   public static final int ASCII = 2; 
    65   public static final int SHORT = 3; 
    66   public static final int LONG = 4; 
    67   public static final int RATIONAL = 5; 
    68   public static final int SBYTE = 6; 
    69   public static final int UNDEFINED = 7; 
    70   public static final int SSHORT = 8; 
    71   public static final int SLONG = 9; 
    72   public static final int SRATIONAL = 10; 
    73   public static final int FLOAT = 11; 
    74   public static final int DOUBLE = 12; 
    75   public static final int IFD = 13; 
    76   public static final int LONG8 = 16; 
    77   public static final int SLONG8 = 17; 
    78   public static final int IFD8 = 18; 
    79  
    80   private static final int[] BYTES_PER_ELEMENT = { 
    81     -1, //  0: invalid type 
    82     1,  //  1: BYTE 
    83     1,  //  2: ASCII 
    84     2,  //  3: SHORT 
    85     4,  //  4: LONG 
    86     8,  //  5: RATIONAL 
    87     1,  //  6: SBYTE 
    88     1,  //  7: UNDEFINED 
    89     2,  //  8: SSHORT 
    90     4,  //  9: SLONG 
    91     8,  // 10: SRATIONAL 
    92     4,  // 11: FLOAT 
    93     8,  // 12: DOUBLE 
    94     4,  // 13: IFD 
    95     -1, // 14: invalid type 
    96     -1, // 15: invalid type 
    97     8,  // 16: LONG8 
    98     8,  // 17: SLONG8 
    99     8   // 18: IFD8 
    100   }; 
    10158 
    10259  // IFD tags 
     
    928885  public static String getIFDTagName(int tag) { return getFieldName(tag); } 
    929886 
    930   /** Gets the name of the IFD type encoded by the given number. */ 
    931   public static String getIFDTypeName(int type) { return getFieldName(type); } 
    932  
    933   /** 
    934    * Gets the length in bytes of the IFD type encoded by the given number. 
    935    * Returns -1 if the type is unknown. 
    936    */ 
    937   public static int getIFDTypeLength(int type) { 
    938     if (type < 0 || type >= BYTES_PER_ELEMENT.length) return -1; 
    939     return BYTES_PER_ELEMENT[type]; 
    940   } 
    941  
    942887  /** 
    943888   * This method uses reflection to scan the values of this class's 
  • branches/cleanup/components/bio-formats/src/loci/formats/tiff/TiffIFDEntry.java

    r5758 r5964  
    3535 * @author Chris Allan callan at blackcat.ca 
    3636 */ 
    37 public class TiffIFDEntry implements Comparable { 
     37public class TiffIFDEntry implements Comparable<Object> { 
    3838 
    3939  /** The <i>Tag</i> that identifies the field. */ 
     
    4141 
    4242  /** The field <i>Type</i>. */ 
    43   private int type; 
     43  private IFDType type; 
    4444 
    4545  /** The number of values, <i>Count</i> of the indicated <i>Type</i>. */ 
     
    5252  private long valueOffset; 
    5353 
    54   public TiffIFDEntry(int tag, int type, int valueCount, long valueOffset) { 
     54  public TiffIFDEntry(int tag, IFDType type, int valueCount, long valueOffset) { 
    5555    this.tag = tag; 
    5656    this.type = type; 
     
    6969   * @return the entry's <i>Type</i> value. 
    7070   */ 
    71   public int getType() { return type; } 
     71  public IFDType getType() { return type; } 
    7272 
    7373  /** 
  • branches/cleanup/components/bio-formats/src/loci/formats/tiff/TiffParser.java

    r5962 r5964  
    274274    // The following loosely resembles the logic of getIFD()... 
    275275    in.seek(offset); 
    276     long numEntries = bigTiff ? in.readLong() : in.readShort() & 0xffff; 
     276    long numEntries = bigTiff ? in.readLong() : in.readUnsignedShort(); 
    277277 
    278278    for (int i = 0; i < numEntries; i++) { 
     
    310310    LOGGER.debug("getIFDs: seeking IFD at {}", offset); 
    311311    in.seek(offset); 
    312     long numEntries = bigTiff ? in.readLong() : in.readShort() & 0xffff; 
     312    long numEntries = bigTiff ? in.readLong() : in.readUnsignedShort(); 
    313313    LOGGER.debug("getIFDs: {} directory entries to read", numEntries); 
    314314    if (numEntries == 0 || numEntries == 1) return ifd; 
     
    326326      int tag = entry.getTag(); 
    327327      long pointer = entry.getValueOffset(); 
    328       int bpe = IFD.getIFDTypeLength(entry.getType()); 
     328      int bpe = entry.getType().getBytesPerElement(); 
    329329 
    330330      if (count < 0 || bpe <= 0) { 
     
    378378  /** Retrieve the value corresponding to the given TiffIFDEntry. */ 
    379379  public Object getIFDValue(TiffIFDEntry entry) throws IOException { 
    380     int type = entry.getType(); 
     380    IFDType type = entry.getType(); 
    381381    int count = entry.getValueCount(); 
    382382    long offset = entry.getValueOffset(); 
     
    386386    } 
    387387 
    388     if (type == IFD.BYTE) { 
     388    if (type == IFDType.BYTE) { 
    389389      // 8-bit unsigned integer 
    390390      if (count == 1) return new Short(in.readByte()); 
     
    396396      return shorts; 
    397397    } 
    398     else if (type == IFD.ASCII) { 
     398    else if (type == IFDType.ASCII) { 
    399399      // 8-bit byte that contain a 7-bit ASCII code; 
    400400      // the last byte must be NUL (binary zero) 
     
    426426      return strings == null ? (Object) s : strings; 
    427427    } 
    428     else if (type == IFD.SHORT) { 
     428    else if (type == IFDType.SHORT) { 
    429429      // 16-bit (2-byte) unsigned integer 
    430       if (count == 1) return new Integer(in.readShort() & 0xffff); 
     430      if (count == 1) return new Integer(in.readUnsignedShort()); 
    431431      int[] shorts = new int[count]; 
    432432      for (int j=0; j<count; j++) { 
    433         shorts[j] = in.readShort() & 0xffff; 
     433        shorts[j] = in.readUnsignedShort(); 
    434434      } 
    435435      return shorts; 
    436436    } 
    437     else if (type == IFD.LONG || type == IFD.IFD) { 
     437    else if (type == IFDType.LONG || type == IFDType.IFD) { 
    438438      // 32-bit (4-byte) unsigned integer 
    439439      if (count == 1) return new Long(in.readInt()); 
     
    442442      return longs; 
    443443    } 
    444     else if (type == IFD.LONG8 || type == IFD.SLONG8 || type == IFD.IFD8) { 
     444    else if (type == IFDType.LONG8 || type == IFDType.SLONG8 
     445             || type == IFDType.IFD8) { 
    445446      if (count == 1) return new Long(in.readLong()); 
    446447      long[] longs = new long[count]; 
     
    448449      return longs; 
    449450    } 
    450     else if (type == IFD.RATIONAL || type == IFD.SRATIONAL) { 
     451    else if (type == IFDType.RATIONAL || type == IFDType.SRATIONAL) { 
    451452      // Two LONGs or SLONGs: the first represents the numerator 
    452453      // of a fraction; the second, the denominator 
     
    458459      return rationals; 
    459460    } 
    460     else if (type == IFD.SBYTE || type == IFD.UNDEFINED) { 
     461    else if (type == IFDType.SBYTE || type == IFDType.UNDEFINED) { 
    461462      // SBYTE: An 8-bit signed (twos-complement) integer 
    462463      // UNDEFINED: An 8-bit byte that may contain anything, 
     
    467468      return sbytes; 
    468469    } 
    469     else if (type == IFD.SSHORT) { 
     470    else if (type == IFDType.SSHORT) { 
    470471      // A 16-bit (2-byte) signed (twos-complement) integer 
    471472      if (count == 1) return new Short(in.readShort()); 
     
    474475      return sshorts; 
    475476    } 
    476     else if (type == IFD.SLONG) { 
     477    else if (type == IFDType.SLONG) { 
    477478      // A 32-bit (4-byte) signed (twos-complement) integer 
    478479      if (count == 1) return new Integer(in.readInt()); 
     
    481482      return slongs; 
    482483    } 
    483     else if (type == IFD.FLOAT) { 
     484    else if (type == IFDType.FLOAT) { 
    484485      // Single precision (4-byte) IEEE format 
    485486      if (count == 1) return new Float(in.readFloat()); 
     
    488489      return floats; 
    489490    } 
    490     else if (type == IFD.DOUBLE) { 
     491    else if (type == IFDType.DOUBLE) { 
    491492      // Double precision (8-byte) IEEE format 
    492493      if (count == 1) return new Double(in.readDouble()); 
     
    941942 
    942943  TiffIFDEntry readTiffIFDEntry() throws IOException { 
    943     int entryTag = in.readShort() & 0xffff; 
     944    int entryTag = in.readUnsignedShort(); 
    944945 
    945946    // Parse the entry's "Type" 
    946     int entryType = in.readShort() & 0xffff; 
     947    IFDType entryType = IFDType.get(in.readUnsignedShort()); 
    947948 
    948949    // Parse the entry's "ValueCount" 
     
    953954    } 
    954955 
    955     int nValueBytes = valueCount * IFD.getIFDTypeLength(entryType); 
     956    int nValueBytes = valueCount * entryType.getBytesPerElement(); 
    956957    int threshhold = bigTiff ? 8 : 4; 
    957958    long offset = nValueBytes > threshhold ? 
  • branches/cleanup/components/bio-formats/src/loci/formats/tiff/TiffSaver.java

    r5956 r5964  
    143143    if (value instanceof short[]) { 
    144144      short[] q = (short[]) value; 
    145       ifdOut.writeShort(IFD.BYTE); 
     145      ifdOut.writeShort(IFDType.BYTE.getCode()); 
    146146      if (bigTiff) ifdOut.writeLong(q.length); 
    147147      else ifdOut.writeInt(q.length); 
     
    162162    else if (value instanceof String) { // ASCII 
    163163      char[] q = ((String) value).toCharArray(); 
    164       ifdOut.writeShort(IFD.ASCII); // type 
     164      ifdOut.writeShort(IFDType.ASCII.getCode()); // type 
    165165      if (bigTiff) ifdOut.writeLong(q.length + 1); 
    166166      else ifdOut.writeInt(q.length + 1); 
     
    183183    else if (value instanceof int[]) { // SHORT 
    184184      int[] q = (int[]) value; 
    185       ifdOut.writeShort(IFD.SHORT); // type 
     185      ifdOut.writeShort(IFDType.SHORT.getCode()); // type 
    186186      if (bigTiff) ifdOut.writeLong(q.length); 
    187187      else ifdOut.writeInt(q.length); 
     
    211211 
    212212      if (bigTiff) { 
    213         ifdOut.writeShort(IFD.LONG8); 
     213        ifdOut.writeShort(IFDType.LONG8.getCode()); 
    214214        ifdOut.writeLong(q.length); 
    215215 
     
    230230      } 
    231231      else { 
    232         ifdOut.writeShort(IFD.LONG); 
     232        ifdOut.writeShort(IFDType.LONG.getCode()); 
    233233        ifdOut.writeInt(q.length); 
    234234        if (q.length <= dataLength / 4) { 
     
    250250    else if (value instanceof TiffRational[]) { // RATIONAL 
    251251      TiffRational[] q = (TiffRational[]) value; 
    252       ifdOut.writeShort(IFD.RATIONAL); // type 
     252      ifdOut.writeShort(IFDType.RATIONAL.getCode()); // type 
    253253      if (bigTiff) ifdOut.writeLong(q.length); 
    254254      else ifdOut.writeInt(q.length); 
     
    273273    else if (value instanceof float[]) { // FLOAT 
    274274      float[] q = (float[]) value; 
    275       ifdOut.writeShort(IFD.FLOAT); // type 
     275      ifdOut.writeShort(IFDType.FLOAT.getCode()); // type 
    276276      if (bigTiff) ifdOut.writeLong(q.length); 
    277277      else ifdOut.writeInt(q.length); 
     
    298298    else if (value instanceof double[]) { // DOUBLE 
    299299      double[] q = (double[]) value; 
    300       ifdOut.writeShort(IFD.DOUBLE); // type 
     300      ifdOut.writeShort(IFDType.DOUBLE.getCode()); // type 
    301301      if (bigTiff) ifdOut.writeLong(q.length); 
    302302      else ifdOut.writeInt(q.length); 
     
    367367    for (int i=0; i<num; i++) { 
    368368      int oldTag = raf.readUnsignedShort(); 
    369       int oldType = raf.readUnsignedShort(); 
     369      IFDType oldType = IFDType.get(raf.readUnsignedShort()); 
    370370      int oldCount = 
    371371        bigTiff ? (int) (raf.readLong() & 0xffffffff) : raf.readInt(); 
     
    410410        } 
    411411        else if (oldOffset + 
    412           oldCount * IFD.getIFDTypeLength(oldType) == raf.length()) 
     412          oldCount * oldType.getBytesPerElement() == raf.length()) 
    413413        { 
    414414          // old entry was already at EOF; overwrite it 
Note: See TracChangeset for help on using the changeset viewer.