X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fbin%2Fjp2%2Fconvert.c;h=c130b3bfbc2ed2453388f16db05cd595954c6943;hb=0954bc11e3ab6a39d86e5ed51286da4b8989743d;hp=73ec9f6e68a823b370da954cc5bcc7a1ee3d5110;hpb=4123c0ba864fbe0cb52746c4aac18a15c31b1f62;p=openjpeg.git diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 73ec9f6e..c130b3bf 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -558,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 @@ -588,22 +586,22 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, 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]; + 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; @@ -613,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"); @@ -1251,11 +1253,12 @@ 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; } } strncpy(name, outfile, dotpos); - sprintf(name+dotpos, "_%d.pgx", compno); + sprintf(name+dotpos, "_%u.pgx", compno); fdest = fopen(name, "wb"); /* don't need name anymore */ @@ -1339,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; @@ -1759,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) { @@ -1812,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; @@ -1839,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); @@ -1847,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); @@ -1863,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); @@ -1873,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; @@ -1908,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); @@ -1946,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); @@ -1954,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); @@ -1965,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); @@ -2155,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;