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 "JBig2_GsidProc.h" 8 9 #include <memory> 10 11 #include "JBig2_BitStream.h" 12 #include "JBig2_GrdProc.h" 13 #include "JBig2_Image.h" 14 #include "JBig2_List.h" 15 #include "core/include/fxcrt/fx_basic.h" 16 17 FX_DWORD* 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 CJBig2_List<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 pGRD->Continue_decode(pPause); 50 51 if (!pImage) 52 return nullptr; 53 54 GSPLANES.set(i, pImage); 55 56 if (i < GSBPP - 1) 57 pImage->composeFrom(0, 0, GSPLANES.get(i + 1), JBIG2_COMPOSE_XOR); 58 } 59 std::unique_ptr<FX_DWORD, FxFreeDeleter> GSVALS( 60 FX_Alloc2D(FX_DWORD, GSW, GSH)); 61 JBIG2_memset(GSVALS.get(), 0, sizeof(FX_DWORD) * GSW * GSH); 62 for (FX_DWORD y = 0; y < GSH; ++y) { 63 for (FX_DWORD x = 0; x < GSW; ++x) { 64 for (int32_t i = 0; i < GSBPP; ++i) { 65 GSVALS.get()[y * GSW + x] |= GSPLANES.get(i)->getPixel(x, y) << i; 66 } 67 } 68 } 69 return GSVALS.release(); 70 } 71 72 FX_DWORD* CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream* pStream, 73 IFX_Pause* pPause) { 74 std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc()); 75 pGRD->MMR = GSMMR; 76 pGRD->GBW = GSW; 77 pGRD->GBH = GSH; 78 79 std::unique_ptr<CJBig2_Image*> GSPLANES(FX_Alloc(CJBig2_Image*, GSBPP)); 80 JBIG2_memset(GSPLANES.get(), 0, sizeof(CJBig2_Image*) * GSBPP); 81 FXCODEC_STATUS status = 82 pGRD->Start_decode_MMR(&GSPLANES.get()[GSBPP - 1], pStream, nullptr); 83 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { 84 pGRD->Continue_decode(pPause); 85 } 86 if (!GSPLANES.get()[GSBPP - 1]) 87 return nullptr; 88 89 pStream->alignByte(); 90 pStream->offset(3); 91 int32_t J = GSBPP - 2; 92 while (J >= 0) { 93 FXCODEC_STATUS status = 94 pGRD->Start_decode_MMR(&GSPLANES.get()[J], pStream, nullptr); 95 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { 96 pGRD->Continue_decode(pPause); 97 } 98 if (!GSPLANES.get()[J]) { 99 for (int32_t K = GSBPP - 1; K > J; --K) { 100 delete GSPLANES.get()[K]; 101 return nullptr; 102 } 103 } 104 pStream->alignByte(); 105 pStream->offset(3); 106 GSPLANES.get()[J]->composeFrom(0, 0, GSPLANES.get()[J + 1], 107 JBIG2_COMPOSE_XOR); 108 J = J - 1; 109 } 110 std::unique_ptr<FX_DWORD> GSVALS(FX_Alloc2D(FX_DWORD, GSW, GSH)); 111 JBIG2_memset(GSVALS.get(), 0, sizeof(FX_DWORD) * GSW * GSH); 112 for (FX_DWORD y = 0; y < GSH; ++y) { 113 for (FX_DWORD x = 0; x < GSW; ++x) { 114 for (J = 0; J < GSBPP; ++J) { 115 GSVALS.get()[y * GSW + x] |= GSPLANES.get()[J]->getPixel(x, y) << J; 116 } 117 } 118 } 119 for (J = 0; J < GSBPP; ++J) { 120 delete GSPLANES.get()[J]; 121 } 122 return GSVALS.release(); 123 } 124