Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: core/src/fxcodec/jbig2/JBig2_TrdProc.cpp

Issue 1800523005: Move core/src/ up to core/. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_TrdProc.h ('k') | core/src/fxcodec/lbmp/fx_bmp.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/src/fxcodec/jbig2/JBig2_TrdProc.h"
8
9 #include <memory>
10
11 #include "core/src/fxcodec/jbig2/JBig2_ArithDecoder.h"
12 #include "core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h"
13 #include "core/src/fxcodec/jbig2/JBig2_GrrdProc.h"
14 #include "core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h"
15
16 CJBig2_Image* CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream* pStream,
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(
33 new CJBig2_HuffmanDecoder(pStream));
34 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH));
35 SBREG->fill(SBDEFPIXEL);
36 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0)
37 return nullptr;
38
39 STRIPT *= SBSTRIPS;
40 STRIPT = -STRIPT;
41 FIRSTS = 0;
42 NINSTANCES = 0;
43 while (NINSTANCES < SBNUMINSTANCES) {
44 if (pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0)
45 return nullptr;
46
47 DT *= SBSTRIPS;
48 STRIPT = STRIPT + DT;
49 bFirst = TRUE;
50 for (;;) {
51 if (bFirst) {
52 if (pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0)
53 return nullptr;
54
55 FIRSTS = FIRSTS + DFS;
56 CURS = FIRSTS;
57 bFirst = FALSE;
58 } else {
59 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS);
60 if (nVal == JBIG2_OOB) {
61 break;
62 } else if (nVal != 0) {
63 return nullptr;
64 } else {
65 CURS = CURS + IDS + SBDSOFFSET;
66 }
67 }
68 uint8_t CURT = 0;
69 if (SBSTRIPS != 1) {
70 nTmp = 1;
71 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) {
72 nTmp++;
73 }
74 if (pStream->readNBits(nTmp, &nVal) != 0)
75 return nullptr;
76
77 CURT = nVal;
78 }
79 TI = STRIPT + CURT;
80 nVal = 0;
81 nBits = 0;
82 FX_DWORD IDI;
83 for (;;) {
84 if (pStream->read1Bit(&nTmp) != 0)
85 return nullptr;
86
87 nVal = (nVal << 1) | nTmp;
88 nBits++;
89 for (IDI = 0; IDI < SBNUMSYMS; IDI++) {
90 if ((nBits == SBSYMCODES[IDI].codelen) &&
91 (nVal == SBSYMCODES[IDI].code)) {
92 break;
93 }
94 }
95 if (IDI < SBNUMSYMS) {
96 break;
97 }
98 }
99 if (SBREFINE == 0) {
100 RI = 0;
101 } else {
102 if (pStream->read1Bit(&RI) != 0) {
103 return nullptr;
104 }
105 }
106 if (RI == 0) {
107 IBI = SBSYMS[IDI];
108 } else {
109 if ((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0) ||
110 (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0) ||
111 (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0) ||
112 (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0) ||
113 (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
114 return nullptr;
115 }
116 pStream->alignByte();
117 nTmp = pStream->getOffset();
118 IBOI = SBSYMS[IDI];
119 if (!IBOI)
120 return nullptr;
121
122 WOI = IBOI->m_nWidth;
123 HOI = IBOI->m_nHeight;
124 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0)
125 return nullptr;
126
127 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc());
128 pGRRD->GRW = WOI + RDWI;
129 pGRRD->GRH = HOI + RDHI;
130 pGRRD->GRTEMPLATE = SBRTEMPLATE;
131 pGRRD->GRREFERENCE = IBOI;
132 pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI;
133 pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI;
134 pGRRD->TPGRON = 0;
135 pGRRD->GRAT[0] = SBRAT[0];
136 pGRRD->GRAT[1] = SBRAT[1];
137 pGRRD->GRAT[2] = SBRAT[2];
138 pGRRD->GRAT[3] = SBRAT[3];
139
140 {
141 std::unique_ptr<CJBig2_ArithDecoder> pArithDecoder(
142 new CJBig2_ArithDecoder(pStream));
143 IBI = pGRRD->decode(pArithDecoder.get(), grContext);
144 if (!IBI)
145 return nullptr;
146 }
147
148 pStream->alignByte();
149 pStream->offset(2);
150 if ((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
151 delete IBI;
152 return nullptr;
153 }
154 }
155 if (!IBI) {
156 continue;
157 }
158 WI = IBI->m_nWidth;
159 HI = IBI->m_nHeight;
160 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) ||
161 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
162 CURS = CURS + WI - 1;
163 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) ||
164 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
165 CURS = CURS + HI - 1;
166 }
167 SI = CURS;
168 if (TRANSPOSED == 0) {
169 switch (REFCORNER) {
170 case JBIG2_CORNER_TOPLEFT:
171 SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
172 break;
173 case JBIG2_CORNER_TOPRIGHT:
174 SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
175 break;
176 case JBIG2_CORNER_BOTTOMLEFT:
177 SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
178 break;
179 case JBIG2_CORNER_BOTTOMRIGHT:
180 SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
181 break;
182 }
183 } else {
184 switch (REFCORNER) {
185 case JBIG2_CORNER_TOPLEFT:
186 SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
187 break;
188 case JBIG2_CORNER_TOPRIGHT:
189 SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
190 break;
191 case JBIG2_CORNER_BOTTOMLEFT:
192 SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
193 break;
194 case JBIG2_CORNER_BOTTOMRIGHT:
195 SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
196 break;
197 }
198 }
199 if (RI != 0) {
200 delete IBI;
201 }
202 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
203 (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
204 CURS = CURS + WI - 1;
205 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
206 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
207 CURS = CURS + HI - 1;
208 }
209 NINSTANCES = NINSTANCES + 1;
210 }
211 }
212 return SBREG.release();
213 }
214
215 CJBig2_Image* CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder,
216 JBig2ArithCtx* grContext,
217 JBig2IntDecoderState* pIDS) {
218 std::unique_ptr<CJBig2_ArithIntDecoder> IADT;
219 std::unique_ptr<CJBig2_ArithIntDecoder> IAFS;
220 std::unique_ptr<CJBig2_ArithIntDecoder> IADS;
221 std::unique_ptr<CJBig2_ArithIntDecoder> IAIT;
222 std::unique_ptr<CJBig2_ArithIntDecoder> IARI;
223 std::unique_ptr<CJBig2_ArithIntDecoder> IARDW;
224 std::unique_ptr<CJBig2_ArithIntDecoder> IARDH;
225 std::unique_ptr<CJBig2_ArithIntDecoder> IARDX;
226 std::unique_ptr<CJBig2_ArithIntDecoder> IARDY;
227 std::unique_ptr<CJBig2_ArithIaidDecoder> IAID;
228 CJBig2_ArithIntDecoder* pIADT;
229 CJBig2_ArithIntDecoder* pIAFS;
230 CJBig2_ArithIntDecoder* pIADS;
231 CJBig2_ArithIntDecoder* pIAIT;
232 CJBig2_ArithIntDecoder* pIARI;
233 CJBig2_ArithIntDecoder* pIARDW;
234 CJBig2_ArithIntDecoder* pIARDH;
235 CJBig2_ArithIntDecoder* pIARDX;
236 CJBig2_ArithIntDecoder* pIARDY;
237 CJBig2_ArithIaidDecoder* pIAID;
238 if (pIDS) {
239 pIADT = pIDS->IADT;
240 pIAFS = pIDS->IAFS;
241 pIADS = pIDS->IADS;
242 pIAIT = pIDS->IAIT;
243 pIARI = pIDS->IARI;
244 pIARDW = pIDS->IARDW;
245 pIARDH = pIDS->IARDH;
246 pIARDX = pIDS->IARDX;
247 pIARDY = pIDS->IARDY;
248 pIAID = pIDS->IAID;
249 } else {
250 IADT.reset(new CJBig2_ArithIntDecoder());
251 IAFS.reset(new CJBig2_ArithIntDecoder());
252 IADS.reset(new CJBig2_ArithIntDecoder());
253 IAIT.reset(new CJBig2_ArithIntDecoder());
254 IARI.reset(new CJBig2_ArithIntDecoder());
255 IARDW.reset(new CJBig2_ArithIntDecoder());
256 IARDH.reset(new CJBig2_ArithIntDecoder());
257 IARDX.reset(new CJBig2_ArithIntDecoder());
258 IARDY.reset(new CJBig2_ArithIntDecoder());
259 IAID.reset(new CJBig2_ArithIaidDecoder(SBSYMCODELEN));
260 pIADT = IADT.get();
261 pIAFS = IAFS.get();
262 pIADS = IADS.get();
263 pIAIT = IAIT.get();
264 pIARI = IARI.get();
265 pIARDW = IARDW.get();
266 pIARDH = IARDH.get();
267 pIARDX = IARDX.get();
268 pIARDY = IARDY.get();
269 pIAID = IAID.get();
270 }
271 std::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH));
272 SBREG->fill(SBDEFPIXEL);
273 int32_t STRIPT;
274 pIADT->decode(pArithDecoder, &STRIPT);
275 STRIPT *= SBSTRIPS;
276 STRIPT = -STRIPT;
277 int32_t FIRSTS = 0;
278 FX_DWORD NINSTANCES = 0;
279 while (NINSTANCES < SBNUMINSTANCES) {
280 int32_t CURS;
281 int32_t DT;
282 pIADT->decode(pArithDecoder, &DT);
283 DT *= SBSTRIPS;
284 STRIPT += DT;
285 bool bFirst = true;
286 for (;;) {
287 if (bFirst) {
288 int32_t DFS;
289 pIAFS->decode(pArithDecoder, &DFS);
290 FIRSTS += DFS;
291 CURS = FIRSTS;
292 bFirst = false;
293 } else {
294 int32_t IDS;
295 if (!pIADS->decode(pArithDecoder, &IDS))
296 break;
297 CURS += IDS + SBDSOFFSET;
298 }
299 if (NINSTANCES >= SBNUMINSTANCES) {
300 break;
301 }
302 int CURT = 0;
303 if (SBSTRIPS != 1)
304 pIAIT->decode(pArithDecoder, &CURT);
305
306 int32_t TI = STRIPT + CURT;
307 FX_DWORD IDI;
308 pIAID->decode(pArithDecoder, &IDI);
309 if (IDI >= SBNUMSYMS)
310 return nullptr;
311
312 int RI;
313 if (SBREFINE == 0)
314 RI = 0;
315 else
316 pIARI->decode(pArithDecoder, &RI);
317
318 std::unique_ptr<CJBig2_Image> IBI;
319 CJBig2_Image* pIBI;
320 if (RI == 0) {
321 pIBI = SBSYMS[IDI];
322 } else {
323 int32_t RDWI;
324 int32_t RDHI;
325 int32_t RDXI;
326 int32_t RDYI;
327 pIARDW->decode(pArithDecoder, &RDWI);
328 pIARDH->decode(pArithDecoder, &RDHI);
329 pIARDX->decode(pArithDecoder, &RDXI);
330 pIARDY->decode(pArithDecoder, &RDYI);
331 CJBig2_Image* IBOI = SBSYMS[IDI];
332 FX_DWORD WOI = IBOI->m_nWidth;
333 FX_DWORD HOI = IBOI->m_nHeight;
334 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
335 return nullptr;
336 }
337 std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc());
338 pGRRD->GRW = WOI + RDWI;
339 pGRRD->GRH = HOI + RDHI;
340 pGRRD->GRTEMPLATE = SBRTEMPLATE;
341 pGRRD->GRREFERENCE = IBOI;
342 pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI;
343 pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI;
344 pGRRD->TPGRON = 0;
345 pGRRD->GRAT[0] = SBRAT[0];
346 pGRRD->GRAT[1] = SBRAT[1];
347 pGRRD->GRAT[2] = SBRAT[2];
348 pGRRD->GRAT[3] = SBRAT[3];
349 IBI.reset(pGRRD->decode(pArithDecoder, grContext));
350 pIBI = IBI.get();
351 }
352 if (!pIBI)
353 return nullptr;
354
355 FX_DWORD WI = pIBI->m_nWidth;
356 FX_DWORD HI = pIBI->m_nHeight;
357 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT) ||
358 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
359 CURS += WI - 1;
360 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT) ||
361 (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
362 CURS += HI - 1;
363 }
364 int32_t SI = CURS;
365 if (TRANSPOSED == 0) {
366 switch (REFCORNER) {
367 case JBIG2_CORNER_TOPLEFT:
368 SBREG->composeFrom(SI, TI, pIBI, SBCOMBOP);
369 break;
370 case JBIG2_CORNER_TOPRIGHT:
371 SBREG->composeFrom(SI - WI + 1, TI, pIBI, SBCOMBOP);
372 break;
373 case JBIG2_CORNER_BOTTOMLEFT:
374 SBREG->composeFrom(SI, TI - HI + 1, pIBI, SBCOMBOP);
375 break;
376 case JBIG2_CORNER_BOTTOMRIGHT:
377 SBREG->composeFrom(SI - WI + 1, TI - HI + 1, pIBI, SBCOMBOP);
378 break;
379 }
380 } else {
381 switch (REFCORNER) {
382 case JBIG2_CORNER_TOPLEFT:
383 SBREG->composeFrom(TI, SI, pIBI, SBCOMBOP);
384 break;
385 case JBIG2_CORNER_TOPRIGHT:
386 SBREG->composeFrom(TI - WI + 1, SI, pIBI, SBCOMBOP);
387 break;
388 case JBIG2_CORNER_BOTTOMLEFT:
389 SBREG->composeFrom(TI, SI - HI + 1, pIBI, SBCOMBOP);
390 break;
391 case JBIG2_CORNER_BOTTOMRIGHT:
392 SBREG->composeFrom(TI - WI + 1, SI - HI + 1, pIBI, SBCOMBOP);
393 break;
394 }
395 }
396 if (TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
397 (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
398 CURS += WI - 1;
399 } else if (TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT) ||
400 (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
401 CURS += HI - 1;
402 }
403 ++NINSTANCES;
404 }
405 }
406 return SBREG.release();
407 }
OLDNEW
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_TrdProc.h ('k') | core/src/fxcodec/lbmp/fx_bmp.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698