use of qupath.lib.images.writers.ome.OMEPyramidWriter.Builder in project qupath by qupath.
the class ConvertCommand method run.
@Override
public void run() {
long startTime = System.currentTimeMillis();
try {
if (inputFile == null || outputFile == null)
throw new IOException("Incorrect given path(s)");
} catch (IOException e) {
logger.error(e.getLocalizedMessage());
return;
}
// Change name if not ending with .ome.tif
if (!outputFile.getAbsolutePath().toLowerCase().endsWith(".ome.tif"))
outputFile = new File(outputFile.getParentFile(), GeneralTools.getNameWithoutExtension(outputFile) + ".ome.tif");
if (outputFile.exists() && !overwrite) {
logger.error("Output file " + outputFile + " exists!");
return;
}
if (inputFile.equals(outputFile)) {
logger.error("Input and output files are the same!");
return;
}
String[] args;
if (series >= 0)
args = new String[] { "--classname", BioFormatsServerBuilder.class.getName(), "--series", Integer.toString(series) };
else
args = new String[0];
createTileCache();
try (ImageServer<BufferedImage> server = ImageServers.buildServer(inputFile.toURI(), args)) {
// Get compression from user (or CompressionType.DEFAULT)
// CompressionType compressionType = stringToCompressionType(compression);
CompressionType compressionType = compression;
// Check that compression is compatible with image
if (!Arrays.stream(CompressionType.values()).filter(c -> c.supportsImage(server)).anyMatch(c -> c == compressionType)) {
logger.error("Chosen compression " + compressionType.toString() + " is not compatible with the input image.");
}
if (tileSize > -1) {
tileWidth = tileSize;
tileHeight = tileSize;
}
Builder builder = new OMEPyramidWriter.Builder(server).compression(compressionType).tileSize(tileWidth, tileHeight).parallelize(parallelize);
if (bigTiff != null)
builder = builder.bigTiff(bigTiff.booleanValue());
// Make pyramidal, if requested
if (downsample < 1)
downsample = server.getDownsampleForResolution(0);
if (pyramid > 1)
builder.scaledDownsampling(downsample, pyramid);
else
builder.downsamples(downsample);
String patternRange = "(\\d+)-(\\d+)";
String patternInteger = "\\d+";
// Parse z-slices, remembering to convert from 1-based (inclusive) to 0-based (upper value exclusive) indexing
if (zSlices == null || zSlices.isBlank() || "all".equals(zSlices)) {
builder.allZSlices();
} else if (zSlices.matches(patternRange)) {
int zStart = Integer.parseInt(zSlices.substring(0, zSlices.indexOf("-")));
int zEnd = Integer.parseInt(zSlices.substring(zSlices.indexOf("-") + 1));
if (zEnd == zStart)
builder.zSlice(zStart - 1);
else if (zStart > zEnd) {
logger.error("Invalid range of --zslices (must be ascending): " + zSlices);
return;
} else
builder.zSlices(zStart - 1, zEnd);
} else if (zSlices.matches(patternInteger)) {
int z = Integer.parseInt(zSlices);
builder.zSlice(z - 1);
} else {
logger.error("Unknown value for --zslices: " + zSlices);
return;
}
// Parse timepoints, remembering to convert from 1-based (inclusive) to 0-based (upper value exclusive) indexing
if ("all".equals(timepoints)) {
builder.allTimePoints();
} else if (timepoints.matches(patternRange)) {
int tStart = Integer.parseInt(timepoints.substring(0, timepoints.indexOf("-")));
int tEnd = Integer.parseInt(timepoints.substring(timepoints.indexOf("-") + 1));
if (tStart == tEnd)
builder.timePoint(tStart - 1);
else if (tStart > tEnd) {
logger.error("Invalid range of --timepoints (must be ascending): " + timepoints);
return;
} else
builder.timePoints(tStart - 1, tEnd);
} else if (timepoints.matches(patternInteger)) {
int t = Integer.parseInt(timepoints);
builder.timePoint(t - 1);
} else {
logger.error("Unknown value for --timepoints: " + timepoints);
return;
}
// Parse the bounding box, if required
if (crop != null && !crop.isBlank()) {
var matcher = Pattern.compile("(\\d+),(\\d+),(\\d+),(\\d+)").matcher(crop);
if (matcher.matches()) {
int x = Integer.parseInt(matcher.group(1));
int y = Integer.parseInt(matcher.group(2));
int w = Integer.parseInt(matcher.group(3));
int h = Integer.parseInt(matcher.group(4));
builder.region(x, y, w, h);
} else {
logger.error("Unknown value for --crop: " + crop);
return;
}
}
builder.build().writeSeries(outputFile.getPath());
long duration = System.currentTimeMillis() - startTime;
logger.info(String.format("%s written in %.1f seconds", outputFile.getAbsolutePath(), duration / 1000.0));
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
}
Aggregations