1 // Copyright 2015 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 "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 "third_party/base/ptr_util.h"
15 
16 CJBig2_GRRDProc::CJBig2_GRRDProc() = default;
17 
18 CJBig2_GRRDProc::~CJBig2_GRRDProc() = default;
19 
Decode(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)20 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::Decode(
21     CJBig2_ArithDecoder* pArithDecoder,
22     JBig2ArithCtx* grContext) {
23   if (!CJBig2_Image::IsValidImageSize(GRW, GRH))
24     return pdfium::MakeUnique<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, grContext);
31     }
32     return DecodeTemplate0Unopt(pArithDecoder, grContext);
33   }
34 
35   if ((GRREFERENCEDX == 0) && (GRW == (uint32_t)GRREFERENCE->width()))
36     return DecodeTemplate1Opt(pArithDecoder, grContext);
37 
38   return DecodeTemplate1Unopt(pArithDecoder, grContext);
39 }
40 
DecodeTemplate0Unopt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)41 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate0Unopt(
42     CJBig2_ArithDecoder* pArithDecoder,
43     JBig2ArithCtx* grContext) {
44   auto GRREG = pdfium::MakeUnique<CJBig2_Image>(GRW, GRH);
45   if (!GRREG->data())
46     return nullptr;
47 
48   GRREG->Fill(0);
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(&grContext[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(&grContext[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(&grContext[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   uint32_t CONTEXT = lines[4];
114   CONTEXT |= lines[3] << 3;
115   CONTEXT |= lines[2] << 6;
116   CONTEXT |= GRREFERENCE->GetPixel(w - GRREFERENCEDX + GRAT[2],
117                                    h - GRREFERENCEDY + GRAT[3])
118              << 8;
119   CONTEXT |= lines[1] << 9;
120   CONTEXT |= lines[0] << 10;
121   CONTEXT |= GRREG.GetPixel(w + GRAT[0], h + GRAT[1]) << 12;
122   return CONTEXT;
123 }
124 
DecodeTemplate0UnoptSetPixel(CJBig2_Image * GRREG,uint32_t * lines,uint32_t w,uint32_t h,int bVal)125 void CJBig2_GRRDProc::DecodeTemplate0UnoptSetPixel(CJBig2_Image* GRREG,
126                                                    uint32_t* lines,
127                                                    uint32_t w,
128                                                    uint32_t h,
129                                                    int bVal) {
130   GRREG->SetPixel(w, h, bVal);
131   lines[0] = ((lines[0] << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x03;
132   lines[1] = ((lines[1] << 1) | bVal) & 0x01;
133   lines[2] = ((lines[2] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
134                                                       h - GRREFERENCEDY - 1)) &
135              0x03;
136   lines[3] = ((lines[3] << 1) |
137               GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
138              0x07;
139   lines[4] = ((lines[4] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
140                                                       h - GRREFERENCEDY + 1)) &
141              0x07;
142 }
143 
DecodeTemplate0Opt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)144 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate0Opt(
145     CJBig2_ArithDecoder* pArithDecoder,
146     JBig2ArithCtx* grContext) {
147   if (!GRREFERENCE->data())
148     return nullptr;
149 
150   int32_t iGRW = static_cast<int32_t>(GRW);
151   int32_t iGRH = static_cast<int32_t>(GRH);
152   auto GRREG = pdfium::MakeUnique<CJBig2_Image>(iGRW, iGRH);
153   if (!GRREG->data())
154     return nullptr;
155 
156   int LTP = 0;
157   uint8_t* pLine = GRREG->data();
158   uint8_t* pLineR = GRREFERENCE->data();
159   intptr_t nStride = GRREG->stride();
160   intptr_t nStrideR = GRREFERENCE->stride();
161   int32_t GRWR = GRREFERENCE->width();
162   int32_t GRHR = GRREFERENCE->height();
163   if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1)
164     GRREFERENCEDY = 0;
165   intptr_t nOffset = -GRREFERENCEDY * nStrideR;
166   for (int32_t h = 0; h < iGRH; h++) {
167     if (TPGRON) {
168       if (pArithDecoder->IsComplete())
169         return nullptr;
170 
171       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0010]);
172     }
173     uint32_t line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
174     int32_t reference_h = h - GRREFERENCEDY;
175     bool line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
176     bool line2_r_ok = (reference_h > -1 && reference_h < GRHR);
177     bool line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
178     uint32_t line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
179     uint32_t line2_r = line2_r_ok ? pLineR[nOffset] : 0;
180     uint32_t line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
181     if (!LTP) {
182       uint32_t CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
183                          ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
184       for (int32_t w = 0; w < iGRW; w += 8) {
185         int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
186         if (h > 0) {
187           line1 = (line1 << 8) |
188                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
189         }
190         if (h > GRHR + GRREFERENCEDY + 1) {
191           line1_r = 0;
192           line2_r = 0;
193           line3_r = 0;
194         } else {
195           if (line1_r_ok) {
196             line1_r =
197                 (line1_r << 8) |
198                 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
199           }
200           if (line2_r_ok) {
201             line2_r = (line2_r << 8) |
202                       (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
203           }
204           if (line3_r_ok) {
205             line3_r =
206                 (line3_r << 8) |
207                 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
208           } else {
209             line3_r = 0;
210           }
211         }
212         uint8_t cVal = 0;
213         for (int32_t k = 0; k < nBits; k++) {
214           int bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
215           cVal |= bVal << (7 - k);
216           CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
217                     ((line1 >> (7 - k)) & 0x0400) |
218                     ((line1_r >> (7 - k)) & 0x0040) |
219                     ((line2_r >> (10 - k)) & 0x0008) |
220                     ((line3_r >> (13 - k)) & 0x0001);
221         }
222         pLine[w >> 3] = cVal;
223       }
224     } else {
225       uint32_t CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0) |
226                          ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
227       for (int32_t w = 0; w < iGRW; w += 8) {
228         int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
229         if (h > 0) {
230           line1 = (line1 << 8) |
231                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
232         }
233         if (line1_r_ok) {
234           line1_r =
235               (line1_r << 8) |
236               (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
237         }
238         if (line2_r_ok) {
239           line2_r = (line2_r << 8) |
240                     (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
241         }
242         if (line3_r_ok) {
243           line3_r =
244               (line3_r << 8) |
245               (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
246         } else {
247           line3_r = 0;
248         }
249         uint8_t cVal = 0;
250         for (int32_t k = 0; k < nBits; k++) {
251           int bVal = GRREFERENCE->GetPixel(w + k, h);
252           if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w + k - 1, h - 1)) &&
253                 (bVal == GRREFERENCE->GetPixel(w + k, h - 1)) &&
254                 (bVal == GRREFERENCE->GetPixel(w + k + 1, h - 1)) &&
255                 (bVal == GRREFERENCE->GetPixel(w + k - 1, h)) &&
256                 (bVal == GRREFERENCE->GetPixel(w + k + 1, h)) &&
257                 (bVal == GRREFERENCE->GetPixel(w + k - 1, h + 1)) &&
258                 (bVal == GRREFERENCE->GetPixel(w + k, h + 1)) &&
259                 (bVal == GRREFERENCE->GetPixel(w + k + 1, h + 1)))) {
260             if (pArithDecoder->IsComplete())
261               return nullptr;
262 
263             bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
264           }
265           cVal |= bVal << (7 - k);
266           CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
267                     ((line1 >> (7 - k)) & 0x0400) |
268                     ((line1_r >> (7 - k)) & 0x0040) |
269                     ((line2_r >> (10 - k)) & 0x0008) |
270                     ((line3_r >> (13 - k)) & 0x0001);
271         }
272         pLine[w >> 3] = cVal;
273       }
274     }
275     pLine += nStride;
276     if (h < GRHR + GRREFERENCEDY)
277       pLineR += nStrideR;
278   }
279   return GRREG;
280 }
281 
DecodeTemplate1Unopt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)282 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate1Unopt(
283     CJBig2_ArithDecoder* pArithDecoder,
284     JBig2ArithCtx* grContext) {
285   auto GRREG = pdfium::MakeUnique<CJBig2_Image>(GRW, GRH);
286   if (!GRREG->data())
287     return nullptr;
288 
289   GRREG->Fill(0);
290   int LTP = 0;
291   for (uint32_t h = 0; h < GRH; h++) {
292     if (TPGRON) {
293       if (pArithDecoder->IsComplete())
294         return nullptr;
295 
296       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0008]);
297     }
298     if (!LTP) {
299       uint32_t line1 = GRREG->GetPixel(1, h - 1);
300       line1 |= GRREG->GetPixel(0, h - 1) << 1;
301       line1 |= GRREG->GetPixel(-1, h - 1) << 2;
302       uint32_t line2 = 0;
303       uint32_t line3 =
304           GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
305       uint32_t line4 =
306           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
307       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
308       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
309                << 2;
310       uint32_t line5 =
311           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
312       line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
313                << 1;
314       for (uint32_t w = 0; w < GRW; w++) {
315         uint32_t CONTEXT = line5;
316         CONTEXT |= line4 << 2;
317         CONTEXT |= line3 << 5;
318         CONTEXT |= line2 << 6;
319         CONTEXT |= line1 << 7;
320         if (pArithDecoder->IsComplete())
321           return nullptr;
322 
323         int bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
324         GRREG->SetPixel(w, h, bVal);
325         line1 = ((line1 << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x07;
326         line2 = ((line2 << 1) | bVal) & 0x01;
327         line3 = ((line3 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 1,
328                                                       h - GRREFERENCEDY - 1)) &
329                 0x01;
330         line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
331                                                       h - GRREFERENCEDY)) &
332                 0x07;
333         line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
334                                                       h - GRREFERENCEDY + 1)) &
335                 0x03;
336       }
337     } else {
338       uint32_t line1 = GRREG->GetPixel(1, h - 1);
339       line1 |= GRREG->GetPixel(0, h - 1) << 1;
340       line1 |= GRREG->GetPixel(-1, h - 1) << 2;
341       uint32_t line2 = 0;
342       uint32_t line3 =
343           GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
344       uint32_t line4 =
345           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
346       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
347       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
348                << 2;
349       uint32_t line5 =
350           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
351       line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
352                << 1;
353       for (uint32_t w = 0; w < GRW; w++) {
354         int bVal = GRREFERENCE->GetPixel(w, h);
355         if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w - 1, h - 1)) &&
356               (bVal == GRREFERENCE->GetPixel(w, h - 1)) &&
357               (bVal == GRREFERENCE->GetPixel(w + 1, h - 1)) &&
358               (bVal == GRREFERENCE->GetPixel(w - 1, h)) &&
359               (bVal == GRREFERENCE->GetPixel(w + 1, h)) &&
360               (bVal == GRREFERENCE->GetPixel(w - 1, h + 1)) &&
361               (bVal == GRREFERENCE->GetPixel(w, h + 1)) &&
362               (bVal == GRREFERENCE->GetPixel(w + 1, h + 1)))) {
363           uint32_t CONTEXT = line5;
364           CONTEXT |= line4 << 2;
365           CONTEXT |= line3 << 5;
366           CONTEXT |= line2 << 6;
367           CONTEXT |= line1 << 7;
368           if (pArithDecoder->IsComplete())
369             return nullptr;
370 
371           bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
372         }
373         GRREG->SetPixel(w, h, bVal);
374         line1 = ((line1 << 1) | GRREG->GetPixel(w + 2, h - 1)) & 0x07;
375         line2 = ((line2 << 1) | bVal) & 0x01;
376         line3 = ((line3 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 1,
377                                                       h - GRREFERENCEDY - 1)) &
378                 0x01;
379         line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
380                                                       h - GRREFERENCEDY)) &
381                 0x07;
382         line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
383                                                       h - GRREFERENCEDY + 1)) &
384                 0x03;
385       }
386     }
387   }
388   return GRREG;
389 }
390 
DecodeTemplate1Opt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)391 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate1Opt(
392     CJBig2_ArithDecoder* pArithDecoder,
393     JBig2ArithCtx* grContext) {
394   if (!GRREFERENCE->data())
395     return nullptr;
396 
397   int32_t iGRW = static_cast<int32_t>(GRW);
398   int32_t iGRH = static_cast<int32_t>(GRH);
399   auto GRREG = pdfium::MakeUnique<CJBig2_Image>(iGRW, iGRH);
400   if (!GRREG->data())
401     return nullptr;
402 
403   int LTP = 0;
404   uint8_t* pLine = GRREG->data();
405   uint8_t* pLineR = GRREFERENCE->data();
406   intptr_t nStride = GRREG->stride();
407   intptr_t nStrideR = GRREFERENCE->stride();
408   int32_t GRWR = GRREFERENCE->width();
409   int32_t GRHR = GRREFERENCE->height();
410   if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
411     GRREFERENCEDY = 0;
412   }
413   intptr_t nOffset = -GRREFERENCEDY * nStrideR;
414   for (int32_t h = 0; h < iGRH; h++) {
415     if (TPGRON) {
416       if (pArithDecoder->IsComplete())
417         return nullptr;
418 
419       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0008]);
420     }
421     uint32_t line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
422     int32_t reference_h = h - GRREFERENCEDY;
423     bool line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
424     bool line2_r_ok = (reference_h > -1 && reference_h < GRHR);
425     bool line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
426     uint32_t line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
427     uint32_t line2_r = line2_r_ok ? pLineR[nOffset] : 0;
428     uint32_t line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
429     if (!LTP) {
430       uint32_t CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
431                          ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
432       for (int32_t w = 0; w < iGRW; w += 8) {
433         int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
434         if (h > 0)
435           line1 = (line1 << 8) |
436                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
437         if (line1_r_ok)
438           line1_r =
439               (line1_r << 8) |
440               (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
441         if (line2_r_ok)
442           line2_r = (line2_r << 8) |
443                     (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
444         if (line3_r_ok) {
445           line3_r =
446               (line3_r << 8) |
447               (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
448         } else {
449           line3_r = 0;
450         }
451         uint8_t cVal = 0;
452         for (int32_t k = 0; k < nBits; k++) {
453           int bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
454           cVal |= bVal << (7 - k);
455           CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
456                     ((line1 >> (7 - k)) & 0x0080) |
457                     ((line1_r >> (9 - k)) & 0x0020) |
458                     ((line2_r >> (11 - k)) & 0x0004) |
459                     ((line3_r >> (13 - k)) & 0x0001);
460         }
461         pLine[w >> 3] = cVal;
462       }
463     } else {
464       uint32_t CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020) |
465                          ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
466       for (int32_t w = 0; w < iGRW; w += 8) {
467         int32_t nBits = iGRW - w > 8 ? 8 : iGRW - w;
468         if (h > 0)
469           line1 = (line1 << 8) |
470                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
471         if (line1_r_ok)
472           line1_r =
473               (line1_r << 8) |
474               (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
475         if (line2_r_ok)
476           line2_r = (line2_r << 8) |
477                     (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
478         if (line3_r_ok) {
479           line3_r =
480               (line3_r << 8) |
481               (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
482         } else {
483           line3_r = 0;
484         }
485         uint8_t cVal = 0;
486         for (int32_t k = 0; k < nBits; k++) {
487           int bVal = GRREFERENCE->GetPixel(w + k, h);
488           if (!(TPGRON && (bVal == GRREFERENCE->GetPixel(w + k - 1, h - 1)) &&
489                 (bVal == GRREFERENCE->GetPixel(w + k, h - 1)) &&
490                 (bVal == GRREFERENCE->GetPixel(w + k + 1, h - 1)) &&
491                 (bVal == GRREFERENCE->GetPixel(w + k - 1, h)) &&
492                 (bVal == GRREFERENCE->GetPixel(w + k + 1, h)) &&
493                 (bVal == GRREFERENCE->GetPixel(w + k - 1, h + 1)) &&
494                 (bVal == GRREFERENCE->GetPixel(w + k, h + 1)) &&
495                 (bVal == GRREFERENCE->GetPixel(w + k + 1, h + 1)))) {
496             if (pArithDecoder->IsComplete())
497               return nullptr;
498 
499             bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
500           }
501           cVal |= bVal << (7 - k);
502           CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
503                     ((line1 >> (7 - k)) & 0x0080) |
504                     ((line1_r >> (9 - k)) & 0x0020) |
505                     ((line2_r >> (11 - k)) & 0x0004) |
506                     ((line3_r >> (13 - k)) & 0x0001);
507         }
508         pLine[w >> 3] = cVal;
509       }
510     }
511     pLine += nStride;
512     if (h < GRHR + GRREFERENCEDY)
513       pLineR += nStrideR;
514   }
515   return GRREG;
516 }
517