Class 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 Detail

    • 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.