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