|
|
Here you go (it's Windows source). You can easily modify to make it compile in
Linux.
/*
convert a series of 24bit uncompressed TGA files
into a POV-Ray 3.1 df3 density file
*/
#include<stdio.h>
#include<stdlib.h>
#include<dir.h>
#include<string.h>
char *fileNames;
char baseName[256];
int nFiles, longestName;
int convert( void );
int main()
{
char searchName[256];
char *fName;
struct ffblk ff;
int Err;
int nameLen;
printf("Enter base name of files to convert ");
gets( baseName );
printf("\n");
strcpy(searchName,baseName);
strcat( searchName,"*.tga");
Err = findfirst(searchName,&ff,0);
if( Err == -1 )
{
printf("Can't find any files matching \'%s\'\n", searchName );
return 1;
}
nFiles = 1;
longestName = strlen(ff.ff_name);
while( findnext(&ff) == 0 )
{
nFiles++;
nameLen = strlen(ff.ff_name);
longestName = max(longestName,nameLen);
}
longestName++;
fileNames = calloc(nFiles,longestName);
if( !fileNames )
{
puts("Couldn't allocate name buffer");
return 2;
}
findfirst(searchName,&ff,0);
fName = fileNames;
strcpy( fName, ff.ff_name);
fName += longestName;
while( findnext(&ff) == 0 )
{
strcpy( fName, ff.ff_name);
fName += longestName;
}
qsort( fileNames, nFiles, longestName, strcmp );
convert();
free( fileNames );
return 0;
}
/* assumes 24bit uncompressed TGA */
int convert( void )
{
char dfName[256];
char *fName;
FILE *tgaFile, *dfFile;
static int width,height;
unsigned char d;
int f,p;
double i;
strcpy( dfName, baseName);
strcat( dfName,".df3");
/* open first tga to get dimensions */
tgaFile = fopen( fileNames,"rb");
fseek( tgaFile, 12,SEEK_SET );
/* read width,height of first TGA
low byte first */
fread( &d,1,1,tgaFile );
width = d;
fread( &d,1,1,tgaFile );
width += ((int)d) << 8;
fread( &d,1,1,tgaFile );
height = d;
fread( &d,1,1,tgaFile );
height += ((int)d) << 8;
fclose( tgaFile );
dfFile = fopen( dfName,"wb");
/* write width,height,depth of df3 file
high byte first */
d = (width >> 8);
fwrite( &d,1,1,dfFile );
d = (width & 0xff);
fwrite( &d,1,1,dfFile );
d = (height >> 8);
fwrite( &d,1,1,dfFile );
d = (height & 0xff);
fwrite( &d,1,1,dfFile );
d = (nFiles >> 8);
fwrite( &d,1,1,dfFile );
d = (nFiles & 0xff);
fwrite( &d,1,1,dfFile );
fName = fileNames;
for( f = 0; f < nFiles; f++ )
{
puts(fName);
tgaFile = fopen( fName,"rb");
fseek( tgaFile,18,SEEK_SET);
for( p = 0; p < width*height; p++ )
{
/* read B,G,R convert to grey level */
fread( &d,1,1,tgaFile );
i = (double)d*0.11;
fread( &d,1,1,tgaFile );
i += (double)d*0.59;
fread( &d,1,1,tgaFile );
i += (double)d*0.30;
d = (int)i;
fwrite( &d,1,1,dfFile);
}
fclose(tgaFile);
fName += longestName;
}
fclose(dfFile);
return 0;
}
Post a reply to this message
|
|