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 "../../../include/fxcodec/fx_codec.h" | 7 #include "../../../include/fxcodec/fx_codec.h" |
8 #include "codec_int.h" | 8 #include "codec_int.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 bitpos++; | 274 bitpos++; |
275 int next_off = ins_off + ins * 3; | 275 int next_off = ins_off + ins * 3; |
276 for (; ins_off < next_off; ins_off += 3) { | 276 for (; ins_off < next_off; ins_off += 3) { |
277 if (ins_array[ins_off] == code) { | 277 if (ins_array[ins_off] == code) { |
278 return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256; | 278 return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256; |
279 } | 279 } |
280 } | 280 } |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
| 284 FX_BOOL FaxG4GetRow(const uint8_t* src_buf, |
| 285 int bitsize, |
| 286 int& bitpos, |
| 287 uint8_t* dest_buf, |
| 288 const uint8_t* ref_buf, |
| 289 int columns) { |
| 290 int a0 = -1; |
| 291 bool a0color = true; |
| 292 while (1) { |
| 293 if (bitpos >= bitsize) { |
| 294 return FALSE; |
| 295 } |
| 296 int a1, a2, b1, b2; |
| 297 FaxG4FindB1B2(ref_buf, columns, a0, a0color, b1, b2); |
| 298 FX_BOOL bit = NEXTBIT; |
| 299 int v_delta = 0; |
| 300 if (bit) { |
| 301 } else { |
| 302 if (bitpos >= bitsize) { |
| 303 return FALSE; |
| 304 } |
| 305 FX_BOOL bit1 = NEXTBIT; |
| 306 if (bitpos >= bitsize) { |
| 307 return FALSE; |
| 308 } |
| 309 FX_BOOL bit2 = NEXTBIT; |
| 310 if (bit1 && bit2) { |
| 311 v_delta = 1; |
| 312 } else if (bit1) { |
| 313 v_delta = -1; |
| 314 } else if (bit2) { |
| 315 int run_len1 = 0; |
| 316 while (1) { |
| 317 int run = FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, |
| 318 src_buf, bitpos, bitsize); |
| 319 run_len1 += run; |
| 320 if (run < 64) { |
| 321 break; |
| 322 } |
| 323 } |
| 324 if (a0 < 0) { |
| 325 run_len1++; |
| 326 } |
| 327 a1 = a0 + run_len1; |
| 328 if (!a0color) { |
| 329 FaxFillBits(dest_buf, columns, a0, a1); |
| 330 } |
| 331 int run_len2 = 0; |
| 332 while (1) { |
| 333 int run = FaxGetRun(a0color ? FaxBlackRunIns : FaxWhiteRunIns, |
| 334 src_buf, bitpos, bitsize); |
| 335 run_len2 += run; |
| 336 if (run < 64) { |
| 337 break; |
| 338 } |
| 339 } |
| 340 a2 = a1 + run_len2; |
| 341 if (a0color) { |
| 342 FaxFillBits(dest_buf, columns, a1, a2); |
| 343 } |
| 344 a0 = a2; |
| 345 if (a0 < columns) { |
| 346 continue; |
| 347 } |
| 348 return TRUE; |
| 349 } else { |
| 350 if (bitpos >= bitsize) { |
| 351 return FALSE; |
| 352 } |
| 353 bit = NEXTBIT; |
| 354 if (bit) { |
| 355 if (!a0color) { |
| 356 FaxFillBits(dest_buf, columns, a0, b2); |
| 357 } |
| 358 if (b2 >= columns) { |
| 359 return TRUE; |
| 360 } |
| 361 a0 = b2; |
| 362 continue; |
| 363 } else { |
| 364 if (bitpos >= bitsize) { |
| 365 return FALSE; |
| 366 } |
| 367 FX_BOOL bit1 = NEXTBIT; |
| 368 if (bitpos >= bitsize) { |
| 369 return FALSE; |
| 370 } |
| 371 FX_BOOL bit2 = NEXTBIT; |
| 372 if (bit1 && bit2) { |
| 373 v_delta = 2; |
| 374 } else if (bit1) { |
| 375 v_delta = -2; |
| 376 } else if (bit2) { |
| 377 if (bitpos >= bitsize) { |
| 378 return FALSE; |
| 379 } |
| 380 bit = NEXTBIT; |
| 381 if (bit) { |
| 382 v_delta = 3; |
| 383 } else { |
| 384 v_delta = -3; |
| 385 } |
| 386 } else { |
| 387 if (bitpos >= bitsize) { |
| 388 return FALSE; |
| 389 } |
| 390 bit = NEXTBIT; |
| 391 if (bit) { |
| 392 bitpos += 3; |
| 393 continue; |
| 394 } else { |
| 395 bitpos += 5; |
| 396 return TRUE; |
| 397 } |
| 398 } |
| 399 } |
| 400 } |
| 401 } |
| 402 a1 = b1 + v_delta; |
| 403 if (!a0color) { |
| 404 FaxFillBits(dest_buf, columns, a0, a1); |
| 405 } |
| 406 if (a1 >= columns) { |
| 407 return TRUE; |
| 408 } |
| 409 a0 = a1; |
| 410 a0color = !a0color; |
| 411 } |
| 412 } |
| 413 |
| 414 FX_BOOL FaxSkipEOL(const uint8_t* src_buf, int bitsize, int& bitpos) { |
| 415 int startbit = bitpos; |
| 416 while (bitpos < bitsize) { |
| 417 int bit = NEXTBIT; |
| 418 if (bit) { |
| 419 if (bitpos - startbit <= 11) { |
| 420 bitpos = startbit; |
| 421 } |
| 422 return TRUE; |
| 423 } |
| 424 } |
| 425 return FALSE; |
| 426 } |
| 427 |
| 428 FX_BOOL FaxGet1DLine(const uint8_t* src_buf, |
| 429 int bitsize, |
| 430 int& bitpos, |
| 431 uint8_t* dest_buf, |
| 432 int columns) { |
| 433 bool color = true; |
| 434 int startpos = 0; |
| 435 while (1) { |
| 436 if (bitpos >= bitsize) { |
| 437 return FALSE; |
| 438 } |
| 439 int run_len = 0; |
| 440 while (1) { |
| 441 int run = FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, |
| 442 bitpos, bitsize); |
| 443 if (run < 0) { |
| 444 while (bitpos < bitsize) { |
| 445 int bit = NEXTBIT; |
| 446 if (bit) { |
| 447 return TRUE; |
| 448 } |
| 449 } |
| 450 return FALSE; |
| 451 } |
| 452 run_len += run; |
| 453 if (run < 64) { |
| 454 break; |
| 455 } |
| 456 } |
| 457 if (!color) { |
| 458 FaxFillBits(dest_buf, columns, startpos, startpos + run_len); |
| 459 } |
| 460 startpos += run_len; |
| 461 if (startpos >= columns) { |
| 462 break; |
| 463 } |
| 464 color = !color; |
| 465 } |
| 466 return TRUE; |
| 467 } |
| 468 |
284 const uint8_t BlackRunTerminator[128] = { | 469 const uint8_t BlackRunTerminator[128] = { |
285 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, | 470 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, |
286 0x03, 5, 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, | 471 0x03, 5, 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, |
287 0x07, 8, 0x18, 9, 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, | 472 0x07, 8, 0x18, 9, 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, |
288 0x6c, 11, 0x37, 11, 0x28, 11, 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, | 473 0x6c, 11, 0x37, 11, 0x28, 11, 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, |
289 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12, 0x6a, 12, 0x6b, 12, 0xd2, 12, | 474 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12, 0x6a, 12, 0x6b, 12, 0xd2, 12, |
290 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12, 0x6c, 12, 0x6d, 12, | 475 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12, 0x6c, 12, 0x6d, 12, |
291 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12, 0x64, 12, | 476 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12, 0x64, 12, |
292 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12, | 477 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12, |
293 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, | 478 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 return m_pScanlineBuf; | 723 return m_pScanlineBuf; |
539 } | 724 } |
540 FX_DWORD CCodec_FaxDecoder::GetSrcOffset() { | 725 FX_DWORD CCodec_FaxDecoder::GetSrcOffset() { |
541 FX_DWORD ret = (bitpos + 7) / 8; | 726 FX_DWORD ret = (bitpos + 7) / 8; |
542 if (ret > m_SrcSize) { | 727 if (ret > m_SrcSize) { |
543 ret = m_SrcSize; | 728 ret = m_SrcSize; |
544 } | 729 } |
545 return ret; | 730 return ret; |
546 } | 731 } |
547 | 732 |
548 FX_BOOL FaxSkipEOL(const uint8_t* src_buf, int bitsize, int& bitpos) { | |
549 int startbit = bitpos; | |
550 while (bitpos < bitsize) { | |
551 int bit = NEXTBIT; | |
552 if (bit) { | |
553 if (bitpos - startbit <= 11) { | |
554 bitpos = startbit; | |
555 } | |
556 return TRUE; | |
557 } | |
558 } | |
559 return FALSE; | |
560 } | |
561 | |
562 FX_BOOL FaxGet1DLine(const uint8_t* src_buf, | |
563 int bitsize, | |
564 int& bitpos, | |
565 uint8_t* dest_buf, | |
566 int columns) { | |
567 bool color = true; | |
568 int startpos = 0; | |
569 while (1) { | |
570 if (bitpos >= bitsize) { | |
571 return FALSE; | |
572 } | |
573 int run_len = 0; | |
574 while (1) { | |
575 int run = FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, | |
576 bitpos, bitsize); | |
577 if (run < 0) { | |
578 while (bitpos < bitsize) { | |
579 int bit = NEXTBIT; | |
580 if (bit) { | |
581 return TRUE; | |
582 } | |
583 } | |
584 return FALSE; | |
585 } | |
586 run_len += run; | |
587 if (run < 64) { | |
588 break; | |
589 } | |
590 } | |
591 if (!color) { | |
592 FaxFillBits(dest_buf, columns, startpos, startpos + run_len); | |
593 } | |
594 startpos += run_len; | |
595 if (startpos >= columns) { | |
596 break; | |
597 } | |
598 color = !color; | |
599 } | |
600 return TRUE; | |
601 } | |
602 | |
603 FX_BOOL FaxG4GetRow(const uint8_t* src_buf, | |
604 int bitsize, | |
605 int& bitpos, | |
606 uint8_t* dest_buf, | |
607 const uint8_t* ref_buf, | |
608 int columns) { | |
609 int a0 = -1; | |
610 bool a0color = true; | |
611 while (1) { | |
612 if (bitpos >= bitsize) { | |
613 return FALSE; | |
614 } | |
615 int a1, a2, b1, b2; | |
616 FaxG4FindB1B2(ref_buf, columns, a0, a0color, b1, b2); | |
617 FX_BOOL bit = NEXTBIT; | |
618 int v_delta = 0; | |
619 if (bit) { | |
620 } else { | |
621 if (bitpos >= bitsize) { | |
622 return FALSE; | |
623 } | |
624 FX_BOOL bit1 = NEXTBIT; | |
625 if (bitpos >= bitsize) { | |
626 return FALSE; | |
627 } | |
628 FX_BOOL bit2 = NEXTBIT; | |
629 if (bit1 && bit2) { | |
630 v_delta = 1; | |
631 } else if (bit1) { | |
632 v_delta = -1; | |
633 } else if (bit2) { | |
634 int run_len1 = 0; | |
635 while (1) { | |
636 int run = FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, | |
637 src_buf, bitpos, bitsize); | |
638 run_len1 += run; | |
639 if (run < 64) { | |
640 break; | |
641 } | |
642 } | |
643 if (a0 < 0) { | |
644 run_len1++; | |
645 } | |
646 a1 = a0 + run_len1; | |
647 if (!a0color) { | |
648 FaxFillBits(dest_buf, columns, a0, a1); | |
649 } | |
650 int run_len2 = 0; | |
651 while (1) { | |
652 int run = FaxGetRun(a0color ? FaxBlackRunIns : FaxWhiteRunIns, | |
653 src_buf, bitpos, bitsize); | |
654 run_len2 += run; | |
655 if (run < 64) { | |
656 break; | |
657 } | |
658 } | |
659 a2 = a1 + run_len2; | |
660 if (a0color) { | |
661 FaxFillBits(dest_buf, columns, a1, a2); | |
662 } | |
663 a0 = a2; | |
664 if (a0 < columns) { | |
665 continue; | |
666 } | |
667 return TRUE; | |
668 } else { | |
669 if (bitpos >= bitsize) { | |
670 return FALSE; | |
671 } | |
672 bit = NEXTBIT; | |
673 if (bit) { | |
674 if (!a0color) { | |
675 FaxFillBits(dest_buf, columns, a0, b2); | |
676 } | |
677 if (b2 >= columns) { | |
678 return TRUE; | |
679 } | |
680 a0 = b2; | |
681 continue; | |
682 } else { | |
683 if (bitpos >= bitsize) { | |
684 return FALSE; | |
685 } | |
686 FX_BOOL bit1 = NEXTBIT; | |
687 if (bitpos >= bitsize) { | |
688 return FALSE; | |
689 } | |
690 FX_BOOL bit2 = NEXTBIT; | |
691 if (bit1 && bit2) { | |
692 v_delta = 2; | |
693 } else if (bit1) { | |
694 v_delta = -2; | |
695 } else if (bit2) { | |
696 if (bitpos >= bitsize) { | |
697 return FALSE; | |
698 } | |
699 bit = NEXTBIT; | |
700 if (bit) { | |
701 v_delta = 3; | |
702 } else { | |
703 v_delta = -3; | |
704 } | |
705 } else { | |
706 if (bitpos >= bitsize) { | |
707 return FALSE; | |
708 } | |
709 bit = NEXTBIT; | |
710 if (bit) { | |
711 bitpos += 3; | |
712 continue; | |
713 } else { | |
714 bitpos += 5; | |
715 return TRUE; | |
716 } | |
717 } | |
718 } | |
719 } | |
720 } | |
721 a1 = b1 + v_delta; | |
722 if (!a0color) { | |
723 FaxFillBits(dest_buf, columns, a0, a1); | |
724 } | |
725 if (a1 >= columns) { | |
726 return TRUE; | |
727 } | |
728 a0 = a1; | |
729 a0color = !a0color; | |
730 } | |
731 } | |
732 | |
733 void FaxG4Decode(const uint8_t* src_buf, | 733 void FaxG4Decode(const uint8_t* src_buf, |
734 FX_DWORD src_size, | 734 FX_DWORD src_size, |
735 int* pbitpos, | 735 int* pbitpos, |
736 uint8_t* dest_buf, | 736 uint8_t* dest_buf, |
737 int width, | 737 int width, |
738 int height, | 738 int height, |
739 int pitch) { | 739 int pitch) { |
740 if (pitch == 0) { | 740 if (pitch == 0) { |
741 pitch = (width + 7) / 8; | 741 pitch = (width + 7) / 8; |
742 } | 742 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 FX_BOOL EndOfLine, | 821 FX_BOOL EndOfLine, |
822 FX_BOOL EncodedByteAlign, | 822 FX_BOOL EncodedByteAlign, |
823 FX_BOOL BlackIs1, | 823 FX_BOOL BlackIs1, |
824 int Columns, | 824 int Columns, |
825 int Rows) { | 825 int Rows) { |
826 CCodec_FaxDecoder* pDecoder = new CCodec_FaxDecoder; | 826 CCodec_FaxDecoder* pDecoder = new CCodec_FaxDecoder; |
827 pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, | 827 pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, |
828 EncodedByteAlign, BlackIs1, Columns, Rows); | 828 EncodedByteAlign, BlackIs1, Columns, Rows); |
829 return pDecoder; | 829 return pDecoder; |
830 } | 830 } |
OLD | NEW |