Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp

Issue 1359233002: Split up JBig2_GeneralDecoder.cpp. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h ('k') | core/src/fxcodec/jbig2/JBig2_GrdProc.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "JBig2_GeneralDecoder.h"
8
9 #include "../../../../third_party/base/nonstd_unique_ptr.h"
10 #include "JBig2_ArithDecoder.h"
11 #include "JBig2_ArithIntDecoder.h"
12 #include "JBig2_HuffmanDecoder.h"
13 #include "JBig2_HuffmanTable.h"
14 #include "JBig2_PatternDict.h"
15
16 extern const JBig2ArithQe QeTable[] = {
17 {0x5601, 1, 1, 1}, {0x3401, 2, 6, 0}, {0x1801, 3, 9, 0},
18 {0x0AC1, 4, 12, 0}, {0x0521, 5, 29, 0}, {0x0221, 38, 33, 0},
19 {0x5601, 7, 6, 1}, {0x5401, 8, 14, 0}, {0x4801, 9, 14, 0},
20 {0x3801, 10, 14, 0}, {0x3001, 11, 17, 0}, {0x2401, 12, 18, 0},
21 {0x1C01, 13, 20, 0}, {0x1601, 29, 21, 0}, {0x5601, 15, 14, 1},
22 {0x5401, 16, 14, 0}, {0x5101, 17, 15, 0}, {0x4801, 18, 16, 0},
23 {0x3801, 19, 17, 0}, {0x3401, 20, 18, 0}, {0x3001, 21, 19, 0},
24 {0x2801, 22, 19, 0}, {0x2401, 23, 20, 0}, {0x2201, 24, 21, 0},
25 {0x1C01, 25, 22, 0}, {0x1801, 26, 23, 0}, {0x1601, 27, 24, 0},
26 {0x1401, 28, 25, 0}, {0x1201, 29, 26, 0}, {0x1101, 30, 27, 0},
27 {0x0AC1, 31, 28, 0}, {0x09C1, 32, 29, 0}, {0x08A1, 33, 30, 0},
28 {0x0521, 34, 31, 0}, {0x0441, 35, 32, 0}, {0x02A1, 36, 33, 0},
29 {0x0221, 37, 34, 0}, {0x0141, 38, 35, 0}, {0x0111, 39, 36, 0},
30 {0x0085, 40, 37, 0}, {0x0049, 41, 38, 0}, {0x0025, 42, 39, 0},
31 {0x0015, 43, 40, 0}, {0x0009, 44, 41, 0}, {0x0005, 45, 42, 0},
32 {0x0001, 45, 43, 0}, {0x5601, 46, 46, 0}};
33
34 extern const unsigned int JBIG2_QE_NUM = FX_ArraySize(QeTable);
35
36 bool CJBig2_GRDProc::UseTemplate0Opt3() const {
37 return (GBAT[0] == 3) && (GBAT[1] == -1) && (GBAT[2] == -3) &&
38 (GBAT[3] == -1) && (GBAT[4] == 2) && (GBAT[5] == -2) &&
39 (GBAT[6] == -2) && (GBAT[7] == -2);
40 }
41
42 bool CJBig2_GRDProc::UseTemplate1Opt3() const {
43 return (GBAT[0] == 3) && (GBAT[1] == -1);
44 }
45
46 bool CJBig2_GRDProc::UseTemplate23Opt3() const {
47 return (GBAT[0] == 2) && (GBAT[1] == -1);
48 }
49
50 CJBig2_Image* CJBig2_GRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
51 JBig2ArithCtx* gbContext) {
52 if (GBW == 0 || GBH == 0)
53 return new CJBig2_Image(GBW, GBH);
54
55 if (GBTEMPLATE == 0) {
56 if (UseTemplate0Opt3())
57 return decode_Arith_Template0_opt3(pArithDecoder, gbContext);
58 return decode_Arith_Template0_unopt(pArithDecoder, gbContext);
59 } else if (GBTEMPLATE == 1) {
60 if (UseTemplate1Opt3())
61 return decode_Arith_Template1_opt3(pArithDecoder, gbContext);
62 return decode_Arith_Template1_unopt(pArithDecoder, gbContext);
63 } else if (GBTEMPLATE == 2) {
64 if (UseTemplate23Opt3())
65 return decode_Arith_Template2_opt3(pArithDecoder, gbContext);
66 return decode_Arith_Template2_unopt(pArithDecoder, gbContext);
67 } else {
68 if (UseTemplate23Opt3())
69 return decode_Arith_Template3_opt3(pArithDecoder, gbContext);
70 return decode_Arith_Template3_unopt(pArithDecoder, gbContext);
71 }
72 }
73 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template0_opt3(
74 CJBig2_ArithDecoder* pArithDecoder,
75 JBig2ArithCtx* gbContext) {
76 FX_BOOL LTP, SLTP, bVal;
77 FX_DWORD CONTEXT;
78 FX_DWORD line1, line2;
79 uint8_t *pLine, *pLine1, *pLine2, cVal;
80 int32_t nStride, nStride2, k;
81 int32_t nLineBytes, nBitsLeft, cc;
82 LTP = 0;
83 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
84 if (!GBREG->m_pData)
85 return nullptr;
86
87 pLine = GBREG->m_pData;
88 nStride = GBREG->m_nStride;
89 nStride2 = nStride << 1;
90 nLineBytes = ((GBW + 7) >> 3) - 1;
91 nBitsLeft = GBW - (nLineBytes << 3);
92 FX_DWORD height = GBH & 0x7fffffff;
93 for (FX_DWORD h = 0; h < height; h++) {
94 if (TPGDON) {
95 SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
96 LTP = LTP ^ SLTP;
97 }
98 if (LTP == 1) {
99 GBREG->copyLine(h, h - 1);
100 } else {
101 if (h > 1) {
102 pLine1 = pLine - nStride2;
103 pLine2 = pLine - nStride;
104 line1 = (*pLine1++) << 6;
105 line2 = *pLine2++;
106 CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
107 for (cc = 0; cc < nLineBytes; cc++) {
108 line1 = (line1 << 8) | ((*pLine1++) << 6);
109 line2 = (line2 << 8) | (*pLine2++);
110 cVal = 0;
111 for (k = 7; k >= 0; k--) {
112 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
113 cVal |= bVal << k;
114 CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal |
115 ((line1 >> k) & 0x0800) | ((line2 >> k) & 0x0010));
116 }
117 pLine[cc] = cVal;
118 }
119 line1 <<= 8;
120 line2 <<= 8;
121 cVal = 0;
122 for (k = 0; k < nBitsLeft; k++) {
123 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
124 cVal |= bVal << (7 - k);
125 CONTEXT =
126 (((CONTEXT & 0x7bf7) << 1) | bVal |
127 ((line1 >> (7 - k)) & 0x0800) | ((line2 >> (7 - k)) & 0x0010));
128 }
129 pLine[nLineBytes] = cVal;
130 } else {
131 pLine2 = pLine - nStride;
132 line2 = (h & 1) ? (*pLine2++) : 0;
133 CONTEXT = (line2 & 0x07f0);
134 for (cc = 0; cc < nLineBytes; cc++) {
135 if (h & 1) {
136 line2 = (line2 << 8) | (*pLine2++);
137 }
138 cVal = 0;
139 for (k = 7; k >= 0; k--) {
140 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
141 cVal |= bVal << k;
142 CONTEXT =
143 (((CONTEXT & 0x7bf7) << 1) | bVal | ((line2 >> k) & 0x0010));
144 }
145 pLine[cc] = cVal;
146 }
147 line2 <<= 8;
148 cVal = 0;
149 for (k = 0; k < nBitsLeft; k++) {
150 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
151 cVal |= bVal << (7 - k);
152 CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal |
153 (((line2 >> (7 - k))) & 0x0010));
154 }
155 pLine[nLineBytes] = cVal;
156 }
157 }
158 pLine += nStride;
159 }
160 return GBREG.release();
161 }
162
163 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template0_unopt(
164 CJBig2_ArithDecoder* pArithDecoder,
165 JBig2ArithCtx* gbContext) {
166 FX_BOOL LTP, SLTP, bVal;
167 FX_DWORD CONTEXT;
168 FX_DWORD line1, line2, line3;
169 LTP = 0;
170 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
171 GBREG->fill(0);
172 for (FX_DWORD h = 0; h < GBH; h++) {
173 if (TPGDON) {
174 SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
175 LTP = LTP ^ SLTP;
176 }
177 if (LTP == 1) {
178 GBREG->copyLine(h, h - 1);
179 } else {
180 line1 = GBREG->getPixel(1, h - 2);
181 line1 |= GBREG->getPixel(0, h - 2) << 1;
182 line2 = GBREG->getPixel(2, h - 1);
183 line2 |= GBREG->getPixel(1, h - 1) << 1;
184 line2 |= GBREG->getPixel(0, h - 1) << 2;
185 line3 = 0;
186 for (FX_DWORD w = 0; w < GBW; w++) {
187 if (USESKIP && SKIP->getPixel(w, h)) {
188 bVal = 0;
189 } else {
190 CONTEXT = line3;
191 CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
192 CONTEXT |= line2 << 5;
193 CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
194 CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
195 CONTEXT |= line1 << 12;
196 CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
197 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
198 }
199 if (bVal) {
200 GBREG->setPixel(w, h, bVal);
201 }
202 line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
203 line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
204 line3 = ((line3 << 1) | bVal) & 0x0f;
205 }
206 }
207 }
208 return GBREG.release();
209 }
210
211 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template1_opt3(
212 CJBig2_ArithDecoder* pArithDecoder,
213 JBig2ArithCtx* gbContext) {
214 FX_BOOL LTP, SLTP, bVal;
215 FX_DWORD CONTEXT;
216 FX_DWORD line1, line2;
217 uint8_t *pLine, *pLine1, *pLine2, cVal;
218 int32_t nStride, nStride2, k;
219 int32_t nLineBytes, nBitsLeft, cc;
220 LTP = 0;
221 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
222 if (!GBREG->m_pData)
223 return nullptr;
224
225 pLine = GBREG->m_pData;
226 nStride = GBREG->m_nStride;
227 nStride2 = nStride << 1;
228 nLineBytes = ((GBW + 7) >> 3) - 1;
229 nBitsLeft = GBW - (nLineBytes << 3);
230 for (FX_DWORD h = 0; h < GBH; h++) {
231 if (TPGDON) {
232 SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
233 LTP = LTP ^ SLTP;
234 }
235 if (LTP == 1) {
236 GBREG->copyLine(h, h - 1);
237 } else {
238 if (h > 1) {
239 pLine1 = pLine - nStride2;
240 pLine2 = pLine - nStride;
241 line1 = (*pLine1++) << 4;
242 line2 = *pLine2++;
243 CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
244 for (cc = 0; cc < nLineBytes; cc++) {
245 line1 = (line1 << 8) | ((*pLine1++) << 4);
246 line2 = (line2 << 8) | (*pLine2++);
247 cVal = 0;
248 for (k = 7; k >= 0; k--) {
249 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
250 cVal |= bVal << k;
251 CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal |
252 ((line1 >> k) & 0x0200) | ((line2 >> (k + 1)) & 0x0008);
253 }
254 pLine[cc] = cVal;
255 }
256 line1 <<= 8;
257 line2 <<= 8;
258 cVal = 0;
259 for (k = 0; k < nBitsLeft; k++) {
260 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
261 cVal |= bVal << (7 - k);
262 CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal |
263 ((line1 >> (7 - k)) & 0x0200) |
264 ((line2 >> (8 - k)) & 0x0008);
265 }
266 pLine[nLineBytes] = cVal;
267 } else {
268 pLine2 = pLine - nStride;
269 line2 = (h & 1) ? (*pLine2++) : 0;
270 CONTEXT = (line2 >> 1) & 0x01f8;
271 for (cc = 0; cc < nLineBytes; cc++) {
272 if (h & 1) {
273 line2 = (line2 << 8) | (*pLine2++);
274 }
275 cVal = 0;
276 for (k = 7; k >= 0; k--) {
277 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
278 cVal |= bVal << k;
279 CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal |
280 ((line2 >> (k + 1)) & 0x0008);
281 }
282 pLine[cc] = cVal;
283 }
284 line2 <<= 8;
285 cVal = 0;
286 for (k = 0; k < nBitsLeft; k++) {
287 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
288 cVal |= bVal << (7 - k);
289 CONTEXT =
290 ((CONTEXT & 0x0efb) << 1) | bVal | ((line2 >> (8 - k)) & 0x0008);
291 }
292 pLine[nLineBytes] = cVal;
293 }
294 }
295 pLine += nStride;
296 }
297 return GBREG.release();
298 }
299
300 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template1_unopt(
301 CJBig2_ArithDecoder* pArithDecoder,
302 JBig2ArithCtx* gbContext) {
303 FX_BOOL LTP, SLTP, bVal;
304 FX_DWORD CONTEXT;
305 FX_DWORD line1, line2, line3;
306 LTP = 0;
307 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
308 GBREG->fill(0);
309 for (FX_DWORD h = 0; h < GBH; h++) {
310 if (TPGDON) {
311 SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
312 LTP = LTP ^ SLTP;
313 }
314 if (LTP == 1) {
315 GBREG->copyLine(h, h - 1);
316 } else {
317 line1 = GBREG->getPixel(2, h - 2);
318 line1 |= GBREG->getPixel(1, h - 2) << 1;
319 line1 |= GBREG->getPixel(0, h - 2) << 2;
320 line2 = GBREG->getPixel(2, h - 1);
321 line2 |= GBREG->getPixel(1, h - 1) << 1;
322 line2 |= GBREG->getPixel(0, h - 1) << 2;
323 line3 = 0;
324 for (FX_DWORD w = 0; w < GBW; w++) {
325 if (USESKIP && SKIP->getPixel(w, h)) {
326 bVal = 0;
327 } else {
328 CONTEXT = line3;
329 CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
330 CONTEXT |= line2 << 4;
331 CONTEXT |= line1 << 9;
332 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
333 }
334 if (bVal) {
335 GBREG->setPixel(w, h, bVal);
336 }
337 line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
338 line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
339 line3 = ((line3 << 1) | bVal) & 0x07;
340 }
341 }
342 }
343 return GBREG.release();
344 }
345 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template2_opt3(
346 CJBig2_ArithDecoder* pArithDecoder,
347 JBig2ArithCtx* gbContext) {
348 FX_BOOL LTP, SLTP, bVal;
349 FX_DWORD CONTEXT;
350 FX_DWORD line1, line2;
351 uint8_t *pLine, *pLine1, *pLine2, cVal;
352 int32_t nStride, nStride2, k;
353 int32_t nLineBytes, nBitsLeft, cc;
354 LTP = 0;
355 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
356 if (!GBREG->m_pData)
357 return nullptr;
358
359 pLine = GBREG->m_pData;
360 nStride = GBREG->m_nStride;
361 nStride2 = nStride << 1;
362 nLineBytes = ((GBW + 7) >> 3) - 1;
363 nBitsLeft = GBW - (nLineBytes << 3);
364 for (FX_DWORD h = 0; h < GBH; h++) {
365 if (TPGDON) {
366 SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
367 LTP = LTP ^ SLTP;
368 }
369 if (LTP == 1) {
370 GBREG->copyLine(h, h - 1);
371 } else {
372 if (h > 1) {
373 pLine1 = pLine - nStride2;
374 pLine2 = pLine - nStride;
375 line1 = (*pLine1++) << 1;
376 line2 = *pLine2++;
377 CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
378 for (cc = 0; cc < nLineBytes; cc++) {
379 line1 = (line1 << 8) | ((*pLine1++) << 1);
380 line2 = (line2 << 8) | (*pLine2++);
381 cVal = 0;
382 for (k = 7; k >= 0; k--) {
383 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
384 cVal |= bVal << k;
385 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
386 ((line1 >> k) & 0x0080) | ((line2 >> (k + 3)) & 0x0004);
387 }
388 pLine[cc] = cVal;
389 }
390 line1 <<= 8;
391 line2 <<= 8;
392 cVal = 0;
393 for (k = 0; k < nBitsLeft; k++) {
394 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
395 cVal |= bVal << (7 - k);
396 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
397 ((line1 >> (7 - k)) & 0x0080) |
398 ((line2 >> (10 - k)) & 0x0004);
399 }
400 pLine[nLineBytes] = cVal;
401 } else {
402 pLine2 = pLine - nStride;
403 line2 = (h & 1) ? (*pLine2++) : 0;
404 CONTEXT = (line2 >> 3) & 0x007c;
405 for (cc = 0; cc < nLineBytes; cc++) {
406 if (h & 1) {
407 line2 = (line2 << 8) | (*pLine2++);
408 }
409 cVal = 0;
410 for (k = 7; k >= 0; k--) {
411 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
412 cVal |= bVal << k;
413 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
414 ((line2 >> (k + 3)) & 0x0004);
415 }
416 pLine[cc] = cVal;
417 }
418 line2 <<= 8;
419 cVal = 0;
420 for (k = 0; k < nBitsLeft; k++) {
421 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
422 cVal |= bVal << (7 - k);
423 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
424 (((line2 >> (10 - k))) & 0x0004);
425 }
426 pLine[nLineBytes] = cVal;
427 }
428 }
429 pLine += nStride;
430 }
431 return GBREG.release();
432 }
433
434 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template2_unopt(
435 CJBig2_ArithDecoder* pArithDecoder,
436 JBig2ArithCtx* gbContext) {
437 FX_BOOL LTP, SLTP, bVal;
438 FX_DWORD CONTEXT;
439 FX_DWORD line1, line2, line3;
440 LTP = 0;
441 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
442 GBREG->fill(0);
443 for (FX_DWORD h = 0; h < GBH; h++) {
444 if (TPGDON) {
445 SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
446 LTP = LTP ^ SLTP;
447 }
448 if (LTP == 1) {
449 GBREG->copyLine(h, h - 1);
450 } else {
451 line1 = GBREG->getPixel(1, h - 2);
452 line1 |= GBREG->getPixel(0, h - 2) << 1;
453 line2 = GBREG->getPixel(1, h - 1);
454 line2 |= GBREG->getPixel(0, h - 1) << 1;
455 line3 = 0;
456 for (FX_DWORD w = 0; w < GBW; w++) {
457 if (USESKIP && SKIP->getPixel(w, h)) {
458 bVal = 0;
459 } else {
460 CONTEXT = line3;
461 CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
462 CONTEXT |= line2 << 3;
463 CONTEXT |= line1 << 7;
464 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
465 }
466 if (bVal) {
467 GBREG->setPixel(w, h, bVal);
468 }
469 line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
470 line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
471 line3 = ((line3 << 1) | bVal) & 0x03;
472 }
473 }
474 }
475 return GBREG.release();
476 }
477
478 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template3_opt3(
479 CJBig2_ArithDecoder* pArithDecoder,
480 JBig2ArithCtx* gbContext) {
481 FX_BOOL LTP, SLTP, bVal;
482 FX_DWORD CONTEXT;
483 FX_DWORD line1;
484 uint8_t *pLine, *pLine1, cVal;
485 int32_t nStride, k;
486 int32_t nLineBytes, nBitsLeft, cc;
487 LTP = 0;
488 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
489 if (!GBREG->m_pData)
490 return nullptr;
491
492 pLine = GBREG->m_pData;
493 nStride = GBREG->m_nStride;
494 nLineBytes = ((GBW + 7) >> 3) - 1;
495 nBitsLeft = GBW - (nLineBytes << 3);
496 for (FX_DWORD h = 0; h < GBH; h++) {
497 if (TPGDON) {
498 SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
499 LTP = LTP ^ SLTP;
500 }
501 if (LTP == 1) {
502 GBREG->copyLine(h, h - 1);
503 } else {
504 if (h > 0) {
505 pLine1 = pLine - nStride;
506 line1 = *pLine1++;
507 CONTEXT = (line1 >> 1) & 0x03f0;
508 for (cc = 0; cc < nLineBytes; cc++) {
509 line1 = (line1 << 8) | (*pLine1++);
510 cVal = 0;
511 for (k = 7; k >= 0; k--) {
512 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
513 cVal |= bVal << k;
514 CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal |
515 ((line1 >> (k + 1)) & 0x0010);
516 }
517 pLine[cc] = cVal;
518 }
519 line1 <<= 8;
520 cVal = 0;
521 for (k = 0; k < nBitsLeft; k++) {
522 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
523 cVal |= bVal << (7 - k);
524 CONTEXT =
525 ((CONTEXT & 0x01f7) << 1) | bVal | ((line1 >> (8 - k)) & 0x0010);
526 }
527 pLine[nLineBytes] = cVal;
528 } else {
529 CONTEXT = 0;
530 for (cc = 0; cc < nLineBytes; cc++) {
531 cVal = 0;
532 for (k = 7; k >= 0; k--) {
533 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
534 cVal |= bVal << k;
535 CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
536 }
537 pLine[cc] = cVal;
538 }
539 cVal = 0;
540 for (k = 0; k < nBitsLeft; k++) {
541 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
542 cVal |= bVal << (7 - k);
543 CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
544 }
545 pLine[nLineBytes] = cVal;
546 }
547 }
548 pLine += nStride;
549 }
550 return GBREG.release();
551 }
552
553 CJBig2_Image* CJBig2_GRDProc::decode_Arith_Template3_unopt(
554 CJBig2_ArithDecoder* pArithDecoder,
555 JBig2ArithCtx* gbContext) {
556 FX_BOOL LTP, SLTP, bVal;
557 FX_DWORD CONTEXT;
558 FX_DWORD line1, line2;
559 LTP = 0;
560 nonstd::unique_ptr<CJBig2_Image> GBREG(new CJBig2_Image(GBW, GBH));
561 GBREG->fill(0);
562 for (FX_DWORD h = 0; h < GBH; h++) {
563 if (TPGDON) {
564 SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
565 LTP = LTP ^ SLTP;
566 }
567 if (LTP == 1) {
568 GBREG->copyLine(h, h - 1);
569 } else {
570 line1 = GBREG->getPixel(1, h - 1);
571 line1 |= GBREG->getPixel(0, h - 1) << 1;
572 line2 = 0;
573 for (FX_DWORD w = 0; w < GBW; w++) {
574 if (USESKIP && SKIP->getPixel(w, h)) {
575 bVal = 0;
576 } else {
577 CONTEXT = line2;
578 CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
579 CONTEXT |= line1 << 5;
580 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
581 }
582 if (bVal) {
583 GBREG->setPixel(w, h, bVal);
584 }
585 line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
586 line2 = ((line2 << 1) | bVal) & 0x0f;
587 }
588 }
589 }
590 return GBREG.release();
591 }
592
593 CJBig2_Image* CJBig2_GRRDProc::decode(CJBig2_ArithDecoder* pArithDecoder,
594 JBig2ArithCtx* grContext) {
595 if (GRW == 0 || GRH == 0)
596 return new CJBig2_Image(GRW, GRH);
597
598 if (GRTEMPLATE == 0) {
599 if ((GRAT[0] == -1) && (GRAT[1] == -1) && (GRAT[2] == -1) &&
600 (GRAT[3] == -1) && (GRREFERENCEDX == 0) &&
601 (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
602 return decode_Template0_opt(pArithDecoder, grContext);
603 }
604 return decode_Template0_unopt(pArithDecoder, grContext);
605 }
606
607 if ((GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth))
608 return decode_Template1_opt(pArithDecoder, grContext);
609 return decode_Template1_unopt(pArithDecoder, grContext);
610 }
611
612 CJBig2_Image* CJBig2_GRRDProc::decode_Template0_unopt(
613 CJBig2_ArithDecoder* pArithDecoder,
614 JBig2ArithCtx* grContext) {
615 FX_BOOL LTP, SLTP, bVal;
616 FX_DWORD CONTEXT;
617 FX_DWORD line1, line2, line3, line4, line5;
618 LTP = 0;
619 nonstd::unique_ptr<CJBig2_Image> GRREG(new CJBig2_Image(GRW, GRH));
620 GRREG->fill(0);
621 for (FX_DWORD h = 0; h < GRH; h++) {
622 if (TPGRON) {
623 SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
624 LTP = LTP ^ SLTP;
625 }
626 if (LTP == 0) {
627 line1 = GRREG->getPixel(1, h - 1);
628 line1 |= GRREG->getPixel(0, h - 1) << 1;
629 line2 = 0;
630 line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
631 line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1)
632 << 1;
633 line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
634 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
635 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
636 << 2;
637 line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
638 line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
639 << 1;
640 line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1)
641 << 2;
642 for (FX_DWORD w = 0; w < GRW; w++) {
643 CONTEXT = line5;
644 CONTEXT |= line4 << 3;
645 CONTEXT |= line3 << 6;
646 CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2],
647 h - GRREFERENCEDY + GRAT[3])
648 << 8;
649 CONTEXT |= line2 << 9;
650 CONTEXT |= line1 << 10;
651 CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
652 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
653 GRREG->setPixel(w, h, bVal);
654 line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
655 line2 = ((line2 << 1) | bVal) & 0x01;
656 line3 = ((line3 << 1) |
657 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2,
658 h - GRREFERENCEDY - 1)) &
659 0x03;
660 line4 =
661 ((line4 << 1) |
662 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
663 0x07;
664 line5 = ((line5 << 1) |
665 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2,
666 h - GRREFERENCEDY + 1)) &
667 0x07;
668 }
669 } else {
670 line1 = GRREG->getPixel(1, h - 1);
671 line1 |= GRREG->getPixel(0, h - 1) << 1;
672 line2 = 0;
673 line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
674 line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1)
675 << 1;
676 line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
677 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
678 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
679 << 2;
680 line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
681 line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
682 << 1;
683 line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1)
684 << 2;
685 for (FX_DWORD w = 0; w < GRW; w++) {
686 bVal = GRREFERENCE->getPixel(w, h);
687 if (!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1)) &&
688 (bVal == GRREFERENCE->getPixel(w, h - 1)) &&
689 (bVal == GRREFERENCE->getPixel(w + 1, h - 1)) &&
690 (bVal == GRREFERENCE->getPixel(w - 1, h)) &&
691 (bVal == GRREFERENCE->getPixel(w + 1, h)) &&
692 (bVal == GRREFERENCE->getPixel(w - 1, h + 1)) &&
693 (bVal == GRREFERENCE->getPixel(w, h + 1)) &&
694 (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
695 CONTEXT = line5;
696 CONTEXT |= line4 << 3;
697 CONTEXT |= line3 << 6;
698 CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2],
699 h - GRREFERENCEDY + GRAT[3])
700 << 8;
701 CONTEXT |= line2 << 9;
702 CONTEXT |= line1 << 10;
703 CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
704 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
705 }
706 GRREG->setPixel(w, h, bVal);
707 line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
708 line2 = ((line2 << 1) | bVal) & 0x01;
709 line3 = ((line3 << 1) |
710 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2,
711 h - GRREFERENCEDY - 1)) &
712 0x03;
713 line4 =
714 ((line4 << 1) |
715 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
716 0x07;
717 line5 = ((line5 << 1) |
718 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2,
719 h - GRREFERENCEDY + 1)) &
720 0x07;
721 }
722 }
723 }
724 return GRREG.release();
725 }
726
727 CJBig2_Image* CJBig2_GRRDProc::decode_Template0_opt(
728 CJBig2_ArithDecoder* pArithDecoder,
729 JBig2ArithCtx* grContext) {
730 if (!GRREFERENCE->m_pData)
731 return nullptr;
732
733 FX_BOOL LTP, SLTP, bVal;
734 FX_DWORD CONTEXT;
735 FX_DWORD line1, line1_r, line2_r, line3_r;
736 uint8_t *pLine, *pLineR, cVal;
737 intptr_t nStride, nStrideR, nOffset;
738 int32_t k, nBits;
739 int32_t GRWR, GRHR;
740 int32_t GRW, GRH;
741 GRW = (int32_t)CJBig2_GRRDProc::GRW;
742 GRH = (int32_t)CJBig2_GRRDProc::GRH;
743 LTP = 0;
744 nonstd::unique_ptr<CJBig2_Image> GRREG(new CJBig2_Image(GRW, GRH));
745 if (!GRREG->m_pData)
746 return nullptr;
747
748 pLine = GRREG->m_pData;
749 pLineR = GRREFERENCE->m_pData;
750 nStride = GRREG->m_nStride;
751 nStrideR = GRREFERENCE->m_nStride;
752 GRWR = (int32_t)GRREFERENCE->m_nWidth;
753 GRHR = (int32_t)GRREFERENCE->m_nHeight;
754 if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
755 GRREFERENCEDY = 0;
756 }
757 nOffset = -GRREFERENCEDY * nStrideR;
758 for (int32_t h = 0; h < GRH; h++) {
759 if (TPGRON) {
760 SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
761 LTP = LTP ^ SLTP;
762 }
763 line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
764 int32_t reference_h = h - GRREFERENCEDY;
765 FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
766 FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
767 FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
768 line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
769 line2_r = line2_r_ok ? pLineR[nOffset] : 0;
770 line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
771 if (LTP == 0) {
772 CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
773 ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
774 for (int32_t w = 0; w < GRW; w += 8) {
775 nBits = GRW - w > 8 ? 8 : GRW - w;
776 if (h > 0)
777 line1 = (line1 << 8) |
778 (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
779 if (h > GRHR + GRREFERENCEDY + 1) {
780 line1_r = 0;
781 line2_r = 0;
782 line3_r = 0;
783 } else {
784 if (line1_r_ok)
785 line1_r =
786 (line1_r << 8) |
787 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
788 if (line2_r_ok)
789 line2_r = (line2_r << 8) |
790 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
791 if (line3_r_ok) {
792 line3_r =
793 (line3_r << 8) |
794 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
795 } else {
796 line3_r = 0;
797 }
798 }
799 cVal = 0;
800 for (k = 0; k < nBits; k++) {
801 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
802 cVal |= bVal << (7 - k);
803 CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
804 ((line1 >> (7 - k)) & 0x0400) |
805 ((line1_r >> (7 - k)) & 0x0040) |
806 ((line2_r >> (10 - k)) & 0x0008) |
807 ((line3_r >> (13 - k)) & 0x0001);
808 }
809 pLine[w >> 3] = cVal;
810 }
811 } else {
812 CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
813 ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
814 for (int32_t w = 0; w < GRW; w += 8) {
815 nBits = GRW - w > 8 ? 8 : GRW - w;
816 if (h > 0)
817 line1 = (line1 << 8) |
818 (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
819 if (line1_r_ok)
820 line1_r =
821 (line1_r << 8) |
822 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
823 if (line2_r_ok)
824 line2_r = (line2_r << 8) |
825 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
826 if (line3_r_ok) {
827 line3_r =
828 (line3_r << 8) |
829 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
830 } else {
831 line3_r = 0;
832 }
833 cVal = 0;
834 for (k = 0; k < nBits; k++) {
835 bVal = GRREFERENCE->getPixel(w + k, h);
836 if (!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1)) &&
837 (bVal == GRREFERENCE->getPixel(w + k, h - 1)) &&
838 (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1)) &&
839 (bVal == GRREFERENCE->getPixel(w + k - 1, h)) &&
840 (bVal == GRREFERENCE->getPixel(w + k + 1, h)) &&
841 (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1)) &&
842 (bVal == GRREFERENCE->getPixel(w + k, h + 1)) &&
843 (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
844 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
845 }
846 cVal |= bVal << (7 - k);
847 CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
848 ((line1 >> (7 - k)) & 0x0400) |
849 ((line1_r >> (7 - k)) & 0x0040) |
850 ((line2_r >> (10 - k)) & 0x0008) |
851 ((line3_r >> (13 - k)) & 0x0001);
852 }
853 pLine[w >> 3] = cVal;
854 }
855 }
856 pLine += nStride;
857 if (h < GRHR + GRREFERENCEDY) {
858 pLineR += nStrideR;
859 }
860 }
861 return GRREG.release();
862 }
863
864 CJBig2_Image* CJBig2_GRRDProc::decode_Template1_unopt(
865 CJBig2_ArithDecoder* pArithDecoder,
866 JBig2ArithCtx* grContext) {
867 FX_BOOL LTP, SLTP, bVal;
868 FX_DWORD CONTEXT;
869 FX_DWORD line1, line2, line3, line4, line5;
870 LTP = 0;
871 nonstd::unique_ptr<CJBig2_Image> GRREG(new CJBig2_Image(GRW, GRH));
872 GRREG->fill(0);
873 for (FX_DWORD h = 0; h < GRH; h++) {
874 if (TPGRON) {
875 SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
876 LTP = LTP ^ SLTP;
877 }
878 if (LTP == 0) {
879 line1 = GRREG->getPixel(1, h - 1);
880 line1 |= GRREG->getPixel(0, h - 1) << 1;
881 line1 |= GRREG->getPixel(-1, h - 1) << 2;
882 line2 = 0;
883 line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
884 line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
885 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
886 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
887 << 2;
888 line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
889 line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
890 << 1;
891 for (FX_DWORD w = 0; w < GRW; w++) {
892 CONTEXT = line5;
893 CONTEXT |= line4 << 2;
894 CONTEXT |= line3 << 5;
895 CONTEXT |= line2 << 6;
896 CONTEXT |= line1 << 7;
897 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
898 GRREG->setPixel(w, h, bVal);
899 line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
900 line2 = ((line2 << 1) | bVal) & 0x01;
901 line3 = ((line3 << 1) |
902 GRREFERENCE->getPixel(w - GRREFERENCEDX + 1,
903 h - GRREFERENCEDY - 1)) &
904 0x01;
905 line4 =
906 ((line4 << 1) |
907 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
908 0x07;
909 line5 = ((line5 << 1) |
910 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2,
911 h - GRREFERENCEDY + 1)) &
912 0x03;
913 }
914 } else {
915 line1 = GRREG->getPixel(1, h - 1);
916 line1 |= GRREG->getPixel(0, h - 1) << 1;
917 line1 |= GRREG->getPixel(-1, h - 1) << 2;
918 line2 = 0;
919 line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
920 line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
921 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
922 line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
923 << 2;
924 line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
925 line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
926 << 1;
927 for (FX_DWORD w = 0; w < GRW; w++) {
928 bVal = GRREFERENCE->getPixel(w, h);
929 if (!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1)) &&
930 (bVal == GRREFERENCE->getPixel(w, h - 1)) &&
931 (bVal == GRREFERENCE->getPixel(w + 1, h - 1)) &&
932 (bVal == GRREFERENCE->getPixel(w - 1, h)) &&
933 (bVal == GRREFERENCE->getPixel(w + 1, h)) &&
934 (bVal == GRREFERENCE->getPixel(w - 1, h + 1)) &&
935 (bVal == GRREFERENCE->getPixel(w, h + 1)) &&
936 (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
937 CONTEXT = line5;
938 CONTEXT |= line4 << 2;
939 CONTEXT |= line3 << 5;
940 CONTEXT |= line2 << 6;
941 CONTEXT |= line1 << 7;
942 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
943 }
944 GRREG->setPixel(w, h, bVal);
945 line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
946 line2 = ((line2 << 1) | bVal) & 0x01;
947 line3 = ((line3 << 1) |
948 GRREFERENCE->getPixel(w - GRREFERENCEDX + 1,
949 h - GRREFERENCEDY - 1)) &
950 0x01;
951 line4 =
952 ((line4 << 1) |
953 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
954 0x07;
955 line5 = ((line5 << 1) |
956 GRREFERENCE->getPixel(w - GRREFERENCEDX + 2,
957 h - GRREFERENCEDY + 1)) &
958 0x03;
959 }
960 }
961 }
962 return GRREG.release();
963 }
964
965 CJBig2_Image* CJBig2_GRRDProc::decode_Template1_opt(
966 CJBig2_ArithDecoder* pArithDecoder,
967 JBig2ArithCtx* grContext) {
968 if (!GRREFERENCE->m_pData)
969 return nullptr;
970
971 FX_BOOL LTP, SLTP, bVal;
972 FX_DWORD CONTEXT;
973 FX_DWORD line1, line1_r, line2_r, line3_r;
974 uint8_t *pLine, *pLineR, cVal;
975 intptr_t nStride, nStrideR, nOffset;
976 int32_t k, nBits;
977 int32_t GRWR, GRHR;
978 int32_t GRW, GRH;
979 GRW = (int32_t)CJBig2_GRRDProc::GRW;
980 GRH = (int32_t)CJBig2_GRRDProc::GRH;
981 LTP = 0;
982 nonstd::unique_ptr<CJBig2_Image> GRREG(new CJBig2_Image(GRW, GRH));
983 if (!GRREG->m_pData)
984 return nullptr;
985
986 pLine = GRREG->m_pData;
987 pLineR = GRREFERENCE->m_pData;
988 nStride = GRREG->m_nStride;
989 nStrideR = GRREFERENCE->m_nStride;
990 GRWR = (int32_t)GRREFERENCE->m_nWidth;
991 GRHR = (int32_t)GRREFERENCE->m_nHeight;
992 if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
993 GRREFERENCEDY = 0;
994 }
995 nOffset = -GRREFERENCEDY * nStrideR;
996 for (int32_t h = 0; h < GRH; h++) {
997 if (TPGRON) {
998 SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
999 LTP = LTP ^ SLTP;
1000 }
1001 line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
1002 int32_t reference_h = h - GRREFERENCEDY;
1003 FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
1004 FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
1005 FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
1006 line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
1007 line2_r = line2_r_ok ? pLineR[nOffset] : 0;
1008 line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
1009 if (LTP == 0) {
1010 CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
1011 ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
1012 for (int32_t w = 0; w < GRW; w += 8) {
1013 nBits = GRW - w > 8 ? 8 : GRW - w;
1014 if (h > 0)
1015 line1 = (line1 << 8) |
1016 (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
1017 if (line1_r_ok)
1018 line1_r =
1019 (line1_r << 8) |
1020 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
1021 if (line2_r_ok)
1022 line2_r = (line2_r << 8) |
1023 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
1024 if (line3_r_ok) {
1025 line3_r =
1026 (line3_r << 8) |
1027 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
1028 } else {
1029 line3_r = 0;
1030 }
1031 cVal = 0;
1032 for (k = 0; k < nBits; k++) {
1033 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
1034 cVal |= bVal << (7 - k);
1035 CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
1036 ((line1 >> (7 - k)) & 0x0080) |
1037 ((line1_r >> (9 - k)) & 0x0020) |
1038 ((line2_r >> (11 - k)) & 0x0004) |
1039 ((line3_r >> (13 - k)) & 0x0001);
1040 }
1041 pLine[w >> 3] = cVal;
1042 }
1043 } else {
1044 CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
1045 ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
1046 for (int32_t w = 0; w < GRW; w += 8) {
1047 nBits = GRW - w > 8 ? 8 : GRW - w;
1048 if (h > 0)
1049 line1 = (line1 << 8) |
1050 (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
1051 if (line1_r_ok)
1052 line1_r =
1053 (line1_r << 8) |
1054 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
1055 if (line2_r_ok)
1056 line2_r = (line2_r << 8) |
1057 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
1058 if (line3_r_ok) {
1059 line3_r =
1060 (line3_r << 8) |
1061 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
1062 } else {
1063 line3_r = 0;
1064 }
1065 cVal = 0;
1066 for (k = 0; k < nBits; k++) {
1067 bVal = GRREFERENCE->getPixel(w + k, h);
1068 if (!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1)) &&
1069 (bVal == GRREFERENCE->getPixel(w + k, h - 1)) &&
1070 (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1)) &&
1071 (bVal == GRREFERENCE->getPixel(w + k - 1, h)) &&
1072 (bVal == GRREFERENCE->getPixel(w + k + 1, h)) &&
1073 (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1)) &&
1074 (bVal == GRREFERENCE->getPixel(w + k, h + 1)) &&
1075 (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
1076 bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
1077 }
1078 cVal |= bVal << (7 - k);
1079 CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
1080 ((line1 >> (7 - k)) & 0x0080) |
1081 ((line1_r >> (9 - k)) & 0x0020) |
1082 ((line2_r >> (11 - k)) & 0x0004) |
1083 ((line3_r >> (13 - k)) & 0x0001);
1084 }
1085 pLine[w >> 3] = cVal;
1086 }
1087 }
1088 pLine += nStride;
1089 if (h < GRHR + GRREFERENCEDY) {
1090 pLineR += nStrideR;
1091 }
1092 }
1093 return GRREG.release();
1094 }
1095
1096 CJBig2_Image* CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream* pStream,
1097 JBig2ArithCtx* grContext) {
1098 int32_t STRIPT, FIRSTS;
1099 FX_DWORD NINSTANCES;
1100 int32_t DT, DFS, CURS;
1101 uint8_t CURT;
1102 int32_t SI, TI;
1103 FX_DWORD IDI;
1104 CJBig2_Image* IBI;
1105 FX_DWORD WI, HI;
1106 int32_t IDS;
1107 FX_BOOL RI;
1108 int32_t RDWI, RDHI, RDXI, RDYI;
1109 CJBig2_Image* IBOI;
1110 FX_DWORD WOI, HOI;
1111 FX_BOOL bFirst;
1112 FX_DWORD nTmp;
1113 int32_t nVal, nBits;
1114 nonstd::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder(
1115 new CJBig2_HuffmanDecoder(pStream));
1116 nonstd::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH));
1117 SBREG->fill(SBDEFPIXEL);
1118 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0)
1119 return nullptr;
1120
1121 STRIPT *= SBSTRIPS;
1122 STRIPT = -STRIPT;
1123 FIRSTS = 0;
1124 NINSTANCES = 0;
1125 while (NINSTANCES < SBNUMINSTANCES) {
1126 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0)
1127 return nullptr;
1128
1129 DT *= SBSTRIPS;
1130 STRIPT = STRIPT + DT;
1131 bFirst = TRUE;
1132 for (;;) {
1133 if (bFirst) {
1134 if (pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0)
1135 return nullptr;
1136
1137 FIRSTS = FIRSTS + DFS;
1138 CURS = FIRSTS;
1139 bFirst = FALSE;
1140 } else {
1141 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS);
1142 if (nVal == JBIG2_OOB) {
1143 break;
1144 } else if (nVal != 0) {
1145 return nullptr;
1146 } else {
1147 CURS = CURS + IDS + SBDSOFFSET;
1148 }
1149 }
1150 if (SBSTRIPS == 1) {
1151 CURT = 0;
1152 } else {
1153 nTmp = 1;
1154 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) {
1155 nTmp++;
1156 }
1157 if (pStream->readNBits(nTmp, &nVal) != 0)
1158 return nullptr;
1159
1160 CURT = nVal;
1161 }
1162 TI = STRIPT + CURT;
1163 nVal = 0;
1164 nBits = 0;
1165 for (;;) {
1166 if (pStream->read1Bit(&nTmp) != 0)
1167 return nullptr;
1168
1169 nVal = (nVal << 1) | nTmp;
1170 nBits++;
1171 for (IDI = 0; IDI < SBNUMSYMS; IDI++) {
1172 if ((nBits == SBSYMCODES[IDI].codelen) &&
1173 (nVal == SBSYMCODES[IDI].code)) {
1174 break;
1175 }
1176 }
1177 if (IDI < SBNUMSYMS) {
1178 break;
1179 }
1180 }
1181 if (SBREFINE == 0) {
1182 RI = 0;
1183 } else {
1184 if (pStream->read1Bit(&RI) != 0) {
1185 return nullptr;
1186 }
1187 }
1188 if (RI == 0) {
1189 IBI = SBSYMS[IDI];
1190 } else {
1191 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0) ||
1192 (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0) ||
1193 (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0) ||
1194 (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0) ||
1195 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
1196 return nullptr;
1197 }
1198 pStream->alignByte();
1199 nTmp = pStream->getOffset();
1200 IBOI = SBSYMS[IDI];
1201 if (!IBOI)
1202 return nullptr;
1203
1204 WOI = IBOI->m_nWidth;
1205 HOI = IBOI->m_nHeight;
1206 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0)
1207 return nullptr;
1208
1209 nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc());
1210 pGRRD->GRW = WOI + RDWI;
1211 pGRRD->GRH = HOI + RDHI;
1212 pGRRD->GRTEMPLATE = SBRTEMPLATE;
1213 pGRRD->GRREFERENCE = IBOI;
1214 pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI;
1215 pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI;
1216 pGRRD->TPGRON = 0;
1217 pGRRD->GRAT[0] = SBRAT[0];
1218 pGRRD->GRAT[1] = SBRAT[1];
1219 pGRRD->GRAT[2] = SBRAT[2];
1220 pGRRD->GRAT[3] = SBRAT[3];
1221
1222 {
1223 nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder(
1224 new CJBig2_ArithDecoder(pStream));
1225 IBI = pGRRD->decode(pArithDecoder.get(), grContext);
1226 if (!IBI)
1227 return nullptr;
1228 }
1229
1230 pStream->alignByte();
1231 pStream->offset(2);
1232 if ((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
1233 delete IBI;
1234 return nullptr;
1235 }
1236 }
1237 if (!IBI) {
1238 continue;
1239 }
1240 WI = IBI->m_nWidth;
1241 HI = IBI->m_nHeight;
1242 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) ||
1243 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
1244 CURS = CURS + WI - 1;
1245 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) ||
1246 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
1247 CURS = CURS + HI - 1;
1248 }
1249 SI = CURS;
1250 if (TRANSPOSED == 0) {
1251 switch (REFCORNER) {
1252 case JBIG2_CORNER_TOPLEFT:
1253 SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
1254 break;
1255 case JBIG2_CORNER_TOPRIGHT:
1256 SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
1257 break;
1258 case JBIG2_CORNER_BOTTOMLEFT:
1259 SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
1260 break;
1261 case JBIG2_CORNER_BOTTOMRIGHT:
1262 SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
1263 break;
1264 }
1265 } else {
1266 switch (REFCORNER) {
1267 case JBIG2_CORNER_TOPLEFT:
1268 SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
1269 break;
1270 case JBIG2_CORNER_TOPRIGHT:
1271 SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
1272 break;
1273 case JBIG2_CORNER_BOTTOMLEFT:
1274 SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
1275 break;
1276 case JBIG2_CORNER_BOTTOMRIGHT:
1277 SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
1278 break;
1279 }
1280 }
1281 if (RI != 0) {
1282 delete IBI;
1283 }
1284 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
1285 (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
1286 CURS = CURS + WI - 1;
1287 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
1288 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
1289 CURS = CURS + HI - 1;
1290 }
1291 NINSTANCES = NINSTANCES + 1;
1292 }
1293 }
1294 return SBREG.release();
1295 }
1296
1297 CJBig2_Image* CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
1298 JBig2ArithCtx* grContext,
1299 JBig2IntDecoderState* pIDS) {
1300 int32_t STRIPT, FIRSTS;
1301 FX_DWORD NINSTANCES;
1302 int32_t DT, DFS, CURS;
1303 int32_t CURT;
1304 int32_t SI, TI;
1305 FX_DWORD IDI;
1306 CJBig2_Image* IBI;
1307 FX_DWORD WI, HI;
1308 int32_t IDS;
1309 int RI;
1310 int32_t RDWI, RDHI, RDXI, RDYI;
1311 CJBig2_Image* IBOI;
1312 FX_DWORD WOI, HOI;
1313 FX_BOOL bFirst;
1314 int32_t nRet, nVal;
1315 int32_t bRetained;
1316 CJBig2_ArithIntDecoder *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH,
1317 *IARDX, *IARDY;
1318 CJBig2_ArithIaidDecoder* IAID;
1319 if (pIDS) {
1320 IADT = pIDS->IADT;
1321 IAFS = pIDS->IAFS;
1322 IADS = pIDS->IADS;
1323 IAIT = pIDS->IAIT;
1324 IARI = pIDS->IARI;
1325 IARDW = pIDS->IARDW;
1326 IARDH = pIDS->IARDH;
1327 IARDX = pIDS->IARDX;
1328 IARDY = pIDS->IARDY;
1329 IAID = pIDS->IAID;
1330 bRetained = TRUE;
1331 } else {
1332 IADT = new CJBig2_ArithIntDecoder();
1333 IAFS = new CJBig2_ArithIntDecoder();
1334 IADS = new CJBig2_ArithIntDecoder();
1335 IAIT = new CJBig2_ArithIntDecoder();
1336 IARI = new CJBig2_ArithIntDecoder();
1337 IARDW = new CJBig2_ArithIntDecoder();
1338 IARDH = new CJBig2_ArithIntDecoder();
1339 IARDX = new CJBig2_ArithIntDecoder();
1340 IARDY = new CJBig2_ArithIntDecoder();
1341 IAID = new CJBig2_ArithIaidDecoder(SBSYMCODELEN);
1342 bRetained = FALSE;
1343 }
1344 nonstd::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH));
1345 SBREG->fill(SBDEFPIXEL);
1346 if (IADT->decode(pArithDecoder, &STRIPT) == -1) {
1347 goto failed;
1348 }
1349 STRIPT *= SBSTRIPS;
1350 STRIPT = -STRIPT;
1351 FIRSTS = 0;
1352 NINSTANCES = 0;
1353 while (NINSTANCES < SBNUMINSTANCES) {
1354 if (IADT->decode(pArithDecoder, &DT) == -1) {
1355 goto failed;
1356 }
1357 DT *= SBSTRIPS;
1358 STRIPT = STRIPT + DT;
1359 bFirst = TRUE;
1360 for (;;) {
1361 if (bFirst) {
1362 if (IAFS->decode(pArithDecoder, &DFS) == -1) {
1363 goto failed;
1364 }
1365 FIRSTS = FIRSTS + DFS;
1366 CURS = FIRSTS;
1367 bFirst = FALSE;
1368 } else {
1369 nRet = IADS->decode(pArithDecoder, &IDS);
1370 if (nRet == JBIG2_OOB) {
1371 break;
1372 } else if (nRet != 0) {
1373 goto failed;
1374 } else {
1375 CURS = CURS + IDS + SBDSOFFSET;
1376 }
1377 }
1378 if (NINSTANCES >= SBNUMINSTANCES) {
1379 break;
1380 }
1381 if (SBSTRIPS == 1) {
1382 CURT = 0;
1383 } else {
1384 if (IAIT->decode(pArithDecoder, &nVal) == -1) {
1385 goto failed;
1386 }
1387 CURT = nVal;
1388 }
1389 TI = STRIPT + CURT;
1390 if (IAID->decode(pArithDecoder, &nVal) == -1) {
1391 goto failed;
1392 }
1393 IDI = nVal;
1394 if (IDI >= SBNUMSYMS) {
1395 goto failed;
1396 }
1397 if (SBREFINE == 0) {
1398 RI = 0;
1399 } else {
1400 if (IARI->decode(pArithDecoder, &RI) == -1) {
1401 goto failed;
1402 }
1403 }
1404 if (!SBSYMS[IDI]) {
1405 goto failed;
1406 }
1407 if (RI == 0) {
1408 IBI = SBSYMS[IDI];
1409 } else {
1410 if ((IARDW->decode(pArithDecoder, &RDWI) == -1) ||
1411 (IARDH->decode(pArithDecoder, &RDHI) == -1) ||
1412 (IARDX->decode(pArithDecoder, &RDXI) == -1) ||
1413 (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
1414 goto failed;
1415 }
1416 IBOI = SBSYMS[IDI];
1417 WOI = IBOI->m_nWidth;
1418 HOI = IBOI->m_nHeight;
1419 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
1420 goto failed;
1421 }
1422 nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc());
1423 pGRRD->GRW = WOI + RDWI;
1424 pGRRD->GRH = HOI + RDHI;
1425 pGRRD->GRTEMPLATE = SBRTEMPLATE;
1426 pGRRD->GRREFERENCE = IBOI;
1427 pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI;
1428 pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI;
1429 pGRRD->TPGRON = 0;
1430 pGRRD->GRAT[0] = SBRAT[0];
1431 pGRRD->GRAT[1] = SBRAT[1];
1432 pGRRD->GRAT[2] = SBRAT[2];
1433 pGRRD->GRAT[3] = SBRAT[3];
1434 IBI = pGRRD->decode(pArithDecoder, grContext);
1435 if (!IBI)
1436 goto failed;
1437 }
1438 WI = IBI->m_nWidth;
1439 HI = IBI->m_nHeight;
1440 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) ||
1441 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
1442 CURS = CURS + WI - 1;
1443 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) ||
1444 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
1445 CURS = CURS + HI - 1;
1446 }
1447 SI = CURS;
1448 if (TRANSPOSED == 0) {
1449 switch (REFCORNER) {
1450 case JBIG2_CORNER_TOPLEFT:
1451 SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
1452 break;
1453 case JBIG2_CORNER_TOPRIGHT:
1454 SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
1455 break;
1456 case JBIG2_CORNER_BOTTOMLEFT:
1457 SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
1458 break;
1459 case JBIG2_CORNER_BOTTOMRIGHT:
1460 SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
1461 break;
1462 }
1463 } else {
1464 switch (REFCORNER) {
1465 case JBIG2_CORNER_TOPLEFT:
1466 SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
1467 break;
1468 case JBIG2_CORNER_TOPRIGHT:
1469 SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
1470 break;
1471 case JBIG2_CORNER_BOTTOMLEFT:
1472 SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
1473 break;
1474 case JBIG2_CORNER_BOTTOMRIGHT:
1475 SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
1476 break;
1477 }
1478 }
1479 if (RI != 0) {
1480 delete IBI;
1481 }
1482 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
1483 (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
1484 CURS = CURS + WI - 1;
1485 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
1486 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
1487 CURS = CURS + HI - 1;
1488 }
1489 NINSTANCES = NINSTANCES + 1;
1490 }
1491 }
1492 if (bRetained == FALSE) {
1493 delete IADT;
1494 delete IAFS;
1495 delete IADS;
1496 delete IAIT;
1497 delete IARI;
1498 delete IARDW;
1499 delete IARDH;
1500 delete IARDX;
1501 delete IARDY;
1502 delete IAID;
1503 }
1504 return SBREG.release();
1505 failed:
1506 if (bRetained == FALSE) {
1507 delete IADT;
1508 delete IAFS;
1509 delete IADS;
1510 delete IAIT;
1511 delete IARI;
1512 delete IARDW;
1513 delete IARDH;
1514 delete IARDX;
1515 delete IARDY;
1516 delete IAID;
1517 }
1518 return nullptr;
1519 }
1520
1521 CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith(
1522 CJBig2_ArithDecoder* pArithDecoder,
1523 JBig2ArithCtx* gbContext,
1524 JBig2ArithCtx* grContext) {
1525 CJBig2_Image** SDNEWSYMS;
1526 FX_DWORD HCHEIGHT, NSYMSDECODED;
1527 int32_t HCDH;
1528 FX_DWORD SYMWIDTH, TOTWIDTH;
1529 int32_t DW;
1530 CJBig2_Image* BS;
1531 FX_DWORD I, J, REFAGGNINST;
1532 FX_BOOL* EXFLAGS;
1533 FX_DWORD EXINDEX;
1534 FX_BOOL CUREXFLAG;
1535 FX_DWORD EXRUNLENGTH;
1536 int32_t nVal;
1537 FX_DWORD nTmp;
1538 FX_DWORD SBNUMSYMS;
1539 uint8_t SBSYMCODELEN;
1540 FX_DWORD IDI;
1541 int32_t RDXI, RDYI;
1542 CJBig2_Image** SBSYMS;
1543 nonstd::unique_ptr<CJBig2_ArithIaidDecoder> IAID;
1544 nonstd::unique_ptr<CJBig2_SymbolDict> pDict;
1545 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IADH(new CJBig2_ArithIntDecoder);
1546 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IADW(new CJBig2_ArithIntDecoder);
1547 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IAAI(new CJBig2_ArithIntDecoder);
1548 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IARDX(new CJBig2_ArithIntDecoder);
1549 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IARDY(new CJBig2_ArithIntDecoder);
1550 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IAEX(new CJBig2_ArithIntDecoder);
1551 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IADT(new CJBig2_ArithIntDecoder);
1552 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IAFS(new CJBig2_ArithIntDecoder);
1553 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IADS(new CJBig2_ArithIntDecoder);
1554 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IAIT(new CJBig2_ArithIntDecoder);
1555 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IARI(new CJBig2_ArithIntDecoder);
1556 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IARDW(new CJBig2_ArithIntDecoder);
1557 nonstd::unique_ptr<CJBig2_ArithIntDecoder> IARDH(new CJBig2_ArithIntDecoder);
1558 nTmp = 0;
1559 while ((FX_DWORD)(1 << nTmp) < (SDNUMINSYMS + SDNUMNEWSYMS)) {
1560 nTmp++;
1561 }
1562 IAID.reset(new CJBig2_ArithIaidDecoder((uint8_t)nTmp));
1563 SDNEWSYMS = FX_Alloc(CJBig2_Image*, SDNUMNEWSYMS);
1564 FXSYS_memset(SDNEWSYMS, 0, SDNUMNEWSYMS * sizeof(CJBig2_Image*));
1565
1566 HCHEIGHT = 0;
1567 NSYMSDECODED = 0;
1568 while (NSYMSDECODED < SDNUMNEWSYMS) {
1569 BS = nullptr;
1570 if (IADH->decode(pArithDecoder, &HCDH) == -1) {
1571 goto failed;
1572 }
1573 HCHEIGHT = HCHEIGHT + HCDH;
1574 if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
1575 goto failed;
1576 }
1577 SYMWIDTH = 0;
1578 TOTWIDTH = 0;
1579 for (;;) {
1580 nVal = IADW->decode(pArithDecoder, &DW);
1581 if (nVal == JBIG2_OOB) {
1582 break;
1583 } else if (nVal != 0) {
1584 goto failed;
1585 } else {
1586 if (NSYMSDECODED >= SDNUMNEWSYMS) {
1587 goto failed;
1588 }
1589 SYMWIDTH = SYMWIDTH + DW;
1590 if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
1591 goto failed;
1592 } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
1593 TOTWIDTH = TOTWIDTH + SYMWIDTH;
1594 SDNEWSYMS[NSYMSDECODED] = nullptr;
1595 NSYMSDECODED = NSYMSDECODED + 1;
1596 continue;
1597 }
1598 TOTWIDTH = TOTWIDTH + SYMWIDTH;
1599 }
1600 if (SDREFAGG == 0) {
1601 nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
1602 pGRD->MMR = 0;
1603 pGRD->GBW = SYMWIDTH;
1604 pGRD->GBH = HCHEIGHT;
1605 pGRD->GBTEMPLATE = SDTEMPLATE;
1606 pGRD->TPGDON = 0;
1607 pGRD->USESKIP = 0;
1608 pGRD->GBAT[0] = SDAT[0];
1609 pGRD->GBAT[1] = SDAT[1];
1610 pGRD->GBAT[2] = SDAT[2];
1611 pGRD->GBAT[3] = SDAT[3];
1612 pGRD->GBAT[4] = SDAT[4];
1613 pGRD->GBAT[5] = SDAT[5];
1614 pGRD->GBAT[6] = SDAT[6];
1615 pGRD->GBAT[7] = SDAT[7];
1616 BS = pGRD->decode_Arith(pArithDecoder, gbContext);
1617 if (!BS) {
1618 goto failed;
1619 }
1620 } else {
1621 if (IAAI->decode(pArithDecoder, (int*)&REFAGGNINST) == -1) {
1622 goto failed;
1623 }
1624 if (REFAGGNINST > 1) {
1625 nonstd::unique_ptr<CJBig2_TRDProc> pDecoder(new CJBig2_TRDProc());
1626 pDecoder->SBHUFF = SDHUFF;
1627 pDecoder->SBREFINE = 1;
1628 pDecoder->SBW = SYMWIDTH;
1629 pDecoder->SBH = HCHEIGHT;
1630 pDecoder->SBNUMINSTANCES = REFAGGNINST;
1631 pDecoder->SBSTRIPS = 1;
1632 pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
1633 SBNUMSYMS = pDecoder->SBNUMSYMS;
1634 nTmp = 0;
1635 while ((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
1636 nTmp++;
1637 }
1638 SBSYMCODELEN = (uint8_t)nTmp;
1639 pDecoder->SBSYMCODELEN = SBSYMCODELEN;
1640 SBSYMS = FX_Alloc(CJBig2_Image*, SBNUMSYMS);
1641 JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
1642 JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS,
1643 NSYMSDECODED * sizeof(CJBig2_Image*));
1644 pDecoder->SBSYMS = SBSYMS;
1645 pDecoder->SBDEFPIXEL = 0;
1646 pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
1647 pDecoder->TRANSPOSED = 0;
1648 pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
1649 pDecoder->SBDSOFFSET = 0;
1650 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFFS(
1651 new CJBig2_HuffmanTable(HuffmanTable_B6,
1652 FX_ArraySize(HuffmanTable_B6),
1653 HuffmanTable_HTOOB_B6));
1654 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFDS(
1655 new CJBig2_HuffmanTable(HuffmanTable_B8,
1656 FX_ArraySize(HuffmanTable_B8),
1657 HuffmanTable_HTOOB_B8));
1658 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFDT(
1659 new CJBig2_HuffmanTable(HuffmanTable_B11,
1660 FX_ArraySize(HuffmanTable_B11),
1661 HuffmanTable_HTOOB_B11));
1662 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDW(
1663 new CJBig2_HuffmanTable(HuffmanTable_B15,
1664 FX_ArraySize(HuffmanTable_B15),
1665 HuffmanTable_HTOOB_B15));
1666 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDH(
1667 new CJBig2_HuffmanTable(HuffmanTable_B15,
1668 FX_ArraySize(HuffmanTable_B15),
1669 HuffmanTable_HTOOB_B15));
1670 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDX(
1671 new CJBig2_HuffmanTable(HuffmanTable_B15,
1672 FX_ArraySize(HuffmanTable_B15),
1673 HuffmanTable_HTOOB_B15));
1674 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDY(
1675 new CJBig2_HuffmanTable(HuffmanTable_B15,
1676 FX_ArraySize(HuffmanTable_B15),
1677 HuffmanTable_HTOOB_B15));
1678 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRSIZE(
1679 new CJBig2_HuffmanTable(HuffmanTable_B1,
1680 FX_ArraySize(HuffmanTable_B1),
1681 HuffmanTable_HTOOB_B1));
1682 pDecoder->SBHUFFFS = SBHUFFFS.get();
1683 pDecoder->SBHUFFDS = SBHUFFDS.get();
1684 pDecoder->SBHUFFDT = SBHUFFDT.get();
1685 pDecoder->SBHUFFRDW = SBHUFFRDW.get();
1686 pDecoder->SBHUFFRDH = SBHUFFRDH.get();
1687 pDecoder->SBHUFFRDX = SBHUFFRDX.get();
1688 pDecoder->SBHUFFRDY = SBHUFFRDY.get();
1689 pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
1690 pDecoder->SBRTEMPLATE = SDRTEMPLATE;
1691 pDecoder->SBRAT[0] = SDRAT[0];
1692 pDecoder->SBRAT[1] = SDRAT[1];
1693 pDecoder->SBRAT[2] = SDRAT[2];
1694 pDecoder->SBRAT[3] = SDRAT[3];
1695 JBig2IntDecoderState ids;
1696 ids.IADT = IADT.get();
1697 ids.IAFS = IAFS.get();
1698 ids.IADS = IADS.get();
1699 ids.IAIT = IAIT.get();
1700 ids.IARI = IARI.get();
1701 ids.IARDW = IARDW.get();
1702 ids.IARDH = IARDH.get();
1703 ids.IARDX = IARDX.get();
1704 ids.IARDY = IARDY.get();
1705 ids.IAID = IAID.get();
1706 BS = pDecoder->decode_Arith(pArithDecoder, grContext, &ids);
1707 if (!BS) {
1708 FX_Free(SBSYMS);
1709 goto failed;
1710 }
1711 FX_Free(SBSYMS);
1712 } else if (REFAGGNINST == 1) {
1713 SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
1714 if (IAID->decode(pArithDecoder, (int*)&IDI) == -1) {
1715 goto failed;
1716 }
1717 if ((IARDX->decode(pArithDecoder, &RDXI) == -1) ||
1718 (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
1719 goto failed;
1720 }
1721 if (IDI >= SBNUMSYMS) {
1722 goto failed;
1723 }
1724 SBSYMS = FX_Alloc(CJBig2_Image*, SBNUMSYMS);
1725 JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
1726 JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS,
1727 NSYMSDECODED * sizeof(CJBig2_Image*));
1728 if (!SBSYMS[IDI]) {
1729 FX_Free(SBSYMS);
1730 goto failed;
1731 }
1732 nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc());
1733 pGRRD->GRW = SYMWIDTH;
1734 pGRRD->GRH = HCHEIGHT;
1735 pGRRD->GRTEMPLATE = SDRTEMPLATE;
1736 pGRRD->GRREFERENCE = SBSYMS[IDI];
1737 pGRRD->GRREFERENCEDX = RDXI;
1738 pGRRD->GRREFERENCEDY = RDYI;
1739 pGRRD->TPGRON = 0;
1740 pGRRD->GRAT[0] = SDRAT[0];
1741 pGRRD->GRAT[1] = SDRAT[1];
1742 pGRRD->GRAT[2] = SDRAT[2];
1743 pGRRD->GRAT[3] = SDRAT[3];
1744 BS = pGRRD->decode(pArithDecoder, grContext);
1745 if (!BS) {
1746 FX_Free(SBSYMS);
1747 goto failed;
1748 }
1749 FX_Free(SBSYMS);
1750 }
1751 }
1752 SDNEWSYMS[NSYMSDECODED] = BS;
1753 BS = nullptr;
1754 NSYMSDECODED = NSYMSDECODED + 1;
1755 }
1756 }
1757 EXINDEX = 0;
1758 CUREXFLAG = 0;
1759 EXFLAGS = FX_Alloc(FX_BOOL, SDNUMINSYMS + SDNUMNEWSYMS);
1760 while (EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
1761 if (IAEX->decode(pArithDecoder, (int*)&EXRUNLENGTH) == -1) {
1762 FX_Free(EXFLAGS);
1763 goto failed;
1764 }
1765 if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
1766 FX_Free(EXFLAGS);
1767 goto failed;
1768 }
1769 if (EXRUNLENGTH != 0) {
1770 for (I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
1771 EXFLAGS[I] = CUREXFLAG;
1772 }
1773 }
1774 EXINDEX = EXINDEX + EXRUNLENGTH;
1775 CUREXFLAG = !CUREXFLAG;
1776 }
1777 pDict.reset(new CJBig2_SymbolDict);
1778 pDict->SDNUMEXSYMS = SDNUMEXSYMS;
1779 pDict->SDEXSYMS = FX_Alloc(CJBig2_Image*, SDNUMEXSYMS);
1780 I = J = 0;
1781 for (I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
1782 if (EXFLAGS[I] && J < SDNUMEXSYMS) {
1783 if (I < SDNUMINSYMS) {
1784 pDict->SDEXSYMS[J] = new CJBig2_Image(*SDINSYMS[I]);
1785 } else {
1786 pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
1787 }
1788 J = J + 1;
1789 } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
1790 delete SDNEWSYMS[I - SDNUMINSYMS];
1791 }
1792 }
1793 if (J < SDNUMEXSYMS) {
1794 pDict->SDNUMEXSYMS = J;
1795 }
1796 FX_Free(EXFLAGS);
1797 FX_Free(SDNEWSYMS);
1798 return pDict.release();
1799 failed:
1800 for (I = 0; I < NSYMSDECODED; I++) {
1801 if (SDNEWSYMS[I]) {
1802 delete SDNEWSYMS[I];
1803 SDNEWSYMS[I] = nullptr;
1804 }
1805 }
1806 FX_Free(SDNEWSYMS);
1807 return nullptr;
1808 }
1809
1810 CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman(CJBig2_BitStream* pStream,
1811 JBig2ArithCtx* gbContext,
1812 JBig2ArithCtx* grContext,
1813 IFX_Pause* pPause) {
1814 CJBig2_Image** SDNEWSYMS;
1815 FX_DWORD* SDNEWSYMWIDTHS;
1816 FX_DWORD HCHEIGHT, NSYMSDECODED;
1817 int32_t HCDH;
1818 FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
1819 int32_t DW;
1820 CJBig2_Image *BS, *BHC;
1821 FX_DWORD I, J, REFAGGNINST;
1822 FX_BOOL* EXFLAGS;
1823 FX_DWORD EXINDEX;
1824 FX_BOOL CUREXFLAG;
1825 FX_DWORD EXRUNLENGTH;
1826 int32_t nVal, nBits;
1827 FX_DWORD nTmp;
1828 FX_DWORD SBNUMSYMS;
1829 uint8_t SBSYMCODELEN;
1830 JBig2HuffmanCode* SBSYMCODES;
1831 FX_DWORD IDI;
1832 int32_t RDXI, RDYI;
1833 FX_DWORD BMSIZE;
1834 FX_DWORD stride;
1835 CJBig2_Image** SBSYMS;
1836 nonstd::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder(
1837 new CJBig2_HuffmanDecoder(pStream));
1838 SDNEWSYMS = FX_Alloc(CJBig2_Image*, SDNUMNEWSYMS);
1839 FXSYS_memset(SDNEWSYMS, 0, SDNUMNEWSYMS * sizeof(CJBig2_Image*));
1840 SDNEWSYMWIDTHS = nullptr;
1841 BHC = nullptr;
1842 if (SDREFAGG == 0) {
1843 SDNEWSYMWIDTHS = FX_Alloc(FX_DWORD, SDNUMNEWSYMS);
1844 FXSYS_memset(SDNEWSYMWIDTHS, 0, SDNUMNEWSYMS * sizeof(FX_DWORD));
1845 }
1846 nonstd::unique_ptr<CJBig2_SymbolDict> pDict(new CJBig2_SymbolDict());
1847 nonstd::unique_ptr<CJBig2_HuffmanTable> pTable;
1848
1849 HCHEIGHT = 0;
1850 NSYMSDECODED = 0;
1851 BS = nullptr;
1852 while (NSYMSDECODED < SDNUMNEWSYMS) {
1853 if (pHuffmanDecoder->decodeAValue(SDHUFFDH, &HCDH) != 0) {
1854 goto failed;
1855 }
1856 HCHEIGHT = HCHEIGHT + HCDH;
1857 if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
1858 goto failed;
1859 }
1860 SYMWIDTH = 0;
1861 TOTWIDTH = 0;
1862 HCFIRSTSYM = NSYMSDECODED;
1863 for (;;) {
1864 nVal = pHuffmanDecoder->decodeAValue(SDHUFFDW, &DW);
1865 if (nVal == JBIG2_OOB) {
1866 break;
1867 } else if (nVal != 0) {
1868 goto failed;
1869 } else {
1870 if (NSYMSDECODED >= SDNUMNEWSYMS) {
1871 goto failed;
1872 }
1873 SYMWIDTH = SYMWIDTH + DW;
1874 if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
1875 goto failed;
1876 } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
1877 TOTWIDTH = TOTWIDTH + SYMWIDTH;
1878 SDNEWSYMS[NSYMSDECODED] = nullptr;
1879 NSYMSDECODED = NSYMSDECODED + 1;
1880 continue;
1881 }
1882 TOTWIDTH = TOTWIDTH + SYMWIDTH;
1883 }
1884 if (SDREFAGG == 1) {
1885 if (pHuffmanDecoder->decodeAValue(SDHUFFAGGINST, (int*)&REFAGGNINST) !=
1886 0) {
1887 goto failed;
1888 }
1889 BS = nullptr;
1890 if (REFAGGNINST > 1) {
1891 nonstd::unique_ptr<CJBig2_TRDProc> pDecoder(new CJBig2_TRDProc());
1892 pDecoder->SBHUFF = SDHUFF;
1893 pDecoder->SBREFINE = 1;
1894 pDecoder->SBW = SYMWIDTH;
1895 pDecoder->SBH = HCHEIGHT;
1896 pDecoder->SBNUMINSTANCES = REFAGGNINST;
1897 pDecoder->SBSTRIPS = 1;
1898 pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
1899 SBNUMSYMS = pDecoder->SBNUMSYMS;
1900 SBSYMCODES = FX_Alloc(JBig2HuffmanCode, SBNUMSYMS);
1901 nTmp = 1;
1902 while ((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
1903 nTmp++;
1904 }
1905 for (I = 0; I < SBNUMSYMS; I++) {
1906 SBSYMCODES[I].codelen = nTmp;
1907 SBSYMCODES[I].code = I;
1908 }
1909 pDecoder->SBSYMCODES = SBSYMCODES;
1910 SBSYMS = FX_Alloc(CJBig2_Image*, SBNUMSYMS);
1911 JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
1912 JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS,
1913 NSYMSDECODED * sizeof(CJBig2_Image*));
1914 pDecoder->SBSYMS = SBSYMS;
1915 pDecoder->SBDEFPIXEL = 0;
1916 pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
1917 pDecoder->TRANSPOSED = 0;
1918 pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
1919 pDecoder->SBDSOFFSET = 0;
1920 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFFS(
1921 new CJBig2_HuffmanTable(HuffmanTable_B6,
1922 FX_ArraySize(HuffmanTable_B6),
1923 HuffmanTable_HTOOB_B6));
1924 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFDS(
1925 new CJBig2_HuffmanTable(HuffmanTable_B8,
1926 FX_ArraySize(HuffmanTable_B8),
1927 HuffmanTable_HTOOB_B8));
1928 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFDT(
1929 new CJBig2_HuffmanTable(HuffmanTable_B11,
1930 FX_ArraySize(HuffmanTable_B11),
1931 HuffmanTable_HTOOB_B11));
1932 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDW(
1933 new CJBig2_HuffmanTable(HuffmanTable_B15,
1934 FX_ArraySize(HuffmanTable_B15),
1935 HuffmanTable_HTOOB_B15));
1936 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDH(
1937 new CJBig2_HuffmanTable(HuffmanTable_B15,
1938 FX_ArraySize(HuffmanTable_B15),
1939 HuffmanTable_HTOOB_B15));
1940 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDX(
1941 new CJBig2_HuffmanTable(HuffmanTable_B15,
1942 FX_ArraySize(HuffmanTable_B15),
1943 HuffmanTable_HTOOB_B15));
1944 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDY(
1945 new CJBig2_HuffmanTable(HuffmanTable_B15,
1946 FX_ArraySize(HuffmanTable_B15),
1947 HuffmanTable_HTOOB_B15));
1948 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRSIZE(
1949 new CJBig2_HuffmanTable(HuffmanTable_B1,
1950 FX_ArraySize(HuffmanTable_B1),
1951 HuffmanTable_HTOOB_B1));
1952 pDecoder->SBHUFFFS = SBHUFFFS.get();
1953 pDecoder->SBHUFFDS = SBHUFFDS.get();
1954 pDecoder->SBHUFFDT = SBHUFFDT.get();
1955 pDecoder->SBHUFFRDW = SBHUFFRDW.get();
1956 pDecoder->SBHUFFRDH = SBHUFFRDH.get();
1957 pDecoder->SBHUFFRDX = SBHUFFRDX.get();
1958 pDecoder->SBHUFFRDY = SBHUFFRDY.get();
1959 pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
1960 pDecoder->SBRTEMPLATE = SDRTEMPLATE;
1961 pDecoder->SBRAT[0] = SDRAT[0];
1962 pDecoder->SBRAT[1] = SDRAT[1];
1963 pDecoder->SBRAT[2] = SDRAT[2];
1964 pDecoder->SBRAT[3] = SDRAT[3];
1965 BS = pDecoder->decode_Huffman(pStream, grContext);
1966 if (!BS) {
1967 FX_Free(SBSYMCODES);
1968 FX_Free(SBSYMS);
1969 goto failed;
1970 }
1971 FX_Free(SBSYMCODES);
1972 FX_Free(SBSYMS);
1973 } else if (REFAGGNINST == 1) {
1974 SBNUMSYMS = SDNUMINSYMS + SDNUMNEWSYMS;
1975 nTmp = 1;
1976 while ((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
1977 nTmp++;
1978 }
1979 SBSYMCODELEN = (uint8_t)nTmp;
1980 SBSYMCODES = FX_Alloc(JBig2HuffmanCode, SBNUMSYMS);
1981 for (I = 0; I < SBNUMSYMS; I++) {
1982 SBSYMCODES[I].codelen = SBSYMCODELEN;
1983 SBSYMCODES[I].code = I;
1984 }
1985 nVal = 0;
1986 nBits = 0;
1987 for (;;) {
1988 if (pStream->read1Bit(&nTmp) != 0) {
1989 FX_Free(SBSYMCODES);
1990 goto failed;
1991 }
1992 nVal = (nVal << 1) | nTmp;
1993 for (IDI = 0; IDI < SBNUMSYMS; IDI++) {
1994 if ((nVal == SBSYMCODES[IDI].code) &&
1995 (nBits == SBSYMCODES[IDI].codelen)) {
1996 break;
1997 }
1998 }
1999 if (IDI < SBNUMSYMS) {
2000 break;
2001 }
2002 }
2003 FX_Free(SBSYMCODES);
2004 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDX(
2005 new CJBig2_HuffmanTable(HuffmanTable_B15,
2006 FX_ArraySize(HuffmanTable_B15),
2007 HuffmanTable_HTOOB_B15));
2008 nonstd::unique_ptr<CJBig2_HuffmanTable> SBHUFFRSIZE(
2009 new CJBig2_HuffmanTable(HuffmanTable_B1,
2010 FX_ArraySize(HuffmanTable_B1),
2011 HuffmanTable_HTOOB_B1));
2012 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDX.get(), &RDXI) != 0) ||
2013 (pHuffmanDecoder->decodeAValue(SBHUFFRDX.get(), &RDYI) != 0) ||
2014 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE.get(), &nVal) != 0)) {
2015 goto failed;
2016 }
2017 pStream->alignByte();
2018 nTmp = pStream->getOffset();
2019 SBSYMS = FX_Alloc(CJBig2_Image*, SBNUMSYMS);
2020 JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
2021 JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS,
2022 NSYMSDECODED * sizeof(CJBig2_Image*));
2023 nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc());
2024 pGRRD->GRW = SYMWIDTH;
2025 pGRRD->GRH = HCHEIGHT;
2026 pGRRD->GRTEMPLATE = SDRTEMPLATE;
2027 pGRRD->GRREFERENCE = SBSYMS[IDI];
2028 pGRRD->GRREFERENCEDX = RDXI;
2029 pGRRD->GRREFERENCEDY = RDYI;
2030 pGRRD->TPGRON = 0;
2031 pGRRD->GRAT[0] = SDRAT[0];
2032 pGRRD->GRAT[1] = SDRAT[1];
2033 pGRRD->GRAT[2] = SDRAT[2];
2034 pGRRD->GRAT[3] = SDRAT[3];
2035 nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder(
2036 new CJBig2_ArithDecoder(pStream));
2037 BS = pGRRD->decode(pArithDecoder.get(), grContext);
2038 if (!BS) {
2039 FX_Free(SBSYMS);
2040 goto failed;
2041 }
2042 pStream->alignByte();
2043 pStream->offset(2);
2044 if ((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
2045 delete BS;
2046 FX_Free(SBSYMS);
2047 goto failed;
2048 }
2049 FX_Free(SBSYMS);
2050 }
2051 SDNEWSYMS[NSYMSDECODED] = BS;
2052 }
2053 if (SDREFAGG == 0) {
2054 SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
2055 }
2056 NSYMSDECODED = NSYMSDECODED + 1;
2057 }
2058 if (SDREFAGG == 0) {
2059 if (pHuffmanDecoder->decodeAValue(SDHUFFBMSIZE, (int32_t*)&BMSIZE) != 0) {
2060 goto failed;
2061 }
2062 pStream->alignByte();
2063 if (BMSIZE == 0) {
2064 stride = (TOTWIDTH + 7) >> 3;
2065 if (pStream->getByteLeft() >= stride * HCHEIGHT) {
2066 BHC = new CJBig2_Image(TOTWIDTH, HCHEIGHT);
2067 for (I = 0; I < HCHEIGHT; I++) {
2068 JBIG2_memcpy(BHC->m_pData + I * BHC->m_nStride,
2069 pStream->getPointer(), stride);
2070 pStream->offset(stride);
2071 }
2072 } else {
2073 goto failed;
2074 }
2075 } else {
2076 nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
2077 pGRD->MMR = 1;
2078 pGRD->GBW = TOTWIDTH;
2079 pGRD->GBH = HCHEIGHT;
2080 FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHC, pStream);
2081 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2082 pGRD->Continue_decode(pPause);
2083 }
2084 pStream->alignByte();
2085 }
2086 nTmp = 0;
2087 if (!BHC) {
2088 continue;
2089 }
2090 for (I = HCFIRSTSYM; I < NSYMSDECODED; I++) {
2091 SDNEWSYMS[I] = BHC->subImage(nTmp, 0, SDNEWSYMWIDTHS[I], HCHEIGHT);
2092 nTmp += SDNEWSYMWIDTHS[I];
2093 }
2094 delete BHC;
2095 BHC = nullptr;
2096 }
2097 }
2098 EXINDEX = 0;
2099 CUREXFLAG = 0;
2100 pTable.reset(new CJBig2_HuffmanTable(
2101 HuffmanTable_B1, FX_ArraySize(HuffmanTable_B1), HuffmanTable_HTOOB_B1));
2102 EXFLAGS = FX_Alloc(FX_BOOL, SDNUMINSYMS + SDNUMNEWSYMS);
2103 while (EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
2104 if (pHuffmanDecoder->decodeAValue(pTable.get(), (int*)&EXRUNLENGTH) != 0) {
2105 FX_Free(EXFLAGS);
2106 goto failed;
2107 }
2108 if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
2109 FX_Free(EXFLAGS);
2110 goto failed;
2111 }
2112 if (EXRUNLENGTH != 0) {
2113 for (I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
2114 EXFLAGS[I] = CUREXFLAG;
2115 }
2116 }
2117 EXINDEX = EXINDEX + EXRUNLENGTH;
2118 CUREXFLAG = !CUREXFLAG;
2119 }
2120 pDict->SDNUMEXSYMS = SDNUMEXSYMS;
2121 pDict->SDEXSYMS = FX_Alloc(CJBig2_Image*, SDNUMEXSYMS);
2122 I = J = 0;
2123 for (I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
2124 if (EXFLAGS[I] && J < SDNUMEXSYMS) {
2125 if (I < SDNUMINSYMS) {
2126 pDict->SDEXSYMS[J] = new CJBig2_Image(*SDINSYMS[I]);
2127 } else {
2128 pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
2129 }
2130 J = J + 1;
2131 } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
2132 delete SDNEWSYMS[I - SDNUMINSYMS];
2133 }
2134 }
2135 if (J < SDNUMEXSYMS) {
2136 pDict->SDNUMEXSYMS = J;
2137 }
2138 FX_Free(EXFLAGS);
2139 FX_Free(SDNEWSYMS);
2140 if (SDREFAGG == 0) {
2141 FX_Free(SDNEWSYMWIDTHS);
2142 }
2143 return pDict.release();
2144 failed:
2145 for (I = 0; I < NSYMSDECODED; I++) {
2146 delete SDNEWSYMS[I];
2147 }
2148 FX_Free(SDNEWSYMS);
2149 if (SDREFAGG == 0) {
2150 FX_Free(SDNEWSYMWIDTHS);
2151 }
2152 return nullptr;
2153 }
2154
2155 CJBig2_Image* CJBig2_HTRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
2156 JBig2ArithCtx* gbContext,
2157 IFX_Pause* pPause) {
2158 FX_DWORD ng, mg;
2159 int32_t x, y;
2160 FX_DWORD HBPP;
2161 FX_DWORD* GI;
2162 nonstd::unique_ptr<CJBig2_Image> HSKIP;
2163 nonstd::unique_ptr<CJBig2_Image> HTREG(new CJBig2_Image(HBW, HBH));
2164 HTREG->fill(HDEFPIXEL);
2165 if (HENABLESKIP == 1) {
2166 HSKIP.reset(new CJBig2_Image(HGW, HGH));
2167 for (mg = 0; mg < HGH; mg++) {
2168 for (ng = 0; ng < HGW; ng++) {
2169 x = (HGX + mg * HRY + ng * HRX) >> 8;
2170 y = (HGY + mg * HRX - ng * HRY) >> 8;
2171 if ((x + HPW <= 0) | (x >= (int32_t)HBW) | (y + HPH <= 0) |
2172 (y >= (int32_t)HPH)) {
2173 HSKIP->setPixel(ng, mg, 1);
2174 } else {
2175 HSKIP->setPixel(ng, mg, 0);
2176 }
2177 }
2178 }
2179 }
2180 HBPP = 1;
2181 while ((FX_DWORD)(1 << HBPP) < HNUMPATS) {
2182 HBPP++;
2183 }
2184 nonstd::unique_ptr<CJBig2_GSIDProc> pGID(new CJBig2_GSIDProc());
2185 pGID->GSMMR = HMMR;
2186 pGID->GSW = HGW;
2187 pGID->GSH = HGH;
2188 pGID->GSBPP = (uint8_t)HBPP;
2189 pGID->GSUSESKIP = HENABLESKIP;
2190 pGID->GSKIP = HSKIP.get();
2191 pGID->GSTEMPLATE = HTEMPLATE;
2192 GI = pGID->decode_Arith(pArithDecoder, gbContext, pPause);
2193 if (!GI)
2194 return nullptr;
2195
2196 for (mg = 0; mg < HGH; mg++) {
2197 for (ng = 0; ng < HGW; ng++) {
2198 x = (HGX + mg * HRY + ng * HRX) >> 8;
2199 y = (HGY + mg * HRX - ng * HRY) >> 8;
2200 FX_DWORD pat_index = GI[mg * HGW + ng];
2201 if (pat_index >= HNUMPATS) {
2202 pat_index = HNUMPATS - 1;
2203 }
2204 HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
2205 }
2206 }
2207 FX_Free(GI);
2208 return HTREG.release();
2209 }
2210
2211 CJBig2_Image* CJBig2_HTRDProc::decode_MMR(CJBig2_BitStream* pStream,
2212 IFX_Pause* pPause) {
2213 FX_DWORD ng, mg;
2214 int32_t x, y;
2215 FX_DWORD* GI;
2216 nonstd::unique_ptr<CJBig2_Image> HTREG(new CJBig2_Image(HBW, HBH));
2217 HTREG->fill(HDEFPIXEL);
2218 FX_DWORD HBPP = 1;
2219 while ((FX_DWORD)(1 << HBPP) < HNUMPATS) {
2220 HBPP++;
2221 }
2222 nonstd::unique_ptr<CJBig2_GSIDProc> pGID(new CJBig2_GSIDProc());
2223 pGID->GSMMR = HMMR;
2224 pGID->GSW = HGW;
2225 pGID->GSH = HGH;
2226 pGID->GSBPP = (uint8_t)HBPP;
2227 pGID->GSUSESKIP = 0;
2228 GI = pGID->decode_MMR(pStream, pPause);
2229 if (!GI)
2230 return nullptr;
2231
2232 for (mg = 0; mg < HGH; mg++) {
2233 for (ng = 0; ng < HGW; ng++) {
2234 x = (HGX + mg * HRY + ng * HRX) >> 8;
2235 y = (HGY + mg * HRX - ng * HRY) >> 8;
2236 FX_DWORD pat_index = GI[mg * HGW + ng];
2237 if (pat_index >= HNUMPATS) {
2238 pat_index = HNUMPATS - 1;
2239 }
2240 HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
2241 }
2242 }
2243 FX_Free(GI);
2244 return HTREG.release();
2245 }
2246
2247 CJBig2_PatternDict* CJBig2_PDDProc::decode_Arith(
2248 CJBig2_ArithDecoder* pArithDecoder,
2249 JBig2ArithCtx* gbContext,
2250 IFX_Pause* pPause) {
2251 FX_DWORD GRAY;
2252 CJBig2_Image* BHDC = nullptr;
2253 nonstd::unique_ptr<CJBig2_PatternDict> pDict(new CJBig2_PatternDict());
2254 pDict->NUMPATS = GRAYMAX + 1;
2255 pDict->HDPATS = FX_Alloc(CJBig2_Image*, pDict->NUMPATS);
2256 JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*) * pDict->NUMPATS);
2257
2258 nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
2259 pGRD->MMR = HDMMR;
2260 pGRD->GBW = (GRAYMAX + 1) * HDPW;
2261 pGRD->GBH = HDPH;
2262 pGRD->GBTEMPLATE = HDTEMPLATE;
2263 pGRD->TPGDON = 0;
2264 pGRD->USESKIP = 0;
2265 pGRD->GBAT[0] = -(int32_t)HDPW;
2266 pGRD->GBAT[1] = 0;
2267 if (pGRD->GBTEMPLATE == 0) {
2268 pGRD->GBAT[2] = -3;
2269 pGRD->GBAT[3] = -1;
2270 pGRD->GBAT[4] = 2;
2271 pGRD->GBAT[5] = -2;
2272 pGRD->GBAT[6] = -2;
2273 pGRD->GBAT[7] = -2;
2274 }
2275 FXCODEC_STATUS status =
2276 pGRD->Start_decode_Arith(&BHDC, pArithDecoder, gbContext);
2277 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2278 pGRD->Continue_decode(pPause);
2279 }
2280 if (!BHDC)
2281 return nullptr;
2282
2283 GRAY = 0;
2284 while (GRAY <= GRAYMAX) {
2285 pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
2286 GRAY = GRAY + 1;
2287 }
2288 delete BHDC;
2289 return pDict.release();
2290 }
2291
2292 CJBig2_PatternDict* CJBig2_PDDProc::decode_MMR(CJBig2_BitStream* pStream,
2293 IFX_Pause* pPause) {
2294 FX_DWORD GRAY;
2295 CJBig2_Image* BHDC = nullptr;
2296 nonstd::unique_ptr<CJBig2_PatternDict> pDict(new CJBig2_PatternDict());
2297 pDict->NUMPATS = GRAYMAX + 1;
2298 pDict->HDPATS = FX_Alloc(CJBig2_Image*, pDict->NUMPATS);
2299 JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*) * pDict->NUMPATS);
2300
2301 nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
2302 pGRD->MMR = HDMMR;
2303 pGRD->GBW = (GRAYMAX + 1) * HDPW;
2304 pGRD->GBH = HDPH;
2305 FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHDC, pStream);
2306 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2307 pGRD->Continue_decode(pPause);
2308 }
2309 if (!BHDC)
2310 return nullptr;
2311
2312 GRAY = 0;
2313 while (GRAY <= GRAYMAX) {
2314 pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
2315 GRAY = GRAY + 1;
2316 }
2317 delete BHDC;
2318 return pDict.release();
2319 }
2320
2321 FX_DWORD* CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
2322 JBig2ArithCtx* gbContext,
2323 IFX_Pause* pPause) {
2324 CJBig2_Image** GSPLANES;
2325 int32_t J, K;
2326 FX_DWORD x, y;
2327 FX_DWORD* GSVALS;
2328 GSPLANES = FX_Alloc(CJBig2_Image*, GSBPP);
2329 GSVALS = FX_Alloc2D(FX_DWORD, GSW, GSH);
2330 JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*) * GSBPP);
2331 JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD) * GSW * GSH);
2332
2333 nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
2334 pGRD->MMR = GSMMR;
2335 pGRD->GBW = GSW;
2336 pGRD->GBH = GSH;
2337 pGRD->GBTEMPLATE = GSTEMPLATE;
2338 pGRD->TPGDON = 0;
2339 pGRD->USESKIP = GSUSESKIP;
2340 pGRD->SKIP = GSKIP;
2341 if (GSTEMPLATE <= 1) {
2342 pGRD->GBAT[0] = 3;
2343 } else {
2344 pGRD->GBAT[0] = 2;
2345 }
2346 pGRD->GBAT[1] = -1;
2347 if (pGRD->GBTEMPLATE == 0) {
2348 pGRD->GBAT[2] = -3;
2349 pGRD->GBAT[3] = -1;
2350 pGRD->GBAT[4] = 2;
2351 pGRD->GBAT[5] = -2;
2352 pGRD->GBAT[6] = -2;
2353 pGRD->GBAT[7] = -2;
2354 }
2355 FXCODEC_STATUS status =
2356 pGRD->Start_decode_Arith(&GSPLANES[GSBPP - 1], pArithDecoder, gbContext);
2357 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2358 pGRD->Continue_decode(pPause);
2359 }
2360 if (!GSPLANES[GSBPP - 1]) {
2361 goto failed;
2362 }
2363 J = GSBPP - 2;
2364 while (J >= 0) {
2365 FXCODEC_STATUS status =
2366 pGRD->Start_decode_Arith(&GSPLANES[J], pArithDecoder, gbContext);
2367 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2368 pGRD->Continue_decode(pPause);
2369 }
2370 if (!GSPLANES[J]) {
2371 for (K = GSBPP - 1; K > J; K--) {
2372 delete GSPLANES[K];
2373 goto failed;
2374 }
2375 }
2376 GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
2377 J = J - 1;
2378 }
2379 for (y = 0; y < GSH; y++) {
2380 for (x = 0; x < GSW; x++) {
2381 for (J = 0; J < GSBPP; J++) {
2382 GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
2383 }
2384 }
2385 }
2386 for (J = 0; J < GSBPP; J++) {
2387 delete GSPLANES[J];
2388 }
2389 FX_Free(GSPLANES);
2390 return GSVALS;
2391 failed:
2392 FX_Free(GSPLANES);
2393 FX_Free(GSVALS);
2394 return nullptr;
2395 }
2396
2397 FX_DWORD* CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream* pStream,
2398 IFX_Pause* pPause) {
2399 CJBig2_Image** GSPLANES;
2400 int32_t J, K;
2401 FX_DWORD x, y;
2402 FX_DWORD* GSVALS;
2403 GSPLANES = FX_Alloc(CJBig2_Image*, GSBPP);
2404 GSVALS = FX_Alloc2D(FX_DWORD, GSW, GSH);
2405 JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*) * GSBPP);
2406 JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD) * GSW * GSH);
2407
2408 nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
2409 pGRD->MMR = GSMMR;
2410 pGRD->GBW = GSW;
2411 pGRD->GBH = GSH;
2412 FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[GSBPP - 1], pStream);
2413 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2414 pGRD->Continue_decode(pPause);
2415 }
2416 if (!GSPLANES[GSBPP - 1]) {
2417 goto failed;
2418 }
2419 pStream->alignByte();
2420 pStream->offset(3);
2421 J = GSBPP - 2;
2422 while (J >= 0) {
2423 FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[J], pStream);
2424 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
2425 pGRD->Continue_decode(pPause);
2426 }
2427 if (!GSPLANES[J]) {
2428 for (K = GSBPP - 1; K > J; K--) {
2429 delete GSPLANES[K];
2430 goto failed;
2431 }
2432 }
2433 pStream->alignByte();
2434 pStream->offset(3);
2435 GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
2436 J = J - 1;
2437 }
2438 for (y = 0; y < GSH; y++) {
2439 for (x = 0; x < GSW; x++) {
2440 for (J = 0; J < GSBPP; J++) {
2441 GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
2442 }
2443 }
2444 }
2445 for (J = 0; J < GSBPP; J++) {
2446 delete GSPLANES[J];
2447 }
2448 FX_Free(GSPLANES);
2449 return GSVALS;
2450 failed:
2451 FX_Free(GSPLANES);
2452 FX_Free(GSVALS);
2453 return nullptr;
2454 }
2455
2456 FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith(
2457 CJBig2_Image** pImage,
2458 CJBig2_ArithDecoder* pArithDecoder,
2459 JBig2ArithCtx* gbContext,
2460 IFX_Pause* pPause) {
2461 if (GBW == 0 || GBH == 0) {
2462 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2463 return FXCODEC_STATUS_DECODE_FINISH;
2464 }
2465 m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
2466 m_pPause = pPause;
2467 if (!*pImage)
2468 *pImage = new CJBig2_Image(GBW, GBH);
2469 if (!(*pImage)->m_pData) {
2470 delete *pImage;
2471 *pImage = nullptr;
2472 m_ProssiveStatus = FXCODEC_STATUS_ERROR;
2473 return FXCODEC_STATUS_ERROR;
2474 }
2475 m_DecodeType = 1;
2476 m_pImage = pImage;
2477 (*m_pImage)->fill(0);
2478 m_pArithDecoder = pArithDecoder;
2479 m_gbContext = gbContext;
2480 LTP = 0;
2481 m_pLine = nullptr;
2482 m_loopIndex = 0;
2483 return decode_Arith(pPause);
2484 }
2485
2486 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith(IFX_Pause* pPause) {
2487 int iline = m_loopIndex;
2488 CJBig2_Image* pImage = *m_pImage;
2489 if (GBTEMPLATE == 0) {
2490 if (UseTemplate0Opt3()) {
2491 m_ProssiveStatus = decode_Arith_Template0_opt3(pImage, m_pArithDecoder,
2492 m_gbContext, pPause);
2493 } else {
2494 m_ProssiveStatus = decode_Arith_Template0_unopt(pImage, m_pArithDecoder,
2495 m_gbContext, pPause);
2496 }
2497 } else if (GBTEMPLATE == 1) {
2498 if (UseTemplate1Opt3()) {
2499 m_ProssiveStatus = decode_Arith_Template1_opt3(pImage, m_pArithDecoder,
2500 m_gbContext, pPause);
2501 } else {
2502 m_ProssiveStatus = decode_Arith_Template1_unopt(pImage, m_pArithDecoder,
2503 m_gbContext, pPause);
2504 }
2505 } else if (GBTEMPLATE == 2) {
2506 if (UseTemplate23Opt3()) {
2507 m_ProssiveStatus = decode_Arith_Template2_opt3(pImage, m_pArithDecoder,
2508 m_gbContext, pPause);
2509 } else {
2510 m_ProssiveStatus = decode_Arith_Template2_unopt(pImage, m_pArithDecoder,
2511 m_gbContext, pPause);
2512 }
2513 } else {
2514 if (UseTemplate23Opt3()) {
2515 m_ProssiveStatus = decode_Arith_Template3_opt3(pImage, m_pArithDecoder,
2516 m_gbContext, pPause);
2517 } else {
2518 m_ProssiveStatus = decode_Arith_Template3_unopt(pImage, m_pArithDecoder,
2519 m_gbContext, pPause);
2520 }
2521 }
2522 m_ReplaceRect.left = 0;
2523 m_ReplaceRect.right = pImage->m_nWidth;
2524 m_ReplaceRect.top = iline;
2525 m_ReplaceRect.bottom = m_loopIndex;
2526 if (m_ProssiveStatus == FXCODEC_STATUS_DECODE_FINISH) {
2527 m_loopIndex = 0;
2528 }
2529 return m_ProssiveStatus;
2530 }
2531
2532 FXCODEC_STATUS CJBig2_GRDProc::Start_decode_MMR(CJBig2_Image** pImage,
2533 CJBig2_BitStream* pStream,
2534 IFX_Pause* pPause) {
2535 int bitpos, i;
2536 *pImage = new CJBig2_Image(GBW, GBH);
2537 if (!(*pImage)->m_pData) {
2538 delete (*pImage);
2539 (*pImage) = nullptr;
2540 m_ProssiveStatus = FXCODEC_STATUS_ERROR;
2541 return m_ProssiveStatus;
2542 }
2543 bitpos = (int)pStream->getBitPos();
2544 _FaxG4Decode(pStream->getBuf(), pStream->getLength(), &bitpos,
2545 (*pImage)->m_pData, GBW, GBH, (*pImage)->m_nStride);
2546 pStream->setBitPos(bitpos);
2547 for (i = 0; (FX_DWORD)i < (*pImage)->m_nStride * GBH; i++) {
2548 (*pImage)->m_pData[i] = ~(*pImage)->m_pData[i];
2549 }
2550 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2551 return m_ProssiveStatus;
2552 }
2553
2554 FXCODEC_STATUS CJBig2_GRDProc::Continue_decode(IFX_Pause* pPause) {
2555 if (m_ProssiveStatus != FXCODEC_STATUS_DECODE_TOBECONTINUE)
2556 return m_ProssiveStatus;
2557
2558 if (m_DecodeType != 1) {
2559 m_ProssiveStatus = FXCODEC_STATUS_ERROR;
2560 return m_ProssiveStatus;
2561 }
2562
2563 return decode_Arith(pPause);
2564 }
2565
2566 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_opt3(
2567 CJBig2_Image* pImage,
2568 CJBig2_ArithDecoder* pArithDecoder,
2569 JBig2ArithCtx* gbContext,
2570 IFX_Pause* pPause) {
2571 FX_BOOL SLTP, bVal;
2572 FX_DWORD CONTEXT;
2573 FX_DWORD line1, line2;
2574 uint8_t *pLine1, *pLine2, cVal;
2575 int32_t nStride, nStride2, k;
2576 int32_t nLineBytes, nBitsLeft, cc;
2577 if (!m_pLine) {
2578 m_pLine = pImage->m_pData;
2579 }
2580 nStride = pImage->m_nStride;
2581 nStride2 = nStride << 1;
2582 nLineBytes = ((GBW + 7) >> 3) - 1;
2583 nBitsLeft = GBW - (nLineBytes << 3);
2584 FX_DWORD height = GBH & 0x7fffffff;
2585 for (; m_loopIndex < height; m_loopIndex++) {
2586 if (TPGDON) {
2587 SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
2588 LTP = LTP ^ SLTP;
2589 }
2590 if (LTP == 1) {
2591 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
2592 } else {
2593 if (m_loopIndex > 1) {
2594 pLine1 = m_pLine - nStride2;
2595 pLine2 = m_pLine - nStride;
2596 line1 = (*pLine1++) << 6;
2597 line2 = *pLine2++;
2598 CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
2599 for (cc = 0; cc < nLineBytes; cc++) {
2600 line1 = (line1 << 8) | ((*pLine1++) << 6);
2601 line2 = (line2 << 8) | (*pLine2++);
2602 cVal = 0;
2603 for (k = 7; k >= 0; k--) {
2604 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2605 cVal |= bVal << k;
2606 CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal |
2607 ((line1 >> k) & 0x0800) | ((line2 >> k) & 0x0010));
2608 }
2609 m_pLine[cc] = cVal;
2610 }
2611 line1 <<= 8;
2612 line2 <<= 8;
2613 cVal = 0;
2614 for (k = 0; k < nBitsLeft; k++) {
2615 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2616 cVal |= bVal << (7 - k);
2617 CONTEXT =
2618 (((CONTEXT & 0x7bf7) << 1) | bVal |
2619 ((line1 >> (7 - k)) & 0x0800) | ((line2 >> (7 - k)) & 0x0010));
2620 }
2621 m_pLine[nLineBytes] = cVal;
2622 } else {
2623 pLine2 = m_pLine - nStride;
2624 line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
2625 CONTEXT = (line2 & 0x07f0);
2626 for (cc = 0; cc < nLineBytes; cc++) {
2627 if (m_loopIndex & 1) {
2628 line2 = (line2 << 8) | (*pLine2++);
2629 }
2630 cVal = 0;
2631 for (k = 7; k >= 0; k--) {
2632 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2633 cVal |= bVal << k;
2634 CONTEXT =
2635 (((CONTEXT & 0x7bf7) << 1) | bVal | ((line2 >> k) & 0x0010));
2636 }
2637 m_pLine[cc] = cVal;
2638 }
2639 line2 <<= 8;
2640 cVal = 0;
2641 for (k = 0; k < nBitsLeft; k++) {
2642 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2643 cVal |= bVal << (7 - k);
2644 CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal |
2645 ((line2 >> (7 - k)) & 0x0010));
2646 }
2647 m_pLine[nLineBytes] = cVal;
2648 }
2649 }
2650 m_pLine += nStride;
2651 if (pPause && pPause->NeedToPauseNow()) {
2652 m_loopIndex++;
2653 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
2654 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
2655 }
2656 }
2657 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2658 return FXCODEC_STATUS_DECODE_FINISH;
2659 }
2660
2661 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_unopt(
2662 CJBig2_Image* pImage,
2663 CJBig2_ArithDecoder* pArithDecoder,
2664 JBig2ArithCtx* gbContext,
2665 IFX_Pause* pPause) {
2666 FX_BOOL SLTP, bVal;
2667 FX_DWORD CONTEXT;
2668 FX_DWORD line1, line2, line3;
2669 for (; m_loopIndex < GBH; m_loopIndex++) {
2670 if (TPGDON) {
2671 SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
2672 LTP = LTP ^ SLTP;
2673 }
2674 if (LTP == 1) {
2675 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
2676 } else {
2677 line1 = pImage->getPixel(1, m_loopIndex - 2);
2678 line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
2679 line2 = pImage->getPixel(2, m_loopIndex - 1);
2680 line2 |= pImage->getPixel(1, m_loopIndex - 1) << 1;
2681 line2 |= pImage->getPixel(0, m_loopIndex - 1) << 2;
2682 line3 = 0;
2683 for (FX_DWORD w = 0; w < GBW; w++) {
2684 if (USESKIP && SKIP->getPixel(w, m_loopIndex)) {
2685 bVal = 0;
2686 } else {
2687 CONTEXT = line3;
2688 CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
2689 CONTEXT |= line2 << 5;
2690 CONTEXT |= pImage->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
2691 CONTEXT |= pImage->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
2692 CONTEXT |= line1 << 12;
2693 CONTEXT |= pImage->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
2694 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2695 }
2696 if (bVal) {
2697 pImage->setPixel(w, m_loopIndex, bVal);
2698 }
2699 line1 =
2700 ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
2701 line2 =
2702 ((line2 << 1) | pImage->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
2703 line3 = ((line3 << 1) | bVal) & 0x0f;
2704 }
2705 }
2706 if (pPause && pPause->NeedToPauseNow()) {
2707 m_loopIndex++;
2708 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
2709 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
2710 }
2711 }
2712 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2713 return FXCODEC_STATUS_DECODE_FINISH;
2714 }
2715
2716 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_opt3(
2717 CJBig2_Image* pImage,
2718 CJBig2_ArithDecoder* pArithDecoder,
2719 JBig2ArithCtx* gbContext,
2720 IFX_Pause* pPause) {
2721 FX_BOOL SLTP, bVal;
2722 FX_DWORD CONTEXT;
2723 FX_DWORD line1, line2;
2724 uint8_t *pLine1, *pLine2, cVal;
2725 int32_t nStride, nStride2, k;
2726 int32_t nLineBytes, nBitsLeft, cc;
2727 if (!m_pLine) {
2728 m_pLine = pImage->m_pData;
2729 }
2730 nStride = pImage->m_nStride;
2731 nStride2 = nStride << 1;
2732 nLineBytes = ((GBW + 7) >> 3) - 1;
2733 nBitsLeft = GBW - (nLineBytes << 3);
2734 for (; m_loopIndex < GBH; m_loopIndex++) {
2735 if (TPGDON) {
2736 SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
2737 LTP = LTP ^ SLTP;
2738 }
2739 if (LTP == 1) {
2740 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
2741 } else {
2742 if (m_loopIndex > 1) {
2743 pLine1 = m_pLine - nStride2;
2744 pLine2 = m_pLine - nStride;
2745 line1 = (*pLine1++) << 4;
2746 line2 = *pLine2++;
2747 CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
2748 for (cc = 0; cc < nLineBytes; cc++) {
2749 line1 = (line1 << 8) | ((*pLine1++) << 4);
2750 line2 = (line2 << 8) | (*pLine2++);
2751 cVal = 0;
2752 for (k = 7; k >= 0; k--) {
2753 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2754 cVal |= bVal << k;
2755 CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal |
2756 ((line1 >> k) & 0x0200) | ((line2 >> (k + 1)) & 0x0008);
2757 }
2758 m_pLine[cc] = cVal;
2759 }
2760 line1 <<= 8;
2761 line2 <<= 8;
2762 cVal = 0;
2763 for (k = 0; k < nBitsLeft; k++) {
2764 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2765 cVal |= bVal << (7 - k);
2766 CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal |
2767 ((line1 >> (7 - k)) & 0x0200) |
2768 ((line2 >> (8 - k)) & 0x0008);
2769 }
2770 m_pLine[nLineBytes] = cVal;
2771 } else {
2772 pLine2 = m_pLine - nStride;
2773 line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
2774 CONTEXT = (line2 >> 1) & 0x01f8;
2775 for (cc = 0; cc < nLineBytes; cc++) {
2776 if (m_loopIndex & 1) {
2777 line2 = (line2 << 8) | (*pLine2++);
2778 }
2779 cVal = 0;
2780 for (k = 7; k >= 0; k--) {
2781 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2782 cVal |= bVal << k;
2783 CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal |
2784 ((line2 >> (k + 1)) & 0x0008);
2785 }
2786 m_pLine[cc] = cVal;
2787 }
2788 line2 <<= 8;
2789 cVal = 0;
2790 for (k = 0; k < nBitsLeft; k++) {
2791 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2792 cVal |= bVal << (7 - k);
2793 CONTEXT =
2794 ((CONTEXT & 0x0efb) << 1) | bVal | ((line2 >> (8 - k)) & 0x0008);
2795 }
2796 m_pLine[nLineBytes] = cVal;
2797 }
2798 }
2799 m_pLine += nStride;
2800 if (pPause && pPause->NeedToPauseNow()) {
2801 m_loopIndex++;
2802 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
2803 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
2804 }
2805 }
2806 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2807 return FXCODEC_STATUS_DECODE_FINISH;
2808 }
2809
2810 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_unopt(
2811 CJBig2_Image* pImage,
2812 CJBig2_ArithDecoder* pArithDecoder,
2813 JBig2ArithCtx* gbContext,
2814 IFX_Pause* pPause) {
2815 FX_BOOL SLTP, bVal;
2816 FX_DWORD CONTEXT;
2817 FX_DWORD line1, line2, line3;
2818 for (FX_DWORD h = 0; h < GBH; h++) {
2819 if (TPGDON) {
2820 SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
2821 LTP = LTP ^ SLTP;
2822 }
2823 if (LTP == 1) {
2824 pImage->copyLine(h, h - 1);
2825 } else {
2826 line1 = pImage->getPixel(2, h - 2);
2827 line1 |= pImage->getPixel(1, h - 2) << 1;
2828 line1 |= pImage->getPixel(0, h - 2) << 2;
2829 line2 = pImage->getPixel(2, h - 1);
2830 line2 |= pImage->getPixel(1, h - 1) << 1;
2831 line2 |= pImage->getPixel(0, h - 1) << 2;
2832 line3 = 0;
2833 for (FX_DWORD w = 0; w < GBW; w++) {
2834 if (USESKIP && SKIP->getPixel(w, h)) {
2835 bVal = 0;
2836 } else {
2837 CONTEXT = line3;
2838 CONTEXT |= pImage->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
2839 CONTEXT |= line2 << 4;
2840 CONTEXT |= line1 << 9;
2841 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2842 }
2843 if (bVal) {
2844 pImage->setPixel(w, h, bVal);
2845 }
2846 line1 = ((line1 << 1) | pImage->getPixel(w + 3, h - 2)) & 0x0f;
2847 line2 = ((line2 << 1) | pImage->getPixel(w + 3, h - 1)) & 0x1f;
2848 line3 = ((line3 << 1) | bVal) & 0x07;
2849 }
2850 }
2851 if (pPause && pPause->NeedToPauseNow()) {
2852 m_loopIndex++;
2853 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
2854 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
2855 }
2856 }
2857 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2858 return FXCODEC_STATUS_DECODE_FINISH;
2859 }
2860
2861 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_opt3(
2862 CJBig2_Image* pImage,
2863 CJBig2_ArithDecoder* pArithDecoder,
2864 JBig2ArithCtx* gbContext,
2865 IFX_Pause* pPause) {
2866 FX_BOOL SLTP, bVal;
2867 FX_DWORD CONTEXT;
2868 FX_DWORD line1, line2;
2869 uint8_t *pLine1, *pLine2, cVal;
2870 int32_t nStride, nStride2, k;
2871 int32_t nLineBytes, nBitsLeft, cc;
2872 if (!m_pLine) {
2873 m_pLine = pImage->m_pData;
2874 }
2875 nStride = pImage->m_nStride;
2876 nStride2 = nStride << 1;
2877 nLineBytes = ((GBW + 7) >> 3) - 1;
2878 nBitsLeft = GBW - (nLineBytes << 3);
2879 for (; m_loopIndex < GBH; m_loopIndex++) {
2880 if (TPGDON) {
2881 SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
2882 LTP = LTP ^ SLTP;
2883 }
2884 if (LTP == 1) {
2885 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
2886 } else {
2887 if (m_loopIndex > 1) {
2888 pLine1 = m_pLine - nStride2;
2889 pLine2 = m_pLine - nStride;
2890 line1 = (*pLine1++) << 1;
2891 line2 = *pLine2++;
2892 CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
2893 for (cc = 0; cc < nLineBytes; cc++) {
2894 line1 = (line1 << 8) | ((*pLine1++) << 1);
2895 line2 = (line2 << 8) | (*pLine2++);
2896 cVal = 0;
2897 for (k = 7; k >= 0; k--) {
2898 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2899 cVal |= bVal << k;
2900 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
2901 ((line1 >> k) & 0x0080) | ((line2 >> (k + 3)) & 0x0004);
2902 }
2903 m_pLine[cc] = cVal;
2904 }
2905 line1 <<= 8;
2906 line2 <<= 8;
2907 cVal = 0;
2908 for (k = 0; k < nBitsLeft; k++) {
2909 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2910 cVal |= bVal << (7 - k);
2911 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
2912 ((line1 >> (7 - k)) & 0x0080) |
2913 ((line2 >> (10 - k)) & 0x0004);
2914 }
2915 m_pLine[nLineBytes] = cVal;
2916 } else {
2917 pLine2 = m_pLine - nStride;
2918 line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
2919 CONTEXT = (line2 >> 3) & 0x007c;
2920 for (cc = 0; cc < nLineBytes; cc++) {
2921 if (m_loopIndex & 1) {
2922 line2 = (line2 << 8) | (*pLine2++);
2923 }
2924 cVal = 0;
2925 for (k = 7; k >= 0; k--) {
2926 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2927 cVal |= bVal << k;
2928 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
2929 ((line2 >> (k + 3)) & 0x0004);
2930 }
2931 m_pLine[cc] = cVal;
2932 }
2933 line2 <<= 8;
2934 cVal = 0;
2935 for (k = 0; k < nBitsLeft; k++) {
2936 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2937 cVal |= bVal << (7 - k);
2938 CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal |
2939 (((line2 >> (10 - k))) & 0x0004);
2940 }
2941 m_pLine[nLineBytes] = cVal;
2942 }
2943 }
2944 m_pLine += nStride;
2945 if (pPause && m_loopIndex % 50 == 0 && pPause->NeedToPauseNow()) {
2946 m_loopIndex++;
2947 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
2948 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
2949 }
2950 }
2951 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
2952 return FXCODEC_STATUS_DECODE_FINISH;
2953 }
2954
2955 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_unopt(
2956 CJBig2_Image* pImage,
2957 CJBig2_ArithDecoder* pArithDecoder,
2958 JBig2ArithCtx* gbContext,
2959 IFX_Pause* pPause) {
2960 FX_BOOL SLTP, bVal;
2961 FX_DWORD CONTEXT;
2962 FX_DWORD line1, line2, line3;
2963 for (; m_loopIndex < GBH; m_loopIndex++) {
2964 if (TPGDON) {
2965 SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
2966 LTP = LTP ^ SLTP;
2967 }
2968 if (LTP == 1) {
2969 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
2970 } else {
2971 line1 = pImage->getPixel(1, m_loopIndex - 2);
2972 line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
2973 line2 = pImage->getPixel(1, m_loopIndex - 1);
2974 line2 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
2975 line3 = 0;
2976 for (FX_DWORD w = 0; w < GBW; w++) {
2977 if (USESKIP && SKIP->getPixel(w, m_loopIndex)) {
2978 bVal = 0;
2979 } else {
2980 CONTEXT = line3;
2981 CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
2982 CONTEXT |= line2 << 3;
2983 CONTEXT |= line1 << 7;
2984 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
2985 }
2986 if (bVal) {
2987 pImage->setPixel(w, m_loopIndex, bVal);
2988 }
2989 line1 =
2990 ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
2991 line2 =
2992 ((line2 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
2993 line3 = ((line3 << 1) | bVal) & 0x03;
2994 }
2995 }
2996 if (pPause && pPause->NeedToPauseNow()) {
2997 m_loopIndex++;
2998 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
2999 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
3000 }
3001 }
3002 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
3003 return FXCODEC_STATUS_DECODE_FINISH;
3004 }
3005
3006 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_opt3(
3007 CJBig2_Image* pImage,
3008 CJBig2_ArithDecoder* pArithDecoder,
3009 JBig2ArithCtx* gbContext,
3010 IFX_Pause* pPause) {
3011 FX_BOOL SLTP, bVal;
3012 FX_DWORD CONTEXT;
3013 FX_DWORD line1;
3014 uint8_t *pLine1, cVal;
3015 int32_t nStride, k;
3016 int32_t nLineBytes, nBitsLeft, cc;
3017 if (!m_pLine) {
3018 m_pLine = pImage->m_pData;
3019 }
3020 nStride = pImage->m_nStride;
3021 nLineBytes = ((GBW + 7) >> 3) - 1;
3022 nBitsLeft = GBW - (nLineBytes << 3);
3023 for (; m_loopIndex < GBH; m_loopIndex++) {
3024 if (TPGDON) {
3025 SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
3026 LTP = LTP ^ SLTP;
3027 }
3028 if (LTP == 1) {
3029 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
3030 } else {
3031 if (m_loopIndex > 0) {
3032 pLine1 = m_pLine - nStride;
3033 line1 = *pLine1++;
3034 CONTEXT = (line1 >> 1) & 0x03f0;
3035 for (cc = 0; cc < nLineBytes; cc++) {
3036 line1 = (line1 << 8) | (*pLine1++);
3037 cVal = 0;
3038 for (k = 7; k >= 0; k--) {
3039 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
3040 cVal |= bVal << k;
3041 CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal |
3042 ((line1 >> (k + 1)) & 0x0010);
3043 }
3044 m_pLine[cc] = cVal;
3045 }
3046 line1 <<= 8;
3047 cVal = 0;
3048 for (k = 0; k < nBitsLeft; k++) {
3049 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
3050 cVal |= bVal << (7 - k);
3051 CONTEXT =
3052 ((CONTEXT & 0x01f7) << 1) | bVal | ((line1 >> (8 - k)) & 0x0010);
3053 }
3054 m_pLine[nLineBytes] = cVal;
3055 } else {
3056 CONTEXT = 0;
3057 for (cc = 0; cc < nLineBytes; cc++) {
3058 cVal = 0;
3059 for (k = 7; k >= 0; k--) {
3060 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
3061 cVal |= bVal << k;
3062 CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
3063 }
3064 m_pLine[cc] = cVal;
3065 }
3066 cVal = 0;
3067 for (k = 0; k < nBitsLeft; k++) {
3068 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
3069 cVal |= bVal << (7 - k);
3070 CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
3071 }
3072 m_pLine[nLineBytes] = cVal;
3073 }
3074 }
3075 m_pLine += nStride;
3076 if (pPause && pPause->NeedToPauseNow()) {
3077 m_loopIndex++;
3078 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
3079 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
3080 }
3081 }
3082 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
3083 return FXCODEC_STATUS_DECODE_FINISH;
3084 }
3085
3086 FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_unopt(
3087 CJBig2_Image* pImage,
3088 CJBig2_ArithDecoder* pArithDecoder,
3089 JBig2ArithCtx* gbContext,
3090 IFX_Pause* pPause) {
3091 FX_BOOL SLTP, bVal;
3092 FX_DWORD CONTEXT;
3093 FX_DWORD line1, line2;
3094 for (; m_loopIndex < GBH; m_loopIndex++) {
3095 if (TPGDON) {
3096 SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
3097 LTP = LTP ^ SLTP;
3098 }
3099 if (LTP == 1) {
3100 pImage->copyLine(m_loopIndex, m_loopIndex - 1);
3101 } else {
3102 line1 = pImage->getPixel(1, m_loopIndex - 1);
3103 line1 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
3104 line2 = 0;
3105 for (FX_DWORD w = 0; w < GBW; w++) {
3106 if (USESKIP && SKIP->getPixel(w, m_loopIndex)) {
3107 bVal = 0;
3108 } else {
3109 CONTEXT = line2;
3110 CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
3111 CONTEXT |= line1 << 5;
3112 bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
3113 }
3114 if (bVal) {
3115 pImage->setPixel(w, m_loopIndex, bVal);
3116 }
3117 line1 =
3118 ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
3119 line2 = ((line2 << 1) | bVal) & 0x0f;
3120 }
3121 }
3122 if (pPause && pPause->NeedToPauseNow()) {
3123 m_loopIndex++;
3124 m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
3125 return FXCODEC_STATUS_DECODE_TOBECONTINUE;
3126 }
3127 }
3128 m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
3129 return FXCODEC_STATUS_DECODE_FINISH;
3130 }
OLDNEW
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h ('k') | core/src/fxcodec/jbig2/JBig2_GrdProc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698