7#include "core/fxcodec/jpeg/jpeg_progressive_decoder.h"
11#include "core/fxcodec/cfx_codec_memory.h"
12#include "core/fxcodec/fx_codec.h"
13#include "core/fxcodec/jpeg/jpeg_common.h"
14#include "core/fxcodec/scanlinedecoder.h"
15#include "core/fxcrt/fx_safe_types.h"
16#include "core/fxge/dib/cfx_dibbase.h"
17#include "core/fxge/dib/fx_dib.h"
18#include "third_party/abseil-cpp/absl/types/optional.h"
19#include "third_party/base/check.h"
20#include "third_party/base/memory/ptr_util.h"
39 auto* pContext =
reinterpret_cast<CJpegContext*>(cinfo->client_data);
40 longjmp(pContext->m_JumpMark, -1);
44 if (cinfo->src->bytes_in_buffer <
static_cast<size_t>(num)) {
45 auto* pContext =
reinterpret_cast<CJpegContext*>(cinfo->client_data);
46 pContext->m_SkipSize = (
unsigned int)(num - cinfo->src->bytes_in_buffer);
47 cinfo->src->bytes_in_buffer = 0;
49 cinfo->src->next_input_byte += num;
50 cinfo->src->bytes_in_buffer -= num;
57 CFX_DIBAttribute* pAttribute) {
58 pAttribute->m_nXDPI = info.X_density;
59 pAttribute->m_nYDPI = info.Y_density;
60 pAttribute->m_wDPIUnit =
61 static_cast<CFX_DIBAttribute::ResUnit>(info.density_unit);
65 m_Info.client_data =
this;
66 m_Info.err = &m_ErrMgr;
68 m_ErrMgr.error_exit = error_fatal;
69 m_ErrMgr.emit_message = error_do_nothing_int;
70 m_ErrMgr.output_message = error_do_nothing;
71 m_ErrMgr.format_message = error_do_nothing_char;
72 m_ErrMgr.reset_error_mgr = error_do_nothing;
74 m_SrcMgr.init_source = src_do_nothing;
75 m_SrcMgr.term_source = src_do_nothing;
76 m_SrcMgr.skip_input_data = src_skip_data;
77 m_SrcMgr.fill_input_buffer = src_fill_buffer;
78 m_SrcMgr.resync_to_restart = src_resync;
82 jpeg_destroy_decompress(&m_Info);
89JpegProgressiveDecoder* g_jpeg_decoder =
nullptr;
95 CHECK(!g_jpeg_decoder);
96 g_jpeg_decoder =
new JpegProgressiveDecoder();
101 delete g_jpeg_decoder;
102 g_jpeg_decoder =
nullptr;
107 return g_jpeg_decoder;
111std::unique_ptr<ProgressiveDecoderIface::Context>
112JpegProgressiveDecoder::Start() {
114 auto* pContext =
new CJpegContext
();
120 jpeg_create_decompress(&pContext->m_Info);
121 pContext->m_Info.src = &pContext->m_SrcMgr;
123 return pdfium::WrapUnique(pContext);
136 CFX_DIBAttribute* pAttribute) {
139 auto* ctx =
static_cast<CJpegContext*>(pContext);
140 int ret = jpeg_read_header(&ctx->m_Info, TRUE);
141 if (ret == JPEG_SUSPENDED)
143 if (ret != JPEG_HEADER_OK)
146 *width = ctx->m_Info.image_width;
147 *height = ctx->m_Info.image_height;
148 *nComps = ctx->m_Info.num_components;
149 JpegLoadAttribute(ctx->m_Info, pAttribute);
155 auto* ctx =
static_cast<CJpegContext*>(pContext);
156 ctx->m_Info.scale_denom =
static_cast<
unsigned int>(down_scale);
157 return !!jpeg_start_decompress(&ctx->m_Info);
162 unsigned char* dest_buf) {
163 auto* ctx =
static_cast<CJpegContext*>(pContext);
164 unsigned int nlines = jpeg_read_scanlines(&ctx->m_Info, &dest_buf, 1);
169 auto* ctx =
static_cast<CJpegContext*>(pContext);
170 return static_cast<
FX_FILESIZE>(ctx->m_SrcMgr.bytes_in_buffer);
174 RetainPtr<CFX_CodecMemory> codec_memory) {
175 pdfium::span<uint8_t> src_buf = codec_memory->GetUnconsumedSpan();
176 auto* ctx =
static_cast<CJpegContext*>(pContext);
179 ctx->m_SrcMgr.bytes_in_buffer = 0;
186 ctx->m_SrcMgr.next_input_byte = src_buf.data();
187 ctx->m_SrcMgr.bytes_in_buffer = src_buf.size();
191JpegProgressiveDecoder::JpegProgressiveDecoder() =
default;
jpeg_decompress_struct m_Info
~JpegProgressiveDecoder() override
static void InitializeGlobals()
static bool ReadScanline(Context *pContext, uint8_t *dest_buf)
static jmp_buf & GetJumpMark(Context *pContext)
static int ReadHeader(Context *pContext, int *width, int *height, int *nComps, CFX_DIBAttribute *pAttribute)
static JpegProgressiveDecoder * GetInstance()
static void DestroyGlobals()
FX_FILESIZE GetAvailInput(Context *pContext) const override
bool Input(Context *pContext, RetainPtr< CFX_CodecMemory > codec_memory) override
static bool StartScanline(Context *pContext, int down_scale)
static void JpegLoadAttribute(const jpeg_decompress_struct &info, CFX_DIBAttribute *pAttribute)
static void src_skip_data(jpeg_decompress_struct *cinfo, long num)
static void error_fatal(j_common_ptr cinfo)