X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fbin%2Fjp2%2Fconvert.c;h=940ac2fa80d7cd0faa7ace64d0ab7581223460b4;hb=319fc971fef8a1e1c1c543506c26805873e3f258;hp=73ec9f6e68a823b370da954cc5bcc7a1ee3d5110;hpb=4123c0ba864fbe0cb52746c4aac18a15c31b1f62;p=openjpeg.git diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 73ec9f6e..940ac2fa 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; @@ -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; @@ -1908,7 +1911,11 @@ 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"); + fclose(fdest); + return 1; + } for (compno = 0; compno < ncomp; compno++) { if (ncomp > 1) @@ -1918,7 +1925,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0; const size_t dotpos = olen - 4; strncpy(destname, outfile, dotpos); - sprintf(destname+dotpos, "_%d.pgm", compno); + sprintf(destname+dotpos, "_%u.pgm", compno); } else sprintf(destname, "%s", outfile); @@ -2155,7 +2162,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;