| OLD | NEW |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fxcodec/jbig2/JBig2_TrdProc.h" | 7 #include "core/fxcodec/jbig2/JBig2_TrdProc.h" |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "core/fxcodec/jbig2/JBig2_ArithDecoder.h" | 11 #include "core/fxcodec/jbig2/JBig2_ArithDecoder.h" |
| 12 #include "core/fxcodec/jbig2/JBig2_ArithIntDecoder.h" | 12 #include "core/fxcodec/jbig2/JBig2_ArithIntDecoder.h" |
| 13 #include "core/fxcodec/jbig2/JBig2_GrrdProc.h" | 13 #include "core/fxcodec/jbig2/JBig2_GrrdProc.h" |
| 14 #include "core/fxcodec/jbig2/JBig2_HuffmanDecoder.h" | 14 #include "core/fxcodec/jbig2/JBig2_HuffmanDecoder.h" |
| 15 | 15 |
| 16 CJBig2_Image* CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream* pStream, | 16 CJBig2_Image* CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream* pStream, |
| 17 JBig2ArithCtx* grContext) { | 17 JBig2ArithCtx* grContext) { |
| 18 int32_t STRIPT, FIRSTS; | |
| 19 FX_DWORD NINSTANCES; | |
| 20 int32_t DT, DFS, CURS; | |
| 21 int32_t SI, TI; | |
| 22 CJBig2_Image* IBI; | |
| 23 FX_DWORD WI, HI; | |
| 24 int32_t IDS; | |
| 25 FX_BOOL RI; | |
| 26 int32_t RDWI, RDHI, RDXI, RDYI; | |
| 27 CJBig2_Image* IBOI; | |
| 28 FX_DWORD WOI, HOI; | |
| 29 FX_BOOL bFirst; | |
| 30 FX_DWORD nTmp; | |
| 31 int32_t nVal, nBits; | |
| 32 std::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder( | 18 std::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder( |
| 33 new CJBig2_HuffmanDecoder(pStream)); | 19 new CJBig2_HuffmanDecoder(pStream)); |
| 34 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); | 20 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); |
| 35 SBREG->fill(SBDEFPIXEL); | 21 SBREG->fill(SBDEFPIXEL); |
| 22 int32_t STRIPT; |
| 36 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) | 23 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) |
| 37 return nullptr; | 24 return nullptr; |
| 38 | 25 |
| 39 STRIPT *= SBSTRIPS; | 26 STRIPT *= SBSTRIPS; |
| 40 STRIPT = -STRIPT; | 27 STRIPT = -STRIPT; |
| 41 FIRSTS = 0; | 28 FX_DWORD NINSTANCES = 0; |
| 42 NINSTANCES = 0; | |
| 43 while (NINSTANCES < SBNUMINSTANCES) { | 29 while (NINSTANCES < SBNUMINSTANCES) { |
| 30 int32_t DT; |
| 44 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) | 31 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) |
| 45 return nullptr; | 32 return nullptr; |
| 46 | 33 |
| 47 DT *= SBSTRIPS; | 34 DT *= SBSTRIPS; |
| 48 STRIPT = STRIPT + DT; | 35 STRIPT = STRIPT + DT; |
| 49 bFirst = TRUE; | 36 bool bFirst = true; |
| 37 int32_t FIRSTS = 0; |
| 38 int32_t CURS = 0; |
| 50 for (;;) { | 39 for (;;) { |
| 51 if (bFirst) { | 40 if (bFirst) { |
| 41 int32_t DFS; |
| 52 if (pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) | 42 if (pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) |
| 53 return nullptr; | 43 return nullptr; |
| 54 | 44 |
| 55 FIRSTS = FIRSTS + DFS; | 45 FIRSTS = FIRSTS + DFS; |
| 56 CURS = FIRSTS; | 46 CURS = FIRSTS; |
| 57 bFirst = FALSE; | 47 bFirst = false; |
| 58 } else { | 48 } else { |
| 59 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS); | 49 int32_t IDS; |
| 50 int32_t nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS); |
| 60 if (nVal == JBIG2_OOB) { | 51 if (nVal == JBIG2_OOB) { |
| 61 break; | 52 break; |
| 62 } else if (nVal != 0) { | 53 } else if (nVal != 0) { |
| 63 return nullptr; | 54 return nullptr; |
| 64 } else { | 55 } else { |
| 65 CURS = CURS + IDS + SBDSOFFSET; | 56 CURS = CURS + IDS + SBDSOFFSET; |
| 66 } | 57 } |
| 67 } | 58 } |
| 68 uint8_t CURT = 0; | 59 uint8_t CURT = 0; |
| 69 if (SBSTRIPS != 1) { | 60 if (SBSTRIPS != 1) { |
| 70 nTmp = 1; | 61 FX_DWORD nTmp = 1; |
| 71 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { | 62 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { |
| 72 nTmp++; | 63 nTmp++; |
| 73 } | 64 } |
| 65 int32_t nVal; |
| 74 if (pStream->readNBits(nTmp, &nVal) != 0) | 66 if (pStream->readNBits(nTmp, &nVal) != 0) |
| 75 return nullptr; | 67 return nullptr; |
| 76 | 68 |
| 77 CURT = nVal; | 69 CURT = nVal; |
| 78 } | 70 } |
| 79 TI = STRIPT + CURT; | 71 int32_t TI = STRIPT + CURT; |
| 80 nVal = 0; | 72 int32_t nVal = 0; |
| 81 nBits = 0; | 73 int32_t nBits = 0; |
| 82 FX_DWORD IDI; | 74 FX_DWORD IDI; |
| 83 for (;;) { | 75 for (;;) { |
| 76 FX_DWORD nTmp; |
| 84 if (pStream->read1Bit(&nTmp) != 0) | 77 if (pStream->read1Bit(&nTmp) != 0) |
| 85 return nullptr; | 78 return nullptr; |
| 86 | 79 |
| 87 nVal = (nVal << 1) | nTmp; | 80 nVal = (nVal << 1) | nTmp; |
| 88 nBits++; | 81 nBits++; |
| 89 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { | 82 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { |
| 90 if ((nBits == SBSYMCODES[IDI].codelen) && | 83 if ((nBits == SBSYMCODES[IDI].codelen) && |
| 91 (nVal == SBSYMCODES[IDI].code)) { | 84 (nVal == SBSYMCODES[IDI].code)) { |
| 92 break; | 85 break; |
| 93 } | 86 } |
| 94 } | 87 } |
| 95 if (IDI < SBNUMSYMS) { | 88 if (IDI < SBNUMSYMS) { |
| 96 break; | 89 break; |
| 97 } | 90 } |
| 98 } | 91 } |
| 99 if (SBREFINE == 0) { | 92 FX_BOOL RI = 0; |
| 100 RI = 0; | 93 if (SBREFINE != 0 && pStream->read1Bit(&RI) != 0) { |
| 101 } else { | 94 return nullptr; |
| 102 if (pStream->read1Bit(&RI) != 0) { | |
| 103 return nullptr; | |
| 104 } | |
| 105 } | 95 } |
| 96 CJBig2_Image* IBI = nullptr; |
| 106 if (RI == 0) { | 97 if (RI == 0) { |
| 107 IBI = SBSYMS[IDI]; | 98 IBI = SBSYMS[IDI]; |
| 108 } else { | 99 } else { |
| 100 int32_t RDWI; |
| 101 int32_t RDHI; |
| 102 int32_t RDXI; |
| 103 int32_t RDYI; |
| 109 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0) || | 104 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0) || |
| 110 (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0) || | 105 (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0) || |
| 111 (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0) || | 106 (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0) || |
| 112 (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0) || | 107 (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0) || |
| 113 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) { | 108 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) { |
| 114 return nullptr; | 109 return nullptr; |
| 115 } | 110 } |
| 116 pStream->alignByte(); | 111 pStream->alignByte(); |
| 117 nTmp = pStream->getOffset(); | 112 FX_DWORD nTmp = pStream->getOffset(); |
| 118 IBOI = SBSYMS[IDI]; | 113 CJBig2_Image* IBOI = SBSYMS[IDI]; |
| 119 if (!IBOI) | 114 if (!IBOI) |
| 120 return nullptr; | 115 return nullptr; |
| 121 | 116 |
| 122 WOI = IBOI->m_nWidth; | 117 FX_DWORD WOI = IBOI->m_nWidth; |
| 123 HOI = IBOI->m_nHeight; | 118 FX_DWORD HOI = IBOI->m_nHeight; |
| 124 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) | 119 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) |
| 125 return nullptr; | 120 return nullptr; |
| 126 | 121 |
| 127 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); | 122 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); |
| 128 pGRRD->GRW = WOI + RDWI; | 123 pGRRD->GRW = WOI + RDWI; |
| 129 pGRRD->GRH = HOI + RDHI; | 124 pGRRD->GRH = HOI + RDHI; |
| 130 pGRRD->GRTEMPLATE = SBRTEMPLATE; | 125 pGRRD->GRTEMPLATE = SBRTEMPLATE; |
| 131 pGRRD->GRREFERENCE = IBOI; | 126 pGRRD->GRREFERENCE = IBOI; |
| 132 pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI; | 127 pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI; |
| 133 pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI; | 128 pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 148 pStream->alignByte(); | 143 pStream->alignByte(); |
| 149 pStream->offset(2); | 144 pStream->offset(2); |
| 150 if ((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) { | 145 if ((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) { |
| 151 delete IBI; | 146 delete IBI; |
| 152 return nullptr; | 147 return nullptr; |
| 153 } | 148 } |
| 154 } | 149 } |
| 155 if (!IBI) { | 150 if (!IBI) { |
| 156 continue; | 151 continue; |
| 157 } | 152 } |
| 158 WI = IBI->m_nWidth; | 153 FX_DWORD WI = IBI->m_nWidth; |
| 159 HI = IBI->m_nHeight; | 154 FX_DWORD HI = IBI->m_nHeight; |
| 160 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) || | 155 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) || |
| 161 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { | 156 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { |
| 162 CURS = CURS + WI - 1; | 157 CURS = CURS + WI - 1; |
| 163 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) || | 158 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) || |
| 164 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { | 159 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { |
| 165 CURS = CURS + HI - 1; | 160 CURS = CURS + HI - 1; |
| 166 } | 161 } |
| 167 SI = CURS; | 162 int32_t SI = CURS; |
| 168 if (TRANSPOSED == 0) { | 163 if (TRANSPOSED == 0) { |
| 169 switch (REFCORNER) { | 164 switch (REFCORNER) { |
| 170 case JBIG2_CORNER_TOPLEFT: | 165 case JBIG2_CORNER_TOPLEFT: |
| 171 SBREG->composeFrom(SI, TI, IBI, SBCOMBOP); | 166 SBREG->composeFrom(SI, TI, IBI, SBCOMBOP); |
| 172 break; | 167 break; |
| 173 case JBIG2_CORNER_TOPRIGHT: | 168 case JBIG2_CORNER_TOPRIGHT: |
| 174 SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP); | 169 SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP); |
| 175 break; | 170 break; |
| 176 case JBIG2_CORNER_BOTTOMLEFT: | 171 case JBIG2_CORNER_BOTTOMLEFT: |
| 177 SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP); | 172 SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 } | 265 } |
| 271 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); | 266 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); |
| 272 SBREG->fill(SBDEFPIXEL); | 267 SBREG->fill(SBDEFPIXEL); |
| 273 int32_t STRIPT; | 268 int32_t STRIPT; |
| 274 pIADT->decode(pArithDecoder, &STRIPT); | 269 pIADT->decode(pArithDecoder, &STRIPT); |
| 275 STRIPT *= SBSTRIPS; | 270 STRIPT *= SBSTRIPS; |
| 276 STRIPT = -STRIPT; | 271 STRIPT = -STRIPT; |
| 277 int32_t FIRSTS = 0; | 272 int32_t FIRSTS = 0; |
| 278 FX_DWORD NINSTANCES = 0; | 273 FX_DWORD NINSTANCES = 0; |
| 279 while (NINSTANCES < SBNUMINSTANCES) { | 274 while (NINSTANCES < SBNUMINSTANCES) { |
| 280 int32_t CURS; | 275 int32_t CURS = 0; |
| 281 int32_t DT; | 276 int32_t DT; |
| 282 pIADT->decode(pArithDecoder, &DT); | 277 pIADT->decode(pArithDecoder, &DT); |
| 283 DT *= SBSTRIPS; | 278 DT *= SBSTRIPS; |
| 284 STRIPT += DT; | 279 STRIPT += DT; |
| 285 bool bFirst = true; | 280 bool bFirst = true; |
| 286 for (;;) { | 281 for (;;) { |
| 287 if (bFirst) { | 282 if (bFirst) { |
| 288 int32_t DFS; | 283 int32_t DFS; |
| 289 pIAFS->decode(pArithDecoder, &DFS); | 284 pIAFS->decode(pArithDecoder, &DFS); |
| 290 FIRSTS += DFS; | 285 FIRSTS += DFS; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 CURS += WI - 1; | 393 CURS += WI - 1; |
| 399 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) || | 394 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) || |
| 400 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) { | 395 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) { |
| 401 CURS += HI - 1; | 396 CURS += HI - 1; |
| 402 } | 397 } |
| 403 ++NINSTANCES; | 398 ++NINSTANCES; |
| 404 } | 399 } |
| 405 } | 400 } |
| 406 return SBREG.release(); | 401 return SBREG.release(); |
| 407 } | 402 } |
| OLD | NEW |