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