This commit is contained in:
parent
b34c800fe4
commit
b8a82dd6fe
2 changed files with 233 additions and 0 deletions
137
Utils/Tga2Gfx/Tga2Gfx.cpp
Normal file
137
Utils/Tga2Gfx/Tga2Gfx.cpp
Normal file
|
@ -0,0 +1,137 @@
|
|||
/***************************/
|
||||
/*** TGA 2 Gfx Convertor ***/
|
||||
/***************************/
|
||||
|
||||
//#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
//#include <string.h>
|
||||
#include <stdio.h>
|
||||
//#include <time.h>
|
||||
//#include <math.h>
|
||||
#include <io.h>
|
||||
#include <fstream.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
|
||||
//***************************************************************************
|
||||
struct sTgaHdr
|
||||
{
|
||||
char id; // 0
|
||||
char colmaptype; // 1
|
||||
char imagetype; // 2
|
||||
char fei[2]; // 3
|
||||
char cml[2]; // 5
|
||||
char cmes; // 7
|
||||
short xorig; // 8
|
||||
short yorig; // 10
|
||||
short width; // 12
|
||||
short height; // 14
|
||||
char depth; // 15
|
||||
char imagedesc; // 16
|
||||
};
|
||||
|
||||
|
||||
//***************************************************************************
|
||||
sTgaHdr *TGA;
|
||||
short *Buffer;
|
||||
char *InFile;
|
||||
char *OutFile;
|
||||
|
||||
//***************************************************************************
|
||||
void Load()
|
||||
{
|
||||
FILE *File;
|
||||
int Size;
|
||||
|
||||
File=fopen(InFile,"rb");
|
||||
fseek(File,0,SEEK_END);
|
||||
Size=ftell(File);
|
||||
fseek(File,0,SEEK_SET);
|
||||
|
||||
if( (TGA=(sTgaHdr*)malloc(Size))==NULL ) {printf("Out of memory.\n");exit(123);}
|
||||
|
||||
fread(TGA, 1,Size,File);
|
||||
fclose(File);
|
||||
|
||||
if( (Buffer=(short*)malloc(TGA->width*TGA->height*sizeof(short)))==NULL ) {printf("Out of memory.\n");exit(123);}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
void Process()
|
||||
{
|
||||
unsigned char *InSrc=(unsigned char*)TGA+sizeof(sTgaHdr);
|
||||
short *Dst=Buffer;
|
||||
/*
|
||||
printf("width %i\n",TGA->width);
|
||||
printf("height %i\n",TGA->height);
|
||||
printf("depth %i\n",TGA->depth);
|
||||
printf("imagedesc %i\n",TGA->imagedesc);
|
||||
*/
|
||||
for (int Y=0; Y<TGA->height; Y++)
|
||||
{
|
||||
unsigned char *Src=&InSrc[(TGA->height-Y)*(TGA->width*3)]; // Flip Image
|
||||
for (int X=0; X<TGA->width; X++)
|
||||
{
|
||||
int B=*Src++;
|
||||
int G=*Src++;
|
||||
int R=*Src++;
|
||||
|
||||
if (TGA->depth==24)
|
||||
{
|
||||
R>>=(8-5);
|
||||
G>>=(8-5);
|
||||
B>>=(8-5);
|
||||
}
|
||||
*Dst=0;
|
||||
*Dst|=R<<0;
|
||||
*Dst|=G<<5;
|
||||
*Dst|=B<<10;
|
||||
Dst++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
void Write()
|
||||
{
|
||||
FILE *File;
|
||||
|
||||
File=fopen(OutFile,"wb");
|
||||
|
||||
fwrite(Buffer,TGA->width*TGA->height*sizeof(short),1,File);
|
||||
fclose(File);
|
||||
|
||||
free(TGA);
|
||||
free(Buffer);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
//***************************************************************************
|
||||
//***************************************************************************
|
||||
|
||||
void Usage(char *ErrStr)
|
||||
{
|
||||
printf("\nTga2Gfx: by Dave\n");
|
||||
printf("Usage: Tga2Gfx <file> [ <file>.. ] [ switches.. ]\n");
|
||||
printf("Switches:\n");
|
||||
printf(" -o:[FILE] Set output File\n");
|
||||
printf("\nERROR: %s\n",ErrStr);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc!=3) Usage("Incorrect Params");
|
||||
|
||||
InFile=argv[1];
|
||||
OutFile=argv[2];
|
||||
|
||||
|
||||
Load();
|
||||
Process();
|
||||
Write();
|
||||
return(0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue