home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / 3DENGINE.ZIP / Makelite.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  3.3 KB  |  150 lines

  1. //
  2. // MAKELITE.CPP
  3. //
  4. // Utility for generating light-sourcing tables.
  5. // Written by Christopher Lampton
  6. // for Gardens of Imagination (1994)
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <values.h>
  13. #include <conio.h>
  14. #include <io.h>
  15. #include <fcntl.h>
  16. #include "pcx.h"
  17.  
  18. const MAXLIGHT=32;
  19. const PALETTESIZE=256;
  20.  
  21. pcx_struct litepal;
  22. int red[PALETTESIZE],green[PALETTESIZE],
  23.     blue[PALETTESIZE];
  24. unsigned char litesource[MAXLIGHT+1][PALETTESIZE];
  25. float redscore,greenscore,bluescore;
  26. FILE *handle;
  27.  
  28. void main(int argc,char* argv[])
  29. {
  30.  
  31.   // Set default target to black:
  32.  
  33.   int redtarget=0;
  34.   int greentarget=0;
  35.   int bluetarget=0;
  36.  
  37.   // Check for filenames on the command line:
  38.  
  39.   if (argc<2) {
  40.     printf("You must type a name for the source file.\n");
  41.     exit(1);
  42.   }
  43.  
  44.   if (argc<3) {
  45.     printf("You must type a name for the target file.\n");
  46.     exit(1);
  47.   }
  48.  
  49.   // Check for target values on command line:
  50.  
  51.   if (argc>=4) redtarget=atof(argv[3]);
  52.   if (argc>=5) bluetarget=atof(argv[4]);
  53.   if (argc>=6) greentarget=atof(argv[5]);
  54.  
  55.   // Let user know what's going on:
  56.  
  57.   printf("Loading palette.\n");
  58.  
  59.   // Load the PCX file:
  60.  
  61.   if (loadPCX(argv[1],&litepal)) exit(1);
  62.  
  63.   // Copy palette to RGB arrays:
  64.  
  65.   for (int color=0; color<PALETTESIZE; color++) {
  66.     red[color]=litepal.palette[color*3];
  67.     green[color]=litepal.palette[color*3+1];
  68.     blue[color]=litepal.palette[color*3+2];
  69.   }
  70.  
  71.   // Let user know what we're about to do:
  72.  
  73.   printf("Calculating lightsourcing tables");
  74.  
  75.   // Iterate through all 256 palette colors:
  76.  
  77.   for (color=0; color<PALETTESIZE; color++) {
  78.  
  79.     // Let user know we haven't died:
  80.  
  81.     printf(".");
  82.  
  83.     // Iterate through all 32+1 intensity levels:
  84.  
  85.     for (int level=0; level<=MAXLIGHT; level++) {
  86.  
  87.       // Calculate ideal lightsourced RGB values:
  88.  
  89.       float redlite=(float)(red[color]-redtarget) / MAXLIGHT
  90.               * level + redtarget;
  91.       float greenlite=(float)(green[color]-greentarget) / MAXLIGHT
  92.               * level + greentarget;
  93.       float bluelite=(float)(blue[color]-bluetarget) / MAXLIGHT
  94.               * level + bluetarget;
  95.  
  96.       // Initialize score to very large number:
  97.  
  98.       float bestscore=MAXFLOAT;
  99.  
  100.       // Search the palette for closest match:
  101.  
  102.       for (int color2=0; color2<PALETTESIZE; color2++) {
  103.  
  104.         // Assign proximity score to color:
  105.  
  106.         redscore=fabs(red[color2]-redlite);
  107.         greenscore=fabs(green[color2]-greenlite);
  108.         bluescore=fabs(blue[color2]-bluelite);
  109.         float score=redscore+greenscore+bluescore;
  110.  
  111.         // Is this better (i.e. smaller) than best score?
  112.  
  113.         if (score<bestscore) {
  114.  
  115.           // If so, replace best score:
  116.  
  117.           bestscore=score;
  118.  
  119.           // And remember which color got it:
  120.  
  121.           litesource[level][color]=color2;
  122.         }
  123.       }
  124.     }
  125.   }
  126.  
  127.   // Tell user that the worst is over:
  128.  
  129.   printf("\nWriting lightsourcing tables to disk.\n");
  130.  
  131.   // Try to open disk file:
  132.  
  133.   if ((handle=fopen(argv[2],"wb"))==NULL) {
  134.     perror("Error");
  135.     exit;
  136.   }
  137.  
  138.   // If successful, write lightsource tables to file:
  139.  
  140.   fwrite(litesource,MAXLIGHT+1,PALETTESIZE,handle);
  141.  
  142.   // Close up shop:
  143.  
  144.   fclose(handle);
  145.  
  146.   // And tell the user to breathe easy:
  147.  
  148.   printf("Done!\n");
  149. }
  150.