Changeset 3229


Ignore:
Timestamp:
10/05/07 11:54:53 (12 years ago)
Author:
curtis
Message:

OME-TIFF sample data generator now writes OME-TIFF with multiple series.
Still need to add a distinction between multiple Images vs multiple Pixels,
as well as implement the -scramble option.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/utils/MakeTestOmeTiff.java

    r3228 r3229  
    99import java.util.Hashtable; 
    1010import java.util.Vector; 
    11 import loci.formats.FormatException; 
    12 import loci.formats.FormatTools; 
     11import java.util.Calendar; 
     12import loci.formats.*; 
    1313import loci.formats.out.TiffWriter; 
    1414 
     
    4242      if (parity != (scramble ? 1 : 0)) usage = true; 
    4343    } 
     44 
    4445    if (usage) { 
    4546      System.out.println("Usage: java MakeTestOmeTiff name dist [-scramble]"); 
     
    8586    String[] dimOrder = new String[numSeries]; 
    8687    BufferedImage[][] images = new BufferedImage[numSeries][]; 
     88    int[] offsets = new int[numSeries]; 
    8789    for (int s=0; s<numSeries; s++) { 
    8890      sizeX[s] = Integer.parseInt(args[leadParams + paramCount * s]); 
     
    9193      sizeC[s] = Integer.parseInt(args[leadParams + paramCount * s + 3]); 
    9294      sizeT[s] = Integer.parseInt(args[leadParams + paramCount * s + 4]); 
     95      int len = sizeZ[s] * sizeC[s] * sizeT[s]; 
    9396      dimOrder[s] = args[leadParams + paramCount * s + 5].toUpperCase(); 
    94       images[s] = new BufferedImage[sizeZ[s] * sizeC[s] * sizeT[s]]; 
     97      images[s] = new BufferedImage[len]; 
     98      if (s < numSeries - 1) offsets[s + 1] = offsets[s] + len; 
    9599    } 
    96100    int ndx = 0; 
     
    181185    Hashtable lastHash = new Hashtable(); 
    182186    boolean[][] last = new boolean[numSeries][]; 
     187    Hashtable firstZ = new Hashtable(); 
     188    Hashtable firstC = new Hashtable(); 
     189    Hashtable firstT = new Hashtable(); 
    183190    StringBuffer sb = new StringBuffer(); 
    184191    for (int s=0; s<numSeries; s++) { 
     
    188195      for (int p=0; p<len; p++) { 
    189196        sb.append(name); 
    190         if (!allS) sb.append("_series" + s); 
     197        if (!allS && numSeries > 1) sb.append("_series" + (s + 1)); 
    191198        int[] zct = FormatTools.getZCTCoords(dimOrder[s], 
    192199          sizeZ[s], sizeC[s], sizeT[s], len, p); 
    193         if (!allZ) sb.append("_Z" + zct[0]); 
    194         if (!allC) sb.append("_C" + zct[1]); 
    195         if (!allT) sb.append("_T" + zct[2]); 
     200        if (!allZ && sizeZ[s] > 1) sb.append("_Z" + (zct[0] + 1)); 
     201        if (!allC && sizeC[s] > 1) sb.append("_C" + (zct[1] + 1)); 
     202        if (!allT && sizeT[s] > 1) sb.append("_T" + (zct[2] + 1)); 
    196203        sb.append(".ome.tif"); 
    197204        filenames[s][p] = sb.toString(); 
     
    204211        if (index != null) last[index.series][index.plane] = false; 
    205212        lastHash.put(key, new ImageIndex(s, p)); 
     213 
     214        // update FirstZ, FirstC and FirstT values for this filename 
     215        if (!allZ && sizeZ[s] > 1) { 
     216          firstZ.put(filenames[s][p], new Integer(zct[0])); 
     217        } 
     218        if (!allC && sizeC[s] > 1) { 
     219          firstC.put(filenames[s][p], new Integer(zct[1])); 
     220        } 
     221        if (!allT && sizeT[s] > 1) { 
     222          firstT.put(filenames[s][p], new Integer(zct[2])); 
     223        } 
    206224      } 
    207225    } 
     226 
     227    // build OME-XML block 
     228 
     229    // CreationDate is required; initialize a default value (current time) 
     230    // use ISO 8601 dateTime format (e.g., 1988-04-07T18:39:09) 
     231    sb.setLength(0); 
     232    Calendar now = Calendar.getInstance(); 
     233    int year = now.get(Calendar.YEAR); 
     234    int month = now.get(Calendar.MONTH); 
     235    int day = now.get(Calendar.DAY_OF_MONTH); 
     236    int hour = now.get(Calendar.HOUR_OF_DAY); 
     237    int min = now.get(Calendar.MINUTE); 
     238    int sec = now.get(Calendar.SECOND); 
     239    sb.append(year); 
     240    sb.append("-"); 
     241    if (month < 9) sb.append("0"); 
     242    sb.append(month + 1); 
     243    sb.append("-"); 
     244    if (day < 10) sb.append("0"); 
     245    sb.append(day); 
     246    sb.append("T"); 
     247    if (hour < 10) sb.append("0"); 
     248    sb.append(hour); 
     249    sb.append(":"); 
     250    if (min < 10) sb.append("0"); 
     251    sb.append(min); 
     252    sb.append(":"); 
     253    if (sec < 10) sb.append("0"); 
     254    sb.append(sec); 
     255    String creationDate = sb.toString(); 
     256 
     257    sb.setLength(0); 
     258    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
     259      "<!-- Warning: this comment is an OME-XML metadata block, which " + 
     260      "contains crucial dimensional parameters and other important metadata. " + 
     261      "Please edit cautiously (if at all), and back up the original data " + 
     262      "before doing so. For more information, see the OME-TIFF web site: " + 
     263      "http://loci.wisc.edu/ome/ome-tiff.html. --><OME " + 
     264      "xmlns=\"http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd\" " + 
     265      "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 
     266      "xsi:schemaLocation=\"" + 
     267      "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd\">"); 
     268    for (int s=0; s<numSeries; s++) { 
     269      sb.append("<Image " + 
     270        "ID=\"org.openmicroscopy:Image:" + (s + 1) + "\" " + 
     271        "Name=\"" + name + "\" " + 
     272        "DefaultPixels=\"org.openmicroscopy:Pixels:" + (s + 1) + "\">" + 
     273        "<CreationDate>" + creationDate + "</CreationDate>" + 
     274        "<Pixels ID=\"Pixels:1\" " + 
     275        "DimensionOrder=\"" + dimOrder[s] + "\" " + 
     276        "PixelType=\"uint8\" " + 
     277        "BigEndian=\"true\" " + 
     278        "SizeX=\"" + sizeX[s] + "\" " + 
     279        "SizeY=\"" + sizeY[s] + "\" " + 
     280        "SizeZ=\"" + sizeZ[s] + "\" " + 
     281        "SizeC=\"" + sizeC[s] + "\" " + 
     282        "SizeT=\"" + sizeT[s] + "\">" + 
     283        "TIFF_DATA_SERIES_" + s + // placeholder 
     284        "</Pixels></Image>"); 
     285    } 
     286    sb.append("</OME>"); 
     287    String xmlTemplate = sb.toString(); 
    208288 
    209289    TiffWriter out = new TiffWriter(); 
     
    219299          " T" + zct[2] + " -> " + filenames[s][p] + (last[s][p] ? "*" : "")); 
    220300        out.setId(filenames[s][p]); 
    221         out.saveImage(images[s][p], last[s][p]); 
     301        // write comment stub, to be overwritten later 
     302        Hashtable ifd = new Hashtable(); 
     303        TiffTools.putIFDValue(ifd, TiffTools.IMAGE_DESCRIPTION, ""); 
     304        out.saveImage(images[s][p], ifd, last[s][p]); 
     305        if (last[s][p]) { 
     306          // append OME-XML block 
     307          String xml = xmlTemplate; 
     308          for (int ss=0; ss<numSeries; ss++) { 
     309            String pattern = "TIFF_DATA_SERIES_" + ss; 
     310            if (allS) { 
     311              xml = xml.replaceFirst(pattern, 
     312                "<TiffData IFD=\"" + offsets[ss] + "\" " + 
     313                "NumPlanes=\"" + images[ss].length + "\"/>"); 
     314            } 
     315            else if (s == ss) { 
     316              Integer fz = (Integer) firstZ.get(filenames[s][p]); 
     317              Integer fc = (Integer) firstC.get(filenames[s][p]); 
     318              Integer ft = (Integer) firstT.get(filenames[s][p]); 
     319              sb.setLength(0); 
     320              sb.append("<TiffData"); 
     321              if (fz != null) sb.append(" FirstZ=\"" + fz + "\""); 
     322              if (fc != null) sb.append(" FirstC=\"" + fc + "\""); 
     323              if (ft != null) sb.append(" FirstT=\"" + ft + "\""); 
     324              sb.append("/>"); 
     325              xml = xml.replaceFirst(pattern, sb.toString()); 
     326            } 
     327            else { 
     328              xml = xml.replaceFirst(pattern, "<TiffData NumPlanes=\"0\"/>"); 
     329            } 
     330          } 
     331          TiffTools.overwriteComment(filenames[s][p], xml); 
     332        } 
    222333      } 
    223334    } 
    224 //    loci.formats.gui.ImageViewer view = new loci.formats.gui.ImageViewer(); 
    225 //    view.setImages(images[0]); 
    226 //    view.setVisible(true); 
    227335  } 
    228336 
Note: See TracChangeset for help on using the changeset viewer.