]> icculus.org git repositories - taylor/freespace2.git/blob - src/palman/palman.cpp
Initial revision
[taylor/freespace2.git] / src / palman / palman.cpp
1 /*
2  * $Logfile: /Freespace2/code/Palman/PalMan.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Palette manager routines
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 5     3/31/99 8:24p Dave
15  * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
16  * and background nebulae. Added per-ship non-dimming pixel colors.
17  * 
18  * 4     2/05/99 12:52p Dave
19  * Fixed Glide nondarkening textures.
20  * 
21  * 3     2/03/99 6:06p Dave
22  * Groundwork for FS2 PXO usertracker support.  Gametracker support next.
23  * 
24  * 2     10/07/98 10:53a Dave
25  * Initial checkin.
26  * 
27  * 1     10/07/98 10:50a Dave
28  * 
29  * 55    5/20/98 9:46p John
30  * added code so the places in code that change half the palette don't
31  * have to clear the screen.
32  * 
33  * 54    5/16/98 4:37p John
34  * 
35  * 53    5/16/98 4:34p John
36  * fixed bug where only colors divisible by 4 would work.
37  * 
38  * 52    5/15/98 3:28p John
39  * fixed bug with blue nondarkening colors
40  * 
41  * 51    5/14/98 3:38p John
42  * Added in more non-darkening colors for Adam.  Had to fix some bugs in
43  * BmpMan and Ani stuff to get this to work.
44  * 
45  * 50    5/13/98 10:22p John
46  * Added cfile functions to read/write rle compressed blocks of data.
47  * Made palman use it for .clr files.  Made alphacolors calculate on the
48  * fly rather than caching to/from disk.
49  * 
50  * 49    4/30/98 4:53p John
51  * Restructured and cleaned up cfile code.  Added capability to read off
52  * of CD-ROM drive and out of multiple pack files.
53  * 
54  * 48    4/20/98 4:44p John
55  * Fixed problems with black being xparent on model cache rneders.  Made
56  * model cache key off of detail level setting and framerate.
57  * 
58  * 47    4/15/98 5:44p John
59  * Made no colors except green map to 0,255,0
60  * 
61  * 46    4/09/98 11:04p John
62  * Changed ID's in output files to make more sense.
63  * 
64  * 45    4/01/98 5:35p John
65  * Made only the used POFs page in for a level.   Reduced some interp
66  * arrays.    Made custom detail level work differently.
67  * 
68  * 44    3/25/98 8:08p John
69  * Restructured software rendering into two modules; One for windowed
70  * debug mode and one for DirectX fullscreen.   
71  * 
72  * 43    3/24/98 3:58p John
73  * Put in (hopefully) final gamma setting code.
74  * 
75  * 42    2/24/98 1:59p John
76  * Made fade table use a gamma
77  * 
78  * 41    1/29/98 8:18a John
79  * Put in some commented out hooks for RGB lighting
80  * 
81  * 40    1/19/98 6:15p John
82  * Fixed all my Optimized Build compiler warnings
83  * 
84  * 39    1/10/98 1:14p John
85  * Added explanation to debug console commands
86  * 
87  * 38    12/21/97 4:33p John
88  * Made debug console functions a class that registers itself
89  * automatically, so you don't need to add the function to
90  * debugfunctions.cpp.  
91  * 
92  * 37    12/17/97 5:11p John
93  * Added brightening back into fade table.  Added code for doing the fast
94  * dynamic gun flashes and thruster flashes.
95  * 
96  * 36    12/17/97 1:16p John
97  * Made bright white not fade.
98  * 
99  * 35    12/03/97 4:48p Adam
100  * JOHN: Made engine glow scale, made thrust blend be additive.
101  * 
102  * 34    12/02/97 4:00p John
103  * Added first rev of thruster glow, along with variable levels of
104  * translucency, which retquired some restructing of palman.
105  * 
106  * 33    11/22/97 1:05p John
107  * added back in reverse palette lookup table.
108  * 
109  * 32    11/21/97 1:32p John
110  * made palette caching work on game palette better.
111  * 
112  * 31    11/21/97 11:32a John
113  * Added nebulas.   Fixed some warpout bugs.
114  * 
115  * 30    11/14/97 12:31p John
116  * Fixed some DirectX bugs.  Moved the 8-16 xlat tables into Graphics
117  * libs.  Made 16-bpp DirectX modes know what bitmap format they're in.
118  * 
119  * 29    10/31/97 10:48a John
120  * upped clr version to force rebuild after changing palette code.
121  * 
122  * 28    10/14/97 8:08a John
123  * added a bunch more 16 bit support
124  * 
125  * 27    10/03/97 9:10a John
126  * added better antialiased line drawer
127  * 
128  * 26    9/20/97 8:16a John
129  * Made .clr files go into the Cache directory. Replaced cfopen(name,NULL)
130  * to delete a file with cf_delete.
131  * 
132  * 25    9/09/97 3:39p Sandeep
133  * warning level 4 bugs
134  * 
135  * 24    7/28/97 5:08p John
136  * changed the color blending calculations.
137  * 
138  * 23    7/17/97 11:37a John
139  * made the fullscreen and windowed use separate palette cache files.
140  * 
141  * 22    7/16/97 5:29p John
142  * added palette table caching and made scaler and liner no light tmapper
143  * do alpha blending in 8 bpp mode.
144  * 
145  * 21    7/16/97 3:07p John
146  * 
147  * 20    7/10/97 2:06p John
148  * added code to specify alphablending type for bitmaps.
149  * 
150  * 19    6/11/97 6:00p John
151  * sped up alpha matching a bit.
152  * 
153  * 18    6/11/97 5:49p John
154  * Changed palette code to only recalculate alphacolors when needed, not
155  * when palette changes.
156  * 
157  * 17    6/11/97 1:13p John
158  * Started fixing all the text colors in the game.
159  * 
160  * 16    5/21/97 11:06a Lawrance
161  * added user_palette_find()
162  * 
163  * 15    5/14/97 10:53a John
164  * fixed some discrepencies between d3d and software palette setting.
165  * 
166  * 14    5/12/97 12:27p John
167  * Restructured Graphics Library to add support for multiple renderers.
168  * 
169  * 13    4/10/97 4:47p John
170  * used .3R .5G .1B for color matching.  Made palette brightening not be
171  * quite so much.
172  * 
173  * 12    4/08/97 5:18p John
174  * First rev of decent (dynamic, correct) lighting in FreeSpace.
175  * 
176  * 11    11/26/96 6:50p John
177  * Added some more hicolor primitives.  Made windowed mode run as current
178  * bpp, if bpp is 8,16,or 32.
179  * 
180  * 10    11/26/96 2:53p Allender
181  * simplify restore palette code
182  * 
183  * 9     11/26/96 9:44a Allender
184  * allow for use of different bitmap palettes
185  *
186  * $NoKeywords: $
187  */
188
189 #include "pstypes.h"
190 #include "cfile.h"
191 #include "palman.h"
192 #include "bmpman.h"
193 #include "2d.h"
194 #include "pcxutils.h"
195 #include "parselo.h"
196 #include "grinternal.h"
197
198 #define SQUARE(x) ((x)*(x))
199
200 #define NUM_BLEND_TABLES 3
201 float blend_table_factors[NUM_BLEND_TABLES] = { 0.5f, 1.0f, 1.2f };
202
203 ubyte palette_org[256*3];
204 ubyte gr_palette[256*3];
205 ubyte gr_fade_table[(256*34)*2];
206 static ubyte palette_blend_table[NUM_BLEND_TABLES*256*256];
207
208 int palette_blend_table_calculated = 0;
209 int palette_fade_table_calculated = 0;
210
211 uint gr_palette_checksum = 0;
212
213 uint palman_screen_signature = 0;
214
215 #define LOOKUP_SIZE (64*64*64)
216 ubyte palette_lookup[64*64*64];
217
218 static char palette_name[128] = { "none" };
219
220 static int Palman_restrict_colors = 0;
221
222 //extern ubyte palette_org[256*3];
223
224 int Palman_num_nondarkening_default = 0;
225 ubyte Palman_non_darkening_default[MAX_NONDARK_COLORS][3];
226
227 int Palman_num_nondarkening = 0;
228 ubyte Palman_non_darkening[MAX_NONDARK_COLORS][3];
229
230 int palman_is_nondarkening(int r,int g, int b)
231 {
232         int i;  
233
234         for (i=0; i<Palman_num_nondarkening; i++ )      {
235                 if ( (r==Palman_non_darkening[i][0]) && (g==Palman_non_darkening[i][1]) && (b==Palman_non_darkening[i][2]) )    {
236                         return 1;
237                 }
238         }
239         return 0;
240 }
241
242 void palman_load_pixels()
243 {
244         // open pixels.tbl
245         read_file_text("pixels.tbl");
246         reset_parse();
247
248         // parse pixels 
249         while(!optional_string("#END")){
250                 // nondarkening pixel
251                 if(required_string("+ND")){
252                         stuff_byte(&Palman_non_darkening_default[Palman_num_nondarkening_default][0]);
253                         stuff_byte(&Palman_non_darkening_default[Palman_num_nondarkening_default][1]);
254                         stuff_byte(&Palman_non_darkening_default[Palman_num_nondarkening_default++][2]);
255                 }
256         }
257
258         // set this to be the active table
259         palman_set_nondarkening(Palman_non_darkening_default, Palman_num_nondarkening_default);
260 }
261
262 void palman_set_nondarkening(ubyte colors[MAX_NONDARK_COLORS][3], int size)
263 {
264         // if we're supposed to use the passed table
265         memcpy(Palman_non_darkening, colors, MAX_NONDARK_COLORS * 3);
266         Palman_num_nondarkening = size;
267 }
268
269 void palette_cache_clear()
270 {
271         int i;
272
273         for (i=0; i<LOOKUP_SIZE; i++ )  {
274                 palette_lookup[i] = 255;
275         }
276 }
277
278 int palette_cache_find( int r, int g, int b )
279 {
280         if ( !palman_is_nondarkening(r,g,b))    {
281                 int value = ((r/4)<<12)+((g/4)<<6)+(b/4);
282                 if ( palette_lookup[value] != 255 )     {
283                         return palette_lookup[value];
284                 }
285         }
286         return -1;
287 }
288
289 void palette_cache_add( int r, int g, int b, int index )
290 {
291         int value = ((r/4)<<12)+((g/4)<<6)+(b/4);
292
293         if ( !palman_is_nondarkening(r,g,b))    {
294                 palette_lookup[value] = (ubyte)index;
295         }
296 }
297
298 char palette_base_filename[128] = { "default" };
299 int palette_base_loaded = 0;
300
301 void palette_load_table( char * filename )
302 {
303         int i;
304         int w, h;
305         int pcx_error;
306
307         strcpy( palette_base_filename, filename );
308         char * p = strchr(palette_base_filename,'.');
309         if ( p )        {
310                 *p = 0;
311         }
312
313         pcx_error = pcx_read_header(palette_base_filename, &w, &h, palette_org );
314         if ( pcx_error != PCX_ERROR_NONE )      {
315                 // Read the old .256 file
316                 CFILE *fp;
317                 int fsize;
318                 fp = cfopen( palette_base_filename, "rb" );
319                 if ( fp==NULL)
320                         Error( LOCATION, "Can't open palette file <%s>",palette_base_filename);
321
322                 fsize   = cfilelength( fp );
323                 Assert( fsize == 9472 );
324                 cfread( palette_org, 256*3, 1, fp );
325                 cfclose(fp);
326
327                 for (i=0; i<768; i++ )  {       
328                         palette_org[i] = ubyte((palette_org[i]*255)/63);
329                 }
330         }
331
332         palette_base_loaded = 1;
333
334         gr_set_palette(palette_base_filename, palette_org);
335 }
336
337
338 DCF(palette,"Loads a new palette")
339 {
340         if ( Dc_command )       {
341                 dc_get_arg(ARG_STRING|ARG_NONE);
342                 if ( Dc_arg_type == ARG_NONE )  {
343                 } else {
344                         palette_load_table( Dc_arg );
345                 }
346         }
347         if ( Dc_help )  {
348                 dc_printf( "Usage: palette filename\nLoads the palette file.\n" );
349         }
350
351 }
352
353 int Palman_allow_any_color = 0;
354
355
356 uint palette_find( int r, int g, int b )
357 {
358         int i, j;
359         int best_value, best_index, value;
360
361         int     is_transparent = 0;
362         if ( (r == 0) && (g==255) && (b==0) )   {
363                 is_transparent = 1;
364         }
365
366         int restrict = 0;
367         if ( Palman_restrict_colors && (!Palman_allow_any_color) )      {
368                 restrict = 1;
369         }
370                         
371 //      int     rgb = ((r/4)<<12)+((g/4)<<6)+(b/4);
372
373         i = palette_cache_find(r,g,b);
374         if ( i != -1 )  {
375                 if ( restrict ) {
376                         if ( i > 127 )  {
377                                 return i;
378                         }
379                 } else {
380                         return i;
381                 }
382         }
383
384         best_value = 1000000000;
385         best_index = -1;
386
387         int bottom_color = 0;
388         if ( restrict ) {
389                 bottom_color = 128;
390         }
391
392         j=3*bottom_color; 
393
394         for (i=bottom_color; i<255; i++ )       {
395                 int pr, pg, pb;
396
397                 pr = gr_palette[j];
398                 pg = gr_palette[j+1];
399                 pb = gr_palette[j+2];
400
401                 value = SQUARE(r-pr) + SQUARE(g-pg) + SQUARE(b-pb);
402
403                 if ( (best_index==-1) || (value < best_value) ) {
404                         // Don't map anything to 0,255,0 (transparent) ever, except 0,255,0
405                         if (value==0) {
406                                 palette_cache_add( r, g, b, i );
407                                 return i;
408                         }
409                         // Not an exact match, so don't let anything map to a nondarkening color.
410                         if ( (!is_transparent) && (!palman_is_nondarkening( pr, pg, pb ))  )    {
411                                 best_value = value;
412                                 best_index = i;
413                         }
414                 }
415                 j += 3;
416         }
417
418         if ( best_index == -1 ) {
419                 best_index = bottom_color;
420         }
421
422         palette_cache_add( r, g, b, best_index );
423         return best_index;
424 }
425
426 // version 0 - initial revision
427 // version 2 - changed 16-bpp fade table to include the 1.5 brightening factor
428 // version 3 - changed to put black in color 0 all the time
429 // version 4 - took out all truecolor lookup tables
430 // version 5 - changed palette to use 254 entries for nebula, made color mapping different
431 // version 6 - took out 1.5 brightness
432 // version 7 - added blending tables
433 // version 8 - made glows use additive blending
434 // version 9 - made 255,255,255 not fade
435 // version 10 - made fade table go to white again for anything l above 0.75.
436 // version 11 - made fade table go from 0-1 instead of 0-0.75
437 // version 12 - added variable gamma
438 // version 13 - Reduced blending tables from 5 to 3. Save 128KB RAM.
439 // version 14 - made palette never map anything to green.
440 // version 15 - made green blending with anything be black
441 // version 16 - added compression
442 // version 17 - added more nondarkening colors
443 // version 18 - fixed bug with blue nondarkening colors
444 // version 19 - fixed bug where only colors divisible by 4 got used.
445 // version 20 - added flag to only use lower 128 colors for palette.
446 #define PAL_ID 'LAPV'                   // VPAL (Volition Palette)
447 #define PAL_VERSION  20
448 #define PAL_LAST_COMPATIBLE_VERSION 20
449
450 void palette_write_cached1( char *name )
451 {
452         CFILE *fp;
453         char new_name[128];
454
455         strcpy( new_name, name );
456         strcat( new_name, ".clr" );
457         
458 //      mprintf(( "Writing palette cache file '%s'\n", new_name ));
459
460         fp = cfopen( new_name, "wb", CFILE_NORMAL, CF_TYPE_CACHE );
461         if ( !fp ) return;
462         
463         cfwrite_uint( PAL_ID, fp );
464         cfwrite_int(PAL_VERSION, fp );
465         cfwrite( &gr_palette_checksum, 4, 1, fp );
466
467         cfwrite_compressed( &gr_palette, 256*3, 1, fp );                                                // < 1 KB
468
469         cfwrite_compressed( &palette_lookup, LOOKUP_SIZE, 1, fp );                      // 256KB
470         
471         if ( palette_fade_table_calculated )    {
472                 cfwrite_int(1,fp);
473                 cfwrite_int(Gr_gamma_int,fp);
474                 cfwrite_compressed( &gr_fade_table,   256*34*2, 1, fp );                // 17KB
475         } else {
476                 cfwrite_int(0,fp);
477         }
478         
479         if ( palette_blend_table_calculated )   {
480                 cfwrite_int(NUM_BLEND_TABLES,fp);
481                 cfwrite_compressed( &palette_blend_table,  256*256, NUM_BLEND_TABLES, fp );     //64KB*
482         } else {
483                 cfwrite_int(0,fp);
484         }
485
486         cfclose(fp);
487 //      mprintf(( "Done.\n" ));
488 }
489
490 // Returns TRUE if successful, else 0
491
492 int palette_read_cached( char *name )
493 {
494         CFILE *fp;
495         char new_name[128];
496         int version;
497         uint id, new_checksum;
498         ubyte new_palette[768];
499
500         strcpy( new_name, name );
501         strcat( new_name, ".clr" );
502
503 //      mprintf(( "Reading palette '%s'\n", name ));
504         
505         fp = cfopen( new_name, "rb", CFILE_NORMAL, CF_TYPE_CACHE );
506
507         // Couldn't find file
508         if ( !fp ) {
509                 mprintf(( "No cached palette file\n" ));
510                 return 0;
511         }
512
513         id  = cfread_uint( fp );
514         if ( id != PAL_ID )     {
515                 mprintf(( "Cached palette file has incorrect ID\n" ));
516                 cfclose(fp);
517                 return 0;
518         }
519         version = cfread_int( fp );
520         if ( version < PAL_LAST_COMPATIBLE_VERSION ) {
521                 mprintf(( "Cached palette file is an older incompatible version\n" ));
522                 cfclose(fp);
523                 return 0;
524         }
525         
526         cfread( &new_checksum, 4, 1, fp );
527         if ( gr_palette_checksum != new_checksum )      {
528                 mprintf(( "Cached palette file is out of date (Checksum)\n" ));
529                 cfclose(fp);
530                 return 0;
531         }
532
533         cfread_compressed( &new_palette, 256*3, 1, fp );
534         if ( memcmp( new_palette, gr_palette, 768 ) )   {
535                 mprintf(( "Cached palette file is out of date (Contents)\n" ));
536                 cfclose(fp);
537                 return 0;
538         }
539
540         cfread_compressed( &palette_lookup, LOOKUP_SIZE, 1, fp );                       // 256KB
541
542         int fade_table_saved = cfread_int(fp);
543         
544         if ( fade_table_saved ) {
545                 int new_gamma;
546                 cfread( &new_gamma, 4, 1, fp );
547                 cfread_compressed( &gr_fade_table,   256*34*2, 1, fp );         // 17KB
548                 if ( new_gamma == Gr_gamma_int )        {
549                         palette_fade_table_calculated = 1;
550                 } else {
551                         palette_fade_table_calculated = 0;
552                 }
553         } else {
554                 palette_fade_table_calculated = 0;
555         }
556         
557         int num_blend_tables_saved = cfread_int(fp);
558         if ( (num_blend_tables_saved == NUM_BLEND_TABLES) && (num_blend_tables_saved>0))        {
559                 palette_blend_table_calculated = 1;
560                 cfread_compressed( &palette_blend_table,  256*256, NUM_BLEND_TABLES, fp );      //64KB*
561         } else {
562                 palette_blend_table_calculated = 0;
563         }
564
565         cfclose(fp);
566
567 //      mprintf(( "Done.\n" ));
568
569         return 1;
570 }
571
572 void palman_create_blend_table(float factor, ubyte *table)
573 {
574         int i;
575
576         // Make the blending table
577         for (i=0; i<256; i++ )  {
578                 int j, r, g, b;
579                 float si, fr, fg, fb, br, bg, bb;
580                 float Sf, Df;
581
582                 fr = i2fl(gr_palette[i*3+0]);
583                 fg = i2fl(gr_palette[i*3+1]);
584                 fb = i2fl(gr_palette[i*3+2]);
585
586                 // Make everything blended with Xparent be black
587                 if ( i==255 )   {
588                         fr = fg = fb = 0.0f;
589                 }
590
591                 si = (( fr+fg+fb ) / (256.0f*3.0f)) * factor;
592
593                 if ( factor > 1.0f )    {
594                         if ( si > 1.0f )        {
595                                 Sf = 1.0f;
596                                 Df = 0.0f;
597                         } else  {
598                                 Sf = 1.0f;
599                                 Df = 1.0f - si;
600                         }
601                 } else {
602                         if ( si > 1.0f )        {
603                                 Sf = 1.0f;
604                                 Df = 0.0f;
605                         } else  {
606                                 Sf = si;
607                                 Df = 1.0f;
608                         }
609                         Sf = factor;
610                         Df = 1.0f;
611                 }
612  
613 //              Sf = Df =1.0f;
614
615                 for (j=0; j<256; j++ )  {
616                         br = i2fl(gr_palette[j*3+0]);
617                         bg = i2fl(gr_palette[j*3+1]);
618                         bb = i2fl(gr_palette[j*3+2]);
619
620                         // Make all things on top of Xparent be black
621                         if ( j==255 )   {
622                                 br = bg = bb = 0.0f;
623                         }
624
625                         r = fl2i( fr*Sf + br*Df );
626                         g = fl2i( fg*Sf + bg*Df );
627                         b = fl2i( fb*Sf + bb*Df );
628
629                         int max = r;
630                         if ( g > max ) max = g;
631                         if ( b > max ) max = b;
632                         if ( max > 255 )        {
633                                 r = (255*r)/max;
634                                 g = (255*g)/max;
635                                 b = (255*b)/max;
636                         }
637                         if ( r > 255 ) r = 255; else if ( r < 0 ) r = 0;
638                         if ( g > 255 ) g = 255; else if ( g < 0 ) g = 0;
639                         if ( b > 255 ) b = 255; else if ( b < 0 ) b = 0;
640
641                         if ( i == 255 )
642                                 table[i*256+j] = (unsigned char)j;
643                         else {
644                                 // If background transparent, and color isn't bright, call it transparent.
645                                 if ( j == 255 && ((r+g+b) < 110))       {
646                                         table[i*256+j] = 255;
647                                 } else {
648                                         table[i*256+j] = (unsigned char)palette_find(r,g,b);
649                                 }
650                         }
651                 }
652         }
653 }
654
655 void palette_flush()
656 {
657         // DB 2/3/99 - I think this was causing some wacky unhandled exceptions at game shutdown. Since we don't use palettes anymore.....
658         /*
659         if ( stricmp( palette_name, "none" ) )  {
660                 palette_write_cached1( palette_name );
661         }
662         */
663 }
664
665
666 // When gr_set_palette is called, it fills in gr_palette and then calls this
667 // function, which should update all the tables.
668 // Pass NULL to flush current palette.
669 void palette_update(char *name_with_extension, int restrict_font_to_128)
670 {
671 //      int i;
672         uint tmp_checksum;
673         char name[128];
674
675         Palman_restrict_colors = restrict_font_to_128;
676         
677 //      mprintf(( "<<<<<<<<< PALETTE UPDATE (%s) >>>>>>>>>>>\n", (name_with_extension?name_with_extension:"null") ));
678
679         strcpy( name, name_with_extension );
680         char *p = strchr( name, '.' );
681         if ( p ) *p = 0;
682
683         strcpy( palette_name, name );
684
685         tmp_checksum = palette_compute_checksum( gr_palette );
686         if ( tmp_checksum == gr_palette_checksum ) return;
687
688         gr_palette_checksum = tmp_checksum;
689
690         // Clear the lookup cache, since the palette has changed
691         palette_cache_clear();
692         palette_blend_table_calculated = 0;
693         palette_fade_table_calculated = 0;
694
695         // For "none" palettes, don't calculate tables
696         if ( !stricmp( name, "none" ) ) {
697                 bm_update();                    // update the bitmap palette's
698                 return;
699         }
700
701         // Read in the cached info if there is any.
702         if ( palette_read_cached( name ) )      {
703                 bm_update();                    // update the bitmap palette's
704                 return;
705         }
706         
707         bm_update();                    // update the bitmap palette's
708 }
709
710 ubyte *palette_get_fade_table()
711 {
712         int i,l;
713
714         if ( palman_screen_signature != gr_screen.signature )   {
715                 palman_screen_signature = gr_screen.signature;
716                 palette_fade_table_calculated = 0;
717         }
718
719
720         if ( !palette_fade_table_calculated )   {
721                 //mprintf(( "Creating fading table..." ));      
722
723                 for (i=0; i<256; i++ )  {
724                         int r, g, b;
725                         int ur, ug, ub;
726                         r = gr_palette[i*3+0];
727                         g = gr_palette[i*3+1];
728                         b = gr_palette[i*3+2];
729
730                         if ( palman_is_nondarkening(r,g,b))             {
731                                 // Make pure white not fade
732                                 for (l=0; l<32; l++ )   {
733                                         gr_fade_table[((l+1)*256)+i] = (unsigned char)i;
734                                 }
735                         } else {
736                                 for (l=0; l<32; l++ )   {
737
738                                         if ( l < 24 )   {
739                                                 float f = (float)pow(i2fl(l)/23.0f, 1.0f/Gr_gamma);
740                                                 ur = fl2i(i2fl(r)*f); if ( ur > 255 ) ur = 255;
741                                                 ug = fl2i(i2fl(g)*f); if ( ug > 255 ) ug = 255;
742                                                 ub = fl2i(i2fl(b)*f); if ( ub > 255 ) ub = 255;
743                                         } else {
744                                                 int x,y;
745                                                 int gi, gr, gg, gb;
746                         
747                                                 gi = (r+g+b)/3;
748
749                                                 #ifdef RGB_LIGHTING
750                                                         gr = r;
751                                                         gg = g;
752                                                         gb = gi*2;
753                                                 #else
754                                                         gr = r*2;
755                                                         gg = g*2;
756                                                         gb = b*2;
757                                                 #endif
758                                 
759                                                 x = l-24;                       // x goes from 0 to 7
760                                                 y = 31-l;                       // y goes from 7 to 0
761
762                                                 ur = ((gr*x)+(r*y))/7; if ( ur > 255 ) ur = 255;
763                                                 ug = ((gg*x)+(g*y))/7; if ( ug > 255 ) ug = 255;
764                                                 ub = ((gb*x)+(b*y))/7; if ( ub > 255 ) ub = 255;
765                                         }
766                                         gr_fade_table[((l+1)*256)+i] = (unsigned char)palette_find( ur, ug, ub );
767
768                                 }
769                         }
770                         gr_fade_table[ (0*256)+i ] = gr_fade_table[ (1*256)+i ];
771                         gr_fade_table[ (33*256)+i ] = gr_fade_table[ (32*256)+i ];
772                 }
773
774                 // Mirror the fade table
775                 for (i=0; i<34; i++ )   {
776                         for ( l = 0; l < 256; l++ )     {
777                                 gr_fade_table[ ((67-i)*256)+l ] = gr_fade_table[ (i*256)+l ];
778                         }
779                 }
780
781 //              mprintf(( "done\n" ));  
782                 palette_fade_table_calculated = 1;
783         }
784
785         return &gr_fade_table[0];
786 }
787
788
789 ubyte *palette_get_blend_table(float alpha)
790 {
791         int i;
792
793         if ( !palette_blend_table_calculated )  {
794 //              mprintf(( "Creating blending table..." ));      
795                 for (i=0; i<NUM_BLEND_TABLES; i++ )     {
796                         palman_create_blend_table(blend_table_factors[i], &palette_blend_table[i*256*256] );
797                 }
798 //              mprintf(( "done\n" ));  
799                 palette_blend_table_calculated = 1;
800         }
801         
802         for (i=0; i<NUM_BLEND_TABLES; i++ )     {
803                 if ( alpha <= blend_table_factors[i] )  
804                         break;
805         } 
806         if ( i<0 ) i = 0;
807         if ( i>NUM_BLEND_TABLES-1 ) i = NUM_BLEND_TABLES-1;
808
809         return &palette_blend_table[i*256*256];
810 }
811
812
813
814 // compute a simple checksum on the given palette.  Used by the bitmap manager
815 // to determine if we need to reload a new palette for a bitmap.  Code liberally
816 // stolen from descent networking checksum code
817 uint palette_compute_checksum( ubyte *pal )
818 {
819         int i;
820         uint sum1, sum2;
821
822         sum1 = sum2 = 0;
823
824         for (i = 0; i < 768; i++) {
825                 sum1 += (uint)pal[i];
826                 if ( sum1 >= 255 ) sum1 -= 255;
827                 sum2 += sum1;
828         }
829         sum2 %= 255;
830
831         return ((sum1<<8)+sum2);
832 }
833
834 // this function takes a bitmap number and sets the game palette to the palette of this
835 // bitmap.
836 void palette_use_bm_palette(int n)
837 {
838         ubyte tmp[768];
839         char name[128];
840
841         bm_get_palette(n, tmp, name);                           // get the palette for this bitmap
842
843         gr_set_palette(name, tmp);                              // load the new palette.
844 }
845
846 void palette_restore_palette()
847 {
848         ubyte tmp[768];
849         memcpy(tmp, palette_org, 3*256);
850
851         if ( palette_base_loaded )              {
852                 gr_set_palette(palette_base_filename, tmp);
853         }
854 }