This page discusses some common operations related to the OME-TIFF format, and gives example source code in Java for performing them. We strongly recommend you read and understand the OME-TIFF specification before attempting to deploy any of the following code.
This section is out of date, and in need of update.
To extract a comment from a TIFF file without the aid of a TIFF library, the following steps are required:
LOCI's Bio-Formats library provides a lot of functionality related to OME-XML and OME-TIFF, to ease the burden of file format handling and conversions. Rather than offer source code that performs all of the above actions on its own, we instead offer examples that utilize Bio-Formats, for more robust and succinct operation.
The following program extracts comments from TIFF files, and outputs what it finds to the standard output stream. It requires the Bio-Formats library (bio-formats.jar).
The key lines are the construction of a
loci.formats.RandomAccessStream around the file, acquisition
of the first IFD as a Hashtable object with
TiffTools.getFirstIFD(in) and extraction of the comment string
using TiffTools.getIFDValue(ifd, TiffTools.IMAGE_DESCRIPTION).
Modifying a TIFF comment can be tricky because the length of the altered OME-XML string is unlikely to be the same as before. As such, the IFD's ImageDescription directory entry must be updated to reflect the new byte count. In addition, if the string is longer than before, it will no longer fit at its old offset, unless the comment was at the end of the file, so the entry's offset might need to change as well.
We have included a method within Bio-Formats,
TiffTools.overwriteIFDValue(), that efficiently alters a
directory entry with a minimum of waste. The count field of the entry is
intelligently updated to match the new length. If the new length is longer
than the old length, it appends the new data to the end of the file and
updates the offset field; if not, or if the old data is already at the end
of the file, it overwrites the old data in place.
The following program extracts comments from TIFF files, prompts the user to alter the comments on the command line, and writes updated comments back to the files. It requires the Bio-Formats library (bio-formats.jar).
Similar to the TiffComment.java example above, the comment is acquired
using TiffTools.getFirstIFD(fin). The major addition to this
example is the use of TiffTools.overwriteComment(args[i], xml)
to update the comment string.
One of the major goals of Bio-Formats is to standardize the metadata from all supported third-party formats into OME-XML. Doing so makes conversion to OME-TIFF very straightforward—just write the pixels to TIFF however you want (e.g., with libtiff), and store the converted OME-XML metadata into the TIFF comment. The complicated part is doing the conversion from proprietary third-party metadata into OME-XML—a task that Bio-Formats greatly simplifies.
The following program converts the files given on the command line into OME-TIFF format. It requires the Bio-Formats library (bio-formats.jar), as well as the Java OME-XML library (ome-java.jar).
The code functions by creating an ImageReader for reading the
input files' image planes sequentially, and an OMETiffWriter
for writing the planes to OME-TIFF files on disk. The OME-XML is generated
by attaching an OMEXMLMetadata object to the reader, such that
when each file is initialized, the object (at heart an XML DOM object in
memory) is automatically populated with the converted metadata. The
OMEXMLMetadata object is then fed to the
OMETiffWriter, which extracts the appropriate OME-XML string
and embeds it into the OME-TIFF file properly.
While our ultimate goal is for the Bio-Formats metadata conversion facility to be a reference implementation for conversion of third-party formats into OME-XML and OME-TIFF, please be aware that the current code is a work in progress. We would greatly value suggestions and assistance regarding the OME-XML conversion relating to any specific format. If there is any metadata missing or converted incorrectly, please let us know.
In some cases, it is useful to extract specific parameters or tweak
certain values in a dataset's OME-XML metadata block. We are actively
developing a module, OME Notes, offering this
ability to end users in a graphical user interface. It uses Bio-Formats
OMEXMLMetadata objects (see the "Converting other formats to
OME-TIFF" example above), and its backing
org.openmicroscopy.xml.OMENode objects, which are
part of OME's Java OME-XML library.
Working with XML is a major topic unto itself; here we provide a brief
example of the OMEXMLMetadata class (which implements the
MetadataStore and MetadataRetrieve interfaces)
to greatly simplify OME-XML-related development tasks.
The following program edits the "image name" metadata value for the file given on the command line. It requires the Bio-Formats library (bio-formats.jar), as well as the Java OME-XML library (ome-java.jar).
As in the ConvertToOmeTiff.java example above, we attach an
OMEXMLMetadata object to the reader to extract OME-XML
metadata from the input file. We obtain the current image name (if any)
by calling the omexmlMeta.getImageName(zero) method. The zero
indicates the Image within the OME-XML block about which we are interested;
in this case, we are asking for the name of the first Image.
After updating the name somehow (in our case, reversing the string),
we write the updated name back into the metadata structure via a call to
omexmlMeta.setImage(name, null, null, zero). The nulls
indicate that no change should be made to those metadata fields, and once
again the zero indicates that we wish to update the first Image.
Lastly, the MetadataTools utility class contains a number of
useful methods for working with Bio-Formats metadata objects (i.e.,
MetadataStore and MetadataRetrieve
implementations), including the getOMEXML method for easily
extracting an OME-XML string from a MetadataRetrieve object
(which we utilize here), as well the convertMetadata method
for transcoding between metadata object implementations.