| 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 "core/fxcodec/codec/codec_int.h" | 7 #include "core/fxcodec/codec/codec_int.h" |
| 8 #include "core/fxcodec/include/fx_codec.h" | 8 #include "core/fxcodec/include/fx_codec.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 } | 454 } |
| 455 startpos += run_len; | 455 startpos += run_len; |
| 456 if (startpos >= columns) { | 456 if (startpos >= columns) { |
| 457 break; | 457 break; |
| 458 } | 458 } |
| 459 color = !color; | 459 color = !color; |
| 460 } | 460 } |
| 461 return TRUE; | 461 return TRUE; |
| 462 } | 462 } |
| 463 | 463 |
| 464 const uint8_t BlackRunTerminator[128] = { | |
| 465 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, | |
| 466 0x03, 5, 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, | |
| 467 0x07, 8, 0x18, 9, 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, | |
| 468 0x6c, 11, 0x37, 11, 0x28, 11, 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, | |
| 469 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12, 0x6a, 12, 0x6b, 12, 0xd2, 12, | |
| 470 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12, 0x6c, 12, 0x6d, 12, | |
| 471 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12, 0x64, 12, | |
| 472 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12, | |
| 473 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, | |
| 474 0x67, 12, | |
| 475 }; | |
| 476 | |
| 477 const uint8_t BlackRunMarkup[80] = { | |
| 478 0x0f, 10, 0xc8, 12, 0xc9, 12, 0x5b, 12, 0x33, 12, 0x34, 12, 0x35, 12, | |
| 479 0x6c, 13, 0x6d, 13, 0x4a, 13, 0x4b, 13, 0x4c, 13, 0x4d, 13, 0x72, 13, | |
| 480 0x73, 13, 0x74, 13, 0x75, 13, 0x76, 13, 0x77, 13, 0x52, 13, 0x53, 13, | |
| 481 0x54, 13, 0x55, 13, 0x5a, 13, 0x5b, 13, 0x64, 13, 0x65, 13, 0x08, 11, | |
| 482 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12, 0x14, 12, 0x15, 12, 0x16, 12, | |
| 483 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12, | |
| 484 }; | |
| 485 | |
| 486 const uint8_t WhiteRunTerminator[128] = { | |
| 487 0x35, 8, 0x07, 6, 0x07, 4, 0x08, 4, 0x0B, 4, 0x0C, 4, 0x0E, 4, 0x0F, 4, | |
| 488 0x13, 5, 0x14, 5, 0x07, 5, 0x08, 5, 0x08, 6, 0x03, 6, 0x34, 6, 0x35, 6, | |
| 489 0x2a, 6, 0x2B, 6, 0x27, 7, 0x0c, 7, 0x08, 7, 0x17, 7, 0x03, 7, 0x04, 7, | |
| 490 0x28, 7, 0x2B, 7, 0x13, 7, 0x24, 7, 0x18, 7, 0x02, 8, 0x03, 8, 0x1a, 8, | |
| 491 0x1b, 8, 0x12, 8, 0x13, 8, 0x14, 8, 0x15, 8, 0x16, 8, 0x17, 8, 0x28, 8, | |
| 492 0x29, 8, 0x2a, 8, 0x2b, 8, 0x2c, 8, 0x2d, 8, 0x04, 8, 0x05, 8, 0x0a, 8, | |
| 493 0x0b, 8, 0x52, 8, 0x53, 8, 0x54, 8, 0x55, 8, 0x24, 8, 0x25, 8, 0x58, 8, | |
| 494 0x59, 8, 0x5a, 8, 0x5b, 8, 0x4a, 8, 0x4b, 8, 0x32, 8, 0x33, 8, 0x34, 8, | |
| 495 }; | |
| 496 | |
| 497 const uint8_t WhiteRunMarkup[80] = { | |
| 498 0x1b, 5, 0x12, 5, 0x17, 6, 0x37, 7, 0x36, 8, 0x37, 8, 0x64, 8, | |
| 499 0x65, 8, 0x68, 8, 0x67, 8, 0xcc, 9, 0xcd, 9, 0xd2, 9, 0xd3, 9, | |
| 500 0xd4, 9, 0xd5, 9, 0xd6, 9, 0xd7, 9, 0xd8, 9, 0xd9, 9, 0xda, 9, | |
| 501 0xdb, 9, 0x98, 9, 0x99, 9, 0x9a, 9, 0x18, 6, 0x9b, 9, 0x08, 11, | |
| 502 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12, 0x14, 12, 0x15, 12, 0x16, 12, | |
| 503 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12, | |
| 504 }; | |
| 505 | |
| 506 void AddBitStream(uint8_t* dest_buf, int& dest_bitpos, int data, int bitlen) { | |
| 507 for (int i = bitlen - 1; i >= 0; i--) { | |
| 508 if (data & (1 << i)) { | |
| 509 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 510 } | |
| 511 dest_bitpos++; | |
| 512 } | |
| 513 } | |
| 514 | |
| 515 void FaxEncodeRun(uint8_t* dest_buf, int& dest_bitpos, int run, bool bWhite) { | |
| 516 while (run >= 2560) { | |
| 517 AddBitStream(dest_buf, dest_bitpos, 0x1f, 12); | |
| 518 run -= 2560; | |
| 519 } | |
| 520 if (run >= 64) { | |
| 521 int markup = run - run % 64; | |
| 522 const uint8_t* p = bWhite ? WhiteRunMarkup : BlackRunMarkup; | |
| 523 p += (markup / 64 - 1) * 2; | |
| 524 AddBitStream(dest_buf, dest_bitpos, *p, p[1]); | |
| 525 } | |
| 526 run %= 64; | |
| 527 const uint8_t* p = bWhite ? WhiteRunTerminator : BlackRunTerminator; | |
| 528 p += run * 2; | |
| 529 AddBitStream(dest_buf, dest_bitpos, *p, p[1]); | |
| 530 } | |
| 531 | |
| 532 void FaxEncode2DLine(uint8_t* dest_buf, | |
| 533 int& dest_bitpos, | |
| 534 const uint8_t* src_buf, | |
| 535 const uint8_t* ref_buf, | |
| 536 int cols) { | |
| 537 int a0 = -1; | |
| 538 bool a0color = true; | |
| 539 while (1) { | |
| 540 int a1 = FindBit(src_buf, cols, a0 + 1, !a0color); | |
| 541 int b1, b2; | |
| 542 FaxG4FindB1B2(ref_buf, cols, a0, a0color, b1, b2); | |
| 543 if (b2 < a1) { | |
| 544 dest_bitpos += 3; | |
| 545 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 546 dest_bitpos++; | |
| 547 a0 = b2; | |
| 548 } else if (a1 - b1 <= 3 && b1 - a1 <= 3) { | |
| 549 int delta = a1 - b1; | |
| 550 switch (delta) { | |
| 551 case 0: | |
| 552 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 553 break; | |
| 554 case 1: | |
| 555 case 2: | |
| 556 case 3: | |
| 557 dest_bitpos += delta == 1 ? 1 : delta + 2; | |
| 558 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 559 dest_bitpos++; | |
| 560 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 561 break; | |
| 562 case -1: | |
| 563 case -2: | |
| 564 case -3: | |
| 565 dest_bitpos += delta == -1 ? 1 : -delta + 2; | |
| 566 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 567 dest_bitpos++; | |
| 568 break; | |
| 569 } | |
| 570 dest_bitpos++; | |
| 571 a0 = a1; | |
| 572 a0color = !a0color; | |
| 573 } else { | |
| 574 int a2 = FindBit(src_buf, cols, a1 + 1, a0color); | |
| 575 dest_bitpos++; | |
| 576 dest_bitpos++; | |
| 577 dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); | |
| 578 dest_bitpos++; | |
| 579 if (a0 < 0) { | |
| 580 a0 = 0; | |
| 581 } | |
| 582 FaxEncodeRun(dest_buf, dest_bitpos, a1 - a0, a0color); | |
| 583 FaxEncodeRun(dest_buf, dest_bitpos, a2 - a1, !a0color); | |
| 584 a0 = a2; | |
| 585 } | |
| 586 if (a0 >= cols) { | |
| 587 return; | |
| 588 } | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 } // namespace | 464 } // namespace |
| 593 | 465 |
| 594 class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { | 466 class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { |
| 595 public: | 467 public: |
| 596 CCodec_FaxDecoder(); | 468 CCodec_FaxDecoder(const uint8_t* src_buf, |
| 469 uint32_t src_size, |
| 470 int width, |
| 471 int height, |
| 472 int K, |
| 473 FX_BOOL EndOfLine, |
| 474 FX_BOOL EncodedByteAlign, |
| 475 FX_BOOL BlackIs1, |
| 476 int Columns, |
| 477 int Rows); |
| 597 ~CCodec_FaxDecoder() override; | 478 ~CCodec_FaxDecoder() override; |
| 598 | 479 |
| 599 FX_BOOL Create(const uint8_t* src_buf, | |
| 600 uint32_t src_size, | |
| 601 int width, | |
| 602 int height, | |
| 603 int K, | |
| 604 FX_BOOL EndOfLine, | |
| 605 FX_BOOL EncodedByteAlign, | |
| 606 FX_BOOL BlackIs1, | |
| 607 int Columns, | |
| 608 int Rows); | |
| 609 | 480 |
| 610 // CCodec_ScanlineDecoder | 481 // CCodec_ScanlineDecoder |
| 611 FX_BOOL v_Rewind() override; | 482 FX_BOOL v_Rewind() override; |
| 612 uint8_t* v_GetNextLine() override; | 483 uint8_t* v_GetNextLine() override; |
| 613 uint32_t GetSrcOffset() override; | 484 uint32_t GetSrcOffset() override; |
| 614 | 485 |
| 615 int m_Encoding, m_bEndOfLine, m_bByteAlign, m_bBlack; | 486 int m_Encoding, m_bEndOfLine, m_bByteAlign, m_bBlack; |
| 616 int bitpos; | 487 int bitpos; |
| 617 const uint8_t* m_pSrcBuf; | 488 const uint8_t* m_pSrcBuf; |
| 618 uint32_t m_SrcSize; | 489 uint32_t m_SrcSize; |
| 619 uint8_t* m_pScanlineBuf; | 490 uint8_t* m_pScanlineBuf; |
| 620 uint8_t* m_pRefBuf; | 491 uint8_t* m_pRefBuf; |
| 621 }; | 492 }; |
| 622 | 493 |
| 623 CCodec_FaxDecoder::CCodec_FaxDecoder() { | 494 CCodec_FaxDecoder::CCodec_FaxDecoder(const uint8_t* src_buf, |
| 624 m_pScanlineBuf = NULL; | 495 uint32_t src_size, |
| 625 m_pRefBuf = NULL; | 496 int width, |
| 626 } | 497 int height, |
| 627 CCodec_FaxDecoder::~CCodec_FaxDecoder() { | 498 int K, |
| 628 FX_Free(m_pScanlineBuf); | 499 FX_BOOL EndOfLine, |
| 629 FX_Free(m_pRefBuf); | 500 FX_BOOL EncodedByteAlign, |
| 630 } | 501 FX_BOOL BlackIs1, |
| 631 FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, | 502 int Columns, |
| 632 uint32_t src_size, | 503 int Rows) { |
| 633 int width, | |
| 634 int height, | |
| 635 int K, | |
| 636 FX_BOOL EndOfLine, | |
| 637 FX_BOOL EncodedByteAlign, | |
| 638 FX_BOOL BlackIs1, | |
| 639 int Columns, | |
| 640 int Rows) { | |
| 641 m_Encoding = K; | 504 m_Encoding = K; |
| 642 m_bEndOfLine = EndOfLine; | 505 m_bEndOfLine = EndOfLine; |
| 643 m_bByteAlign = EncodedByteAlign; | 506 m_bByteAlign = EncodedByteAlign; |
| 644 m_bBlack = BlackIs1; | 507 m_bBlack = BlackIs1; |
| 645 m_OrigWidth = Columns; | 508 m_OrigWidth = Columns; |
| 646 m_OrigHeight = Rows; | 509 m_OrigHeight = Rows; |
| 647 if (m_OrigWidth == 0) { | 510 if (m_OrigWidth == 0) |
| 648 m_OrigWidth = width; | 511 m_OrigWidth = width; |
| 649 } | 512 if (m_OrigHeight == 0) |
| 650 if (m_OrigHeight == 0) { | |
| 651 m_OrigHeight = height; | 513 m_OrigHeight = height; |
| 652 } | |
| 653 // Should not overflow. Checked by FPDFAPI_CreateFaxDecoder. | 514 // Should not overflow. Checked by FPDFAPI_CreateFaxDecoder. |
| 654 m_Pitch = (static_cast<uint32_t>(m_OrigWidth) + 31) / 32 * 4; | 515 m_Pitch = (static_cast<uint32_t>(m_OrigWidth) + 31) / 32 * 4; |
| 655 m_OutputWidth = m_OrigWidth; | 516 m_OutputWidth = m_OrigWidth; |
| 656 m_OutputHeight = m_OrigHeight; | 517 m_OutputHeight = m_OrigHeight; |
| 657 m_pScanlineBuf = FX_Alloc(uint8_t, m_Pitch); | 518 m_pScanlineBuf = FX_Alloc(uint8_t, m_Pitch); |
| 658 m_pRefBuf = FX_Alloc(uint8_t, m_Pitch); | 519 m_pRefBuf = FX_Alloc(uint8_t, m_Pitch); |
| 659 m_pSrcBuf = src_buf; | 520 m_pSrcBuf = src_buf; |
| 660 m_SrcSize = src_size; | 521 m_SrcSize = src_size; |
| 661 m_nComps = 1; | 522 m_nComps = 1; |
| 662 m_bpc = 1; | 523 m_bpc = 1; |
| 663 return TRUE; | |
| 664 } | 524 } |
| 525 |
| 526 CCodec_FaxDecoder::~CCodec_FaxDecoder() { |
| 527 FX_Free(m_pScanlineBuf); |
| 528 FX_Free(m_pRefBuf); |
| 529 } |
| 530 |
| 665 FX_BOOL CCodec_FaxDecoder::v_Rewind() { | 531 FX_BOOL CCodec_FaxDecoder::v_Rewind() { |
| 666 FXSYS_memset(m_pRefBuf, 0xff, m_Pitch); | 532 FXSYS_memset(m_pRefBuf, 0xff, m_Pitch); |
| 667 bitpos = 0; | 533 bitpos = 0; |
| 668 return TRUE; | 534 return TRUE; |
| 669 } | 535 } |
| 670 uint8_t* CCodec_FaxDecoder::v_GetNextLine() { | 536 uint8_t* CCodec_FaxDecoder::v_GetNextLine() { |
| 671 int bitsize = m_SrcSize * 8; | 537 int bitsize = m_SrcSize * 8; |
| 672 FaxSkipEOL(m_pSrcBuf, bitsize, bitpos); | 538 FaxSkipEOL(m_pSrcBuf, bitsize, bitpos); |
| 673 if (bitpos >= bitsize) { | 539 if (bitpos >= bitsize) { |
| 674 return NULL; | 540 return NULL; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 for (int iRow = 0; iRow < height; iRow++) { | 606 for (int iRow = 0; iRow < height; iRow++) { |
| 741 uint8_t* line_buf = dest_buf + iRow * pitch; | 607 uint8_t* line_buf = dest_buf + iRow * pitch; |
| 742 FXSYS_memset(line_buf, 0xff, pitch); | 608 FXSYS_memset(line_buf, 0xff, pitch); |
| 743 FaxG4GetRow(src_buf, src_size << 3, bitpos, line_buf, ref_buf, width); | 609 FaxG4GetRow(src_buf, src_size << 3, bitpos, line_buf, ref_buf, width); |
| 744 FXSYS_memcpy(ref_buf, line_buf, pitch); | 610 FXSYS_memcpy(ref_buf, line_buf, pitch); |
| 745 } | 611 } |
| 746 FX_Free(ref_buf); | 612 FX_Free(ref_buf); |
| 747 *pbitpos = bitpos; | 613 *pbitpos = bitpos; |
| 748 } | 614 } |
| 749 | 615 |
| 750 class CCodec_FaxEncoder { | |
| 751 public: | |
| 752 CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); | |
| 753 ~CCodec_FaxEncoder(); | |
| 754 void Encode(uint8_t*& dest_buf, uint32_t& dest_size); | |
| 755 void Encode2DLine(const uint8_t* scan_line); | |
| 756 CFX_BinaryBuf m_DestBuf; | |
| 757 uint8_t* m_pRefLine; | |
| 758 uint8_t* m_pLineBuf; | |
| 759 int m_Cols, m_Rows, m_Pitch; | |
| 760 const uint8_t* m_pSrcBuf; | |
| 761 }; | |
| 762 CCodec_FaxEncoder::CCodec_FaxEncoder(const uint8_t* src_buf, | |
| 763 int width, | |
| 764 int height, | |
| 765 int pitch) { | |
| 766 m_pSrcBuf = src_buf; | |
| 767 m_Cols = width; | |
| 768 m_Rows = height; | |
| 769 m_Pitch = pitch; | |
| 770 m_pRefLine = FX_Alloc(uint8_t, m_Pitch); | |
| 771 FXSYS_memset(m_pRefLine, 0xff, m_Pitch); | |
| 772 m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); | |
| 773 m_DestBuf.EstimateSize(0, 10240); | |
| 774 } | |
| 775 CCodec_FaxEncoder::~CCodec_FaxEncoder() { | |
| 776 FX_Free(m_pRefLine); | |
| 777 FX_Free(m_pLineBuf); | |
| 778 } | |
| 779 void CCodec_FaxEncoder::Encode(uint8_t*& dest_buf, uint32_t& dest_size) { | |
| 780 int dest_bitpos = 0; | |
| 781 uint8_t last_byte = 0; | |
| 782 for (int i = 0; i < m_Rows; i++) { | |
| 783 const uint8_t* scan_line = m_pSrcBuf + i * m_Pitch; | |
| 784 FXSYS_memset(m_pLineBuf, 0, m_Pitch * 8); | |
| 785 m_pLineBuf[0] = last_byte; | |
| 786 FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols); | |
| 787 m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8); | |
| 788 last_byte = m_pLineBuf[dest_bitpos / 8]; | |
| 789 dest_bitpos %= 8; | |
| 790 FXSYS_memcpy(m_pRefLine, scan_line, m_Pitch); | |
| 791 } | |
| 792 if (dest_bitpos) { | |
| 793 m_DestBuf.AppendByte(last_byte); | |
| 794 } | |
| 795 dest_size = m_DestBuf.GetSize(); | |
| 796 dest_buf = m_DestBuf.DetachBuffer(); | |
| 797 } | |
| 798 FX_BOOL CCodec_FaxModule::Encode(const uint8_t* src_buf, | |
| 799 int width, | |
| 800 int height, | |
| 801 int pitch, | |
| 802 uint8_t*& dest_buf, | |
| 803 uint32_t& dest_size) { | |
| 804 CCodec_FaxEncoder encoder(src_buf, width, height, pitch); | |
| 805 encoder.Encode(dest_buf, dest_size); | |
| 806 return TRUE; | |
| 807 } | |
| 808 CCodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( | 616 CCodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( |
| 809 const uint8_t* src_buf, | 617 const uint8_t* src_buf, |
| 810 uint32_t src_size, | 618 uint32_t src_size, |
| 811 int width, | 619 int width, |
| 812 int height, | 620 int height, |
| 813 int K, | 621 int K, |
| 814 FX_BOOL EndOfLine, | 622 FX_BOOL EndOfLine, |
| 815 FX_BOOL EncodedByteAlign, | 623 FX_BOOL EncodedByteAlign, |
| 816 FX_BOOL BlackIs1, | 624 FX_BOOL BlackIs1, |
| 817 int Columns, | 625 int Columns, |
| 818 int Rows) { | 626 int Rows) { |
| 819 CCodec_FaxDecoder* pDecoder = new CCodec_FaxDecoder; | 627 return new CCodec_FaxDecoder(src_buf, src_size, width, height, K, EndOfLine, |
| 820 pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, | 628 EncodedByteAlign, BlackIs1, Columns, Rows); |
| 821 EncodedByteAlign, BlackIs1, Columns, Rows); | |
| 822 return pDecoder; | |
| 823 } | 629 } |
| OLD | NEW |