//
// SewTiffs.java
//

import java.awt.image.BufferedImage;
import java.util.Hashtable;
import loci.formats.FilePattern;
import loci.formats.RandomAccessStream;
import loci.formats.TiffTools;
import loci.formats.in.TiffReader;
import loci.formats.out.TiffWriter;

/** Stitches the first plane from a collection of TIFFs into a single file. */
public class SewTiffs {

  private static final int DOTS = 50;

  public static void main(String[] args) throws Exception {
    if (args.length < 2) {
      System.out.println(
        "Usage: java SewTiffs base_name channel_num [time_count]");
      System.exit(1);
    }
    String base = args[0];
    int c = Integer.parseInt(args[1]);
    int num;
    if (args.length < 3) {
      FilePattern fp = new FilePattern(base + "_C1_TP1.tiff");
      int[] count = fp.getCount();
      num = count[1];
    }
    else num = Integer.parseInt(args[2]);
    System.out.println("Fixing " + base + "_C" + c + "_TP<1-" + num + ">.tiff");
    TiffReader in = new TiffReader();
    TiffWriter out = new TiffWriter();
    String outId = base + "_C" + c + ".tiff";
    System.out.println("Writing " + outId);
    System.out.print("   ");
    for (int t=0; t<num; t++) {
      String inId = base + "_C" + c + "_TP" + (t + 1) + ".tiff";

      // read first image plane
      BufferedImage image = in.openImage(inId, 0);
      in.close();

      if (t == 0) {
        // read first IFD
        RandomAccessStream ras = new RandomAccessStream(inId);
        Hashtable ifd = TiffTools.getFirstIFD(ras, 0);
        ras.close();

        // preserve TIFF comment
        Object descObj =
          TiffTools.getIFDValue(ifd, TiffTools.IMAGE_DESCRIPTION);
        String desc = null;
        if (descObj instanceof String) desc = (String) descObj;
        else if (descObj instanceof String[]) desc = ((String[]) descObj)[0];

        if (desc != null) {
          ifd = new Hashtable();
          TiffTools.putIFDValue(ifd, TiffTools.IMAGE_DESCRIPTION, desc);
          out.saveImage(outId, image, ifd, t == num - 1);
          System.out.print(".");
          continue;
        }
      }

      // write image plane
      out.save(outId, image, t == num - 1);

      // update status
      System.out.print(".");
      if (t % DOTS == DOTS - 1) {
        System.out.println(" " + (t + 1));
        System.out.print("   ");
      }
    }
    System.out.println();
  }

}

@@ -54,16 +54,37 @@
 dImage;%0A
+import java.io.File;%0A
 import j
@@ -99,24 +99,24 @@
 .Hashtable;%0A
-
 import loci.
@@ -776,16 +776,25 @@
 Pattern(
+new File(
 base + %22
@@ -803,24 +803,25 @@
 1_TP1.tiff%22)
+)
 ;%0A      int%5B
@@ -1188,24 +1188,53 @@
 int(%22   %22);%0A
+    boolean comment = false;%0A
     for (int
@@ -2037,16 +2037,42 @@
  desc);%0A
+          comment = true;%0A
         
@@ -2437,24 +2437,24 @@
     %7D%0A    %7D%0A
-
     System.o
@@ -2467,15 +2467,138 @@
 ntln();%0A
+    if (comment) System.out.println(%22OME-TIFF comment saved.%22);%0A    else System.out.println(%22No OME-TIFF comment found.%22);%0A
   %7D%0A%0A%7D%0A

Fix file pattern detection; report whether OME-TIFF comment was saved.