#include "stdio.h" #include "stdlib.h" #include "math.h" /* image converter based on http://paulbourke.net/dataformats/tga/ tga converter to various formats: here for thomson TO8/TO9 formats with specific gamma factor */ typedef struct { char idlength; char colourmaptype; char datatypecode; short int colourmaporigin; short int colourmaplength; char colourmapdepth; short int x_origin; short int y_origin; short width; short height; char bitsperpixel; char imagedescriptor; } HEADER; typedef struct { unsigned char r,g,b,a; } PIXEL; void MergeBytes(PIXEL *,unsigned char *,int); int main(int argc,char **argv) { int n=0,i,j; int bytes2read,skipover = 0; unsigned char p[5]; FILE *fptr; HEADER header; PIXEL *pixels; if (argc < 2) { fprintf(stderr,"Usage: %s tgafile\n",argv[0]); exit(-1); } /* Open the file */ if ((fptr = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"File open failed\n"); exit(-1); } /* Display the header fields */ header.idlength = fgetc(fptr); fprintf(stderr,"ID length: %d\n",header.idlength); header.colourmaptype = fgetc(fptr); fprintf(stderr,"Colourmap type: %d\n",header.colourmaptype); header.datatypecode = fgetc(fptr); fprintf(stderr,"Image type: %d\n",header.datatypecode); fread(&header.colourmaporigin,2,1,fptr); fprintf(stderr,"Colour map offset: %d\n",header.colourmaporigin); fread(&header.colourmaplength,2,1,fptr); fprintf(stderr,"Colour map length: %d\n",header.colourmaplength); header.colourmapdepth = fgetc(fptr); fprintf(stderr,"Colour map depth: %d\n",header.colourmapdepth); fread(&header.x_origin,2,1,fptr); fprintf(stderr,"X origin: %d\n",header.x_origin); fread(&header.y_origin,2,1,fptr); fprintf(stderr,"Y origin: %d\n",header.y_origin); fread(&header.width,2,1,fptr); fprintf(stderr,"Width: %d\n",header.width); fread(&header.height,2,1,fptr); fprintf(stderr,"Height: %d\n",header.height); header.bitsperpixel = fgetc(fptr); fprintf(stderr,"Bits per pixel: %d\n",header.bitsperpixel); header.imagedescriptor = fgetc(fptr); fprintf(stderr,"Descriptor: %d\n",header.imagedescriptor); /* Allocate space for the image */ if ((pixels = malloc(header.width*header.height*sizeof(PIXEL))) == NULL) { fprintf(stderr,"malloc of image failed\n"); exit(-1); } for (i=0;i16) { fprintf(stderr,"[ERROR] too many colors %d\n",col_num); exit(-1); } for (i=0;i<16;i++) palette_to[i]=0; for (i=0;i>16)<<8; palette_to[i]|=pal_conv((palette_24b[i]&0xFF00)>>8)<<4; palette_to[i]|=pal_conv((palette_24b[i]&0xFF)); fprintf(stderr,"[INFO] palette[%d]=%03x\n",i,palette_to[i]); } /* ouput in 2 files raw data 8kb each (palette at end) */ FILE *COL; FILE *PT; COL=fopen("COL.BIN","wb"); PT=fopen("PT.BIN","wb"); // on modifie les pixels 2 par 2 for (i=0;i>8,PT); fputc((palette_to[i]&0xFF),PT); fputc((palette_to[i]&0xFF00)>>8,COL); fputc((palette_to[i]&0xFF),COL); } fclose(PT); fclose(COL); } int pal_conv(int v) { int i; /* convert palette to thomson gamma factor */ // static int gamma[16]={0,100,127,147,163,179,191,203,215,223,231,239,243,247,251,255}; static int seuils[17]={0,50,114,137,155,171,185,197,209,219,227,235,241,245,249,253,257}; for (i=0;i<16;i++) { if (vr = p[2]; pixel->g = p[1]; pixel->b = p[0]; pixel->a = p[3]; } else if (bytes == 3) { pixel->r = p[2]; pixel->g = p[1]; pixel->b = p[0]; pixel->a = 0; } else if (bytes == 2) { pixel->r = (p[1] & 0x7c) << 1; pixel->g = ((p[1] & 0x03) << 6) | ((p[0] & 0xe0) >> 2); pixel->b = (p[0] & 0x1f) << 3; pixel->a = (p[1] & 0x80); } }