• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The PDFium Authors
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 "core/fxcodec/jbig2/JBig2_GrrdProc.h"
8 
9 #include <memory>
10 
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"
15 
16 CJBig2_GRRDProc::CJBig2_GRRDProc() = default;
17 
18 CJBig2_GRRDProc::~CJBig2_GRRDProc() = default;
19 
Decode(CJBig2_ArithDecoder * pArithDecoder,pdfium::span<JBig2ArithCtx> grContexts)20 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::Decode(
21     CJBig2_ArithDecoder* pArithDecoder,
22     pdfium::span<JBig2ArithCtx> grContexts) {
23   if (!CJBig2_Image::IsValidImageSize(GRW, GRH))
24     return std::make_unique<CJBig2_Image>(GRW, GRH);
25 
26   if (!GRTEMPLATE) {
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);
31     }
32     return DecodeTemplate0Unopt(pArithDecoder, grContexts);
33   }
34 
35   if ((GRREFERENCEDX == 0) && (GRW == (uint32_t)GRREFERENCE->width()))
36     return DecodeTemplate1Opt(pArithDecoder, grContexts);
37 
38   return DecodeTemplate1Unopt(pArithDecoder, grContexts);
39 }
40 
DecodeTemplate0Unopt(CJBig2_ArithDecoder * pArithDecoder,pdfium::span<JBig2ArithCtx> grContexts)41 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate0Unopt(
42     CJBig2_ArithDecoder* pArithDecoder,
43     pdfium::span<JBig2ArithCtx> grContexts) {
44   auto GRREG = std::make_unique<CJBig2_Image>(GRW, GRH);
45   if (!GRREG->data())
46     return nullptr;
47 
48   GRREG->Fill(false);
49   int LTP = 0;
50   for (uint32_t h = 0; h < GRH; h++) {
51     if (TPGRON) {
52       if (pArithDecoder->IsComplete()) {
53         return nullptr;
54       }
55       LTP = LTP ^ pArithDecoder->Decode(&grContexts[0x0010]);
56     }
57     uint32_t lines[5];
58     lines[0] = GRREG->GetPixel(1, h - 1);
59     lines[0] |= GRREG->GetPixel(0, h - 1) << 1;
60     lines[1] = 0;
61     lines[2] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
62     lines[2] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1)
63                 << 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)
67                 << 2;
68     lines[4] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
69     lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
70                 << 1;
71     lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1)
72                 << 2;
73     if (!LTP) {
74       for (uint32_t w = 0; w < GRW; w++) {
75         uint32_t CONTEXT =
76             DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
77         if (pArithDecoder->IsComplete())
78           return nullptr;
79 
80         int bVal = pArithDecoder->Decode(&grContexts[CONTEXT]);
81         DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
82       }
83     } else {
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)))) {
94           uint32_t CONTEXT =
95               DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
96           if (pArithDecoder->IsComplete())
97             return nullptr;
98 
99           bVal = pArithDecoder->Decode(&grContexts[CONTEXT]);
100         }
101         DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
102       }
103     }
104   }
105   return GRREG;
106 }
107 
DecodeTemplate0UnoptCalculateContext(const CJBig2_Image & GRREG,const uint32_t * lines,uint32_t w,uint32_t h) const108 uint32_t CJBig2_GRRDProc::DecodeTemplate0UnoptCalculateContext(
109     const CJBig2_Image& GRREG,
110     const uint32_t* lines,
111     uint32_t w,
112     uint32_t h) const {
113   UNSAFE_TODO({
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])
119                << 8;
120     CONTEXT |= lines[1] << 9;
121     CONTEXT |= lines[0] << 10;
122     CONTEXT |= GRREG.GetPixel(w + GRAT[0], h + GRAT[1]) << 12;
123     return CONTEXT;
124   });
125 }
126 
DecodeTemplate0UnoptSetPixel(CJBig2_Image * GRREG,uint32_t * lines,uint32_t w,uint32_t h,int bVal)127 void CJBig2_GRRDProc::DecodeTemplate0UnoptSetPixel(CJBig2_Image* GRREG,
128                                                    uint32_t* lines,
129                                                    uint32_t w,
130                                                    uint32_t h,
131                                                    int bVal) {
132   GRREG->SetPixel(w, h, bVal);
133   UNSAFE_TODO({
134     lines[0] = ((lines[0] << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x03;
135     lines[1] = ((lines[1] << 1) | bVal) & 0x01;
136     lines[2] =
137         ((lines[2] << 1) |
138          GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) &
139         0x03;
140     lines[3] = ((lines[3] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
141                                                         h - GRREFERENCEDY)) &
142                0x07;
143     lines[4] =
144         ((lines[4] << 1) |
145          GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) &
146         0x07;
147   });
148 }
149 
DecodeTemplate0Opt(CJBig2_ArithDecoder * pArithDecoder,pdfium::span<JBig2ArithCtx> grContexts)150 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate0Opt(
151     CJBig2_ArithDecoder* pArithDecoder,
152     pdfium::span<JBig2ArithCtx> grContexts) {
153   if (!GRREFERENCE->data())
154     return nullptr;
155 
156   int32_t iGRW = static_cast<int32_t>(GRW);
157   int32_t iGRH = static_cast<int32_t>(GRH);
158   auto GRREG = std::make_unique<CJBig2_Image>(iGRW, iGRH);
159   if (!GRREG->data())
160     return nullptr;
161 
162   int LTP = 0;
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();
169   if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1)
170     GRREFERENCEDY = 0;
171   intptr_t nOffset = -GRREFERENCEDY * nStrideR;
172   for (int32_t h = 0; h < iGRH; h++) {
173     if (TPGRON) {
174       if (pArithDecoder->IsComplete()) {
175         return nullptr;
176       }
177       LTP = LTP ^ pArithDecoder->Decode(&grContexts[0x0010]);
178     }
179     uint32_t line1 = (h > 0) ? UNSAFE_TODO(pLine[-nStride]) << 4 : 0;
180     int32_t reference_h = h - GRREFERENCEDY;
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;
187     if (!LTP) {
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;
192         if (h > 0) {
193           line1 =
194               (line1 << 8) |
195               (w + 8 < iGRW ? UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 4
196                             : 0);
197         }
198         if (h > GRHR + GRREFERENCEDY + 1) {
199           line1_r = 0;
200           line2_r = 0;
201           line3_r = 0;
202         } else {
203           if (line1_r_ok) {
204             line1_r =
205                 (line1_r << 8) |
206                 (w + 8 < GRWR
207                      ? UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
208                      : 0);
209           }
210           if (line2_r_ok) {
211             line2_r =
212                 (line2_r << 8) |
213                 (w + 8 < GRWR ? UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1])
214                               : 0);
215           }
216           if (line3_r_ok) {
217             line3_r =
218                 (line3_r << 8) |
219                 (w + 8 < GRWR
220                      ? UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
221                      : 0);
222           } else {
223             line3_r = 0;
224           }
225         }
226         uint8_t cVal = 0;
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);
235         }
236         UNSAFE_TODO(pLine[w >> 3] = cVal);
237       }
238     } else {
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;
243         if (h > 0) {
244           line1 =
245               (line1 << 8) |
246               (w + 8 < iGRW ? UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 4
247                             : 0);
248         }
249         if (line1_r_ok) {
250           line1_r =
251               (line1_r << 8) |
252               (w + 8 < GRWR
253                    ? UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
254                    : 0);
255         }
256         if (line2_r_ok) {
257           line2_r =
258               (line2_r << 8) |
259               (w + 8 < GRWR ? UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1]) : 0);
260         }
261         if (line3_r_ok) {
262           line3_r =
263               (line3_r << 8) |
264               (w + 8 < GRWR
265                    ? UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
266                    : 0);
267         } else {
268           line3_r = 0;
269         }
270         uint8_t cVal = 0;
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)))) {
281             if (pArithDecoder->IsComplete())
282               return nullptr;
283 
284             bVal = pArithDecoder->Decode(&grContexts[CONTEXT]);
285           }
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);
292         }
293         UNSAFE_TODO(pLine[w >> 3] = cVal);
294       }
295     }
296     UNSAFE_TODO(pLine += nStride);
297     if (h < GRHR + GRREFERENCEDY) {
298       UNSAFE_TODO(pLineR += nStrideR);
299     }
300   }
301   return GRREG;
302 }
303 
DecodeTemplate1Unopt(CJBig2_ArithDecoder * pArithDecoder,pdfium::span<JBig2ArithCtx> grContexts)304 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate1Unopt(
305     CJBig2_ArithDecoder* pArithDecoder,
306     pdfium::span<JBig2ArithCtx> grContexts) {
307   auto GRREG = std::make_unique<CJBig2_Image>(GRW, GRH);
308   if (!GRREG->data())
309     return nullptr;
310 
311   GRREG->Fill(false);
312   int LTP = 0;
313   for (uint32_t h = 0; h < GRH; h++) {
314     if (TPGRON) {
315       if (pArithDecoder->IsComplete()) {
316         return nullptr;
317       }
318       LTP = LTP ^ pArithDecoder->Decode(&grContexts[0x0008]);
319     }
320     if (!LTP) {
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;
324       uint32_t line2 = 0;
325       uint32_t line3 =
326           GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
327       uint32_t line4 =
328           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
329       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
330       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
331                << 2;
332       uint32_t line5 =
333           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
334       line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
335                << 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;
342         if (pArithDecoder->IsComplete()) {
343           return nullptr;
344         }
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)) &
351                 0x01;
352         line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
353                                                       h - GRREFERENCEDY)) &
354                 0x07;
355         line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
356                                                       h - GRREFERENCEDY + 1)) &
357                 0x03;
358       }
359     } else {
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;
363       uint32_t line2 = 0;
364       uint32_t line3 =
365           GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
366       uint32_t line4 =
367           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
368       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
369       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
370                << 2;
371       uint32_t line5 =
372           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
373       line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
374                << 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;
390           if (pArithDecoder->IsComplete()) {
391             return nullptr;
392           }
393           bVal = pArithDecoder->Decode(&grContexts[CONTEXT]);
394         }
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)) &
400                 0x01;
401         line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
402                                                       h - GRREFERENCEDY)) &
403                 0x07;
404         line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
405                                                       h - GRREFERENCEDY + 1)) &
406                 0x03;
407       }
408     }
409   }
410   return GRREG;
411 }
412 
DecodeTemplate1Opt(CJBig2_ArithDecoder * pArithDecoder,pdfium::span<JBig2ArithCtx> grContexts)413 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate1Opt(
414     CJBig2_ArithDecoder* pArithDecoder,
415     pdfium::span<JBig2ArithCtx> grContexts) {
416   if (!GRREFERENCE->data())
417     return nullptr;
418 
419   int32_t iGRW = static_cast<int32_t>(GRW);
420   int32_t iGRH = static_cast<int32_t>(GRH);
421   auto GRREG = std::make_unique<CJBig2_Image>(iGRW, iGRH);
422   if (!GRREG->data())
423     return nullptr;
424 
425   int LTP = 0;
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();
432   if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
433     GRREFERENCEDY = 0;
434   }
435   intptr_t nOffset = -GRREFERENCEDY * nStrideR;
436   for (int32_t h = 0; h < iGRH; h++) {
437     if (TPGRON) {
438       if (pArithDecoder->IsComplete())
439         return nullptr;
440 
441       LTP = LTP ^ pArithDecoder->Decode(&grContexts[0x0008]);
442     }
443     uint32_t line1 = (h > 0) ? UNSAFE_TODO(pLine[-nStride]) << 1 : 0;
444     int32_t reference_h = h - GRREFERENCEDY;
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;
451     if (!LTP) {
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;
456         if (h > 0)
457           line1 =
458               (line1 << 8) |
459               (w + 8 < iGRW ? UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 1
460                             : 0);
461         if (line1_r_ok)
462           line1_r =
463               (line1_r << 8) |
464               (w + 8 < GRWR
465                    ? UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
466                    : 0);
467         if (line2_r_ok)
468           line2_r =
469               (line2_r << 8) |
470               (w + 8 < GRWR ? UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1]) : 0);
471         if (line3_r_ok) {
472           line3_r =
473               (line3_r << 8) |
474               (w + 8 < GRWR
475                    ? UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
476                    : 0);
477         } else {
478           line3_r = 0;
479         }
480         uint8_t cVal = 0;
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);
489         }
490         UNSAFE_TODO(pLine[w >> 3] = cVal);
491       }
492     } else {
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;
497         if (h > 0)
498           line1 =
499               (line1 << 8) |
500               (w + 8 < iGRW ? UNSAFE_TODO(pLine[-nStride + (w >> 3) + 1]) << 1
501                             : 0);
502         if (line1_r_ok)
503           line1_r =
504               (line1_r << 8) |
505               (w + 8 < GRWR
506                    ? UNSAFE_TODO(pLineR[nOffset - nStrideR + (w >> 3) + 1])
507                    : 0);
508         if (line2_r_ok)
509           line2_r =
510               (line2_r << 8) |
511               (w + 8 < GRWR ? UNSAFE_TODO(pLineR[nOffset + (w >> 3) + 1]) : 0);
512         if (line3_r_ok) {
513           line3_r =
514               (line3_r << 8) |
515               (w + 8 < GRWR
516                    ? UNSAFE_TODO(pLineR[nOffset + nStrideR + (w >> 3) + 1])
517                    : 0);
518         } else {
519           line3_r = 0;
520         }
521         uint8_t cVal = 0;
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)))) {
532             if (pArithDecoder->IsComplete())
533               return nullptr;
534 
535             bVal = pArithDecoder->Decode(&grContexts[CONTEXT]);
536           }
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);
543         }
544         UNSAFE_TODO(pLine[w >> 3] = cVal);
545       }
546     }
547     UNSAFE_TODO(pLine += nStride);
548     if (h < GRHR + GRREFERENCEDY) {
549       UNSAFE_TODO(pLineR += nStrideR);
550     }
551   }
552   return GRREG;
553 }
554