it.unimi.di.mg4j.io
Class ArithmeticCoder

java.lang.Object
  extended by it.unimi.di.mg4j.io.ArithmeticCoder

public final class ArithmeticCoder
extends Object

An arithmetic coder.

This class provides an arithmetic coder. The output of the coder goes to an OutputBitStream. You must first create a coder specifying the number of input symbols, then call encode(int, OutputBitStream) for all symbols to be coded, and finally flush(OutputBitStream). This last operation will output some bits so that the last symbol is encoded nonambiguously.

The code is inspired by the arithmetic coder by John Carpinelli, Radford M. Neal, Wayne Salamonsen and Lang Stuiver, which is in turn based on Arithmetic Coding Revisited, by Alistair Moffat, Radford M. Neal and Ian H. Witten (Proc. IEEE Data Compression Conference, Snowbird, Utah, March 1995).

Since:
0.1
Author:
Sebastiano Vigna
See Also:
ArithmeticDecoder

Field Summary
static int BITS
          Number of bits used by the coder.
 
Constructor Summary
ArithmeticCoder(int n)
          Creates a new coder.
 
Method Summary
 int encode(int x, OutputBitStream obs)
          Encodes a symbol.
 int flush(OutputBitStream obs)
          Flushes the last bits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BITS

public static final int BITS
Number of bits used by the coder.

See Also:
Constant Field Values
Constructor Detail

ArithmeticCoder

public ArithmeticCoder(int n)
Creates a new coder.

Parameters:
n - number of symbols used by the coder.
Method Detail

encode

public int encode(int x,
                  OutputBitStream obs)
           throws IOException
Encodes a symbol.

Parameters:
x - a bit.
obs - the output stream.
Returns:
the number of bits written (note that it can be 0, as arithmetic compression can encode a symbol in a fraction of a bit).
Throws:
IOException - if obs does.

flush

public int flush(OutputBitStream obs)
          throws IOException
Flushes the last bits.

This method must be called when coding is over. It guarantees that enough bits are output to make the decoding of the last symbol nonambiguous, whichever bits follow in the stream.

Parameters:
obs - the output stream.
Returns:
the number of bits written.
Throws:
IOException - if obs does.