1 // Copyright 2014 PDFium Authors. All rights reserved.
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 <algorithm>
8 #include <limits>
9 #include <vector>
10
11 #include "codec_int.h"
12 #include "core/include/fpdfapi/fpdf_resource.h"
13 #include "core/include/fxcodec/fx_codec.h"
14 #include "core/include/fxcrt/fx_safe_types.h"
15 #include "third_party/lcms2-2.6/include/lcms2.h"
16 #include "third_party/libopenjpeg20/openjpeg.h"
17
fx_error_callback(const char * msg,void * client_data)18 static void fx_error_callback(const char* msg, void* client_data) {
19 (void)client_data;
20 }
fx_warning_callback(const char * msg,void * client_data)21 static void fx_warning_callback(const char* msg, void* client_data) {
22 (void)client_data;
23 }
fx_info_callback(const char * msg,void * client_data)24 static void fx_info_callback(const char* msg, void* client_data) {
25 (void)client_data;
26 }
opj_read_from_memory(void * p_buffer,OPJ_SIZE_T nb_bytes,void * p_user_data)27 OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
28 OPJ_SIZE_T nb_bytes,
29 void* p_user_data) {
30 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
31 if (!srcData || !srcData->src_data || srcData->src_size == 0) {
32 return -1;
33 }
34 // Reads at EOF return an error code.
35 if (srcData->offset >= srcData->src_size) {
36 return -1;
37 }
38 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
39 OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
40 memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength);
41 srcData->offset += readlength;
42 return readlength;
43 }
opj_write_from_memory(void * p_buffer,OPJ_SIZE_T nb_bytes,void * p_user_data)44 OPJ_SIZE_T opj_write_from_memory(void* p_buffer,
45 OPJ_SIZE_T nb_bytes,
46 void* p_user_data) {
47 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
48 if (!srcData || !srcData->src_data || srcData->src_size == 0) {
49 return -1;
50 }
51 // Writes at EOF return an error code.
52 if (srcData->offset >= srcData->src_size) {
53 return -1;
54 }
55 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
56 OPJ_SIZE_T writeLength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
57 memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength);
58 srcData->offset += writeLength;
59 return writeLength;
60 }
opj_skip_from_memory(OPJ_OFF_T nb_bytes,void * p_user_data)61 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
62 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
63 if (!srcData || !srcData->src_data || srcData->src_size == 0) {
64 return -1;
65 }
66 // Offsets are signed and may indicate a negative skip. Do not support this
67 // because of the strange return convention where either bytes skipped or
68 // -1 is returned. Following that convention, a successful relative seek of
69 // -1 bytes would be required to to give the same result as the error case.
70 if (nb_bytes < 0) {
71 return -1;
72 }
73 // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd.
74 uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes);
75 // Additionally, the offset may take us beyond the range of a size_t (e.g.
76 // 32-bit platforms). If so, just clamp at EOF.
77 if (unsignedNbBytes >
78 std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) {
79 srcData->offset = srcData->src_size;
80 } else {
81 OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(unsignedNbBytes);
82 // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
83 // clamping at EOF. We can get away with this since we don't actually
84 // provide negative relative skips from beyond EOF back to inside the
85 // data, which would be the only reason to need to know exactly how far
86 // beyond EOF we are.
87 srcData->offset =
88 std::min(srcData->offset + checkedNbBytes, srcData->src_size);
89 }
90 return nb_bytes;
91 }
opj_seek_from_memory(OPJ_OFF_T nb_bytes,void * p_user_data)92 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
93 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
94 if (!srcData || !srcData->src_data || srcData->src_size == 0) {
95 return OPJ_FALSE;
96 }
97 // Offsets are signed and may indicate a negative position, which would
98 // be before the start of the file. Do not support this.
99 if (nb_bytes < 0) {
100 return OPJ_FALSE;
101 }
102 // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd.
103 uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes);
104 // Additionally, the offset may take us beyond the range of a size_t (e.g.
105 // 32-bit platforms). If so, just clamp at EOF.
106 if (unsignedNbBytes > std::numeric_limits<OPJ_SIZE_T>::max()) {
107 srcData->offset = srcData->src_size;
108 } else {
109 OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(nb_bytes);
110 // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
111 // again clamping at EOF.
112 srcData->offset = std::min(checkedNbBytes, srcData->src_size);
113 }
114 return OPJ_TRUE;
115 }
fx_opj_stream_create_memory_stream(DecodeData * data,OPJ_SIZE_T p_size,OPJ_BOOL p_is_read_stream)116 opj_stream_t* fx_opj_stream_create_memory_stream(DecodeData* data,
117 OPJ_SIZE_T p_size,
118 OPJ_BOOL p_is_read_stream) {
119 opj_stream_t* l_stream = 00;
120 if (!data || !data->src_data || data->src_size <= 0) {
121 return NULL;
122 }
123 l_stream = opj_stream_create(p_size, p_is_read_stream);
124 if (!l_stream) {
125 return NULL;
126 }
127 opj_stream_set_user_data(l_stream, data, NULL);
128 opj_stream_set_user_data_length(l_stream, data->src_size);
129 opj_stream_set_read_function(l_stream, opj_read_from_memory);
130 opj_stream_set_write_function(l_stream, opj_write_from_memory);
131 opj_stream_set_skip_function(l_stream, opj_skip_from_memory);
132 opj_stream_set_seek_function(l_stream, opj_seek_from_memory);
133 return l_stream;
134 }
sycc_to_rgb(int offset,int upb,int y,int cb,int cr,int * out_r,int * out_g,int * out_b)135 static void sycc_to_rgb(int offset,
136 int upb,
137 int y,
138 int cb,
139 int cr,
140 int* out_r,
141 int* out_g,
142 int* out_b) {
143 int r, g, b;
144 cb -= offset;
145 cr -= offset;
146 r = y + (int)(1.402 * (float)cr);
147 if (r < 0) {
148 r = 0;
149 } else if (r > upb) {
150 r = upb;
151 }
152 *out_r = r;
153 g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
154 if (g < 0) {
155 g = 0;
156 } else if (g > upb) {
157 g = upb;
158 }
159 *out_g = g;
160 b = y + (int)(1.772 * (float)cb);
161 if (b < 0) {
162 b = 0;
163 } else if (b > upb) {
164 b = upb;
165 }
166 *out_b = b;
167 }
sycc444_to_rgb(opj_image_t * img)168 static void sycc444_to_rgb(opj_image_t* img) {
169 int prec = img->comps[0].prec;
170 int offset = 1 << (prec - 1);
171 int upb = (1 << prec) - 1;
172 OPJ_UINT32 maxw =
173 std::min(std::min(img->comps[0].w, img->comps[1].w), img->comps[2].w);
174 OPJ_UINT32 maxh =
175 std::min(std::min(img->comps[0].h, img->comps[1].h), img->comps[2].h);
176 FX_SAFE_SIZE_T max_size = maxw;
177 max_size *= maxh;
178 if (!max_size.IsValid())
179 return;
180
181 const int* y = img->comps[0].data;
182 const int* cb = img->comps[1].data;
183 const int* cr = img->comps[2].data;
184 int *d0, *d1, *d2, *r, *g, *b;
185 d0 = r = FX_Alloc(int, max_size.ValueOrDie());
186 d1 = g = FX_Alloc(int, max_size.ValueOrDie());
187 d2 = b = FX_Alloc(int, max_size.ValueOrDie());
188 for (size_t i = 0; i < max_size.ValueOrDie(); ++i) {
189 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
190 ++y;
191 ++cb;
192 ++cr;
193 ++r;
194 ++g;
195 ++b;
196 }
197 FX_Free(img->comps[0].data);
198 img->comps[0].data = d0;
199 FX_Free(img->comps[1].data);
200 img->comps[1].data = d1;
201 FX_Free(img->comps[2].data);
202 img->comps[2].data = d2;
203 }
sycc422_to_rgb(opj_image_t * img)204 static void sycc422_to_rgb(opj_image_t* img) {
205 int prec = img->comps[0].prec;
206 int offset = 1 << (prec - 1);
207 int upb = (1 << prec) - 1;
208 OPJ_UINT32 maxw =
209 std::min(std::min(img->comps[0].w, img->comps[1].w), img->comps[2].w);
210 OPJ_UINT32 maxh =
211 std::min(std::min(img->comps[0].h, img->comps[1].h), img->comps[2].h);
212 FX_SAFE_SIZE_T max_size = maxw;
213 max_size *= maxh;
214 if (!max_size.IsValid())
215 return;
216
217 const int* y = img->comps[0].data;
218 const int* cb = img->comps[1].data;
219 const int* cr = img->comps[2].data;
220 int *d0, *d1, *d2, *r, *g, *b;
221 d0 = r = FX_Alloc(int, max_size.ValueOrDie());
222 d1 = g = FX_Alloc(int, max_size.ValueOrDie());
223 d2 = b = FX_Alloc(int, max_size.ValueOrDie());
224 for (uint32_t i = 0; i < maxh; ++i) {
225 OPJ_UINT32 j;
226 for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
227 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
228 ++y;
229 ++r;
230 ++g;
231 ++b;
232 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
233 ++y;
234 ++r;
235 ++g;
236 ++b;
237 ++cb;
238 ++cr;
239 }
240 if (j < maxw) {
241 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
242 ++y;
243 ++r;
244 ++g;
245 ++b;
246 ++cb;
247 ++cr;
248 }
249 }
250 FX_Free(img->comps[0].data);
251 img->comps[0].data = d0;
252 FX_Free(img->comps[1].data);
253 img->comps[1].data = d1;
254 FX_Free(img->comps[2].data);
255 img->comps[2].data = d2;
256 img->comps[1].w = maxw;
257 img->comps[1].h = maxh;
258 img->comps[2].w = maxw;
259 img->comps[2].h = maxh;
260 img->comps[1].dx = img->comps[0].dx;
261 img->comps[2].dx = img->comps[0].dx;
262 img->comps[1].dy = img->comps[0].dy;
263 img->comps[2].dy = img->comps[0].dy;
264 }
sycc420_size_is_valid(OPJ_UINT32 y,OPJ_UINT32 cbcr)265 static bool sycc420_size_is_valid(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
266 if (!y || !cbcr)
267 return false;
268
269 return (cbcr == y / 2) || ((y & 1) && (cbcr == y / 2 + 1));
270 }
sycc420_must_extend_cbcr(OPJ_UINT32 y,OPJ_UINT32 cbcr)271 static bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
272 return (y & 1) && (cbcr == y / 2);
273 }
sycc420_to_rgb(opj_image_t * img)274 void sycc420_to_rgb(opj_image_t* img) {
275 OPJ_UINT32 prec = img->comps[0].prec;
276 if (!prec)
277 return;
278 OPJ_UINT32 offset = 1 << (prec - 1);
279 OPJ_UINT32 upb = (1 << prec) - 1;
280 OPJ_UINT32 yw = img->comps[0].w;
281 OPJ_UINT32 yh = img->comps[0].h;
282 OPJ_UINT32 cbw = img->comps[1].w;
283 OPJ_UINT32 cbh = img->comps[1].h;
284 OPJ_UINT32 crw = img->comps[2].w;
285 OPJ_UINT32 crh = img->comps[2].h;
286 if (cbw != crw || cbh != crh)
287 return;
288 if (!sycc420_size_is_valid(yw, cbw) || !sycc420_size_is_valid(yh, cbh))
289 return;
290 bool extw = sycc420_must_extend_cbcr(yw, cbw);
291 bool exth = sycc420_must_extend_cbcr(yh, cbh);
292 FX_SAFE_DWORD safeSize = yw;
293 safeSize *= yh;
294 if (!safeSize.IsValid())
295 return;
296 int* r = FX_Alloc(int, safeSize.ValueOrDie());
297 int* g = FX_Alloc(int, safeSize.ValueOrDie());
298 int* b = FX_Alloc(int, safeSize.ValueOrDie());
299 int* d0 = r;
300 int* d1 = g;
301 int* d2 = b;
302 const int* y = img->comps[0].data;
303 const int* cb = img->comps[1].data;
304 const int* cr = img->comps[2].data;
305 const int* ny = nullptr;
306 int* nr = nullptr;
307 int* ng = nullptr;
308 int* nb = nullptr;
309 OPJ_UINT32 i = 0;
310 OPJ_UINT32 j = 0;
311 for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
312 ny = y + yw;
313 nr = r + yw;
314 ng = g + yw;
315 nb = b + yw;
316 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
317 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
318 ++y;
319 ++r;
320 ++g;
321 ++b;
322 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
323 ++y;
324 ++r;
325 ++g;
326 ++b;
327 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
328 ++ny;
329 ++nr;
330 ++ng;
331 ++nb;
332 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
333 ++ny;
334 ++nr;
335 ++ng;
336 ++nb;
337 ++cb;
338 ++cr;
339 }
340 if (j < yw) {
341 if (extw) {
342 --cb;
343 --cr;
344 }
345 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
346 ++y;
347 ++r;
348 ++g;
349 ++b;
350 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
351 ++ny;
352 ++nr;
353 ++ng;
354 ++nb;
355 ++cb;
356 ++cr;
357 }
358 y += yw;
359 r += yw;
360 g += yw;
361 b += yw;
362 }
363 if (i < yh) {
364 if (exth) {
365 cb -= cbw;
366 cr -= crw;
367 }
368 for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
369 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
370 ++y;
371 ++r;
372 ++g;
373 ++b;
374 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
375 ++y;
376 ++r;
377 ++g;
378 ++b;
379 ++cb;
380 ++cr;
381 }
382 if (j < yw) {
383 if (extw) {
384 --cb;
385 --cr;
386 }
387 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
388 }
389 }
390
391 FX_Free(img->comps[0].data);
392 img->comps[0].data = d0;
393 FX_Free(img->comps[1].data);
394 img->comps[1].data = d1;
395 FX_Free(img->comps[2].data);
396 img->comps[2].data = d2;
397 img->comps[1].w = yw;
398 img->comps[1].h = yh;
399 img->comps[2].w = yw;
400 img->comps[2].h = yh;
401 img->comps[1].w = yw;
402 img->comps[1].h = yh;
403 img->comps[2].w = yw;
404 img->comps[2].h = yh;
405 img->comps[1].dx = img->comps[0].dx;
406 img->comps[2].dx = img->comps[0].dx;
407 img->comps[1].dy = img->comps[0].dy;
408 img->comps[2].dy = img->comps[0].dy;
409 }
color_sycc_to_rgb(opj_image_t * img)410 void color_sycc_to_rgb(opj_image_t* img) {
411 if (img->numcomps < 3) {
412 img->color_space = OPJ_CLRSPC_GRAY;
413 return;
414 }
415 if ((img->comps[0].dx == 1) && (img->comps[1].dx == 2) &&
416 (img->comps[2].dx == 2) && (img->comps[0].dy == 1) &&
417 (img->comps[1].dy == 2) && (img->comps[2].dy == 2)) {
418 sycc420_to_rgb(img);
419 } else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 2) &&
420 (img->comps[2].dx == 2) && (img->comps[0].dy == 1) &&
421 (img->comps[1].dy == 1) && (img->comps[2].dy == 1)) {
422 sycc422_to_rgb(img);
423 } else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 1) &&
424 (img->comps[2].dx == 1) && (img->comps[0].dy == 1) &&
425 (img->comps[1].dy == 1) && (img->comps[2].dy == 1)) {
426 sycc444_to_rgb(img);
427 } else {
428 return;
429 }
430 img->color_space = OPJ_CLRSPC_SRGB;
431 }
color_apply_icc_profile(opj_image_t * image)432 void color_apply_icc_profile(opj_image_t* image) {
433 cmsHPROFILE out_prof;
434 cmsUInt32Number in_type;
435 cmsUInt32Number out_type;
436 int* r;
437 int* g;
438 int* b;
439 int max;
440 cmsHPROFILE in_prof =
441 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
442 if (!in_prof) {
443 return;
444 }
445 cmsColorSpaceSignature out_space = cmsGetColorSpace(in_prof);
446 cmsUInt32Number intent = cmsGetHeaderRenderingIntent(in_prof);
447 int max_w = (int)image->comps[0].w;
448 int max_h = (int)image->comps[0].h;
449 int prec = (int)image->comps[0].prec;
450 OPJ_COLOR_SPACE oldspace = image->color_space;
451 if (out_space == cmsSigRgbData) {
452 if (prec <= 8) {
453 in_type = TYPE_RGB_8;
454 out_type = TYPE_RGB_8;
455 } else {
456 in_type = TYPE_RGB_16;
457 out_type = TYPE_RGB_16;
458 }
459 out_prof = cmsCreate_sRGBProfile();
460 image->color_space = OPJ_CLRSPC_SRGB;
461 } else if (out_space == cmsSigGrayData) {
462 if (prec <= 8) {
463 in_type = TYPE_GRAY_8;
464 out_type = TYPE_RGB_8;
465 } else {
466 in_type = TYPE_GRAY_16;
467 out_type = TYPE_RGB_16;
468 }
469 out_prof = cmsCreate_sRGBProfile();
470 image->color_space = OPJ_CLRSPC_SRGB;
471 } else if (out_space == cmsSigYCbCrData) {
472 in_type = TYPE_YCbCr_16;
473 out_type = TYPE_RGB_16;
474 out_prof = cmsCreate_sRGBProfile();
475 image->color_space = OPJ_CLRSPC_SRGB;
476 } else {
477 return;
478 }
479 cmsHTRANSFORM transform =
480 cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
481 cmsCloseProfile(in_prof);
482 cmsCloseProfile(out_prof);
483 if (!transform) {
484 image->color_space = oldspace;
485 return;
486 }
487 if (image->numcomps > 2) {
488 if (prec <= 8) {
489 unsigned char *inbuf, *outbuf, *in, *out;
490 max = max_w * max_h;
491 cmsUInt32Number nr_samples = max * 3 * sizeof(unsigned char);
492 in = inbuf = FX_Alloc(unsigned char, nr_samples);
493 out = outbuf = FX_Alloc(unsigned char, nr_samples);
494 r = image->comps[0].data;
495 g = image->comps[1].data;
496 b = image->comps[2].data;
497 for (int i = 0; i < max; ++i) {
498 *in++ = (unsigned char)*r++;
499 *in++ = (unsigned char)*g++;
500 *in++ = (unsigned char)*b++;
501 }
502 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
503 r = image->comps[0].data;
504 g = image->comps[1].data;
505 b = image->comps[2].data;
506 for (int i = 0; i < max; ++i) {
507 *r++ = (int)*out++;
508 *g++ = (int)*out++;
509 *b++ = (int)*out++;
510 }
511 FX_Free(inbuf);
512 FX_Free(outbuf);
513 } else {
514 unsigned short *inbuf, *outbuf, *in, *out;
515 max = max_w * max_h;
516 cmsUInt32Number nr_samples = max * 3 * sizeof(unsigned short);
517 in = inbuf = FX_Alloc(unsigned short, nr_samples);
518 out = outbuf = FX_Alloc(unsigned short, nr_samples);
519 r = image->comps[0].data;
520 g = image->comps[1].data;
521 b = image->comps[2].data;
522 for (int i = 0; i < max; ++i) {
523 *in++ = (unsigned short)*r++;
524 *in++ = (unsigned short)*g++;
525 *in++ = (unsigned short)*b++;
526 }
527 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
528 r = image->comps[0].data;
529 g = image->comps[1].data;
530 b = image->comps[2].data;
531 for (int i = 0; i < max; ++i) {
532 *r++ = (int)*out++;
533 *g++ = (int)*out++;
534 *b++ = (int)*out++;
535 }
536 FX_Free(inbuf);
537 FX_Free(outbuf);
538 }
539 } else {
540 unsigned char *in, *inbuf, *out, *outbuf;
541 max = max_w * max_h;
542 cmsUInt32Number nr_samples =
543 (cmsUInt32Number)max * 3 * sizeof(unsigned char);
544 in = inbuf = FX_Alloc(unsigned char, nr_samples);
545 out = outbuf = FX_Alloc(unsigned char, nr_samples);
546 image->comps = (opj_image_comp_t*)realloc(
547 image->comps, (image->numcomps + 2) * sizeof(opj_image_comp_t));
548 if (image->numcomps == 2) {
549 image->comps[3] = image->comps[1];
550 }
551 image->comps[1] = image->comps[0];
552 image->comps[2] = image->comps[0];
553 image->comps[1].data = FX_Alloc(int, (size_t)max);
554 FXSYS_memset(image->comps[1].data, 0, sizeof(int) * (size_t)max);
555 image->comps[2].data = FX_Alloc(int, (size_t)max);
556 FXSYS_memset(image->comps[2].data, 0, sizeof(int) * (size_t)max);
557 image->numcomps += 2;
558 r = image->comps[0].data;
559 for (int i = 0; i < max; ++i) {
560 *in++ = (unsigned char)*r++;
561 }
562 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
563 r = image->comps[0].data;
564 g = image->comps[1].data;
565 b = image->comps[2].data;
566 for (int i = 0; i < max; ++i) {
567 *r++ = (int)*out++;
568 *g++ = (int)*out++;
569 *b++ = (int)*out++;
570 }
571 FX_Free(inbuf);
572 FX_Free(outbuf);
573 }
574 cmsDeleteTransform(transform);
575 }
color_apply_conversion(opj_image_t * image)576 void color_apply_conversion(opj_image_t* image) {
577 int* row;
578 int enumcs, numcomps;
579 numcomps = image->numcomps;
580 if (numcomps < 3) {
581 return;
582 }
583 row = (int*)image->icc_profile_buf;
584 enumcs = row[0];
585 if (enumcs == 14) {
586 int *L, *a, *b, *red, *green, *blue, *src0, *src1, *src2;
587 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
588 double minL, maxL, mina, maxa, minb, maxb;
589 unsigned int default_type;
590 unsigned int i, max;
591 cmsHPROFILE in, out;
592 cmsHTRANSFORM transform;
593 cmsUInt16Number RGB[3];
594 cmsCIELab Lab;
595 in = cmsCreateLab4Profile(NULL);
596 out = cmsCreate_sRGBProfile();
597 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
598 INTENT_PERCEPTUAL, 0);
599 cmsCloseProfile(in);
600 cmsCloseProfile(out);
601 if (!transform) {
602 return;
603 }
604 prec0 = (double)image->comps[0].prec;
605 prec1 = (double)image->comps[1].prec;
606 prec2 = (double)image->comps[2].prec;
607 default_type = row[1];
608 if (default_type == 0x44454600) {
609 rl = 100;
610 ra = 170;
611 rb = 200;
612 ol = 0;
613 oa = pow(2, prec1 - 1);
614 ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
615 } else {
616 rl = row[2];
617 ra = row[4];
618 rb = row[6];
619 ol = row[3];
620 oa = row[5];
621 ob = row[7];
622 }
623 L = src0 = image->comps[0].data;
624 a = src1 = image->comps[1].data;
625 b = src2 = image->comps[2].data;
626 max = image->comps[0].w * image->comps[0].h;
627 red = FX_Alloc(int, max);
628 image->comps[0].data = red;
629 green = FX_Alloc(int, max);
630 image->comps[1].data = green;
631 blue = FX_Alloc(int, max);
632 image->comps[2].data = blue;
633 minL = -(rl * ol) / (pow(2, prec0) - 1);
634 maxL = minL + rl;
635 mina = -(ra * oa) / (pow(2, prec1) - 1);
636 maxa = mina + ra;
637 minb = -(rb * ob) / (pow(2, prec2) - 1);
638 maxb = minb + rb;
639 for (i = 0; i < max; ++i) {
640 Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
641 ++L;
642 Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
643 ++a;
644 Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
645 ++b;
646 cmsDoTransform(transform, &Lab, RGB, 1);
647 *red++ = RGB[0];
648 *green++ = RGB[1];
649 *blue++ = RGB[2];
650 }
651 cmsDeleteTransform(transform);
652 FX_Free(src0);
653 FX_Free(src1);
654 FX_Free(src2);
655 image->color_space = OPJ_CLRSPC_SRGB;
656 image->comps[0].prec = 16;
657 image->comps[1].prec = 16;
658 image->comps[2].prec = 16;
659 return;
660 }
661 }
662 class CJPX_Decoder {
663 public:
664 explicit CJPX_Decoder(CPDF_ColorSpace* cs);
665 ~CJPX_Decoder();
666 FX_BOOL Init(const unsigned char* src_data, FX_DWORD src_size);
667 void GetInfo(FX_DWORD* width, FX_DWORD* height, FX_DWORD* components);
668 bool Decode(uint8_t* dest_buf,
669 int pitch,
670 const std::vector<uint8_t>& offsets);
671
672 private:
673 const uint8_t* m_SrcData;
674 FX_DWORD m_SrcSize;
675 opj_image_t* image;
676 opj_codec_t* l_codec;
677 opj_stream_t* l_stream;
678 const CPDF_ColorSpace* const m_ColorSpace;
679 };
680
CJPX_Decoder(CPDF_ColorSpace * cs)681 CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs)
682 : image(nullptr), l_codec(nullptr), l_stream(nullptr), m_ColorSpace(cs) {}
683
~CJPX_Decoder()684 CJPX_Decoder::~CJPX_Decoder() {
685 if (l_codec) {
686 opj_destroy_codec(l_codec);
687 }
688 if (l_stream) {
689 opj_stream_destroy(l_stream);
690 }
691 if (image) {
692 opj_image_destroy(image);
693 }
694 }
695
Init(const unsigned char * src_data,FX_DWORD src_size)696 FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, FX_DWORD src_size) {
697 static const unsigned char szJP2Header[] = {
698 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a};
699 if (!src_data || src_size < sizeof(szJP2Header))
700 return FALSE;
701
702 image = NULL;
703 m_SrcData = src_data;
704 m_SrcSize = src_size;
705 DecodeData srcData(const_cast<unsigned char*>(src_data), src_size);
706 l_stream = fx_opj_stream_create_memory_stream(&srcData,
707 OPJ_J2K_STREAM_CHUNK_SIZE, 1);
708 if (!l_stream) {
709 return FALSE;
710 }
711 opj_dparameters_t parameters;
712 opj_set_default_decoder_parameters(¶meters);
713 parameters.decod_format = 0;
714 parameters.cod_format = 3;
715 if (FXSYS_memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) {
716 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
717 parameters.decod_format = 1;
718 } else {
719 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
720 }
721 if (!l_codec) {
722 return FALSE;
723 }
724 if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED)
725 parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
726 opj_set_info_handler(l_codec, fx_info_callback, 00);
727 opj_set_warning_handler(l_codec, fx_warning_callback, 00);
728 opj_set_error_handler(l_codec, fx_error_callback, 00);
729 if (!opj_setup_decoder(l_codec, ¶meters)) {
730 return FALSE;
731 }
732 if (!opj_read_header(l_stream, l_codec, &image)) {
733 image = NULL;
734 return FALSE;
735 }
736 image->pdfium_use_colorspace = !!m_ColorSpace;
737
738 if (!parameters.nb_tile_to_decode) {
739 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0,
740 parameters.DA_x1, parameters.DA_y1)) {
741 opj_image_destroy(image);
742 image = NULL;
743 return FALSE;
744 }
745 if (!(opj_decode(l_codec, l_stream, image) &&
746 opj_end_decompress(l_codec, l_stream))) {
747 opj_image_destroy(image);
748 image = NULL;
749 return FALSE;
750 }
751 } else {
752 if (!opj_get_decoded_tile(l_codec, l_stream, image,
753 parameters.tile_index)) {
754 return FALSE;
755 }
756 }
757 opj_stream_destroy(l_stream);
758 l_stream = NULL;
759 if (image->color_space != OPJ_CLRSPC_SYCC && image->numcomps == 3 &&
760 image->comps[0].dx == image->comps[0].dy && image->comps[1].dx != 1) {
761 image->color_space = OPJ_CLRSPC_SYCC;
762 } else if (image->numcomps <= 2) {
763 image->color_space = OPJ_CLRSPC_GRAY;
764 }
765 if (image->color_space == OPJ_CLRSPC_SYCC) {
766 color_sycc_to_rgb(image);
767 }
768 if (image->icc_profile_buf) {
769 FX_Free(image->icc_profile_buf);
770 image->icc_profile_buf = NULL;
771 image->icc_profile_len = 0;
772 }
773 if (!image) {
774 return FALSE;
775 }
776 return TRUE;
777 }
778
GetInfo(FX_DWORD * width,FX_DWORD * height,FX_DWORD * components)779 void CJPX_Decoder::GetInfo(FX_DWORD* width,
780 FX_DWORD* height,
781 FX_DWORD* components) {
782 *width = (FX_DWORD)image->x1;
783 *height = (FX_DWORD)image->y1;
784 *components = (FX_DWORD)image->numcomps;
785 }
786
Decode(uint8_t * dest_buf,int pitch,const std::vector<uint8_t> & offsets)787 bool CJPX_Decoder::Decode(uint8_t* dest_buf,
788 int pitch,
789 const std::vector<uint8_t>& offsets) {
790 if (image->comps[0].w != image->x1 || image->comps[0].h != image->y1)
791 return false;
792
793 if (pitch<(int)(image->comps[0].w * 8 * image->numcomps + 31)>> 5 << 2)
794 return false;
795
796 FXSYS_memset(dest_buf, 0xff, image->y1 * pitch);
797 std::vector<uint8_t*> channel_bufs(image->numcomps);
798 std::vector<int> adjust_comps(image->numcomps);
799 for (uint32_t i = 0; i < image->numcomps; i++) {
800 channel_bufs[i] = dest_buf + offsets[i];
801 adjust_comps[i] = image->comps[i].prec - 8;
802 if (i > 0) {
803 if (image->comps[i].dx != image->comps[i - 1].dx ||
804 image->comps[i].dy != image->comps[i - 1].dy ||
805 image->comps[i].prec != image->comps[i - 1].prec) {
806 return false;
807 }
808 }
809 }
810 int width = image->comps[0].w;
811 int height = image->comps[0].h;
812 for (uint32_t channel = 0; channel < image->numcomps; ++channel) {
813 uint8_t* pChannel = channel_bufs[channel];
814 if (adjust_comps[channel] < 0) {
815 for (int row = 0; row < height; ++row) {
816 uint8_t* pScanline = pChannel + row * pitch;
817 for (int col = 0; col < width; ++col) {
818 uint8_t* pPixel = pScanline + col * image->numcomps;
819 int src = image->comps[channel].data[row * width + col];
820 src += image->comps[channel].sgnd
821 ? 1 << (image->comps[channel].prec - 1)
822 : 0;
823 if (adjust_comps[channel] > 0) {
824 *pPixel = 0;
825 } else {
826 *pPixel = (uint8_t)(src << -adjust_comps[channel]);
827 }
828 }
829 }
830 } else {
831 for (int row = 0; row < height; ++row) {
832 uint8_t* pScanline = pChannel + row * pitch;
833 for (int col = 0; col < width; ++col) {
834 uint8_t* pPixel = pScanline + col * image->numcomps;
835 if (!image->comps[channel].data) {
836 continue;
837 }
838 int src = image->comps[channel].data[row * width + col];
839 src += image->comps[channel].sgnd
840 ? 1 << (image->comps[channel].prec - 1)
841 : 0;
842 if (adjust_comps[channel] - 1 < 0) {
843 *pPixel = (uint8_t)((src >> adjust_comps[channel]));
844 } else {
845 int tmpPixel = (src >> adjust_comps[channel]) +
846 ((src >> (adjust_comps[channel] - 1)) % 2);
847 if (tmpPixel > 255) {
848 tmpPixel = 255;
849 } else if (tmpPixel < 0) {
850 tmpPixel = 0;
851 }
852 *pPixel = (uint8_t)tmpPixel;
853 }
854 }
855 }
856 }
857 }
858 return true;
859 }
860
CCodec_JpxModule()861 CCodec_JpxModule::CCodec_JpxModule() {}
~CCodec_JpxModule()862 CCodec_JpxModule::~CCodec_JpxModule() {
863 }
864
CreateDecoder(const uint8_t * src_buf,FX_DWORD src_size,CPDF_ColorSpace * cs)865 CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf,
866 FX_DWORD src_size,
867 CPDF_ColorSpace* cs) {
868 std::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(cs));
869 return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr;
870 }
871
GetImageInfo(CJPX_Decoder * pDecoder,FX_DWORD * width,FX_DWORD * height,FX_DWORD * components)872 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder,
873 FX_DWORD* width,
874 FX_DWORD* height,
875 FX_DWORD* components) {
876 pDecoder->GetInfo(width, height, components);
877 }
878
Decode(CJPX_Decoder * pDecoder,uint8_t * dest_data,int pitch,const std::vector<uint8_t> & offsets)879 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder,
880 uint8_t* dest_data,
881 int pitch,
882 const std::vector<uint8_t>& offsets) {
883 return pDecoder->Decode(dest_data, pitch, offsets);
884 }
885
DestroyDecoder(CJPX_Decoder * pDecoder)886 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) {
887 delete pDecoder;
888 }
889