Chromium Code Reviews| 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; | 18 int32_t STRIPT = 0; |
| 19 FX_DWORD NINSTANCES; | 19 int32_t DT = 0, DFS = 0; |
| 20 int32_t DT, DFS, CURS; | 20 int32_t CURS = 0; |
| 21 int32_t SI, TI; | 21 CJBig2_Image* IBI = nullptr; |
| 22 CJBig2_Image* IBI; | 22 int32_t IDS = 0; |
| 23 FX_DWORD WI, HI; | 23 FX_BOOL RI = 0; |
| 24 int32_t IDS; | 24 int32_t RDWI = 0, RDHI = 0, RDXI = 0, RDYI = 0; |
|
dsinclair
2016/03/16 13:23:32
ditto
Wei Li
2016/03/16 17:51:31
Done.
| |
| 25 FX_BOOL RI; | 25 FX_DWORD nTmp = 0; |
| 26 int32_t RDWI, RDHI, RDXI, RDYI; | 26 int32_t nVal = 0, nBits = 0; |
|
Tom Sepez
2016/03/16 18:34:59
These are probably OK since the compiler can't pro
Wei Li
2016/03/17 02:24:05
Very good idea. :)
| |
| 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( | 27 std::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder( |
| 33 new CJBig2_HuffmanDecoder(pStream)); | 28 new CJBig2_HuffmanDecoder(pStream)); |
| 34 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); | 29 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); |
| 35 SBREG->fill(SBDEFPIXEL); | 30 SBREG->fill(SBDEFPIXEL); |
| 36 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) | 31 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) |
| 37 return nullptr; | 32 return nullptr; |
| 38 | 33 |
| 39 STRIPT *= SBSTRIPS; | 34 STRIPT *= SBSTRIPS; |
| 40 STRIPT = -STRIPT; | 35 STRIPT = -STRIPT; |
| 41 FIRSTS = 0; | 36 int32_t FIRSTS = 0; |
| 42 NINSTANCES = 0; | 37 FX_DWORD NINSTANCES = 0; |
| 43 while (NINSTANCES < SBNUMINSTANCES) { | 38 while (NINSTANCES < SBNUMINSTANCES) { |
| 44 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) | 39 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) |
| 45 return nullptr; | 40 return nullptr; |
| 46 | 41 |
| 47 DT *= SBSTRIPS; | 42 DT *= SBSTRIPS; |
| 48 STRIPT = STRIPT + DT; | 43 STRIPT = STRIPT + DT; |
| 49 bFirst = TRUE; | 44 FX_BOOL bFirst = TRUE; |
|
dsinclair
2016/03/16 13:23:32
nit: bool
Wei Li
2016/03/16 17:51:31
Done.
| |
| 50 for (;;) { | 45 for (;;) { |
| 51 if (bFirst) { | 46 if (bFirst) { |
| 52 if (pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) | 47 if (pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) |
| 53 return nullptr; | 48 return nullptr; |
| 54 | 49 |
| 55 FIRSTS = FIRSTS + DFS; | 50 FIRSTS = FIRSTS + DFS; |
| 56 CURS = FIRSTS; | 51 CURS = FIRSTS; |
| 57 bFirst = FALSE; | 52 bFirst = FALSE; |
| 58 } else { | 53 } else { |
| 59 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS); | 54 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS); |
| 60 if (nVal == JBIG2_OOB) { | 55 if (nVal == JBIG2_OOB) { |
| 61 break; | 56 break; |
| 62 } else if (nVal != 0) { | 57 } else if (nVal != 0) { |
| 63 return nullptr; | 58 return nullptr; |
| 64 } else { | 59 } else { |
| 65 CURS = CURS + IDS + SBDSOFFSET; | 60 CURS = CURS + IDS + SBDSOFFSET; |
| 66 } | 61 } |
| 67 } | 62 } |
| 68 uint8_t CURT = 0; | 63 uint8_t CURT = 0; |
| 69 if (SBSTRIPS != 1) { | 64 if (SBSTRIPS != 1) { |
| 70 nTmp = 1; | 65 nTmp = 1; |
| 71 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { | 66 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { |
| 72 nTmp++; | 67 nTmp++; |
| 73 } | 68 } |
| 74 if (pStream->readNBits(nTmp, &nVal) != 0) | 69 if (pStream->readNBits(nTmp, &nVal) != 0) |
| 75 return nullptr; | 70 return nullptr; |
| 76 | 71 |
| 77 CURT = nVal; | 72 CURT = nVal; |
| 78 } | 73 } |
| 79 TI = STRIPT + CURT; | 74 int32_t TI = STRIPT + CURT; |
| 80 nVal = 0; | 75 nVal = 0; |
| 81 nBits = 0; | 76 nBits = 0; |
| 82 FX_DWORD IDI; | 77 FX_DWORD IDI; |
| 83 for (;;) { | 78 for (;;) { |
| 84 if (pStream->read1Bit(&nTmp) != 0) | 79 if (pStream->read1Bit(&nTmp) != 0) |
| 85 return nullptr; | 80 return nullptr; |
| 86 | 81 |
| 87 nVal = (nVal << 1) | nTmp; | 82 nVal = (nVal << 1) | nTmp; |
| 88 nBits++; | 83 nBits++; |
| 89 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { | 84 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 108 } else { | 103 } else { |
| 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 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 |