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/include/fxcodec/fx_codec.h" | 8 #include "core/include/fxcodec/fx_codec.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 0x16, 2240 % 256, 2240 / 256, 0x17, 2304 % 256, 2304 / 256, | 250 0x16, 2240 % 256, 2240 / 256, 0x17, 2304 % 256, 2304 / 256, |
251 0x1c, 2368 % 256, 2368 / 256, 0x1d, 2432 % 256, 2432 / 256, | 251 0x1c, 2368 % 256, 2368 / 256, 0x1d, 2432 % 256, 2432 / 256, |
252 0x1e, 2496 % 256, 2496 / 256, 0x1f, 2560 % 256, 2560 / 256, | 252 0x1e, 2496 % 256, 2496 / 256, 0x1f, 2560 % 256, 2560 / 256, |
253 0xff, | 253 0xff, |
254 }; | 254 }; |
255 | 255 |
256 int FaxGetRun(const uint8_t* ins_array, | 256 int FaxGetRun(const uint8_t* ins_array, |
257 const uint8_t* src_buf, | 257 const uint8_t* src_buf, |
258 int& bitpos, | 258 int& bitpos, |
259 int bitsize) { | 259 int bitsize) { |
260 FX_DWORD code = 0; | 260 uint32_t code = 0; |
261 int ins_off = 0; | 261 int ins_off = 0; |
262 while (1) { | 262 while (1) { |
263 uint8_t ins = ins_array[ins_off++]; | 263 uint8_t ins = ins_array[ins_off++]; |
264 if (ins == 0xff) { | 264 if (ins == 0xff) { |
265 return -1; | 265 return -1; |
266 } | 266 } |
267 if (bitpos >= bitsize) { | 267 if (bitpos >= bitsize) { |
268 return -1; | 268 return -1; |
269 } | 269 } |
270 code <<= 1; | 270 code <<= 1; |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 } | 595 } |
596 | 596 |
597 } // namespace | 597 } // namespace |
598 | 598 |
599 class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { | 599 class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { |
600 public: | 600 public: |
601 CCodec_FaxDecoder(); | 601 CCodec_FaxDecoder(); |
602 ~CCodec_FaxDecoder() override; | 602 ~CCodec_FaxDecoder() override; |
603 | 603 |
604 FX_BOOL Create(const uint8_t* src_buf, | 604 FX_BOOL Create(const uint8_t* src_buf, |
605 FX_DWORD src_size, | 605 uint32_t src_size, |
606 int width, | 606 int width, |
607 int height, | 607 int height, |
608 int K, | 608 int K, |
609 FX_BOOL EndOfLine, | 609 FX_BOOL EndOfLine, |
610 FX_BOOL EncodedByteAlign, | 610 FX_BOOL EncodedByteAlign, |
611 FX_BOOL BlackIs1, | 611 FX_BOOL BlackIs1, |
612 int Columns, | 612 int Columns, |
613 int Rows); | 613 int Rows); |
614 | 614 |
615 // CCodec_ScanlineDecoder | 615 // CCodec_ScanlineDecoder |
616 void v_DownScale(int dest_width, int dest_height) override {} | 616 void v_DownScale(int dest_width, int dest_height) override {} |
617 FX_BOOL v_Rewind() override; | 617 FX_BOOL v_Rewind() override; |
618 uint8_t* v_GetNextLine() override; | 618 uint8_t* v_GetNextLine() override; |
619 FX_DWORD GetSrcOffset() override; | 619 uint32_t GetSrcOffset() override; |
620 | 620 |
621 int m_Encoding, m_bEndOfLine, m_bByteAlign, m_bBlack; | 621 int m_Encoding, m_bEndOfLine, m_bByteAlign, m_bBlack; |
622 int bitpos; | 622 int bitpos; |
623 const uint8_t* m_pSrcBuf; | 623 const uint8_t* m_pSrcBuf; |
624 FX_DWORD m_SrcSize; | 624 uint32_t m_SrcSize; |
625 uint8_t* m_pScanlineBuf; | 625 uint8_t* m_pScanlineBuf; |
626 uint8_t* m_pRefBuf; | 626 uint8_t* m_pRefBuf; |
627 }; | 627 }; |
628 | 628 |
629 CCodec_FaxDecoder::CCodec_FaxDecoder() { | 629 CCodec_FaxDecoder::CCodec_FaxDecoder() { |
630 m_pScanlineBuf = NULL; | 630 m_pScanlineBuf = NULL; |
631 m_pRefBuf = NULL; | 631 m_pRefBuf = NULL; |
632 } | 632 } |
633 CCodec_FaxDecoder::~CCodec_FaxDecoder() { | 633 CCodec_FaxDecoder::~CCodec_FaxDecoder() { |
634 FX_Free(m_pScanlineBuf); | 634 FX_Free(m_pScanlineBuf); |
635 FX_Free(m_pRefBuf); | 635 FX_Free(m_pRefBuf); |
636 } | 636 } |
637 FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, | 637 FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, |
638 FX_DWORD src_size, | 638 uint32_t src_size, |
639 int width, | 639 int width, |
640 int height, | 640 int height, |
641 int K, | 641 int K, |
642 FX_BOOL EndOfLine, | 642 FX_BOOL EndOfLine, |
643 FX_BOOL EncodedByteAlign, | 643 FX_BOOL EncodedByteAlign, |
644 FX_BOOL BlackIs1, | 644 FX_BOOL BlackIs1, |
645 int Columns, | 645 int Columns, |
646 int Rows) { | 646 int Rows) { |
647 m_Encoding = K; | 647 m_Encoding = K; |
648 m_bEndOfLine = EndOfLine; | 648 m_bEndOfLine = EndOfLine; |
649 m_bByteAlign = EncodedByteAlign; | 649 m_bByteAlign = EncodedByteAlign; |
650 m_bBlack = BlackIs1; | 650 m_bBlack = BlackIs1; |
651 m_OrigWidth = Columns; | 651 m_OrigWidth = Columns; |
652 m_OrigHeight = Rows; | 652 m_OrigHeight = Rows; |
653 if (m_OrigWidth == 0) { | 653 if (m_OrigWidth == 0) { |
654 m_OrigWidth = width; | 654 m_OrigWidth = width; |
655 } | 655 } |
656 if (m_OrigHeight == 0) { | 656 if (m_OrigHeight == 0) { |
657 m_OrigHeight = height; | 657 m_OrigHeight = height; |
658 } | 658 } |
659 // Should not overflow. Checked by FPDFAPI_CreateFaxDecoder. | 659 // Should not overflow. Checked by FPDFAPI_CreateFaxDecoder. |
660 m_Pitch = (static_cast<FX_DWORD>(m_OrigWidth) + 31) / 32 * 4; | 660 m_Pitch = (static_cast<uint32_t>(m_OrigWidth) + 31) / 32 * 4; |
661 m_OutputWidth = m_OrigWidth; | 661 m_OutputWidth = m_OrigWidth; |
662 m_OutputHeight = m_OrigHeight; | 662 m_OutputHeight = m_OrigHeight; |
663 m_pScanlineBuf = FX_Alloc(uint8_t, m_Pitch); | 663 m_pScanlineBuf = FX_Alloc(uint8_t, m_Pitch); |
664 m_pRefBuf = FX_Alloc(uint8_t, m_Pitch); | 664 m_pRefBuf = FX_Alloc(uint8_t, m_Pitch); |
665 m_pSrcBuf = src_buf; | 665 m_pSrcBuf = src_buf; |
666 m_SrcSize = src_size; | 666 m_SrcSize = src_size; |
667 m_nComps = 1; | 667 m_nComps = 1; |
668 m_bpc = 1; | 668 m_bpc = 1; |
669 m_bColorTransformed = FALSE; | 669 m_bColorTransformed = FALSE; |
670 return TRUE; | 670 return TRUE; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 m_bByteAlign = FALSE; | 710 m_bByteAlign = FALSE; |
711 } else { | 711 } else { |
712 bitpos0++; | 712 bitpos0++; |
713 } | 713 } |
714 } | 714 } |
715 if (m_bByteAlign) { | 715 if (m_bByteAlign) { |
716 bitpos = bitpos1; | 716 bitpos = bitpos1; |
717 } | 717 } |
718 } | 718 } |
719 if (m_bBlack) { | 719 if (m_bBlack) { |
720 for (FX_DWORD i = 0; i < m_Pitch; i++) { | 720 for (uint32_t i = 0; i < m_Pitch; i++) { |
721 m_pScanlineBuf[i] = ~m_pScanlineBuf[i]; | 721 m_pScanlineBuf[i] = ~m_pScanlineBuf[i]; |
722 } | 722 } |
723 } | 723 } |
724 return m_pScanlineBuf; | 724 return m_pScanlineBuf; |
725 } | 725 } |
726 FX_DWORD CCodec_FaxDecoder::GetSrcOffset() { | 726 uint32_t CCodec_FaxDecoder::GetSrcOffset() { |
727 FX_DWORD ret = (bitpos + 7) / 8; | 727 uint32_t ret = (bitpos + 7) / 8; |
728 if (ret > m_SrcSize) { | 728 if (ret > m_SrcSize) { |
729 ret = m_SrcSize; | 729 ret = m_SrcSize; |
730 } | 730 } |
731 return ret; | 731 return ret; |
732 } | 732 } |
733 | 733 |
734 void FaxG4Decode(const uint8_t* src_buf, | 734 void FaxG4Decode(const uint8_t* src_buf, |
735 FX_DWORD src_size, | 735 uint32_t src_size, |
736 int* pbitpos, | 736 int* pbitpos, |
737 uint8_t* dest_buf, | 737 uint8_t* dest_buf, |
738 int width, | 738 int width, |
739 int height, | 739 int height, |
740 int pitch) { | 740 int pitch) { |
741 if (pitch == 0) { | 741 if (pitch == 0) { |
742 pitch = (width + 7) / 8; | 742 pitch = (width + 7) / 8; |
743 } | 743 } |
744 uint8_t* ref_buf = FX_Alloc(uint8_t, pitch); | 744 uint8_t* ref_buf = FX_Alloc(uint8_t, pitch); |
745 FXSYS_memset(ref_buf, 0xff, pitch); | 745 FXSYS_memset(ref_buf, 0xff, pitch); |
746 int bitpos = *pbitpos; | 746 int bitpos = *pbitpos; |
747 for (int iRow = 0; iRow < height; iRow++) { | 747 for (int iRow = 0; iRow < height; iRow++) { |
748 uint8_t* line_buf = dest_buf + iRow * pitch; | 748 uint8_t* line_buf = dest_buf + iRow * pitch; |
749 FXSYS_memset(line_buf, 0xff, pitch); | 749 FXSYS_memset(line_buf, 0xff, pitch); |
750 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); |
751 FXSYS_memcpy(ref_buf, line_buf, pitch); | 751 FXSYS_memcpy(ref_buf, line_buf, pitch); |
752 } | 752 } |
753 FX_Free(ref_buf); | 753 FX_Free(ref_buf); |
754 *pbitpos = bitpos; | 754 *pbitpos = bitpos; |
755 } | 755 } |
756 | 756 |
757 class CCodec_FaxEncoder { | 757 class CCodec_FaxEncoder { |
758 public: | 758 public: |
759 CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); | 759 CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); |
760 ~CCodec_FaxEncoder(); | 760 ~CCodec_FaxEncoder(); |
761 void Encode(uint8_t*& dest_buf, FX_DWORD& dest_size); | 761 void Encode(uint8_t*& dest_buf, uint32_t& dest_size); |
762 void Encode2DLine(const uint8_t* scan_line); | 762 void Encode2DLine(const uint8_t* scan_line); |
763 CFX_BinaryBuf m_DestBuf; | 763 CFX_BinaryBuf m_DestBuf; |
764 uint8_t* m_pRefLine; | 764 uint8_t* m_pRefLine; |
765 uint8_t* m_pLineBuf; | 765 uint8_t* m_pLineBuf; |
766 int m_Cols, m_Rows, m_Pitch; | 766 int m_Cols, m_Rows, m_Pitch; |
767 const uint8_t* m_pSrcBuf; | 767 const uint8_t* m_pSrcBuf; |
768 }; | 768 }; |
769 CCodec_FaxEncoder::CCodec_FaxEncoder(const uint8_t* src_buf, | 769 CCodec_FaxEncoder::CCodec_FaxEncoder(const uint8_t* src_buf, |
770 int width, | 770 int width, |
771 int height, | 771 int height, |
772 int pitch) { | 772 int pitch) { |
773 m_pSrcBuf = src_buf; | 773 m_pSrcBuf = src_buf; |
774 m_Cols = width; | 774 m_Cols = width; |
775 m_Rows = height; | 775 m_Rows = height; |
776 m_Pitch = pitch; | 776 m_Pitch = pitch; |
777 m_pRefLine = FX_Alloc(uint8_t, m_Pitch); | 777 m_pRefLine = FX_Alloc(uint8_t, m_Pitch); |
778 FXSYS_memset(m_pRefLine, 0xff, m_Pitch); | 778 FXSYS_memset(m_pRefLine, 0xff, m_Pitch); |
779 m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); | 779 m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); |
780 m_DestBuf.EstimateSize(0, 10240); | 780 m_DestBuf.EstimateSize(0, 10240); |
781 } | 781 } |
782 CCodec_FaxEncoder::~CCodec_FaxEncoder() { | 782 CCodec_FaxEncoder::~CCodec_FaxEncoder() { |
783 FX_Free(m_pRefLine); | 783 FX_Free(m_pRefLine); |
784 FX_Free(m_pLineBuf); | 784 FX_Free(m_pLineBuf); |
785 } | 785 } |
786 void CCodec_FaxEncoder::Encode(uint8_t*& dest_buf, FX_DWORD& dest_size) { | 786 void CCodec_FaxEncoder::Encode(uint8_t*& dest_buf, uint32_t& dest_size) { |
787 int dest_bitpos = 0; | 787 int dest_bitpos = 0; |
788 uint8_t last_byte = 0; | 788 uint8_t last_byte = 0; |
789 for (int i = 0; i < m_Rows; i++) { | 789 for (int i = 0; i < m_Rows; i++) { |
790 const uint8_t* scan_line = m_pSrcBuf + i * m_Pitch; | 790 const uint8_t* scan_line = m_pSrcBuf + i * m_Pitch; |
791 FXSYS_memset(m_pLineBuf, 0, m_Pitch * 8); | 791 FXSYS_memset(m_pLineBuf, 0, m_Pitch * 8); |
792 m_pLineBuf[0] = last_byte; | 792 m_pLineBuf[0] = last_byte; |
793 FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols); | 793 FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols); |
794 m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8); | 794 m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8); |
795 last_byte = m_pLineBuf[dest_bitpos / 8]; | 795 last_byte = m_pLineBuf[dest_bitpos / 8]; |
796 dest_bitpos %= 8; | 796 dest_bitpos %= 8; |
797 FXSYS_memcpy(m_pRefLine, scan_line, m_Pitch); | 797 FXSYS_memcpy(m_pRefLine, scan_line, m_Pitch); |
798 } | 798 } |
799 if (dest_bitpos) { | 799 if (dest_bitpos) { |
800 m_DestBuf.AppendByte(last_byte); | 800 m_DestBuf.AppendByte(last_byte); |
801 } | 801 } |
802 dest_size = m_DestBuf.GetSize(); | 802 dest_size = m_DestBuf.GetSize(); |
803 dest_buf = m_DestBuf.DetachBuffer(); | 803 dest_buf = m_DestBuf.DetachBuffer(); |
804 } | 804 } |
805 FX_BOOL CCodec_FaxModule::Encode(const uint8_t* src_buf, | 805 FX_BOOL CCodec_FaxModule::Encode(const uint8_t* src_buf, |
806 int width, | 806 int width, |
807 int height, | 807 int height, |
808 int pitch, | 808 int pitch, |
809 uint8_t*& dest_buf, | 809 uint8_t*& dest_buf, |
810 FX_DWORD& dest_size) { | 810 uint32_t& dest_size) { |
811 CCodec_FaxEncoder encoder(src_buf, width, height, pitch); | 811 CCodec_FaxEncoder encoder(src_buf, width, height, pitch); |
812 encoder.Encode(dest_buf, dest_size); | 812 encoder.Encode(dest_buf, dest_size); |
813 return TRUE; | 813 return TRUE; |
814 } | 814 } |
815 ICodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( | 815 ICodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( |
816 const uint8_t* src_buf, | 816 const uint8_t* src_buf, |
817 FX_DWORD src_size, | 817 uint32_t src_size, |
818 int width, | 818 int width, |
819 int height, | 819 int height, |
820 int K, | 820 int K, |
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 |