Changeset 4270


Ignore:
Timestamp:
07/31/08 23:15:59 (12 years ago)
Author:
melissa
Message:

Fixed overwriteIFDValue to handle BigTIFF files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/TiffTools.java

    r4269 r4270  
    20342034    } 
    20352035    boolean little = header[0] == LITTLE && header[1] == LITTLE; // II 
    2036     long offset = 4; // offset to the IFD 
    2037     int num = 0; // number of directory entries 
     2036    boolean bigTiff = header[2] == 0x2b || header[3] == 0x2b; 
     2037    long offset = bigTiff ? 8 : 4; // offset to the IFD 
     2038    long num = 0; // number of directory entries 
     2039 
     2040    int baseOffset = bigTiff ? 8 : 2; 
     2041    int bytesPerEntry = bigTiff ? BIG_TIFF_BYTES_PER_ENTRY : BYTES_PER_ENTRY; 
     2042 
     2043    raf.seek(offset); 
    20382044 
    20392045    // skip to the correct IFD 
    20402046    for (int i=0; i<=ifd; i++) { 
    2041       offset = DataTools.read4UnsignedBytes(raf, little); 
     2047      offset = bigTiff ? DataTools.read8SignedBytes(raf, little) : 
     2048        DataTools.read4UnsignedBytes(raf, little); 
    20422049      if (offset <= 0) { 
    20432050        throw new FormatException("No such IFD (" + ifd + " of " + i + ")"); 
    20442051      } 
    20452052      raf.seek(offset); 
    2046       num = DataTools.read2UnsignedBytes(raf, little); 
    2047       if (i < ifd) raf.seek(offset + 2 + BYTES_PER_ENTRY * num); 
     2053      num = bigTiff ? DataTools.read8SignedBytes(raf, little) : 
     2054        DataTools.read2UnsignedBytes(raf, little); 
     2055      if (i < ifd) raf.seek(offset + baseOffset + bytesPerEntry * num); 
    20482056    } 
    20492057 
     
    20522060      int oldTag = DataTools.read2UnsignedBytes(raf, little); 
    20532061      int oldType = DataTools.read2UnsignedBytes(raf, little); 
    2054       int oldCount = DataTools.read4SignedBytes(raf, little); 
    2055       int oldOffset = DataTools.read4SignedBytes(raf, little); 
     2062      int oldCount = 
     2063        bigTiff ? (int) (DataTools.read8SignedBytes(raf, little) & 0xffffffff) : 
     2064        DataTools.read4SignedBytes(raf, little); 
     2065      long oldOffset = bigTiff ? DataTools.read8SignedBytes(raf, little) : 
     2066        DataTools.read4SignedBytes(raf, little); 
    20562067      if (oldTag == tag) { 
    20572068        // write new value to buffers 
    2058         ByteArrayOutputStream ifdBuf = new ByteArrayOutputStream(14); 
     2069        ByteArrayOutputStream ifdBuf = new ByteArrayOutputStream(bytesPerEntry); 
    20592070        DataOutputStream ifdOut = new DataOutputStream(ifdBuf); 
    20602071        ByteArrayOutputStream extraBuf = new ByteArrayOutputStream(); 
    20612072        DataOutputStream extraOut = new DataOutputStream(extraBuf); 
    20622073        writeIFDValue(ifdOut, extraBuf, extraOut, oldOffset, tag, value, 
    2063           header[2] == 0x2b); 
     2074          bigTiff); 
    20642075        byte[] bytes = ifdBuf.toByteArray(); 
    20652076        byte[] extra = extraBuf.toByteArray(); 
     
    20682079        int newTag = DataTools.bytesToInt(bytes, 0, 2, false); 
    20692080        int newType = DataTools.bytesToInt(bytes, 2, 2, false); 
    2070         int newCount = DataTools.bytesToInt(bytes, 4, false); 
    2071         int newOffset = DataTools.bytesToInt(bytes, 8, false); 
     2081        int newCount; 
     2082        long newOffset; 
     2083        if (bigTiff) { 
     2084          newCount = 
     2085            (int) (DataTools.bytesToLong(bytes, 4, false) & 0xffffffff); 
     2086          newOffset = DataTools.bytesToLong(bytes, 12, false); 
     2087        } 
     2088        else { 
     2089          newCount = DataTools.bytesToInt(bytes, 4, false); 
     2090          newOffset = DataTools.bytesToInt(bytes, 8, false); 
     2091        } 
    20722092        boolean terminate = false; 
    20732093        if (DEBUG) { 
     
    20992119        else { 
    21002120          // old entry was elsewhere; append to EOF, orphaning old entry 
    2101           newOffset = (int) raf.length(); 
     2121          newOffset = raf.length(); 
    21022122          if (DEBUG) debug("overwriteIFDValue: old entry will be orphaned"); 
    21032123        } 
    21042124 
    21052125        // overwrite old entry 
    2106         raf.seek(raf.getFilePointer() - 10); // jump back 
     2126        raf.seek(raf.getFilePointer() - (bigTiff ? 18 : 10)); // jump back 
    21072127        DataTools.writeShort(raf, newType, little); 
    2108         DataTools.writeInt(raf, newCount, little); 
    2109         DataTools.writeInt(raf, newOffset, little); 
     2128        if (bigTiff) DataTools.writeLong(raf, newCount, little); 
     2129        else DataTools.writeInt(raf, newCount, little); 
     2130        if (bigTiff) DataTools.writeLong(raf, newOffset, little); 
     2131        else DataTools.writeInt(raf, (int) newOffset, little); 
    21102132        if (extra.length > 0) { 
    21112133          raf.seek(newOffset); 
Note: See TracChangeset for help on using the changeset viewer.