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