summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/org/fox/ttcomics2/junrar/unpack/ppm/RangeCoder.java
blob: 57b00138716e75eb2bf9c5a14bd92d2c2fdd084b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
 * Original author: Edmund Wagner
 * Creation date: 31.05.2007
 *
 * Source: $HeadURL$
 * Last changed: $LastChangedDate$
 * 
 * the unrar licence applies to all junrar source and binary distributions 
 * you are not allowed to use this source to re-create the RAR compression algorithm
 * 
 * Here some html entities which can be used for escaping javadoc tags:
 * "&":  "&" or "&"
 * "<":  "&#060;" or "&lt;"
 * ">":  "&#062;" or "&gt;"
 * "@":  "&#064;" 
 */
package org.fox.ttcomics2.junrar.unpack.ppm;

import org.fox.ttcomics2.junrar.exception.RarException;
import org.fox.ttcomics2.junrar.unpack.Unpack;

import java.io.IOException;


public class RangeCoder {
    public static final int TOP = 1 << 24;

    public static final int BOT = 1 << 15;

    private static final long uintMask = 0xFFFFffffL;
    private final SubRange subRange = new SubRange();
    // uint low, code, range;
    private long low, code, range;
    private Unpack unpackRead;

    public SubRange getSubRange() {
        return subRange;
    }

    public void initDecoder(Unpack unpackRead) throws IOException, RarException {
        this.unpackRead = unpackRead;

        low = code = 0L;
        range = 0xFFFFffffL;
        for (int i = 0; i < 4; i++) {
            code = ((code << 8) | getChar()) & uintMask;
        }
    }

    public int getCurrentCount() {
        range = (range / subRange.getScale()) & uintMask;
        return (int) ((code - low) / (range));
    }

    public long getCurrentShiftCount(int SHIFT) {
        range = range >>> SHIFT;
        return ((code - low) / (range)) & uintMask;
    }

    public void decode() {
        low = (low + (range * subRange.getLowCount())) & uintMask;
        range = (range * (subRange.getHighCount() - subRange.getLowCount())) & uintMask;
    }

    private int getChar() throws IOException, RarException {
        return (unpackRead.getChar());
    }

    public void ariDecNormalize() throws IOException, RarException {
//		while ((low ^ (low + range)) < TOP || range < BOT && ((range = -low & (BOT - 1)) != 0 ? true : true)) 
//		{
//			code = ((code << 8) | unpackRead.getChar()&0xff)&uintMask;
//			range = (range << 8)&uintMask;
//			low = (low << 8)&uintMask;
//		}

        // Rewrote for clarity
        boolean c2 = false;
        while ((low ^ (low + range)) < TOP || (c2 = range < BOT)) {
            if (c2) {
                range = (-low & (BOT - 1)) & uintMask;
                c2 = false;
            }
            code = ((code << 8) | getChar()) & uintMask;
            range = (range << 8) & uintMask;
            low = (low << 8) & uintMask;
        }
    }

    // Debug
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append("RangeCoder[");
        buffer.append("\n  low=");
        buffer.append(low);
        buffer.append("\n  code=");
        buffer.append(code);
        buffer.append("\n  range=");
        buffer.append(range);
        buffer.append("\n  subrange=");
        buffer.append(subRange);
        buffer.append("]");
        return buffer.toString();
    }

    public static class SubRange {
        // uint LowCount, HighCount, scale;
        private long lowCount, highCount, scale;

        public long getHighCount() {
            return highCount;
        }

        public void setHighCount(long highCount) {
            this.highCount = highCount & uintMask;
        }

        public long getLowCount() {
            return lowCount & uintMask;
        }

        public void setLowCount(long lowCount) {
            this.lowCount = lowCount & uintMask;
        }

        public long getScale() {
            return scale;
        }

        public void setScale(long scale) {
            this.scale = scale & uintMask;
        }

        public void incScale(int dScale) {
            setScale(getScale() + dScale);
        }

        // Debug
        public String toString() {
            StringBuilder buffer = new StringBuilder();
            buffer.append("SubRange[");
            buffer.append("\n  lowCount=");
            buffer.append(lowCount);
            buffer.append("\n  highCount=");
            buffer.append(highCount);
            buffer.append("\n  scale=");
            buffer.append(scale);
            buffer.append("]");
            return buffer.toString();
        }
    }
}