// batchTiffConvert.txt
// Author: Melissa Linkert <melissa at glencoesoftware.com>
// Last updated: 28 March 2011

// Converts all files in a directory to TIFF using the Bio-Formats macro
// extensions.  There will either be one TIFF file per plane or one TIFF file
// per input file, depending upon the value of 'oneFilePerSlice' (see below).

// By default, one TIFF file is created for each plane in each file.

oneFilePerSlice = true;

directory = getDirectory("Choose input files");
fileList = getFileList(directory);

outputDirectory = getDirectory("Choose output directory");

run("Bio-Formats Macro Extensions");
setBatchMode(true);

for (i=0; i<fileList.length; i++) {
  file = directory + fileList[i];
  if (oneFilePerSlice) {
    Ext.setId(file);
    Ext.getImageCount(imageCount);

    for (image=0; image<imageCount; image++) {
      Ext.openImage("", image);
      outFile = outputDirectory + fileList[i] + "-" + image + ".tiff";
      saveFile(outFile);
      close();
    }
    Ext.close();
  }
  else {
    Ext.openImagePlus(file);
    outFile = outputDirectory + fileList[i] + ".tiff";
    saveFile(outFile);
    close();
  }
}

showStatus("Finished.");
setBatchMode(false);

function saveFile(outFile) {
   run("Bio-Formats Exporter", "save=[" + outFile + "] compression=Uncompressed");
}
