import java.io.*; /** * Class which performs both the arithmetic encoding and decoding algorithms, using * integer implementation, adaptation, and contexts. * * @author Amanda Askew & Dane Barney * @version 2003.09.11 */ public class ArithmeticCoder { private final long UPPER_BOUND = (long)Integer.MAX_VALUE*2+1; private final long MIDDLE_BOUND = UPPER_BOUND/2+1; private final long ONE_4TH_BOUND = MIDDLE_BOUND/2; private final long THREE_4TH_BOUND = ONE_4TH_BOUND*3; private long L, R, T; private int C; public ArithmeticCoder() { // initialize L and R, the upper and lower bounds of the interval L = 0; R = UPPER_BOUND; T = 0; // the unsigned value of the tag buffer (used only for decoding) C = 0; // middle half scaling counter (used only for encoding) } /** * Arithmetically encodes one value. * * @param val the value to encode * @param context the context associated with this value * @param out the outstream to write to */ public void encodeValue(int val, Context context, BitWriter out) throws IOException { assert (val >= 0); out.writeBit(val); } /** * The first step in decoding. This shifts in the first 32-bits (the size of type "int") * into the tag, so that decoding can begin. */ public void initializeTag(BitReader in) throws IOException { } /** * Arithmetically decodes and returns one value. * * @param context the context associated with this value * @param in the bitstream being read from * @return the decoded value */ public int decodeValue(Context context, BitReader in) throws IOException { return in.readBit(true); } /** * Clean up. */ public void finishEncode(BitWriter out) { } }