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 
decode_Arith(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * gbContext,IFX_Pause * pPause)15 CJBig2_PatternDict* CJBig2_PDDProc::decode_Arith(
16     CJBig2_ArithDecoder* pArithDecoder,
17     JBig2ArithCtx* gbContext,
18     IFX_Pause* pPause) {
19   uint32_t GRAY;
20   CJBig2_Image* BHDC = nullptr;
21   std::unique_ptr<CJBig2_PatternDict> pDict(new CJBig2_PatternDict());
22   pDict->NUMPATS = GRAYMAX + 1;
23   pDict->HDPATS = FX_Alloc(CJBig2_Image*, pDict->NUMPATS);
24   JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*) * pDict->NUMPATS);
25 
26   std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
27   pGRD->MMR = HDMMR;
28   pGRD->GBW = (GRAYMAX + 1) * HDPW;
29   pGRD->GBH = HDPH;
30   pGRD->GBTEMPLATE = HDTEMPLATE;
31   pGRD->TPGDON = 0;
32   pGRD->USESKIP = 0;
33   pGRD->GBAT[0] = -(int32_t)HDPW;
34   pGRD->GBAT[1] = 0;
35   if (pGRD->GBTEMPLATE == 0) {
36     pGRD->GBAT[2] = -3;
37     pGRD->GBAT[3] = -1;
38     pGRD->GBAT[4] = 2;
39     pGRD->GBAT[5] = -2;
40     pGRD->GBAT[6] = -2;
41     pGRD->GBAT[7] = -2;
42   }
43   FXCODEC_STATUS status =
44       pGRD->Start_decode_Arith(&BHDC, pArithDecoder, gbContext, nullptr);
45   while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE)
46     status = pGRD->Continue_decode(pPause);
47   if (!BHDC)
48     return nullptr;
49 
50   GRAY = 0;
51   while (GRAY <= GRAYMAX) {
52     pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
53     GRAY = GRAY + 1;
54   }
55   delete BHDC;
56   return pDict.release();
57 }
58 
decode_MMR(CJBig2_BitStream * pStream,IFX_Pause * pPause)59 CJBig2_PatternDict* CJBig2_PDDProc::decode_MMR(CJBig2_BitStream* pStream,
60                                                IFX_Pause* pPause) {
61   uint32_t GRAY;
62   CJBig2_Image* BHDC = nullptr;
63   std::unique_ptr<CJBig2_PatternDict> pDict(new CJBig2_PatternDict());
64   pDict->NUMPATS = GRAYMAX + 1;
65   pDict->HDPATS = FX_Alloc(CJBig2_Image*, pDict->NUMPATS);
66   JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*) * pDict->NUMPATS);
67 
68   std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
69   pGRD->MMR = HDMMR;
70   pGRD->GBW = (GRAYMAX + 1) * HDPW;
71   pGRD->GBH = HDPH;
72   pGRD->Start_decode_MMR(&BHDC, pStream, nullptr);
73   if (!BHDC)
74     return nullptr;
75 
76   GRAY = 0;
77   while (GRAY <= GRAYMAX) {
78     pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
79     GRAY = GRAY + 1;
80   }
81   delete BHDC;
82   return pDict.release();
83 }
84