Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -quality option to bfconvert #4171

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
import loci.formats.MetadataTools;
import loci.formats.MinMaxCalculator;
import loci.formats.MissingLibraryException;
import loci.formats.codec.CodecOptions;
import loci.formats.codec.JPEG2000CodecOptions;
import loci.formats.gui.Index16ColorModel;
import loci.formats.in.DynamicMetadataOptions;
import loci.formats.meta.IMetadata;
Expand Down Expand Up @@ -133,6 +135,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 +277,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 +356,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 +405,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 +782,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 +802,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 +1005,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 +1278,14 @@ private boolean overwriteCheck(String path, boolean throwOnExist) throws IOExcep
return true;
}

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

// -- Main method --

public static void main(String[] args) throws FormatException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ else if (x % thisTileWidth != 0 || y % thisTileHeight != 0 ||
}
else {
Codec codec = getCodec();
CodecOptions options = new CodecOptions();
CodecOptions options = new CodecOptions(getCodecOptions());
options.width = tileWidth[resolutionIndex];
options.height = tileHeight[resolutionIndex];
options.channels = getSamplesPerPixel();
Expand Down