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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fxcodec/fx_codec.h"
9 #include "../fx_libopenjpeg/libopenjpeg20/openjpeg.h"
10 #include "../lcms2/include/fx_lcms2.h"
11 static void fx_error_callback(const char *msg, void *client_data)
15 static void fx_warning_callback(const char *msg, void *client_data)
19 static void fx_info_callback(const char *msg, void *client_data)
24 unsigned char* src_data;
28 static OPJ_SIZE_T opj_read_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void* p_user_data)
30 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
31 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
34 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
35 OPJ_SIZE_T readlength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLength;
36 memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength);
37 srcData->offset += readlength;
40 static OPJ_SIZE_T opj_write_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void* p_user_data)
42 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
43 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
46 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
47 OPJ_SIZE_T writeLength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLength;
48 memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength);
49 srcData->offset += writeLength;
52 static OPJ_OFF_T opj_skip_from_memory (OPJ_OFF_T p_nb_bytes, void* p_user_data)
54 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
55 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
58 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
59 OPJ_SIZE_T skipLength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLength;
60 srcData->offset += skipLength;
63 static OPJ_BOOL opj_seek_from_memory (OPJ_OFF_T p_nb_bytes, void* p_user_data)
65 DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
66 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
69 if (p_nb_bytes >= srcData->src_size) {
72 srcData->offset = p_nb_bytes;
75 opj_stream_t* fx_opj_stream_create_memory_stream (DecodeData* data, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream)
77 opj_stream_t* l_stream = 00;
78 if (!data || ! data->src_data || data->src_size <= 0 ) {
81 l_stream = opj_stream_create(p_size, p_is_read_stream);
85 opj_stream_set_user_data_v3(l_stream, data, NULL);
86 opj_stream_set_user_data_length(l_stream, data->src_size);
87 opj_stream_set_read_function(l_stream, opj_read_from_memory);
88 opj_stream_set_write_function(l_stream, opj_write_from_memory);
89 opj_stream_set_skip_function(l_stream, opj_skip_from_memory);
90 opj_stream_set_seek_function(l_stream, opj_seek_from_memory);
93 static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
94 int *out_r, int *out_g, int *out_b)
99 r = y + (int)(1.402 * (float)cr);
105 g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
111 b = y + (int)(1.772 * (float)cb);
118 static void sycc444_to_rgb(opj_image_t *img)
120 int *d0, *d1, *d2, *r, *g, *b;
121 const int *y, *cb, *cr;
122 int maxw, maxh, max, i, offset, upb;
123 i = (int)img->comps[0].prec;
124 offset = 1 << (i - 1);
126 maxw = (int)img->comps[0].w;
127 maxh = (int)img->comps[0].h;
129 y = img->comps[0].data;
130 cb = img->comps[1].data;
131 cr = img->comps[2].data;
132 d0 = r = FX_Alloc(int, (size_t)max);
133 d1 = g = FX_Alloc(int, (size_t)max);
134 d2 = b = FX_Alloc(int, (size_t)max);
135 for(i = 0; i < max; ++i) {
136 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
144 FX_Free(img->comps[0].data);
145 img->comps[0].data = d0;
146 FX_Free(img->comps[1].data);
147 img->comps[1].data = d1;
148 FX_Free(img->comps[2].data);
149 img->comps[2].data = d2;
151 static void sycc422_to_rgb(opj_image_t *img)
153 int *d0, *d1, *d2, *r, *g, *b;
154 const int *y, *cb, *cr;
155 int maxw, maxh, max, offset, upb;
157 i = (int)img->comps[0].prec;
158 offset = 1 << (i - 1);
160 maxw = (int)img->comps[0].w;
161 maxh = (int)img->comps[0].h;
163 y = img->comps[0].data;
164 cb = img->comps[1].data;
165 cr = img->comps[2].data;
166 d0 = r = FX_Alloc(int, (size_t)max);
167 d1 = g = FX_Alloc(int, (size_t)max);
168 d2 = b = FX_Alloc(int, (size_t)max);
169 for(i = 0; i < maxh; ++i) {
170 for(j = 0; j < maxw; j += 2) {
171 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
176 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
185 FX_Free(img->comps[0].data);
186 img->comps[0].data = d0;
187 FX_Free(img->comps[1].data);
188 img->comps[1].data = d1;
189 FX_Free(img->comps[2].data);
190 img->comps[2].data = d2;
191 img->comps[1].w = maxw;
192 img->comps[1].h = maxh;
193 img->comps[2].w = maxw;
194 img->comps[2].h = maxh;
195 img->comps[1].w = (OPJ_UINT32)maxw;
196 img->comps[1].h = (OPJ_UINT32)maxh;
197 img->comps[2].w = (OPJ_UINT32)maxw;
198 img->comps[2].h = (OPJ_UINT32)maxh;
199 img->comps[1].dx = img->comps[0].dx;
200 img->comps[2].dx = img->comps[0].dx;
201 img->comps[1].dy = img->comps[0].dy;
202 img->comps[2].dy = img->comps[0].dy;
204 static void sycc420_to_rgb(opj_image_t *img)
206 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
207 const int *y, *cb, *cr, *ny;
208 int maxw, maxh, max, offset, upb;
210 i = (int)img->comps[0].prec;
211 offset = 1 << (i - 1);
213 maxw = (int)img->comps[0].w;
214 maxh = (int)img->comps[0].h;
216 y = img->comps[0].data;
217 cb = img->comps[1].data;
218 cr = img->comps[2].data;
219 d0 = r = FX_Alloc(int, (size_t)max);
220 d1 = g = FX_Alloc(int, (size_t)max);
221 d2 = b = FX_Alloc(int, (size_t)max);
222 for(i = 0; i < maxh; i += 2) {
227 for(j = 0; j < maxw; j += 2) {
228 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
233 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
238 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
243 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
256 FX_Free(img->comps[0].data);
257 img->comps[0].data = d0;
258 FX_Free(img->comps[1].data);
259 img->comps[1].data = d1;
260 FX_Free(img->comps[2].data);
261 img->comps[2].data = d2;
262 img->comps[1].w = maxw;
263 img->comps[1].h = maxh;
264 img->comps[2].w = maxw;
265 img->comps[2].h = maxh;
266 img->comps[1].w = (OPJ_UINT32)maxw;
267 img->comps[1].h = (OPJ_UINT32)maxh;
268 img->comps[2].w = (OPJ_UINT32)maxw;
269 img->comps[2].h = (OPJ_UINT32)maxh;
270 img->comps[1].dx = img->comps[0].dx;
271 img->comps[2].dx = img->comps[0].dx;
272 img->comps[1].dy = img->comps[0].dy;
273 img->comps[2].dy = img->comps[0].dy;
275 void color_sycc_to_rgb(opj_image_t *img)
277 if(img->numcomps < 3) {
278 img->color_space = OPJ_CLRSPC_GRAY;
281 if((img->comps[0].dx == 1)
282 && (img->comps[1].dx == 2)
283 && (img->comps[2].dx == 2)
284 && (img->comps[0].dy == 1)
285 && (img->comps[1].dy == 2)
286 && (img->comps[2].dy == 2)) {
288 } else if((img->comps[0].dx == 1)
289 && (img->comps[1].dx == 2)
290 && (img->comps[2].dx == 2)
291 && (img->comps[0].dy == 1)
292 && (img->comps[1].dy == 1)
293 && (img->comps[2].dy == 1)) {
295 } else if((img->comps[0].dx == 1)
296 && (img->comps[1].dx == 1)
297 && (img->comps[2].dx == 1)
298 && (img->comps[0].dy == 1)
299 && (img->comps[1].dy == 1)
300 && (img->comps[2].dy == 1)) {
305 img->color_space = OPJ_CLRSPC_SRGB;
307 void color_apply_icc_profile(opj_image_t *image)
309 cmsHPROFILE in_prof, out_prof;
310 cmsHTRANSFORM transform;
311 cmsColorSpaceSignature in_space, out_space;
312 cmsUInt32Number intent, in_type, out_type, nr_samples;
314 int prec, i, max, max_w, max_h;
315 OPJ_COLOR_SPACE oldspace;
317 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
318 if(in_prof == NULL) {
321 in_space = cmsGetPCS(in_prof);
322 out_space = cmsGetColorSpace(in_prof);
323 intent = cmsGetHeaderRenderingIntent(in_prof);
324 max_w = (int)image->comps[0].w;
325 max_h = (int)image->comps[0].h;
326 prec = (int)image->comps[0].prec;
327 oldspace = image->color_space;
328 if(out_space == cmsSigRgbData) {
330 in_type = TYPE_RGB_8;
331 out_type = TYPE_RGB_8;
333 in_type = TYPE_RGB_16;
334 out_type = TYPE_RGB_16;
336 out_prof = cmsCreate_sRGBProfile();
337 image->color_space = OPJ_CLRSPC_SRGB;
338 } else if(out_space == cmsSigGrayData) {
340 in_type = TYPE_GRAY_8;
341 out_type = TYPE_RGB_8;
343 in_type = TYPE_GRAY_16;
344 out_type = TYPE_RGB_16;
346 out_prof = cmsCreate_sRGBProfile();
347 image->color_space = OPJ_CLRSPC_SRGB;
348 } else if(out_space == cmsSigYCbCrData) {
349 in_type = TYPE_YCbCr_16;
350 out_type = TYPE_RGB_16;
351 out_prof = cmsCreate_sRGBProfile();
352 image->color_space = OPJ_CLRSPC_SRGB;
356 transform = cmsCreateTransform(in_prof, in_type,
357 out_prof, out_type, intent, 0);
358 cmsCloseProfile(in_prof);
359 cmsCloseProfile(out_prof);
360 if(transform == NULL) {
361 image->color_space = oldspace;
364 if(image->numcomps > 2) {
366 unsigned char *inbuf, *outbuf, *in, *out;
368 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
369 in = inbuf = FX_Alloc(unsigned char, nr_samples);
370 out = outbuf = FX_Alloc(unsigned char, nr_samples);
371 r = image->comps[0].data;
372 g = image->comps[1].data;
373 b = image->comps[2].data;
374 for(i = 0; i < max; ++i) {
375 *in++ = (unsigned char) * r++;
376 *in++ = (unsigned char) * g++;
377 *in++ = (unsigned char) * b++;
379 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
380 r = image->comps[0].data;
381 g = image->comps[1].data;
382 b = image->comps[2].data;
383 for(i = 0; i < max; ++i) {
384 *r++ = (int) * out++;
385 *g++ = (int) * out++;
386 *b++ = (int) * out++;
391 unsigned short *inbuf, *outbuf, *in, *out;
393 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
394 in = inbuf = FX_Alloc(unsigned short, nr_samples);
395 out = outbuf = FX_Alloc(unsigned short, nr_samples);
396 r = image->comps[0].data;
397 g = image->comps[1].data;
398 b = image->comps[2].data;
399 for(i = 0; i < max; ++i) {
400 *in++ = (unsigned short) * r++;
401 *in++ = (unsigned short) * g++;
402 *in++ = (unsigned short) * b++;
404 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
405 r = image->comps[0].data;
406 g = image->comps[1].data;
407 b = image->comps[2].data;
408 for(i = 0; i < max; ++i) {
409 *r++ = (int) * out++;
410 *g++ = (int) * out++;
411 *b++ = (int) * out++;
417 unsigned char *in, *inbuf, *out, *outbuf;
419 nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
420 in = inbuf = FX_Alloc(unsigned char, nr_samples);
421 out = outbuf = FX_Alloc(unsigned char, nr_samples);
422 image->comps = (opj_image_comp_t*)
423 realloc(image->comps, (image->numcomps + 2) * sizeof(opj_image_comp_t));
424 if(image->numcomps == 2) {
425 image->comps[3] = image->comps[1];
427 image->comps[1] = image->comps[0];
428 image->comps[2] = image->comps[0];
429 image->comps[1].data = FX_Alloc(int, (size_t)max);
430 FXSYS_memset8(image->comps[1].data, 0, sizeof(int) * (size_t)max);
431 image->comps[2].data = FX_Alloc(int, (size_t)max);
432 FXSYS_memset8(image->comps[2].data, 0, sizeof(int) * (size_t)max);
433 image->numcomps += 2;
434 r = image->comps[0].data;
435 for(i = 0; i < max; ++i) {
436 *in++ = (unsigned char) * r++;
438 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
439 r = image->comps[0].data;
440 g = image->comps[1].data;
441 b = image->comps[2].data;
442 for(i = 0; i < max; ++i) {
443 *r++ = (int) * out++;
444 *g++ = (int) * out++;
445 *b++ = (int) * out++;
450 cmsDeleteTransform(transform);
452 void color_apply_conversion(opj_image_t *image)
455 int enumcs, numcomps;
456 numcomps = image->numcomps;
460 row = (int*)image->icc_profile_buf;
463 int *L, *a, *b, *red, *green, *blue, *src0, *src1, *src2;
464 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
465 double minL, maxL, mina, maxa, minb, maxb;
466 unsigned int default_type, il;
467 unsigned int i, max, illu;
469 cmsHTRANSFORM transform;
470 cmsUInt16Number RGB[3];
474 in = cmsCreateLab4Profile(NULL);
475 out = cmsCreate_sRGBProfile();
477 cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
478 INTENT_PERCEPTUAL, 0);
480 cmsCloseProfile(out);
481 if(transform == NULL) {
484 prec0 = (double)image->comps[0].prec;
485 prec1 = (double)image->comps[1].prec;
486 prec2 = (double)image->comps[2].prec;
487 default_type = row[1];
488 if(default_type == 0x44454600) {
493 oa = pow(2, prec1 - 1);
494 ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
503 L = src0 = image->comps[0].data;
504 a = src1 = image->comps[1].data;
505 b = src2 = image->comps[2].data;
506 max = image->comps[0].w * image->comps[0].h;
507 red = FX_Alloc(int, max);
508 image->comps[0].data = red;
509 green = FX_Alloc(int, max);
510 image->comps[1].data = green;
511 blue = FX_Alloc(int, max);
512 image->comps[2].data = blue;
513 minL = -(rl * ol) / (pow(2, prec0) - 1);
515 mina = -(ra * oa) / (pow(2, prec1) - 1);
517 minb = -(rb * ob) / (pow(2, prec2) - 1);
519 for(i = 0; i < max; ++i) {
520 Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
522 Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
524 Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
526 cmsDoTransform(transform, &Lab, RGB, 1);
531 cmsDeleteTransform(transform);
535 image->color_space = OPJ_CLRSPC_SRGB;
536 image->comps[0].prec = 16;
537 image->comps[1].prec = 16;
538 image->comps[2].prec = 16;
542 class CJPX_Decoder : public CFX_Object
547 FX_BOOL Init(const unsigned char* src_data, int src_size);
548 void GetInfo(FX_DWORD& width, FX_DWORD& height, FX_DWORD& codestream_nComps, FX_DWORD& output_nComps);
549 FX_BOOL Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets);
550 FX_LPCBYTE m_SrcData;
553 opj_codec_t* l_codec;
554 opj_stream_t *l_stream;
555 FX_BOOL m_useColorSpace;
557 CJPX_Decoder::CJPX_Decoder(): image(NULL), l_codec(NULL), l_stream(NULL), m_useColorSpace(FALSE)
560 CJPX_Decoder::~CJPX_Decoder()
563 opj_destroy_codec(l_codec);
566 opj_stream_destroy(l_stream);
569 opj_image_destroy(image);
572 FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size)
574 static const unsigned char szJP2Header[] = { 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a };
575 if (!src_data || src_size < sizeof(szJP2Header)) {
579 m_SrcData = src_data;
580 m_SrcSize = src_size;
583 srcData.src_size = src_size;
584 srcData.src_data = const_cast<unsigned char*>(src_data);
585 l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK_SIZE, 1);
586 if (l_stream == NULL) {
589 opj_dparameters_t parameters;
590 opj_set_default_decoder_parameters(¶meters);
591 parameters.decod_format = 0;
592 parameters.cod_format = 3;
593 if(FXSYS_memcmp32(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) {
594 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
595 parameters.decod_format = 1;
597 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
602 opj_set_info_handler(l_codec, fx_info_callback, 00);
603 opj_set_warning_handler(l_codec, fx_warning_callback, 00);
604 opj_set_error_handler(l_codec, fx_error_callback, 00);
605 if ( !opj_setup_decoder(l_codec, ¶meters) ) {
608 if(! opj_read_header(l_stream, l_codec, &image)) {
612 if(this->m_useColorSpace) {
613 image->useColorSpace = 1;
615 image->useColorSpace = 0;
617 if (!parameters.nb_tile_to_decode) {
618 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0,
619 parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)) {
620 opj_image_destroy(image);
624 if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
625 opj_image_destroy(image);
630 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
634 opj_stream_destroy(l_stream);
636 if( image->color_space != OPJ_CLRSPC_SYCC
637 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
638 && image->comps[1].dx != 1 ) {
639 image->color_space = OPJ_CLRSPC_SYCC;
640 } else if (image->numcomps <= 2) {
641 image->color_space = OPJ_CLRSPC_GRAY;
643 if(image->color_space == OPJ_CLRSPC_SYCC) {
644 color_sycc_to_rgb(image);
646 if(image->icc_profile_buf && !image->useColorSpace) {
647 FX_Free(image->icc_profile_buf);
648 image->icc_profile_buf = NULL;
649 image->icc_profile_len = 0;
656 void CJPX_Decoder::GetInfo(FX_DWORD& width, FX_DWORD& height, FX_DWORD& codestream_nComps, FX_DWORD& output_nComps)
658 width = (FX_DWORD)image->x1;
659 height = (FX_DWORD)image->y1;
660 output_nComps = codestream_nComps = (FX_DWORD)image->numcomps;
662 FX_BOOL CJPX_Decoder::Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
664 FX_BYTE** channel_bufs;
666 int i, wid, hei, row, col, channel, src;
668 FX_LPBYTE pChannel, pScanline, pPixel;
670 if(image->comps[0].w != image->x1 || image->comps[0].h != image->y1) {
673 if(pitch < (int)(image->comps[0].w * 8 * image->numcomps + 31) >> 5 << 2) {
676 FXSYS_memset8(dest_buf, 0xff, image->y1 * pitch);
677 channel_bufs = FX_Alloc(FX_BYTE*, image->numcomps);
678 if (channel_bufs == NULL) {
681 adjust_comps = FX_Alloc(int, image->numcomps);
682 if (adjust_comps == NULL) {
683 FX_Free(channel_bufs);
687 for (i = 0; i < (int)image->numcomps; i ++) {
688 channel_bufs[i] = dest_buf + offsets[i];
689 adjust_comps[i] = image->comps[i].prec - 8;
691 if(image->comps[i].dx != image->comps[i - 1].dx
692 || image->comps[i].dy != image->comps[i - 1].dy
693 || image->comps[i].prec != image->comps[i - 1].prec) {
699 wid = image->comps[0].w;
700 hei = image->comps[0].h;
701 for (channel = 0; channel < (int)image->numcomps; channel++) {
702 pChannel = channel_bufs[channel];
703 if(adjust_comps[channel] < 0) {
704 for(row = 0; row < hei; row++) {
705 pScanline = pChannel + row * pitch;
706 for (col = 0; col < wid; col++) {
707 pPixel = pScanline + col * image->numcomps;
708 src = image->comps[channel].data[row * wid + col];
709 src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
710 if (adjust_comps[channel] > 0) {
713 *pPixel = (FX_BYTE)(src << -adjust_comps[channel]);
718 for(row = 0; row < hei; row++) {
719 pScanline = pChannel + row * pitch;
720 for (col = 0; col < wid; col++) {
721 pPixel = pScanline + col * image->numcomps;
722 if (!image->comps[channel].data) {
725 src = image->comps[channel].data[row * wid + col];
726 src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
727 if (adjust_comps[channel] - 1 < 0) {
728 *pPixel = (FX_BYTE)((src >> adjust_comps[channel]));
730 int tmpPixel = (src >> adjust_comps[channel]) + ((src >> (adjust_comps[channel] - 1)) % 2);
731 if (tmpPixel > 255) {
733 } else if (tmpPixel < 0) {
736 *pPixel = (FX_BYTE)tmpPixel;
743 FX_Free(channel_bufs);
744 FX_Free(adjust_comps);
747 FX_Free(channel_bufs);
748 FX_Free(adjust_comps);
751 void initialize_transition_table();
752 void initialize_significance_luts();
753 void initialize_sign_lut();
754 CCodec_JpxModule::CCodec_JpxModule()
757 void* CCodec_JpxModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size , FX_BOOL useColorSpace)
759 CJPX_Decoder* pDecoder = FX_NEW CJPX_Decoder;
760 if (pDecoder == NULL) {
763 pDecoder->m_useColorSpace = useColorSpace;
764 if (!pDecoder->Init(src_buf, src_size)) {
770 void CCodec_JpxModule::GetImageInfo(FX_LPVOID ctx, FX_DWORD& width, FX_DWORD& height,
771 FX_DWORD& codestream_nComps, FX_DWORD& output_nComps)
773 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
774 pDecoder->GetInfo(width, height, codestream_nComps, output_nComps);
776 FX_BOOL CCodec_JpxModule::Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
778 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
779 return pDecoder->Decode(dest_data, pitch, bTranslateColor, offsets);
781 void CCodec_JpxModule::DestroyDecoder(void* ctx)
783 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;