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 "JBig2_HtrdProc.h"
8 
9 #include <memory>
10 
11 #include "JBig2_GsidProc.h"
12 #include "core/include/fxcrt/fx_basic.h"
13 
decode_Arith(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * gbContext,IFX_Pause * pPause)14 CJBig2_Image* CJBig2_HTRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
15                                             JBig2ArithCtx* gbContext,
16                                             IFX_Pause* pPause) {
17   FX_DWORD ng, mg;
18   int32_t x, y;
19   FX_DWORD HBPP;
20   FX_DWORD* GI;
21   std::unique_ptr<CJBig2_Image> HSKIP;
22   std::unique_ptr<CJBig2_Image> HTREG(new CJBig2_Image(HBW, HBH));
23   HTREG->fill(HDEFPIXEL);
24   if (HENABLESKIP == 1) {
25     HSKIP.reset(new CJBig2_Image(HGW, HGH));
26     for (mg = 0; mg < HGH; mg++) {
27       for (ng = 0; ng < HGW; ng++) {
28         x = (HGX + mg * HRY + ng * HRX) >> 8;
29         y = (HGY + mg * HRX - ng * HRY) >> 8;
30         if ((x + HPW <= 0) | (x >= (int32_t)HBW) | (y + HPH <= 0) |
31             (y >= (int32_t)HPH)) {
32           HSKIP->setPixel(ng, mg, 1);
33         } else {
34           HSKIP->setPixel(ng, mg, 0);
35         }
36       }
37     }
38   }
39   HBPP = 1;
40   while ((FX_DWORD)(1 << HBPP) < HNUMPATS) {
41     HBPP++;
42   }
43   std::unique_ptr<CJBig2_GSIDProc> pGID(new CJBig2_GSIDProc());
44   pGID->GSMMR = HMMR;
45   pGID->GSW = HGW;
46   pGID->GSH = HGH;
47   pGID->GSBPP = (uint8_t)HBPP;
48   pGID->GSUSESKIP = HENABLESKIP;
49   pGID->GSKIP = HSKIP.get();
50   pGID->GSTEMPLATE = HTEMPLATE;
51   GI = pGID->decode_Arith(pArithDecoder, gbContext, pPause);
52   if (!GI)
53     return nullptr;
54 
55   for (mg = 0; mg < HGH; mg++) {
56     for (ng = 0; ng < HGW; ng++) {
57       x = (HGX + mg * HRY + ng * HRX) >> 8;
58       y = (HGY + mg * HRX - ng * HRY) >> 8;
59       FX_DWORD pat_index = GI[mg * HGW + ng];
60       if (pat_index >= HNUMPATS) {
61         pat_index = HNUMPATS - 1;
62       }
63       HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
64     }
65   }
66   FX_Free(GI);
67   return HTREG.release();
68 }
69 
decode_MMR(CJBig2_BitStream * pStream,IFX_Pause * pPause)70 CJBig2_Image* CJBig2_HTRDProc::decode_MMR(CJBig2_BitStream* pStream,
71                                           IFX_Pause* pPause) {
72   FX_DWORD ng, mg;
73   int32_t x, y;
74   FX_DWORD* GI;
75   std::unique_ptr<CJBig2_Image> HTREG(new CJBig2_Image(HBW, HBH));
76   HTREG->fill(HDEFPIXEL);
77   FX_DWORD HBPP = 1;
78   while ((FX_DWORD)(1 << HBPP) < HNUMPATS) {
79     HBPP++;
80   }
81   std::unique_ptr<CJBig2_GSIDProc> pGID(new CJBig2_GSIDProc());
82   pGID->GSMMR = HMMR;
83   pGID->GSW = HGW;
84   pGID->GSH = HGH;
85   pGID->GSBPP = (uint8_t)HBPP;
86   pGID->GSUSESKIP = 0;
87   GI = pGID->decode_MMR(pStream, pPause);
88   if (!GI)
89     return nullptr;
90 
91   for (mg = 0; mg < HGH; mg++) {
92     for (ng = 0; ng < HGW; ng++) {
93       x = (HGX + mg * HRY + ng * HRX) >> 8;
94       y = (HGY + mg * HRX - ng * HRY) >> 8;
95       FX_DWORD pat_index = GI[mg * HGW + ng];
96       if (pat_index >= HNUMPATS) {
97         pat_index = HNUMPATS - 1;
98       }
99       HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
100     }
101   }
102   FX_Free(GI);
103   return HTREG.release();
104 }
105