Skip to content

Commit

Permalink
If -precompressed specified without -compression, use reader comp…
Browse files Browse the repository at this point in the history
…ression type automatically

This doesn't yet completely support mixed compression types within the same dataset, but something like:

$ bfconvert -noflat -precompressed CMU-1.svs CMU-1.dcm

should run without error and convert the pyramid without recompressing.
  • Loading branch information
melissalinkert committed Jun 16, 2024
1 parent 6136fb3 commit 93e38d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import loci.formats.FileStitcher;
import loci.formats.FormatException;
import loci.formats.FormatTools;
import loci.formats.ICompressedTileReader;
import loci.formats.IFormatReader;
import loci.formats.IFormatWriter;
import loci.formats.ImageReader;
Expand All @@ -71,7 +72,9 @@
import loci.formats.MetadataTools;
import loci.formats.MinMaxCalculator;
import loci.formats.MissingLibraryException;
import loci.formats.codec.Codec;
import loci.formats.codec.CodecOptions;
import loci.formats.codec.CompressionType;
import loci.formats.codec.JPEG2000CodecOptions;
import loci.formats.gui.Index16ColorModel;
import loci.formats.in.DynamicMetadataOptions;
Expand Down Expand Up @@ -558,6 +561,11 @@ public boolean testConvert(IFormatWriter writer, String[] args)

reader.setId(in);

if (compression == null && precompressed) {
compression = getReaderCodecName();
LOGGER.info("Implicitly using compression = {}", compression);
}

if (swapOrder != null) {
dimSwapper.swapDimensions(swapOrder);
}
Expand Down Expand Up @@ -1308,6 +1316,18 @@ private void setCodecOptions(IFormatWriter writer) {
}
}

private String getReaderCodecName() throws FormatException, IOException {
if (reader instanceof ICompressedTileReader) {
ICompressedTileReader r = (ICompressedTileReader) reader;
Codec c = r.getTileCodec(0);
CompressionType type = CompressionType.get(c);
if (type != null) {
return type.getCompression();
}
}
return null;
}

// -- 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 @@ -115,5 +115,27 @@ public int getCode() {
public String getCompression() {
return compression;
}


/**
* Look up the compression type by Codec instance.
*/
public static CompressionType get(Codec c) {
if (c instanceof ZlibCodec) {
return ZLIB;
}
if (c instanceof LZWCodec) {
return LZW;
}
if (c instanceof JPEGCodec) {
return JPEG;
}
if (c instanceof JPEG2000Codec) {
return J2K;
}
if (c instanceof PassthroughCodec) {
return UNCOMPRESSED;
}
return null;
}

}

0 comments on commit 93e38d8

Please sign in to comment.