//
// EditOMETIFF.java
//

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import loci.formats.*;
import loci.formats.in.TiffReader;
import loci.formats.out.TiffWriter;

/** Allows raw user OME-XML comment editing for the given OME-TIFF files. */
public class EditOMETIFF {

  public static void main(String[] args) throws Exception {
    if (args.length == 0) {
      System.out.println("Usage: java EditOMETIFF file1 file2 ...");
      return;
    }
    TiffReader opener = new TiffReader();
    TiffWriter saver = new TiffWriter();
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    for (int i=0; i<args.length; i++) {
      String f = args[i];
      String nf = "new-" + f;
      System.out.print("Reading " + f + " ");
      RandomAccessStream fin = new RandomAccessStream(f);
      Hashtable ifd = TiffTools.getFirstIFD(fin);
      fin.close();
      String ome = (String)
        TiffTools.getIFDValue(ifd, TiffTools.IMAGE_DESCRIPTION);
      System.out.println("[done]");
      System.out.println("OME-XML block =");
      System.out.println(ome);
      System.out.println("Enter new OME-XML block (no line breaks):");
      String xml = in.readLine();
      System.out.print("Saving " + f);
      int num = opener.getImageCount(f);
      for (int j=0; j<num; j++) {
        System.out.print(".");
        BufferedImage img = opener.openImage(f, j);

        ifd = new Hashtable();
        if (j == 0) {
          // update OME-XML block
          TiffTools.putIFDValue(ifd, TiffTools.IMAGE_DESCRIPTION, xml);
        }

        // write file to disk
        saver.saveImage(nf, img, ifd, j == num - 1);
      }
      File oldFile = new File(f);
      File newFile = new File(nf);
      oldFile.delete();
      newFile.renameTo(oldFile);
      System.out.println(" [done]");
    }
  }

}

@@ -24,45 +24,8 @@
 //%0A%0A
-import java.awt.image.BufferedImage;%0A
 impo
@@ -90,44 +90,26 @@
 ats.
-*;%0Aimport loci.formats.in.TiffReader
+RandomAccessStream
 ;%0Aim
@@ -130,22 +130,17 @@
 ats.
-out.TiffWriter
+TiffTools
 ;%0A%0A/
@@ -424,91 +424,8 @@
   %7D%0A
-    TiffReader opener = new TiffReader();%0A    TiffWriter saver = new TiffWriter();%0A
     
@@ -439,16 +439,17 @@
 dReader 
+c
 in = new
@@ -569,38 +569,8 @@
 i%5D;%0A
-      String nf = %22new-%22 + f;%0A
     
@@ -587,16 +587,18 @@
 ut.print
+ln
 (%22Readin
@@ -736,24 +736,98 @@
 in.close();%0A
+      if (ifd == null) System.out.println(%22Warning: first IFD is null!%22);%0A
       String
@@ -1109,16 +1109,17 @@
 g xml = 
+c
 in.readL
@@ -1174,252 +1174,68 @@
     
-int num = opener.getImageCount(f);%0A      for (int j=0; j%3Cnum; j++) %7B%0A        System.out.print(%22.%22);%0A        BufferedImage img = opener.openImage(f, j);%0A%0A        ifd = new Hashtable();%0A        if (j == 0) %7B%0A          // update OME-XML block%0A    
+RandomAccessFile raf = new RandomAccessFile(args%5Bi%5D, %22rw%22);%0A
     
@@ -1246,19 +1246,25 @@
 ffTools.
-put
+overwrite
 IFDValue
@@ -1256,35 +1256,38 @@
 erwriteIFDValue(
-ifd
+raf, 0
 , TiffTools.IMAG
@@ -1317,227 +1317,18 @@
     
-  %7D%0A%0A        // write file to disk%0A        saver.saveImage(nf, img, ifd, j == num - 1);%0A      %7D%0A      File oldFile = new File(f);%0A      File newFile = new File(nf);%0A      oldFile.delete();%0A      newFile.renameTo(oldFile
+raf.close(
 );%0A 

Use new TiffTools.overwriteIFDValue method to update TIFF comment.