X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fbin%2Fjp2%2Fconvert.c;h=c130b3bfbc2ed2453388f16db05cd595954c6943;hb=17a0a8a195a9aad76a6bdb174edc8aa5fb8b7831;hp=a894a369b9c75721a4437dd5621be9e99d3cb185;hpb=3109759fd772ead96ceb851ea760e6596277da22;p=openjpeg.git diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index a894a369..c130b3bf 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -114,6 +114,7 @@ static void scale_component_up(opj_image_comp_t* component, OPJ_UINT32 precision } } component->prec = precision; + component->bpp = precision; } void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision) { @@ -557,14 +558,12 @@ struct tga_header }; #endif /* INFORMATION_ONLY */ -static unsigned short get_ushort(unsigned short val) { - +static unsigned short get_ushort(const unsigned char *data) { + unsigned short val = *(const unsigned short *)data; #ifdef OPJ_BIG_ENDIAN - return (unsigned short)(((val & 0xffU) << 8) | (val >> 8)); -#else - return val; + val = ((val & 0xffU) << 8) | (val >> 8); #endif - + return val; } #define TGA_HEADER_SIZE 18 @@ -573,7 +572,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, unsigned int *width, unsigned int *height, int *flip_image) { int palette_size; - unsigned char *tga ; + unsigned char tga[TGA_HEADER_SIZE]; unsigned char id_len, /*cmap_type,*/ image_type; unsigned char pixel_depth, image_desc; unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size; @@ -581,31 +580,28 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, if (!bits_per_pixel || !width || !height || !flip_image) return 0; - tga = (unsigned char*)malloc(18); if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0 ; } - id_len = (unsigned char)tga[0]; - /*cmap_type = (unsigned char)tga[1];*/ - image_type = (unsigned char)tga[2]; - /*cmap_index = get_ushort(*(unsigned short*)(&tga[3]));*/ - cmap_len = get_ushort(*(unsigned short*)(&tga[5])); - cmap_entry_size = (unsigned char)tga[7]; + id_len = tga[0]; + /*cmap_type = tga[1];*/ + image_type = tga[2]; + /*cmap_index = get_ushort(&tga[3]);*/ + cmap_len = get_ushort(&tga[5]); + cmap_entry_size = tga[7]; #if 0 - x_origin = get_ushort(*(unsigned short*)(&tga[8])); - y_origin = get_ushort(*(unsigned short*)(&tga[10])); + x_origin = get_ushort(&tga[8]); + y_origin = get_ushort(&tga[10]); #endif - image_w = get_ushort(*(unsigned short*)(&tga[12])); - image_h = get_ushort(*(unsigned short*)(&tga[14])); - pixel_depth = (unsigned char)tga[16]; - image_desc = (unsigned char)tga[17]; - - free(tga); + image_w = get_ushort(&tga[12]); + image_h = get_ushort(&tga[14]); + pixel_depth = tga[16]; + image_desc = tga[17]; *bits_per_pixel = (unsigned int)pixel_depth; *width = (unsigned int)image_w; @@ -615,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"); @@ -737,12 +737,16 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { 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)); @@ -775,8 +779,11 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { /* 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; @@ -804,18 +811,21 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { { 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; } @@ -834,24 +844,28 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { { 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; } @@ -866,6 +880,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { fprintf(stderr, "Currently unsupported bit depth : %s\n", filename); } } + fclose(f); return image; } @@ -892,6 +907,7 @@ int imagetotga(opj_image_t * image, const char *outfile) { 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; } @@ -1084,6 +1100,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { 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; } @@ -1101,6 +1118,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { } else if (endian2=='M' && endian1=='L') { bigendian = 0; } else { + fclose(f); fprintf(stderr, "Bad pgx header, please check input file\n"); return NULL; } @@ -1215,7 +1233,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) 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 */ @@ -1226,25 +1244,31 @@ int imagetopgx(opj_image_t * image, const char *outfile) 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) { + fprintf(stderr, "imagetopgx: memory out\n"); + goto fin; + } + } strncpy(name, outfile, dotpos); - sprintf(name+dotpos, "_%d.pgx", compno); + sprintf(name+dotpos, "_%u.pgx", compno); fdest = fopen(name, "wb"); - /* dont need name anymore */ - if( total > 256 ) free(name); + /* don't need name anymore */ + 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; @@ -1260,26 +1284,28 @@ int imagetopgx(opj_image_t * image, const char *outfile) 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); @@ -1316,7 +1342,7 @@ static char *skip_int(char *start, int *out_n) char *s; char c; - *out_n = 0; s = start; + *out_n = 0; s = skip_white(start); if(s == NULL) return NULL; @@ -1653,6 +1679,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); + fclose(fp); return NULL; } if(one) @@ -1735,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) { @@ -1788,7 +1815,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) { const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA"); - fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n" + fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n" "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(), wr, hr, ncomp, max, tt); alpha = image->comps[ncomp - 1].data; @@ -1815,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); @@ -1823,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); @@ -1839,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); @@ -1849,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; @@ -1884,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, "_%d.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); @@ -1922,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); @@ -1930,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); @@ -1941,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); @@ -2006,6 +2036,7 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p if (!cmptparm) { fprintf(stderr, "Failed to allocate image components parameters !!\n"); fprintf(stderr,"Aborting\n"); + fclose(f); return NULL; } /* initialize image components */ @@ -2039,6 +2070,8 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p 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; @@ -2055,10 +2088,14 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p 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 ) @@ -2075,6 +2112,8 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p } else { fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n"); + opj_image_destroy(image); + fclose(f); return NULL; } @@ -2122,7 +2161,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL for(compno = 0; compno < image->numcomps; compno++) { - fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w, + fprintf(stdout,"Component %u characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w, image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned"); w = (int)image->comps[compno].w;