diff options
-rw-r--r-- | core/include/fxcodec/fx_codec.h | 23 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 60 | ||||
-rw-r--r-- | core/src/fxcodec/codec/codec_int.h | 25 | ||||
-rw-r--r-- | core/src/fxcodec/codec/fx_codec_jpx_opj.cpp | 85 |
4 files changed, 92 insertions, 101 deletions
diff --git a/core/include/fxcodec/fx_codec.h b/core/include/fxcodec/fx_codec.h index 9fc19edd53..833ac2d10d 100644 --- a/core/include/fxcodec/fx_codec.h +++ b/core/include/fxcodec/fx_codec.h @@ -14,6 +14,7 @@ #include "fx_codec_provider.h" class CFX_DIBSource; +class CJPX_Decoder; class ICodec_ScanlineDecoder; class ICodec_ProgressiveDecoder; class ICodec_BasicModule; @@ -213,27 +214,26 @@ class ICodec_JpegModule { virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr = NULL) = 0; }; + class ICodec_JpxModule { public: virtual ~ICodec_JpxModule() {} - virtual void* CreateDecoder(const uint8_t* src_buf, - FX_DWORD src_size, - FX_BOOL useColorSpace = FALSE) = 0; + virtual CJPX_Decoder* CreateDecoder(const uint8_t* src_buf, + FX_DWORD src_size, + bool use_colorspace) = 0; - virtual void GetImageInfo(void* ctx, - FX_DWORD& width, - FX_DWORD& height, - FX_DWORD& codestream_nComps, - FX_DWORD& output_nComps) = 0; + virtual void GetImageInfo(CJPX_Decoder* pDecoder, + FX_DWORD* width, + FX_DWORD* height, + FX_DWORD* components) = 0; - virtual FX_BOOL Decode(void* ctx, + virtual FX_BOOL Decode(CJPX_Decoder* pDecoder, uint8_t* dest_data, int pitch, - FX_BOOL bTranslateColor, uint8_t* offsets) = 0; - virtual void DestroyDecoder(void* ctx) = 0; + virtual void DestroyDecoder(CJPX_Decoder* pDecoder) = 0; }; class ICodec_PngModule { public: @@ -350,6 +350,7 @@ class ICodec_TiffModule { virtual void DestroyDecoder(void* ctx) = 0; }; + class ICodec_Jbig2Module { public: virtual ~ICodec_Jbig2Module() {} diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index 977abe1b7b..f75453557d 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -62,17 +62,17 @@ FX_SAFE_DWORD CalculatePitch32(int bpp, int width) { class JpxBitMapContext { public: explicit JpxBitMapContext(ICodec_JpxModule* jpx_module) - : jpx_module_(jpx_module), ctx_(nullptr), output_offsets_(nullptr) {} + : jpx_module_(jpx_module), decoder_(nullptr), output_offsets_(nullptr) {} ~JpxBitMapContext() { FX_Free(output_offsets_); - jpx_module_->DestroyDecoder(ctx_); + jpx_module_->DestroyDecoder(decoder_); } - // Takes ownership of |ctx|. - void set_context(void* ctx) { ctx_ = ctx; } + // Takes ownership of |decoder|. + void set_decoder(CJPX_Decoder* decoder) { decoder_ = decoder; } - void* context() { return ctx_; } + CJPX_Decoder* decoder() { return decoder_; } // Takes ownership of |output_offsets|. void set_output_offsets(unsigned char* output_offsets) { @@ -83,7 +83,7 @@ class JpxBitMapContext { private: ICodec_JpxModule* jpx_module_; // Weak pointer. - void* ctx_; // Decoder context, owned. + CJPX_Decoder* decoder_; // Decoder, owned. unsigned char* output_offsets_; // Output offsets for decoding, owned. // Disallow evil constructors @@ -698,74 +698,64 @@ void CPDF_DIBSource::LoadJpxBitmap() { nonstd::unique_ptr<JpxBitMapContext> context( new JpxBitMapContext(pJpxModule)); - context->set_context(pJpxModule->CreateDecoder(m_pStreamAcc->GetData(), + context->set_decoder(pJpxModule->CreateDecoder(m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), m_pColorSpace != nullptr)); - if (!context->context()) + if (!context->decoder()) return; FX_DWORD width = 0; FX_DWORD height = 0; - FX_DWORD codestream_nComps = 0; - FX_DWORD image_nComps = 0; - pJpxModule->GetImageInfo(context->context(), width, height, codestream_nComps, - image_nComps); + FX_DWORD components = 0; + pJpxModule->GetImageInfo(context->decoder(), &width, &height, &components); if ((int)width < m_Width || (int)height < m_Height) return; - int output_nComps; - FX_BOOL bTranslateColor; FX_BOOL bSwapRGB = FALSE; if (m_pColorSpace) { - if (codestream_nComps != (FX_DWORD)m_pColorSpace->CountComponents()) + if (components != (FX_DWORD)m_pColorSpace->CountComponents()) return; - output_nComps = codestream_nComps; - bTranslateColor = FALSE; + if (m_pColorSpace == CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB)) { bSwapRGB = TRUE; m_pColorSpace = nullptr; } } else { - bTranslateColor = TRUE; - if (image_nComps) { - output_nComps = image_nComps; - } else { - output_nComps = codestream_nComps; - } - if (output_nComps == 3) { + if (components == 3) { bSwapRGB = TRUE; - } else if (output_nComps == 4) { + } else if (components == 4) { m_pColorSpace = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); - bTranslateColor = FALSE; } - m_nComponents = output_nComps; + m_nComponents = components; } + FXDIB_Format format; - if (output_nComps == 1) { + if (components == 1) { format = FXDIB_8bppRgb; - } else if (output_nComps <= 3) { + } else if (components <= 3) { format = FXDIB_Rgb; - } else if (output_nComps == 4) { + } else if (components == 4) { format = FXDIB_Rgb32; } else { - width = (width * output_nComps + 2) / 3; + width = (width * components + 2) / 3; format = FXDIB_Rgb; } + m_pCachedBitmap.reset(new CFX_DIBitmap); if (!m_pCachedBitmap->Create(width, height, format)) { m_pCachedBitmap.reset(); return; } m_pCachedBitmap->Clear(0xFFFFFFFF); - context->set_output_offsets(FX_Alloc(uint8_t, output_nComps)); - for (int i = 0; i < output_nComps; ++i) + context->set_output_offsets(FX_Alloc(uint8_t, components)); + for (int i = 0; i < components; ++i) context->output_offsets()[i] = i; if (bSwapRGB) { context->output_offsets()[0] = 2; context->output_offsets()[2] = 0; } - if (!pJpxModule->Decode(context->context(), m_pCachedBitmap->GetBuffer(), - m_pCachedBitmap->GetPitch(), bTranslateColor, + if (!pJpxModule->Decode(context->decoder(), m_pCachedBitmap->GetBuffer(), + m_pCachedBitmap->GetPitch(), context->output_offsets())) { m_pCachedBitmap.reset(); return; diff --git a/core/src/fxcodec/codec/codec_int.h b/core/src/fxcodec/codec/codec_int.h index d172d7157e..75e1fd141b 100644 --- a/core/src/fxcodec/codec/codec_int.h +++ b/core/src/fxcodec/codec/codec_int.h @@ -311,20 +311,21 @@ class CCodec_IccModule : public ICodec_IccModule { class CCodec_JpxModule : public ICodec_JpxModule { public: CCodec_JpxModule(); - void* CreateDecoder(const uint8_t* src_buf, - FX_DWORD src_size, - FX_BOOL useColorSpace = FALSE); - void GetImageInfo(void* ctx, - FX_DWORD& width, - FX_DWORD& height, - FX_DWORD& codestream_nComps, - FX_DWORD& output_nComps); - FX_BOOL Decode(void* ctx, + ~CCodec_JpxModule() override; + + // ICodec_JpxModule: + CJPX_Decoder* CreateDecoder(const uint8_t* src_buf, + FX_DWORD src_size, + bool use_colorspace) override; + void GetImageInfo(CJPX_Decoder* pDecoder, + FX_DWORD* width, + FX_DWORD* height, + FX_DWORD* components) override; + FX_BOOL Decode(CJPX_Decoder* pDecoder, uint8_t* dest_data, int pitch, - FX_BOOL bTranslateColor, - uint8_t* offsets); - void DestroyDecoder(void* ctx); + uint8_t* offsets) override; + void DestroyDecoder(CJPX_Decoder* pDecoder) override; }; class CCodec_TiffModule : public ICodec_TiffModule { diff --git a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp index e1b9d255de..81de30e58a 100644 --- a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp +++ b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp @@ -613,26 +613,30 @@ void color_apply_conversion(opj_image_t* image) { } class CJPX_Decoder { public: - CJPX_Decoder(); + explicit CJPX_Decoder(bool use_colorspace); ~CJPX_Decoder(); FX_BOOL Init(const unsigned char* src_data, int src_size); - void GetInfo(FX_DWORD& width, - FX_DWORD& height, - FX_DWORD& codestream_nComps, - FX_DWORD& output_nComps); + void GetInfo(FX_DWORD* width, FX_DWORD* height, FX_DWORD* components); FX_BOOL Decode(uint8_t* dest_buf, int pitch, - FX_BOOL bTranslateColor, uint8_t* offsets); + + private: const uint8_t* m_SrcData; int m_SrcSize; opj_image_t* image; opj_codec_t* l_codec; opj_stream_t* l_stream; - FX_BOOL m_useColorSpace; + bool m_UseColorSpace; }; -CJPX_Decoder::CJPX_Decoder() - : image(NULL), l_codec(NULL), l_stream(NULL), m_useColorSpace(FALSE) {} + +CJPX_Decoder::CJPX_Decoder(bool use_colorspace) + : image(nullptr), + l_codec(nullptr), + l_stream(nullptr), + m_UseColorSpace(use_colorspace) { +} + CJPX_Decoder::~CJPX_Decoder() { if (l_codec) { opj_destroy_codec(l_codec); @@ -644,6 +648,7 @@ CJPX_Decoder::~CJPX_Decoder() { opj_image_destroy(image); } } + FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size) { static const unsigned char szJP2Header[] = { 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a}; @@ -722,17 +727,17 @@ FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size) { } return TRUE; } -void CJPX_Decoder::GetInfo(FX_DWORD& width, - FX_DWORD& height, - FX_DWORD& codestream_nComps, - FX_DWORD& output_nComps) { - width = (FX_DWORD)image->x1; - height = (FX_DWORD)image->y1; - output_nComps = codestream_nComps = (FX_DWORD)image->numcomps; + +void CJPX_Decoder::GetInfo(FX_DWORD* width, + FX_DWORD* height, + FX_DWORD* components) { + *width = (FX_DWORD)image->x1; + *height = (FX_DWORD)image->y1; + *components = (FX_DWORD)image->numcomps; } + FX_BOOL CJPX_Decoder::Decode(uint8_t* dest_buf, int pitch, - FX_BOOL bTranslateColor, uint8_t* offsets) { int i, wid, hei, row, col, channel, src; uint8_t* pChannel; @@ -815,38 +820,32 @@ done: FX_Free(adjust_comps); return result; } -void initialize_transition_table(); -void initialize_significance_luts(); -void initialize_sign_lut(); + CCodec_JpxModule::CCodec_JpxModule() {} -void* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf, - FX_DWORD src_size, - FX_BOOL useColorSpace) { - CJPX_Decoder* pDecoder = new CJPX_Decoder; - pDecoder->m_useColorSpace = useColorSpace; - if (!pDecoder->Init(src_buf, src_size)) { - delete pDecoder; - return NULL; - } - return pDecoder; +CCodec_JpxModule::~CCodec_JpxModule() { } -void CCodec_JpxModule::GetImageInfo(void* ctx, - FX_DWORD& width, - FX_DWORD& height, - FX_DWORD& codestream_nComps, - FX_DWORD& output_nComps) { - CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; - pDecoder->GetInfo(width, height, codestream_nComps, output_nComps); + +CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf, + FX_DWORD src_size, + bool use_colorspace) { + nonstd::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(use_colorspace)); + return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr; } -FX_BOOL CCodec_JpxModule::Decode(void* ctx, + +void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, + FX_DWORD* width, + FX_DWORD* height, + FX_DWORD* components) { + pDecoder->GetInfo(width, height, components); +} + +FX_BOOL CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, uint8_t* dest_data, int pitch, - FX_BOOL bTranslateColor, uint8_t* offsets) { - CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; - return pDecoder->Decode(dest_data, pitch, bTranslateColor, offsets); + return pDecoder->Decode(dest_data, pitch, offsets); } -void CCodec_JpxModule::DestroyDecoder(void* ctx) { - CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; + +void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { delete pDecoder; } |