Package ome.codecs
Class LZWCodec
- java.lang.Object
-
- ome.codecs.BaseCodec
-
- ome.codecs.LZWCodec
-
- All Implemented Interfaces:
Codec
public class LZWCodec extends BaseCodec
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 forCLEAR
andEND_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
-
-
Constructor Summary
Constructors Constructor Description LZWCodec()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
compress(byte[] input, CodecOptions options)
Compresses a block of data.ByteBuffer
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:maxBytes
-
Methods inherited from class ome.codecs.BaseCodec
compress, decompress, decompress, decompress, decompress, test
-
-
-
-
Method Detail
-
compress
public byte[] compress(byte[] input, CodecOptions options) throws CodecException
Description copied from interface:Codec
Compresses 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:BaseCodec
Compress using ByteBuffers. This implementation just calls the byte array based compression method and wraps the result properly.- Specified by:
compress
in interfaceCodec
- Overrides:
compress
in classBaseCodec
- Parameters:
output
- A preallocated ByteBuffer that may be used for the compressed data. This may benull
to 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:
Codec.compress(ByteBuffer, byte[], CodecOptions)
-
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:
decompress
in interfaceCodec
- Specified by:
decompress
in 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:
Codec.decompress(RandomAccessInputStream, CodecOptions)
-
-