home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ Games Programming
/
CPPGAMES.ISO
/
thx
/
source
/
utils
/
genpal.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-12
|
3KB
|
135 lines
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <io.h>
#include "utils.h"
const int MAXPAL = 2048;
const int STRLEN = 80;
const int TRUE = 1;
const int FALSE = 0;
const int DUP = TRUE;
const int NOTDUP = FALSE;
const char OUTPUTFILE[] = "new.pal";
struct pal_entry // struct compadible with the
{ // fg_setdacs routine
unsigned char r;
unsigned char g;
unsigned char b;
};
static pal_entry map[MAXPAL];
static int count;
static int tolerance;
int load_pal(char* fname);
int find_slot_for(unsigned int r,unsigned int g,unsigned int b);
void write_new_palette(const char* fname);
void bld_palatte(char *file);
int main(int argc,char** argv)
{
if (argc<3)
{
cout << endl;
cout << "USAGE : GENPAL <tolerance> <palette1.pal> [palette2.pal] [...]\n";
cout << " Genpal merges as many palette files as you provide\n";
cout << " on the command line\n";
cout << " and produces a master palette file called 'new.pal'\n";
return -1;
}
tolerance=atoi(argv[1]);
int rtn = parse_cmdline(argc-2, argv+2, bld_palatte);
if (rtn)
{
cout << " total: " << count << '\n';
write_new_palette(OUTPUTFILE);
}
return 0;
}
void bld_palatte(char *file)
{
cout << file << ' ';
int dups = load_pal(file);
cout << count << " (" << dups << " duplicates)\n";
}
int load_pal(char* fname)
{
int c=0,dupcount=0;
int r,g,b;
static char str[STRLEN+1];
int numentries;
ifstream pal(fname);
if (pal.bad()) return 0;
pal.getline(str,STRLEN);
pal.getline(str,STRLEN);
pal.getline(str,STRLEN);
numentries=atoi(str);
for (int i=0;i<numentries;i++)
{
pal >> r;
pal >> g;
pal >> b;
if (!(r==0 && g==0 && b==0))
{
if (find_slot_for(r,g,b)==DUP)
dupcount++;
c++;
}
}
cout << "(" << c << " colors) -> ";
return dupcount;
}
int find_slot_for(unsigned int r,unsigned int g,unsigned int b)
{
int score;
if (count==0)
{
map[0].r=r;
map[0].g=g;
map[0].b=b;
count=1;
return NOTDUP;
}
if (count < MAXPAL)
{
for (int i=0;i<count;i++)
{
score=0;
score+=abs(map[i].r-r);
score+=abs(map[i].g-g);
score+=abs(map[i].b-b);
if (score <= tolerance)
return DUP;
}
map[count].r=r;
map[count].g=g;
map[count].b=b;
count++;
}
return NOTDUP;
}
void write_new_palette(const char* fname)
{
ofstream newpal(fname);
newpal << "NeoPaint Palette File\n";
newpal << "(C)1992-93 NeoSoft Corp.\n";
newpal << "256\n";
newpal << "0 0 0\n";
newpal << "0 0 0\n";
newpal << "63 63 63\n";
for (int i=0;i<256-3;i++)
newpal << (int)map[i].r << ' '
<< (int)map[i].g << ' '
<< (int)map[i].b << '\n';
}