| 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 <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 image->comps[0].prec = 16; | 684 image->comps[0].prec = 16; |
| 685 image->comps[1].prec = 16; | 685 image->comps[1].prec = 16; |
| 686 image->comps[2].prec = 16; | 686 image->comps[2].prec = 16; |
| 687 return; | 687 return; |
| 688 } | 688 } |
| 689 } | 689 } |
| 690 class CJPX_Decoder { | 690 class CJPX_Decoder { |
| 691 public: | 691 public: |
| 692 explicit CJPX_Decoder(CPDF_ColorSpace* cs); | 692 explicit CJPX_Decoder(CPDF_ColorSpace* cs); |
| 693 ~CJPX_Decoder(); | 693 ~CJPX_Decoder(); |
| 694 FX_BOOL Init(const unsigned char* src_data, uint32_t src_size); | 694 bool Init(const unsigned char* src_data, uint32_t src_size); |
| 695 void GetInfo(uint32_t* width, uint32_t* height, uint32_t* components); | 695 void GetInfo(uint32_t* width, uint32_t* height, uint32_t* components); |
| 696 bool Decode(uint8_t* dest_buf, | 696 bool Decode(uint8_t* dest_buf, |
| 697 int pitch, | 697 int pitch, |
| 698 const std::vector<uint8_t>& offsets); | 698 const std::vector<uint8_t>& offsets); |
| 699 | 699 |
| 700 private: | 700 private: |
| 701 const uint8_t* m_SrcData; | 701 const uint8_t* m_SrcData; |
| 702 uint32_t m_SrcSize; | 702 uint32_t m_SrcSize; |
| 703 opj_image_t* image; | 703 opj_image_t* image; |
| 704 opj_codec_t* l_codec; | 704 opj_codec_t* l_codec; |
| 705 opj_stream_t* l_stream; | 705 opj_stream_t* l_stream; |
| 706 const CPDF_ColorSpace* const m_ColorSpace; | 706 const CPDF_ColorSpace* const m_ColorSpace; |
| 707 }; | 707 }; |
| 708 | 708 |
| 709 CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs) | 709 CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs) |
| 710 : image(nullptr), l_codec(nullptr), l_stream(nullptr), m_ColorSpace(cs) {} | 710 : image(nullptr), l_codec(nullptr), l_stream(nullptr), m_ColorSpace(cs) {} |
| 711 | 711 |
| 712 CJPX_Decoder::~CJPX_Decoder() { | 712 CJPX_Decoder::~CJPX_Decoder() { |
| 713 if (l_codec) { | 713 if (l_codec) { |
| 714 opj_destroy_codec(l_codec); | 714 opj_destroy_codec(l_codec); |
| 715 } | 715 } |
| 716 if (l_stream) { | 716 if (l_stream) { |
| 717 opj_stream_destroy(l_stream); | 717 opj_stream_destroy(l_stream); |
| 718 } | 718 } |
| 719 if (image) { | 719 if (image) { |
| 720 opj_image_destroy(image); | 720 opj_image_destroy(image); |
| 721 } | 721 } |
| 722 } | 722 } |
| 723 | 723 |
| 724 FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) { | 724 bool CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) { |
| 725 static const unsigned char szJP2Header[] = { | 725 static const unsigned char szJP2Header[] = { |
| 726 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a}; | 726 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a}; |
| 727 if (!src_data || src_size < sizeof(szJP2Header)) | 727 if (!src_data || src_size < sizeof(szJP2Header)) |
| 728 return FALSE; | 728 return false; |
| 729 | 729 |
| 730 image = nullptr; | 730 image = nullptr; |
| 731 m_SrcData = src_data; | 731 m_SrcData = src_data; |
| 732 m_SrcSize = src_size; | 732 m_SrcSize = src_size; |
| 733 DecodeData srcData(const_cast<unsigned char*>(src_data), src_size); | 733 DecodeData srcData(const_cast<unsigned char*>(src_data), src_size); |
| 734 l_stream = fx_opj_stream_create_memory_stream(&srcData, | 734 l_stream = fx_opj_stream_create_memory_stream(&srcData, |
| 735 OPJ_J2K_STREAM_CHUNK_SIZE, 1); | 735 OPJ_J2K_STREAM_CHUNK_SIZE, 1); |
| 736 if (!l_stream) { | 736 if (!l_stream) { |
| 737 return FALSE; | 737 return false; |
| 738 } | 738 } |
| 739 opj_dparameters_t parameters; | 739 opj_dparameters_t parameters; |
| 740 opj_set_default_decoder_parameters(¶meters); | 740 opj_set_default_decoder_parameters(¶meters); |
| 741 parameters.decod_format = 0; | 741 parameters.decod_format = 0; |
| 742 parameters.cod_format = 3; | 742 parameters.cod_format = 3; |
| 743 if (FXSYS_memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { | 743 if (FXSYS_memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { |
| 744 l_codec = opj_create_decompress(OPJ_CODEC_JP2); | 744 l_codec = opj_create_decompress(OPJ_CODEC_JP2); |
| 745 parameters.decod_format = 1; | 745 parameters.decod_format = 1; |
| 746 } else { | 746 } else { |
| 747 l_codec = opj_create_decompress(OPJ_CODEC_J2K); | 747 l_codec = opj_create_decompress(OPJ_CODEC_J2K); |
| 748 } | 748 } |
| 749 if (!l_codec) { | 749 if (!l_codec) { |
| 750 return FALSE; | 750 return false; |
| 751 } | 751 } |
| 752 if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED) | 752 if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED) |
| 753 parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; | 753 parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; |
| 754 opj_set_info_handler(l_codec, fx_info_callback, 00); | 754 opj_set_info_handler(l_codec, fx_info_callback, 00); |
| 755 opj_set_warning_handler(l_codec, fx_warning_callback, 00); | 755 opj_set_warning_handler(l_codec, fx_warning_callback, 00); |
| 756 opj_set_error_handler(l_codec, fx_error_callback, 00); | 756 opj_set_error_handler(l_codec, fx_error_callback, 00); |
| 757 if (!opj_setup_decoder(l_codec, ¶meters)) { | 757 if (!opj_setup_decoder(l_codec, ¶meters)) { |
| 758 return FALSE; | 758 return false; |
| 759 } | 759 } |
| 760 if (!opj_read_header(l_stream, l_codec, &image)) { | 760 if (!opj_read_header(l_stream, l_codec, &image)) { |
| 761 image = nullptr; | 761 image = nullptr; |
| 762 return FALSE; | 762 return false; |
| 763 } | 763 } |
| 764 image->pdfium_use_colorspace = !!m_ColorSpace; | 764 image->pdfium_use_colorspace = !!m_ColorSpace; |
| 765 | 765 |
| 766 if (!parameters.nb_tile_to_decode) { | 766 if (!parameters.nb_tile_to_decode) { |
| 767 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0, | 767 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0, |
| 768 parameters.DA_x1, parameters.DA_y1)) { | 768 parameters.DA_x1, parameters.DA_y1)) { |
| 769 opj_image_destroy(image); | 769 opj_image_destroy(image); |
| 770 image = nullptr; | 770 image = nullptr; |
| 771 return FALSE; | 771 return false; |
| 772 } | 772 } |
| 773 if (!(opj_decode(l_codec, l_stream, image) && | 773 if (!(opj_decode(l_codec, l_stream, image) && |
| 774 opj_end_decompress(l_codec, l_stream))) { | 774 opj_end_decompress(l_codec, l_stream))) { |
| 775 opj_image_destroy(image); | 775 opj_image_destroy(image); |
| 776 image = nullptr; | 776 image = nullptr; |
| 777 return FALSE; | 777 return false; |
| 778 } | 778 } |
| 779 } else { | 779 } else { |
| 780 if (!opj_get_decoded_tile(l_codec, l_stream, image, | 780 if (!opj_get_decoded_tile(l_codec, l_stream, image, |
| 781 parameters.tile_index)) { | 781 parameters.tile_index)) { |
| 782 return FALSE; | 782 return false; |
| 783 } | 783 } |
| 784 } | 784 } |
| 785 opj_stream_destroy(l_stream); | 785 opj_stream_destroy(l_stream); |
| 786 l_stream = nullptr; | 786 l_stream = nullptr; |
| 787 if (image->color_space != OPJ_CLRSPC_SYCC && image->numcomps == 3 && | 787 if (image->color_space != OPJ_CLRSPC_SYCC && image->numcomps == 3 && |
| 788 image->comps[0].dx == image->comps[0].dy && image->comps[1].dx != 1) { | 788 image->comps[0].dx == image->comps[0].dy && image->comps[1].dx != 1) { |
| 789 image->color_space = OPJ_CLRSPC_SYCC; | 789 image->color_space = OPJ_CLRSPC_SYCC; |
| 790 } else if (image->numcomps <= 2) { | 790 } else if (image->numcomps <= 2) { |
| 791 image->color_space = OPJ_CLRSPC_GRAY; | 791 image->color_space = OPJ_CLRSPC_GRAY; |
| 792 } | 792 } |
| 793 if (image->color_space == OPJ_CLRSPC_SYCC) { | 793 if (image->color_space == OPJ_CLRSPC_SYCC) { |
| 794 color_sycc_to_rgb(image); | 794 color_sycc_to_rgb(image); |
| 795 } | 795 } |
| 796 if (image->icc_profile_buf) { | 796 if (image->icc_profile_buf) { |
| 797 FX_Free(image->icc_profile_buf); | 797 FX_Free(image->icc_profile_buf); |
| 798 image->icc_profile_buf = nullptr; | 798 image->icc_profile_buf = nullptr; |
| 799 image->icc_profile_len = 0; | 799 image->icc_profile_len = 0; |
| 800 } | 800 } |
| 801 if (!image) { | 801 if (!image) { |
| 802 return FALSE; | 802 return false; |
| 803 } | 803 } |
| 804 return TRUE; | 804 return true; |
| 805 } | 805 } |
| 806 | 806 |
| 807 void CJPX_Decoder::GetInfo(uint32_t* width, | 807 void CJPX_Decoder::GetInfo(uint32_t* width, |
| 808 uint32_t* height, | 808 uint32_t* height, |
| 809 uint32_t* components) { | 809 uint32_t* components) { |
| 810 *width = (uint32_t)image->x1; | 810 *width = (uint32_t)image->x1; |
| 811 *height = (uint32_t)image->y1; | 811 *height = (uint32_t)image->y1; |
| 812 *components = (uint32_t)image->numcomps; | 812 *components = (uint32_t)image->numcomps; |
| 813 } | 813 } |
| 814 | 814 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, | 909 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, |
| 910 uint8_t* dest_data, | 910 uint8_t* dest_data, |
| 911 int pitch, | 911 int pitch, |
| 912 const std::vector<uint8_t>& offsets) { | 912 const std::vector<uint8_t>& offsets) { |
| 913 return pDecoder->Decode(dest_data, pitch, offsets); | 913 return pDecoder->Decode(dest_data, pitch, offsets); |
| 914 } | 914 } |
| 915 | 915 |
| 916 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { | 916 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { |
| 917 delete pDecoder; | 917 delete pDecoder; |
| 918 } | 918 } |
| OLD | NEW |