home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / LOADMAP.C < prev    next >
Text File  |  1991-07-07  |  2KB  |  93 lines

  1. /** loadmap.c **/
  2.  
  3.  
  4. #include    <stdio.h>
  5. #include    <stdlib.h>
  6. #include    <dos.h>
  7. #include    <string.h>
  8. #include    "fractint.h"
  9.  
  10. typedef unsigned char uchar;
  11.  
  12. typedef struct palett {
  13.    uchar red;
  14.    uchar green;
  15.    uchar blue;
  16. } Palettetype;
  17.  
  18. extern Palettetype    dacbox[ 256 ];
  19. extern unsigned far    *tga16;
  20. extern long    far    *tga32;
  21. extern char    far    *mapdacbox;
  22. extern int        colorstate; /* comments in cmdfiles */
  23. extern char        colorfile[];
  24.  
  25. /***************************************************************************/
  26.  
  27. void SetTgaColors() {
  28. unsigned    r, g, b, index;
  29.     if (tga16 != NULL)
  30.     for( index = 0; index < 256; index++ ) {
  31.         r = dacbox[index].red    << 2;
  32.         g = dacbox[index].green << 2;
  33.         b = dacbox[index].blue    << 2;
  34.         tga16[index] = ((r&248)<<7) | ((g&248)<<2) | (b>>3);
  35.         tga32[index] = ((long)r<<16) | (g<<8) | b;
  36.     }
  37. }
  38.  
  39. int ValidateLuts( char * fn )
  40. {
  41. FILE * f;
  42. unsigned    r, g, b, index;
  43. unsigned char    line[160];
  44. unsigned char    temp[81];
  45.     strcpy (temp,fn);
  46.     if (strchr(temp,'.') == NULL) /* Did name have an extension? */
  47.         strcat(temp,".map");  /* No? Then add .map */
  48.     findpath( temp, line);          /* search the dos path */
  49.     f = fopen( line, "r" );
  50.     if (f == NULL) {
  51.         sprintf(line,"Could not load color map %s",fn);
  52.         stopmsg(0,line);
  53.         return 1;
  54.         }
  55.     for( index = 0; index < 256; index++ ) {
  56.         if (fgets(line,100,f) == NULL)
  57.             break;
  58.         sscanf( line, "%d %d %d", &r, &g, &b );
  59.         /** load global dac values **/
  60.         dacbox[index].red   = r >> 2;    /* maps default to 8 bits */
  61.         dacbox[index].green = g >> 2;    /* DAC wants 6 bits */
  62.         dacbox[index].blue  = b >> 2;
  63.     }
  64.     fclose( f );
  65.     while (index < 256)  { /* zap unset entries */
  66.         dacbox[index].red = dacbox[index].blue = dacbox[index].green = 40;
  67.         ++index;
  68.     }
  69.     SetTgaColors();
  70.     colorstate = 2;
  71.     strcpy(colorfile,fn);
  72.     return 0;
  73. }
  74.  
  75.  
  76. /***************************************************************************/
  77.  
  78. int SetColorPaletteName( char * fn )
  79. {
  80.     if( ValidateLuts( fn ) != 0)
  81.         return 1;
  82.     if( mapdacbox == NULL && (mapdacbox = farmemalloc(768L)) == NULL) {
  83.         static char far msg[]={"Insufficient memory for color map."};
  84.         stopmsg(0,msg);
  85.         return 1;
  86.         }
  87.     far_memcpy((char far *)mapdacbox,(char far *)dacbox,768);
  88.     /* PB, 900829, removed atexit(RestoreMap) stuff, goodbye covers it */
  89.     return 0;
  90. }
  91.  
  92.  
  93.