home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / fract5.zip / LOADMAP.C < prev    next >
C/C++ Source or Header  |  1989-09-25  |  3KB  |  125 lines

  1. /** loadmap.c **/
  2.  
  3.  
  4. #include    <stdio.h>
  5. #include    <stdlib.h>
  6. #include    <dos.h>
  7.  
  8. typedef unsigned char uchar;
  9.  
  10. typedef struct palett {
  11.    uchar red;
  12.    uchar green;
  13.    uchar blue;
  14. } Palettetype;
  15.  
  16. extern char                    loadPalette;
  17. extern int                    targaType;
  18. extern Palettetype    dacbox[ 256 ];
  19. extern unsigned            tga16[ 256 ];
  20. extern long                    tga32[ 256 ];
  21.  
  22.  
  23. static char * palName = "default.map";
  24. static int        maploaded;
  25. static Palettetype    low16[16];
  26.  
  27. /***************************************************************************/
  28.  
  29. int
  30. CustomLut( void )
  31. {
  32.     return( loadPalette );
  33. }
  34.  
  35.  
  36. /***************************************************************************/
  37.  
  38. void
  39. RestoreMap()
  40. {
  41. int index;
  42.     for( index = 0; index < 16; index++ )
  43.         dacbox[index] = low16[index];
  44.     storedac();
  45. }
  46.  
  47.  
  48. /***************************************************************************/
  49.  
  50. void
  51. ValidateLuts( FILE * f )
  52. {
  53. unsigned    r, g, b, index;
  54. unsigned char   line[101];
  55.     for( index = 0; index < 256; index++ ) {
  56.         if( f != NULL && fgets(line,100,f) != NULL) {
  57.             sscanf( line, "%d %d %d", &r, &g, &b );
  58.             /** load global dac values **/
  59.             dacbox[index].red   = r >> 2;    /* maps default to 8 bits */
  60.             dacbox[index].green = g >> 2;    /* DAC wants 6 bits */
  61.             dacbox[index].blue    = b >> 2;
  62.         }
  63.         else {
  64.             f = NULL;
  65.             r = dacbox[index].red   << 2;
  66.             g = dacbox[index].green << 2;
  67.             b = dacbox[index].blue  << 2;
  68.         }
  69.  
  70.         /** load local tables **/
  71.         tga16[index] = ((r&248)<<7) | ((g&248)<<2) | (b>>3);
  72.         tga32[index] = ((long)r<<16) | (g<<8) | b;
  73. /***
  74. printf( "%08lx\n", tga32[index] );
  75. ***/
  76.     }
  77. }
  78.  
  79.  
  80. /***************************************************************************/
  81.  
  82. int
  83. LoadColorMap( void )
  84. {
  85. char temp[81];
  86. FILE *f;
  87. unsigned    r, g, b, index;
  88.  
  89.     if( maploaded ) return( 1 );
  90.     findpath( palName, temp);        /* search the dos path */
  91.     f = fopen( temp, "r" );
  92.     if( f != NULL ) {
  93.         /** save first 16 color map entries **/
  94.         loaddac();
  95.         for( index = 0; index < 16; index++ )
  96.             low16[index] = dacbox[index];
  97.         atexit( RestoreMap );
  98.         ValidateLuts( f );
  99.         fclose( f );
  100.         maploaded = 1;
  101.         return( 1 );
  102.     }
  103.     return( 0 );
  104. }
  105.  
  106.  
  107. /***************************************************************************/
  108.  
  109. void
  110. SetColorPaletteName( char * fn )
  111. {
  112.     palName = fn;
  113.     loadPalette = 0;
  114.     if (LoadColorMap() == 0) {
  115.         strcpy(palName,"default.map");
  116.         buzzer(2);
  117.         }
  118.     else
  119.         loadPalette = 1;
  120. }
  121.  
  122.  
  123. /***************************************************************************/
  124.  
  125.