Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cjpx_decoder.cpp
Go to the documentation of this file.
1// Copyright 2019 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxcodec/jpx/cjpx_decoder.h"
8
9#include <string.h>
10
11#include <algorithm>
12#include <limits>
13#include <utility>
14#include <vector>
15
16#include "core/fxcodec/jpx/jpx_decode_utils.h"
17#include "core/fxcrt/fx_safe_types.h"
18#include "core/fxcrt/span_util.h"
19#include "core/fxge/calculate_pitch.h"
20#include "third_party/abseil-cpp/absl/types/optional.h"
21#include "third_party/base/memory/ptr_util.h"
22
23#if !defined(USE_SYSTEM_LIBOPENJPEG2)
24#include "third_party/libopenjpeg/opj_malloc.h"
25#endif
26
27namespace fxcodec {
28
29namespace {
30
31// Used with std::unique_ptr to call opj_image_data_free on raw memory.
32struct OpjImageDataDeleter {
33 inline void operator()(void* ptr) const { opj_image_data_free(ptr); }
34};
35
36using ScopedOpjImageData = std::unique_ptr<int, OpjImageDataDeleter>;
37
38struct OpjImageRgbData {
39 ScopedOpjImageData r;
40 ScopedOpjImageData g;
41 ScopedOpjImageData b;
42};
43
44void fx_ignore_callback(const char* msg, void* client_data) {}
45
46opj_stream_t* fx_opj_stream_create_memory_stream(DecodeData* data) {
47 if (!data || !data->src_data || data->src_size <= 0)
48 return nullptr;
49
50 opj_stream_t* stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,
51 /*p_is_input=*/OPJ_TRUE);
52 if (!stream)
53 return nullptr;
54
55 opj_stream_set_user_data(stream, data, nullptr);
56 opj_stream_set_user_data_length(stream, data->src_size);
57 opj_stream_set_read_function(stream, opj_read_from_memory);
58 opj_stream_set_skip_function(stream, opj_skip_from_memory);
59 opj_stream_set_seek_function(stream, opj_seek_from_memory);
60 return stream;
61}
62
63absl::optional<OpjImageRgbData> alloc_rgb(size_t size) {
64 OpjImageRgbData data;
65 data.r.reset(static_cast<int*>(opj_image_data_alloc(size)));
66 if (!data.r)
67 return absl::nullopt;
68
69 data.g.reset(static_cast<int*>(opj_image_data_alloc(size)));
70 if (!data.g)
71 return absl::nullopt;
72
73 data.b.reset(static_cast<int*>(opj_image_data_alloc(size)));
74 if (!data.b)
75 return absl::nullopt;
76
77 return data;
78}
79
80void sycc_to_rgb(int offset,
81 int upb,
82 int y,
83 int cb,
84 int cr,
85 int* out_r,
86 int* out_g,
87 int* out_b) {
88 cb -= offset;
89 cr -= offset;
90 *out_r = std::clamp(y + static_cast<int>(1.402 * cr), 0, upb);
91 *out_g = std::clamp(y - static_cast<int>(0.344 * cb + 0.714 * cr), 0, upb);
92 *out_b = std::clamp(y + static_cast<int>(1.772 * cb), 0, upb);
93}
94
95void sycc444_to_rgb(opj_image_t* img) {
96 int prec = img->comps[0].prec;
97 // If we shift 31 we're going to go negative, then things go bad.
98 if (prec > 30)
99 return;
100 int offset = 1 << (prec - 1);
101 int upb = (1 << prec) - 1;
102 OPJ_UINT32 maxw =
103 std::min({img->comps[0].w, img->comps[1].w, img->comps[2].w});
104 OPJ_UINT32 maxh =
105 std::min({img->comps[0].h, img->comps[1].h, img->comps[2].h});
106 FX_SAFE_SIZE_T max_size = maxw;
107 max_size *= maxh;
108 max_size *= sizeof(int);
109 if (!max_size.IsValid())
110 return;
111
112 const int* y = img->comps[0].data;
113 const int* cb = img->comps[1].data;
114 const int* cr = img->comps[2].data;
115 if (!y || !cb || !cr)
116 return;
117
118 absl::optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
119 if (!data.has_value())
120 return;
121
122 int* r = data.value().r.get();
123 int* g = data.value().g.get();
124 int* b = data.value().b.get();
125 max_size /= sizeof(int);
126 for (size_t i = 0; i < max_size.ValueOrDie(); ++i)
127 sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
128
129 opj_image_data_free(img->comps[0].data);
130 opj_image_data_free(img->comps[1].data);
131 opj_image_data_free(img->comps[2].data);
132 img->comps[0].data = data.value().r.release();
133 img->comps[1].data = data.value().g.release();
134 img->comps[2].data = data.value().b.release();
135}
136
137bool sycc420_422_size_is_valid(opj_image_t* img) {
138 return img && img->comps[0].w != std::numeric_limits<OPJ_UINT32>::max() &&
139 (img->comps[0].w + 1) / 2 == img->comps[1].w &&
140 img->comps[1].w == img->comps[2].w &&
141 img->comps[1].h == img->comps[2].h;
142}
143
144bool sycc420_size_is_valid(opj_image_t* img) {
145 return sycc420_422_size_is_valid(img) &&
146 img->comps[0].h != std::numeric_limits<OPJ_UINT32>::max() &&
147 (img->comps[0].h + 1) / 2 == img->comps[1].h;
148}
149
150bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
151 return (y & 1) && (cbcr == y / 2);
152}
153
154void sycc420_to_rgb(opj_image_t* img) {
155 if (!sycc420_size_is_valid(img))
156 return;
157
158 OPJ_UINT32 prec = img->comps[0].prec;
159 if (!prec)
160 return;
161
162 OPJ_UINT32 offset = 1 << (prec - 1);
163 OPJ_UINT32 upb = (1 << prec) - 1;
164 OPJ_UINT32 yw = img->comps[0].w;
165 OPJ_UINT32 yh = img->comps[0].h;
166 OPJ_UINT32 cbw = img->comps[1].w;
167 OPJ_UINT32 cbh = img->comps[1].h;
168 OPJ_UINT32 crw = img->comps[2].w;
169 bool extw = sycc420_must_extend_cbcr(yw, cbw);
170 bool exth = sycc420_must_extend_cbcr(yh, cbh);
171 FX_SAFE_UINT32 safe_size = yw;
172 safe_size *= yh;
173 safe_size *= sizeof(int);
174 if (!safe_size.IsValid())
175 return;
176
177 const int* y = img->comps[0].data;
178 const int* cb = img->comps[1].data;
179 const int* cr = img->comps[2].data;
180 if (!y || !cb || !cr)
181 return;
182
183 absl::optional<OpjImageRgbData> data = alloc_rgb(safe_size.ValueOrDie());
184 if (!data.has_value())
185 return;
186
187 int* r = data.value().r.get();
188 int* g = data.value().g.get();
189 int* b = data.value().b.get();
190 const int* ny = nullptr;
191 int* nr = nullptr;
192 int* ng = nullptr;
193 int* nb = nullptr;
194 OPJ_UINT32 i = 0;
195 OPJ_UINT32 j = 0;
196 for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
197 ny = y + yw;
198 nr = r + yw;
199 ng = g + yw;
200 nb = b + yw;
201 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
202 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
203 ++y;
204 ++r;
205 ++g;
206 ++b;
207 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
208 ++y;
209 ++r;
210 ++g;
211 ++b;
212 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
213 ++ny;
214 ++nr;
215 ++ng;
216 ++nb;
217 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
218 ++ny;
219 ++nr;
220 ++ng;
221 ++nb;
222 ++cb;
223 ++cr;
224 }
225 if (j < yw) {
226 if (extw) {
227 --cb;
228 --cr;
229 }
230 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
231 ++y;
232 ++r;
233 ++g;
234 ++b;
235 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
236 ++ny;
237 ++nr;
238 ++ng;
239 ++nb;
240 ++cb;
241 ++cr;
242 }
243 y += yw;
244 r += yw;
245 g += yw;
246 b += yw;
247 }
248 if (i < yh) {
249 if (exth) {
250 cb -= cbw;
251 cr -= crw;
252 }
253 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
254 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
255 ++y;
256 ++r;
257 ++g;
258 ++b;
259 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
260 ++y;
261 ++r;
262 ++g;
263 ++b;
264 ++cb;
265 ++cr;
266 }
267 if (j < yw) {
268 if (extw) {
269 --cb;
270 --cr;
271 }
272 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
273 }
274 }
275
276 opj_image_data_free(img->comps[0].data);
277 opj_image_data_free(img->comps[1].data);
278 opj_image_data_free(img->comps[2].data);
279 img->comps[0].data = data.value().r.release();
280 img->comps[1].data = data.value().g.release();
281 img->comps[2].data = data.value().b.release();
282 img->comps[1].w = yw;
283 img->comps[1].h = yh;
284 img->comps[2].w = yw;
285 img->comps[2].h = yh;
286 img->comps[1].dx = img->comps[0].dx;
287 img->comps[2].dx = img->comps[0].dx;
288 img->comps[1].dy = img->comps[0].dy;
289 img->comps[2].dy = img->comps[0].dy;
290}
291
292bool sycc422_size_is_valid(opj_image_t* img) {
293 return sycc420_422_size_is_valid(img) && img->comps[0].h == img->comps[1].h;
294}
295
296void sycc422_to_rgb(opj_image_t* img) {
297 if (!sycc422_size_is_valid(img))
298 return;
299
300 int prec = img->comps[0].prec;
301 if (prec <= 0 || prec >= 32)
302 return;
303
304 int offset = 1 << (prec - 1);
305 int upb = (1 << prec) - 1;
306 OPJ_UINT32 maxw = img->comps[0].w;
307 OPJ_UINT32 maxh = img->comps[0].h;
308 FX_SAFE_SIZE_T max_size = maxw;
309 max_size *= maxh;
310 max_size *= sizeof(int);
311 if (!max_size.IsValid())
312 return;
313
314 const int* y = img->comps[0].data;
315 const int* cb = img->comps[1].data;
316 const int* cr = img->comps[2].data;
317 if (!y || !cb || !cr)
318 return;
319
320 absl::optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
321 if (!data.has_value())
322 return;
323
324 int* r = data.value().r.get();
325 int* g = data.value().g.get();
326 int* b = data.value().b.get();
327 for (uint32_t i = 0; i < maxh; ++i) {
328 OPJ_UINT32 j;
329 for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
330 sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
331 sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
332 }
333 if (j < maxw) {
334 sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
335 }
336 }
337
338 opj_image_data_free(img->comps[0].data);
339 opj_image_data_free(img->comps[1].data);
340 opj_image_data_free(img->comps[2].data);
341 img->comps[0].data = data.value().r.release();
342 img->comps[1].data = data.value().g.release();
343 img->comps[2].data = data.value().b.release();
344 img->comps[1].w = maxw;
345 img->comps[1].h = maxh;
346 img->comps[2].w = maxw;
347 img->comps[2].h = maxh;
348 img->comps[1].dx = img->comps[0].dx;
349 img->comps[2].dx = img->comps[0].dx;
350 img->comps[1].dy = img->comps[0].dy;
351 img->comps[2].dy = img->comps[0].dy;
352}
353
354bool is_sycc420(const opj_image_t* img) {
355 return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
356 img->comps[1].dx == 2 && img->comps[1].dy == 2 &&
357 img->comps[2].dx == 2 && img->comps[2].dy == 2;
358}
359
360bool is_sycc422(const opj_image_t* img) {
361 return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
362 img->comps[1].dx == 2 && img->comps[1].dy == 1 &&
363 img->comps[2].dx == 2 && img->comps[2].dy == 1;
364}
365
366bool is_sycc444(const opj_image_t* img) {
367 return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
368 img->comps[1].dx == 1 && img->comps[1].dy == 1 &&
369 img->comps[2].dx == 1 && img->comps[2].dy == 1;
370}
371
372void color_sycc_to_rgb(opj_image_t* img) {
373 if (img->numcomps < 3) {
374 img->color_space = OPJ_CLRSPC_GRAY;
375 return;
376 }
377 if (is_sycc420(img))
378 sycc420_to_rgb(img);
379 else if (is_sycc422(img))
380 sycc422_to_rgb(img);
381 else if (is_sycc444(img))
382 sycc444_to_rgb(img);
383 else
384 return;
385
386 img->color_space = OPJ_CLRSPC_SRGB;
387}
388
389} // namespace
390
391// static
392std::unique_ptr<CJPX_Decoder> CJPX_Decoder::Create(
393 pdfium::span<const uint8_t> src_span,
395 uint8_t resolution_levels_to_skip) {
396 // Private ctor.
397 auto decoder = pdfium::WrapUnique(new CJPX_Decoder(option));
398 if (!decoder->Init(src_span, resolution_levels_to_skip))
399 return nullptr;
400 return decoder;
401}
402
403// static
404void CJPX_Decoder::Sycc420ToRgbForTesting(opj_image_t* img) {
405 sycc420_to_rgb(img);
406}
407
408CJPX_Decoder::CJPX_Decoder(ColorSpaceOption option)
409 : m_ColorSpaceOption(option) {}
410
412 if (m_Codec)
413 opj_destroy_codec(m_Codec.ExtractAsDangling());
414 if (m_Stream)
415 opj_stream_destroy(m_Stream.ExtractAsDangling());
416 if (m_Image)
417 opj_image_destroy(m_Image.ExtractAsDangling());
418}
419
420bool CJPX_Decoder::Init(pdfium::span<const uint8_t> src_data,
421 uint8_t resolution_levels_to_skip) {
422 static constexpr uint8_t kJP2Header[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50,
423 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a};
424 if (src_data.size() < sizeof(kJP2Header) ||
425 resolution_levels_to_skip > kMaxResolutionsToSkip) {
426 return false;
427 }
428
429 m_Image = nullptr;
430 m_SrcData = src_data;
431 m_DecodeData = std::make_unique<DecodeData>(src_data.data(), src_data.size());
432 m_Stream = fx_opj_stream_create_memory_stream(m_DecodeData.get());
433 if (!m_Stream)
434 return false;
435
436 opj_set_default_decoder_parameters(&m_Parameters);
437 m_Parameters.decod_format = 0;
438 m_Parameters.cod_format = 3;
439 m_Parameters.cp_reduce = resolution_levels_to_skip;
440 if (memcmp(m_SrcData.data(), kJP2Header, sizeof(kJP2Header)) == 0) {
441 m_Codec = opj_create_decompress(OPJ_CODEC_JP2);
442 m_Parameters.decod_format = 1;
443 } else {
444 m_Codec = opj_create_decompress(OPJ_CODEC_J2K);
445 }
446 if (!m_Codec)
447 return false;
448
449 if (m_ColorSpaceOption == kIndexedColorSpace)
450 m_Parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
451 opj_set_info_handler(m_Codec, fx_ignore_callback, nullptr);
452 opj_set_warning_handler(m_Codec, fx_ignore_callback, nullptr);
453 opj_set_error_handler(m_Codec, fx_ignore_callback, nullptr);
454 if (!opj_setup_decoder(m_Codec, &m_Parameters))
455 return false;
456
457 m_Image = nullptr;
458 opj_image_t* pTempImage = nullptr;
459 if (!opj_read_header(m_Stream, m_Codec, &pTempImage))
460 return false;
461
462 m_Image = pTempImage;
463 return true;
464}
465
467 if (!m_Parameters.nb_tile_to_decode) {
468 if (!opj_set_decode_area(m_Codec, m_Image, m_Parameters.DA_x0,
469 m_Parameters.DA_y0, m_Parameters.DA_x1,
470 m_Parameters.DA_y1)) {
471 opj_image_destroy(m_Image.ExtractAsDangling());
472 return false;
473 }
474 if (!(opj_decode(m_Codec, m_Stream, m_Image) &&
475 opj_end_decompress(m_Codec, m_Stream))) {
476 opj_image_destroy(m_Image.ExtractAsDangling());
477 return false;
478 }
479 } else if (!opj_get_decoded_tile(m_Codec, m_Stream, m_Image,
480 m_Parameters.tile_index)) {
481 return false;
482 }
483
484 opj_stream_destroy(m_Stream.ExtractAsDangling());
485 if (m_Image->color_space != OPJ_CLRSPC_SYCC && m_Image->numcomps == 3 &&
486 m_Image->comps[0].dx == m_Image->comps[0].dy &&
487 m_Image->comps[1].dx != 1) {
488 m_Image->color_space = OPJ_CLRSPC_SYCC;
489 } else if (m_Image->numcomps <= 2) {
490 m_Image->color_space = OPJ_CLRSPC_GRAY;
491 }
492 if (m_Image->color_space == OPJ_CLRSPC_SYCC)
493 color_sycc_to_rgb(m_Image);
494
495 if (m_Image->icc_profile_buf) {
496 // TODO(palmer): Using |opj_free| here resolves the crash described in
497 // https://crbug.com/737033, but ultimately we need to harmonize the
498 // memory allocation strategy across OpenJPEG and its PDFium callers.
499#if !defined(USE_SYSTEM_LIBOPENJPEG2)
500 opj_free(m_Image->icc_profile_buf);
501#else
502 free(m_Image->icc_profile_buf);
503#endif
504 m_Image->icc_profile_buf = nullptr;
505 m_Image->icc_profile_len = 0;
506 }
507 return true;
508}
509
511 return {m_Image->comps[0].w, m_Image->comps[0].h, m_Image->numcomps,
512 m_Image->color_space};
513}
514
515bool CJPX_Decoder::Decode(pdfium::span<uint8_t> dest_buf,
516 uint32_t pitch,
517 bool swap_rgb,
518 uint32_t component_count) {
519 CHECK_LE(component_count, m_Image->numcomps);
520 uint32_t channel_count = component_count;
521 if (channel_count == 3 && m_Image->numcomps == 4) {
522 // When decoding for an ARGB image, include the alpha channel in the channel
523 // count.
524 channel_count = 4;
525 }
526
527 absl::optional<uint32_t> calculated_pitch =
528 fxge::CalculatePitch32(8 * channel_count, m_Image->comps[0].w);
529 if (!calculated_pitch.has_value() || pitch < calculated_pitch.value()) {
530 return false;
531 }
532
533 if (swap_rgb && channel_count < 3) {
534 return false;
535 }
536
537 // Initialize `channel_bufs` and `adjust_comps` to store information from all
538 // the channels of the JPX image. They will contain more information besides
539 // the color component data if `m_Image->numcomps` > `component_count`.
540 // Currently only the color component data is used for rendering.
541 // TODO(crbug.com/pdfium/1747): Make full use of the component information.
542 fxcrt::spanset(dest_buf.first(m_Image->comps[0].h * pitch), 0xff);
543 std::vector<uint8_t*> channel_bufs(m_Image->numcomps);
544 std::vector<int> adjust_comps(m_Image->numcomps);
545 for (uint32_t i = 0; i < m_Image->numcomps; i++) {
546 channel_bufs[i] = dest_buf.subspan(i).data();
547 adjust_comps[i] = m_Image->comps[i].prec - 8;
548 if (i > 0) {
549 if (m_Image->comps[i].dx != m_Image->comps[i - 1].dx ||
550 m_Image->comps[i].dy != m_Image->comps[i - 1].dy ||
551 m_Image->comps[i].prec != m_Image->comps[i - 1].prec) {
552 return false;
553 }
554 }
555 }
556 if (swap_rgb)
557 std::swap(channel_bufs[0], channel_bufs[2]);
558
559 uint32_t width = m_Image->comps[0].w;
560 uint32_t height = m_Image->comps[0].h;
561 for (uint32_t channel = 0; channel < channel_count; ++channel) {
562 uint8_t* pChannel = channel_bufs[channel];
563 const int adjust = adjust_comps[channel];
564 const opj_image_comp_t& comps = m_Image->comps[channel];
565 if (!comps.data)
566 continue;
567
568 // Perfomance-sensitive code below. Combining these 3 for-loops below will
569 // cause a slowdown.
570 const uint32_t src_offset = comps.sgnd ? 1 << (comps.prec - 1) : 0;
571 if (adjust < 0) {
572 for (uint32_t row = 0; row < height; ++row) {
573 uint8_t* pScanline = pChannel + row * pitch;
574 for (uint32_t col = 0; col < width; ++col) {
575 uint8_t* pPixel = pScanline + col * channel_count;
576 int src = comps.data[row * width + col] + src_offset;
577 *pPixel = static_cast<uint8_t>(src << -adjust);
578 }
579 }
580 } else if (adjust == 0) {
581 for (uint32_t row = 0; row < height; ++row) {
582 uint8_t* pScanline = pChannel + row * pitch;
583 for (uint32_t col = 0; col < width; ++col) {
584 uint8_t* pPixel = pScanline + col * channel_count;
585 int src = comps.data[row * width + col] + src_offset;
586 *pPixel = static_cast<uint8_t>(src);
587 }
588 }
589 } else {
590 for (uint32_t row = 0; row < height; ++row) {
591 uint8_t* pScanline = pChannel + row * pitch;
592 for (uint32_t col = 0; col < width; ++col) {
593 uint8_t* pPixel = pScanline + col * channel_count;
594 int src = comps.data[row * width + col] + src_offset;
595 int pixel = (src >> adjust) + ((src >> (adjust - 1)) % 2);
596 pixel = std::clamp(pixel, 0, 255);
597 *pPixel = static_cast<uint8_t>(pixel);
598 }
599 }
600 }
601 }
602 return true;
603}
604
605} // namespace fxcodec
ScopedOpjImageData r
ScopedOpjImageData g
ScopedOpjImageData b
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
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)
const uint8_t * src_data