Last active
June 16, 2017 15:58
-
-
Save simleo/7445f1fcdf969fdcc2fd9c8634199895 to your computer and use it in GitHub Desktop.
Create a companion XML file associated to a plain TIFF file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.UUID; | |
import java.io.PrintWriter; | |
import loci.formats.ImageReader; | |
import loci.formats.FormatTools; | |
import loci.formats.ome.OMEXMLMetadata; | |
import loci.formats.services.OMEXMLService; | |
import loci.common.Constants; | |
import loci.common.Location; | |
import loci.common.xml.XMLTools; | |
import loci.common.services.ServiceFactory; | |
import ome.xml.meta.OMEXMLMetadataRoot; | |
import ome.xml.model.primitives.NonNegativeInteger; | |
import org.slf4j.LoggerFactory; | |
import ch.qos.logback.classic.Level; | |
import ch.qos.logback.classic.Logger; | |
public final class CreateCompanion { | |
public static void main(String[] args) throws Exception { | |
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); | |
root.setLevel(Level.INFO); | |
//-- | |
String id = args[0]; | |
ImageReader reader = new ImageReader(); | |
ServiceFactory factory = new ServiceFactory(); | |
OMEXMLService service = factory.getInstance(OMEXMLService.class); | |
OMEXMLMetadata meta = service.createOMEXMLMetadata(); | |
reader.setOriginalMetadataPopulated(true); // should be optional | |
reader.setMetadataStore(meta); | |
reader.setId(id); | |
meta.resolveReferences(); | |
service.removeBinData(meta); | |
service.removeTiffData(meta); | |
String filename = new Location(id).getName(); | |
String uuid = "urn:uuid:" + UUID.randomUUID().toString(); | |
String targetUuid = "urn:uuid:" + UUID.randomUUID().toString(); | |
meta.setUUID(uuid); | |
OMEXMLMetadataRoot metaroot = (OMEXMLMetadataRoot) meta.getRoot(); | |
metaroot.setCreator(FormatTools.CREATOR); | |
for (int series = 0; series < meta.getImageCount(); series++) { | |
reader.setSeries(series); | |
int imageCount = reader.getImageCount(); | |
if (imageCount == 0) { | |
meta.setTiffDataPlaneCount(new NonNegativeInteger(0), series, 0); | |
continue; | |
} | |
for (int plane = 0; plane < imageCount; plane++) { | |
int[] zct = reader.getZCTCoords(plane); | |
int ifd = plane; // Single file | |
meta.setUUIDFileName(filename, series, plane); | |
meta.setUUIDValue(targetUuid, series, plane); | |
meta.setTiffDataFirstZ(new NonNegativeInteger(zct[0]), series, plane); | |
meta.setTiffDataFirstC(new NonNegativeInteger(zct[1]), series, plane); | |
meta.setTiffDataFirstT(new NonNegativeInteger(zct[2]), series, plane); | |
meta.setTiffDataIFD(new NonNegativeInteger(ifd), series, plane); | |
meta.setTiffDataPlaneCount(new NonNegativeInteger(1), series, plane); | |
} | |
} | |
String xml = service.getOMEXML(meta); | |
String companion = "test.companion.ome"; | |
PrintWriter out = new PrintWriter(companion, Constants.ENCODING); | |
out.println(XMLTools.indentXML(xml, true)); | |
out.close(); | |
reader.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment