d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
+ if(r == NULL || g == NULL || b == NULL) goto fails;
+
for(i = 0U; i < max; ++i)
{
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
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;
+ return;
+
+fails:
+ if(r) free(r);
+ if(g) free(g);
+ if(b) free(b);
}/* sycc444_to_rgb() */
d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
+ if(r == NULL || g == NULL || b == NULL) goto fails;
+
for(i=0U; i < maxh; ++i)
{
for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
img->comps[2].dx = img->comps[0].dx;
img->comps[1].dy = img->comps[0].dy;
img->comps[2].dy = img->comps[0].dy;
+ return;
+
+fails:
+ if(r) free(r);
+ if(g) free(g);
+ if(b) free(b);
}/* sycc422_to_rgb() */
d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
+ if(r == NULL || g == NULL || b == NULL) goto fails;
+
for(i=0U; i < (maxh & ~(unsigned int)1U); i += 2U)
{
ny = y + maxw;
img->comps[2].dx = img->comps[0].dx;
img->comps[1].dy = img->comps[0].dy;
img->comps[2].dy = img->comps[0].dy;
+ return;
+
+fails:
+ if(r) free(r);
+ if(g) free(g);
+ if(b) free(b);
}/* sycc420_to_rgb() */
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;
+ int prec, i, max, max_w, max_h, ok = 0;
+ OPJ_COLOR_SPACE new_space;
in_prof =
cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
max_w = (int)image->comps[0].w;
max_h = (int)image->comps[0].h;
prec = (int)image->comps[0].prec;
- oldspace = image->color_space;
if(out_space == cmsSigRgbData) /* enumCS 16 */
{
out_type = TYPE_RGB_16;
}
out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
+ 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();
- image->color_space = OPJ_CLRSPC_SRGB;
+ 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();
- image->color_space = OPJ_CLRSPC_SRGB;
+ new_space = OPJ_CLRSPC_SRGB;
}
else
{
(out_space>>24) & 0xff,(out_space>>16) & 0xff,
(out_space>>8) & 0xff, out_space & 0xff);
#endif
+ cmsCloseProfile(in_prof);
+
+ return;
+ }
+ if(out_prof == NULL)
+ {
+ cmsCloseProfile(in_prof);
+
return;
}
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);
if( prec <= 8 )
{
unsigned char *inbuf, *outbuf, *in, *out;
+
max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
+ nr_samples = (size_t)(max * 3 * 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;
*g++ = (int)*out++;
*b++ = (int)*out++;
}
- free(inbuf); free(outbuf);
+ ok = 1;
+
+fails0:
+ if(inbuf) free(inbuf);
+ if(outbuf) free(outbuf);
}
- else
+ else /* prec > 8 */
{
unsigned short *inbuf, *outbuf, *in, *out;
+
max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
+ nr_samples = (size_t)(max * 3 * 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;
*g++ = (int)*out++;
*b++ = (int)*out++;
}
- free(inbuf); free(outbuf);
+ ok = 1;
+
+fails1:
+ if(inbuf) free(inbuf);
+ if(outbuf) free(outbuf);
}
}
- else /* GRAY, GRAYA */
+ else /* image->numcomps <= 2 : GRAY, GRAYA */
{
+ if(prec <= 8)
+ {
unsigned char *in, *inbuf, *out, *outbuf;
+ opj_image_comp_t *new_comps;
+
max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
+ 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));
+
+ if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails2;
- image->comps = (opj_image_comp_t*)
+ new_comps = (opj_image_comp_t*)
realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
+ if(new_comps == NULL) goto fails2;
+
+ image->comps = new_comps;
+
if(image->numcomps == 2)
image->comps[3] = image->comps[1];
image->comps[1] = image->comps[0];
image->comps[2] = image->comps[0];
- image->comps[1].data = (int*)calloc((size_t)max, sizeof(int));
- image->comps[2].data = (int*)calloc((size_t)max, sizeof(int));
+ image->comps[1].data = g;
+ image->comps[2].data = b;
image->numcomps += 2;
r = image->comps[0].data;
for(i = 0; i < max; ++i)
- {
+ {
*in++ = (unsigned char)*r++;
- }
+ }
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].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;
+
+fails2:
+ if(inbuf) free(inbuf);
+ if(outbuf) free(outbuf);
+ if(g) free(g);
+ if(b) free(b);
}
- free(inbuf); free(outbuf);
+ else /* prec > 8 */
+ {
+ unsigned short *in, *inbuf, *out, *outbuf;
+ opj_image_comp_t *new_comps;
+
+ max = max_w * max_h;
+ nr_samples = (size_t)(max * 3 * 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));
+
+ if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails3;
+
+ new_comps = (opj_image_comp_t*)
+ realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
+
+ if(new_comps == NULL) goto fails3;
+
+ image->comps = new_comps;
- }/* if(image->numcomps */
+ if(image->numcomps == 2)
+ image->comps[3] = image->comps[1];
+
+ image->comps[1] = image->comps[0];
+ image->comps[2] = image->comps[0];
+
+ image->comps[1].data = g;
+ image->comps[2].data = b;
+
+ image->numcomps += 2;
+
+ r = image->comps[0].data;
+
+ for(i = 0; 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:
+ if(inbuf) free(inbuf);
+ if(outbuf) free(outbuf);
+ if(g) free(g);
+ if(b) free(b);
+ }
+ }/* if(image->numcomps > 2) */
cmsDeleteTransform(transform);
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)
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
#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;
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;
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 */
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");
{
name = (char*)malloc(total+1);
if (name == NULL) {
+ fprintf(stderr, "imagetopgx: memory out\n");
goto fin;
}
}
fprintf(stderr," is written to the file\n");
}
destname = (char*)malloc(strlen(outfile) + 8);
-
+ if(destname == NULL){
+ fprintf(stderr, "imagetopnm: memory out\n");
+ fclose(fdest);
+ return 1;
+ }
for (compno = 0; compno < ncomp; compno++)
{
if (ncomp > 1)
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;
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;
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);
}
}
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);
}
}
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 */
if(rows)
{
for(i = 0; i < height; ++i)
- free(rows[i]);
+ if(rows[i]) free(rows[i]);
free(rows);
}
if (row32s) {
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;
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++) {
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;
}
}
return 0;
+
}
/* -------------------------------------------------------------------------- */
/* 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, ¶meters,&img_fol, &raw_cp, indexfilename, sizeof(indexfilename)) == 1) {
- return 1;
+ goto fails;
}
/* Read directory if necessary */
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);
}
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;
}
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;
}
/* parse input and get user encoding parameters */
if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
- destroy_parameters(¶meters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
/* Initialize reading of directory */
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(¶meters);
+ 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(¶meters);
- 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(¶meters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
if (num_images==0){
fprintf(stdout,"Folder is empty\n");
- destroy_parameters(¶meters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}else{
num_images=1;
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(¶meters);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
/* decode the JPEG2000 stream */
/* 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(¶meters);
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(¶meters);
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) {
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(¶meters);
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(¶meters);
opj_destroy_codec(l_codec);
opj_stream_destroy(l_stream);
opj_image_destroy(image);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}
else {
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(¶meters);
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);
}
image = upsample_image_components(image);
if (image == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n");
- destroy_parameters(¶meters);
opj_destroy_codec(l_codec);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}
}
if (image == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
- destroy_parameters(¶meters);
opj_destroy_codec(l_codec);
- return EXIT_FAILURE;
+ failed = 1; goto fin;
}
}
if(failed) (void)remove(parameters.outfile); /* ignore return value */
}
+fin:
destroy_parameters(¶meters);
+ 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()*/
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;
}
/* Parse input and get user encoding parameters */
if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
+ if(img_fol.imgdirpath) free(img_fol.imgdirpath);
+
return EXIT_FAILURE;
}
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;
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
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 */
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*/
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 );
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;
}
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)
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;
}
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;
}
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;
}
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;
}
{
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) {
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 */
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;
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);
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);
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)
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);
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);
}
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:
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);
l_data_size = (OPJ_UINT32)tile_width * (OPJ_UINT32)tile_height * (OPJ_UINT32)num_comps * (OPJ_UINT32)(comp_prec/8);
l_data = (OPJ_BYTE*) malloc(l_data_size * sizeof(OPJ_BYTE));
-
+ if(l_data == NULL){
+ return 1;
+ }
fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n");
for (i=0;i<l_data_size;++i) {
l_data[i] = (OPJ_BYTE)i; /*rand();*/
l_codec = opj_create_compress(OPJ_CODEC_J2K);
}
if (!l_codec) {
+ free(l_data);
return 1;
}
l_image = opj_image_tile_create(num_comps,l_params,OPJ_CLRSPC_SRGB);
if (! l_image) {
+ free(l_data);
opj_destroy_codec(l_codec);
return 1;
}
fprintf(stderr, "ERROR -> test_tile_encoder: failed to setup the codec!\n");
opj_destroy_codec(l_codec);
opj_image_destroy(l_image);
+ free(l_data);
return 1;
}
fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the stream from the output file %s !\n",output_file );
opj_destroy_codec(l_codec);
opj_image_destroy(l_image);
+ free(l_data);
return 1;
}
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(l_image);
+ free(l_data);
return 1;
}
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(l_image);
+ free(l_data);
return 1;
}
}
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(l_image);
+ free(l_data);
return 1;
}