Skip to content

Commit

Permalink
Add -quality option to bfconvert
Browse files Browse the repository at this point in the history
This primarily affects JPEG compression with TIFF, OME-TIFF, and DICOM output.

Fixes ome#3370.
  • Loading branch information
melissalinkert committed Mar 25, 2024
1 parent 8bcb3c3 commit 303b3d5
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import loci.formats.MetadataTools;
import loci.formats.MinMaxCalculator;
import loci.formats.MissingLibraryException;
import loci.formats.codec.CodecOptions;
import loci.formats.gui.Index16ColorModel;
import loci.formats.in.DynamicMetadataOptions;
import loci.formats.meta.IMetadata;
Expand Down Expand Up @@ -133,6 +134,8 @@ public final class ImageConverter {
private boolean precompressed = false;
private boolean tryPrecompressed = false;

private Double compressionQuality = null;

private String extraMetadata = null;

private IFormatReader reader;
Expand Down Expand Up @@ -273,6 +276,9 @@ else if (args[i].equals("-fill")) {
else if (args[i].equals("-extra-metadata")) {
extraMetadata = args[++i];
}
else if (args[i].equals("-quality")) {
compressionQuality = DataTools.parseDouble(args[++i]);
}
else if (!args[i].equals(CommandLineTools.NO_UPGRADE_CHECK)) {
LOGGER.error("Found unknown command flag: {}; exiting.", args[i]);
return false;
Expand Down Expand Up @@ -349,7 +355,7 @@ private void printUsage() {
" [-option key value] [-novalid] [-validate] [-tilex tileSizeX]",
" [-tiley tileSizeY] [-pyramid-scale scale]",
" [-swap dimensionsOrderString] [-fill color]",
" [-precompressed]",
" [-precompressed] [-quality compressionQuality]",
" [-pyramid-resolutions numResolutionLevels] in_file out_file",
"",
" -version: print the library version and exit",
Expand Down Expand Up @@ -398,6 +404,7 @@ private void printUsage() {
" Most input and output formats do not support this option.",
" Do not use -crop, -fill, or -autoscale, or pyramid generation options",
" with this option.",
" -quality: double quality value for JPEG compression (0-1)",
"",
"The extension of the output file specifies the file format to use",
"for the conversion. The list of available formats and extensions is:",
Expand Down Expand Up @@ -774,6 +781,7 @@ else if (saveTileWidth > 0 && saveTileHeight > 0) {
if (!ok) {
return false;
}
setCodecOptions(writer);
writer.setId(outputName);
if (compression != null) writer.setCompression(compression);
}
Expand All @@ -793,6 +801,7 @@ else if (saveTileWidth > 0 && saveTileHeight > 0) {
if (!ok) {
return false;
}
setCodecOptions(writer);
writer.setId(tileName);
if (compression != null) writer.setCompression(compression);
}
Expand Down Expand Up @@ -995,6 +1004,7 @@ private long convertTilePlane(IFormatWriter writer, int index, int outputIndex,
writer.setMetadataRetrieve(retrieve);

overwriteCheck(tileName, true);
setCodecOptions(writer);
writer.setId(tileName);
if (compression != null) writer.setCompression(compression);

Expand Down Expand Up @@ -1267,6 +1277,14 @@ private boolean overwriteCheck(String path, boolean throwOnExist) throws IOExcep
return true;
}

private void setCodecOptions(IFormatWriter writer) {
if (compressionQuality != null) {
CodecOptions codecOptions = CodecOptions.getDefaultOptions();
codecOptions.quality = compressionQuality;
writer.setCodecOptions(codecOptions);
}
}

// -- Main method --

public static void main(String[] args) throws FormatException, IOException {
Expand Down

0 comments on commit 303b3d5

Please sign in to comment.