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