Xj3D VRML/X3D Code API

org.web3d.vrml.export.compressors
Class LZW

java.lang.Object
  extended byorg.web3d.vrml.export.compressors.LZW

public class LZW
extends java.lang.Object

The Limpel, Ziv, Welch (LZW) algorithm compresses any sequence of byte data. The resulting compressed codes are larger than bytes, but can each represent a group of bytes, at least some of the time, for what is usually an overall saving.

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:

This and many similar algorithms have been patented: This Java was ported from C code in the copyrighted article, "LZW Data Compression" by Mark Nelson, in Dr. Dobb's Journal October, 1989.

There is no need to construct an LZW object, the LZW.Compress () and LZW.Expand () methods are static.

Version:
$Revision: 1.2 $

Constructor Summary
LZW()
           
 
Method Summary
static int Compress(byte[] inRaw, CodeOutputPacker outComp)
          Compress a sequence of raw data bytes.
static int Compress(byte[] inRaw, int validCount, CodeOutputPacker outComp)
          Compress a sequence of raw data bytes.
static int Compress(java.io.InputStream inRaw, CodeOutputPacker outComp)
           
static int Expand(CodeInputUnpacker inComp, byte[] outExp)
          Expand Compressed codes back to raw data.
static int Expand(CodeInputUnpacker inComp, java.io.OutputStream outExp)
          Expand Compressed codes back to raw data.
static void main(java.lang.String[] argv)
          For test and demonstration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LZW

public LZW()
Method Detail

Compress

public static int Compress(java.io.InputStream inRaw,
                           CodeOutputPacker outComp)
                    throws java.io.IOException
Throws:
java.io.IOException

Compress

public static int Compress(byte[] inRaw,
                           CodeOutputPacker outComp)
                    throws java.io.IOException
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:
java.io.IOException - from underlying stream.

Compress

public static int Compress(byte[] inRaw,
                           int validCount,
                           CodeOutputPacker outComp)
                    throws java.io.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:
java.io.IOException - from underlying stream.

Expand

public static int Expand(CodeInputUnpacker inComp,
                         java.io.OutputStream outExp)
                  throws java.io.IOException,
                         java.lang.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:
java.io.IOException - from either underlying stream.
java.lang.IllegalArgumentException - if input was not LZW compressed data.

Expand

public static int Expand(CodeInputUnpacker inComp,
                         byte[] outExp)
                  throws java.io.IOException,
                         java.lang.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:
java.io.IOException - from either underlying stream.
java.lang.IllegalArgumentException - if input was not LZW compressed data.

main

public static void main(java.lang.String[] argv)
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.


Xj3D VRML/X3D Code API

Copyright © 2001 - 2005 Web3D Consortium