OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "core/fxcodec/codec/codec_int.h" | 10 #include "core/fxcodec/codec/codec_int.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 for (int i = startpos % 8; i < 8; ++i) | 119 for (int i = startpos % 8; i < 8; ++i) |
120 dest_buf[first_byte] -= 1 << (7 - i); | 120 dest_buf[first_byte] -= 1 << (7 - i); |
121 for (int i = 0; i <= (endpos - 1) % 8; ++i) | 121 for (int i = 0; i <= (endpos - 1) % 8; ++i) |
122 dest_buf[last_byte] -= 1 << (7 - i); | 122 dest_buf[last_byte] -= 1 << (7 - i); |
123 | 123 |
124 if (last_byte > first_byte + 1) | 124 if (last_byte > first_byte + 1) |
125 FXSYS_memset(dest_buf + first_byte + 1, 0, last_byte - first_byte - 1); | 125 FXSYS_memset(dest_buf + first_byte + 1, 0, last_byte - first_byte - 1); |
126 } | 126 } |
127 | 127 |
128 #define NEXTBIT() \ | 128 inline bool NextBit(const uint8_t* src_buf, int* bitpos) { |
129 src_buf[*bitpos / 8] & (1 << (7 - *bitpos % 8)); \ | 129 int pos = (*bitpos)++; |
130 ++(*bitpos); | 130 return !!(src_buf[pos / 8] & (1 << (7 - pos % 8))); |
| 131 } |
131 | 132 |
132 const uint8_t FaxBlackRunIns[] = { | 133 const uint8_t FaxBlackRunIns[] = { |
133 0, 2, 0x02, 3, 0, 0x03, | 134 0, 2, 0x02, 3, 0, 0x03, |
134 2, 0, 2, 0x02, 1, 0, | 135 2, 0, 2, 0x02, 1, 0, |
135 0x03, 4, 0, 2, 0x02, 6, | 136 0x03, 4, 0, 2, 0x02, 6, |
136 0, 0x03, 5, 0, 1, 0x03, | 137 0, 0x03, 5, 0, 1, 0x03, |
137 7, 0, 2, 0x04, 9, 0, | 138 7, 0, 2, 0x04, 9, 0, |
138 0x05, 8, 0, 3, 0x04, 10, | 139 0x05, 8, 0, 3, 0x04, 10, |
139 0, 0x05, 11, 0, 0x07, 12, | 140 0, 0x05, 11, 0, 0x07, 12, |
140 0, 2, 0x04, 13, 0, 0x07, | 141 0, 2, 0x04, 13, 0, 0x07, |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 bool a0color = true; | 283 bool a0color = true; |
283 while (1) { | 284 while (1) { |
284 if (*bitpos >= bitsize) | 285 if (*bitpos >= bitsize) |
285 return FALSE; | 286 return FALSE; |
286 | 287 |
287 int a1; | 288 int a1; |
288 int a2; | 289 int a2; |
289 int b1; | 290 int b1; |
290 int b2; | 291 int b2; |
291 FaxG4FindB1B2(ref_buf, columns, a0, a0color, &b1, &b2); | 292 FaxG4FindB1B2(ref_buf, columns, a0, a0color, &b1, &b2); |
292 FX_BOOL bit = NEXTBIT(); | 293 |
293 int v_delta = 0; | 294 int v_delta = 0; |
294 if (!bit) { | 295 if (!NextBit(src_buf, bitpos)) { |
295 if (*bitpos >= bitsize) | 296 if (*bitpos >= bitsize) |
296 return FALSE; | 297 return FALSE; |
297 | 298 |
298 FX_BOOL bit1 = NEXTBIT(); | 299 FX_BOOL bit1 = NextBit(src_buf, bitpos); |
299 if (*bitpos >= bitsize) | 300 if (*bitpos >= bitsize) |
300 return FALSE; | 301 return FALSE; |
301 | 302 |
302 FX_BOOL bit2 = NEXTBIT(); | 303 FX_BOOL bit2 = NextBit(src_buf, bitpos); |
303 if (bit1) { | 304 if (bit1) { |
304 v_delta = bit2 ? 1 : -1; | 305 v_delta = bit2 ? 1 : -1; |
305 } else if (bit2) { | 306 } else if (bit2) { |
306 int run_len1 = 0; | 307 int run_len1 = 0; |
307 while (1) { | 308 while (1) { |
308 int run = FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, | 309 int run = FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, |
309 src_buf, bitpos, bitsize); | 310 src_buf, bitpos, bitsize); |
310 run_len1 += run; | 311 run_len1 += run; |
311 if (run < 64) { | 312 if (run < 64) { |
312 break; | 313 break; |
(...skipping 21 matching lines...) Expand all Loading... |
334 | 335 |
335 a0 = a2; | 336 a0 = a2; |
336 if (a0 < columns) | 337 if (a0 < columns) |
337 continue; | 338 continue; |
338 | 339 |
339 return TRUE; | 340 return TRUE; |
340 } else { | 341 } else { |
341 if (*bitpos >= bitsize) | 342 if (*bitpos >= bitsize) |
342 return FALSE; | 343 return FALSE; |
343 | 344 |
344 bit = NEXTBIT(); | 345 if (NextBit(src_buf, bitpos)) { |
345 if (bit) { | |
346 if (!a0color) | 346 if (!a0color) |
347 FaxFillBits(dest_buf, columns, a0, b2); | 347 FaxFillBits(dest_buf, columns, a0, b2); |
348 | 348 |
349 if (b2 >= columns) | 349 if (b2 >= columns) |
350 return TRUE; | 350 return TRUE; |
351 | 351 |
352 a0 = b2; | 352 a0 = b2; |
353 continue; | 353 continue; |
354 } | 354 } |
355 | 355 |
356 if (*bitpos >= bitsize) | 356 if (*bitpos >= bitsize) |
357 return FALSE; | 357 return FALSE; |
358 | 358 |
359 FX_BOOL next_bit1 = NEXTBIT(); | 359 FX_BOOL next_bit1 = NextBit(src_buf, bitpos); |
360 if (*bitpos >= bitsize) | 360 if (*bitpos >= bitsize) |
361 return FALSE; | 361 return FALSE; |
362 | 362 |
363 FX_BOOL next_bit2 = NEXTBIT(); | 363 FX_BOOL next_bit2 = NextBit(src_buf, bitpos); |
364 if (next_bit1) { | 364 if (next_bit1) { |
365 v_delta = next_bit2 ? 2 : -2; | 365 v_delta = next_bit2 ? 2 : -2; |
366 } else if (next_bit2) { | 366 } else if (next_bit2) { |
367 if (*bitpos >= bitsize) | 367 if (*bitpos >= bitsize) |
368 return FALSE; | 368 return FALSE; |
369 | 369 |
370 bit = NEXTBIT(); | 370 v_delta = NextBit(src_buf, bitpos) ? 3 : -3; |
371 v_delta = bit ? 3 : -3; | |
372 } else { | 371 } else { |
373 if (*bitpos >= bitsize) | 372 if (*bitpos >= bitsize) |
374 return FALSE; | 373 return FALSE; |
375 | 374 |
376 bit = NEXTBIT(); | 375 if (NextBit(src_buf, bitpos)) { |
377 if (bit) { | |
378 *bitpos += 3; | 376 *bitpos += 3; |
379 continue; | 377 continue; |
380 } | 378 } |
381 *bitpos += 5; | 379 *bitpos += 5; |
382 return TRUE; | 380 return TRUE; |
383 } | 381 } |
384 } | 382 } |
385 } | 383 } |
386 a1 = b1 + v_delta; | 384 a1 = b1 + v_delta; |
387 if (!a0color) | 385 if (!a0color) |
388 FaxFillBits(dest_buf, columns, a0, a1); | 386 FaxFillBits(dest_buf, columns, a0, a1); |
389 | 387 |
390 if (a1 >= columns) | 388 if (a1 >= columns) |
391 return TRUE; | 389 return TRUE; |
392 | 390 |
393 // The position of picture element must be monotonic increasing. | 391 // The position of picture element must be monotonic increasing. |
394 if (a0 >= a1) | 392 if (a0 >= a1) |
395 return FALSE; | 393 return FALSE; |
396 | 394 |
397 a0 = a1; | 395 a0 = a1; |
398 a0color = !a0color; | 396 a0color = !a0color; |
399 } | 397 } |
400 } | 398 } |
401 | 399 |
402 FX_BOOL FaxSkipEOL(const uint8_t* src_buf, int bitsize, int* bitpos) { | 400 FX_BOOL FaxSkipEOL(const uint8_t* src_buf, int bitsize, int* bitpos) { |
403 int startbit = *bitpos; | 401 int startbit = *bitpos; |
404 while (*bitpos < bitsize) { | 402 while (*bitpos < bitsize) { |
405 int bit = NEXTBIT(); | 403 if (!NextBit(src_buf, bitpos)) |
406 if (!bit) | |
407 continue; | 404 continue; |
408 | |
409 if (*bitpos - startbit <= 11) | 405 if (*bitpos - startbit <= 11) |
410 *bitpos = startbit; | 406 *bitpos = startbit; |
411 return TRUE; | 407 return TRUE; |
412 } | 408 } |
413 return FALSE; | 409 return FALSE; |
414 } | 410 } |
415 | 411 |
416 FX_BOOL FaxGet1DLine(const uint8_t* src_buf, | 412 FX_BOOL FaxGet1DLine(const uint8_t* src_buf, |
417 int bitsize, | 413 int bitsize, |
418 int* bitpos, | 414 int* bitpos, |
419 std::vector<uint8_t>* dest_buf, | 415 std::vector<uint8_t>* dest_buf, |
420 int columns) { | 416 int columns) { |
421 bool color = true; | 417 bool color = true; |
422 int startpos = 0; | 418 int startpos = 0; |
423 while (1) { | 419 while (1) { |
424 if (*bitpos >= bitsize) | 420 if (*bitpos >= bitsize) |
425 return FALSE; | 421 return FALSE; |
426 | 422 |
427 int run_len = 0; | 423 int run_len = 0; |
428 while (1) { | 424 while (1) { |
429 int run = FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, | 425 int run = FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, |
430 bitpos, bitsize); | 426 bitpos, bitsize); |
431 if (run < 0) { | 427 if (run < 0) { |
432 while (*bitpos < bitsize) { | 428 while (*bitpos < bitsize) { |
433 int bit = NEXTBIT(); | 429 if (NextBit(src_buf, bitpos)) |
434 if (bit) { | |
435 return TRUE; | 430 return TRUE; |
436 } | |
437 } | 431 } |
438 return FALSE; | 432 return FALSE; |
439 } | 433 } |
440 run_len += run; | 434 run_len += run; |
441 if (run < 64) { | 435 if (run < 64) { |
442 break; | 436 break; |
443 } | 437 } |
444 } | 438 } |
445 if (!color) | 439 if (!color) |
446 FaxFillBits(dest_buf->data(), columns, startpos, startpos + run_len); | 440 FaxFillBits(dest_buf->data(), columns, startpos, startpos + run_len); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 return nullptr; | 600 return nullptr; |
607 | 601 |
608 // Reject unreasonable large input. | 602 // Reject unreasonable large input. |
609 if (actual_width > kMaxImageDimension || actual_height > kMaxImageDimension) | 603 if (actual_width > kMaxImageDimension || actual_height > kMaxImageDimension) |
610 return nullptr; | 604 return nullptr; |
611 | 605 |
612 uint32_t pitch = (static_cast<uint32_t>(actual_width) + 31) / 32 * 4; | 606 uint32_t pitch = (static_cast<uint32_t>(actual_width) + 31) / 32 * 4; |
613 return new CCodec_FaxDecoder(src_buf, src_size, actual_width, actual_height, | 607 return new CCodec_FaxDecoder(src_buf, src_size, actual_width, actual_height, |
614 pitch, K, EndOfLine, EncodedByteAlign, BlackIs1); | 608 pitch, K, EndOfLine, EncodedByteAlign, BlackIs1); |
615 } | 609 } |
OLD | NEW |