#include "stdio.h" #include "stdlib.h" #include "math.h" /* raw font converter from TGA image type 2 and 10 */ 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); } { int col,row; int nbcol,nbrow; int subline,subcol; int segment; nbcol=header.width/8; nbrow=header.height/8; for (row=0;row127) { segment|=1; } } fprintf(stdout,"$%X",segment); if (subline!=7) { fprintf(stdout,","); } else { fprintf(stdout,"\n"); } } } } } } void output_tga(HEADER header,PIXEL *pixels) { FILE *fptr; int i; /* Write the result as a uncompressed TGA */ if ((fptr = fopen("tgatest.tga","w")) == NULL) { fprintf(stderr,"Failed to open outputfile\n"); exit(-1); } putc(0,fptr); putc(0,fptr); putc(2,fptr); /* uncompressed RGB */ putc(0,fptr); putc(0,fptr); putc(0,fptr); putc(0,fptr); putc(0,fptr); putc(0,fptr); putc(0,fptr); /* X origin */ putc(0,fptr); putc(0,fptr); /* y origin */ putc((header.width & 0x00FF),fptr); putc((header.width & 0xFF00) / 256,fptr); putc((header.height & 0x00FF),fptr); putc((header.height & 0xFF00) / 256,fptr); putc(32,fptr); /* 24 bit bitmap */ putc(0,fptr); for (i=0;ir = 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); } }