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"
14#include "core/fxcrt/compiler_specific.h"
22 pdfium::span<JBig2ArithCtx> grContexts) {
27 if ((GRAT[0] == -1) && (GRAT[1] == -1) && (GRAT[2] == -1) &&
28 (GRAT[3] == -1) && (GRREFERENCEDX == 0) &&
29 (GRW == (uint32_t)GRREFERENCE->width())) {
30 return DecodeTemplate0Opt(pArithDecoder, grContexts);
32 return DecodeTemplate0Unopt(pArithDecoder, grContexts);
35 if ((GRREFERENCEDX == 0) && (GRW == (uint32_t)GRREFERENCE->width()))
36 return DecodeTemplate1Opt(pArithDecoder, grContexts);
38 return DecodeTemplate1Unopt(pArithDecoder, grContexts);
43 pdfium::span<JBig2ArithCtx> grContexts) {
50 for (uint32_t h = 0; h <
GRH; h++) {
55 LTP = LTP ^ pArithDecoder
->Decode(&grContexts[0x0010]
);
58 lines[0] = GRREG->GetPixel(1, h - 1);
59 lines[0] |= GRREG->GetPixel(0, h - 1) << 1;
61 lines[2] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
62 lines[2] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1)
64 lines[3] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
65 lines[3] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
66 lines[3] |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
68 lines[4] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
69 lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
71 lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1)
74 for (uint32_t w = 0; w <
GRW; w++) {
76 DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
80 int bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
81 DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
84 for (uint32_t w = 0; w <
GRW; w++) {
85 int bVal = GRREFERENCE->GetPixel(w, h);
86 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w - 1, h - 1)) &&
87 (bVal == GRREFERENCE->GetPixel(w, h - 1)) &&
88 (bVal == GRREFERENCE->GetPixel(w + 1, h - 1)) &&
89 (bVal == GRREFERENCE->GetPixel(w - 1, h)) &&
90 (bVal == GRREFERENCE->GetPixel(w + 1, h)) &&
91 (bVal == GRREFERENCE->GetPixel(w - 1, h + 1)) &&
92 (bVal == GRREFERENCE->GetPixel(w, h + 1)) &&
93 (bVal == GRREFERENCE->GetPixel(w + 1, h + 1)))) {
95 DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
99 bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
101 DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
110 const uint32_t* lines,
114 uint32_t CONTEXT = lines[4];
115 CONTEXT |= lines[3] << 3;
116 CONTEXT |= lines[2] << 6;
117 CONTEXT |= GRREFERENCE->GetPixel(w - GRREFERENCEDX + GRAT[2],
118 h - GRREFERENCEDY + GRAT[3])
120 CONTEXT |= lines[1] << 9;
121 CONTEXT |= lines[0] << 10;
122 CONTEXT |= GRREG.GetPixel(w + GRAT[0], h + GRAT[1]) << 12;
134 lines[0] = ((lines[0] << 1) | GRREG
->GetPixel(w + 2
, h - 1
)) & 0x03;
135 lines[1] = ((lines[1] << 1) | bVal) & 0x01;
138 GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) &
140 lines[3] = ((lines[3] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
141 h - GRREFERENCEDY)) &
145 GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) &
152 pdfium::span<JBig2ArithCtx> grContexts) {
153 if (!GRREFERENCE->data())
156 int32_t iGRW =
static_cast<int32_t>(
GRW);
157 int32_t iGRH =
static_cast<int32_t>(
GRH);
163 uint8_t* pLine = GRREG->data();
164 uint8_t* pLineR = GRREFERENCE->data();
165 intptr_t nStride = GRREG->stride();
166 intptr_t nStrideR = GRREFERENCE->stride();
167 int32_t GRWR = GRREFERENCE->width();
168 int32_t GRHR = GRREFERENCE->height();
172 for (int32_t h = 0; h < iGRH; h++) {
177 LTP = LTP ^ pArithDecoder
->Decode(&grContexts[0x0010]
);
179 uint32_t line1 = (h > 0) ?
UNSAFE_TODO(pLine[-nStride]) << 4 : 0;
181 bool line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
182 bool line2_r_ok = (reference_h > -1 && reference_h < GRHR);
183 bool line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
184 uint32_t line1_r = line1_r_ok ?
UNSAFE_TODO(pLineR[nOffset - nStrideR]) : 0;
185 uint32_t line2_r = line2_r_ok ?
UNSAFE_TODO(pLineR[nOffset]) : 0;
186 uint32_t line3_r = line3_r_ok ?
UNSAFE_TODO(pLineR[nOffset + nStrideR]) : 0;
188 uint32_t CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
189 ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
190 for (int32_t w = 0; w < iGRW; w += 8) {
191 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
195 (w + 8 < iGRW ?
UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 4
207 ?
UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
213 (w + 8 < GRWR ?
UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1])
220 ?
UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
227 for (int32_t k = 0; k < nBits; k++) {
228 int bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
229 cVal |= bVal << (7 - k);
230 CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
231 ((line1 >> (7 - k)) & 0x0400) |
232 ((line1_r >> (7 - k)) & 0x0040) |
233 ((line2_r >> (10 - k)) & 0x0008) |
234 ((line3_r >> (13 - k)) & 0x0001);
239 uint32_t CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
240 ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
241 for (int32_t w = 0; w < iGRW; w += 8) {
242 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
246 (w + 8 < iGRW ?
UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 4
253 ?
UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
259 (w + 8 < GRWR ?
UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1]) : 0);
265 ?
UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
271 for (int32_t k = 0; k < nBits; k++) {
272 int bVal = GRREFERENCE->GetPixel(w + k, h);
273 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w + k - 1, h - 1)) &&
274 (bVal == GRREFERENCE->GetPixel(w + k, h - 1)) &&
275 (bVal == GRREFERENCE->GetPixel(w + k + 1, h - 1)) &&
276 (bVal == GRREFERENCE->GetPixel(w + k - 1, h)) &&
277 (bVal == GRREFERENCE->GetPixel(w + k + 1, h)) &&
278 (bVal == GRREFERENCE->GetPixel(w + k - 1, h + 1)) &&
279 (bVal == GRREFERENCE->GetPixel(w + k, h + 1)) &&
280 (bVal == GRREFERENCE->GetPixel(w + k + 1, h + 1)))) {
284 bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
286 cVal |= bVal << (7 - k);
287 CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
288 ((line1 >> (7 - k)) & 0x0400) |
289 ((line1_r >> (7 - k)) & 0x0040) |
290 ((line2_r >> (10 - k)) & 0x0008) |
291 ((line3_r >> (13 - k)) & 0x0001);
306 pdfium::span<JBig2ArithCtx> grContexts) {
313 for (uint32_t h = 0; h <
GRH; h++) {
318 LTP = LTP ^ pArithDecoder
->Decode(&grContexts[0x0008]
);
321 uint32_t line1 = GRREG->GetPixel(1, h - 1);
322 line1 |= GRREG->GetPixel(0, h - 1) << 1;
323 line1 |= GRREG->GetPixel(-1, h - 1) << 2;
326 GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
328 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
329 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
330 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
333 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
334 line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
336 for (uint32_t w = 0; w <
GRW; w++) {
337 uint32_t CONTEXT = line5;
338 CONTEXT |= line4 << 2;
339 CONTEXT |= line3 << 5;
340 CONTEXT |= line2 << 6;
341 CONTEXT |= line1 << 7;
345 int bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
346 GRREG->SetPixel(w, h, bVal);
347 line1 = ((line1 << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x07;
348 line2 = ((line2 << 1) | bVal) & 0x01;
349 line3 = ((line3 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 1,
350 h - GRREFERENCEDY - 1)) &
352 line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
353 h - GRREFERENCEDY)) &
355 line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
356 h - GRREFERENCEDY + 1)) &
360 uint32_t line1 = GRREG->GetPixel(1, h - 1);
361 line1 |= GRREG->GetPixel(0, h - 1) << 1;
362 line1 |= GRREG->GetPixel(-1, h - 1) << 2;
365 GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
367 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
368 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
369 line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
372 GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
373 line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
375 for (uint32_t w = 0; w <
GRW; w++) {
376 int bVal = GRREFERENCE->GetPixel(w, h);
377 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w - 1, h - 1)) &&
378 (bVal == GRREFERENCE->GetPixel(w, h - 1)) &&
379 (bVal == GRREFERENCE->GetPixel(w + 1, h - 1)) &&
380 (bVal == GRREFERENCE->GetPixel(w - 1, h)) &&
381 (bVal == GRREFERENCE->GetPixel(w + 1, h)) &&
382 (bVal == GRREFERENCE->GetPixel(w - 1, h + 1)) &&
383 (bVal == GRREFERENCE->GetPixel(w, h + 1)) &&
384 (bVal == GRREFERENCE->GetPixel(w + 1, h + 1)))) {
385 uint32_t CONTEXT = line5;
386 CONTEXT |= line4 << 2;
387 CONTEXT |= line3 << 5;
388 CONTEXT |= line2 << 6;
389 CONTEXT |= line1 << 7;
393 bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
395 GRREG->SetPixel(w, h, bVal);
396 line1 = ((line1 << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x07;
397 line2 = ((line2 << 1) | bVal) & 0x01;
398 line3 = ((line3 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 1,
399 h - GRREFERENCEDY - 1)) &
401 line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
402 h - GRREFERENCEDY)) &
404 line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
405 h - GRREFERENCEDY + 1)) &
415 pdfium::span<JBig2ArithCtx> grContexts) {
416 if (!GRREFERENCE->data())
419 int32_t iGRW =
static_cast<int32_t>(
GRW);
420 int32_t iGRH =
static_cast<int32_t>(
GRH);
426 uint8_t* pLine = GRREG->data();
427 uint8_t* pLineR = GRREFERENCE->data();
428 intptr_t nStride = GRREG->stride();
429 intptr_t nStrideR = GRREFERENCE->stride();
430 int32_t GRWR = GRREFERENCE->width();
431 int32_t GRHR = GRREFERENCE->height();
436 for (int32_t h = 0; h < iGRH; h++) {
441 LTP = LTP ^ pArithDecoder
->Decode(&grContexts[0x0008]
);
443 uint32_t line1 = (h > 0) ?
UNSAFE_TODO(pLine[-nStride]) << 1 : 0;
445 bool line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
446 bool line2_r_ok = (reference_h > -1 && reference_h < GRHR);
447 bool line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
448 uint32_t line1_r = line1_r_ok ?
UNSAFE_TODO(pLineR[nOffset - nStrideR]) : 0;
449 uint32_t line2_r = line2_r_ok ?
UNSAFE_TODO(pLineR[nOffset]) : 0;
450 uint32_t line3_r = line3_r_ok ?
UNSAFE_TODO(pLineR[nOffset + nStrideR]) : 0;
452 uint32_t CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
453 ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
454 for (int32_t w = 0; w < iGRW; w += 8) {
455 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
459 (w + 8 < iGRW ?
UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 1
465 ?
UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
470 (w + 8 < GRWR ?
UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1]) : 0);
475 ?
UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
481 for (int32_t k = 0; k < nBits; k++) {
482 int bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
483 cVal |= bVal << (7 - k);
484 CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
485 ((line1 >> (7 - k)) & 0x0080) |
486 ((line1_r >> (9 - k)) & 0x0020) |
487 ((line2_r >> (11 - k)) & 0x0004) |
488 ((line3_r >> (13 - k)) & 0x0001);
493 uint32_t CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
494 ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
495 for (int32_t w = 0; w < iGRW; w += 8) {
496 int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
500 (w + 8 < iGRW ?
UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 1
506 ?
UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
511 (w + 8 < GRWR ?
UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1]) : 0);
516 ?
UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
522 for (int32_t k = 0; k < nBits; k++) {
523 int bVal = GRREFERENCE->GetPixel(w + k, h);
524 if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w + k - 1, h - 1)) &&
525 (bVal == GRREFERENCE->GetPixel(w + k, h - 1)) &&
526 (bVal == GRREFERENCE->GetPixel(w + k + 1, h - 1)) &&
527 (bVal == GRREFERENCE->GetPixel(w + k - 1, h)) &&
528 (bVal == GRREFERENCE->GetPixel(w + k + 1, h)) &&
529 (bVal == GRREFERENCE->GetPixel(w + k - 1, h + 1)) &&
530 (bVal == GRREFERENCE->GetPixel(w + k, h + 1)) &&
531 (bVal == GRREFERENCE->GetPixel(w + k + 1, h + 1)))) {
535 bVal = pArithDecoder
->Decode(&grContexts[CONTEXT]
);
537 cVal |= bVal << (7 - k);
538 CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
539 ((line1 >> (7 - k)) & 0x0080) |
540 ((line1_r >> (9 - k)) & 0x0020) |
541 ((line2_r >> (11 - k)) & 0x0004) |
542 ((line3_r >> (13 - k)) & 0x0001);
int Decode(JBig2ArithCtx *pCX)
std::unique_ptr< CJBig2_Image > Decode(CJBig2_ArithDecoder *pArithDecoder, pdfium::span< JBig2ArithCtx > grContexts)
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