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