return 0;
}
- if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
+ if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) {
+ fclose(f);
return NULL;
+ }
/* We currently only support 24 & 32 bit tga's ... */
- if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
+ if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
+ fclose(f);
return NULL;
+ }
/* initialize image components */
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
/* create the image */
image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if (!image)
+ if (!image) {
+ fclose(f);
return NULL;
+ }
+
/* set image offset and reference grid */
image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&g, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&r, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&g, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&r, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&a, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
}
}
+ fclose(f);
return image;
}
if ((image->comps[0].dx != image->comps[i+1].dx)
||(image->comps[0].dy != image->comps[i+1].dy)
||(image->comps[0].prec != image->comps[i+1].prec)) {
+ fclose(fdest);
fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
return 1;
}
fseek(f, 0, SEEK_SET);
if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
+ fclose(f);
fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
return NULL;
}
} else if (endian2=='M' && endian1=='L') {
bigendian = 0;
} else {
+ fclose(f);
fprintf(stderr, "Bad pgx header, please check input file\n");
return NULL;
}
FILE *fdest = NULL;
for (compno = 0; compno < image->numcomps; compno++)
- {
+ {
opj_image_comp_t *comp = &image->comps[compno];
char bname[256]; /* buffer for name */
char *name = bname; /* pointer */
const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
if( outfile[dotpos] != '.' )
- {
+ {
/* `pgx` was recognized but there is no dot at expected position */
fprintf(stderr, "ERROR -> Impossible happen." );
goto fin;
- }
+ }
if( total > 256 )
- {
+ {
name = (char*)malloc(total+1);
- }
+ if (name == NULL) {
+ goto fin;
+ }
+ }
strncpy(name, outfile, dotpos);
sprintf(name+dotpos, "_%d.pgx", compno);
fdest = fopen(name, "wb");
/* dont need name anymore */
- if( total > 256 ) free(name);
+
if (!fdest)
- {
+ {
+
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
+ if( total > 256 ) free(name);
goto fin;
- }
+ }
w = (int)image->comps[compno].w;
h = (int)image->comps[compno].h;
nbytes = 4;
for (i = 0; i < w * h; i++)
- {
+ {
/* FIXME: clamp func is being called within a loop */
const int val = clamp(image->comps[compno].data[i],
(int)comp->prec, (int)comp->sgnd);
for (j = nbytes - 1; j >= 0; j--)
- {
+ {
int v = (int)(val >> (j * 8));
unsigned char byte = (unsigned char)v;
res = fwrite(&byte, 1, 1, fdest);
if( res < 1 )
- {
+ {
fprintf(stderr, "failed to write 1 byte for %s\n", name);
+ if( total > 256 ) free(name);
goto fin;
- }
- }
- }
+ }
+ }
+ }
+ if( total > 256 ) free(name);
fclose(fdest); fdest = NULL;
- }
+ }
fails = 0;
fin:
if(fdest) fclose(fdest);
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(fp);
return NULL;
}
if(one)
if (!cmptparm) {
fprintf(stderr, "Failed to allocate image components parameters !!\n");
fprintf(stderr,"Aborting\n");
+ fclose(f);
return NULL;
}
/* initialize image components */
for (i = 0; i < nloop; i++) {
if (!fread(&value, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
unsigned char temp2;
if (!fread(&temp1, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if (!fread(&temp2, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if( big_endian )
}
else {
fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}