7#include "core/fxcodec/jpx/cjpx_decoder.h"
17#include "core/fxcodec/jpx/jpx_decode_utils.h"
18#include "core/fxcrt/check_op.h"
19#include "core/fxcrt/fx_safe_types.h"
20#include "core/fxcrt/numerics/safe_conversions.h"
21#include "core/fxcrt/ptr_util.h"
22#include "core/fxcrt/span.h"
23#include "core/fxcrt/stl_util.h"
24#include "core/fxge/calculate_pitch.h"
26#if !defined(USE_SYSTEM_LIBOPENJPEG2)
27#include "third_party/libopenjpeg/opj_malloc.h"
35struct OpjImageDataDeleter {
36 inline void operator()(
void* ptr)
const { opj_image_data_free(ptr); }
39using ScopedOpjImageData =
std::unique_ptr<
int, OpjImageDataDeleter>;
41struct OpjImageRgbData {
47void fx_ignore_callback(
const char* msg,
void* client_data) {}
49opj_stream_t* fx_opj_stream_create_memory_stream(
DecodeData* data) {
53 opj_stream_t* stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,
58 opj_stream_set_user_data(stream, data,
nullptr);
59 opj_stream_set_user_data_length(stream, data
->src_size);
66std::optional<OpjImageRgbData> alloc_rgb(size_t size) {
68 data.r.reset(
static_cast<
int*>(opj_image_data_alloc(size)));
72 data.g.reset(
static_cast<
int*>(opj_image_data_alloc(size)));
76 data.b.reset(
static_cast<
int*>(opj_image_data_alloc(size)));
83void sycc_to_rgb(
int offset,
93 *out_r =
std::clamp(y +
static_cast<
int>(1.402 * cr), 0, upb);
94 *out_g =
std::clamp(y -
static_cast<
int>(0.344 * cb + 0.714 * cr), 0, upb);
95 *out_b =
std::clamp(y +
static_cast<
int>(1.772 * cb), 0, upb);
98pdfium::span<opj_image_comp_t> components_span(opj_image_t* img) {
103void sycc444_to_rgb(opj_image_t* img) {
104 auto components = components_span(img);
105 int prec = components[0].prec;
109 int offset = 1 << (prec - 1);
110 int upb = (1 << prec) - 1;
112 std::min({components[0].w, components[1].w, components[2].w});
114 std::min({components[0].h, components[1].h, components[2].h});
115 FX_SAFE_SIZE_T max_size = maxw;
117 max_size *=
sizeof(
int);
118 if (!max_size.IsValid())
121 const int* y = components[0].data;
122 const int* cb = components[1].data;
123 const int* cr = components[2].data;
124 if (!y || !cb || !cr)
127 std::optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
128 if (!data.has_value())
131 int* r = data.value().r.get();
132 int* g = data.value().g.get();
133 int* b = data.value().b.get();
134 max_size /=
sizeof(
int);
135 for (size_t i = 0; i < max_size.ValueOrDie(); ++i) {
136 UNSAFE_TODO(sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++));
138 opj_image_data_free(components[0].data);
139 opj_image_data_free(components[1].data);
140 opj_image_data_free(components[2].data);
141 components[0].data = data.value().r.release();
142 components[1].data = data.value().g.release();
143 components[2].data = data.value().b.release();
146bool sycc420_422_size_is_valid(pdfium::span<opj_image_comp_t> components) {
147 return components[0].w !=
std::numeric_limits<OPJ_UINT32>::max() &&
148 (components[0].w + 1) / 2 == components[1].w &&
149 components[1].w == components[2].w &&
150 components[1].h == components[2].h;
153bool sycc420_size_is_valid(pdfium::span<opj_image_comp_t> components) {
154 return sycc420_422_size_is_valid(components) &&
155 components[0].h != std::numeric_limits<OPJ_UINT32>::max() &&
156 (components[0].h + 1) / 2 == components[1].h;
159bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
160 return (y & 1) && (cbcr == y / 2);
163void sycc420_to_rgb(opj_image_t* img) {
167 auto components = components_span(img);
168 if (!sycc420_size_is_valid(components)) {
171 OPJ_UINT32 prec = components[0].prec;
175 OPJ_UINT32 offset = 1 << (prec - 1);
176 OPJ_UINT32 upb = (1 << prec) - 1;
177 OPJ_UINT32 yw = components[0].w;
178 OPJ_UINT32 yh = components[0].h;
179 OPJ_UINT32 cbw = components[1].w;
180 OPJ_UINT32 cbh = components[1].h;
181 OPJ_UINT32 crw = components[2].w;
182 bool extw = sycc420_must_extend_cbcr(yw, cbw);
183 bool exth = sycc420_must_extend_cbcr(yh, cbh);
186 safe_size *=
sizeof(
int);
187 if (!safe_size.IsValid())
190 const int* y = components[0].data;
191 const int* cb = components[1].data;
192 const int* cr = components[2].data;
193 if (!y || !cb || !cr)
196 std::optional<OpjImageRgbData> data = alloc_rgb(safe_size.ValueOrDie());
197 if (!data.has_value())
200 int* r = data.value().r.get();
201 int* g = data.value().g.get();
202 int* b = data.value().b.get();
203 const int* ny =
nullptr;
210 for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
215 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
216 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
221 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
226 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
231 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
244 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
249 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
267 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
268 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
273 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
286 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
290 opj_image_data_free(components[0].data);
291 opj_image_data_free(components[1].data);
292 opj_image_data_free(components[2].data);
293 components[0].data = data.value().r.release();
294 components[1].data = data.value().g.release();
295 components[2].data = data.value().b.release();
296 components[1].w = yw;
297 components[1].h = yh;
298 components[2].w = yw;
299 components[2].h = yh;
300 components[1].dx = components[0].dx;
301 components[2].dx = components[0].dx;
302 components[1].dy = components[0].dy;
303 components[2].dy = components[0].dy;
306bool sycc422_size_is_valid(pdfium::span<opj_image_comp_t> components) {
307 return sycc420_422_size_is_valid(components) &&
308 components[0].h == components[1].h;
311void sycc422_to_rgb(opj_image_t* img) {
315 auto components = components_span(img);
316 if (!sycc422_size_is_valid(components)) {
319 int prec = components[0].prec;
320 if (prec <= 0 || prec >= 32)
323 int offset = 1 << (prec - 1);
324 int upb = (1 << prec) - 1;
325 OPJ_UINT32 maxw = components[0].w;
326 OPJ_UINT32 maxh = components[0].h;
327 FX_SAFE_SIZE_T max_size = maxw;
329 max_size *=
sizeof(
int);
330 if (!max_size.IsValid())
333 const int* y = components[0].data;
334 const int* cb = components[1].data;
335 const int* cr = components[2].data;
336 if (!y || !cb || !cr)
339 std::optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
340 if (!data.has_value())
343 int* r = data.value().r.get();
344 int* g = data.value().g.get();
345 int* b = data.value().b.get();
347 for (uint32_t i = 0; i < maxh; ++i) {
349 for (j = 0; j < (maxw & ~
static_cast<OPJ_UINT32>(1)); j += 2) {
350 sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
351 sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
354 sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
358 opj_image_data_free(components[0].data);
359 opj_image_data_free(components[1].data);
360 opj_image_data_free(components[2].data);
361 components[0].data = data.value().r.release();
362 components[1].data = data.value().g.release();
363 components[2].data = data.value().b.release();
364 components[1].w = maxw;
365 components[1].h = maxh;
366 components[2].w = maxw;
367 components[2].h = maxh;
368 components[1].dx = components[0].dx;
369 components[2].dx = components[0].dx;
370 components[1].dy = components[0].dy;
371 components[2].dy = components[0].dy;
374bool is_sycc420(pdfium::span<opj_image_comp_t> components) {
375 return components[0].dx == 1 && components[0].dy == 1 &&
376 components[1].dx == 2 && components[1].dy == 2 &&
377 components[2].dx == 2 && components[2].dy == 2;
380bool is_sycc422(pdfium::span<opj_image_comp_t> components) {
381 return components[0].dx == 1 && components[0].dy == 1 &&
382 components[1].dx == 2 && components[1].dy == 1 &&
383 components[2].dx == 2 && components[2].dy == 1;
386bool is_sycc444(pdfium::span<opj_image_comp_t> components) {
387 return components[0].dx == 1 && components[0].dy == 1 &&
388 components[1].dx == 1 && components[1].dy == 1 &&
389 components[2].dx == 1 && components[2].dy == 1;
392void color_sycc_to_rgb(opj_image_t* img) {
393 auto components = components_span(img);
394 if (components.size() < 3) {
395 img->color_space = OPJ_CLRSPC_GRAY;
398 if (is_sycc420(components)) {
400 }
else if (is_sycc422(components)) {
402 }
else if (is_sycc444(components)) {
407 img->color_space = OPJ_CLRSPC_SRGB;
414 pdfium::span<
const uint8_t> src_span,
416 uint8_t resolution_levels_to_skip,
420 if (!decoder->Init(src_span, resolution_levels_to_skip, strict_mode)) {
432 : m_ColorSpaceOption(option) {}
436bool CJPX_Decoder::Init(pdfium::span<
const uint8_t> src_data,
437 uint8_t resolution_levels_to_skip,
439 static constexpr uint8_t kJP2Header[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50,
440 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a};
441 if (src_data.size() <
sizeof(kJP2Header) ||
447 m_SrcData = src_data;
448 m_DecodeData = std::make_unique<DecodeData>(src_data);
449 m_Stream.reset(fx_opj_stream_create_memory_stream(m_DecodeData.get()));
453 opj_set_default_decoder_parameters(&m_Parameters);
454 m_Parameters.decod_format = 0;
455 m_Parameters.cod_format = 3;
456 m_Parameters.cp_reduce = resolution_levels_to_skip;
457 if (memcmp(m_SrcData.data(), kJP2Header,
sizeof(kJP2Header)) == 0) {
458 m_Codec.reset(opj_create_decompress(OPJ_CODEC_JP2));
459 m_Parameters.decod_format = 1;
461 m_Codec.reset(opj_create_decompress(OPJ_CODEC_J2K));
467 m_Parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
469 opj_set_info_handler(m_Codec.get(), fx_ignore_callback,
nullptr);
470 opj_set_warning_handler(m_Codec.get(), fx_ignore_callback,
nullptr);
471 opj_set_error_handler(m_Codec.get(), fx_ignore_callback,
nullptr);
472 if (!opj_setup_decoder(m_Codec.get(), &m_Parameters)) {
478 CHECK(opj_decoder_set_strict_mode(m_Codec.get(),
false));
481 opj_image_t* pTempImage =
nullptr;
482 if (!opj_read_header(m_Stream.get(), m_Codec.get(), &pTempImage)) {
486 m_Image.reset(pTempImage);
491 if (!m_Parameters.nb_tile_to_decode) {
492 if (!opj_set_decode_area(m_Codec.get(), m_Image.get(), m_Parameters.DA_x0,
493 m_Parameters.DA_y0, m_Parameters.DA_x1,
494 m_Parameters.DA_y1)) {
498 if (!(opj_decode(m_Codec.get(), m_Stream.get(), m_Image.get()) &&
499 opj_end_decompress(m_Codec.get(), m_Stream.get()))) {
503 }
else if (!opj_get_decoded_tile(m_Codec.get(), m_Stream.get(), m_Image.get(),
504 m_Parameters.tile_index)) {
509 auto components = components_span(m_Image.get());
510 if (m_Image->color_space != OPJ_CLRSPC_SYCC && components.size() == 3 &&
511 components[0].dx == components[0].dy && components[1].dx != 1) {
512 m_Image->color_space = OPJ_CLRSPC_SYCC;
513 }
else if (m_Image->numcomps <= 2) {
514 m_Image->color_space = OPJ_CLRSPC_GRAY;
516 if (m_Image->color_space == OPJ_CLRSPC_SYCC)
517 color_sycc_to_rgb(m_Image.get());
521 if (m_Image->icc_profile_buf) {
522#if defined(USE_SYSTEM_LIBOPENJPEG2)
525 free(m_Image->icc_profile_buf);
529 opj_free(m_Image->icc_profile_buf);
531 m_Image->icc_profile_buf =
nullptr;
532 m_Image->icc_profile_len = 0;
538 const auto components = components_span(m_Image.get());
539 return {components[0].w, components[0].h,
540 pdfium::checked_cast<uint32_t>(components.size()),
541 m_Image->color_space};
547 uint32_t component_count) {
548 CHECK_LE(component_count, m_Image->numcomps);
549 uint32_t channel_count = component_count;
550 if (channel_count == 3 && m_Image->numcomps == 4) {
556 std::optional<uint32_t> calculated_pitch =
557 fxge::CalculatePitch32(8 * channel_count, m_Image->comps[0].w);
558 if (!calculated_pitch.has_value() || pitch < calculated_pitch.value()) {
562 if (swap_rgb && channel_count < 3) {
571 fxcrt::Fill(dest_buf.first(m_Image->comps[0].h * pitch), 0xff);
572 std::vector<uint8_t*> channel_bufs(m_Image->numcomps);
573 std::vector<
int> adjust_comps(m_Image->numcomps);
574 const pdfium::span<opj_image_comp_t> components =
575 components_span(m_Image.get());
576 for (size_t i = 0; i < components.size(); i++) {
577 channel_bufs[i] = dest_buf.subspan(i).data();
578 adjust_comps[i] = components[i].prec - 8;
580 if (components[i].dx != components[i - 1].dx ||
581 components[i].dy != components[i - 1].dy ||
582 components[i].prec != components[i - 1].prec) {
588 std::swap(channel_bufs[0], channel_bufs[2]);
590 uint32_t width = components[0].w;
591 uint32_t height = components[0].h;
592 for (uint32_t channel = 0; channel < channel_count; ++channel) {
593 uint8_t* pChannel = channel_bufs[channel];
594 const int adjust = adjust_comps[channel];
595 const opj_image_comp_t& comps = components[channel];
602 const uint32_t src_offset = comps.sgnd ? 1 << (comps.prec - 1) : 0;
604 for (uint32_t row = 0; row < height; ++row) {
605 uint8_t* pScanline = pChannel + row * pitch;
606 for (uint32_t col = 0; col < width; ++col) {
607 uint8_t* pPixel = pScanline + col * channel_count;
608 int src = comps.data[row * width + col] + src_offset;
609 *pPixel =
static_cast<uint8_t>(src << -adjust);
612 }
else if (adjust == 0) {
613 for (uint32_t row = 0; row < height; ++row) {
614 uint8_t* pScanline = pChannel + row * pitch;
615 for (uint32_t col = 0; col < width; ++col) {
616 uint8_t* pPixel = pScanline + col * channel_count;
617 int src = comps.data[row * width + col] + src_offset;
618 *pPixel =
static_cast<uint8_t>(src);
622 for (uint32_t row = 0; row < height; ++row) {
623 uint8_t* pScanline = pChannel + row * pitch;
624 for (uint32_t col = 0; col < width; ++col) {
625 uint8_t* pPixel = pScanline + col * channel_count;
626 int src = comps.data[row * width + col] + src_offset;
627 int pixel = (src >> adjust) + ((src >> (adjust - 1)) % 2);
628 pixel =
std::clamp(pixel, 0, 255);
629 *pPixel =
static_cast<uint8_t>(pixel);
static void Sycc420ToRgbForTesting(opj_image_t *img)
static constexpr uint8_t kMaxResolutionsToSkip
bool Decode(pdfium::span< uint8_t > dest_buf, uint32_t pitch, bool swap_rgb, uint32_t component_count)
JpxImageInfo GetInfo() const
#define UNSAFE_BUFFERS(...)
pdfium::CheckedNumeric< uint32_t > FX_SAFE_UINT32
OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void *p_user_data)
OPJ_SIZE_T opj_read_from_memory(void *p_buffer, OPJ_SIZE_T nb_bytes, void *p_user_data)
OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void *p_user_data)