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