Class LZW
LZW compression recognizes sequences that have already appeared in the data, and replaces each later appearance by a unique code. The degree of compression depends on how much repetition is found in the data. LZW compression is effective with data having many and / or lengthy repeat sequences, such as text, and graphics with areas of solid color or patterns.
The sequence history should allow for the expected amount of repetition, without wasting too much space in each code on a history index. This version is fixed at 12 bit codes, which can refer to nearly a 2^^12 item sequence history, less some internal overhead. This size is considered good for short message blocks, and is simple to implement.
Note! Other versions of LZW may use other sizes of sequence history, and other coding conventions. They are not inter-operable with this algorithm.
The original papers:
- Terry Welch, A Technique for High-Performance Data Compression, IEEE Computer, June 1984
- J. Ziv and A. Lempel, A Universal Algorithm for Sequential Data Compression, IEEE Transactions on Information Theory, May 1977
- One form of the original LZ78 algorithm was patented (4,464,650) by its authors Lempel, Ziv, Cohn and Eastman. This patent is owned by Unisys.
- The LZW algorithm used in 'compress' is patented, both by IBM (4,814,746) and Unisys (4,558,302).
There is no need to construct an LZW object, the LZW.Compress () and LZW.Expand () methods are static.
- Version:
- $Revision: 1.2 $
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
Compress
(byte[] inRaw, int validCount, CodeOutputPacker outComp) Compress a sequence of raw data bytes.static int
Compress
(byte[] inRaw, CodeOutputPacker outComp) Compress a sequence of raw data bytes.static int
Compress
(InputStream inRaw, CodeOutputPacker outComp) static int
Expand
(CodeInputUnpacker inComp, byte[] outExp) Expand Compressed codes back to raw data.static int
Expand
(CodeInputUnpacker inComp, OutputStream outExp) Expand Compressed codes back to raw data.static void
For test and demonstration.
-
Constructor Details
-
LZW
public LZW()
-
-
Method Details
-
Compress
- Throws:
IOException
-
Compress
Compress a sequence of raw data bytes.- Parameters:
inRaw
- array of data bytes to be compressed,outComp
- packer for destination for compressed data, either to a stream or an array.- Returns:
- number of raw data bytes compressed.
- Throws:
IOException
- from underlying stream.
-
Compress
public static int Compress(byte[] inRaw, int validCount, CodeOutputPacker outComp) throws IOException Compress a sequence of raw data bytes.- Parameters:
inRaw
- array of data bytes to be compressed,validCount
- how much of inRaw array to be compressed,outComp
- packer for destination for compressed data, either to a stream or an array.- Returns:
- number of raw data bytes compressed.
- Throws:
IOException
- from underlying stream.
-
Expand
public static int Expand(CodeInputUnpacker inComp, OutputStream outExp) throws IOException, IllegalArgumentException Expand Compressed codes back to raw data. This will fail if input was not LZW compressed data.- Parameters:
inComp
- unpacker from source.outExp
- destination for decoded bytes.- Returns:
- count of decoded raw data bytes.
- Throws:
IOException
- from either underlying stream.IllegalArgumentException
- if input was not LZW compressed data.
-
Expand
public static int Expand(CodeInputUnpacker inComp, byte[] outExp) throws IOException, IllegalArgumentException Expand Compressed codes back to raw data. This will fail if input was not LZW compressed data.- Parameters:
inComp
- unpacker from source.outExp
- destination for decoded bytes.- Returns:
- count of decoded raw data bytes.
- Throws:
IOException
- from either underlying stream.IllegalArgumentException
- if input was not LZW compressed data.
-
main
For test and demonstration. From a built-in string and from any named files. Test files may be text or binary, but be prepared for the binary output.- Parameters:
argv
-
-