Changeset 5612


Ignore:
Timestamp:
10/18/09 15:35:55 (10 years ago)
Author:
curtis
Message:

Backport r5611 OME-TIFF writer fixes to 4.1 branch.

Location:
branches/4.1/components/bio-formats/src/loci/formats
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1/components/bio-formats/src/loci/formats/out/OMETiffWriter.java

    r5334 r5612  
    2626import java.io.File; 
    2727import java.io.IOException; 
    28 import java.util.Vector; 
    29  
     28import java.util.ArrayList; 
     29import java.util.HashMap; 
     30import java.util.UUID; 
     31 
     32import loci.common.Location; 
    3033import loci.formats.FormatException; 
    3134import loci.formats.FormatTools; 
     
    5356    "http://loci.wisc.edu/ome/ome-tiff.html. -->"; 
    5457 
     58  // -- Static fields -- 
     59 
     60  private static HashMap<String, String> uuids = new HashMap<String, String>(); 
     61 
    5562  // -- Fields -- 
    5663 
    57   private Vector seriesMap; 
     64  private ArrayList<Integer> seriesMap; 
     65  private boolean wroteLast; 
    5866 
    5967  // -- Constructor -- 
     
    7078    out = null; 
    7179    if (currentId != null) { 
     80      if (!wroteLast) { 
     81        // FIXME 
     82        throw new IOException( 
     83          "Sorry, closing OME-TIFF files early is not yet supported."); 
     84      } 
     85 
    7286      // extract OME-XML string from metadata object 
    7387      MetadataRetrieve retrieve = getMetadataRetrieve(); 
     
    7589 
    7690      // generate UUID and add to OME element 
    77       // Use Java 1.5 method java.util.UUID.randomUUID() via reflection? 
    78       // but need a separate UUID for each file's OME element 
    79       // now all TiffData elements are identical across all files 
    80       // the ONLY change is the UUID of the root OME element 
    81  
    82       // main challenge is that programmer must still provide TiffData tag 
    83       // list indicating which IFDs of which files correspond to which planes; 
    84       // TiffData properties are part of MetadataStore now for this purpose 
    85       // 
    86       // if programmer indicates which TiffData blocks map to which files using 
    87       // UUID with FileName, we have all the information we need to write each 
    88       // plane to the correct file... assuming planes are fed to saveImage 
    89       // according to the given dimension order 
    90       // 
    91       // finish thinking this through 
     91      String filename = new Location(currentId).getName(); 
     92      String uuid = getUUID(filename); 
     93      omeMeta.setUUID(uuid); 
    9294 
    9395      for (int series=0; series<omeMeta.getImageCount(); series++) { 
     
    98100 
    99101        int imageCount = 0; 
    100         for (int q=0; q<seriesMap.size(); q++) { 
    101           if ((((Integer) seriesMap.get(q))).intValue() == series) imageCount++; 
     102        int ifdCount = seriesMap.size(); 
     103        for (int q=0; q<ifdCount; q++) { 
     104          if (seriesMap.get(q).intValue() == series) imageCount++; 
    102105        } 
    103106 
     
    118121        } 
    119122 
    120         int num = 0; 
    121         for (int plane=0; plane<imageCount; plane++) { 
    122           while (((Integer) seriesMap.get(num)).intValue() != series) { 
    123             num++; 
    124           } 
     123        int ifd = 0, plane = 0; 
     124        while (plane < imageCount) { 
     125          // skip past IFDs from other series 
     126          while (seriesMap.get(ifd).intValue() != series) { 
     127            ifd++; 
     128          } 
     129          // determine number of sequential IFDs 
     130          int end = ifd; 
     131          while (end < ifdCount && seriesMap.get(end).intValue() == series) { 
     132            end++; 
     133          } 
     134          int num = end - ifd; 
     135 
     136          // fill in filename and UUID values 
    125137          int[] zct = FormatTools.getZCTCoords(dimensionOrder, 
    126138            sizeZ, sizeC, sizeT, imageCount, plane); 
    127  
    128           // use the relative file path 
    129           String filename = 
    130             currentId.substring(currentId.lastIndexOf(File.separator) + 1); 
    131  
    132139          omeMeta.setTiffDataFileName(filename, series, 0, plane); 
    133           omeMeta.setTiffDataFirstZ(new Integer(zct[0]), series, 0, plane); 
    134           omeMeta.setTiffDataFirstC(new Integer(zct[1]), series, 0, plane); 
    135           omeMeta.setTiffDataFirstT(new Integer(zct[2]), series, 0, plane); 
    136           omeMeta.setTiffDataIFD(new Integer(num), series, 0, plane); 
    137           omeMeta.setTiffDataNumPlanes(new Integer(1), series, 0, plane); 
    138           num++; 
     140          omeMeta.setTiffDataUUID(uuid, series, 0, plane); 
     141          // fill in any non-default TiffData attributes 
     142          if (zct[0] > 0) { 
     143            omeMeta.setTiffDataFirstZ(new Integer(zct[0]), series, 0, plane); 
     144          } 
     145          if (zct[1] > 0) { 
     146            omeMeta.setTiffDataFirstC(new Integer(zct[1]), series, 0, plane); 
     147          } 
     148          if (zct[2] > 0) { 
     149            omeMeta.setTiffDataFirstT(new Integer(zct[2]), series, 0, plane); 
     150          } 
     151          if (ifd > 0) { 
     152            omeMeta.setTiffDataIFD(new Integer(ifd), series, 0, plane); 
     153          } 
     154          if (num != ifdCount) { 
     155            omeMeta.setTiffDataNumPlanes(new Integer(num), series, 0, plane); 
     156          } 
     157          plane += num; 
     158          ifd = end; 
    139159        } 
    140160      } 
     
    159179    super.close(); 
    160180    seriesMap = null; 
     181    wroteLast = false; 
    161182  } 
    162183 
     
    167188    boolean last) throws FormatException, IOException 
    168189  { 
    169     if (seriesMap == null) seriesMap = new Vector(); 
     190    if (seriesMap == null) seriesMap = new ArrayList<Integer>(); 
    170191    seriesMap.add(new Integer(series)); 
     192    if (last) wroteLast = true; 
    171193    super.saveBytes(buf, series, lastInSeries, last); 
    172194  } 
    173195 
     196  // -- IFormatHandler API methods -- 
     197 
     198  /* @see IFormatHandler#setId(String) */ 
     199  public void setId(String id) throws FormatException, IOException { 
     200    if (id.equals(currentId)) return; 
     201    if (new Location(id).exists()) { 
     202      // FIXME 
     203      throw new FormatException( 
     204        "Sorry, appending to existing OME-TIFF files is not yet supported."); 
     205    } 
     206    super.setId(id); 
     207  } 
     208 
     209  // -- Helper methods -- 
     210 
     211  /** Gets the UUID corresponding to the given filename. */ 
     212  private static String getUUID(String filename) { 
     213    String uuid; 
     214    synchronized (uuids) { 
     215      uuid = uuids.get(filename); 
     216      if (uuid == null) { 
     217        uuid = UUID.randomUUID().toString(); 
     218        uuids.put(filename, uuid); 
     219      } 
     220    } 
     221    return uuid; 
     222  } 
     223 
    174224} 
  • branches/4.1/components/bio-formats/src/loci/formats/out/TiffWriter.java

    r5576 r5612  
    5151public class TiffWriter extends FormatWriter { 
    5252 
     53  // -- Constants -- 
     54 
     55  public static final String COMPRESSION_UNCOMPRESSED = "Uncompressed"; 
     56  public static final String COMPRESSION_LZW = "LZW"; 
     57  public static final String COMPRESSION_J2K = "J2K"; 
     58  public static final String COMPRESSION_J2K_LOSSY = "J2K-Lossy"; 
     59  public static final String COMPRESSION_JPEG = "JPEG"; 
     60 
    5361  // -- Fields -- 
    5462 
     
    7482    super(format, exts); 
    7583    lastOffset = 0; 
    76     compressionTypes = 
    77       new String[] {"Uncompressed", "LZW", "J2K", "J2K-Lossy", "JPEG"}; 
     84    compressionTypes = new String[] { 
     85      COMPRESSION_UNCOMPRESSED, 
     86      COMPRESSION_LZW, 
     87      COMPRESSION_J2K, 
     88      COMPRESSION_J2K_LOSSY, 
     89      COMPRESSION_JPEG 
     90    }; 
    7891    isBigTiff = false; 
    7992  } 
     
    206219    if (compression == null) compression = ""; 
    207220    Integer compressType = new Integer(TiffCompression.UNCOMPRESSED); 
    208     if (compression.equals("LZW")) { 
     221    if (compression.equals(COMPRESSION_LZW)) { 
    209222      compressType = new Integer(TiffCompression.LZW); 
    210223    } 
    211     else if (compression.equals("J2K")) { 
     224    else if (compression.equals(COMPRESSION_J2K)) { 
    212225      compressType = new Integer(TiffCompression.JPEG_2000); 
    213226    } 
    214     else if (compression.equals("J2K-Lossy")) { 
     227    else if (compression.equals(COMPRESSION_J2K_LOSSY)) { 
    215228      compressType = new Integer(TiffCompression.JPEG_2000_LOSSY); 
    216229    } 
    217     else if (compression.equals("JPEG")) { 
     230    else if (compression.equals(COMPRESSION_JPEG)) { 
    218231      compressType = new Integer(TiffCompression.JPEG); 
    219232    } 
     
    227240  /* @see loci.formats.IFormatWriter#getPixelTypes(String) */ 
    228241  public int[] getPixelTypes(String codec) { 
    229     if (codec.startsWith("J2K") || codec.equals("JPEG")) { 
     242    if (codec.startsWith(COMPRESSION_J2K) || codec.equals(COMPRESSION_JPEG)) { 
    230243      return new int[] {FormatTools.INT8, FormatTools.UINT8}; 
    231244    } 
  • branches/4.1/components/bio-formats/src/loci/formats/tools/ImageConverter.java

    r5575 r5612  
    118118        "be replaced with the indicated metadata value from the input file.", 
    119119        "", 
    120         "   Pattern:    Metadata value:", 
     120        "   Pattern:\tMetadata value:", 
    121121        "   ---------------------------", 
    122         "   " + FormatTools.SERIES_NUM + "          series index", 
    123         "   " + FormatTools.SERIES_NAME + "          series name", 
    124         "   " + FormatTools.CHANNEL_NUM + "          channel index", 
    125         "   " + FormatTools.CHANNEL_NAME +"          channel name", 
    126         "   " + FormatTools.Z_NUM + "          Z index", 
    127         "   " + FormatTools.T_NUM + "          T index", 
     122        "   " + FormatTools.SERIES_NUM + "\t\tseries index", 
     123        "   " + FormatTools.SERIES_NAME + "\t\tseries name", 
     124        "   " + FormatTools.CHANNEL_NUM + "\t\tchannel index", 
     125        "   " + FormatTools.CHANNEL_NAME +"\t\tchannel name", 
     126        "   " + FormatTools.Z_NUM + "\t\tZ index", 
     127        "   " + FormatTools.T_NUM + "\t\tT index", 
    128128        "", 
    129129        "If any of these patterns are present, then the images to be saved", 
    130130        "will be split into multiple files.  For example, if the input file", 
    131131        "contains 5 Z sections and 3 timepoints, and out_file is", 
    132         "'converted_Z%z_T%t.tiff'", 
     132        "", 
     133        "  converted_Z" + FormatTools.Z_NUM + "_T" + 
     134        FormatTools.T_NUM + ".tiff", 
     135        "", 
    133136        "then 15 files will be created, with the names", 
    134137        "", 
     
    180183    } 
    181184 
    182     LogTools.print("[" + writer.getFormat() + "] "); 
     185    String format = writer.getFormat(); 
     186    LogTools.print("[" + format + "] "); 
    183187    long mid = System.currentTimeMillis(); 
    184188 
     189    if (format.equals("OME-TIFF") && 
     190      (out.indexOf(FormatTools.SERIES_NUM) > 0 || 
     191      out.indexOf(FormatTools.SERIES_NAME) > 0 || 
     192      out.indexOf(FormatTools.CHANNEL_NUM) > 0 || 
     193      out.indexOf(FormatTools.CHANNEL_NAME) > 0 || 
     194      out.indexOf(FormatTools.Z_NUM) > 0 || 
     195      out.indexOf(FormatTools.T_NUM) > 0)) 
     196    { 
     197      // FIXME 
     198      LogTools.println(); 
     199      LogTools.println( 
     200        "Sorry, conversion to multiple OME-TIFF files is not yet supported."); 
     201      return false; 
     202    } 
     203 
     204    int total = 0; 
    185205    int num = writer.canDoStacks() ? reader.getSeriesCount() : 1; 
    186206    long read = 0, write = 0; 
     
    191211      writer.setInterleaved(reader.isInterleaved()); 
    192212      int numImages = writer.canDoStacks() ? reader.getImageCount() : 1; 
     213      total += numImages; 
    193214      for (int i=0; i<numImages; i++) { 
    194215        writer.setId(FormatTools.getFilename(q, i, reader, out)); 
     
    219240    float sec = (end - start) / 1000f; 
    220241    long initial = mid - start; 
    221     float readAvg = (float) read / num; 
    222     float writeAvg = (float) write / num; 
     242    float readAvg = (float) read / total; 
     243    float writeAvg = (float) write / total; 
    223244    LogTools.println(sec + "s elapsed (" + 
    224       readAvg + "+" + writeAvg + "ms per image, " + initial + "ms overhead)"); 
     245      readAvg + "+" + writeAvg + "ms per plane, " + initial + "ms overhead)"); 
    225246 
    226247    return true; 
  • branches/4.1/components/bio-formats/src/loci/formats/tools/ImageInfo.java

    r5533 r5612  
    714714    float sec2 = (e2 - s2) / 1000f; 
    715715    float avg = (float) (e2 - s2) / images.length; 
    716     LogTools.println(sec2 + "s elapsed (" + avg + "ms per image)"); 
     716    LogTools.println(sec2 + "s elapsed (" + avg + "ms per plane)"); 
    717717 
    718718    if (minmax) printMinMaxValues(); 
Note: See TracChangeset for help on using the changeset viewer.