home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / source / utils / genpal.cpp < prev    next >
C/C++ Source or Header  |  1995-05-12  |  3KB  |  135 lines

  1. #include <fstream.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <io.h>
  5. #include "utils.h"
  6.  
  7. const int MAXPAL = 2048;
  8. const int STRLEN = 80;
  9.  
  10. const int TRUE  = 1;
  11. const int FALSE = 0;
  12. const int DUP = TRUE;
  13. const int NOTDUP = FALSE;
  14.  
  15. const char OUTPUTFILE[] = "new.pal";
  16.  
  17. struct pal_entry       // struct compadible with the
  18.   {                    // fg_setdacs routine
  19.   unsigned char r;
  20.   unsigned char g;
  21.   unsigned char b;
  22.   };
  23.  
  24. static pal_entry map[MAXPAL];
  25. static int count;
  26. static int tolerance;
  27.  
  28. int load_pal(char* fname);
  29. int find_slot_for(unsigned int r,unsigned int g,unsigned int b);
  30. void write_new_palette(const char* fname);
  31. void bld_palatte(char *file);
  32.  
  33. int main(int argc,char** argv)
  34.   {
  35.   if (argc<3)
  36.     {
  37.     cout << endl;
  38.     cout << "USAGE : GENPAL <tolerance> <palette1.pal> [palette2.pal] [...]\n";
  39.     cout << "  Genpal merges as many palette files as you provide\n";
  40.     cout << "  on the command line\n";
  41.     cout << "  and produces a master palette file called 'new.pal'\n";
  42.     return -1;
  43.     }
  44.   tolerance=atoi(argv[1]);
  45.   int rtn = parse_cmdline(argc-2, argv+2, bld_palatte);
  46.  
  47.   if (rtn)
  48.     {
  49.     cout << "                   total: " << count << '\n';
  50.     write_new_palette(OUTPUTFILE);
  51.     }
  52.   return 0;
  53.   }
  54.  
  55. void bld_palatte(char *file)
  56.   {
  57.     cout << file << ' ';
  58.     int dups = load_pal(file);
  59.     cout << count << "  (" << dups << " duplicates)\n";
  60.   }
  61.  
  62. int load_pal(char* fname)
  63.   {
  64.   int c=0,dupcount=0;
  65.   int r,g,b;
  66.   static char str[STRLEN+1];
  67.   int numentries;
  68.  
  69.   ifstream pal(fname);
  70.   if (pal.bad())  return 0;
  71.   pal.getline(str,STRLEN);
  72.   pal.getline(str,STRLEN);
  73.   pal.getline(str,STRLEN);
  74.   numentries=atoi(str);
  75.   for (int i=0;i<numentries;i++)
  76.     {
  77.     pal >> r;
  78.     pal >> g;
  79.     pal >> b;
  80.     if (!(r==0 && g==0 && b==0))
  81.       {
  82.       if (find_slot_for(r,g,b)==DUP)
  83.         dupcount++;
  84.       c++;
  85.       }
  86.     }
  87.   cout << "(" << c << " colors)  ->  ";
  88.   return dupcount;
  89.   }
  90.  
  91. int find_slot_for(unsigned int r,unsigned int g,unsigned int b)
  92.   {
  93.   int score;
  94.   if (count==0)
  95.     {
  96.     map[0].r=r;
  97.     map[0].g=g;
  98.     map[0].b=b;
  99.     count=1;
  100.     return NOTDUP;
  101.     }
  102.   if (count < MAXPAL)
  103.     {
  104.     for (int i=0;i<count;i++)
  105.       {
  106.       score=0;
  107.       score+=abs(map[i].r-r);
  108.       score+=abs(map[i].g-g);
  109.       score+=abs(map[i].b-b);
  110.       if (score <= tolerance)
  111.         return DUP;
  112.       }
  113.     map[count].r=r;
  114.     map[count].g=g;
  115.     map[count].b=b;
  116.     count++;
  117.     }
  118.   return NOTDUP;
  119.   }
  120.  
  121. void write_new_palette(const char* fname)
  122.   {
  123.   ofstream newpal(fname);
  124.   newpal << "NeoPaint Palette File\n";
  125.   newpal << "(C)1992-93 NeoSoft Corp.\n";
  126.   newpal << "256\n";
  127.   newpal << "0 0 0\n";
  128.   newpal << "0 0 0\n";
  129.   newpal << "63 63 63\n";
  130.   for (int i=0;i<256-3;i++)
  131.     newpal << (int)map[i].r << ' '
  132.            << (int)map[i].g << ' ' 
  133.            << (int)map[i].b << '\n';
  134.   }
  135.