// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+#include <algorithm>
+#include <limits>
+
#include "../../../include/fxcodec/fx_codec.h"
#include "codec_int.h"
#include "../fx_libopenjpeg/libopenjpeg20/openjpeg.h"
#include "../lcms2/include/fx_lcms2.h"
+
static void fx_error_callback(const char *msg, void *client_data)
{
(void)client_data;
{
(void)client_data;
}
-typedef struct {
- const unsigned char* src_data;
- int src_size;
- int offset;
-} decodeData;
-static OPJ_SIZE_T opj_read_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, decodeData* srcData)
+OPJ_SIZE_T opj_read_from_memory(void* p_buffer, OPJ_SIZE_T nb_bytes, void* p_user_data)
{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0) {
return -1;
}
- OPJ_SIZE_T readlength = p_nb_bytes;
- OPJ_SIZE_T bufferLength = (OPJ_SIZE_T)(srcData->src_size - srcData->offset);
- if(bufferLength <= 0) {
- return 0;
- }
- if(bufferLength <= p_nb_bytes) {
- readlength = bufferLength;
+ // Reads at EOF return an error code.
+ if (srcData->offset >= srcData->src_size) {
+ return -1;
}
- memcpy(p_buffer, &(srcData->src_data[srcData->offset]), readlength);
- srcData->offset += (int)readlength;
+ OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
+ OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
+ memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength);
+ srcData->offset += readlength;
return readlength;
}
-static OPJ_SIZE_T opj_write_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, decodeData* srcData)
+OPJ_SIZE_T opj_write_from_memory(void* p_buffer, OPJ_SIZE_T nb_bytes, void* p_user_data)
{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0) {
return -1;
}
- OPJ_SIZE_T writeLength = p_nb_bytes;
- OPJ_SIZE_T bufferLength = (OPJ_SIZE_T)(srcData->src_size - srcData->offset);
- if(bufferLength <= p_nb_bytes) {
- writeLength = bufferLength;
+ // Writes at EOF return an error code.
+ if (srcData->offset >= srcData->src_size) {
+ return -1;
}
- memcpy((void*&)(srcData->src_data[srcData->offset]), p_buffer, writeLength);
- srcData->offset += (int)writeLength;
+ OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
+ OPJ_SIZE_T writeLength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
+ memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength);
+ srcData->offset += writeLength;
return writeLength;
}
-static OPJ_OFF_T opj_skip_from_memory (OPJ_OFF_T p_nb_bytes, decodeData* srcData)
+OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data)
{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0) {
return -1;
}
- OPJ_OFF_T postion = srcData->offset + p_nb_bytes;
- if(postion < 0 ) {
- postion = 0;
- } else if (postion > srcData->src_size) {
+ // Offsets are signed and may indicate a negative skip. Do not support this
+ // because of the strange return convention where either bytes skipped or
+ // -1 is returned. Following that convention, a successful relative seek of
+ // -1 bytes would be required to to give the same result as the error case.
+ if (nb_bytes < 0) {
+ return -1;
}
- srcData->offset = (int)postion;
- return p_nb_bytes;
+ // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd.
+ uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes);
+ // Additionally, the offset may take us beyond the range of a size_t (e.g.
+ // 32-bit platforms). If so, just clamp at EOF.
+ if (unsignedNbBytes > std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) {
+ srcData->offset = srcData->src_size;
+ } else {
+ OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(unsignedNbBytes);
+ // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
+ // clamping at EOF. We can get away with this since we don't actually
+ // provide negative relative skips from beyond EOF back to inside the
+ // data, which would be the only reason to need to know exactly how far
+ // beyond EOF we are.
+ srcData->offset = std::min(srcData->offset + checkedNbBytes, srcData->src_size);
+ }
+ return nb_bytes;
}
-static OPJ_BOOL opj_seek_from_memory (OPJ_OFF_T p_nb_bytes, decodeData * srcData)
+OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data)
{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
- return -1;
+ DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
+ if (!srcData || !srcData->src_data || srcData->src_size == 0) {
+ return OPJ_FALSE;
}
- srcData->offset = (int)p_nb_bytes;
- if(srcData->offset < 0) {
- srcData->offset = 0;
- } else if(srcData->offset > srcData->src_size) {
+ // Offsets are signed and may indicate a negative position, which would
+ // be before the start of the file. Do not support this.
+ if (nb_bytes < 0) {
+ return OPJ_FALSE;
+ }
+ // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd.
+ uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes);
+ // Additionally, the offset may take us beyond the range of a size_t (e.g.
+ // 32-bit platforms). If so, just clamp at EOF.
+ if (unsignedNbBytes > std::numeric_limits<OPJ_SIZE_T>::max()) {
srcData->offset = srcData->src_size;
+ } else {
+ OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(nb_bytes);
+ // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
+ // again clamping at EOF.
+ srcData->offset = std::min(checkedNbBytes, srcData->src_size);
}
return OPJ_TRUE;
}
-opj_stream_t* fx_opj_stream_create_memory_stream (decodeData* data, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream)
+opj_stream_t* fx_opj_stream_create_memory_stream (DecodeData* data, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream)
{
opj_stream_t* l_stream = 00;
if (!data || ! data->src_data || data->src_size <= 0 ) {
if (! l_stream) {
return NULL;
}
- opj_stream_set_user_data_v3(l_stream, data, NULL);
+ opj_stream_set_user_data(l_stream, data, NULL);
opj_stream_set_user_data_length(l_stream, data->src_size);
- opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_memory);
- opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_memory);
- opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_memory);
- opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_memory);
+ opj_stream_set_read_function(l_stream, opj_read_from_memory);
+ opj_stream_set_write_function(l_stream, opj_write_from_memory);
+ opj_stream_set_skip_function(l_stream, opj_skip_from_memory);
+ opj_stream_set_seek_function(l_stream, opj_seek_from_memory);
return l_stream;
}
static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
d0 = r = FX_Alloc(int, (size_t)max);
d1 = g = FX_Alloc(int, (size_t)max);
d2 = b = FX_Alloc(int, (size_t)max);
- for(i = 0; i < maxh; ++i) {
- for(j = 0; j < maxw; j += 2) {
+ for(i = 0; i < maxh; ++i)
+ {
+ for (j = 0; (OPJ_UINT32)j < (maxw & ~(OPJ_UINT32)1); j += 2)
+ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
+ ++y; ++r; ++g; ++b;
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
- ++cb;
- ++cr;
+ ++y; ++r; ++g; ++b; ++cb; ++cr;
+ }
+ if (j < maxw)
+ {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y; ++r; ++g; ++b; ++cb; ++cr;
}
}
FX_Free(img->comps[0].data);
d0 = r = FX_Alloc(int, (size_t)max);
d1 = g = FX_Alloc(int, (size_t)max);
d2 = b = FX_Alloc(int, (size_t)max);
- for(i = 0; i < maxh; i += 2) {
+ for (i = 0; (OPJ_UINT32)i < (maxh & ~(OPJ_UINT32)1); i += 2)
+ {
ny = y + maxw;
nr = r + maxw;
ng = g + maxw;
nb = b + maxw;
- for(j = 0; j < maxw; j += 2) {
+ for (j = 0; (OPJ_UINT32)j < (maxw & ~(OPJ_UINT32)1); j += 2)
+ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
+ ++y; ++r; ++g; ++b;
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
+ ++y; ++r; ++g; ++b;
+ sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+ ++ny; ++nr; ++ng; ++nb;
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
- ++ny;
- ++nr;
- ++ng;
- ++nb;
+ ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
+ }
+ if (j < maxw)
+ {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y; ++r; ++g; ++b;
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
- ++ny;
- ++nr;
- ++ng;
- ++nb;
- ++cb;
- ++cr;
+ ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
}
- y += maxw;
- r += maxw;
- g += maxw;
- b += maxw;
+ y += maxw; r += maxw; g += maxw; b += maxw;
}
+ if (i < maxh)
+ {
+ for (j = 0; (OPJ_UINT32)j < (maxw & ~(OPJ_UINT32)1); j += 2)
+ {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y; ++r; ++g; ++b;
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y; ++r; ++g; ++b; ++cb; ++cr;
+ }
+ if (j < maxw)
+ {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ }
+ }
+
FX_Free(img->comps[0].data);
img->comps[0].data = d0;
FX_Free(img->comps[1].data);
}
void color_apply_icc_profile(opj_image_t *image)
{
- cmsHPROFILE in_prof, out_prof;
- cmsHTRANSFORM transform;
- cmsColorSpaceSignature in_space, out_space;
- cmsUInt32Number intent, in_type, out_type, nr_samples;
- int *r, *g, *b;
- int prec, i, max, max_w, max_h;
- OPJ_COLOR_SPACE oldspace;
- in_prof =
+ cmsHPROFILE out_prof;
+ cmsUInt32Number in_type;
+ cmsUInt32Number out_type;
+ int *r;
+ int *g;
+ int *b;
+ int max;
+ cmsHPROFILE in_prof =
cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
if(in_prof == NULL) {
return;
}
- in_space = cmsGetPCS(in_prof);
- out_space = cmsGetColorSpace(in_prof);
- intent = cmsGetHeaderRenderingIntent(in_prof);
- max_w = (int)image->comps[0].w;
- max_h = (int)image->comps[0].h;
- prec = (int)image->comps[0].prec;
- oldspace = image->color_space;
+ cmsColorSpaceSignature out_space = cmsGetColorSpace(in_prof);
+ cmsUInt32Number intent = cmsGetHeaderRenderingIntent(in_prof);
+ int max_w = (int)image->comps[0].w;
+ int max_h = (int)image->comps[0].h;
+ int prec = (int)image->comps[0].prec;
+ OPJ_COLOR_SPACE oldspace = image->color_space;
if(out_space == cmsSigRgbData) {
if( prec <= 8 ) {
in_type = TYPE_RGB_8;
} else {
return;
}
- transform = cmsCreateTransform(in_prof, in_type,
- out_prof, out_type, intent, 0);
+ cmsHTRANSFORM transform =
+ cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof);
if(transform == NULL) {
if( prec <= 8 ) {
unsigned char *inbuf, *outbuf, *in, *out;
max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
+ cmsUInt32Number nr_samples = max * 3 * sizeof(unsigned char);
in = inbuf = FX_Alloc(unsigned char, nr_samples);
out = outbuf = FX_Alloc(unsigned char, nr_samples);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
+ for(int i = 0; i < max; ++i) {
*in++ = (unsigned char) * r++;
*in++ = (unsigned char) * g++;
*in++ = (unsigned char) * b++;
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
+ for(int i = 0; i < max; ++i) {
*r++ = (int) * out++;
*g++ = (int) * out++;
*b++ = (int) * out++;
} else {
unsigned short *inbuf, *outbuf, *in, *out;
max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
+ cmsUInt32Number nr_samples = max * 3 * sizeof(unsigned short);
in = inbuf = FX_Alloc(unsigned short, nr_samples);
out = outbuf = FX_Alloc(unsigned short, nr_samples);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
+ for(int i = 0; i < max; ++i) {
*in++ = (unsigned short) * r++;
*in++ = (unsigned short) * g++;
*in++ = (unsigned short) * b++;
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
+ for(int i = 0; i < max; ++i) {
*r++ = (int) * out++;
*g++ = (int) * out++;
*b++ = (int) * out++;
} else {
unsigned char *in, *inbuf, *out, *outbuf;
max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
+ cmsUInt32Number nr_samples =
+ (cmsUInt32Number)max * 3 * sizeof(unsigned char);
in = inbuf = FX_Alloc(unsigned char, nr_samples);
out = outbuf = FX_Alloc(unsigned char, nr_samples);
image->comps = (opj_image_comp_t*)
FXSYS_memset8(image->comps[2].data, 0, sizeof(int) * (size_t)max);
image->numcomps += 2;
r = image->comps[0].data;
- for(i = 0; i < max; ++i) {
+ for(int i = 0; i < max; ++i) {
*in++ = (unsigned char) * r++;
}
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
+ for(int i = 0; i < max; ++i) {
*r++ = (int) * out++;
*g++ = (int) * out++;
*b++ = (int) * out++;
int *L, *a, *b, *red, *green, *blue, *src0, *src1, *src2;
double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
double minL, maxL, mina, maxa, minb, maxb;
- unsigned int default_type, il;
- unsigned int i, max, illu;
+ unsigned int default_type;
+ unsigned int i, max;
cmsHPROFILE in, out;
cmsHTRANSFORM transform;
cmsUInt16Number RGB[3];
cmsCIELab Lab;
- illu = 0;
- il = 0;
in = cmsCreateLab4Profile(NULL);
out = cmsCreate_sRGBProfile();
transform =
return;
}
}
-class CJPX_Decoder : public CFX_Object
+class CJPX_Decoder
{
public:
CJPX_Decoder();
}
FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size)
{
+ static const unsigned char szJP2Header[] = { 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a };
+ if (!src_data || src_size < sizeof(szJP2Header)) {
+ return FALSE;
+ }
+ image = NULL;
+ m_SrcData = src_data;
+ m_SrcSize = src_size;
+ DecodeData srcData(const_cast<unsigned char*>(src_data), src_size);
+ l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK_SIZE, 1);
+ if (l_stream == NULL) {
+ return FALSE;
+ }
opj_dparameters_t parameters;
- try {
+ opj_set_default_decoder_parameters(¶meters);
+ parameters.decod_format = 0;
+ parameters.cod_format = 3;
+ if(FXSYS_memcmp32(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) {
+ l_codec = opj_create_decompress(OPJ_CODEC_JP2);
+ parameters.decod_format = 1;
+ } else {
+ l_codec = opj_create_decompress(OPJ_CODEC_J2K);
+ }
+ if(!l_codec) {
+ return FALSE;
+ }
+ opj_set_info_handler(l_codec, fx_info_callback, 00);
+ opj_set_warning_handler(l_codec, fx_warning_callback, 00);
+ opj_set_error_handler(l_codec, fx_error_callback, 00);
+ if ( !opj_setup_decoder(l_codec, ¶meters) ) {
+ return FALSE;
+ }
+ if(! opj_read_header(l_stream, l_codec, &image)) {
image = NULL;
- m_SrcData = src_data;
- m_SrcSize = src_size;
- decodeData srcData;
- srcData.offset = 0;
- srcData.src_size = src_size;
- srcData.src_data = src_data;
- l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK_SIZE, 1);
- if (l_stream == NULL) {
- return FALSE;
- }
- opj_set_default_decoder_parameters(¶meters);
- parameters.decod_format = 0;
- parameters.cod_format = 3;
- if(FXSYS_memcmp32(m_SrcData, "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a", 12) == 0) {
- l_codec = opj_create_decompress(OPJ_CODEC_JP2);
- parameters.decod_format = 1;
- } else {
- l_codec = opj_create_decompress(OPJ_CODEC_J2K);
- }
- if(!l_codec) {
- return FALSE;
- }
- opj_set_info_handler(l_codec, fx_info_callback, 00);
- opj_set_warning_handler(l_codec, fx_warning_callback, 00);
- opj_set_error_handler(l_codec, fx_error_callback, 00);
- if ( !opj_setup_decoder(l_codec, ¶meters) ) {
+ return FALSE;
+ }
+/*
+ if(this->m_useColorSpace) {
+ image->useColorSpace = 1;
+ } else {
+ image->useColorSpace = 0;
+ }
+*/
+ if (!parameters.nb_tile_to_decode) {
+ if (!opj_set_decode_area(l_codec, image, parameters.DA_x0,
+ parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)) {
+ opj_image_destroy(image);
+ image = NULL;
return FALSE;
}
- if(! opj_read_header(l_stream, l_codec, &image)) {
+ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
+ opj_image_destroy(image);
image = NULL;
return FALSE;
}
- if(this->m_useColorSpace) {
- image->useColorSpace = 1;
- } else {
- image->useColorSpace = 0;
- }
- if (!parameters.nb_tile_to_decode) {
- if (!opj_set_decode_area(l_codec, image, parameters.DA_x0,
- parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)) {
- opj_image_destroy(image);
- image = NULL;
- return FALSE;
- }
- if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
- opj_image_destroy(image);
- image = NULL;
- return FALSE;
- }
- } else {
- if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
- return FALSE;
- }
- }
- opj_stream_destroy(l_stream);
- l_stream = NULL;
- if( image->color_space != OPJ_CLRSPC_SYCC
- && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
- && image->comps[1].dx != 1 ) {
- image->color_space = OPJ_CLRSPC_SYCC;
- } else if (image->numcomps <= 2) {
- image->color_space = OPJ_CLRSPC_GRAY;
- }
- if(image->color_space == OPJ_CLRSPC_SYCC) {
- color_sycc_to_rgb(image);
- }
- if(image->icc_profile_buf && !image->useColorSpace) {
- FX_Free(image->icc_profile_buf);
- image->icc_profile_buf = NULL;
- image->icc_profile_len = 0;
- }
- if(!image) {
+ } else {
+ if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
return FALSE;
}
- } catch (...) {
+ }
+ opj_stream_destroy(l_stream);
+ l_stream = NULL;
+ if( image->color_space != OPJ_CLRSPC_SYCC
+ && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
+ && image->comps[1].dx != 1 ) {
+ image->color_space = OPJ_CLRSPC_SYCC;
+ } else if (image->numcomps <= 2) {
+ image->color_space = OPJ_CLRSPC_GRAY;
+ }
+ if(image->color_space == OPJ_CLRSPC_SYCC) {
+ color_sycc_to_rgb(image);
+ }
+ //if(image->icc_profile_buf && !image->useColorSpace) {
+ if(image->icc_profile_buf) {
+ FX_Free(image->icc_profile_buf);
+ image->icc_profile_buf = NULL;
+ image->icc_profile_len = 0;
+ }
+ if(!image) {
return FALSE;
}
return TRUE;
}
FX_BOOL CJPX_Decoder::Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
{
- FX_BYTE** channel_bufs;
- int* adjust_comps;
int i, wid, hei, row, col, channel, src;
- FX_BOOL flag;
FX_LPBYTE pChannel, pScanline, pPixel;
- try {
- if(image->comps[0].w != image->x1 || image->comps[0].h != image->y1) {
- return FALSE;
- }
- if(pitch < (int)(image->comps[0].w * 8 * image->numcomps + 31) >> 5 << 2) {
- return FALSE;
- }
- FXSYS_memset8(dest_buf, 0xff, image->y1 * pitch);
- channel_bufs = FX_Alloc(FX_BYTE*, image->numcomps);
- if (channel_bufs == NULL) {
- return FALSE;
- }
- adjust_comps = FX_Alloc(int, image->numcomps);
- if (adjust_comps == NULL) {
- FX_Free(channel_bufs);
- return FALSE;
- }
- flag = TRUE;
- for (i = 0; i < (int)image->numcomps; i ++) {
- channel_bufs[i] = dest_buf + offsets[i];
- adjust_comps[i] = image->comps[i].prec - 8;
- if(i > 0) {
- if(image->comps[i].dx != image->comps[i - 1].dx
- || image->comps[i].dy != image->comps[i - 1].dy
- || image->comps[i].prec != image->comps[i - 1].prec) {
- flag = FALSE;
- goto failed;
- }
+
+ if(image->comps[0].w != image->x1 || image->comps[0].h != image->y1) {
+ return FALSE;
+ }
+ if(pitch < (int)(image->comps[0].w * 8 * image->numcomps + 31) >> 5 << 2) {
+ return FALSE;
+ }
+ FXSYS_memset8(dest_buf, 0xff, image->y1 * pitch);
+ FX_BYTE** channel_bufs = FX_Alloc(FX_BYTE*, image->numcomps);
+ if (channel_bufs == NULL) {
+ return FALSE;
+ }
+ FX_BOOL result = FALSE;
+ int* adjust_comps = FX_Alloc(int, image->numcomps);
+ if (adjust_comps == NULL) {
+ goto done;
+ }
+ for (i = 0; i < (int)image->numcomps; i ++) {
+ channel_bufs[i] = dest_buf + offsets[i];
+ adjust_comps[i] = image->comps[i].prec - 8;
+ if(i > 0) {
+ if(image->comps[i].dx != image->comps[i - 1].dx
+ || image->comps[i].dy != image->comps[i - 1].dy
+ || image->comps[i].prec != image->comps[i - 1].prec) {
+ goto done;
}
}
- wid = image->comps[0].w;
- hei = image->comps[0].h;
- for (channel = 0; channel < (int)image->numcomps; channel++) {
- pChannel = channel_bufs[channel];
- if(adjust_comps[channel] < 0) {
- for(row = 0; row < hei; row++) {
- pScanline = pChannel + row * pitch;
- for (col = 0; col < wid; col++) {
- pPixel = pScanline + col * image->numcomps;
- src = image->comps[channel].data[row * wid + col];
- src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
- if (adjust_comps[channel] > 0) {
- *pPixel = 0;
- } else {
- *pPixel = (FX_BYTE)(src << -adjust_comps[channel]);
- }
+ }
+ wid = image->comps[0].w;
+ hei = image->comps[0].h;
+ for (channel = 0; channel < (int)image->numcomps; channel++) {
+ pChannel = channel_bufs[channel];
+ if(adjust_comps[channel] < 0) {
+ for(row = 0; row < hei; row++) {
+ pScanline = pChannel + row * pitch;
+ for (col = 0; col < wid; col++) {
+ pPixel = pScanline + col * image->numcomps;
+ src = image->comps[channel].data[row * wid + col];
+ src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
+ if (adjust_comps[channel] > 0) {
+ *pPixel = 0;
+ } else {
+ *pPixel = (FX_BYTE)(src << -adjust_comps[channel]);
}
}
- } else {
- for(row = 0; row < hei; row++) {
- pScanline = pChannel + row * pitch;
- for (col = 0; col < wid; col++) {
- pPixel = pScanline + col * image->numcomps;
- if (!image->comps[channel].data) continue;
- src = image->comps[channel].data[row * wid + col];
- src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
- if (adjust_comps[channel] - 1 < 0) {
- *pPixel = (FX_BYTE)((src >> adjust_comps[channel]));
- } else {
- int tmpPixel = (src >> adjust_comps[channel]) + ((src >> (adjust_comps[channel] - 1)) % 2);
- if (tmpPixel > 255) {
- tmpPixel = 255;
- } else if (tmpPixel < 0) {
- tmpPixel = 0;
- }
- *pPixel = (FX_BYTE)tmpPixel;
+ }
+ } else {
+ for(row = 0; row < hei; row++) {
+ pScanline = pChannel + row * pitch;
+ for (col = 0; col < wid; col++) {
+ pPixel = pScanline + col * image->numcomps;
+ if (!image->comps[channel].data) {
+ continue;
+ }
+ src = image->comps[channel].data[row * wid + col];
+ src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
+ if (adjust_comps[channel] - 1 < 0) {
+ *pPixel = (FX_BYTE)((src >> adjust_comps[channel]));
+ } else {
+ int tmpPixel = (src >> adjust_comps[channel]) + ((src >> (adjust_comps[channel] - 1)) % 2);
+ if (tmpPixel > 255) {
+ tmpPixel = 255;
+ } else if (tmpPixel < 0) {
+ tmpPixel = 0;
}
+ *pPixel = (FX_BYTE)tmpPixel;
}
}
}
}
- } catch (...) {
- if (channel_bufs) {
- FX_Free(channel_bufs);
- }
- FX_Free(adjust_comps);
- return FALSE;
}
+ result = TRUE;
+
+done:
FX_Free(channel_bufs);
FX_Free(adjust_comps);
- return TRUE;
-failed:
- FX_Free(channel_bufs);
- FX_Free(adjust_comps);
- return FALSE;
+ return result;
}
void initialize_transition_table();
void initialize_significance_luts();
}
void* CCodec_JpxModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size , FX_BOOL useColorSpace)
{
- CJPX_Decoder* pDecoder = FX_NEW CJPX_Decoder;
- if (pDecoder == NULL) {
- return NULL;
- }
+ CJPX_Decoder* pDecoder = new CJPX_Decoder;
pDecoder->m_useColorSpace = useColorSpace;
if (!pDecoder->Init(src_buf, src_size)) {
delete pDecoder;