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_HtrdProc.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 #include "core/fxcodec/jbig2/JBig2_BitStream.h"
13 #include "core/fxcodec/jbig2/JBig2_GrdProc.h"
14 #include "core/fxcodec/jbig2/JBig2_Image.h"
15 
DecodeArith(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * gbContext,PauseIndicatorIface * pPause)16 std::unique_ptr<CJBig2_Image> CJBig2_HTRDProc::DecodeArith(
17     CJBig2_ArithDecoder* pArithDecoder,
18     JBig2ArithCtx* gbContext,
19     PauseIndicatorIface* pPause) {
20   std::unique_ptr<CJBig2_Image> HSKIP;
21   if (HENABLESKIP == 1) {
22     HSKIP = std::make_unique<CJBig2_Image>(HGW, HGH);
23     for (uint32_t mg = 0; mg < HGH; ++mg) {
24       for (uint32_t ng = 0; ng < HGW; ++ng) {
25         int32_t x = (HGX + mg * HRY + ng * HRX) >> 8;
26         int32_t y = (HGY + mg * HRX - ng * HRY) >> 8;
27         if ((x + HPW <= 0) | (x >= static_cast<int32_t>(HBW)) | (y + HPH <= 0) |
28             (y >= static_cast<int32_t>(HPH))) {
29           HSKIP->SetPixel(ng, mg, 1);
30         } else {
31           HSKIP->SetPixel(ng, mg, 0);
32         }
33       }
34     }
35   }
36   uint32_t HBPP = 1;
37   while (static_cast<uint32_t>(1 << HBPP) < HNUMPATS)
38     ++HBPP;
39 
40   CJBig2_GRDProc GRD;
41   GRD.MMR = HMMR;
42   GRD.GBW = HGW;
43   GRD.GBH = HGH;
44   GRD.GBTEMPLATE = HTEMPLATE;
45   GRD.TPGDON = 0;
46   GRD.USESKIP = HENABLESKIP;
47   GRD.SKIP = HSKIP.get();
48   if (HTEMPLATE <= 1)
49     GRD.GBAT[0] = 3;
50   else
51     GRD.GBAT[0] = 2;
52   GRD.GBAT[1] = -1;
53   if (GRD.GBTEMPLATE == 0) {
54     GRD.GBAT[2] = -3;
55     GRD.GBAT[3] = -1;
56     GRD.GBAT[4] = 2;
57     GRD.GBAT[5] = -2;
58     GRD.GBAT[6] = -2;
59     GRD.GBAT[7] = -2;
60   }
61 
62   uint8_t GSBPP = static_cast<uint8_t>(HBPP);
63   std::vector<std::unique_ptr<CJBig2_Image>> GSPLANES(GSBPP);
64   for (int32_t i = GSBPP - 1; i >= 0; --i) {
65     std::unique_ptr<CJBig2_Image> pImage;
66     CJBig2_GRDProc::ProgressiveArithDecodeState state;
67     state.pImage = &pImage;
68     state.pArithDecoder = pArithDecoder;
69     state.gbContext = gbContext;
70     state.pPause = nullptr;
71     FXCODEC_STATUS status = GRD.StartDecodeArith(&state);
72     state.pPause = pPause;
73     while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE)
74       status = GRD.ContinueDecode(&state);
75     if (!pImage)
76       return nullptr;
77 
78     GSPLANES[i] = std::move(pImage);
79     if (i < GSBPP - 1)
80       GSPLANES[i]->ComposeFrom(0, 0, GSPLANES[i + 1].get(), JBIG2_COMPOSE_XOR);
81   }
82   return DecodeImage(GSPLANES);
83 }
84 
DecodeMMR(CJBig2_BitStream * pStream)85 std::unique_ptr<CJBig2_Image> CJBig2_HTRDProc::DecodeMMR(
86     CJBig2_BitStream* pStream) {
87   uint32_t HBPP = 1;
88   while (static_cast<uint32_t>(1 << HBPP) < HNUMPATS)
89     ++HBPP;
90 
91   CJBig2_GRDProc GRD;
92   GRD.MMR = HMMR;
93   GRD.GBW = HGW;
94   GRD.GBH = HGH;
95 
96   uint8_t GSBPP = static_cast<uint8_t>(HBPP);
97   std::vector<std::unique_ptr<CJBig2_Image>> GSPLANES(GSBPP);
98   GRD.StartDecodeMMR(&GSPLANES[GSBPP - 1], pStream);
99   if (!GSPLANES[GSBPP - 1])
100     return nullptr;
101 
102   pStream->alignByte();
103   pStream->offset(3);
104   for (int32_t J = GSBPP - 2; J >= 0; --J) {
105     GRD.StartDecodeMMR(&GSPLANES[J], pStream);
106     if (!GSPLANES[J])
107       return nullptr;
108 
109     pStream->alignByte();
110     pStream->offset(3);
111     GSPLANES[J]->ComposeFrom(0, 0, GSPLANES[J + 1].get(), JBIG2_COMPOSE_XOR);
112   }
113   return DecodeImage(GSPLANES);
114 }
115 
DecodeImage(const std::vector<std::unique_ptr<CJBig2_Image>> & GSPLANES)116 std::unique_ptr<CJBig2_Image> CJBig2_HTRDProc::DecodeImage(
117     const std::vector<std::unique_ptr<CJBig2_Image>>& GSPLANES) {
118   auto HTREG = std::make_unique<CJBig2_Image>(HBW, HBH);
119   if (!HTREG->data())
120     return nullptr;
121 
122   HTREG->Fill(HDEFPIXEL);
123   for (uint32_t y = 0; y < HGH; ++y) {
124     for (uint32_t x = 0; x < HGW; ++x) {
125       uint32_t gsval = 0;
126       for (uint8_t i = 0; i < GSPLANES.size(); ++i)
127         gsval |= GSPLANES[i]->GetPixel(x, y) << i;
128       uint32_t pat_index = std::min(gsval, HNUMPATS - 1);
129       int32_t out_x = (HGX + y * HRY + x * HRX) >> 8;
130       int32_t out_y = (HGY + y * HRX - x * HRY) >> 8;
131       (*HPATS)[pat_index]->ComposeTo(HTREG.get(), out_x, out_y, HCOMBOP);
132     }
133   }
134   return HTREG;
135 }
136