Changeset 4046 for trunk/utils


Ignore:
Timestamp:
05/15/08 13:20:21 (12 years ago)
Author:
curtis
Message:

Simplify TIFF comment surgery utility to use latest API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/utils/CommentSurgery.java

    r953 r4046  
    33// 
    44 
    5 import java.io.*; 
    6 import loci.formats.DataTools; 
    75import loci.formats.TiffTools; 
    86 
     
    1513public class CommentSurgery { 
    1614  public static void main(String[] args) throws Exception { 
     15    // the -test flag will print proposed changes to stdout 
     16    // rather than actually changing the comment 
     17    boolean test = args[0].equals("-test"); 
     18    int start = test ? 1 : 0; 
     19 
    1720    for (int i=0; i<args.length; i++) { 
    18       String f = args[i]; 
    19       System.out.println("Processing file " + f); 
    20       RandomAccessFile in = new RandomAccessFile(f, "rw"); 
    21       byte[] four = new byte[4]; 
    22       in.readFully(four); 
    23       boolean little = (four[0] == 'I'); 
    24       int first = DataTools.read4SignedBytes(in, little); 
    25       in.seek(first); 
    26       long fp = first; 
    27       int numEntries = DataTools.read2SignedBytes(in, little); 
    28       fp += 2; 
    29       int oldCount = -1; // length of old comment 
    30       long countOffset = -1; // offset to comment count field 
    31       long commentOffset = -1; // offset to comment itself 
    32       String comment = null; 
    33       for (int j=0; j<numEntries; j++) { 
    34         int tag = DataTools.read2SignedBytes(in, little); 
    35         int type = DataTools.read2SignedBytes(in, little); 
    36         fp += 4; 
    37         countOffset = fp; 
    38         oldCount = DataTools.read4SignedBytes(in, little); 
    39         commentOffset = DataTools.read4SignedBytes(in, little); 
    40         fp += 8; 
    41         if (tag != TiffTools.IMAGE_DESCRIPTION) continue; 
    42         in.seek(commentOffset); 
    43         byte[] b = new byte[oldCount]; 
    44         in.readFully(b); 
    45         long len = new File(f).length(); 
    46         if (in.getFilePointer() != len) { 
    47           System.out.println("ERROR: Comment is not at end of file."); 
    48         //  return; 
    49         } 
    50         comment = new String(b); 
    51         if (comment.length() != oldCount) { 
    52           System.out.println("ERROR: Comment length doesn't match count."); 
    53           return; 
    54         } 
    55         break; 
    56       } 
    57       if (comment == null) { 
     21      String id = args[i]; 
     22      if (!test) System.out.println(id + ": "); 
     23      String xml = TiffTools.getComment(id); 
     24      if (xml == null) { 
    5825        System.out.println("ERROR: No OME-XML comment."); 
    5926        return; 
    6027      } 
     28      int len = xml.length(); 
     29      // do something to the comment; e.g.: 
     30      //xml = xml.replaceAll("LogicalChannel:OWS", "LogicalChannel:OWS347-"); 
     31 
     32      if (test) System.out.println(xml); 
    6133      else { 
    62         // comment is altered here; tailor for whatever is needed 
    63         System.out.println("Old comment =\n" + comment); 
    64         /* Example 1: 
    65         comment = 
    66           comment.replaceAll("LogicalChannel:OWS", "LogicalChannel:OWS347-"); 
    67         */ 
    68         /* Example 2: 
    69         int ndx = comment.indexOf("<Pixels"); 
    70         ndx = comment.indexOf(" />", ndx); 
    71         String tiffData = "<TiffData"; 
    72         int tndx = f.indexOf("_TP"); 
    73         int tval = -1; 
    74         if (tndx >= 0) { 
    75           String s = f.substring(tndx + 3, tndx + 5); 
    76           char c = s.charAt(1); 
    77           if (c < '0' || c > '9') s = s.substring(0, 1); 
    78           int t = Integer.parseInt(s) - 1; 
    79           tiffData += " FirstT=\"" + t + "\""; 
    80         } 
    81         int cndx = f.indexOf("_C"); 
    82         int cval = -1; 
    83         if (cndx >= 0) { 
    84           int c = Integer.parseInt(f.substring(cndx + 2, cndx + 3)) - 1; 
    85           tiffData += " FirstC=\"" + c + "\""; 
    86         } 
    87         tiffData += " />"; 
    88         comment = comment.substring(0, ndx) + ">" + tiffData + 
    89           "</Pixels><CustomAttributes /></Image><CustomAttributes /></OME>"; 
    90         comment = comment.replaceAll("SizeC=\"0\"", "SizeC=\"2\""); 
    91         //comment = comment.replaceAll("SizeT=\"200\"", "SizeT=\"43\""); 
    92         comment = comment.replaceAll("Maimoon ", "Maimoon"); 
    93         */ 
    94         System.out.println("New comment =\n" + comment); 
    95  
    96         // splice in new comment 
    97         byte[] bb = comment.getBytes(); 
    98         if (bb[bb.length - 1] != 0) { 
    99           byte[] bb2 = new byte[bb.length + 1]; 
    100           System.arraycopy(bb, 0, bb2, 0, bb.length); 
    101           bb = bb2; 
    102           System.out.println("WARNING: no null terminator (added one)"); 
    103         } 
    104         byte[] countBytes = new byte[4]; 
    105         if (little) { 
    106           countBytes[0] = (byte) (bb.length % 256); 
    107           countBytes[1] = (byte) ((bb.length >> 8) % 256); 
    108           countBytes[2] = (byte) ((bb.length >> 16) % 256); 
    109           countBytes[3] = (byte) ((bb.length >> 24) % 256); 
    110         } 
    111         else { 
    112           countBytes[0] = (byte) ((bb.length >> 24) % 256); 
    113           countBytes[1] = (byte) ((bb.length >> 16) % 256); 
    114           countBytes[2] = (byte) ((bb.length >> 8) % 256); 
    115           countBytes[3] = (byte) (bb.length % 256); 
    116         } 
    117         long oldLength = in.length(); 
    118         long newLength = oldLength - oldCount + bb.length; 
    119         in.setLength(newLength); 
    120         in.seek(countOffset); 
    121         in.write(countBytes); 
    122         in.seek(commentOffset); 
    123         in.write(bb); 
     34        System.out.println(len + " -> " + xml.length()); 
     35        TiffTools.overwriteComment(id, xml); 
    12436      } 
    125       in.close(); 
    12637    } 
    12738  } 
Note: See TracChangeset for help on using the changeset viewer.