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_PddProc.h"
8
9 #include <memory>
10
11 #include "core/fxcodec/jbig2/JBig2_GrdProc.h"
12 #include "core/fxcodec/jbig2/JBig2_Image.h"
13 #include "core/fxcodec/jbig2/JBig2_PatternDict.h"
14
DecodeArith(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * gbContext,PauseIndicatorIface * pPause)15 std::unique_ptr<CJBig2_PatternDict> CJBig2_PDDProc::DecodeArith(
16 CJBig2_ArithDecoder* pArithDecoder,
17 JBig2ArithCtx* gbContext,
18 PauseIndicatorIface* pPause) {
19 std::unique_ptr<CJBig2_GRDProc> pGRD = CreateGRDProc();
20 if (!pGRD)
21 return nullptr;
22
23 pGRD->GBTEMPLATE = HDTEMPLATE;
24 pGRD->TPGDON = false;
25 pGRD->USESKIP = false;
26 pGRD->GBAT[0] = -1 * static_cast<int32_t>(HDPW);
27 pGRD->GBAT[1] = 0;
28 if (pGRD->GBTEMPLATE == 0) {
29 pGRD->GBAT[2] = -3;
30 pGRD->GBAT[3] = -1;
31 pGRD->GBAT[4] = 2;
32 pGRD->GBAT[5] = -2;
33 pGRD->GBAT[6] = -2;
34 pGRD->GBAT[7] = -2;
35 }
36
37 std::unique_ptr<CJBig2_Image> BHDC;
38 CJBig2_GRDProc::ProgressiveArithDecodeState state;
39 state.pImage = &BHDC;
40 state.pArithDecoder = pArithDecoder;
41 state.gbContext = gbContext;
42 state.pPause = nullptr;
43
44 FXCODEC_STATUS status = pGRD->StartDecodeArith(&state);
45 state.pPause = pPause;
46 while (status == FXCODEC_STATUS::kDecodeToBeContinued)
47 status = pGRD->ContinueDecode(&state);
48 if (!BHDC)
49 return nullptr;
50
51 auto pDict = std::make_unique<CJBig2_PatternDict>(GRAYMAX + 1);
52 for (uint32_t GRAY = 0; GRAY <= GRAYMAX; ++GRAY)
53 pDict->HDPATS[GRAY] = BHDC->SubImage(HDPW * GRAY, 0, HDPW, HDPH);
54 return pDict;
55 }
56
DecodeMMR(CJBig2_BitStream * pStream)57 std::unique_ptr<CJBig2_PatternDict> CJBig2_PDDProc::DecodeMMR(
58 CJBig2_BitStream* pStream) {
59 std::unique_ptr<CJBig2_GRDProc> pGRD = CreateGRDProc();
60 if (!pGRD)
61 return nullptr;
62
63 std::unique_ptr<CJBig2_Image> BHDC;
64 pGRD->StartDecodeMMR(&BHDC, pStream);
65 if (!BHDC)
66 return nullptr;
67
68 auto pDict = std::make_unique<CJBig2_PatternDict>(GRAYMAX + 1);
69 for (uint32_t GRAY = 0; GRAY <= GRAYMAX; ++GRAY)
70 pDict->HDPATS[GRAY] = BHDC->SubImage(HDPW * GRAY, 0, HDPW, HDPH);
71 return pDict;
72 }
73
CreateGRDProc()74 std::unique_ptr<CJBig2_GRDProc> CJBig2_PDDProc::CreateGRDProc() {
75 uint32_t width = (GRAYMAX + 1) * HDPW;
76 uint32_t height = HDPH;
77 if (width > kJBig2MaxImageSize || height > kJBig2MaxImageSize)
78 return nullptr;
79
80 auto pGRD = std::make_unique<CJBig2_GRDProc>();
81 pGRD->MMR = HDMMR;
82 pGRD->GBW = width;
83 pGRD->GBH = height;
84 return pGRD;
85 }
86