summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/common/color.c614
-rw-r--r--src/bin/jp2/convert.c80
-rw-r--r--src/bin/jp2/convertbmp.c8
-rw-r--r--src/bin/jp2/convertpng.c14
-rw-r--r--src/bin/jp2/index.c2
-rw-r--r--src/bin/jp2/opj_compress.c29
-rw-r--r--src/bin/jp2/opj_decompress.c73
-rw-r--r--src/bin/jp2/opj_dump.c49
-rw-r--r--src/lib/openjp2/bio.c22
-rw-r--r--src/lib/openjp2/cidx_manager.c4
-rw-r--r--src/lib/openjp2/j2k.c72
-rw-r--r--src/lib/openjp2/jp2.c10
-rw-r--r--src/lib/openjp2/opj_includes.h8
-rw-r--r--src/lib/openjp2/phix_manager.c4
-rw-r--r--src/lib/openjp2/pi.c5
-rw-r--r--src/lib/openjp2/ppix_manager.c4
-rw-r--r--src/lib/openjp2/raw.c2
-rw-r--r--src/lib/openjp2/t1.c6
-rw-r--r--src/lib/openjp2/tcd.c16
-rw-r--r--src/lib/openjp2/thix_manager.c4
20 files changed, 651 insertions, 375 deletions
diff --git a/src/bin/common/color.c b/src/bin/common/color.c
index 0f2b8dac..944df73b 100644
--- a/src/bin/common/color.c
+++ b/src/bin/common/color.c
@@ -91,22 +91,24 @@ static void sycc444_to_rgb(opj_image_t *img)
{
int *d0, *d1, *d2, *r, *g, *b;
const int *y, *cb, *cr;
- unsigned int maxw, maxh, max, i;
+ size_t maxw, maxh, max, i;
int offset, upb;
upb = (int)img->comps[0].prec;
offset = 1<<(upb - 1); upb = (1<<upb)-1;
- maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
+ maxw = (size_t)img->comps[0].w; maxh = (size_t)img->comps[0].h;
max = maxw * maxh;
y = img->comps[0].data;
cb = img->comps[1].data;
cr = img->comps[2].data;
- d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
- d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
- d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
+ d0 = r = (int*)malloc(sizeof(int) * max);
+ d1 = g = (int*)malloc(sizeof(int) * max);
+ d2 = b = (int*)malloc(sizeof(int) * max);
+
+ if(r == NULL || g == NULL || b == NULL) goto fails;
for(i = 0U; i < max; ++i)
{
@@ -116,91 +118,138 @@ static void sycc444_to_rgb(opj_image_t *img)
free(img->comps[0].data); img->comps[0].data = d0;
free(img->comps[1].data); img->comps[1].data = d1;
free(img->comps[2].data); img->comps[2].data = d2;
+ img->color_space = OPJ_CLRSPC_SRGB;
+ return;
+fails:
+ free(r);
+ free(g);
+ free(b);
}/* sycc444_to_rgb() */
static void sycc422_to_rgb(opj_image_t *img)
{
int *d0, *d1, *d2, *r, *g, *b;
const int *y, *cb, *cr;
- unsigned int maxw, maxh, max;
+ size_t maxw, maxh, max, offx, loopmaxw;
int offset, upb;
- unsigned int i, j;
+ size_t i;
upb = (int)img->comps[0].prec;
offset = 1<<(upb - 1); upb = (1<<upb)-1;
- maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
+ maxw = (size_t)img->comps[0].w; maxh = (size_t)img->comps[0].h;
max = maxw * maxh;
y = img->comps[0].data;
cb = img->comps[1].data;
cr = img->comps[2].data;
- d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
- d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
- d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
+ d0 = r = (int*)malloc(sizeof(int) * max);
+ d1 = g = (int*)malloc(sizeof(int) * max);
+ d2 = b = (int*)malloc(sizeof(int) * max);
+ if(r == NULL || g == NULL || b == NULL) goto fails;
+
+ /* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
+ offx = img->x0 & 1U;
+ loopmaxw = maxw - offx;
+
for(i=0U; i < maxh; ++i)
{
- for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
+ size_t j;
+
+ if (offx > 0U) {
+ sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
+ ++y; ++r; ++g; ++b;
+ }
+
+ for(j=0U; j < (loopmaxw & ~(size_t)1U); j += 2U)
{
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) {
+ if (j < loopmaxw) {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++r; ++g; ++b; ++cb; ++cr;
}
}
+
free(img->comps[0].data); img->comps[0].data = d0;
free(img->comps[1].data); img->comps[1].data = d1;
free(img->comps[2].data); img->comps[2].data = d2;
-#if defined(USE_JPWL) || defined(USE_MJ2)
- img->comps[1].w = maxw; img->comps[1].h = maxh;
- img->comps[2].w = maxw; img->comps[2].h = maxh;
-#else
- img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
- img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
-#endif
- img->comps[1].dx = img->comps[0].dx;
- img->comps[2].dx = img->comps[0].dx;
- img->comps[1].dy = img->comps[0].dy;
- img->comps[2].dy = img->comps[0].dy;
+ img->comps[1].w = img->comps[2].w = img->comps[0].w;
+ img->comps[1].h = img->comps[2].h = img->comps[0].h;
+ img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
+ img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
+ img->color_space = OPJ_CLRSPC_SRGB;
+ return;
+fails:
+ free(r);
+ free(g);
+ free(b);
}/* sycc422_to_rgb() */
static void sycc420_to_rgb(opj_image_t *img)
{
int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
const int *y, *cb, *cr, *ny;
- unsigned int maxw, maxh, max;
+ size_t maxw, maxh, max, offx, loopmaxw, offy, loopmaxh;
int offset, upb;
- unsigned int i, j;
+ size_t i;
upb = (int)img->comps[0].prec;
offset = 1<<(upb - 1); upb = (1<<upb)-1;
- maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
+ maxw = (size_t)img->comps[0].w; maxh = (size_t)img->comps[0].h;
max = maxw * maxh;
y = img->comps[0].data;
cb = img->comps[1].data;
cr = img->comps[2].data;
- d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
- d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
- d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
+ d0 = r = (int*)malloc(sizeof(int) * max);
+ d1 = g = (int*)malloc(sizeof(int) * max);
+ d2 = b = (int*)malloc(sizeof(int) * max);
+
+ if (r == NULL || g == NULL || b == NULL) goto fails;
+
+ /* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
+ offx = img->x0 & 1U;
+ loopmaxw = maxw - offx;
+ /* if img->y0 is odd, then first line shall use Cb/Cr = 0 */
+ offy = img->y0 & 1U;
+ loopmaxh = maxh - offy;
+
+ if (offy > 0U) {
+ size_t j;
+
+ for(j=0; j < maxw; ++j)
+ {
+ sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
+ ++y; ++r; ++g; ++b;
+ }
+ }
- for(i=0U; i < (maxh & ~(unsigned int)1U); i += 2U)
+ for(i=0U; i < (loopmaxh & ~(size_t)1U); i += 2U)
{
+ size_t j;
+
ny = y + maxw;
nr = r + maxw; ng = g + maxw; nb = b + maxw;
+
+ if (offx > 0U) {
+ sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
+ ++y; ++r; ++g; ++b;
+ sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+ ++ny; ++nr; ++ng; ++nb;
+ }
- for(j=0; j < (maxw & ~(unsigned int)1U); j += 2U)
+ for(j=0; j < (loopmaxw & ~(size_t)1U); j += 2U)
{
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++r; ++g; ++b;
@@ -212,7 +261,7 @@ static void sycc420_to_rgb(opj_image_t *img)
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
}
- if(j < maxw)
+ if(j < loopmaxw)
{
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++r; ++g; ++b;
@@ -222,9 +271,11 @@ static void sycc420_to_rgb(opj_image_t *img)
}
y += maxw; r += maxw; g += maxw; b += maxw;
}
- if(i < maxh)
+ if(i < loopmaxh)
{
- for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
+ size_t j;
+
+ for(j=0U; j < (maxw & ~(size_t)1U); j += 2U)
{
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
@@ -244,18 +295,17 @@ static void sycc420_to_rgb(opj_image_t *img)
free(img->comps[1].data); img->comps[1].data = d1;
free(img->comps[2].data); img->comps[2].data = d2;
-#if defined(USE_JPWL) || defined(USE_MJ2)
- img->comps[1].w = maxw; img->comps[1].h = maxh;
- img->comps[2].w = maxw; img->comps[2].h = maxh;
-#else
- img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
- img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
-#endif
- img->comps[1].dx = img->comps[0].dx;
- img->comps[2].dx = img->comps[0].dx;
- img->comps[1].dy = img->comps[0].dy;
- img->comps[2].dy = img->comps[0].dy;
+ img->comps[1].w = img->comps[2].w = img->comps[0].w;
+ img->comps[1].h = img->comps[2].h = img->comps[0].h;
+ img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
+ img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
+ img->color_space = OPJ_CLRSPC_SRGB;
+ return;
+fails:
+ free(r);
+ free(g);
+ free(b);
}/* sycc420_to_rgb() */
void color_sycc_to_rgb(opj_image_t *img)
@@ -300,8 +350,6 @@ void color_sycc_to_rgb(opj_image_t *img)
fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
return;
}
- img->color_space = OPJ_CLRSPC_SRGB;
-
}/* color_sycc_to_rgb() */
#if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
@@ -328,17 +376,17 @@ 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;
+ cmsUInt32Number intent, in_type, out_type;
int *r, *g, *b;
- int prec, i, max, max_w, max_h;
- OPJ_COLOR_SPACE oldspace;
+ size_t nr_samples, i, max, max_w, max_h;
+ int prec, ok = 0;
+ OPJ_COLOR_SPACE new_space;
- in_prof =
- cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
+ in_prof = cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
#ifdef DEBUG_PROFILE
- FILE *icm = fopen("debug.icm","wb");
- fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
- fclose(icm);
+ FILE *icm = fopen("debug.icm","wb");
+ fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
+ fclose(icm);
#endif
if(in_prof == NULL) return;
@@ -348,80 +396,83 @@ void color_apply_icc_profile(opj_image_t *image)
intent = cmsGetHeaderRenderingIntent(in_prof);
- max_w = (int)image->comps[0].w;
- max_h = (int)image->comps[0].h;
+ max_w = image->comps[0].w;
+ max_h = image->comps[0].h;
prec = (int)image->comps[0].prec;
- oldspace = image->color_space;
if(out_space == cmsSigRgbData) /* enumCS 16 */
- {
- if( prec <= 8 )
- {
- in_type = TYPE_RGB_8;
- out_type = TYPE_RGB_8;
- }
- else
- {
- in_type = TYPE_RGB_16;
- out_type = TYPE_RGB_16;
- }
- out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
- }
- else
- if(out_space == cmsSigGrayData) /* enumCS 17 */
- {
- in_type = TYPE_GRAY_8;
- out_type = TYPE_RGB_8;
- out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
- }
- else
- if(out_space == cmsSigYCbCrData) /* enumCS 18 */
- {
- in_type = TYPE_YCbCr_16;
- out_type = TYPE_RGB_16;
- out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
- }
+ {
+ if( prec <= 8 )
+ {
+ in_type = TYPE_RGB_8;
+ out_type = TYPE_RGB_8;
+ }
+ else
+ {
+ in_type = TYPE_RGB_16;
+ out_type = TYPE_RGB_16;
+ }
+ out_prof = cmsCreate_sRGBProfile();
+ new_space = OPJ_CLRSPC_SRGB;
+ }
+ else if(out_space == cmsSigGrayData) /* enumCS 17 */
+ {
+ in_type = TYPE_GRAY_8;
+ out_type = TYPE_RGB_8;
+ out_prof = cmsCreate_sRGBProfile();
+ new_space = OPJ_CLRSPC_SRGB;
+ }
+ else if(out_space == cmsSigYCbCrData) /* enumCS 18 */
+ {
+ in_type = TYPE_YCbCr_16;
+ out_type = TYPE_RGB_16;
+ out_prof = cmsCreate_sRGBProfile();
+ new_space = OPJ_CLRSPC_SRGB;
+ }
else
- {
+ {
#ifdef DEBUG_PROFILE
-fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
-"output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
-__FILE__,__LINE__,out_space,
- (out_space>>24) & 0xff,(out_space>>16) & 0xff,
- (out_space>>8) & 0xff, out_space & 0xff);
+ fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
+ "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
+ __FILE__,__LINE__,out_space,
+ (out_space>>24) & 0xff,(out_space>>16) & 0xff,
+ (out_space>>8) & 0xff, out_space & 0xff);
#endif
- return;
- }
+ cmsCloseProfile(in_prof);
+
+ return;
+ }
+ if(out_prof == NULL)
+ {
+ cmsCloseProfile(in_prof);
+ return;
+ }
#ifdef DEBUG_PROFILE
-fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
-"\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
- max_w,max_h, (void*)in_prof,(void*)out_prof);
-
-fprintf(stderr,"\trender_intent (%u)\n\t"
-"color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
-" type: in(%u) out:(%u)\n",
- intent,
- in_space,
- (in_space>>24) & 0xff,(in_space>>16) & 0xff,
- (in_space>>8) & 0xff, in_space & 0xff,
-
- out_space,
- (out_space>>24) & 0xff,(out_space>>16) & 0xff,
- (out_space>>8) & 0xff, out_space & 0xff,
-
- in_type,out_type
- );
+ fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
+ "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
+ max_w,max_h, (void*)in_prof,(void*)out_prof);
+
+ fprintf(stderr,"\trender_intent (%u)\n\t"
+ "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
+ " type: in(%u) out:(%u)\n",
+ intent,
+ in_space,
+ (in_space>>24) & 0xff,(in_space>>16) & 0xff,
+ (in_space>>8) & 0xff, in_space & 0xff,
+
+ out_space,
+ (out_space>>24) & 0xff,(out_space>>16) & 0xff,
+ (out_space>>8) & 0xff, out_space & 0xff,
+
+ in_type,out_type
+ );
#else
(void)prec;
(void)in_space;
#endif /* DEBUG_PROFILE */
- transform = cmsCreateTransform(in_prof, in_type,
- out_prof, out_type, intent, 0);
+ transform = cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
#ifdef OPJ_HAVE_LIBLCMS2
/* Possible for: LCMS_VERSION >= 2000 :*/
@@ -430,129 +481,217 @@ fprintf(stderr,"\trender_intent (%u)\n\t"
#endif
if(transform == NULL)
- {
+ {
#ifdef DEBUG_PROFILE
-fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
-"ICC Profile ignored.\n",__FILE__,__LINE__);
+ fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
+ "ICC Profile ignored.\n",__FILE__,__LINE__);
#endif
- image->color_space = oldspace;
+
#ifdef OPJ_HAVE_LIBLCMS1
- cmsCloseProfile(in_prof);
- cmsCloseProfile(out_prof);
+ cmsCloseProfile(in_prof);
+ cmsCloseProfile(out_prof);
#endif
- return;
- }
+ return;
+ }
if(image->numcomps > 2)/* RGB, RGBA */
- {
- if( prec <= 8 )
- {
- unsigned char *inbuf, *outbuf, *in, *out;
- max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
- in = inbuf = (unsigned char*)malloc(nr_samples);
- out = outbuf = (unsigned char*)malloc(nr_samples);
+ {
+ if( prec <= 8 )
+ {
+ unsigned char *inbuf, *outbuf, *in, *out;
+
+ max = max_w * max_h;
+ nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
+ in = inbuf = (unsigned char*)malloc(nr_samples);
+ out = outbuf = (unsigned char*)malloc(nr_samples);
+
+ if(inbuf == NULL || outbuf == NULL) goto fails0;
+
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+
+ for(i = 0U; i < max; ++i)
+ {
+ *in++ = (unsigned char)*r++;
+ *in++ = (unsigned char)*g++;
+ *in++ = (unsigned char)*b++;
+ }
+
+ cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+
+ for(i = 0U; i < max; ++i)
+ {
+ *r++ = (int)*out++;
+ *g++ = (int)*out++;
+ *b++ = (int)*out++;
+ }
+ ok = 1;
+
+fails0:
+ free(inbuf);
+ free(outbuf);
+ }
+ else /* prec > 8 */
+ {
+ unsigned short *inbuf, *outbuf, *in, *out;
+
+ max = max_w * max_h;
+ nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
+ in = inbuf = (unsigned short*)malloc(nr_samples);
+ out = outbuf = (unsigned short*)malloc(nr_samples);
+
+ if(inbuf == NULL || outbuf == NULL) goto fails1;
+
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+
+ for(i = 0U ; i < max; ++i)
+ {
+ *in++ = (unsigned short)*r++;
+ *in++ = (unsigned short)*g++;
+ *in++ = (unsigned short)*b++;
+ }
+
+ 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)
+ {
+ *r++ = (int)*out++;
+ *g++ = (int)*out++;
+ *b++ = (int)*out++;
+ }
+ ok = 1;
+
+fails1:
+ free(inbuf);
+ free(outbuf);
+ }
+ }
+ else /* image->numcomps <= 2 : GRAY, GRAYA */
+ {
+ if(prec <= 8)
+ {
+ unsigned char *in, *inbuf, *out, *outbuf;
+ opj_image_comp_t *new_comps;
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
+ max = max_w * max_h;
+ nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
+ in = inbuf = (unsigned char*)malloc(nr_samples);
+ out = outbuf = (unsigned char*)malloc(nr_samples);
+ g = (int*)calloc((size_t)max, sizeof(int));
+ b = (int*)calloc((size_t)max, sizeof(int));
- for(i = 0; i < max; ++i)
- {
- *in++ = (unsigned char)*r++;
- *in++ = (unsigned char)*g++;
- *in++ = (unsigned char)*b++;
- }
+ if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails2;
- cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+ new_comps = (opj_image_comp_t*)realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
+ if(new_comps == NULL) goto fails2;
- for(i = 0; i < max; ++i)
- {
- *r++ = (int)*out++;
- *g++ = (int)*out++;
- *b++ = (int)*out++;
- }
- free(inbuf); free(outbuf);
- }
- else
- {
- unsigned short *inbuf, *outbuf, *in, *out;
- max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
- in = inbuf = (unsigned short*)malloc(nr_samples);
- out = outbuf = (unsigned short*)malloc(nr_samples);
+ image->comps = new_comps;
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
+ if(image->numcomps == 2)
+ image->comps[3] = image->comps[1];
- for(i = 0; i < max; ++i)
- {
- *in++ = (unsigned short)*r++;
- *in++ = (unsigned short)*g++;
- *in++ = (unsigned short)*b++;
- }
+ image->comps[1] = image->comps[0];
+ image->comps[2] = image->comps[0];
- cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+ image->comps[1].data = g;
+ image->comps[2].data = b;
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
+ image->numcomps += 2;
- for(i = 0; i < max; ++i)
- {
- *r++ = (int)*out++;
- *g++ = (int)*out++;
- *b++ = (int)*out++;
- }
- free(inbuf); free(outbuf);
- }
- }
- else /* GRAY, GRAYA */
- {
- unsigned char *in, *inbuf, *out, *outbuf;
- max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
- in = inbuf = (unsigned char*)malloc(nr_samples);
- out = outbuf = (unsigned char*)malloc(nr_samples);
+ r = image->comps[0].data;
+
+ for(i = 0U; 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;
- image->comps = (opj_image_comp_t*)
- realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
+ for(i = 0U; i < max; ++i)
+ {
+ *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
+ }
+ r = g = b = NULL;
+ ok = 1;
- if(image->numcomps == 2)
- image->comps[3] = image->comps[1];
+fails2:
+ free(inbuf);
+ free(outbuf);
+ free(g);
+ free(b);
+ }
+ else /* prec > 8 */
+ {
+ unsigned short *in, *inbuf, *out, *outbuf;
+ opj_image_comp_t *new_comps;
- image->comps[1] = image->comps[0];
- image->comps[2] = image->comps[0];
+ max = max_w * max_h;
+ nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
+ in = inbuf = (unsigned short*)malloc(nr_samples);
+ out = outbuf = (unsigned short*)malloc(nr_samples);
+ g = (int*)calloc((size_t)max, sizeof(int));
+ b = (int*)calloc((size_t)max, sizeof(int));
- image->comps[1].data = (int*)calloc((size_t)max, sizeof(int));
- image->comps[2].data = (int*)calloc((size_t)max, sizeof(int));
+ if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails3;
- image->numcomps += 2;
+ new_comps = (opj_image_comp_t*)realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
- r = image->comps[0].data;
+ if(new_comps == NULL) goto fails3;
- for(i = 0; i < max; ++i)
- {
- *in++ = (unsigned char)*r++;
- }
- cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+ image->comps = new_comps;
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
+ if(image->numcomps == 2)
+ image->comps[3] = image->comps[1];
- for(i = 0; i < max; ++i)
- {
- *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
- }
- free(inbuf); free(outbuf);
+ image->comps[1] = image->comps[0];
+ image->comps[2] = image->comps[0];
- }/* if(image->numcomps */
+ image->comps[1].data = g;
+ image->comps[2].data = b;
+
+ image->numcomps += 2;
+
+ r = image->comps[0].data;
+
+ for(i = 0U; i < max; ++i)
+ {
+ *in++ = (unsigned short)*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)
+ {
+ *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
+ }
+ r = g = b = NULL;
+ ok = 1;
+
+fails3:
+ free(inbuf);
+ free(outbuf);
+ free(g);
+ free(b);
+ }
+ }/* if(image->numcomps > 2) */
cmsDeleteTransform(transform);
@@ -560,15 +699,18 @@ fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof);
#endif
+ if(ok)
+ {
+ image->color_space = new_space;
+ }
}/* color_apply_icc_profile() */
void color_cielab_to_rgb(opj_image_t *image)
{
int *row;
int enumcs, numcomps;
-
- image->color_space = OPJ_CLRSPC_SRGB;
-
+ OPJ_COLOR_SPACE new_space;
+
numcomps = (int)image->numcomps;
if(numcomps != 3)
@@ -595,8 +737,14 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsCIELab Lab;
in = cmsCreateLab4Profile(NULL);
+ if(in == NULL){
+ return;
+ }
out = cmsCreate_sRGBProfile();
-
+ if(out == NULL){
+ cmsCloseProfile(in);
+ return;
+ }
transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
#ifdef OPJ_HAVE_LIBLCMS2
@@ -611,6 +759,8 @@ void color_cielab_to_rgb(opj_image_t *image)
#endif
return;
}
+ new_space = OPJ_CLRSPC_SRGB;
+
prec0 = (double)image->comps[0].prec;
prec1 = (double)image->comps[1].prec;
prec2 = (double)image->comps[2].prec;
@@ -639,7 +789,9 @@ void color_cielab_to_rgb(opj_image_t *image)
red = dst0 = (int*)malloc(max * sizeof(int));
green = dst1 = (int*)malloc(max * sizeof(int));
blue = dst2 = (int*)malloc(max * sizeof(int));
-
+
+ if(red == NULL || green == NULL || blue == NULL) goto fails;
+
minL = -(rl * ol)/(pow(2, prec0)-1);
maxL = minL + rl;
@@ -670,16 +822,27 @@ void color_cielab_to_rgb(opj_image_t *image)
free(src1); image->comps[1].data = dst1;
free(src2); image->comps[2].data = dst2;
- image->color_space = OPJ_CLRSPC_SRGB;
+ image->color_space = new_space;
image->comps[0].prec = 16;
image->comps[1].prec = 16;
image->comps[2].prec = 16;
return;
+
+fails:
+ cmsDeleteTransform(transform);
+#ifdef OPJ_HAVE_LIBLCMS1
+ cmsCloseProfile(in);
+ cmsCloseProfile(out);
+#endif
+ if(red) free(red);
+ if(green) free(green);
+ if(blue) free(blue);
+ return;
}
fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs);
-}/* color_apply_conversion() */
+}/* color_cielab_to_rgb() */
#endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
@@ -744,7 +907,14 @@ void color_esycc_to_rgb(opj_image_t *image)
int flip_value = (1 << (image->comps[0].prec-1));
int max_value = (1 << image->comps[0].prec) - 1;
- if(image->numcomps < 3) return;
+ if (
+ (image->numcomps < 3)
+ || (image->comps[0].dx != image->comps[1].dx) || (image->comps[0].dx != image->comps[2].dx)
+ || (image->comps[0].dy != image->comps[1].dy) || (image->comps[0].dy != image->comps[2].dy)
+ ) {
+ fprintf(stderr,"%s:%d:color_esycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
+ return;
+ }
w = image->comps[0].w;
h = image->comps[0].h;
diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c
index 13713204..c130b3bf 100644
--- a/src/bin/jp2/convert.c
+++ b/src/bin/jp2/convert.c
@@ -611,6 +611,10 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
if (id_len)
{
unsigned char *id = (unsigned char *) malloc(id_len);
+ if(id == 0){
+ fprintf(stderr, "tga_readheader: memory out\n");
+ return 0;
+ }
if ( !fread(id, id_len, 1, fp) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
@@ -1249,6 +1253,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
{
name = (char*)malloc(total+1);
if (name == NULL) {
+ fprintf(stderr, "imagetopgx: memory out\n");
goto fin;
}
}
@@ -1757,7 +1762,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
const char *tmp = outfile;
char *destname;
- alpha = NULL;
+ alpha = NULL;
if((prec = (int)image->comps[0].prec) > 16)
{
@@ -1837,7 +1842,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
if(two)
{
v = *red + adjustR; ++red;
-if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@@ -1845,13 +1850,13 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
if(triple)
{
v = *green + adjustG; ++green;
-if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
v = *blue + adjustB; ++blue;
-if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@@ -1861,7 +1866,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
if(has_alpha)
{
v = *alpha + adjustA; ++alpha;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@@ -1871,28 +1876,28 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
} /* if(two) */
/* prec <= 8: */
- v = *red++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ v = *red++;
+ if(v > 255) v = 255; else if(v < 0) v = 0;
- fprintf(fdest, "%c", (unsigned char)v);
+ fprintf(fdest, "%c", (unsigned char)v);
if(triple)
- {
- v = *green++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ {
+ v = *green++;
+ if(v > 255) v = 255; else if(v < 0) v = 0;
- fprintf(fdest, "%c", (unsigned char)v);
- v = *blue++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ fprintf(fdest, "%c", (unsigned char)v);
+ v = *blue++;
+ if(v > 255) v = 255; else if(v < 0) v = 0;
- fprintf(fdest, "%c", (unsigned char)v);
- }
+ fprintf(fdest, "%c", (unsigned char)v);
+ }
if(has_alpha)
- {
- v = *alpha++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ {
+ v = *alpha++;
+ if(v > 255) v = 255; else if(v < 0) v = 0;
- fprintf(fdest, "%c", (unsigned char)v);
- }
+ fprintf(fdest, "%c", (unsigned char)v);
+ }
} /* for(i */
fclose(fdest); return 0;
@@ -1906,18 +1911,21 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
fprintf(stderr," is written to the file\n");
}
destname = (char*)malloc(strlen(outfile) + 8);
-
+ if(destname == NULL){
+ fprintf(stderr, "imagetopnm: memory out\n");
+ return 1;
+ }
for (compno = 0; compno < ncomp; compno++)
{
- if (ncomp > 1)
- {
- /*sprintf(destname, "%d.%s", compno, outfile);*/
- const size_t olen = strlen(outfile);
- const size_t dotpos = olen - 4;
-
- strncpy(destname, outfile, dotpos);
- sprintf(destname+dotpos, "_%u.pgm", compno);
- }
+ if (ncomp > 1)
+ {
+ /*sprintf(destname, "%d.%s", compno, outfile);*/
+ const size_t olen = strlen(outfile);
+ const size_t dotpos = olen - 4;
+
+ strncpy(destname, outfile, dotpos);
+ sprintf(destname+dotpos, "_%u.pgm", compno);
+ }
else
sprintf(destname, "%s", outfile);
@@ -1944,7 +1952,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
for (i = 0; i < wr * hr; i++)
{
v = *red + adjustR; ++red;
-if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@@ -1952,7 +1960,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
if(has_alpha)
{
v = *alpha++;
-if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@@ -1963,10 +1971,10 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
{
for(i = 0; i < wr * hr; ++i)
{
- v = *red + adjustR; ++red;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ v = *red + adjustR; ++red;
+ if(v > 255) v = 255; else if(v < 0) v = 0;
- fprintf(fdest, "%c", (unsigned char)v);
+ fprintf(fdest, "%c", (unsigned char)v);
}
}
fclose(fdest);
diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c
index 910574b8..d264823f 100644
--- a/src/bin/jp2/convertbmp.c
+++ b/src/bin/jp2/convertbmp.c
@@ -181,7 +181,7 @@ static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_imag
OPJ_UINT32 width, height;
OPJ_UINT32 x, y;
const OPJ_UINT8 *pSrc = NULL;
- OPJ_BOOL hasAlpha = OPJ_FALSE;
+ OPJ_BOOL hasAlpha;
OPJ_UINT32 redShift, redPrec;
OPJ_UINT32 greenShift, greenPrec;
OPJ_UINT32 blueShift, bluePrec;
@@ -239,7 +239,7 @@ static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_imag
OPJ_UINT32 width, height;
OPJ_UINT32 x, y;
const OPJ_UINT8 *pSrc = NULL;
- OPJ_BOOL hasAlpha = OPJ_FALSE;
+ OPJ_BOOL hasAlpha;
OPJ_UINT32 redShift, redPrec;
OPJ_UINT32 greenShift, greenPrec;
OPJ_UINT32 blueShift, bluePrec;
@@ -891,7 +891,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
fprintf(fdest, "%c%c%c", bc, gc, rc);
if ((i + 1) % w == 0) {
- for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
+ for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) /* ADD */
fprintf(fdest, "%c", 0);
}
}
@@ -967,7 +967,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
fprintf(fdest, "%c", (OPJ_UINT8)r);
if ((i + 1) % w == 0) {
- for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
+ for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) /* ADD */
fprintf(fdest, "%c", 0);
}
}
diff --git a/src/bin/jp2/convertpng.c b/src/bin/jp2/convertpng.c
index 8d117412..5635c7d0 100644
--- a/src/bin/jp2/convertpng.c
+++ b/src/bin/jp2/convertpng.c
@@ -185,9 +185,17 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
rows = (OPJ_BYTE**)calloc(height+1, sizeof(OPJ_BYTE*));
- for(i = 0; i < height; ++i)
+ if(rows == NULL){
+ fprintf(stderr, "pngtoimage: memory out\n");
+ goto fin;
+ }
+ for(i = 0; i < height; ++i){
rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png,info));
-
+ if(rows[i] == NULL){
+ fprintf(stderr,"pngtoimage: memory out\n");
+ goto fin;
+ }
+ }
png_read_image(png, rows);
/* Create image */
@@ -235,7 +243,7 @@ fin:
if(rows)
{
for(i = 0; i < height; ++i)
- free(rows[i]);
+ if(rows[i]) free(rows[i]);
free(rows);
}
if (row32s) {
diff --git a/src/bin/jp2/index.c b/src/bin/jp2/index.c
index af6e1381..478f556a 100644
--- a/src/bin/jp2/index.c
+++ b/src/bin/jp2/index.c
@@ -69,7 +69,7 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
return 1;
}
- if (cstr_info->tile[0].distotile)
+ if (cstr_info->tile[0].distotile > 0.0)
disto_on = 1;
else
disto_on = 0;
diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c
index 9d690a56..5a63a9d6 100644
--- a/src/bin/jp2/opj_compress.c
+++ b/src/bin/jp2/opj_compress.c
@@ -647,6 +647,10 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
raw_cp->rawBitDepth = bitdepth;
raw_cp->rawSigned = raw_signed;
raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(ncomp))*sizeof(raw_comp_cparameters_t));
+ if(raw_cp->rawComps == NULL){
+ free(substr1);
+ return 1;
+ }
for (compno = 0; compno < ncomp && !wrong; compno++) {
if (substr2 == NULL) {
raw_cp->rawComps[compno].dx = lastdx;
@@ -725,6 +729,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
numresolution = (OPJ_UINT32)parameters->numresolution;
matrix_width = numresolution * 3;
parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
+ if(parameters->cp_matrice == NULL){
+ return 1;
+ }
s = s + 2;
for (i = 0; i < numlayers; i++) {
@@ -995,6 +1002,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
case 'z': /* Image Directory path */
{
img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+ if(img_fol->imgdirpath == NULL){
+ return 1;
+ }
strcpy(img_fol->imgdirpath,opj_optarg);
img_fol->set_imgdir=1;
}
@@ -1540,6 +1550,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
}
return 0;
+
}
/* -------------------------------------------------------------------------- */
@@ -1635,7 +1646,7 @@ int main(int argc, char **argv) {
/* parse input and get user encoding parameters */
parameters.tcp_mct = (char) 255; /* This will be set later according to the input image or the provided option */
if(parse_cmdline_encoder(argc, argv, &parameters,&img_fol, &raw_cp, indexfilename, sizeof(indexfilename)) == 1) {
- return 1;
+ goto fails;
}
/* Read directory if necessary */
@@ -1848,7 +1859,9 @@ int main(int argc, char **argv) {
OPJ_BYTE *l_data;
OPJ_UINT32 l_data_size = 512*512*3;
l_data = (OPJ_BYTE*) calloc( 1,l_data_size);
- assert( l_data );
+ if(l_data == NULL){
+ goto fails;
+ }
for (i=0;i<l_nb_tiles;++i) {
if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
@@ -1904,4 +1917,16 @@ int main(int argc, char **argv) {
}
return 0;
+
+fails:
+ if(parameters.cp_comment) free(parameters.cp_comment);
+ if(parameters.cp_matrice) free(parameters.cp_matrice);
+ if(raw_cp.rawComps) free(raw_cp.rawComps);
+ if(img_fol.imgdirpath) free(img_fol.imgdirpath);
+ if(dirptr){
+ if(dirptr->filename_buf) free(dirptr->filename_buf);
+ if(dirptr->filename) free(dirptr->filename);
+ free(dirptr);
+ }
+ return 1;
}
diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c
index f3b1cd5c..ab7ff04a 100644
--- a/src/bin/jp2/opj_decompress.c
+++ b/src/bin/jp2/opj_decompress.c
@@ -680,6 +680,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *para
case 'y': /* Image Directory path */
{
img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+ if(img_fol->imgdirpath == NULL){
+ return 1;
+ }
strcpy(img_fol->imgdirpath,opj_optarg);
img_fol->set_imgdir=1;
}
@@ -1200,8 +1203,7 @@ int main(int argc, char **argv)
/* parse input and get user encoding parameters */
if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
- destroy_parameters(&parameters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
/* Initialize reading of directory */
@@ -1210,26 +1212,30 @@ int main(int argc, char **argv)
num_images=get_num_images(img_fol.imgdirpath);
dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
- if(dirptr){
- dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
- dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
+ if(!dirptr){
+ destroy_parameters(&parameters);
+ return EXIT_FAILURE;
+ }
+ dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
+ if(!dirptr->filename_buf){
+ failed = 1; goto fin;
+ }
+
+ dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
- if(!dirptr->filename_buf){
- destroy_parameters(&parameters);
- return EXIT_FAILURE;
- }
- for(it_image=0;it_image<num_images;it_image++){
- dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
- }
+ if(!dirptr->filename){
+ failed = 1; goto fin;
+ }
+ for(it_image=0;it_image<num_images;it_image++){
+ dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
}
+
if(load_images(dirptr,img_fol.imgdirpath)==1){
- destroy_parameters(&parameters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
if (num_images==0){
fprintf(stdout,"Folder is empty\n");
- destroy_parameters(&parameters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}else{
num_images=1;
@@ -1254,8 +1260,7 @@ int main(int argc, char **argv)
l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
if (!l_stream){
fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
- destroy_parameters(&parameters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
/* decode the JPEG2000 stream */
@@ -1297,21 +1302,19 @@ int main(int argc, char **argv)
/* Setup the decoder decoding parameters using user parameters */
if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){
fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
- destroy_parameters(&parameters);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
/* Read the main header of the codestream and if necessary the JP2 boxes*/
if(! opj_read_header(l_stream, l_codec, &image)){
fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
- destroy_parameters(&parameters);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(image);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
if (!parameters.nb_tile_to_decode) {
@@ -1319,21 +1322,19 @@ int main(int argc, char **argv)
if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
(OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
- destroy_parameters(&parameters);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(image);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
/* Get the decoded image */
if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n");
- destroy_parameters(&parameters);
opj_destroy_codec(l_codec);
opj_stream_destroy(l_stream);
opj_image_destroy(image);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}
else {
@@ -1344,16 +1345,15 @@ int main(int argc, char **argv)
opj_destroy_codec(l_codec);
opj_stream_destroy(l_stream);
opj_image_destroy(image);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}*/
if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
- destroy_parameters(&parameters);
opj_destroy_codec(l_codec);
opj_stream_destroy(l_stream);
opj_image_destroy(image);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
}
@@ -1432,9 +1432,8 @@ int main(int argc, char **argv)
image = upsample_image_components(image);
if (image == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n");
- destroy_parameters(&parameters);
opj_destroy_codec(l_codec);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}
@@ -1456,9 +1455,8 @@ int main(int argc, char **argv)
}
if (image == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
- destroy_parameters(&parameters);
opj_destroy_codec(l_codec);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}
@@ -1567,10 +1565,17 @@ int main(int argc, char **argv)
if(failed) (void)remove(parameters.outfile); /* ignore return value */
}
+fin:
destroy_parameters(&parameters);
+ if(failed && img_fol.imgdirpath) free(img_fol.imgdirpath);
+ if(dirptr){
+ if(dirptr->filename) free(dirptr->filename);
+ if(dirptr->filename_buf) free(dirptr->filename_buf);
+ free(dirptr);
+ }
if (numDecompressedImages) {
fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
}
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
-/*end main*/
+/*end main()*/
diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c
index bd608c2b..d62eea14 100644
--- a/src/bin/jp2/opj_dump.c
+++ b/src/bin/jp2/opj_dump.c
@@ -341,6 +341,9 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param
case 'y': /* Image Directory path */
{
img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+ if(img_fol->imgdirpath == NULL){
+ return 1;
+ }
strcpy(img_fol->imgdirpath,opj_optarg);
img_fol->set_imgdir=1;
}
@@ -442,6 +445,8 @@ int main(int argc, char *argv[])
/* Parse input and get user encoding parameters */
if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
+ if(img_fol.imgdirpath) free(img_fol.imgdirpath);
+
return EXIT_FAILURE;
}
@@ -451,25 +456,31 @@ int main(int argc, char *argv[])
num_images=get_num_images(img_fol.imgdirpath);
dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
- if(dirptr){
- dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
- dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
+ if(!dirptr){
+ return EXIT_FAILURE;
+ }
+ dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
+ if(!dirptr->filename_buf){
+ free(dirptr);
+ return EXIT_FAILURE;
+ }
+ dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
- if(!dirptr->filename_buf){
- return EXIT_FAILURE;
- }
+ if(!dirptr->filename){
+ goto fails;
+ }
- for(it_image=0;it_image<num_images;it_image++){
- dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
- }
+ for(it_image=0;it_image<num_images;it_image++){
+ dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
}
+
if(load_images(dirptr,img_fol.imgdirpath)==1){
- return EXIT_FAILURE;
+ goto fails;
}
if (num_images==0){
fprintf(stdout,"Folder is empty\n");
- return EXIT_FAILURE;
+ goto fails;
}
}else{
num_images=1;
@@ -480,7 +491,7 @@ int main(int argc, char *argv[])
fout = fopen(parameters.outfile,"w");
if (!fout){
fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
- return EXIT_FAILURE;
+ goto fails;
}
}
else
@@ -504,7 +515,7 @@ int main(int argc, char *argv[])
l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
if (!l_stream){
fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",parameters.infile);
- return EXIT_FAILURE;
+ goto fails;
}
/* Read the JPEG2000 stream */
@@ -546,7 +557,7 @@ int main(int argc, char *argv[])
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
fclose(fout);
- return EXIT_FAILURE;
+ goto fails;
}
/* Read the main header of the codestream and if necessary the JP2 boxes*/
@@ -556,7 +567,7 @@ int main(int argc, char *argv[])
opj_destroy_codec(l_codec);
opj_image_destroy(image);
fclose(fout);
- return EXIT_FAILURE;
+ goto fails;
}
opj_dump_codec(l_codec, img_fol.flag, fout );
@@ -588,4 +599,12 @@ int main(int argc, char *argv[])
fclose(fout);
return EXIT_SUCCESS;
+
+fails:
+ if(dirptr){
+ if(dirptr->filename) free(dirptr->filename);
+ if(dirptr->filename_buf) free(dirptr->filename_buf);
+ free(dirptr);
+ }
+ return EXIT_FAILURE;
}
diff --git a/src/lib/openjp2/bio.c b/src/lib/openjp2/bio.c
index e4edb372..269769b4 100644
--- a/src/lib/openjp2/bio.c
+++ b/src/lib/openjp2/bio.c
@@ -151,19 +151,31 @@ void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) {
bio->ct = 0;
}
+OPJ_NOSANITIZE("unsigned-integer-overflow")
void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n) {
OPJ_UINT32 i;
- for (i = n - 1; i < n; i--) {
+
+ assert((n > 0U) && (n <= 32U));
+ for (i = n - 1; i < n; i--) { /* overflow used for end-loop condition */
opj_bio_putbit(bio, (v >> i) & 1);
}
}
+OPJ_NOSANITIZE("unsigned-integer-overflow")
OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n) {
OPJ_UINT32 i;
- OPJ_UINT32 v;
- v = 0;
- for (i = n - 1; i < n; i--) {
- v += opj_bio_getbit(bio) << i;
+ OPJ_UINT32 v;
+
+ assert((n > 0U) /* && (n <= 32U)*/);
+#ifdef OPJ_UBSAN_BUILD
+ /* This assert fails for some corrupted images which are gracefully rejected */
+ /* Add this assert only for ubsan build. */
+ /* This is the condition for overflow not to occur below which is needed because of OPJ_NOSANITIZE */
+ assert(n <= 32U);
+#endif
+ v = 0U;
+ for (i = n - 1; i < n; i--) { /* overflow used for end-loop condition */
+ v |= opj_bio_getbit(bio) << i; /* can't overflow, opj_bio_getbit returns 0 or 1 */
}
return v;
}
diff --git a/src/lib/openjp2/cidx_manager.c b/src/lib/openjp2/cidx_manager.c
index ff2dbdaa..30561ed2 100644
--- a/src/lib/openjp2/cidx_manager.c
+++ b/src/lib/openjp2/cidx_manager.c
@@ -60,7 +60,9 @@ int opj_write_cidx( int offset, opj_stream_private_t *cio, opj_codestream_info_t
lenp = -1;
box = (opj_jp2_box_t *)opj_calloc( 32, sizeof(opj_jp2_box_t));
-
+ if(box == NULL){
+ return 0;
+ }
for (i=0;i<2;i++){
if(i)
diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c
index 20407498..8086b004 100644
--- a/src/lib/openjp2/j2k.c
+++ b/src/lib/openjp2/j2k.c
@@ -2063,17 +2063,17 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
/* testcase 4035.pdf.SIGSEGV.d8b.3375 */
/* testcase issue427-null-image-size.jp2 */
if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) {
- opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative or zero image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64 ")\n", (OPJ_INT64)l_image->x1 - l_image->x0, (OPJ_INT64)l_image->y1 - l_image->y0);
return OPJ_FALSE;
}
/* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
- if (!(l_cp->tdx * l_cp->tdy)) {
+ if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) {
opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy);
return OPJ_FALSE;
}
/* testcase 1610.pdf.SIGSEGV.59c.681 */
- if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
+ if ((0xFFFFFFFFU / l_image->x1) < l_image->y1) {
opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
return OPJ_FALSE;
}
@@ -2094,7 +2094,7 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
opj_event_msg(p_manager, EVT_ERROR,
"JPWL: bad image size (%d x %d)\n",
l_image->x1, l_image->y1);
- if (!JPWL_ASSUME || JPWL_ASSUME) {
+ if (!JPWL_ASSUME) {
opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
return OPJ_FALSE;
}
@@ -2154,10 +2154,16 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
opj_event_msg(p_manager, EVT_ERROR,
- "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
+ "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n",
i, l_img_comp->dx, l_img_comp->dy);
return OPJ_FALSE;
}
+ if( l_img_comp->prec > 38) { /* TODO openjpeg won't handle more than ? */
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm)\n",
+ i, l_img_comp->prec);
+ return OPJ_FALSE;
+ }
#ifdef USE_JPWL
if (l_cp->correct) {
@@ -2270,7 +2276,7 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
if (!l_cp->tcps) {
opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
"JPWL: could not alloc tcps field of cp\n");
- if (!JPWL_ASSUME || JPWL_ASSUME) {
+ if (!JPWL_ASSUME) {
opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
return OPJ_FALSE;
}
@@ -2436,22 +2442,22 @@ static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k,
l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
- opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
+ opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
l_current_data += 2;
- opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
+ opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
l_current_data += 2;
- opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
+ opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
++l_current_data;
- opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
+ opj_write_bytes(l_current_data,(OPJ_UINT32)l_tcp->prg,1); /* SGcod (A) */
++l_current_data;
- opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
+ opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
l_current_data+=2;
- opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
+ opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
++l_current_data;
l_remaining_size -= 9;
@@ -2578,6 +2584,9 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k,
p_j2k->cstr_info->prog = l_tcp->prg;
p_j2k->cstr_info->numlayers = l_tcp->numlayers;
p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
+ if(!p_j2k->cstr_info->numdecompos){
+ return OPJ_FALSE;
+ }
for (i = 0; i < l_image->numcomps; ++i) {
p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
}
@@ -3165,7 +3174,7 @@ static void opj_j2k_write_poc_in_memory( opj_j2k_t *p_j2k,
opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
l_current_data+=l_poc_room;
- opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
+ opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg,1); /* Ppoc_i */
++l_current_data;
/* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
@@ -4621,7 +4630,7 @@ static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
opj_event_msg(p_manager, EVT_ERROR,
"JPWL: bad component number in RGN (%d when there are only %d)\n",
l_comp_room, l_nb_comp);
- if (!JPWL_ASSUME || JPWL_ASSUME) {
+ if (!JPWL_ASSUME) {
opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
return OPJ_FALSE;
}
@@ -4706,7 +4715,7 @@ static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
l_rates = l_tcp->rates;
/* Modification of the RATE >> */
- if (*l_rates) {
+ if (*l_rates > 0.0f) {
*l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
/
((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
@@ -4718,7 +4727,7 @@ static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
++l_rates;
for (k = 1; k < l_tcp->numlayers; ++k) {
- if (*l_rates) {
+ if (*l_rates > 0.0f) {
*l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
/
((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
@@ -4741,11 +4750,11 @@ static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
for (j=0;j<l_cp->tw;++j) {
l_rates = l_tcp->rates;
- if (*l_rates) {
+ if (*l_rates > 0.0f) {
*l_rates -= l_sot_remove;
- if (*l_rates < 30) {
- *l_rates = 30;
+ if (*l_rates < 30.0f) {
+ *l_rates = 30.0f;
}
}
@@ -4755,22 +4764,22 @@ static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
for (k = 1; k < l_last_res; ++k) {
- if (*l_rates) {
+ if (*l_rates > 0.0f) {
*l_rates -= l_sot_remove;
- if (*l_rates < *(l_rates - 1) + 10) {
- *l_rates = (*(l_rates - 1)) + 20;
+ if (*l_rates < *(l_rates - 1) + 10.0f) {
+ *l_rates = (*(l_rates - 1)) + 20.0f;
}
}
++l_rates;
}
- if (*l_rates) {
+ if (*l_rates > 0.0f) {
*l_rates -= (l_sot_remove + 2.f);
- if (*l_rates < *(l_rates - 1) + 10) {
- *l_rates = (*(l_rates - 1)) + 20;
+ if (*l_rates < *(l_rates - 1) + 10.0f) {
+ *l_rates = (*(l_rates - 1)) + 20.0f;
}
}
@@ -5362,7 +5371,7 @@ static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k,
l_current_data+=l_nb_bytes_for_comp;
}
- l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
+ l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16;
if (p_mcc_record->m_decorrelation_array) {
l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
@@ -9334,16 +9343,19 @@ void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
/* Dump the codestream info from main header */
if (flag & OPJ_J2K_MH_INFO){
- opj_j2k_dump_MH_info(p_j2k, out_stream);
+ if (p_j2k->m_private_image)
+ opj_j2k_dump_MH_info(p_j2k, out_stream);
}
/* Dump all tile/codestream info */
if (flag & OPJ_J2K_TCH_INFO){
OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
OPJ_UINT32 i;
opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
- for (i=0;i<l_nb_tiles;++i) {
- opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
- ++l_tcp;
+ if (p_j2k->m_private_image) {
+ for (i=0;i<l_nb_tiles;++i) {
+ opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
+ ++l_tcp;
+ }
}
}
diff --git a/src/lib/openjp2/jp2.c b/src/lib/openjp2/jp2.c
index 6c6f6e83..a607c8a9 100644
--- a/src/lib/openjp2/jp2.c
+++ b/src/lib/openjp2/jp2.c
@@ -646,12 +646,13 @@ static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
{
OPJ_UINT32 i;
/* room for 8 bytes for box and 1 byte for each component */
- OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps;
+ OPJ_UINT32 l_bpcc_size;
OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
/* preconditions */
assert(jp2 != 00);
assert(p_nb_bytes_written != 00);
+ l_bpcc_size = 8 + jp2->numcomps;
l_bpcc_data = (OPJ_BYTE *) opj_calloc(1,l_bpcc_size);
if (l_bpcc_data == 00) {
@@ -1404,6 +1405,10 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
+ if(cielab == NULL){
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
+ return OPJ_FALSE;
+ }
cielab[0] = 14; /* enumcs */
/* default values */
@@ -1639,7 +1644,7 @@ static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager )
{
OPJ_UINT32 i;
- OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
+ OPJ_UINT32 l_ftyp_size;
OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
OPJ_BOOL l_result;
@@ -1647,6 +1652,7 @@ static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
assert(cio != 00);
assert(jp2 != 00);
assert(p_manager != 00);
+ l_ftyp_size = 16 + 4 * jp2->numcl;
l_ftyp_data = (OPJ_BYTE *) opj_calloc(1,l_ftyp_size);
diff --git a/src/lib/openjp2/opj_includes.h b/src/lib/openjp2/opj_includes.h
index 5add0918..58a5a9a9 100644
--- a/src/lib/openjp2/opj_includes.h
+++ b/src/lib/openjp2/opj_includes.h
@@ -112,6 +112,14 @@
#endif
#endif
+#ifdef __has_attribute
+ #if __has_attribute(no_sanitize)
+ #define OPJ_NOSANITIZE(kind) __attribute__((no_sanitize(kind)))
+ #endif
+#endif
+#ifndef OPJ_NOSANITIZE
+ #define OPJ_NOSANITIZE(kind)
+#endif
/* MSVC before 2013 and Borland C do not have lrintf */
diff --git a/src/lib/openjp2/phix_manager.c b/src/lib/openjp2/phix_manager.c
index 5a3e8838..45e559d4 100644
--- a/src/lib/openjp2/phix_manager.c
+++ b/src/lib/openjp2/phix_manager.c
@@ -57,7 +57,9 @@ int opj_write_phix( int coff, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused,
OPJ_OFF_T lenp = 0;
box = (opj_jp2_box_t *)opj_calloc( (size_t)cstr_info.numcomps, sizeof(opj_jp2_box_t));
-
+ if(box == NULL){
+ return 0;
+ }
for( i=0;i<2;i++){
if (i)
opj_stream_seek( cio, lenp, p_manager);
diff --git a/src/lib/openjp2/pi.c b/src/lib/openjp2/pi.c
index bfee10a2..cffad668 100644
--- a/src/lib/openjp2/pi.c
+++ b/src/lib/openjp2/pi.c
@@ -747,10 +747,12 @@ static void opj_get_all_encoding_parameters( const opj_image_t *p_image,
}
/* use custom size for precincts*/
- l_level_no = l_tccp->numresolutions - 1;
+ l_level_no = l_tccp->numresolutions;
for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
OPJ_UINT32 l_dx, l_dy;
+ --l_level_no;
+
/* precinct width and height*/
l_pdx = l_tccp->prcw[resno];
l_pdy = l_tccp->prch[resno];
@@ -782,7 +784,6 @@ static void opj_get_all_encoding_parameters( const opj_image_t *p_image,
*p_max_prec = l_product;
}
- --l_level_no;
}
++l_tccp;
++l_img_comp;
diff --git a/src/lib/openjp2/ppix_manager.c b/src/lib/openjp2/ppix_manager.c
index fce51489..018a8812 100644
--- a/src/lib/openjp2/ppix_manager.c
+++ b/src/lib/openjp2/ppix_manager.c
@@ -61,7 +61,9 @@ int opj_write_ppix( int coff, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused,
lenp = -1;
box = (opj_jp2_box_t *)opj_calloc( (size_t)cstr_info.numcomps, sizeof(opj_jp2_box_t));
-
+ if(box == NULL){
+ return 0;
+ }
for (i=0;i<2;i++){
if (i)
diff --git a/src/lib/openjp2/raw.c b/src/lib/openjp2/raw.c
index 2498761c..d3581d1a 100644
--- a/src/lib/openjp2/raw.c
+++ b/src/lib/openjp2/raw.c
@@ -88,7 +88,7 @@ OPJ_UINT32 opj_raw_decode(opj_raw_t *raw) {
}
}
raw->ct--;
- d = (raw->c >> raw->ct) & 0x01;
+ d = ((OPJ_UINT32)raw->c >> raw->ct) & 0x01U;
return d;
}
diff --git a/src/lib/openjp2/t1.c b/src/lib/openjp2/t1.c
index 05a3da66..1e9480bd 100644
--- a/src/lib/openjp2/t1.c
+++ b/src/lib/openjp2/t1.c
@@ -381,7 +381,7 @@ static void opj_t1_enc_sigpass_step( opj_t1_t *t1,
flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
- v = opj_int_abs(*datap) & one ? 1 : 0;
+ v = (opj_int_abs(*datap) & one) ? 1 : 0;
opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
@@ -625,7 +625,7 @@ static void opj_t1_enc_refpass_step( opj_t1_t *t1,
flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
*nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
- v = opj_int_abs(*datap) & one ? 1 : 0;
+ v = (opj_int_abs(*datap) & one) ? 1 : 0;
opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
@@ -849,7 +849,7 @@ static void opj_t1_enc_clnpass_step(
}
if (!(*flagsp & (T1_SIG | T1_VISIT))) {
opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
- v = opj_int_abs(*datap) & one ? 1 : 0;
+ v = (opj_int_abs(*datap) & one) ? 1 : 0;
opj_mqc_encode(mqc, (OPJ_UINT32)v);
if (v) {
LABEL_PARTIAL:
diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c
index af6b53f5..7ecd97cf 100644
--- a/src/lib/openjp2/tcd.c
+++ b/src/lib/openjp2/tcd.c
@@ -486,8 +486,7 @@ OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
OPJ_FLOAT64 lo = min;
OPJ_FLOAT64 hi = max;
- OPJ_BOOL success = OPJ_FALSE;
- OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
+ OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
OPJ_FLOAT64 goodthresh = 0;
OPJ_FLOAT64 stable_thresh = 0;
OPJ_UINT32 i;
@@ -500,7 +499,7 @@ OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
-r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
-q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
==> possible to have some lossy layers and the last layer for sure lossless */
- if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
+ if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0.0f)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0.0))) {
opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
OPJ_FLOAT64 thresh = 0;
@@ -559,19 +558,13 @@ OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
}
}
- success = OPJ_TRUE;
goodthresh = stable_thresh == 0? thresh : stable_thresh;
opj_t2_destroy(t2);
} else {
- success = OPJ_TRUE;
goodthresh = min;
}
- if (!success) {
- return OPJ_FALSE;
- }
-
if(cstr_info) { /* Threshold for Marcela Index */
cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
}
@@ -778,7 +771,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
l_tilec->resolutions_size = l_data_size;
}
- l_level_no = l_tilec->numresolutions - 1;
+ l_level_no = l_tilec->numresolutions;
l_res = l_tilec->resolutions;
l_step_size = l_tccp->stepsizes;
if (l_tccp->qmfbid == 0) {
@@ -795,6 +788,8 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
+ --l_level_no;
+
/* border for each resolution level (global) */
l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
@@ -1024,7 +1019,6 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
++l_step_size;
} /* bandno */
++l_res;
- --l_level_no;
} /* resno */
++l_tccp;
++l_tilec;
diff --git a/src/lib/openjp2/thix_manager.c b/src/lib/openjp2/thix_manager.c
index 0967b1e9..0bd79897 100644
--- a/src/lib/openjp2/thix_manager.c
+++ b/src/lib/openjp2/thix_manager.c
@@ -49,7 +49,9 @@ int opj_write_thix( int coff, opj_codestream_info_t cstr_info, opj_stream_privat
lenp = 0;
box = (opj_jp2_box_t *)opj_calloc( (size_t)(cstr_info.tw*cstr_info.th), sizeof(opj_jp2_box_t));
-
+ if(box == NULL){
+ return 0;
+ }
for ( i = 0; i < 2 ; i++ ){
if (i)
opj_stream_seek( cio, lenp, p_manager);