Package ome.codecs
Class LZWCodec
java.lang.Object
ome.codecs.BaseCodec
ome.codecs.LZWCodec
- All Implemented Interfaces:
Codec
This is an optimized LZW codec for use with TIFF files.
Most of the code is inlined, and specifics of TIFF usage of LZW
(known size of decompressor output; possible lengths of LZW codes; specified
values for
CLEAR and END_OF_INFORMATION codes)
are taken in account.
Estimating the worst-case size of compressor output:
- The worst case means that there is no compression at all, and every input byte generates code to output.
- This means that the LZW table will be full (and reset) after reading each portion of 4096-256-2-1=3837 bytes of input (first 256 codes are preallocated, 2 codes are used for CLEAR and END_OF_IFORMATION, 1 code is lost due to original bug in TIFF library that now is a feature).
- Each full portion of 3837 byte will produce in output:
- 9 bits for CLEAR code;
- 9*253 + 10*512 + 11*1024 + 12*2048 = 43237 bits for character codes.
- Let n=3837, m=(number of bytes in the last incomplete portion),
N=(number of bytes in compressed complete portion with CLEAR code),
M=(number of bytes in compressed last incomplete portion).
We have inequalities:
- N <= 1.41 * n
- M <= 1.41 * m
- The last incomplete portion should also include CLEAR and END_OF_INFORMATION codes; they occupy less than 3 bytes.
- Author:
- Mikhail Kovtun mikhail.kovtun at duke.edu
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]compress(byte[] input, CodecOptions options) Compresses a block of data.compress(ByteBuffer output, byte[] input, CodecOptions options) Compress using ByteBuffers.byte[]decompress(loci.common.RandomAccessInputStream in, CodecOptions options) The CodecOptions parameter should have the following fields set:maxBytesMethods inherited from class ome.codecs.BaseCodec
compress, decompress, decompress, decompress, decompress, test
-
Constructor Details
-
LZWCodec
public LZWCodec()
-
-
Method Details
-
compress
Description copied from interface:CodecCompresses a block of data.- Parameters:
input- The data to be compressed.options- Options to be used during compression, if appropriate.- Returns:
- The compressed data.
- Throws:
CodecException- If input is not a compressed data block of the appropriate type.
-
compress
public ByteBuffer compress(ByteBuffer output, byte[] input, CodecOptions options) throws CodecException Description copied from class:BaseCodecCompress using ByteBuffers. This implementation just calls the byte array based compression method and wraps the result properly.- Specified by:
compressin interfaceCodec- Overrides:
compressin classBaseCodec- Parameters:
output- A preallocated ByteBuffer that may be used for the compressed data. This may benullto force the compressor to allocate a fresh buffer.input- The data to be compressed.options- Options to be used during compression, if appropriate.- Returns:
- The ByteBuffer holding the compressed data.
- Throws:
CodecException- If input cannot be processed.- See Also:
-
decompress
public byte[] decompress(loci.common.RandomAccessInputStream in, CodecOptions options) throws CodecException, IOException The CodecOptions parameter should have the following fields set:maxBytes- Specified by:
decompressin interfaceCodec- Specified by:
decompressin classBaseCodec- Parameters:
in- The stream from which to read compressed data.options- Options to be used during decompression.- Returns:
- The decompressed data.
- Throws:
CodecException- If data is not valid compressed data for this decompressor.IOException- See Also:
-