use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.
the class ImageConverter method testConvert.
// -- Utility methods --
/**
* A utility method for converting a file from the command line.
*/
public boolean testConvert(IFormatWriter writer, String[] args) throws FormatException, IOException {
nextOutputIndex.clear();
options.setValidate(validate);
writer.setMetadataOptions(options);
firstTile = true;
boolean success = parseArgs(args);
if (!success) {
return false;
}
if (printVersion) {
CommandLineTools.printVersion();
return true;
}
CommandLineTools.runUpgradeCheck(args);
if (in == null || out == null) {
printUsage();
return false;
}
if (new Location(out).exists()) {
if (overwrite == null) {
LOGGER.warn("Output file {} exists.", out);
LOGGER.warn("Do you want to overwrite it? ([y]/n)");
BufferedReader r = new BufferedReader(new InputStreamReader(System.in, Constants.ENCODING));
String choice = r.readLine().trim().toLowerCase();
overwrite = !choice.startsWith("n");
}
if (!overwrite) {
LOGGER.warn("Exiting; next time, please specify an output file that " + "does not exist.");
return false;
} else {
new Location(out).delete();
}
}
if (map != null)
Location.mapId(in, map);
long start = System.currentTimeMillis();
LOGGER.info(in);
reader = new ImageReader();
if (stitch) {
reader = new FileStitcher(reader);
Location f = new Location(in);
String pat = null;
if (!f.exists()) {
pat = in;
} else {
pat = FilePattern.findPattern(f);
}
if (pat != null)
in = pat;
}
if (separate)
reader = new ChannelSeparator(reader);
if (merge)
reader = new ChannelMerger(reader);
if (fill)
reader = new ChannelFiller(reader);
minMax = null;
if (autoscale) {
reader = new MinMaxCalculator(reader);
minMax = (MinMaxCalculator) reader;
}
reader.setMetadataOptions(options);
reader.setGroupFiles(group);
reader.setMetadataFiltered(true);
reader.setOriginalMetadataPopulated(true);
OMEXMLService service = null;
try {
ServiceFactory factory = new ServiceFactory();
service = factory.getInstance(OMEXMLService.class);
reader.setMetadataStore(service.createOMEXMLMetadata());
} catch (DependencyException de) {
throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
} catch (ServiceException se) {
throw new FormatException(se);
}
reader.setId(in);
MetadataStore store = reader.getMetadataStore();
MetadataTools.populatePixels(store, reader, false, false);
boolean dimensionsSet = true;
if (width == 0 || height == 0) {
// otherwise default to series 0
if (series >= 0) {
reader.setSeries(series);
}
width = reader.getSizeX();
height = reader.getSizeY();
dimensionsSet = false;
}
if (channel >= reader.getEffectiveSizeC()) {
throw new FormatException("Invalid channel '" + channel + "' (" + reader.getEffectiveSizeC() + " channels in source file)");
}
if (timepoint >= reader.getSizeT()) {
throw new FormatException("Invalid timepoint '" + timepoint + "' (" + reader.getSizeT() + " timepoints in source file)");
}
if (zSection >= reader.getSizeZ()) {
throw new FormatException("Invalid Z section '" + zSection + "' (" + reader.getSizeZ() + " Z sections in source file)");
}
if (store instanceof MetadataRetrieve) {
try {
String xml = service.getOMEXML(service.asRetrieve(store));
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) store.getRoot();
IMetadata meta = service.createOMEXMLMetadata(xml);
if (series >= 0) {
Image exportImage = new Image(root.getImage(series));
Pixels exportPixels = new Pixels(root.getImage(series).getPixels());
exportImage.setPixels(exportPixels);
OMEXMLMetadataRoot newRoot = (OMEXMLMetadataRoot) meta.getRoot();
while (newRoot.sizeOfImageList() > 0) {
newRoot.removeImage(newRoot.getImage(0));
}
newRoot.addImage(exportImage);
meta.setRoot(newRoot);
meta.setPixelsSizeX(new PositiveInteger(width), 0);
meta.setPixelsSizeY(new PositiveInteger(height), 0);
if (autoscale) {
store.setPixelsType(PixelType.UINT8, 0);
}
if (channel >= 0) {
meta.setPixelsSizeC(new PositiveInteger(1), 0);
}
if (zSection >= 0) {
meta.setPixelsSizeZ(new PositiveInteger(1), 0);
}
if (timepoint >= 0) {
meta.setPixelsSizeT(new PositiveInteger(1), 0);
}
writer.setMetadataRetrieve((MetadataRetrieve) meta);
} else {
for (int i = 0; i < reader.getSeriesCount(); i++) {
meta.setPixelsSizeX(new PositiveInteger(width), 0);
meta.setPixelsSizeY(new PositiveInteger(height), 0);
if (autoscale) {
store.setPixelsType(PixelType.UINT8, i);
}
if (channel >= 0) {
meta.setPixelsSizeC(new PositiveInteger(1), 0);
}
if (zSection >= 0) {
meta.setPixelsSizeZ(new PositiveInteger(1), 0);
}
if (timepoint >= 0) {
meta.setPixelsSizeT(new PositiveInteger(1), 0);
}
}
writer.setMetadataRetrieve((MetadataRetrieve) meta);
}
} catch (ServiceException e) {
throw new FormatException(e);
}
}
writer.setWriteSequentially(true);
if (writer instanceof TiffWriter) {
((TiffWriter) writer).setBigTiff(bigtiff);
} else if (writer instanceof ImageWriter) {
IFormatWriter w = ((ImageWriter) writer).getWriter(out);
if (w instanceof TiffWriter) {
((TiffWriter) w).setBigTiff(bigtiff);
}
}
String format = writer.getFormat();
LOGGER.info("[{}] -> {} [{}]", new Object[] { reader.getFormat(), out, format });
long mid = System.currentTimeMillis();
int total = 0;
int num = writer.canDoStacks() ? reader.getSeriesCount() : 1;
long read = 0, write = 0;
int first = series == -1 ? 0 : series;
int last = series == -1 ? num : series + 1;
long timeLastLogged = System.currentTimeMillis();
for (int q = first; q < last; q++) {
reader.setSeries(q);
firstTile = true;
if (!dimensionsSet) {
width = reader.getSizeX();
height = reader.getSizeY();
}
int writerSeries = series == -1 ? q : 0;
writer.setSeries(writerSeries);
writer.setInterleaved(reader.isInterleaved() && !autoscale);
writer.setValidBitsPerPixel(reader.getBitsPerPixel());
int numImages = writer.canDoStacks() ? reader.getImageCount() : 1;
int startPlane = (int) Math.max(0, firstPlane);
int endPlane = (int) Math.min(numImages, lastPlane);
numImages = endPlane - startPlane;
if (channel >= 0) {
numImages /= reader.getEffectiveSizeC();
}
if (zSection >= 0) {
numImages /= reader.getSizeZ();
}
if (timepoint >= 0) {
numImages /= reader.getSizeT();
}
total += numImages;
int count = 0;
for (int i = startPlane; i < endPlane; i++) {
int[] coords = reader.getZCTCoords(i);
if ((zSection >= 0 && coords[0] != zSection) || (channel >= 0 && coords[1] != channel) || (timepoint >= 0 && coords[2] != timepoint)) {
continue;
}
String outputName = FormatTools.getFilename(q, i, reader, out, zeroPadding);
if (outputName.equals(FormatTools.getTileFilename(0, 0, 0, outputName))) {
writer.setId(outputName);
if (compression != null)
writer.setCompression(compression);
} else {
int tileNum = outputName.indexOf(FormatTools.TILE_NUM);
int tileX = outputName.indexOf(FormatTools.TILE_X);
int tileY = outputName.indexOf(FormatTools.TILE_Y);
if (tileNum < 0 && (tileX < 0 || tileY < 0)) {
throw new FormatException("Invalid file name pattern; " + FormatTools.TILE_NUM + " or both of " + FormatTools.TILE_X + " and " + FormatTools.TILE_Y + " must be specified.");
}
}
int outputIndex = 0;
if (nextOutputIndex.containsKey(outputName)) {
outputIndex = nextOutputIndex.get(outputName);
}
long s = System.currentTimeMillis();
long m = convertPlane(writer, i, outputIndex, outputName);
long e = System.currentTimeMillis();
read += m - s;
write += e - m;
nextOutputIndex.put(outputName, outputIndex + 1);
if (i == endPlane - 1) {
nextOutputIndex.remove(outputName);
}
// log number of planes processed every second or so
if (count == numImages - 1 || (e - timeLastLogged) / 1000 > 0) {
int current = (count - startPlane) + 1;
int percent = 100 * current / numImages;
StringBuilder sb = new StringBuilder();
sb.append("\t");
int numSeries = last - first;
if (numSeries > 1) {
sb.append("Series ");
sb.append(q);
sb.append(": converted ");
} else
sb.append("Converted ");
LOGGER.info(sb.toString() + "{}/{} planes ({}%)", new Object[] { current, numImages, percent });
timeLastLogged = e;
}
count++;
}
}
writer.close();
long end = System.currentTimeMillis();
LOGGER.info("[done]");
// output timing results
float sec = (end - start) / 1000f;
long initial = mid - start;
float readAvg = (float) read / total;
float writeAvg = (float) write / total;
LOGGER.info("{}s elapsed ({}+{}ms per plane, {}ms overhead)", new Object[] { sec, readAvg, writeAvg, initial });
return true;
}
use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.
the class ImageInfo method printOMEXML.
public void printOMEXML() throws MissingLibraryException, ServiceException {
LOGGER.info("");
MetadataStore ms = reader.getMetadataStore();
if (baseReader instanceof ImageReader) {
baseReader = ((ImageReader) baseReader).getReader();
}
OMEXMLService service;
try {
ServiceFactory factory = new ServiceFactory();
service = factory.getInstance(OMEXMLService.class);
} catch (DependencyException de) {
throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
}
String version = service.getOMEXMLVersion(ms);
if (version == null)
LOGGER.info("Generating OME-XML");
else {
LOGGER.info("Generating OME-XML (schema version {})", version);
}
if (ms instanceof MetadataRetrieve) {
if (omexmlOnly) {
DebugTools.setRootLevel("INFO");
}
String xml = service.getOMEXML((MetadataRetrieve) ms);
LOGGER.info("{}", XMLTools.indentXML(xml, xmlSpaces, true));
if (omexmlOnly) {
DebugTools.setRootLevel("OFF");
}
} else {
LOGGER.info("The metadata could not be converted to OME-XML.");
if (omexmlVersion == null) {
LOGGER.info("The OME-XML Java library is probably not available.");
} else {
LOGGER.info("{} is probably not a legal schema version.", omexmlVersion);
}
}
}
use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.
the class FormatTools method convert.
/**
* Convenience method for writing all of the images and metadata obtained
* from the specified IFormatReader into the specified IFormatWriter.
*
* It is required that setId(String) be called on the IFormatReader
* object before it is passed to convert(...). setMetadataStore(...)
* should also have been called with an appropriate instance of IMetadata.
*
* The setId(String) method must not be called on the IFormatWriter
* object; this is taken care of internally. Additionally, the
* setMetadataRetrieve(...) method in IFormatWriter should not be called.
*
* @param input the pre-initialized IFormatReader used for reading data.
* @param output the uninitialized IFormatWriter used for writing data.
* @param outputFile the full path name of the output file to be created.
* @throws FormatException if there is a general problem reading from or
* writing to one of the files.
* @throws IOException if there is an I/O-related error.
*/
public static void convert(IFormatReader input, IFormatWriter output, String outputFile) throws FormatException, IOException {
MetadataStore store = input.getMetadataStore();
MetadataRetrieve meta = null;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
meta = service.asRetrieve(store);
} catch (DependencyException de) {
throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
}
output.setMetadataRetrieve(meta);
output.setId(outputFile);
for (int series = 0; series < input.getSeriesCount(); series++) {
input.setSeries(series);
output.setSeries(series);
byte[] buf = new byte[getPlaneSize(input)];
for (int image = 0; image < input.getImageCount(); image++) {
input.openBytes(image, buf);
output.saveBytes(image, buf);
}
}
input.close();
output.close();
}
use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.
the class FormatWriterTest method testWriterConsistency.
// -- Tests --
@Test(groups = { "all" }, dataProvider = "getWriterList")
public void testWriterConsistency(IFormatWriter writer) {
String testName = TestTools.shortClassName(writer) + " " + writer.getCompression() + " testWriterConsistency";
boolean success = true;
String msg = null;
try {
reader.close();
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
reader.setMetadataStore(service.createOMEXMLMetadata());
reader.setId(id);
int type = reader.getPixelType();
if (!writer.isSupportedType(type)) {
success = true;
result(testName, success, msg);
return;
}
config = configTree.get(id);
String prefix = id.substring(id.lastIndexOf(File.separator) + 1, id.lastIndexOf("."));
// will throw an exception
while (prefix.length() < 3) prefix = "x" + prefix;
String suffix = "." + writer.getSuffixes()[0];
File tmpFile = File.createTempFile(prefix, suffix);
tmpFile.deleteOnExit();
String convertedFile = tmpFile.getAbsolutePath();
IMetadata meta = (IMetadata) reader.getMetadataStore();
writer.close();
writer.setMetadataRetrieve((MetadataRetrieve) meta);
// convert the input file
writer.setId(convertedFile);
int seriesCount = writer.canDoStacks() ? reader.getSeriesCount() : 1;
for (int series = 0; series < seriesCount; series++) {
reader.setSeries(series);
writer.setSeries(series);
int imageCount = writer.canDoStacks() ? reader.getImageCount() : 1;
for (int image = 0; image < imageCount; image++) {
writer.saveBytes(image, reader.openBytes(image));
}
}
writer.close();
// verify that the dimensions are accurate
convertedReader.setId(convertedFile);
boolean seriesMatch = convertedReader.getSeriesCount() == config.getSeriesCount();
boolean expectRGB = config.isRGB();
int expectedCount = config.getSizeZ() * config.getSizeT() * (expectRGB ? 1 : config.getSizeC());
boolean imageMatch = convertedReader.getImageCount() == expectedCount;
if (!seriesMatch && writer.canDoStacks()) {
int totalImages = 0;
for (int i = 0; i < reader.getSeriesCount(); i++) {
reader.setSeries(i);
totalImages += reader.getImageCount();
}
reader.setSeries(0);
if (convertedReader.getImageCount() != totalImages) {
success = false;
msg = "Series counts do not match (found " + convertedReader.getSeriesCount() + ", expected " + config.getSeriesCount() + ")";
} else
imageMatch = true;
}
if (success) {
for (int series = 0; series < seriesCount; series++) {
if (series >= convertedReader.getSeriesCount()) {
break;
}
convertedReader.setSeries(series);
config.setSeries(series);
int expectedX = config.getSizeX();
int expectedY = config.getSizeY();
expectRGB = config.isRGB();
if (TestTools.shortClassName(writer).equals("OMEXMLWriter")) {
expectRGB = false;
} else if (TestTools.shortClassName(writer).equals("JPEGWriter")) {
expectRGB = expectRGB || config.isIndexed();
}
int expectedPixelType = FormatTools.pixelTypeFromString(config.getPixelType());
expectedCount = config.getSizeZ() * config.getSizeT() * (expectRGB ? 1 : config.getSizeC());
String expectedMD5 = config.getMD5();
int x = convertedReader.getSizeX();
int y = convertedReader.getSizeY();
int count = convertedReader.getImageCount();
boolean rgb = convertedReader.isRGB();
int pixelType = convertedReader.getPixelType();
boolean isQuicktime = TestTools.shortClassName(writer).equals("QTWriter");
String md5 = TestTools.md5(convertedReader.openBytes(0));
if (msg == null)
msg = checkMismatch(x, expectedX, series, "X");
if (msg == null)
msg = checkMismatch(y, expectedY, series, "Y");
if (msg == null && writer.canDoStacks() && !imageMatch) {
msg = checkMismatch(count, expectedCount, series, "Image count");
}
if (msg == null && !isQuicktime) {
msg = checkMismatch(rgb, expectRGB, series, "RGB");
}
if (msg == null && !isQuicktime) {
msg = checkMismatch(pixelType, expectedPixelType, series, "Pixel type");
}
if (msg == null && isLosslessWriter(writer) && config.isRGB() == expectRGB) {
msg = checkMismatch(md5, expectedMD5, series, "Pixels hash");
}
success = msg == null;
if (!success)
break;
}
}
convertedReader.close();
} catch (Throwable t) {
LOGGER.info("", t);
success = false;
}
result(testName, success, msg);
}
use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.
the class Jpeg2000GrindTest method initializeWriter.
/**
* Initializes the writer.
* @param output The file where to write the compressed data.
* @param compression The compression to use.
* @param bigTiff Pass <code>true</code> to set the <code>bigTiff</code>
* flag, <code>false</code> otherwise.
* @throws Exception Thrown if an error occurred.
*/
private void initializeWriter(String output, String compression, boolean bigTiff) throws Exception {
ServiceFactory sf = new ServiceFactory();
OMEXMLService service = sf.getInstance(OMEXMLService.class);
IMetadata metadata = service.createOMEXMLMetadata();
metadata.setImageID("Image:0", 0);
metadata.setPixelsID("Pixels:0", 0);
metadata.setPixelsBinDataBigEndian(true, 0, 0);
metadata.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);
metadata.setPixelsType(ome.xml.model.enums.PixelType.fromString(PIXEL_TYPE), 0);
metadata.setPixelsSizeX(new PositiveInteger(SIZE_X), 0);
metadata.setPixelsSizeY(new PositiveInteger(SIZE_Y), 0);
metadata.setPixelsSizeZ(new PositiveInteger(1), 0);
metadata.setPixelsSizeC(new PositiveInteger(1), 0);
metadata.setPixelsSizeT(new PositiveInteger(SIZE_Z * SIZE_C * SIZE_T), 0);
metadata.setChannelID("Channel:0", 0, 0);
metadata.setChannelSamplesPerPixel(new PositiveInteger(1), 0, 0);
writer = new TiffWriter();
writer.setMetadataRetrieve(metadata);
writer.setCompression(compression);
writer.setWriteSequentially(false);
writer.setInterleaved(true);
writer.setBigTiff(bigTiff);
writer.setId(output);
bytesPerPixel = FormatTools.getBytesPerPixel(PIXEL_TYPE);
}
Aggregations