};
#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
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;
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;
}
}
const char *tmp = outfile;
char *destname;
- alpha = NULL;
+ alpha = NULL;
if((prec = (int)image->comps[0].prec) > 16)
{
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);
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);
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);
} /* 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;
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, "_%u.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);
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);
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);
{
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);