Chromium Code Reviews| 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 |
| 11 #include "codec_int.h" | 11 #include "codec_int.h" |
| 12 #include "core/include/fpdfapi/fpdf_resource.h" | |
| 12 #include "core/include/fxcodec/fx_codec.h" | 13 #include "core/include/fxcodec/fx_codec.h" |
| 13 #include "core/include/fxcrt/fx_safe_types.h" | 14 #include "core/include/fxcrt/fx_safe_types.h" |
| 14 #include "third_party/lcms2-2.6/include/lcms2.h" | 15 #include "third_party/lcms2-2.6/include/lcms2.h" |
| 15 #include "third_party/libopenjpeg20/openjpeg.h" | 16 #include "third_party/libopenjpeg20/openjpeg.h" |
| 16 | 17 |
| 17 static void fx_error_callback(const char* msg, void* client_data) { | 18 static void fx_error_callback(const char* msg, void* client_data) { |
| 18 (void)client_data; | 19 (void)client_data; |
| 19 } | 20 } |
| 20 static void fx_warning_callback(const char* msg, void* client_data) { | 21 static void fx_warning_callback(const char* msg, void* client_data) { |
| 21 (void)client_data; | 22 (void)client_data; |
| (...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 653 FX_Free(src2); | 654 FX_Free(src2); |
| 654 image->color_space = OPJ_CLRSPC_SRGB; | 655 image->color_space = OPJ_CLRSPC_SRGB; |
| 655 image->comps[0].prec = 16; | 656 image->comps[0].prec = 16; |
| 656 image->comps[1].prec = 16; | 657 image->comps[1].prec = 16; |
| 657 image->comps[2].prec = 16; | 658 image->comps[2].prec = 16; |
| 658 return; | 659 return; |
| 659 } | 660 } |
| 660 } | 661 } |
| 661 class CJPX_Decoder { | 662 class CJPX_Decoder { |
| 662 public: | 663 public: |
| 663 explicit CJPX_Decoder(bool use_colorspace); | 664 explicit CJPX_Decoder(CPDF_ColorSpace* cs); |
| 664 ~CJPX_Decoder(); | 665 ~CJPX_Decoder(); |
| 665 FX_BOOL Init(const unsigned char* src_data, FX_DWORD src_size); | 666 FX_BOOL Init(const unsigned char* src_data, FX_DWORD src_size); |
| 666 void GetInfo(FX_DWORD* width, FX_DWORD* height, FX_DWORD* components); | 667 void GetInfo(FX_DWORD* width, FX_DWORD* height, FX_DWORD* components); |
| 667 bool Decode(uint8_t* dest_buf, | 668 bool Decode(uint8_t* dest_buf, |
| 668 int pitch, | 669 int pitch, |
| 669 const std::vector<uint8_t>& offsets); | 670 const std::vector<uint8_t>& offsets); |
| 670 | 671 |
| 671 private: | 672 private: |
| 672 const uint8_t* m_SrcData; | 673 const uint8_t* m_SrcData; |
| 673 FX_DWORD m_SrcSize; | 674 FX_DWORD m_SrcSize; |
| 674 opj_image_t* image; | 675 opj_image_t* image; |
| 675 opj_codec_t* l_codec; | 676 opj_codec_t* l_codec; |
| 676 opj_stream_t* l_stream; | 677 opj_stream_t* l_stream; |
| 677 const bool m_UseColorSpace; | 678 const CPDF_ColorSpace* m_ColorSpace; |
|
Lei Zhang
2015/12/24 08:42:55
const CPDF_ColorSpace* const, since the pointer it
jun_fang
2015/12/24 10:12:20
Acknowledged.
| |
| 678 }; | 679 }; |
| 679 | 680 |
| 680 CJPX_Decoder::CJPX_Decoder(bool use_colorspace) | 681 CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs) |
| 681 : image(nullptr), | 682 : image(nullptr), l_codec(nullptr), l_stream(nullptr), m_ColorSpace(cs) {} |
| 682 l_codec(nullptr), | |
| 683 l_stream(nullptr), | |
| 684 m_UseColorSpace(use_colorspace) { | |
| 685 } | |
| 686 | 683 |
| 687 CJPX_Decoder::~CJPX_Decoder() { | 684 CJPX_Decoder::~CJPX_Decoder() { |
| 688 if (l_codec) { | 685 if (l_codec) { |
| 689 opj_destroy_codec(l_codec); | 686 opj_destroy_codec(l_codec); |
| 690 } | 687 } |
| 691 if (l_stream) { | 688 if (l_stream) { |
| 692 opj_stream_destroy(l_stream); | 689 opj_stream_destroy(l_stream); |
| 693 } | 690 } |
| 694 if (image) { | 691 if (image) { |
| 695 opj_image_destroy(image); | 692 opj_image_destroy(image); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 717 parameters.cod_format = 3; | 714 parameters.cod_format = 3; |
| 718 if (FXSYS_memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { | 715 if (FXSYS_memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { |
| 719 l_codec = opj_create_decompress(OPJ_CODEC_JP2); | 716 l_codec = opj_create_decompress(OPJ_CODEC_JP2); |
| 720 parameters.decod_format = 1; | 717 parameters.decod_format = 1; |
| 721 } else { | 718 } else { |
| 722 l_codec = opj_create_decompress(OPJ_CODEC_J2K); | 719 l_codec = opj_create_decompress(OPJ_CODEC_J2K); |
| 723 } | 720 } |
| 724 if (!l_codec) { | 721 if (!l_codec) { |
| 725 return FALSE; | 722 return FALSE; |
| 726 } | 723 } |
| 724 if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED) | |
| 725 parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; | |
| 727 opj_set_info_handler(l_codec, fx_info_callback, 00); | 726 opj_set_info_handler(l_codec, fx_info_callback, 00); |
| 728 opj_set_warning_handler(l_codec, fx_warning_callback, 00); | 727 opj_set_warning_handler(l_codec, fx_warning_callback, 00); |
| 729 opj_set_error_handler(l_codec, fx_error_callback, 00); | 728 opj_set_error_handler(l_codec, fx_error_callback, 00); |
| 730 if (!opj_setup_decoder(l_codec, ¶meters)) { | 729 if (!opj_setup_decoder(l_codec, ¶meters)) { |
| 731 return FALSE; | 730 return FALSE; |
| 732 } | 731 } |
| 733 if (!opj_read_header(l_stream, l_codec, &image)) { | 732 if (!opj_read_header(l_stream, l_codec, &image)) { |
| 734 image = NULL; | 733 image = NULL; |
| 735 return FALSE; | 734 return FALSE; |
| 736 } | 735 } |
| 737 image->pdfium_use_colorspace = m_UseColorSpace; | 736 image->pdfium_use_colorspace = !!m_ColorSpace; |
| 738 | 737 |
| 739 if (!parameters.nb_tile_to_decode) { | 738 if (!parameters.nb_tile_to_decode) { |
| 740 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0, | 739 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0, |
| 741 parameters.DA_x1, parameters.DA_y1)) { | 740 parameters.DA_x1, parameters.DA_y1)) { |
| 742 opj_image_destroy(image); | 741 opj_image_destroy(image); |
| 743 image = NULL; | 742 image = NULL; |
| 744 return FALSE; | 743 return FALSE; |
| 745 } | 744 } |
| 746 if (!(opj_decode(l_codec, l_stream, image) && | 745 if (!(opj_decode(l_codec, l_stream, image) && |
| 747 opj_end_decompress(l_codec, l_stream))) { | 746 opj_end_decompress(l_codec, l_stream))) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 } | 857 } |
| 859 return true; | 858 return true; |
| 860 } | 859 } |
| 861 | 860 |
| 862 CCodec_JpxModule::CCodec_JpxModule() {} | 861 CCodec_JpxModule::CCodec_JpxModule() {} |
| 863 CCodec_JpxModule::~CCodec_JpxModule() { | 862 CCodec_JpxModule::~CCodec_JpxModule() { |
| 864 } | 863 } |
| 865 | 864 |
| 866 CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf, | 865 CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf, |
| 867 FX_DWORD src_size, | 866 FX_DWORD src_size, |
| 868 bool use_colorspace) { | 867 CPDF_ColorSpace* cs) { |
| 869 nonstd::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(use_colorspace)); | 868 nonstd::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(cs)); |
| 870 return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr; | 869 return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr; |
| 871 } | 870 } |
| 872 | 871 |
| 873 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, | 872 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, |
| 874 FX_DWORD* width, | 873 FX_DWORD* width, |
| 875 FX_DWORD* height, | 874 FX_DWORD* height, |
| 876 FX_DWORD* components) { | 875 FX_DWORD* components) { |
| 877 pDecoder->GetInfo(width, height, components); | 876 pDecoder->GetInfo(width, height, components); |
| 878 } | 877 } |
| 879 | 878 |
| 880 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, | 879 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, |
| 881 uint8_t* dest_data, | 880 uint8_t* dest_data, |
| 882 int pitch, | 881 int pitch, |
| 883 const std::vector<uint8_t>& offsets) { | 882 const std::vector<uint8_t>& offsets) { |
| 884 return pDecoder->Decode(dest_data, pitch, offsets); | 883 return pDecoder->Decode(dest_data, pitch, offsets); |
| 885 } | 884 } |
| 886 | 885 |
| 887 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { | 886 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { |
| 888 delete pDecoder; | 887 delete pDecoder; |
| 889 } | 888 } |
| OLD | NEW |