• 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 
15 CJBig2_GRRDProc::CJBig2_GRRDProc() = default;
16 
17 CJBig2_GRRDProc::~CJBig2_GRRDProc() = default;
18 
Decode(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)19 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::Decode(
20     CJBig2_ArithDecoder* pArithDecoder,
21     JBig2ArithCtx* grContext) {
22   if (!CJBig2_Image::IsValidImageSize(GRW, GRH))
23     return std::make_unique<CJBig2_Image>(GRW, GRH);
24 
25   if (!GRTEMPLATE) {
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);
30     }
31     return DecodeTemplate0Unopt(pArithDecoder, grContext);
32   }
33 
34   if ((GRREFERENCEDX == 0) && (GRW == (uint32_t)GRREFERENCE->width()))
35     return DecodeTemplate1Opt(pArithDecoder, grContext);
36 
37   return DecodeTemplate1Unopt(pArithDecoder, grContext);
38 }
39 
DecodeTemplate0Unopt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)40 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate0Unopt(
41     CJBig2_ArithDecoder* pArithDecoder,
42     JBig2ArithCtx* grContext) {
43   auto GRREG = std::make_unique<CJBig2_Image>(GRW, GRH);
44   if (!GRREG->data())
45     return nullptr;
46 
47   GRREG->Fill(false);
48   int LTP = 0;
49   for (uint32_t h = 0; h < GRH; h++) {
50     if (TPGRON) {
51       if (pArithDecoder->IsComplete())
52         return nullptr;
53 
54       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0010]);
55     }
56     uint32_t lines[5];
57     lines[0] = GRREG->GetPixel(1, h - 1);
58     lines[0] |= GRREG->GetPixel(0, h - 1) << 1;
59     lines[1] = 0;
60     lines[2] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
61     lines[2] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1)
62                 << 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)
66                 << 2;
67     lines[4] = GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
68     lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
69                 << 1;
70     lines[4] |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1)
71                 << 2;
72     if (!LTP) {
73       for (uint32_t w = 0; w < GRW; w++) {
74         uint32_t CONTEXT =
75             DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
76         if (pArithDecoder->IsComplete())
77           return nullptr;
78 
79         int bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
80         DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
81       }
82     } else {
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)))) {
93           uint32_t CONTEXT =
94               DecodeTemplate0UnoptCalculateContext(*GRREG, lines, w, h);
95           if (pArithDecoder->IsComplete())
96             return nullptr;
97 
98           bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
99         }
100         DecodeTemplate0UnoptSetPixel(GRREG.get(), lines, w, h, bVal);
101       }
102     }
103   }
104   return GRREG;
105 }
106 
DecodeTemplate0UnoptCalculateContext(const CJBig2_Image & GRREG,const uint32_t * lines,uint32_t w,uint32_t h) const107 uint32_t CJBig2_GRRDProc::DecodeTemplate0UnoptCalculateContext(
108     const CJBig2_Image& GRREG,
109     const uint32_t* lines,
110     uint32_t w,
111     uint32_t h) const {
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])
117              << 8;
118   CONTEXT |= lines[1] << 9;
119   CONTEXT |= lines[0] << 10;
120   CONTEXT |= GRREG.GetPixel(w + GRAT[0], h + GRAT[1]) << 12;
121   return CONTEXT;
122 }
123 
DecodeTemplate0UnoptSetPixel(CJBig2_Image * GRREG,uint32_t * lines,uint32_t w,uint32_t h,int bVal)124 void CJBig2_GRRDProc::DecodeTemplate0UnoptSetPixel(CJBig2_Image* GRREG,
125                                                    uint32_t* lines,
126                                                    uint32_t w,
127                                                    uint32_t h,
128                                                    int bVal) {
129   GRREG->SetPixel(w, h, bVal);
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)) &
134              0x03;
135   lines[3] = ((lines[3] << 1) |
136               GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) &
137              0x07;
138   lines[4] = ((lines[4] << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
139                                                       h - GRREFERENCEDY + 1)) &
140              0x07;
141 }
142 
DecodeTemplate0Opt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)143 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate0Opt(
144     CJBig2_ArithDecoder* pArithDecoder,
145     JBig2ArithCtx* grContext) {
146   if (!GRREFERENCE->data())
147     return nullptr;
148 
149   int32_t iGRW = static_cast<int32_t>(GRW);
150   int32_t iGRH = static_cast<int32_t>(GRH);
151   auto GRREG = std::make_unique<CJBig2_Image>(iGRW, iGRH);
152   if (!GRREG->data())
153     return nullptr;
154 
155   int LTP = 0;
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();
162   if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1)
163     GRREFERENCEDY = 0;
164   intptr_t nOffset = -GRREFERENCEDY * nStrideR;
165   for (int32_t h = 0; h < iGRH; h++) {
166     if (TPGRON) {
167       if (pArithDecoder->IsComplete())
168         return nullptr;
169 
170       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0010]);
171     }
172     uint32_t line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
173     int32_t reference_h = h - GRREFERENCEDY;
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;
180     if (!LTP) {
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;
185         if (h > 0) {
186           line1 = (line1 << 8) |
187                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
188         }
189         if (h > GRHR + GRREFERENCEDY + 1) {
190           line1_r = 0;
191           line2_r = 0;
192           line3_r = 0;
193         } else {
194           if (line1_r_ok) {
195             line1_r =
196                 (line1_r << 8) |
197                 (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
198           }
199           if (line2_r_ok) {
200             line2_r = (line2_r << 8) |
201                       (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
202           }
203           if (line3_r_ok) {
204             line3_r =
205                 (line3_r << 8) |
206                 (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
207           } else {
208             line3_r = 0;
209           }
210         }
211         uint8_t cVal = 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);
220         }
221         pLine[w >> 3] = cVal;
222       }
223     } else {
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;
228         if (h > 0) {
229           line1 = (line1 << 8) |
230                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
231         }
232         if (line1_r_ok) {
233           line1_r =
234               (line1_r << 8) |
235               (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
236         }
237         if (line2_r_ok) {
238           line2_r = (line2_r << 8) |
239                     (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
240         }
241         if (line3_r_ok) {
242           line3_r =
243               (line3_r << 8) |
244               (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
245         } else {
246           line3_r = 0;
247         }
248         uint8_t cVal = 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)))) {
259             if (pArithDecoder->IsComplete())
260               return nullptr;
261 
262             bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
263           }
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);
270         }
271         pLine[w >> 3] = cVal;
272       }
273     }
274     pLine += nStride;
275     if (h < GRHR + GRREFERENCEDY)
276       pLineR += nStrideR;
277   }
278   return GRREG;
279 }
280 
DecodeTemplate1Unopt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)281 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate1Unopt(
282     CJBig2_ArithDecoder* pArithDecoder,
283     JBig2ArithCtx* grContext) {
284   auto GRREG = std::make_unique<CJBig2_Image>(GRW, GRH);
285   if (!GRREG->data())
286     return nullptr;
287 
288   GRREG->Fill(false);
289   int LTP = 0;
290   for (uint32_t h = 0; h < GRH; h++) {
291     if (TPGRON) {
292       if (pArithDecoder->IsComplete())
293         return nullptr;
294 
295       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0008]);
296     }
297     if (!LTP) {
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;
301       uint32_t line2 = 0;
302       uint32_t line3 =
303           GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
304       uint32_t line4 =
305           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
306       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
307       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
308                << 2;
309       uint32_t line5 =
310           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
311       line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
312                << 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;
319         if (pArithDecoder->IsComplete())
320           return nullptr;
321 
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)) &
328                 0x01;
329         line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
330                                                       h - GRREFERENCEDY)) &
331                 0x07;
332         line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
333                                                       h - GRREFERENCEDY + 1)) &
334                 0x03;
335       }
336     } else {
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;
340       uint32_t line2 = 0;
341       uint32_t line3 =
342           GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
343       uint32_t line4 =
344           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
345       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
346       line4 |= GRREFERENCE->GetPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY)
347                << 2;
348       uint32_t line5 =
349           GRREFERENCE->GetPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
350       line5 |= GRREFERENCE->GetPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1)
351                << 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;
367           if (pArithDecoder->IsComplete())
368             return nullptr;
369 
370           bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
371         }
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)) &
377                 0x01;
378         line4 = ((line4 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
379                                                       h - GRREFERENCEDY)) &
380                 0x07;
381         line5 = ((line5 << 1) | GRREFERENCE->GetPixel(w - GRREFERENCEDX + 2,
382                                                       h - GRREFERENCEDY + 1)) &
383                 0x03;
384       }
385     }
386   }
387   return GRREG;
388 }
389 
DecodeTemplate1Opt(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * grContext)390 std::unique_ptr<CJBig2_Image> CJBig2_GRRDProc::DecodeTemplate1Opt(
391     CJBig2_ArithDecoder* pArithDecoder,
392     JBig2ArithCtx* grContext) {
393   if (!GRREFERENCE->data())
394     return nullptr;
395 
396   int32_t iGRW = static_cast<int32_t>(GRW);
397   int32_t iGRH = static_cast<int32_t>(GRH);
398   auto GRREG = std::make_unique<CJBig2_Image>(iGRW, iGRH);
399   if (!GRREG->data())
400     return nullptr;
401 
402   int LTP = 0;
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();
409   if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
410     GRREFERENCEDY = 0;
411   }
412   intptr_t nOffset = -GRREFERENCEDY * nStrideR;
413   for (int32_t h = 0; h < iGRH; h++) {
414     if (TPGRON) {
415       if (pArithDecoder->IsComplete())
416         return nullptr;
417 
418       LTP = LTP ^ pArithDecoder->Decode(&grContext[0x0008]);
419     }
420     uint32_t line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
421     int32_t reference_h = h - GRREFERENCEDY;
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;
428     if (!LTP) {
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;
433         if (h > 0)
434           line1 = (line1 << 8) |
435                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
436         if (line1_r_ok)
437           line1_r =
438               (line1_r << 8) |
439               (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
440         if (line2_r_ok)
441           line2_r = (line2_r << 8) |
442                     (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
443         if (line3_r_ok) {
444           line3_r =
445               (line3_r << 8) |
446               (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
447         } else {
448           line3_r = 0;
449         }
450         uint8_t cVal = 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);
459         }
460         pLine[w >> 3] = cVal;
461       }
462     } else {
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;
467         if (h > 0)
468           line1 = (line1 << 8) |
469                   (w + 8 < iGRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
470         if (line1_r_ok)
471           line1_r =
472               (line1_r << 8) |
473               (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
474         if (line2_r_ok)
475           line2_r = (line2_r << 8) |
476                     (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
477         if (line3_r_ok) {
478           line3_r =
479               (line3_r << 8) |
480               (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
481         } else {
482           line3_r = 0;
483         }
484         uint8_t cVal = 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)))) {
495             if (pArithDecoder->IsComplete())
496               return nullptr;
497 
498             bVal = pArithDecoder->Decode(&grContext[CONTEXT]);
499           }
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);
506         }
507         pLine[w >> 3] = cVal;
508       }
509     }
510     pLine += nStride;
511     if (h < GRHR + GRREFERENCEDY)
512       pLineR += nStrideR;
513   }
514   return GRREG;
515 }
516