From 00ba8bbea0ff57d6f11257736408e530e54ef642 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 27 Jun 2018 18:02:30 +0000 Subject: Use spans and UnownedPtrs in cjpx_decoder.h Change-Id: I1c9f8221ce7a0e42407e93deec481be2e941cfde Reviewed-on: https://pdfium-review.googlesource.com/36210 Commit-Queue: Tom Sepez Reviewed-by: dsinclair --- core/fxcodec/codec/cjpx_decoder.h | 13 ++++---- core/fxcodec/codec/fx_codec_jpx_opj.cpp | 56 ++++++++++++++++----------------- 2 files changed, 34 insertions(+), 35 deletions(-) (limited to 'core/fxcodec') diff --git a/core/fxcodec/codec/cjpx_decoder.h b/core/fxcodec/codec/cjpx_decoder.h index 9de9d6c4ab..5be4b874d2 100644 --- a/core/fxcodec/codec/cjpx_decoder.h +++ b/core/fxcodec/codec/cjpx_decoder.h @@ -12,6 +12,7 @@ #include "core/fxcodec/codec/codec_int.h" #include "core/fxcrt/unowned_ptr.h" +#include "third_party/base/span.h" #include "third_party/libopenjpeg20/openjpeg.h" class CPDF_ColorSpace; @@ -21,20 +22,18 @@ class CJPX_Decoder { explicit CJPX_Decoder(CPDF_ColorSpace* cs); ~CJPX_Decoder(); - bool Init(const unsigned char* src_data, uint32_t src_size); + bool Init(pdfium::span src_data); void GetInfo(uint32_t* width, uint32_t* height, uint32_t* components); bool Decode(uint8_t* dest_buf, uint32_t pitch, const std::vector& offsets); private: - const uint8_t* m_SrcData; - uint32_t m_SrcSize; - // TODO(rharrison): Convert these to unowned ptrs, if possible. - opj_image_t* m_Image; - opj_codec_t* m_Codec; + pdfium::span m_SrcData; + UnownedPtr m_Image; + UnownedPtr m_Codec; std::unique_ptr m_DecodeData; - opj_stream_t* m_Stream; + UnownedPtr m_Stream; opj_dparameters_t m_Parameters; UnownedPtr const m_ColorSpace; }; diff --git a/core/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/fxcodec/codec/fx_codec_jpx_opj.cpp index e5ba37cc06..3e44b19a9d 100644 --- a/core/fxcodec/codec/fx_codec_jpx_opj.cpp +++ b/core/fxcodec/codec/fx_codec_jpx_opj.cpp @@ -465,24 +465,23 @@ CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs) CJPX_Decoder::~CJPX_Decoder() { if (m_Codec) - opj_destroy_codec(m_Codec); + opj_destroy_codec(m_Codec.Release()); if (m_Stream) - opj_stream_destroy(m_Stream); + opj_stream_destroy(m_Stream.Release()); if (m_Image) - opj_image_destroy(m_Image); + opj_image_destroy(m_Image.Release()); } -bool CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) { +bool CJPX_Decoder::Init(pdfium::span src_data) { static const unsigned char szJP2Header[] = { 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a}; - if (!src_data || src_size < sizeof(szJP2Header)) + if (src_data.empty() || src_data.size() < sizeof(szJP2Header)) return false; m_Image = nullptr; m_SrcData = src_data; - m_SrcSize = src_size; - m_DecodeData = pdfium::MakeUnique( - const_cast(src_data), src_size); + m_DecodeData = + pdfium::MakeUnique(src_data.data(), src_data.size()); m_Stream = fx_opj_stream_create_memory_stream( m_DecodeData.get(), static_cast(OPJ_J2K_STREAM_CHUNK_SIZE), 1); @@ -492,7 +491,7 @@ bool CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) { opj_set_default_decoder_parameters(&m_Parameters); m_Parameters.decod_format = 0; m_Parameters.cod_format = 3; - if (memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { + if (memcmp(m_SrcData.data(), szJP2Header, sizeof(szJP2Header)) == 0) { m_Codec = opj_create_decompress(OPJ_CODEC_JP2); m_Parameters.decod_format = 1; } else { @@ -503,39 +502,38 @@ bool CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) { if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED) m_Parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; - opj_set_info_handler(m_Codec, fx_ignore_callback, nullptr); - opj_set_warning_handler(m_Codec, fx_ignore_callback, nullptr); - opj_set_error_handler(m_Codec, fx_ignore_callback, nullptr); - if (!opj_setup_decoder(m_Codec, &m_Parameters)) + opj_set_info_handler(m_Codec.Get(), fx_ignore_callback, nullptr); + opj_set_warning_handler(m_Codec.Get(), fx_ignore_callback, nullptr); + opj_set_error_handler(m_Codec.Get(), fx_ignore_callback, nullptr); + if (!opj_setup_decoder(m_Codec.Get(), &m_Parameters)) return false; - if (!opj_read_header(m_Stream, m_Codec, &m_Image)) { - m_Image = nullptr; + m_Image = nullptr; + opj_image_t* pTempImage = nullptr; + if (!opj_read_header(m_Stream.Get(), m_Codec.Get(), &pTempImage)) return false; - } + + m_Image = pTempImage; m_Image->pdfium_use_colorspace = !!m_ColorSpace; if (!m_Parameters.nb_tile_to_decode) { - if (!opj_set_decode_area(m_Codec, m_Image, m_Parameters.DA_x0, + if (!opj_set_decode_area(m_Codec.Get(), m_Image.Get(), m_Parameters.DA_x0, m_Parameters.DA_y0, m_Parameters.DA_x1, m_Parameters.DA_y1)) { - opj_image_destroy(m_Image); - m_Image = nullptr; + opj_image_destroy(m_Image.Release()); return false; } - if (!(opj_decode(m_Codec, m_Stream, m_Image) && - opj_end_decompress(m_Codec, m_Stream))) { - opj_image_destroy(m_Image); - m_Image = nullptr; + if (!(opj_decode(m_Codec.Get(), m_Stream.Get(), m_Image.Get()) && + opj_end_decompress(m_Codec.Get(), m_Stream.Get()))) { + opj_image_destroy(m_Image.Release()); return false; } - } else if (!opj_get_decoded_tile(m_Codec, m_Stream, m_Image, + } else if (!opj_get_decoded_tile(m_Codec.Get(), m_Stream.Get(), m_Image.Get(), m_Parameters.tile_index)) { return false; } - opj_stream_destroy(m_Stream); - m_Stream = nullptr; + opj_stream_destroy(m_Stream.Release()); if (m_Image->color_space != OPJ_CLRSPC_SYCC && m_Image->numcomps == 3 && m_Image->comps[0].dx == m_Image->comps[0].dy && m_Image->comps[1].dx != 1) { @@ -544,7 +542,7 @@ bool CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) { m_Image->color_space = OPJ_CLRSPC_GRAY; } if (m_Image->color_space == OPJ_CLRSPC_SYCC) - color_sycc_to_rgb(m_Image); + color_sycc_to_rgb(m_Image.Get()); if (m_Image->icc_profile_buf) { // TODO(palmer): Using |opj_free| here resolves the crash described in @@ -649,7 +647,9 @@ std::unique_ptr CCodec_JpxModule::CreateDecoder( uint32_t src_size, CPDF_ColorSpace* cs) { auto decoder = pdfium::MakeUnique(cs); - return decoder->Init(src_buf, src_size) ? std::move(decoder) : nullptr; + return decoder->Init(pdfium::make_span(src_buf, src_size)) + ? std::move(decoder) + : nullptr; } void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, -- cgit v1.2.3