Changeset 6920


Ignore:
Timestamp:
09/13/10 09:57:22 (9 years ago)
Author:
callan
Message:

Merged r6743 and r6872 from Trunk. (See #543)

Location:
branches/4.2
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/4.2

  • branches/4.2/components/bio-formats/src/loci/formats/in

  • branches/4.2/components/bio-formats/src/loci/formats/meta

  • branches/4.2/components/bio-formats/src/loci/formats/ome

  • branches/4.2/components/bio-formats/src/loci/formats/out/OMETiffWriter.java

    r6694 r6920  
    4747import loci.formats.meta.MetadataRetrieve; 
    4848import loci.formats.ome.OMEXMLMetadata; 
     49import loci.formats.ome.OMEXMLMetadataImpl; 
    4950import loci.formats.services.OMEXMLService; 
    5051import loci.formats.tiff.TiffSaver; 
     
    7879  private String[][] imageLocations; 
    7980  private int totalPlanes = 0; 
     81  private OMEXMLMetadata omeMeta; 
     82  private OMEXMLService service; 
    8083 
    8184  // -- Constructor -- 
     
    8992  /* @see loci.formats.IFormatHandler#close() */ 
    9093  public void close() throws IOException { 
    91     if (currentId != null) { 
    92       if (!wroteLast) { 
    93         super.close(); 
    94         return; 
    95       } 
    96  
    97       // extract OME-XML string from metadata object 
    98       MetadataRetrieve retrieve = getMetadataRetrieve(); 
    99  
    100       OMEXMLMetadata omeMeta; 
    101       OMEXMLService service; 
    102       try { 
    103         ServiceFactory factory = new ServiceFactory(); 
    104         service = factory.getInstance(OMEXMLService.class); 
    105         omeMeta = service.getOMEMetadata(retrieve); 
    106       } 
    107       catch (DependencyException de) { 
    108         // TODO : Modify close() signature to include FormatException? 
    109         // throw new MissingLibraryException(OMETiffReader.NO_OME_XML_JAR, de); 
    110         throw new RuntimeException(de); 
    111       } 
    112       catch (ServiceException se) { 
    113         // TODO : Modify close() signature to include FormatException? 
    114         // throw new FormatException(se); 
    115         throw new RuntimeException(se); 
    116       } 
    117  
    118       // remove any BinData elements from the OME-XML 
    119  
    120       OME root = (OME) omeMeta.getRoot(); 
    121       List<Image> images = root.copyImageList(); 
    122       for (Image img : images) { 
    123         Pixels pix = img.getPixels(); 
    124         List<BinData> binData = pix.copyBinDataList(); 
    125         for (BinData bin : binData) { 
    126           pix.removeBinData(bin); 
     94    try { 
     95      if (currentId != null && wroteLast) { 
     96        setupServiceAndMetadata(); 
     97 
     98        // remove any BinData elements from the OME-XML 
     99        removeBinData(omeMeta); 
     100 
     101        for (int series=0; series<omeMeta.getImageCount(); series++) { 
     102          populateImage(omeMeta, series); 
    127103        } 
    128       } 
    129       omeMeta.setRoot(root); 
    130  
    131       for (int series=0; series<omeMeta.getImageCount(); series++) { 
    132         String dimensionOrder = 
    133           omeMeta.getPixelsDimensionOrder(series).toString(); 
    134         int sizeZ = omeMeta.getPixelsSizeZ(series).getValue().intValue(); 
    135         int sizeC = omeMeta.getPixelsSizeC(series).getValue().intValue(); 
    136         int sizeT = omeMeta.getPixelsSizeT(series).getValue().intValue(); 
    137  
    138         int imageCount = getPlaneCount(); 
    139         int ifdCount = seriesMap.size(); 
    140  
    141         if (imageCount == 0) { 
    142           omeMeta.setTiffDataPlaneCount(new NonNegativeInteger(0), series, 0); 
    143           continue; 
     104 
     105        ArrayList<String> files = new ArrayList<String>(); 
     106        for (String[] s : imageLocations) { 
     107          for (String f : s) { 
     108            if (!files.contains(f)) { 
     109              files.add(f); 
     110 
     111              String xml = getOMEXML(f); 
     112 
     113              // write OME-XML to the first IFD's comment 
     114              saveComment(f, xml); 
     115            } 
     116          } 
    144117        } 
    145  
    146         PositiveInteger samplesPerPixel = 
    147           new PositiveInteger((sizeZ * sizeC * sizeT) / imageCount); 
    148         for (int c=0; c<omeMeta.getChannelCount(series); c++) { 
    149           omeMeta.setChannelSamplesPerPixel(samplesPerPixel, series, c); 
    150         } 
    151         sizeC /= samplesPerPixel.getValue(); 
    152  
    153         HashMap<String, Integer> ifdCounts = new HashMap<String, Integer>(); 
    154  
    155         for (int plane=0; plane<imageCount; plane++) { 
    156           int[] zct = FormatTools.getZCTCoords(dimensionOrder, 
    157             sizeZ, sizeC, sizeT, imageCount, plane); 
    158           String filename = 
    159             new Location(imageLocations[series][plane]).getName(); 
    160  
    161           Integer ifdIndex = ifdCounts.get(filename); 
    162           int ifd = ifdIndex == null ? 0 : ifdIndex.intValue(); 
    163  
    164           omeMeta.setUUIDFileName(filename, series, plane); 
    165           String uuid = "urn:uuid:" + getUUID(filename); 
    166           omeMeta.setUUIDValue(uuid, series, plane); 
    167           // fill in any non-default TiffData attributes 
    168           omeMeta.setTiffDataFirstZ( 
    169               new NonNegativeInteger(zct[0]), series, plane); 
    170           omeMeta.setTiffDataFirstC( 
    171               new NonNegativeInteger(zct[1]), series, plane); 
    172           omeMeta.setTiffDataFirstT( 
    173               new NonNegativeInteger(zct[2]), series, plane); 
    174           omeMeta.setTiffDataIFD(new NonNegativeInteger(ifd), series, plane); 
    175           omeMeta.setTiffDataPlaneCount( 
    176               new NonNegativeInteger(1), series, plane); 
    177  
    178           ifdCounts.put(filename, ifd + 1); 
    179         } 
    180       } 
    181  
    182       ArrayList<String> files = new ArrayList<String>(); 
    183       for (String[] s : imageLocations) { 
    184         for (String f : s) { 
    185           if (!files.contains(f)) files.add(f); 
    186         } 
    187       } 
    188  
    189       for (String file : files) { 
    190         // generate UUID and add to OME element 
    191         String uuid = "urn:uuid:" + getUUID(new Location(file).getName()); 
    192         omeMeta.setUUID(uuid); 
    193  
    194         String xml; 
    195         try { 
    196           xml = service.getOMEXML(omeMeta); 
    197         } 
    198         catch (ServiceException se) { 
    199           // FIXME: Modify close() signature to include FormatException? 
    200           // throw new FormatException(se); 
    201           throw new RuntimeException(se); 
    202         } 
    203  
    204         // insert warning comment 
    205         String prefix = xml.substring(0, xml.indexOf(">") + 1); 
    206         String suffix = xml.substring(xml.indexOf(">") + 1); 
    207         xml = prefix + WARNING_COMMENT + suffix; 
    208  
    209         if (out != null) out.close(); 
    210         out = new RandomAccessOutputStream(file); 
    211  
    212         // write OME-XML to the first IFD's comment 
    213         try { 
    214           TiffSaver saver = new TiffSaver(out); 
    215           RandomAccessInputStream in = new RandomAccessInputStream(file); 
    216           saver.overwriteLastIFDOffset(in); 
    217           saver.overwriteComment(in, xml); 
    218           in.close(); 
    219         } 
    220         catch (FormatException exc) { 
    221           IOException io = new IOException("Unable to append OME-XML comment"); 
    222           io.initCause(exc); 
    223           throw io; 
    224         } 
    225       } 
    226     } 
    227     super.close(); 
    228     seriesMap = null; 
    229     imageLocations = null; 
    230     wroteLast = false; 
    231     totalPlanes = 0; 
     118      } 
     119    } 
     120    catch (DependencyException de) { 
     121      throw new RuntimeException(de); 
     122    } 
     123    catch (ServiceException se) { 
     124      throw new RuntimeException(se); 
     125    } 
     126    catch (FormatException fe) { 
     127      throw new RuntimeException(fe); 
     128    } 
     129    catch (IllegalArgumentException iae) { 
     130      throw new RuntimeException(iae); 
     131    } 
     132    finally { 
     133      super.close(); 
     134      if (wroteLast) { 
     135        seriesMap = null; 
     136        imageLocations = null; 
     137        wroteLast = false; 
     138        totalPlanes = 0; 
     139        omeMeta = null; 
     140        service = null; 
     141      } 
     142    } 
    232143  } 
    233144 
     
    293204  } 
    294205 
     206  private void setupServiceAndMetadata() 
     207    throws DependencyException, ServiceException 
     208  { 
     209    // extract OME-XML string from metadata object 
     210    MetadataRetrieve retrieve = getMetadataRetrieve(); 
     211 
     212    ServiceFactory factory = new ServiceFactory(); 
     213    service = factory.getInstance(OMEXMLService.class); 
     214    omeMeta = service.getOMEMetadata(retrieve); 
     215    if (omeMeta instanceof OMEXMLMetadataImpl) { 
     216      ((OMEXMLMetadataImpl) omeMeta).resolveReferences(); 
     217    } 
     218  } 
     219 
     220  private String getOMEXML(String file) throws FormatException, IOException { 
     221    // generate UUID and add to OME element 
     222    String uuid = "urn:uuid:" + getUUID(new Location(file).getName()); 
     223    omeMeta.setUUID(uuid); 
     224 
     225    String xml; 
     226    try { 
     227      xml = service.getOMEXML(omeMeta); 
     228    } 
     229    catch (ServiceException se) { 
     230      throw new FormatException(se); 
     231    } 
     232 
     233    // insert warning comment 
     234    String prefix = xml.substring(0, xml.indexOf(">") + 1); 
     235    String suffix = xml.substring(xml.indexOf(">") + 1); 
     236    return prefix + WARNING_COMMENT + suffix; 
     237  } 
     238 
     239  private void saveComment(String file, String xml) throws IOException { 
     240    if (out != null) out.close(); 
     241    out = new RandomAccessOutputStream(file); 
     242    RandomAccessInputStream in = null; 
     243    try { 
     244      TiffSaver saver = new TiffSaver(out); 
     245      in = new RandomAccessInputStream(file); 
     246      saver.overwriteLastIFDOffset(in); 
     247      saver.overwriteComment(in, xml); 
     248      in.close(); 
     249    } 
     250    catch (FormatException exc) { 
     251      IOException io = new IOException("Unable to append OME-XML comment"); 
     252      io.initCause(exc); 
     253      throw io; 
     254    } 
     255    finally { 
     256      if (out != null) out.close(); 
     257      if (in != null) in.close(); 
     258    } 
     259  } 
     260 
     261  private void removeBinData(OMEXMLMetadata omeMeta) { 
     262    OME root = (OME) omeMeta.getRoot(); 
     263    List<Image> images = root.copyImageList(); 
     264    for (Image img : images) { 
     265      Pixels pix = img.getPixels(); 
     266      List<BinData> binData = pix.copyBinDataList(); 
     267      for (BinData bin : binData) { 
     268        pix.removeBinData(bin); 
     269      } 
     270    } 
     271    omeMeta.setRoot(root); 
     272  } 
     273 
     274  private void populateTiffData(OMEXMLMetadata omeMeta, int[] zct, 
     275    int ifd, int series, int plane) 
     276  { 
     277    omeMeta.setTiffDataFirstZ(new NonNegativeInteger(zct[0]), series, plane); 
     278    omeMeta.setTiffDataFirstC(new NonNegativeInteger(zct[1]), series, plane); 
     279    omeMeta.setTiffDataFirstT(new NonNegativeInteger(zct[2]), series, plane); 
     280    omeMeta.setTiffDataIFD(new NonNegativeInteger(ifd), series, plane); 
     281    omeMeta.setTiffDataPlaneCount(new NonNegativeInteger(1), series, plane); 
     282  } 
     283 
     284  private void populateImage(OMEXMLMetadata omeMeta, int series) { 
     285    String dimensionOrder = omeMeta.getPixelsDimensionOrder(series).toString(); 
     286    int sizeZ = omeMeta.getPixelsSizeZ(series).getValue().intValue(); 
     287    int sizeC = omeMeta.getPixelsSizeC(series).getValue().intValue(); 
     288    int sizeT = omeMeta.getPixelsSizeT(series).getValue().intValue(); 
     289 
     290    int imageCount = getPlaneCount(); 
     291    int ifdCount = seriesMap.size(); 
     292 
     293    if (imageCount == 0) { 
     294      omeMeta.setTiffDataPlaneCount(new NonNegativeInteger(0), series, 0); 
     295      return; 
     296    } 
     297 
     298    PositiveInteger samplesPerPixel = 
     299      new PositiveInteger((sizeZ * sizeC * sizeT) / imageCount); 
     300    for (int c=0; c<omeMeta.getChannelCount(series); c++) { 
     301      omeMeta.setChannelSamplesPerPixel(samplesPerPixel, series, c); 
     302    } 
     303    sizeC /= samplesPerPixel.getValue(); 
     304 
     305    HashMap<String, Integer> ifdCounts = new HashMap<String, Integer>(); 
     306 
     307    for (int plane=0; plane<imageCount; plane++) { 
     308      int[] zct = FormatTools.getZCTCoords(dimensionOrder, 
     309        sizeZ, sizeC, sizeT, imageCount, plane); 
     310      String filename = new Location(imageLocations[series][plane]).getName(); 
     311 
     312      Integer ifdIndex = ifdCounts.get(filename); 
     313      int ifd = ifdIndex == null ? 0 : ifdIndex.intValue(); 
     314 
     315      omeMeta.setUUIDFileName(filename, series, plane); 
     316      String uuid = "urn:uuid:" + getUUID(filename); 
     317      omeMeta.setUUIDValue(uuid, series, plane); 
     318 
     319      // fill in any non-default TiffData attributes 
     320      populateTiffData(omeMeta, zct, ifd, series, plane); 
     321      ifdCounts.put(filename, ifd + 1); 
     322    } 
     323  } 
     324 
    295325} 
  • branches/4.2/components/bio-formats/src/loci/formats/tiff

  • branches/4.2/components/bio-formats/test/loci/formats/utests

  • branches/4.2/components/loci-plugins

  • branches/4.2/components/loci-plugins/src/loci/plugins

  • branches/4.2/components/loci-plugins/src/loci/plugins/in/Calibrator.java

  • branches/4.2/components/ome-xml

Note: See TracChangeset for help on using the changeset viewer.