• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GsidProc.h"
8 
9 #include <memory>
10 #include <vector>
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 #include "core/fxcrt/fx_basic.h"
16 
decode_Arith(CJBig2_ArithDecoder * pArithDecoder,JBig2ArithCtx * gbContext,IFX_Pause * pPause)17 uint32_t* CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
18                                         JBig2ArithCtx* gbContext,
19                                         IFX_Pause* pPause) {
20   std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
21   pGRD->MMR = GSMMR;
22   pGRD->GBW = GSW;
23   pGRD->GBH = GSH;
24   pGRD->GBTEMPLATE = GSTEMPLATE;
25   pGRD->TPGDON = 0;
26   pGRD->USESKIP = GSUSESKIP;
27   pGRD->SKIP = GSKIP;
28   if (GSTEMPLATE <= 1) {
29     pGRD->GBAT[0] = 3;
30   } else {
31     pGRD->GBAT[0] = 2;
32   }
33   pGRD->GBAT[1] = -1;
34   if (pGRD->GBTEMPLATE == 0) {
35     pGRD->GBAT[2] = -3;
36     pGRD->GBAT[3] = -1;
37     pGRD->GBAT[4] = 2;
38     pGRD->GBAT[5] = -2;
39     pGRD->GBAT[6] = -2;
40     pGRD->GBAT[7] = -2;
41   }
42 
43   std::vector<std::unique_ptr<CJBig2_Image>> GSPLANES(GSBPP);
44   for (int32_t i = GSBPP - 1; i >= 0; --i) {
45     CJBig2_Image* pImage = nullptr;
46     FXCODEC_STATUS status =
47         pGRD->Start_decode_Arith(&pImage, pArithDecoder, gbContext, nullptr);
48     while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE)
49       status = pGRD->Continue_decode(pPause);
50 
51     if (!pImage)
52       return nullptr;
53 
54     GSPLANES[i].reset(pImage);
55     if (i < GSBPP - 1)
56       pImage->composeFrom(0, 0, GSPLANES[i + 1].get(), JBIG2_COMPOSE_XOR);
57   }
58   std::unique_ptr<uint32_t, FxFreeDeleter> GSVALS(
59       FX_Alloc2D(uint32_t, GSW, GSH));
60   JBIG2_memset(GSVALS.get(), 0, sizeof(uint32_t) * GSW * GSH);
61   for (uint32_t y = 0; y < GSH; ++y) {
62     for (uint32_t x = 0; x < GSW; ++x) {
63       for (int32_t i = 0; i < GSBPP; ++i)
64         GSVALS.get()[y * GSW + x] |= GSPLANES[i]->getPixel(x, y) << i;
65     }
66   }
67   return GSVALS.release();
68 }
69 
decode_MMR(CJBig2_BitStream * pStream,IFX_Pause * pPause)70 uint32_t* CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream* pStream,
71                                       IFX_Pause* pPause) {
72   std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc());
73   pGRD->MMR = GSMMR;
74   pGRD->GBW = GSW;
75   pGRD->GBH = GSH;
76 
77   std::unique_ptr<CJBig2_Image*> GSPLANES(FX_Alloc(CJBig2_Image*, GSBPP));
78   JBIG2_memset(GSPLANES.get(), 0, sizeof(CJBig2_Image*) * GSBPP);
79   pGRD->Start_decode_MMR(&GSPLANES.get()[GSBPP - 1], pStream, nullptr);
80   if (!GSPLANES.get()[GSBPP - 1])
81     return nullptr;
82 
83   pStream->alignByte();
84   pStream->offset(3);
85   int32_t J = GSBPP - 2;
86   while (J >= 0) {
87     pGRD->Start_decode_MMR(&GSPLANES.get()[J], pStream, nullptr);
88     if (!GSPLANES.get()[J]) {
89       for (int32_t K = GSBPP - 1; K > J; --K)
90         delete GSPLANES.get()[K];
91       return nullptr;
92     }
93     pStream->alignByte();
94     pStream->offset(3);
95     GSPLANES.get()[J]->composeFrom(0, 0, GSPLANES.get()[J + 1],
96                                    JBIG2_COMPOSE_XOR);
97     J = J - 1;
98   }
99   std::unique_ptr<uint32_t> GSVALS(FX_Alloc2D(uint32_t, GSW, GSH));
100   JBIG2_memset(GSVALS.get(), 0, sizeof(uint32_t) * GSW * GSH);
101   for (uint32_t y = 0; y < GSH; ++y) {
102     for (uint32_t x = 0; x < GSW; ++x) {
103       for (J = 0; J < GSBPP; ++J) {
104         GSVALS.get()[y * GSW + x] |= GSPLANES.get()[J]->getPixel(x, y) << J;
105       }
106     }
107   }
108   for (J = 0; J < GSBPP; ++J) {
109     delete GSPLANES.get()[J];
110   }
111   return GSVALS.release();
112 }
113