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 std::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder( | 18 std::unique_ptr<CJBig2_HuffmanDecoder> pHuffmanDecoder( |
19 new CJBig2_HuffmanDecoder(pStream)); | 19 new CJBig2_HuffmanDecoder(pStream)); |
20 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); | 20 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); |
21 SBREG->fill(SBDEFPIXEL); | 21 SBREG->fill(SBDEFPIXEL); |
22 int32_t STRIPT; | 22 int32_t STRIPT; |
23 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) | 23 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) |
24 return nullptr; | 24 return nullptr; |
25 | 25 |
26 STRIPT *= SBSTRIPS; | 26 STRIPT *= SBSTRIPS; |
27 STRIPT = -STRIPT; | 27 STRIPT = -STRIPT; |
28 FX_DWORD NINSTANCES = 0; | 28 uint32_t NINSTANCES = 0; |
29 while (NINSTANCES < SBNUMINSTANCES) { | 29 while (NINSTANCES < SBNUMINSTANCES) { |
30 int32_t DT; | 30 int32_t DT; |
31 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) | 31 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) |
32 return nullptr; | 32 return nullptr; |
33 | 33 |
34 DT *= SBSTRIPS; | 34 DT *= SBSTRIPS; |
35 STRIPT = STRIPT + DT; | 35 STRIPT = STRIPT + DT; |
36 bool bFirst = true; | 36 bool bFirst = true; |
37 int32_t FIRSTS = 0; | 37 int32_t FIRSTS = 0; |
38 int32_t CURS = 0; | 38 int32_t CURS = 0; |
(...skipping 12 matching lines...) Expand all Loading... |
51 if (nVal == JBIG2_OOB) { | 51 if (nVal == JBIG2_OOB) { |
52 break; | 52 break; |
53 } else if (nVal != 0) { | 53 } else if (nVal != 0) { |
54 return nullptr; | 54 return nullptr; |
55 } else { | 55 } else { |
56 CURS = CURS + IDS + SBDSOFFSET; | 56 CURS = CURS + IDS + SBDSOFFSET; |
57 } | 57 } |
58 } | 58 } |
59 uint8_t CURT = 0; | 59 uint8_t CURT = 0; |
60 if (SBSTRIPS != 1) { | 60 if (SBSTRIPS != 1) { |
61 FX_DWORD nTmp = 1; | 61 uint32_t nTmp = 1; |
62 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { | 62 while ((uint32_t)(1 << nTmp) < SBSTRIPS) { |
63 nTmp++; | 63 nTmp++; |
64 } | 64 } |
65 int32_t nVal; | 65 int32_t nVal; |
66 if (pStream->readNBits(nTmp, &nVal) != 0) | 66 if (pStream->readNBits(nTmp, &nVal) != 0) |
67 return nullptr; | 67 return nullptr; |
68 | 68 |
69 CURT = nVal; | 69 CURT = nVal; |
70 } | 70 } |
71 int32_t TI = STRIPT + CURT; | 71 int32_t TI = STRIPT + CURT; |
72 int32_t nVal = 0; | 72 int32_t nVal = 0; |
73 int32_t nBits = 0; | 73 int32_t nBits = 0; |
74 FX_DWORD IDI; | 74 uint32_t IDI; |
75 for (;;) { | 75 for (;;) { |
76 FX_DWORD nTmp; | 76 uint32_t nTmp; |
77 if (pStream->read1Bit(&nTmp) != 0) | 77 if (pStream->read1Bit(&nTmp) != 0) |
78 return nullptr; | 78 return nullptr; |
79 | 79 |
80 nVal = (nVal << 1) | nTmp; | 80 nVal = (nVal << 1) | nTmp; |
81 nBits++; | 81 nBits++; |
82 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { | 82 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { |
83 if ((nBits == SBSYMCODES[IDI].codelen) && | 83 if ((nBits == SBSYMCODES[IDI].codelen) && |
84 (nVal == SBSYMCODES[IDI].code)) { | 84 (nVal == SBSYMCODES[IDI].code)) { |
85 break; | 85 break; |
86 } | 86 } |
(...skipping 15 matching lines...) Expand all Loading... |
102 int32_t RDXI; | 102 int32_t RDXI; |
103 int32_t RDYI; | 103 int32_t RDYI; |
104 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0) || | 104 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0) || |
105 (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0) || | 105 (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0) || |
106 (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0) || | 106 (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0) || |
107 (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0) || | 107 (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0) || |
108 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) { | 108 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) { |
109 return nullptr; | 109 return nullptr; |
110 } | 110 } |
111 pStream->alignByte(); | 111 pStream->alignByte(); |
112 FX_DWORD nTmp = pStream->getOffset(); | 112 uint32_t nTmp = pStream->getOffset(); |
113 CJBig2_Image* IBOI = SBSYMS[IDI]; | 113 CJBig2_Image* IBOI = SBSYMS[IDI]; |
114 if (!IBOI) | 114 if (!IBOI) |
115 return nullptr; | 115 return nullptr; |
116 | 116 |
117 FX_DWORD WOI = IBOI->m_nWidth; | 117 uint32_t WOI = IBOI->m_nWidth; |
118 FX_DWORD HOI = IBOI->m_nHeight; | 118 uint32_t HOI = IBOI->m_nHeight; |
119 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) | 119 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) |
120 return nullptr; | 120 return nullptr; |
121 | 121 |
122 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); | 122 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); |
123 pGRRD->GRW = WOI + RDWI; | 123 pGRRD->GRW = WOI + RDWI; |
124 pGRRD->GRH = HOI + RDHI; | 124 pGRRD->GRH = HOI + RDHI; |
125 pGRRD->GRTEMPLATE = SBRTEMPLATE; | 125 pGRRD->GRTEMPLATE = SBRTEMPLATE; |
126 pGRRD->GRREFERENCE = IBOI; | 126 pGRRD->GRREFERENCE = IBOI; |
127 pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI; | 127 pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI; |
128 pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI; | 128 pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI; |
129 pGRRD->TPGRON = 0; | 129 pGRRD->TPGRON = 0; |
130 pGRRD->GRAT[0] = SBRAT[0]; | 130 pGRRD->GRAT[0] = SBRAT[0]; |
131 pGRRD->GRAT[1] = SBRAT[1]; | 131 pGRRD->GRAT[1] = SBRAT[1]; |
132 pGRRD->GRAT[2] = SBRAT[2]; | 132 pGRRD->GRAT[2] = SBRAT[2]; |
133 pGRRD->GRAT[3] = SBRAT[3]; | 133 pGRRD->GRAT[3] = SBRAT[3]; |
134 | 134 |
135 { | 135 { |
136 std::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( | 136 std::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( |
137 new CJBig2_ArithDecoder(pStream)); | 137 new CJBig2_ArithDecoder(pStream)); |
138 IBI = pGRRD->decode(pArithDecoder.get(), grContext); | 138 IBI = pGRRD->decode(pArithDecoder.get(), grContext); |
139 if (!IBI) | 139 if (!IBI) |
140 return nullptr; | 140 return nullptr; |
141 } | 141 } |
142 | 142 |
143 pStream->alignByte(); | 143 pStream->alignByte(); |
144 pStream->offset(2); | 144 pStream->offset(2); |
145 if ((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) { | 145 if ((uint32_t)nVal != (pStream->getOffset() - nTmp)) { |
146 delete IBI; | 146 delete IBI; |
147 return nullptr; | 147 return nullptr; |
148 } | 148 } |
149 } | 149 } |
150 if (!IBI) { | 150 if (!IBI) { |
151 continue; | 151 continue; |
152 } | 152 } |
153 FX_DWORD WI = IBI->m_nWidth; | 153 uint32_t WI = IBI->m_nWidth; |
154 FX_DWORD HI = IBI->m_nHeight; | 154 uint32_t HI = IBI->m_nHeight; |
155 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) || | 155 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) || |
156 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { | 156 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { |
157 CURS = CURS + WI - 1; | 157 CURS = CURS + WI - 1; |
158 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) || | 158 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) || |
159 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { | 159 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { |
160 CURS = CURS + HI - 1; | 160 CURS = CURS + HI - 1; |
161 } | 161 } |
162 int32_t SI = CURS; | 162 int32_t SI = CURS; |
163 if (TRANSPOSED == 0) { | 163 if (TRANSPOSED == 0) { |
164 switch (REFCORNER) { | 164 switch (REFCORNER) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 pIARDY = IARDY.get(); | 263 pIARDY = IARDY.get(); |
264 pIAID = IAID.get(); | 264 pIAID = IAID.get(); |
265 } | 265 } |
266 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); | 266 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); |
267 SBREG->fill(SBDEFPIXEL); | 267 SBREG->fill(SBDEFPIXEL); |
268 int32_t STRIPT; | 268 int32_t STRIPT; |
269 pIADT->decode(pArithDecoder, &STRIPT); | 269 pIADT->decode(pArithDecoder, &STRIPT); |
270 STRIPT *= SBSTRIPS; | 270 STRIPT *= SBSTRIPS; |
271 STRIPT = -STRIPT; | 271 STRIPT = -STRIPT; |
272 int32_t FIRSTS = 0; | 272 int32_t FIRSTS = 0; |
273 FX_DWORD NINSTANCES = 0; | 273 uint32_t NINSTANCES = 0; |
274 while (NINSTANCES < SBNUMINSTANCES) { | 274 while (NINSTANCES < SBNUMINSTANCES) { |
275 int32_t CURS = 0; | 275 int32_t CURS = 0; |
276 int32_t DT; | 276 int32_t DT; |
277 pIADT->decode(pArithDecoder, &DT); | 277 pIADT->decode(pArithDecoder, &DT); |
278 DT *= SBSTRIPS; | 278 DT *= SBSTRIPS; |
279 STRIPT += DT; | 279 STRIPT += DT; |
280 bool bFirst = true; | 280 bool bFirst = true; |
281 for (;;) { | 281 for (;;) { |
282 if (bFirst) { | 282 if (bFirst) { |
283 int32_t DFS; | 283 int32_t DFS; |
284 pIAFS->decode(pArithDecoder, &DFS); | 284 pIAFS->decode(pArithDecoder, &DFS); |
285 FIRSTS += DFS; | 285 FIRSTS += DFS; |
286 CURS = FIRSTS; | 286 CURS = FIRSTS; |
287 bFirst = false; | 287 bFirst = false; |
288 } else { | 288 } else { |
289 int32_t IDS; | 289 int32_t IDS; |
290 if (!pIADS->decode(pArithDecoder, &IDS)) | 290 if (!pIADS->decode(pArithDecoder, &IDS)) |
291 break; | 291 break; |
292 CURS += IDS + SBDSOFFSET; | 292 CURS += IDS + SBDSOFFSET; |
293 } | 293 } |
294 if (NINSTANCES >= SBNUMINSTANCES) { | 294 if (NINSTANCES >= SBNUMINSTANCES) { |
295 break; | 295 break; |
296 } | 296 } |
297 int CURT = 0; | 297 int CURT = 0; |
298 if (SBSTRIPS != 1) | 298 if (SBSTRIPS != 1) |
299 pIAIT->decode(pArithDecoder, &CURT); | 299 pIAIT->decode(pArithDecoder, &CURT); |
300 | 300 |
301 int32_t TI = STRIPT + CURT; | 301 int32_t TI = STRIPT + CURT; |
302 FX_DWORD IDI; | 302 uint32_t IDI; |
303 pIAID->decode(pArithDecoder, &IDI); | 303 pIAID->decode(pArithDecoder, &IDI); |
304 if (IDI >= SBNUMSYMS) | 304 if (IDI >= SBNUMSYMS) |
305 return nullptr; | 305 return nullptr; |
306 | 306 |
307 int RI; | 307 int RI; |
308 if (SBREFINE == 0) | 308 if (SBREFINE == 0) |
309 RI = 0; | 309 RI = 0; |
310 else | 310 else |
311 pIARI->decode(pArithDecoder, &RI); | 311 pIARI->decode(pArithDecoder, &RI); |
312 | 312 |
313 std::unique_ptr<CJBig2_Image> IBI; | 313 std::unique_ptr<CJBig2_Image> IBI; |
314 CJBig2_Image* pIBI; | 314 CJBig2_Image* pIBI; |
315 if (RI == 0) { | 315 if (RI == 0) { |
316 pIBI = SBSYMS[IDI]; | 316 pIBI = SBSYMS[IDI]; |
317 } else { | 317 } else { |
318 int32_t RDWI; | 318 int32_t RDWI; |
319 int32_t RDHI; | 319 int32_t RDHI; |
320 int32_t RDXI; | 320 int32_t RDXI; |
321 int32_t RDYI; | 321 int32_t RDYI; |
322 pIARDW->decode(pArithDecoder, &RDWI); | 322 pIARDW->decode(pArithDecoder, &RDWI); |
323 pIARDH->decode(pArithDecoder, &RDHI); | 323 pIARDH->decode(pArithDecoder, &RDHI); |
324 pIARDX->decode(pArithDecoder, &RDXI); | 324 pIARDX->decode(pArithDecoder, &RDXI); |
325 pIARDY->decode(pArithDecoder, &RDYI); | 325 pIARDY->decode(pArithDecoder, &RDYI); |
326 CJBig2_Image* IBOI = SBSYMS[IDI]; | 326 CJBig2_Image* IBOI = SBSYMS[IDI]; |
327 FX_DWORD WOI = IBOI->m_nWidth; | 327 uint32_t WOI = IBOI->m_nWidth; |
328 FX_DWORD HOI = IBOI->m_nHeight; | 328 uint32_t HOI = IBOI->m_nHeight; |
329 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) { | 329 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) { |
330 return nullptr; | 330 return nullptr; |
331 } | 331 } |
332 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); | 332 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); |
333 pGRRD->GRW = WOI + RDWI; | 333 pGRRD->GRW = WOI + RDWI; |
334 pGRRD->GRH = HOI + RDHI; | 334 pGRRD->GRH = HOI + RDHI; |
335 pGRRD->GRTEMPLATE = SBRTEMPLATE; | 335 pGRRD->GRTEMPLATE = SBRTEMPLATE; |
336 pGRRD->GRREFERENCE = IBOI; | 336 pGRRD->GRREFERENCE = IBOI; |
337 pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI; | 337 pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI; |
338 pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI; | 338 pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI; |
339 pGRRD->TPGRON = 0; | 339 pGRRD->TPGRON = 0; |
340 pGRRD->GRAT[0] = SBRAT[0]; | 340 pGRRD->GRAT[0] = SBRAT[0]; |
341 pGRRD->GRAT[1] = SBRAT[1]; | 341 pGRRD->GRAT[1] = SBRAT[1]; |
342 pGRRD->GRAT[2] = SBRAT[2]; | 342 pGRRD->GRAT[2] = SBRAT[2]; |
343 pGRRD->GRAT[3] = SBRAT[3]; | 343 pGRRD->GRAT[3] = SBRAT[3]; |
344 IBI.reset(pGRRD->decode(pArithDecoder, grContext)); | 344 IBI.reset(pGRRD->decode(pArithDecoder, grContext)); |
345 pIBI = IBI.get(); | 345 pIBI = IBI.get(); |
346 } | 346 } |
347 if (!pIBI) | 347 if (!pIBI) |
348 return nullptr; | 348 return nullptr; |
349 | 349 |
350 FX_DWORD WI = pIBI->m_nWidth; | 350 uint32_t WI = pIBI->m_nWidth; |
351 FX_DWORD HI = pIBI->m_nHeight; | 351 uint32_t HI = pIBI->m_nHeight; |
352 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) || | 352 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) || |
353 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { | 353 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { |
354 CURS += WI - 1; | 354 CURS += WI - 1; |
355 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) || | 355 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) || |
356 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { | 356 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) { |
357 CURS += HI - 1; | 357 CURS += HI - 1; |
358 } | 358 } |
359 int32_t SI = CURS; | 359 int32_t SI = CURS; |
360 if (TRANSPOSED == 0) { | 360 if (TRANSPOSED == 0) { |
361 switch (REFCORNER) { | 361 switch (REFCORNER) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 CURS += WI - 1; | 393 CURS += WI - 1; |
394 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) || | 394 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) || |
395 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) { | 395 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) { |
396 CURS += HI - 1; | 396 CURS += HI - 1; |
397 } | 397 } |
398 ++NINSTANCES; | 398 ++NINSTANCES; |
399 } | 399 } |
400 } | 400 } |
401 return SBREG.release(); | 401 return SBREG.release(); |
402 } | 402 } |
OLD | NEW |