<<-- <<-- <<-- <<-- */
-// TGA header definition.
-#pragma pack(push,1) // Pack structure byte aligned
-typedef struct tga_header
+#ifdef INFORMATION_ONLY
+/* TGA header definition. */
+struct tga_header
{
unsigned char id_length; /* Image id field length */
unsigned char colour_map_type; /* Colour map type */
unsigned short image_height; /* Image height */
unsigned char pixel_depth; /* Pixel depth */
unsigned char image_desc; /* Image descriptor */
-} tga_header;
-#pragma pack(pop) // Return to normal structure packing alignment.
+};
+#endif /* INFORMATION_ONLY */
+
+static unsigned short get_ushort(unsigned short val) {
+
+#ifdef ORDER_BIGENDIAN
+ return( ((val & 0xff) << 8) + (val >> 8) );
+#else
+ return( val );
+#endif
+
+}
+
+#define TGA_HEADER_SIZE 18
int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
unsigned int *width, unsigned int *height, int *flip_image)
{
int palette_size;
- tga_header tga ;
+ unsigned char *tga ;
+ unsigned char id_len, cmap_type, image_type;
+ unsigned char pixel_depth, image_desc;
+ unsigned short cmap_index, cmap_len, cmap_entry_size;
+ unsigned short x_origin, y_origin, image_w, image_h;
if (!bits_per_pixel || !width || !height || !flip_image)
return 0;
-
- // Read TGA header
- if ( fread((unsigned char*)&tga, sizeof(tga_header), 1, fp) != 1 )
+ 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];
- *bits_per_pixel = tga.pixel_depth;
-
- *width = tga.image_width;
- *height = tga.image_height ;
- // Ignore tga identifier, if present ...
- if (tga.id_length)
+ x_origin = get_ushort(*(unsigned short*)(&tga[8]));
+ y_origin = get_ushort(*(unsigned short*)(&tga[10]));
+ 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);
+
+ *bits_per_pixel = (unsigned int)pixel_depth;
+ *width = (unsigned int)image_w;
+ *height = (unsigned int)image_h;
+
+ /* Ignore tga identifier, if present ... */
+ if (id_len)
{
- unsigned char *id = (unsigned char *) malloc(tga.id_length);
- if ( !fread(id, tga.id_length, 1, fp) )
+ unsigned char *id = (unsigned char *) malloc(id_len);
+ if ( !fread(id, id_len, 1, fp) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
free(id);
free(id);
}
- // Test for compressed formats ... not yet supported ...
+ /* Test for compressed formats ... not yet supported ...
// Note :- 9 - RLE encoded palettized.
- // 10 - RLE encoded RGB.
- if (tga.image_type > 8)
+ // 10 - RLE encoded RGB. */
+ if (image_type > 8)
{
fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
return 0 ;
}
- *flip_image = !(tga.image_desc & 32);
+ *flip_image = !(image_desc & 32);
- // Palettized formats are not yet supported, skip over the palette, if present ...
- palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
+ /* Palettized formats are not yet supported, skip over the palette, if present ... */
+ palette_size = cmap_len * (cmap_entry_size/8);
if (palette_size>0)
{
int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
opj_bool flip_image)
{
- tga_header tga;
+ unsigned short image_w, image_h, us0;
+ unsigned char uc0, image_type;
+ unsigned char pixel_depth, image_desc;
if (!bits_per_pixel || !width || !height)
return 0;
- memset(&tga, 0, sizeof(tga_header));
+ pixel_depth = 0;
if ( bits_per_pixel < 256 )
- tga.pixel_depth = (unsigned char)bits_per_pixel;
+ pixel_depth = (unsigned char)bits_per_pixel;
else{
fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
return 0;
}
- tga.image_width = (unsigned short)width;
- tga.image_height = (unsigned short)height;
- tga.image_type = 2; // Uncompressed.
- tga.image_desc = 8; // 8 bits per component.
+ uc0 = 0;
- if (flip_image)
- tga.image_desc |= 32;
+ if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
+ if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
+
+ image_type = 2; /* Uncompressed. */
+ if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
+
+ us0 = 0;
+ if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
+ if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
+ if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
- // Write TGA header
- fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
+ if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
+ if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
+
+ image_w = (unsigned short)width;
+ image_h = (unsigned short) height;
+
+ if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
+ if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
+
+ if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
+
+ image_desc = 8; /* 8 bits per component. */
+
+ if (flip_image)
+ image_desc |= 32;
+ if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
return 1;
+
+fails:
+ fputs("\nwrite_tgaheader: write ERROR\n", stderr);
+ return 0;
}
opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
return NULL;
- // We currently only support 24 & 32 bit tga's ...
+ /* We currently only support 24 & 32 bit tga's ... */
if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
return NULL;
/* initialize image components */
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
- mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha.
- save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
+ mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */
+ save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
if (mono) {
color_space = CLRSPC_GRAY;
int imagetotga(opj_image_t * image, const char *outfile) {
int width, height, bpp, x, y;
opj_bool write_alpha;
- int i;
+ int i, adjustR, adjustG, adjustB;
unsigned int alpha_channel;
float r,g,b,a;
unsigned char value;
float scale;
FILE *fdest;
+ size_t res;
fdest = fopen(outfile, "wb");
if (!fdest) {
width = image->comps[0].w;
height = image->comps[0].h;
- // Mono with alpha, or RGB with alpha.
+ /* Mono with alpha, or RGB with alpha. */
write_alpha = (image->numcomps==2) || (image->numcomps==4);
- // Write TGA header
+ /* Write TGA header */
bpp = write_alpha ? 32 : 24;
if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
return 1;
scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
+ adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
+ adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
+ adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+
for (y=0; y < height; y++) {
unsigned int index=y*width;
for (x=0; x < width; x++, index++) {
- r = (float)(image->comps[0].data[index]);
+ r = (float)(image->comps[0].data[index] + adjustR);
if (image->numcomps>2) {
- g = (float)(image->comps[1].data[index]);
- b = (float)(image->comps[2].data[index]);
+ g = (float)(image->comps[1].data[index] + adjustG);
+ b = (float)(image->comps[2].data[index] + adjustB);
}
- else {// Greyscale ...
+ else {/* Greyscale ... */
g = r;
b = r;
}
- // TGA format writes BGR ...
+ /* TGA format writes BGR ... */
value = (unsigned char)(b*scale);
- fwrite(&value,1,1,fdest);
+ res = fwrite(&value,1,1,fdest);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
value = (unsigned char)(g*scale);
- fwrite(&value,1,1,fdest);
+ res = fwrite(&value,1,1,fdest);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
value = (unsigned char)(r*scale);
- fwrite(&value,1,1,fdest);
+ res = fwrite(&value,1,1,fdest);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
if (write_alpha) {
a = (float)(image->comps[alpha_channel].data[index]);
value = (unsigned char)(a*scale);
- fwrite(&value,1,1,fdest);
+ res = fwrite(&value,1,1,fdest);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
}
}
}
free(RGB);
}/* if (Info_h.biBitCount == 24) */
else
- if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB
+ if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
{
if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
else
free(table_B);
}/* RGB8 */
else
- if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
+ if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
{
unsigned char *pix, *beyond;
int *gray, *red, *green, *blue;
}
else
{
- //int *red, *green, *blue;
+ /*int *red, *green, *blue;*/
red = image->comps[0].data;
green = image->comps[1].data;
char bname[256]; /* buffer for name */
char *name = bname; /* pointer */
int nbytes = 0;
+ size_t res;
const size_t olen = strlen(outfile);
const size_t dotpos = olen - 4;
const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
name = (char*)malloc(total+1);
}
strncpy(name, outfile, dotpos);
- //if (image->numcomps > 1) {
+ /*if (image->numcomps > 1) {*/
sprintf(name+dotpos, "_%d.pgx", compno);
/*} else {
strcpy(name+dotpos, ".pgx");
int v = image->comps[compno].data[i];
for (j = nbytes - 1; j >= 0; j--) {
char byte = (char) (v >> (j * 8));
- fwrite(&byte, 1, 1, fdest);
+ res = fwrite(&byte, 1, 1, fdest);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", name);
+ return 1;
+ }
}
}
fclose(fdest);
}
}
else
- if((format == 7 && header_info.bw)) //MONO
+ if((format == 7 && header_info.bw)) /*MONO*/
{
unsigned char uc;
FILE *fdest = NULL;
const char *tmp = outfile;
char *destname;
-
+ alpha = NULL;
if((prec = image->comps[0].prec) > 16)
{
fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
{
fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
opj_version(), wr, hr, max);
- alpha = NULL; adjustA = 0;
+ adjustA = 0;
}
adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
<<-- <<-- <<-- <<-- */
typedef struct tiff_infoheader{
- DWORD tiWidth; // Width of Image in pixel
- DWORD tiHeight; // Height of Image in pixel
- DWORD tiPhoto; // Photometric
- WORD tiBps; // Bits per sample
- WORD tiSf; // Sample Format
- WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
- WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
+ DWORD tiWidth; /* Width of Image in pixel*/
+ DWORD tiHeight; /* Height of Image in pixel */
+ DWORD tiPhoto; /* Photometric */
+ WORD tiBps; /* Bits per sample */
+ WORD tiSf; /* Sample Format */
+ WORD tiSpp; /* Sample per pixel 1-bilevel,gray scale , 2- RGB */
+ WORD tiPC; /* Planar config (1-Interleaved, 2-Planarcomp) */
}tiff_infoheader_t;
int imagetotif(opj_image_t * image, const char *outfile)
}
else
break;
- }//for(i = 0;)
+ }/*for(i = 0;)*/
if(last_i < ssize)
{
}
else
break;
- }//for(i)
- }//if(last_i < ssize)
+ }/*for(i)*/
+ }/*if(last_i < ssize)*/
- } //if(bps == 8)
+ } /*if(bps == 8)*/
else
if(bps == 16)
{
b = (b<<ushift) + (b>>dshift);
if(has_alpha) a = (a<<ushift) + (a>>dshift);
}
- dat8[i+0] = r;//LSB
- dat8[i+1] = (r >> 8);//MSB
+ dat8[i+0] = r;/*LSB*/
+ dat8[i+1] = (r >> 8);/*MSB*/
dat8[i+2] = g;
dat8[i+3] = (g >> 8);
dat8[i+4] = b;
}
else
break;
- }//for(i = 0;)
+ }/*for(i = 0;)*/
if(last_i < ssize)
{
b = (b<<ushift) + (b>>dshift);
if(has_alpha) a = (a<<ushift) + (a>>dshift);
}
- dat8[i+0] = r;//LSB
- if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
+ dat8[i+0] = r;/*LSB*/
+ if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
if(i+2 < ssize) dat8[i+2] = g; else break;
if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
if(i+4 < ssize) dat8[i+4] = b; else break;
}
else
break;
- }//for(i)
- }//if(last_i < ssize)
+ }/*for(i)*/
+ }/*if(last_i < ssize)*/
- }//if(bps == 16)
+ }/*if(bps == 16)*/
(void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
- }//for(strip = 0; )
+ }/*for(strip = 0; )*/
_TIFFfree((void*)buf);
TIFFClose(tif);
return 0;
- }//RGB(A)
+ }/*RGB(A)*/
if(image->numcomps == 1 /* GRAY */
|| ( image->numcomps == 2 /* GRAY_ALPHA */
}
else
break;
- }//for(i )
- }//if(bps == 8
+ }/*for(i )*/
+ }/*if(bps == 8*/
else
if(bps == 16)
{
r = (r<<ushift) + (r>>dshift);
if(has_alpha) a = (a<<ushift) + (a>>dshift);
}
- dat8[i+0] = r;//LSB
- dat8[i+1] = r >> 8;//MSB
+ dat8[i+0] = r;/*LSB*/
+ dat8[i+1] = r >> 8;/*MSB*/
if(has_alpha)
{
dat8[i+2] = a;
dat8[i+3] = a >> 8;
}
index++;
- }//if(index < imgsize)
+ }/*if(index < imgsize)*/
else
break;
- }//for(i )
+ }/*for(i )*/
}
(void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
- }//for(strip
+ }/*for(strip*/
_TIFFfree(buf);
TIFFClose(tif);
{
if(index < imgsize)
{
- image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
- image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
- image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
+ image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
+ image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
+ image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
if(has_alpha)
image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
}
else
break;
- }//for(i = 0)
- }//if(Info.tiBps == 16)
+ }/*for(i = 0)*/
+ }/*if(Info.tiBps == 16)*/
else
if(Info.tiBps == 8)
{
{
if(index < imgsize)
{
- image->comps[0].data[index] = dat8[i+0];// R
- image->comps[1].data[index] = dat8[i+1];// G
- image->comps[2].data[index] = dat8[i+2];// B
+ image->comps[0].data[index] = dat8[i+0];/* R */
+ image->comps[1].data[index] = dat8[i+1];/* G */
+ image->comps[2].data[index] = dat8[i+2];/* B */
if(has_alpha)
image->comps[3].data[index] = dat8[i+3];
image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
}
index++;
- }//if(index
+ }/*if(index*/
else
break;
- }//for(i )
- }//if( Info.tiBps == 8)
+ }/*for(i )*/
+ }/*if( Info.tiBps == 8)*/
else
if(Info.tiBps == 12)/* CINEMA file */
{
}
else
break;
- }//for(i )
+ }/*for(i )*/
}
- }//for(strip = 0; )
+ }/*for(strip = 0; )*/
_TIFFfree(buf);
TIFFClose(tif);
return image;
- }//RGB(A)
+ }/*RGB(A)*/
if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
{
}
else
break;
- }//for(i )
+ }/*for(i )*/
}
else
if(Info.tiBps == 8)
}
else
break;
- }//for(i )
+ }/*for(i )*/
}
- }//for(strip = 0;
+ }/*for(strip = 0;*/
_TIFFfree(buf);
TIFFClose(tif);
- }//GRAY(A)
+ }/*GRAY(A)*/
return image;
int imagetoraw(opj_image_t * image, const char *outfile)
{
FILE *rawFile = NULL;
+ size_t res;
int compno;
int w, h;
int line, row;
for (line = 0; line < h; line++) {
for(row = 0; row < w; row++) {
curr = (signed char) (*ptr & mask);
- fwrite(&curr, sizeof(signed char), 1, rawFile);
+ res = fwrite(&curr, sizeof(signed char), 1, rawFile);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
ptr++;
}
}
for (line = 0; line < h; line++) {
for(row = 0; row < w; row++) {
curr = (unsigned char) (*ptr & mask);
- fwrite(&curr, sizeof(unsigned char), 1, rawFile);
+ res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
ptr++;
}
}
unsigned char temp;
curr = (signed short int) (*ptr & mask);
temp = (unsigned char) (curr >> 8);
- fwrite(&temp, 1, 1, rawFile);
+ res = fwrite(&temp, 1, 1, rawFile);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
temp = (unsigned char) curr;
- fwrite(&temp, 1, 1, rawFile);
+ res = fwrite(&temp, 1, 1, rawFile);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
ptr++;
}
}
unsigned char temp;
curr = (unsigned short int) (*ptr & mask);
temp = (unsigned char) (curr >> 8);
- fwrite(&temp, 1, 1, rawFile);
+ res = fwrite(&temp, 1, 1, rawFile);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
temp = (unsigned char) curr;
- fwrite(&temp, 1, 1, rawFile);
+ res = fwrite(&temp, 1, 1, rawFile);
+ if( res < 1 ) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ return 1;
+ }
ptr++;
}
}