Search in sources :

Example 1 with Builder

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);
    }
}
Also used : Arrays(java.util.Arrays) ImageServer(qupath.lib.images.servers.ImageServer) Logger(org.slf4j.Logger) Builder(qupath.lib.images.writers.ome.OMEPyramidWriter.Builder) BufferedImage(java.awt.image.BufferedImage) Parameters(picocli.CommandLine.Parameters) GeneralTools(qupath.lib.common.GeneralTools) Subcommand(qupath.lib.gui.extensions.Subcommand) ImageServerProvider(qupath.lib.images.servers.ImageServerProvider) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) BioFormatsServerBuilder(qupath.lib.images.servers.bioformats.BioFormatsServerBuilder) File(java.io.File) Option(picocli.CommandLine.Option) ImageRegionStoreFactory(qupath.lib.gui.images.stores.ImageRegionStoreFactory) Pattern(java.util.regex.Pattern) Command(picocli.CommandLine.Command) ImageServers(qupath.lib.images.servers.ImageServers) CompressionType(qupath.lib.images.writers.ome.OMEPyramidWriter.CompressionType) PathPrefs(qupath.lib.gui.prefs.PathPrefs) Builder(qupath.lib.images.writers.ome.OMEPyramidWriter.Builder) BioFormatsServerBuilder(qupath.lib.images.servers.bioformats.BioFormatsServerBuilder) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Builder(qupath.lib.images.writers.ome.OMEPyramidWriter.Builder) File(java.io.File) CompressionType(qupath.lib.images.writers.ome.OMEPyramidWriter.CompressionType)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Pattern (java.util.regex.Pattern)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Command (picocli.CommandLine.Command)1 Option (picocli.CommandLine.Option)1 Parameters (picocli.CommandLine.Parameters)1 GeneralTools (qupath.lib.common.GeneralTools)1 Subcommand (qupath.lib.gui.extensions.Subcommand)1 ImageRegionStoreFactory (qupath.lib.gui.images.stores.ImageRegionStoreFactory)1 PathPrefs (qupath.lib.gui.prefs.PathPrefs)1 ImageServer (qupath.lib.images.servers.ImageServer)1 ImageServerProvider (qupath.lib.images.servers.ImageServerProvider)1 ImageServers (qupath.lib.images.servers.ImageServers)1 BioFormatsServerBuilder (qupath.lib.images.servers.bioformats.BioFormatsServerBuilder)1 Builder (qupath.lib.images.writers.ome.OMEPyramidWriter.Builder)1 CompressionType (qupath.lib.images.writers.ome.OMEPyramidWriter.CompressionType)1