7#include "core/fxcodec/jbig2/JBig2_GrrdProc.h"
11#include "core/fxcodec/jbig2/JBig2_ArithDecoder.h"
12#include "core/fxcodec/jbig2/JBig2_BitStream.h"
13#include "core/fxcodec/jbig2/JBig2_Image.h"
26 if ((GRAT[0] == -1) && (GRAT[1] == -1) && (GRAT[2] == -1) &&
27 (GRAT[3] == -1) && (GRREFERENCEDX == 0) &&
28 (GRW == (uint32_t)GRREFERENCE->width())) {
29 return DecodeTemplate0Opt(pArithDecoder, grContext);
31 return DecodeTemplate0Unopt(pArithDecoder, grContext);
34 if ((GRREFERENCEDX == 0) && (GRW == (uint32_t)GRREFERENCE->width()))
35 return DecodeTemplate1Opt(pArithDecoder, grContext);
37 return DecodeTemplate1Unopt(pArithDecoder, grContext);
49 for (uint32_t h = 0; h <
GRH; h++) {
54 LTP = LTP ^ pArithDecoder
->Decode(&grContext[0x0010]
);
57 lines[0] = GRREG->GetPixel(1, h - 1);
58 lines[0] |= GRREG->GetPixel(0, h - 1) << 1;
60 lines[2] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
61 lines[2] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1)
63 lines[3] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
64 lines[3] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
65 lines[3] |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
67 lines[4] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
68 lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
70 lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1)
73 for (uint32_t w = 0; w <
GRW; w++) {
75 DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
79 int bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
80 DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
83 for (uint32_t w = 0; w <
GRW; w++) {
84 int bVal = GRREFERENCE->GetPixel(w, h);
85 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w - 1, h - 1)) &&
86 (bVal == GRREFERENCE->GetPixel(w, h - 1)) &&
87 (bVal == GRREFERENCE->GetPixel(w + 1, h - 1)) &&
88 (bVal == GRREFERENCE->GetPixel(w - 1, h)) &&
89 (bVal == GRREFERENCE->GetPixel(w + 1, h)) &&
90 (bVal == GRREFERENCE->GetPixel(w - 1, h + 1)) &&
91 (bVal == GRREFERENCE->GetPixel(w, h + 1)) &&
92 (bVal == GRREFERENCE->GetPixel(w + 1, h + 1)))) {
94 DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
98 bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
100 DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
109 const uint32_t* lines,
112 uint32_t CONTEXT = lines[4];
113 CONTEXT |= lines[3] << 3;
114 CONTEXT |= lines[2] << 6;
115 CONTEXT |= GRREFERENCE->GetPixel(w - GRREFERENCEDX + GRAT[2],
116 h - GRREFERENCEDY + GRAT[3])
118 CONTEXT |= lines[1] << 9;
119 CONTEXT |= lines[0] << 10;
130 lines[0] = ((lines[0] << 1) | GRREG
->GetPixel(w + 2
, h - 1
)) & 0x03;
131 lines[1] = ((lines[1] << 1) | bVal) & 0x01;
132 lines[2] = ((lines[2] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
133 h - GRREFERENCEDY - 1)) &
135 lines[3] = ((lines[3] << 1) |
136 GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
138 lines[4] = ((lines[4] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
139 h - GRREFERENCEDY + 1)) &
146 if (!GRREFERENCE->data())
149 int32_t iGRW =
static_cast<int32_t>(
GRW);
150 int32_t iGRH =
static_cast<int32_t>(
GRH);
156 uint8_t* pLine = GRREG->data();
157 uint8_t* pLineR = GRREFERENCE->data();
158 intptr_t nStride = GRREG->stride();
159 intptr_t nStrideR = GRREFERENCE->stride();
160 int32_t GRWR = GRREFERENCE->width();
161 int32_t GRHR = GRREFERENCE->height();
165 for (int32_t h = 0; h < iGRH; h++) {
170 LTP = LTP ^ pArithDecoder
->Decode(&grContext[0x0010]
);
172 uint32_t line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
174 bool line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
175 bool line2_r_ok = (reference_h > -1 && reference_h < GRHR);
176 bool line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
177 uint32_t line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
178 uint32_t line2_r = line2_r_ok ? pLineR[nOffset] : 0;
179 uint32_t line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
181 uint32_t CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
182 ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
183 for (int32_t w = 0; w < iGRW; w += 8) {
184 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
186 line1 = (line1 << 8) |
187 (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
197 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
200 line2_r = (line2_r << 8) |
201 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
206 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
212 for (int32_t k = 0; k < nBits; k++) {
213 int bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
214 cVal |= bVal << (7 - k);
215 CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
216 ((line1 >> (7 - k)) & 0x0400) |
217 ((line1_r >> (7 - k)) & 0x0040) |
218 ((line2_r >> (10 - k)) & 0x0008) |
219 ((line3_r >> (13 - k)) & 0x0001);
221 pLine[w >> 3] = cVal;
224 uint32_t CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
225 ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
226 for (int32_t w = 0; w < iGRW; w += 8) {
227 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
229 line1 = (line1 << 8) |
230 (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
235 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
238 line2_r = (line2_r << 8) |
239 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
244 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
249 for (int32_t k = 0; k < nBits; k++) {
250 int bVal = GRREFERENCE->GetPixel(w + k, h);
251 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w + k - 1, h - 1)) &&
252 (bVal == GRREFERENCE->GetPixel(w + k, h - 1)) &&
253 (bVal == GRREFERENCE->GetPixel(w + k + 1, h - 1)) &&
254 (bVal == GRREFERENCE->GetPixel(w + k - 1, h)) &&
255 (bVal == GRREFERENCE->GetPixel(w + k + 1, h)) &&
256 (bVal == GRREFERENCE->GetPixel(w + k - 1, h + 1)) &&
257 (bVal == GRREFERENCE->GetPixel(w + k, h + 1)) &&
258 (bVal == GRREFERENCE->GetPixel(w + k + 1, h + 1)))) {
262 bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
264 cVal |= bVal << (7 - k);
265 CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
266 ((line1 >> (7 - k)) & 0x0400) |
267 ((line1_r >> (7 - k)) & 0x0040) |
268 ((line2_r >> (10 - k)) & 0x0008) |
269 ((line3_r >> (13 - k)) & 0x0001);
271 pLine[w >> 3] = cVal;
290 for (uint32_t h = 0; h <
GRH; h++) {
295 LTP = LTP ^ pArithDecoder
->Decode(&grContext[0x0008]
);
298 uint32_t line1 = GRREG->GetPixel(1, h - 1);
299 line1 |= GRREG->GetPixel(0, h - 1) << 1;
300 line1 |= GRREG->GetPixel(-1, h - 1) << 2;
303 GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
305 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
306 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
307 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
310 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
311 line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
313 for (uint32_t w = 0; w <
GRW; w++) {
314 uint32_t CONTEXT = line5;
315 CONTEXT |= line4 << 2;
316 CONTEXT |= line3 << 5;
317 CONTEXT |= line2 << 6;
318 CONTEXT |= line1 << 7;
322 int bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
323 GRREG->SetPixel(w, h, bVal);
324 line1 = ((line1 << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x07;
325 line2 = ((line2 << 1) | bVal) & 0x01;
326 line3 = ((line3 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 1,
327 h - GRREFERENCEDY - 1)) &
329 line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
330 h - GRREFERENCEDY)) &
332 line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
333 h - GRREFERENCEDY + 1)) &
337 uint32_t line1 = GRREG->GetPixel(1, h - 1);
338 line1 |= GRREG->GetPixel(0, h - 1) << 1;
339 line1 |= GRREG->GetPixel(-1, h - 1) << 2;
342 GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
344 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
345 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
346 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
349 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
350 line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
352 for (uint32_t w = 0; w <
GRW; w++) {
353 int bVal = GRREFERENCE->GetPixel(w, h);
354 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w - 1, h - 1)) &&
355 (bVal == GRREFERENCE->GetPixel(w, h - 1)) &&
356 (bVal == GRREFERENCE->GetPixel(w + 1, h - 1)) &&
357 (bVal == GRREFERENCE->GetPixel(w - 1, h)) &&
358 (bVal == GRREFERENCE->GetPixel(w + 1, h)) &&
359 (bVal == GRREFERENCE->GetPixel(w - 1, h + 1)) &&
360 (bVal == GRREFERENCE->GetPixel(w, h + 1)) &&
361 (bVal == GRREFERENCE->GetPixel(w + 1, h + 1)))) {
362 uint32_t CONTEXT = line5;
363 CONTEXT |= line4 << 2;
364 CONTEXT |= line3 << 5;
365 CONTEXT |= line2 << 6;
366 CONTEXT |= line1 << 7;
370 bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
372 GRREG->SetPixel(w, h, bVal);
373 line1 = ((line1 << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x07;
374 line2 = ((line2 << 1) | bVal) & 0x01;
375 line3 = ((line3 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 1,
376 h - GRREFERENCEDY - 1)) &
378 line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
379 h - GRREFERENCEDY)) &
381 line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
382 h - GRREFERENCEDY + 1)) &
393 if (!GRREFERENCE->data())
396 int32_t iGRW =
static_cast<int32_t>(
GRW);
397 int32_t iGRH =
static_cast<int32_t>(
GRH);
403 uint8_t* pLine = GRREG->data();
404 uint8_t* pLineR = GRREFERENCE->data();
405 intptr_t nStride = GRREG->stride();
406 intptr_t nStrideR = GRREFERENCE->stride();
407 int32_t GRWR = GRREFERENCE->width();
408 int32_t GRHR = GRREFERENCE->height();
413 for (int32_t h = 0; h < iGRH; h++) {
418 LTP = LTP ^ pArithDecoder
->Decode(&grContext[0x0008]
);
420 uint32_t line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
422 bool line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
423 bool line2_r_ok = (reference_h > -1 && reference_h < GRHR);
424 bool line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
425 uint32_t line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
426 uint32_t line2_r = line2_r_ok ? pLineR[nOffset] : 0;
427 uint32_t line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
429 uint32_t CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
430 ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
431 for (int32_t w = 0; w < iGRW; w += 8) {
432 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
434 line1 = (line1 << 8) |
435 (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
439 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
441 line2_r = (line2_r << 8) |
442 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
446 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
451 for (int32_t k = 0; k < nBits; k++) {
452 int bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
453 cVal |= bVal << (7 - k);
454 CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
455 ((line1 >> (7 - k)) & 0x0080) |
456 ((line1_r >> (9 - k)) & 0x0020) |
457 ((line2_r >> (11 - k)) & 0x0004) |
458 ((line3_r >> (13 - k)) & 0x0001);
460 pLine[w >> 3] = cVal;
463 uint32_t CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
464 ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
465 for (int32_t w = 0; w < iGRW; w += 8) {
466 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
468 line1 = (line1 << 8) |
469 (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
473 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
475 line2_r = (line2_r << 8) |
476 (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
480 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
485 for (int32_t k = 0; k < nBits; k++) {
486 int bVal = GRREFERENCE->GetPixel(w + k, h);
487 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w + k - 1, h - 1)) &&
488 (bVal == GRREFERENCE->GetPixel(w + k, h - 1)) &&
489 (bVal == GRREFERENCE->GetPixel(w + k + 1, h - 1)) &&
490 (bVal == GRREFERENCE->GetPixel(w + k - 1, h)) &&
491 (bVal == GRREFERENCE->GetPixel(w + k + 1, h)) &&
492 (bVal == GRREFERENCE->GetPixel(w + k - 1, h + 1)) &&
493 (bVal == GRREFERENCE->GetPixel(w + k, h + 1)) &&
494 (bVal == GRREFERENCE->GetPixel(w + k + 1, h + 1)))) {
498 bVal = pArithDecoder
->Decode(&grContext[CONTEXT]
);
500 cVal |= bVal << (7 - k);
501 CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
502 ((line1 >> (7 - k)) & 0x0080) |
503 ((line1_r >> (9 - k)) & 0x0020) |
504 ((line2_r >> (11 - k)) & 0x0004) |
505 ((line3_r >> (13 - k)) & 0x0001);
507 pLine[w >> 3] = cVal;
int Decode(JBig2ArithCtx *pCX)
std::unique_ptr< CJBig2_Image > Decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
void SetPixel(int32_t x, int32_t y, int v)
static bool IsValidImageSize(int32_t w, int32_t h)
int GetPixel(int32_t x, int32_t y) const