01: package SevenZip.Compression.RangeCoder;
02:
03: import java.io.IOException;
04:
05: public class BitTreeEncoder {
06: short[] Models;
07: int NumBitLevels;
08:
09: public BitTreeEncoder(int numBitLevels) {
10: NumBitLevels = numBitLevels;
11: Models = new short[1 << numBitLevels];
12: }
13:
14: public void Init() {
15: Decoder.InitBitModels(Models);
16: }
17:
18: public void Encode(Encoder rangeEncoder, int symbol)
19: throws IOException {
20: int m = 1;
21: for (int bitIndex = NumBitLevels; bitIndex != 0;) {
22: bitIndex--;
23: int bit = (symbol >>> bitIndex) & 1;
24: rangeEncoder.Encode(Models, m, bit);
25: m = (m << 1) | bit;
26: }
27: }
28:
29: public void ReverseEncode(Encoder rangeEncoder, int symbol)
30: throws IOException {
31: int m = 1;
32: for (int i = 0; i < NumBitLevels; i++) {
33: int bit = symbol & 1;
34: rangeEncoder.Encode(Models, m, bit);
35: m = (m << 1) | bit;
36: symbol >>= 1;
37: }
38: }
39:
40: public int GetPrice(int symbol) {
41: int price = 0;
42: int m = 1;
43: for (int bitIndex = NumBitLevels; bitIndex != 0;) {
44: bitIndex--;
45: int bit = (symbol >>> bitIndex) & 1;
46: price += Encoder.GetPrice(Models[m], bit);
47: m = (m << 1) + bit;
48: }
49: return price;
50: }
51:
52: public int ReverseGetPrice(int symbol) {
53: int price = 0;
54: int m = 1;
55: for (int i = NumBitLevels; i != 0; i--) {
56: int bit = symbol & 1;
57: symbol >>>= 1;
58: price += Encoder.GetPrice(Models[m], bit);
59: m = (m << 1) | bit;
60: }
61: return price;
62: }
63:
64: public static int ReverseGetPrice(short[] Models, int startIndex,
65: int NumBitLevels, int symbol) {
66: int price = 0;
67: int m = 1;
68: for (int i = NumBitLevels; i != 0; i--) {
69: int bit = symbol & 1;
70: symbol >>>= 1;
71: price += Encoder.GetPrice(Models[startIndex + m], bit);
72: m = (m << 1) | bit;
73: }
74: return price;
75: }
76:
77: public static void ReverseEncode(short[] Models, int startIndex,
78: Encoder rangeEncoder, int NumBitLevels, int symbol)
79: throws IOException {
80: int m = 1;
81: for (int i = 0; i < NumBitLevels; i++) {
82: int bit = symbol & 1;
83: rangeEncoder.Encode(Models, startIndex + m, bit);
84: m = (m << 1) | bit;
85: symbol >>= 1;
86: }
87: }
88: }
|