2 * $Logfile: /Freespace2/code/Palman/PalMan.cpp $
7 * Palette manager routines
10 * Revision 1.1 2002/05/03 03:28:10 root
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.
18 * 4 2/05/99 12:52p Dave
19 * Fixed Glide nondarkening textures.
21 * 3 2/03/99 6:06p Dave
22 * Groundwork for FS2 PXO usertracker support. Gametracker support next.
24 * 2 10/07/98 10:53a Dave
27 * 1 10/07/98 10:50a Dave
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.
33 * 54 5/16/98 4:37p John
35 * 53 5/16/98 4:34p John
36 * fixed bug where only colors divisible by 4 would work.
38 * 52 5/15/98 3:28p John
39 * fixed bug with blue nondarkening colors
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.
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.
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.
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.
58 * 47 4/15/98 5:44p John
59 * Made no colors except green map to 0,255,0
61 * 46 4/09/98 11:04p John
62 * Changed ID's in output files to make more sense.
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.
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.
72 * 43 3/24/98 3:58p John
73 * Put in (hopefully) final gamma setting code.
75 * 42 2/24/98 1:59p John
76 * Made fade table use a gamma
78 * 41 1/29/98 8:18a John
79 * Put in some commented out hooks for RGB lighting
81 * 40 1/19/98 6:15p John
82 * Fixed all my Optimized Build compiler warnings
84 * 39 1/10/98 1:14p John
85 * Added explanation to debug console commands
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
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.
96 * 36 12/17/97 1:16p John
97 * Made bright white not fade.
99 * 35 12/03/97 4:48p Adam
100 * JOHN: Made engine glow scale, made thrust blend be additive.
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.
106 * 33 11/22/97 1:05p John
107 * added back in reverse palette lookup table.
109 * 32 11/21/97 1:32p John
110 * made palette caching work on game palette better.
112 * 31 11/21/97 11:32a John
113 * Added nebulas. Fixed some warpout bugs.
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.
119 * 29 10/31/97 10:48a John
120 * upped clr version to force rebuild after changing palette code.
122 * 28 10/14/97 8:08a John
123 * added a bunch more 16 bit support
125 * 27 10/03/97 9:10a John
126 * added better antialiased line drawer
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.
132 * 25 9/09/97 3:39p Sandeep
133 * warning level 4 bugs
135 * 24 7/28/97 5:08p John
136 * changed the color blending calculations.
138 * 23 7/17/97 11:37a John
139 * made the fullscreen and windowed use separate palette cache files.
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.
145 * 21 7/16/97 3:07p John
147 * 20 7/10/97 2:06p John
148 * added code to specify alphablending type for bitmaps.
150 * 19 6/11/97 6:00p John
151 * sped up alpha matching a bit.
153 * 18 6/11/97 5:49p John
154 * Changed palette code to only recalculate alphacolors when needed, not
155 * when palette changes.
157 * 17 6/11/97 1:13p John
158 * Started fixing all the text colors in the game.
160 * 16 5/21/97 11:06a Lawrance
161 * added user_palette_find()
163 * 15 5/14/97 10:53a John
164 * fixed some discrepencies between d3d and software palette setting.
166 * 14 5/12/97 12:27p John
167 * Restructured Graphics Library to add support for multiple renderers.
169 * 13 4/10/97 4:47p John
170 * used .3R .5G .1B for color matching. Made palette brightening not be
173 * 12 4/08/97 5:18p John
174 * First rev of decent (dynamic, correct) lighting in FreeSpace.
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.
180 * 10 11/26/96 2:53p Allender
181 * simplify restore palette code
183 * 9 11/26/96 9:44a Allender
184 * allow for use of different bitmap palettes
194 #include "pcxutils.h"
196 #include "grinternal.h"
198 #define SQUARE(x) ((x)*(x))
200 #define NUM_BLEND_TABLES 3
201 float blend_table_factors[NUM_BLEND_TABLES] = { 0.5f, 1.0f, 1.2f };
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];
208 int palette_blend_table_calculated = 0;
209 int palette_fade_table_calculated = 0;
211 uint gr_palette_checksum = 0;
213 uint palman_screen_signature = 0;
215 #define LOOKUP_SIZE (64*64*64)
216 ubyte palette_lookup[64*64*64];
218 static char palette_name[128] = { "none" };
220 static int Palman_restrict_colors = 0;
222 //extern ubyte palette_org[256*3];
224 int Palman_num_nondarkening_default = 0;
225 ubyte Palman_non_darkening_default[MAX_NONDARK_COLORS][3];
227 int Palman_num_nondarkening = 0;
228 ubyte Palman_non_darkening[MAX_NONDARK_COLORS][3];
230 int palman_is_nondarkening(int r,int g, int b)
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]) ) {
242 void palman_load_pixels()
245 read_file_text("pixels.tbl");
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]);
258 // set this to be the active table
259 palman_set_nondarkening(Palman_non_darkening_default, Palman_num_nondarkening_default);
262 void palman_set_nondarkening(ubyte colors[MAX_NONDARK_COLORS][3], int size)
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;
269 void palette_cache_clear()
273 for (i=0; i<LOOKUP_SIZE; i++ ) {
274 palette_lookup[i] = 255;
278 int palette_cache_find( int r, int g, int b )
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];
289 void palette_cache_add( int r, int g, int b, int index )
291 int value = ((r/4)<<12)+((g/4)<<6)+(b/4);
293 if ( !palman_is_nondarkening(r,g,b)) {
294 palette_lookup[value] = (ubyte)index;
298 char palette_base_filename[128] = { "default" };
299 int palette_base_loaded = 0;
301 void palette_load_table( char * filename )
307 strcpy( palette_base_filename, filename );
308 char * p = strchr(palette_base_filename,'.');
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
318 fp = cfopen( palette_base_filename, "rb" );
320 Error( LOCATION, "Can't open palette file <%s>",palette_base_filename);
322 fsize = cfilelength( fp );
323 Assert( fsize == 9472 );
324 cfread( palette_org, 256*3, 1, fp );
327 for (i=0; i<768; i++ ) {
328 palette_org[i] = ubyte((palette_org[i]*255)/63);
332 palette_base_loaded = 1;
334 gr_set_palette(palette_base_filename, palette_org);
338 DCF(palette,"Loads a new palette")
341 dc_get_arg(ARG_STRING|ARG_NONE);
342 if ( Dc_arg_type == ARG_NONE ) {
344 palette_load_table( Dc_arg );
348 dc_printf( "Usage: palette filename\nLoads the palette file.\n" );
353 int Palman_allow_any_color = 0;
356 uint palette_find( int r, int g, int b )
359 int best_value, best_index, value;
361 int is_transparent = 0;
362 if ( (r == 0) && (g==255) && (b==0) ) {
367 if ( Palman_restrict_colors && (!Palman_allow_any_color) ) {
371 // int rgb = ((r/4)<<12)+((g/4)<<6)+(b/4);
373 i = palette_cache_find(r,g,b);
384 best_value = 1000000000;
387 int bottom_color = 0;
394 for (i=bottom_color; i<255; i++ ) {
398 pg = gr_palette[j+1];
399 pb = gr_palette[j+2];
401 value = SQUARE(r-pr) + SQUARE(g-pg) + SQUARE(b-pb);
403 if ( (best_index==-1) || (value < best_value) ) {
404 // Don't map anything to 0,255,0 (transparent) ever, except 0,255,0
406 palette_cache_add( r, g, b, i );
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 )) ) {
418 if ( best_index == -1 ) {
419 best_index = bottom_color;
422 palette_cache_add( r, g, b, best_index );
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
450 void palette_write_cached1( char *name )
455 strcpy( new_name, name );
456 strcat( new_name, ".clr" );
458 // mprintf(( "Writing palette cache file '%s'\n", new_name ));
460 fp = cfopen( new_name, "wb", CFILE_NORMAL, CF_TYPE_CACHE );
463 cfwrite_uint( PAL_ID, fp );
464 cfwrite_int(PAL_VERSION, fp );
465 cfwrite( &gr_palette_checksum, 4, 1, fp );
467 cfwrite_compressed( &gr_palette, 256*3, 1, fp ); // < 1 KB
469 cfwrite_compressed( &palette_lookup, LOOKUP_SIZE, 1, fp ); // 256KB
471 if ( palette_fade_table_calculated ) {
473 cfwrite_int(Gr_gamma_int,fp);
474 cfwrite_compressed( &gr_fade_table, 256*34*2, 1, fp ); // 17KB
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*
487 // mprintf(( "Done.\n" ));
490 // Returns TRUE if successful, else 0
492 int palette_read_cached( char *name )
497 uint id, new_checksum;
498 ubyte new_palette[768];
500 strcpy( new_name, name );
501 strcat( new_name, ".clr" );
503 // mprintf(( "Reading palette '%s'\n", name ));
505 fp = cfopen( new_name, "rb", CFILE_NORMAL, CF_TYPE_CACHE );
507 // Couldn't find file
509 mprintf(( "No cached palette file\n" ));
513 id = cfread_uint( fp );
514 if ( id != PAL_ID ) {
515 mprintf(( "Cached palette file has incorrect ID\n" ));
519 version = cfread_int( fp );
520 if ( version < PAL_LAST_COMPATIBLE_VERSION ) {
521 mprintf(( "Cached palette file is an older incompatible version\n" ));
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" ));
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" ));
540 cfread_compressed( &palette_lookup, LOOKUP_SIZE, 1, fp ); // 256KB
542 int fade_table_saved = cfread_int(fp);
544 if ( fade_table_saved ) {
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;
551 palette_fade_table_calculated = 0;
554 palette_fade_table_calculated = 0;
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*
562 palette_blend_table_calculated = 0;
567 // mprintf(( "Done.\n" ));
572 void palman_create_blend_table(float factor, ubyte *table)
576 // Make the blending table
577 for (i=0; i<256; i++ ) {
579 float si, fr, fg, fb, br, bg, bb;
582 fr = i2fl(gr_palette[i*3+0]);
583 fg = i2fl(gr_palette[i*3+1]);
584 fb = i2fl(gr_palette[i*3+2]);
586 // Make everything blended with Xparent be black
591 si = (( fr+fg+fb ) / (256.0f*3.0f)) * factor;
593 if ( factor > 1.0f ) {
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]);
620 // Make all things on top of Xparent be black
625 r = fl2i( fr*Sf + br*Df );
626 g = fl2i( fg*Sf + bg*Df );
627 b = fl2i( fb*Sf + bb*Df );
630 if ( g > max ) max = g;
631 if ( b > max ) max = b;
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;
642 table[i*256+j] = (unsigned char)j;
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;
648 table[i*256+j] = (unsigned char)palette_find(r,g,b);
657 // DB 2/3/99 - I think this was causing some wacky unhandled exceptions at game shutdown. Since we don't use palettes anymore.....
659 if ( stricmp( palette_name, "none" ) ) {
660 palette_write_cached1( palette_name );
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)
675 Palman_restrict_colors = restrict_font_to_128;
677 // mprintf(( "<<<<<<<<< PALETTE UPDATE (%s) >>>>>>>>>>>\n", (name_with_extension?name_with_extension:"null") ));
679 strcpy( name, name_with_extension );
680 char *p = strchr( name, '.' );
683 strcpy( palette_name, name );
685 tmp_checksum = palette_compute_checksum( gr_palette );
686 if ( tmp_checksum == gr_palette_checksum ) return;
688 gr_palette_checksum = tmp_checksum;
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;
695 // For "none" palettes, don't calculate tables
696 if ( !stricmp( name, "none" ) ) {
697 bm_update(); // update the bitmap palette's
701 // Read in the cached info if there is any.
702 if ( palette_read_cached( name ) ) {
703 bm_update(); // update the bitmap palette's
707 bm_update(); // update the bitmap palette's
710 ubyte *palette_get_fade_table()
714 if ( palman_screen_signature != gr_screen.signature ) {
715 palman_screen_signature = gr_screen.signature;
716 palette_fade_table_calculated = 0;
720 if ( !palette_fade_table_calculated ) {
721 //mprintf(( "Creating fading table..." ));
723 for (i=0; i<256; i++ ) {
726 r = gr_palette[i*3+0];
727 g = gr_palette[i*3+1];
728 b = gr_palette[i*3+2];
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;
736 for (l=0; l<32; l++ ) {
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;
759 x = l-24; // x goes from 0 to 7
760 y = 31-l; // y goes from 7 to 0
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;
766 gr_fade_table[((l+1)*256)+i] = (unsigned char)palette_find( ur, ug, ub );
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 ];
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 ];
781 // mprintf(( "done\n" ));
782 palette_fade_table_calculated = 1;
785 return &gr_fade_table[0];
789 ubyte *palette_get_blend_table(float alpha)
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] );
798 // mprintf(( "done\n" ));
799 palette_blend_table_calculated = 1;
802 for (i=0; i<NUM_BLEND_TABLES; i++ ) {
803 if ( alpha <= blend_table_factors[i] )
807 if ( i>NUM_BLEND_TABLES-1 ) i = NUM_BLEND_TABLES-1;
809 return &palette_blend_table[i*256*256];
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 )
824 for (i = 0; i < 768; i++) {
825 sum1 += (uint)pal[i];
826 if ( sum1 >= 255 ) sum1 -= 255;
831 return ((sum1<<8)+sum2);
834 // this function takes a bitmap number and sets the game palette to the palette of this
836 void palette_use_bm_palette(int n)
841 bm_get_palette(n, tmp, name); // get the palette for this bitmap
843 gr_set_palette(name, tmp); // load the new palette.
846 void palette_restore_palette()
849 memcpy(tmp, palette_org, 3*256);
851 if ( palette_base_loaded ) {
852 gr_set_palette(palette_base_filename, tmp);