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 <optional>
14 #include <utility>
15 #include <vector>
16
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"
25
26 #if !defined(USE_SYSTEM_LIBOPENJPEG2)
27 #include "third_party/libopenjpeg/opj_malloc.h"
28 #endif
29
30 namespace fxcodec {
31
32 namespace {
33
34 // Used with std::unique_ptr to call opj_image_data_free on raw memory.
35 struct OpjImageDataDeleter {
operator ()fxcodec::__anonc67eddda0111::OpjImageDataDeleter36 inline void operator()(void* ptr) const { opj_image_data_free(ptr); }
37 };
38
39 using ScopedOpjImageData = std::unique_ptr<int, OpjImageDataDeleter>;
40
41 struct OpjImageRgbData {
42 ScopedOpjImageData r;
43 ScopedOpjImageData g;
44 ScopedOpjImageData b;
45 };
46
fx_ignore_callback(const char * msg,void * client_data)47 void fx_ignore_callback(const char* msg, void* client_data) {}
48
fx_opj_stream_create_memory_stream(DecodeData * data)49 opj_stream_t* fx_opj_stream_create_memory_stream(DecodeData* data) {
50 if (!data || !data->src_data || data->src_size <= 0)
51 return nullptr;
52
53 opj_stream_t* stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,
54 /*p_is_input=*/OPJ_TRUE);
55 if (!stream)
56 return nullptr;
57
58 opj_stream_set_user_data(stream, data, nullptr);
59 opj_stream_set_user_data_length(stream, data->src_size);
60 opj_stream_set_read_function(stream, opj_read_from_memory);
61 opj_stream_set_skip_function(stream, opj_skip_from_memory);
62 opj_stream_set_seek_function(stream, opj_seek_from_memory);
63 return stream;
64 }
65
alloc_rgb(size_t size)66 std::optional<OpjImageRgbData> alloc_rgb(size_t size) {
67 OpjImageRgbData data;
68 data.r.reset(static_cast<int*>(opj_image_data_alloc(size)));
69 if (!data.r)
70 return std::nullopt;
71
72 data.g.reset(static_cast<int*>(opj_image_data_alloc(size)));
73 if (!data.g)
74 return std::nullopt;
75
76 data.b.reset(static_cast<int*>(opj_image_data_alloc(size)));
77 if (!data.b)
78 return std::nullopt;
79
80 return data;
81 }
82
sycc_to_rgb(int offset,int upb,int y,int cb,int cr,int * out_r,int * out_g,int * out_b)83 void sycc_to_rgb(int offset,
84 int upb,
85 int y,
86 int cb,
87 int cr,
88 int* out_r,
89 int* out_g,
90 int* out_b) {
91 cb -= offset;
92 cr -= 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);
96 }
97
components_span(opj_image_t * img)98 pdfium::span<opj_image_comp_t> components_span(opj_image_t* img) {
99 // SAFETY: required from third-party library.
100 return UNSAFE_BUFFERS(pdfium::make_span(img->comps, img->numcomps));
101 }
102
sycc444_to_rgb(opj_image_t * img)103 void sycc444_to_rgb(opj_image_t* img) {
104 auto components = components_span(img);
105 int prec = components[0].prec;
106 // If we shift 31 we're going to go negative, then things go bad.
107 if (prec > 30)
108 return;
109 int offset = 1 << (prec - 1);
110 int upb = (1 << prec) - 1;
111 OPJ_UINT32 maxw =
112 std::min({components[0].w, components[1].w, components[2].w});
113 OPJ_UINT32 maxh =
114 std::min({components[0].h, components[1].h, components[2].h});
115 FX_SAFE_SIZE_T max_size = maxw;
116 max_size *= maxh;
117 max_size *= sizeof(int);
118 if (!max_size.IsValid())
119 return;
120
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)
125 return;
126
127 std::optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
128 if (!data.has_value())
129 return;
130
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++));
137 }
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();
144 }
145
sycc420_422_size_is_valid(pdfium::span<opj_image_comp_t> components)146 bool 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;
151 }
152
sycc420_size_is_valid(pdfium::span<opj_image_comp_t> components)153 bool 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;
157 }
158
sycc420_must_extend_cbcr(OPJ_UINT32 y,OPJ_UINT32 cbcr)159 bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
160 return (y & 1) && (cbcr == y / 2);
161 }
162
sycc420_to_rgb(opj_image_t * img)163 void sycc420_to_rgb(opj_image_t* img) {
164 if (!img) {
165 return;
166 }
167 auto components = components_span(img);
168 if (!sycc420_size_is_valid(components)) {
169 return;
170 }
171 OPJ_UINT32 prec = components[0].prec;
172 if (!prec)
173 return;
174
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);
184 FX_SAFE_UINT32 safe_size = yw;
185 safe_size *= yh;
186 safe_size *= sizeof(int);
187 if (!safe_size.IsValid())
188 return;
189
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)
194 return;
195
196 std::optional<OpjImageRgbData> data = alloc_rgb(safe_size.ValueOrDie());
197 if (!data.has_value())
198 return;
199
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;
204 int* nr = nullptr;
205 int* ng = nullptr;
206 int* nb = nullptr;
207 OPJ_UINT32 i = 0;
208 OPJ_UINT32 j = 0;
209 UNSAFE_TODO({
210 for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
211 ny = y + yw;
212 nr = r + yw;
213 ng = g + yw;
214 nb = b + yw;
215 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
216 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
217 ++y;
218 ++r;
219 ++g;
220 ++b;
221 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
222 ++y;
223 ++r;
224 ++g;
225 ++b;
226 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
227 ++ny;
228 ++nr;
229 ++ng;
230 ++nb;
231 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
232 ++ny;
233 ++nr;
234 ++ng;
235 ++nb;
236 ++cb;
237 ++cr;
238 }
239 if (j < yw) {
240 if (extw) {
241 --cb;
242 --cr;
243 }
244 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
245 ++y;
246 ++r;
247 ++g;
248 ++b;
249 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
250 ++ny;
251 ++nr;
252 ++ng;
253 ++nb;
254 ++cb;
255 ++cr;
256 }
257 y += yw;
258 r += yw;
259 g += yw;
260 b += yw;
261 }
262 if (i < yh) {
263 if (exth) {
264 cb -= cbw;
265 cr -= crw;
266 }
267 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
268 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
269 ++y;
270 ++r;
271 ++g;
272 ++b;
273 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
274 ++y;
275 ++r;
276 ++g;
277 ++b;
278 ++cb;
279 ++cr;
280 }
281 if (j < yw) {
282 if (extw) {
283 --cb;
284 --cr;
285 }
286 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
287 }
288 }
289 });
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;
304 }
305
sycc422_size_is_valid(pdfium::span<opj_image_comp_t> components)306 bool 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;
309 }
310
sycc422_to_rgb(opj_image_t * img)311 void sycc422_to_rgb(opj_image_t* img) {
312 if (!img) {
313 return;
314 }
315 auto components = components_span(img);
316 if (!sycc422_size_is_valid(components)) {
317 return;
318 }
319 int prec = components[0].prec;
320 if (prec <= 0 || prec >= 32)
321 return;
322
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;
328 max_size *= maxh;
329 max_size *= sizeof(int);
330 if (!max_size.IsValid())
331 return;
332
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)
337 return;
338
339 std::optional<OpjImageRgbData> data = alloc_rgb(max_size.ValueOrDie());
340 if (!data.has_value())
341 return;
342
343 int* r = data.value().r.get();
344 int* g = data.value().g.get();
345 int* b = data.value().b.get();
346 UNSAFE_TODO({
347 for (uint32_t i = 0; i < maxh; ++i) {
348 OPJ_UINT32 j;
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++);
352 }
353 if (j < maxw) {
354 sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
355 }
356 }
357 });
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;
372 }
373
is_sycc420(pdfium::span<opj_image_comp_t> components)374 bool 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;
378 }
379
is_sycc422(pdfium::span<opj_image_comp_t> components)380 bool 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;
384 }
385
is_sycc444(pdfium::span<opj_image_comp_t> components)386 bool 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;
390 }
391
color_sycc_to_rgb(opj_image_t * img)392 void 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;
396 return;
397 }
398 if (is_sycc420(components)) {
399 sycc420_to_rgb(img);
400 } else if (is_sycc422(components)) {
401 sycc422_to_rgb(img);
402 } else if (is_sycc444(components)) {
403 sycc444_to_rgb(img);
404 } else {
405 return;
406 }
407 img->color_space = OPJ_CLRSPC_SRGB;
408 }
409
410 } // namespace
411
412 // static
Create(pdfium::span<const uint8_t> src_span,CJPX_Decoder::ColorSpaceOption option,uint8_t resolution_levels_to_skip,bool strict_mode)413 std::unique_ptr<CJPX_Decoder> CJPX_Decoder::Create(
414 pdfium::span<const uint8_t> src_span,
415 CJPX_Decoder::ColorSpaceOption option,
416 uint8_t resolution_levels_to_skip,
417 bool strict_mode) {
418 // Private ctor.
419 auto decoder = pdfium::WrapUnique(new CJPX_Decoder(option));
420 if (!decoder->Init(src_span, resolution_levels_to_skip, strict_mode)) {
421 return nullptr;
422 }
423 return decoder;
424 }
425
426 // static
Sycc420ToRgbForTesting(opj_image_t * img)427 void CJPX_Decoder::Sycc420ToRgbForTesting(opj_image_t* img) {
428 sycc420_to_rgb(img);
429 }
430
CJPX_Decoder(ColorSpaceOption option)431 CJPX_Decoder::CJPX_Decoder(ColorSpaceOption option)
432 : m_ColorSpaceOption(option) {}
433
434 CJPX_Decoder::~CJPX_Decoder() = default;
435
Init(pdfium::span<const uint8_t> src_data,uint8_t resolution_levels_to_skip,bool strict_mode)436 bool CJPX_Decoder::Init(pdfium::span<const uint8_t> src_data,
437 uint8_t resolution_levels_to_skip,
438 bool strict_mode) {
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) ||
442 resolution_levels_to_skip > kMaxResolutionsToSkip) {
443 return false;
444 }
445
446 m_Image.reset();
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()));
450 if (!m_Stream)
451 return false;
452
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;
460 } else {
461 m_Codec.reset(opj_create_decompress(OPJ_CODEC_J2K));
462 }
463 if (!m_Codec)
464 return false;
465
466 if (m_ColorSpaceOption == ColorSpaceOption::kIndexed) {
467 m_Parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
468 }
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)) {
473 return false;
474 }
475
476 // For https://crbug.com/42270564
477 if (!strict_mode) {
478 CHECK(opj_decoder_set_strict_mode(m_Codec.get(), false));
479 }
480
481 opj_image_t* pTempImage = nullptr;
482 if (!opj_read_header(m_Stream.get(), m_Codec.get(), &pTempImage)) {
483 return false;
484 }
485
486 m_Image.reset(pTempImage);
487 return true;
488 }
489
StartDecode()490 bool CJPX_Decoder::StartDecode() {
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)) {
495 m_Image.reset();
496 return false;
497 }
498 if (!(opj_decode(m_Codec.get(), m_Stream.get(), m_Image.get()) &&
499 opj_end_decompress(m_Codec.get(), m_Stream.get()))) {
500 m_Image.reset();
501 return false;
502 }
503 } else if (!opj_get_decoded_tile(m_Codec.get(), m_Stream.get(), m_Image.get(),
504 m_Parameters.tile_index)) {
505 return false;
506 }
507
508 m_Stream.reset();
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;
515 }
516 if (m_Image->color_space == OPJ_CLRSPC_SYCC)
517 color_sycc_to_rgb(m_Image.get());
518
519 // TODO(crbug.com/346606150): Investigate if it makes sense to use the data in
520 // `icc_profile_buf` instead of discarding it.
521 if (m_Image->icc_profile_buf) {
522 #if defined(USE_SYSTEM_LIBOPENJPEG2)
523 // Since opj_free() is an internal function within OpenJPEG, do not assume
524 // it exists and call free() here.
525 free(m_Image->icc_profile_buf);
526 #else
527 // Memory is allocated with opj_malloc() inside OpenJPEG, so call opj_free()
528 // to match that. The bundled copy of OpenJPEG is known to have opj_free().
529 opj_free(m_Image->icc_profile_buf);
530 #endif
531 m_Image->icc_profile_buf = nullptr;
532 m_Image->icc_profile_len = 0;
533 }
534 return true;
535 }
536
GetInfo() const537 CJPX_Decoder::JpxImageInfo CJPX_Decoder::GetInfo() const {
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};
542 }
543
Decode(pdfium::span<uint8_t> dest_buf,uint32_t pitch,bool swap_rgb,uint32_t component_count)544 bool CJPX_Decoder::Decode(pdfium::span<uint8_t> dest_buf,
545 uint32_t pitch,
546 bool swap_rgb,
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) {
551 // When decoding for an ARGB image, include the alpha channel in the channel
552 // count.
553 channel_count = 4;
554 }
555
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()) {
559 return false;
560 }
561
562 if (swap_rgb && channel_count < 3) {
563 return false;
564 }
565
566 // Initialize `channel_bufs` and `adjust_comps` to store information from all
567 // the channels of the JPX image. They will contain more information besides
568 // the color component data if `m_Image->numcomps` > `component_count`.
569 // Currently only the color component data is used for rendering.
570 // TODO(crbug.com/pdfium/1747): Make full use of the component information.
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;
579 if (i > 0) {
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) {
583 return false;
584 }
585 }
586 }
587 if (swap_rgb)
588 std::swap(channel_bufs[0], channel_bufs[2]);
589
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];
596 if (!comps.data)
597 continue;
598
599 // Perfomance-sensitive code below. Combining these 3 for-loops below will
600 // cause a slowdown.
601 UNSAFE_TODO({
602 const uint32_t src_offset = comps.sgnd ? 1 << (comps.prec - 1) : 0;
603 if (adjust < 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);
610 }
611 }
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);
619 }
620 }
621 } else {
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);
630 }
631 }
632 }
633 });
634 }
635 return true;
636 }
637
638 } // namespace fxcodec
639