@file-viewer/renderer-ofd 2.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,183 @@
1
+ /* Copyright 2012 Mozilla Foundation
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+ /* eslint no-var: error */
16
+
17
+ // Table C-2
18
+ const QeTable = [
19
+ { qe: 0x5601, nmps: 1, nlps: 1, switchFlag: 1 },
20
+ { qe: 0x3401, nmps: 2, nlps: 6, switchFlag: 0 },
21
+ { qe: 0x1801, nmps: 3, nlps: 9, switchFlag: 0 },
22
+ { qe: 0x0ac1, nmps: 4, nlps: 12, switchFlag: 0 },
23
+ { qe: 0x0521, nmps: 5, nlps: 29, switchFlag: 0 },
24
+ { qe: 0x0221, nmps: 38, nlps: 33, switchFlag: 0 },
25
+ { qe: 0x5601, nmps: 7, nlps: 6, switchFlag: 1 },
26
+ { qe: 0x5401, nmps: 8, nlps: 14, switchFlag: 0 },
27
+ { qe: 0x4801, nmps: 9, nlps: 14, switchFlag: 0 },
28
+ { qe: 0x3801, nmps: 10, nlps: 14, switchFlag: 0 },
29
+ { qe: 0x3001, nmps: 11, nlps: 17, switchFlag: 0 },
30
+ { qe: 0x2401, nmps: 12, nlps: 18, switchFlag: 0 },
31
+ { qe: 0x1c01, nmps: 13, nlps: 20, switchFlag: 0 },
32
+ { qe: 0x1601, nmps: 29, nlps: 21, switchFlag: 0 },
33
+ { qe: 0x5601, nmps: 15, nlps: 14, switchFlag: 1 },
34
+ { qe: 0x5401, nmps: 16, nlps: 14, switchFlag: 0 },
35
+ { qe: 0x5101, nmps: 17, nlps: 15, switchFlag: 0 },
36
+ { qe: 0x4801, nmps: 18, nlps: 16, switchFlag: 0 },
37
+ { qe: 0x3801, nmps: 19, nlps: 17, switchFlag: 0 },
38
+ { qe: 0x3401, nmps: 20, nlps: 18, switchFlag: 0 },
39
+ { qe: 0x3001, nmps: 21, nlps: 19, switchFlag: 0 },
40
+ { qe: 0x2801, nmps: 22, nlps: 19, switchFlag: 0 },
41
+ { qe: 0x2401, nmps: 23, nlps: 20, switchFlag: 0 },
42
+ { qe: 0x2201, nmps: 24, nlps: 21, switchFlag: 0 },
43
+ { qe: 0x1c01, nmps: 25, nlps: 22, switchFlag: 0 },
44
+ { qe: 0x1801, nmps: 26, nlps: 23, switchFlag: 0 },
45
+ { qe: 0x1601, nmps: 27, nlps: 24, switchFlag: 0 },
46
+ { qe: 0x1401, nmps: 28, nlps: 25, switchFlag: 0 },
47
+ { qe: 0x1201, nmps: 29, nlps: 26, switchFlag: 0 },
48
+ { qe: 0x1101, nmps: 30, nlps: 27, switchFlag: 0 },
49
+ { qe: 0x0ac1, nmps: 31, nlps: 28, switchFlag: 0 },
50
+ { qe: 0x09c1, nmps: 32, nlps: 29, switchFlag: 0 },
51
+ { qe: 0x08a1, nmps: 33, nlps: 30, switchFlag: 0 },
52
+ { qe: 0x0521, nmps: 34, nlps: 31, switchFlag: 0 },
53
+ { qe: 0x0441, nmps: 35, nlps: 32, switchFlag: 0 },
54
+ { qe: 0x02a1, nmps: 36, nlps: 33, switchFlag: 0 },
55
+ { qe: 0x0221, nmps: 37, nlps: 34, switchFlag: 0 },
56
+ { qe: 0x0141, nmps: 38, nlps: 35, switchFlag: 0 },
57
+ { qe: 0x0111, nmps: 39, nlps: 36, switchFlag: 0 },
58
+ { qe: 0x0085, nmps: 40, nlps: 37, switchFlag: 0 },
59
+ { qe: 0x0049, nmps: 41, nlps: 38, switchFlag: 0 },
60
+ { qe: 0x0025, nmps: 42, nlps: 39, switchFlag: 0 },
61
+ { qe: 0x0015, nmps: 43, nlps: 40, switchFlag: 0 },
62
+ { qe: 0x0009, nmps: 44, nlps: 41, switchFlag: 0 },
63
+ { qe: 0x0005, nmps: 45, nlps: 42, switchFlag: 0 },
64
+ { qe: 0x0001, nmps: 45, nlps: 43, switchFlag: 0 },
65
+ { qe: 0x5601, nmps: 46, nlps: 46, switchFlag: 0 },
66
+ ];
67
+
68
+ /**
69
+ * This class implements the QM Coder decoding as defined in
70
+ * JPEG 2000 Part I Final Committee Draft Version 1.0
71
+ * Annex C.3 Arithmetic decoding procedure
72
+ * available at http://www.jpeg.org/public/fcd15444-1.pdf
73
+ *
74
+ * The arithmetic decoder is used in conjunction with context models to decode
75
+ * JPEG2000 and JBIG2 streams.
76
+ */
77
+ class ArithmeticDecoder {
78
+ // C.3.5 Initialisation of the decoder (INITDEC)
79
+ constructor(data, start, end) {
80
+ this.data = data;
81
+ this.bp = start;
82
+ this.dataEnd = end;
83
+
84
+ this.chigh = data[start];
85
+ this.clow = 0;
86
+
87
+ this.byteIn();
88
+
89
+ this.chigh = ((this.chigh << 7) & 0xffff) | ((this.clow >> 9) & 0x7f);
90
+ this.clow = (this.clow << 7) & 0xffff;
91
+ this.ct -= 7;
92
+ this.a = 0x8000;
93
+ }
94
+
95
+ // C.3.4 Compressed data input (BYTEIN)
96
+ byteIn() {
97
+ const data = this.data;
98
+ let bp = this.bp;
99
+
100
+ if (data[bp] === 0xff) {
101
+ if (data[bp + 1] > 0x8f) {
102
+ this.clow += 0xff00;
103
+ this.ct = 8;
104
+ } else {
105
+ bp++;
106
+ this.clow += data[bp] << 9;
107
+ this.ct = 7;
108
+ this.bp = bp;
109
+ }
110
+ } else {
111
+ bp++;
112
+ this.clow += bp < this.dataEnd ? data[bp] << 8 : 0xff00;
113
+ this.ct = 8;
114
+ this.bp = bp;
115
+ }
116
+ if (this.clow > 0xffff) {
117
+ this.chigh += this.clow >> 16;
118
+ this.clow &= 0xffff;
119
+ }
120
+ }
121
+
122
+ // C.3.2 Decoding a decision (DECODE)
123
+ readBit(contexts, pos) {
124
+ // Contexts are packed into 1 byte:
125
+ // highest 7 bits carry cx.index, lowest bit carries cx.mps
126
+ let cx_index = contexts[pos] >> 1,
127
+ cx_mps = contexts[pos] & 1;
128
+ const qeTableIcx = QeTable[cx_index];
129
+ const qeIcx = qeTableIcx.qe;
130
+ let d;
131
+ let a = this.a - qeIcx;
132
+
133
+ if (this.chigh < qeIcx) {
134
+ // exchangeLps
135
+ if (a < qeIcx) {
136
+ a = qeIcx;
137
+ d = cx_mps;
138
+ cx_index = qeTableIcx.nmps;
139
+ } else {
140
+ a = qeIcx;
141
+ d = 1 ^ cx_mps;
142
+ if (qeTableIcx.switchFlag === 1) {
143
+ cx_mps = d;
144
+ }
145
+ cx_index = qeTableIcx.nlps;
146
+ }
147
+ } else {
148
+ this.chigh -= qeIcx;
149
+ if ((a & 0x8000) !== 0) {
150
+ this.a = a;
151
+ return cx_mps;
152
+ }
153
+ // exchangeMps
154
+ if (a < qeIcx) {
155
+ d = 1 ^ cx_mps;
156
+ if (qeTableIcx.switchFlag === 1) {
157
+ cx_mps = d;
158
+ }
159
+ cx_index = qeTableIcx.nlps;
160
+ } else {
161
+ d = cx_mps;
162
+ cx_index = qeTableIcx.nmps;
163
+ }
164
+ }
165
+ // C.3.3 renormD;
166
+ do {
167
+ if (this.ct === 0) {
168
+ this.byteIn();
169
+ }
170
+
171
+ a <<= 1;
172
+ this.chigh = ((this.chigh << 1) & 0xffff) | ((this.clow >> 15) & 1);
173
+ this.clow = (this.clow << 1) & 0xffff;
174
+ this.ct--;
175
+ } while ((a & 0x8000) === 0);
176
+ this.a = a;
177
+
178
+ contexts[pos] = (cx_index << 1) | cx_mps;
179
+ return d;
180
+ }
181
+ }
182
+
183
+ export { ArithmeticDecoder };