aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/e_color.cpp
blob: b0c222ad017682607a6a6d91d993553bf3300759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*
 * PARSEC - Color Manipulation Code
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/26 03:43:33 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1996-1999
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

// C library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#include "vid_defs.h"

// local module header
#include "e_color.h"


// flags
#define CALC_TEXTURE_GOURAUD_TABLES



// number of different intensity levels used for gouraud shading
#define NUM_GOURAUD_INTENSITY_LEVELS	256

// size of pseudo palettes for direct color modes
#define PAL_XLAT_TABLE_TO_16BIT_SIZE	( VGA_PAL_NUMCOLORS * 2 )
#define PAL_XLAT_TABLE_TO_32BIT_SIZE	( VGA_PAL_NUMCOLORS * 4 )

// maximum size of a single translucency mapping table
#define TRTAB_MAXSIZE					( 65536 * 2 )

// maximum size of blueprint color lookup table
#define BLUEPRINT_CLUT_SIZE				( 4*256 * 5 )


// error strings
static char no_bitmap_mem[]	 = "no mem for bitmap color translation.";
static char no_font_mem[]	 = "no mem for font color translation.";


// initialization done flag
static int color_init_done = FALSE;



// translation tables for colors from low to high bit-resolution --------------
//
byte colbits_1_to_8[]	= { 0x00, 0xff };
byte colbits_2_to_8[]	= { 0x00, 0x55, 0xaa, 0xff };
byte colbits_3_to_8[]	= { 0x00, 0x24, 0x49, 0x6d, 0x92, 0xb6, 0xdb, 0xff };
byte colbits_4_to_8[]	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
							0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
byte colbits_5_to_8[]	= { 0x00, 0x08, 0x10, 0x18, 0x21, 0x29, 0x31, 0x39,
							0x42, 0x4a, 0x52, 0x5a, 0x63, 0x6b, 0x73, 0x7b,
							0x84, 0x8c, 0x94, 0x9c, 0xa5, 0xad, 0xb5, 0xbd,
							0xc6, 0xce, 0xd6, 0xde, 0xe7, 0xef, 0xf7, 0xff };
byte colbits_6_to_8[]	= { 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
							0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c,
							0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d,
							0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d,
							0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
							0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
							0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7, 0xdb, 0xdf,
							0xe3, 0xe7, 0xeb, 0xef, 0xf3, 0xf7, 0xfb, 0xff };



// color setup (conversion) flags ---------------------------------------------
//
dword ColorSetupFlags = COLORSETUP_DEFAULT;


// calculate 1K translation table for color indexes to visuals ----------------
//
PRIVATE
void SetupColorIndexToVisual( char *palette )
{
	ASSERT( palette != NULL );

	// destination table
	visual_t *xlattab = IndexToVisualTab;


	ASSERT( ( COLRGBA_CHANNELBITS == 8 ) && ( VGA_PAL_COLBITS == 6 ) );

	dword alpha = 0x00000000;
	if ( ColorSetupFlags & COLORSETUP_32BPP_PAL_SET_ALPHA ) {
		alpha = ( ColorSetupFlags & COLORSETUP_32BPP_PAL_ALPHA_IN_LSB ) ?
			0x000000ff : 0xff000000;
	}

	// first entry is always black
	*xlattab++ = 0;

	// calculate next 255 entries
	for ( int ppos = 3; ppos < VGA_PAL_NUMCOLORS * 3; ppos+=3 ) {

		byte r  = colbits_6_to_8[ palette[ ppos + 0 ] & 0x3f ];
		byte g  = colbits_6_to_8[ palette[ ppos + 1 ] & 0x3f ];
		byte b  = colbits_6_to_8[ palette[ ppos + 2 ] & 0x3f ];

		dword w = ( ColorSetupFlags & COLORSETUP_32BPP_PAL_SWAP_RGB ) ?
			( ( b << 16 ) | ( g << 8 ) | r ) :
			( ( r << 16 ) | ( g << 8 ) | b );

		if ( ColorSetupFlags & COLORSETUP_32BPP_PAL_ALPHA_IN_LSB )
			w <<= 8;

		if ( w == 0 )
			++w;
		*xlattab++ = ( w | alpha );
	}
}


// calculate 1K translation table for color indexes to RGBA colors ------------
//
PRIVATE
void SetupColorIndexToRGBA( char *palette )
{
	ASSERT( palette != NULL );

	// destination table
	colrgba_s *colrgba = IndexToRGBATab;

	// first entry is always black
	colrgba->R = 0;
	colrgba->G = 0;
	colrgba->B = 0;
	colrgba->A = 255;
	colrgba++;

	// calculate 256 entries
	for ( int ppos = 3; ppos < VGA_PAL_NUMCOLORS * 3; ppos+=3 ) {

		colrgba->R = colbits_6_to_8[ palette[ ppos + 0 ] & 0x3f ];
		colrgba->G = colbits_6_to_8[ palette[ ppos + 1 ] & 0x3f ];
		colrgba->B = colbits_6_to_8[ palette[ ppos + 2 ] & 0x3f ];
		colrgba->A = 255;
		colrgba++;
	}
}


// setup color index translation tables ---------------------------------------
//
void SetupTranslationPalette( char *palette )
{
	ASSERT( palette != NULL );

	SetupColorIndexToVisual( palette );
	SetupColorIndexToRGBA( palette );
}


// translate color index to current visual (color in framebuffer format) ------
//
visual_t ColorIndexToVisual( int colindx )
{
//	ASSERT( ( colindx >= 0 ) && ( colindx <= VGA_PAL_NUMCOLORS ) );

	//NOTE:
	// this function is available without checks
	// as COLINDX_TO_VISUAL() macro in COLOR.H

	return IndexToVisualTab[ colindx & 0xff ];
}


// translate color index to RGBA color ----------------------------------------
//
void ColorIndexToRGBA( colrgba_s *colrgba, int colindx )
{
	ASSERT( colrgba != NULL );
//	ASSERT( ( colindx >= 0 ) && ( colindx <= VGA_PAL_NUMCOLORS ) );

	//NOTE:
	// this function is available without checks
	// as COLINDX_TO_RGBA() macro in COLOR.H

	*colrgba = IndexToRGBATab[ colindx & 0xff ];
}


// translate visual (color in framebuffer format) into RGBA color -------------
//
void VisualToRGBA( colrgba_s *colrgba, visual_t visual )
{
	ASSERT( colrgba != NULL );

	// visual has [ABGR] layout
	colrgba->R = ( visual       ) & 0xff; // & 0xff redundant
	colrgba->G = ( visual >> 8  ) & 0xff; // & 0xff redundant
	colrgba->B = ( visual >> 16 ) & 0xff; // & 0xff redundant
	colrgba->A = 255;
}


// translate RGBA color to visual (color in framebuffer format) ---------------
//
visual_t RGBAToVisual( colrgba_s *colrgba )
{
	ASSERT( colrgba != NULL );

	visual_t visual = 0;

	// visual has [ABGR] layout
	visual  = (dword)colrgba->R;
	visual |= (dword)colrgba->G << 8;
	visual |= (dword)colrgba->B << 16;
	visual |= (dword)255        << 24;

	return visual;
}


// texture geometry table for font textures -----------------------------------
//
struct fonttex_info_s {

	int		width;
	int		height;
	int		width_log2;
	int		height_log2;

	dword	geometry;
	dword	lod;
};

static fonttex_info_s fonttex_info[] = {

	// fontid 0
	{ 256, 256, 8, 8, (TEXGEO_SCALE_1|TEXGEO_ASPECT_1x1), TEXLOD_256 },

	// fontid 1: 4x9
	{ 128, 128, 7, 7, (TEXGEO_SCALE_1|TEXGEO_ASPECT_1x1), TEXLOD_128 },

	// fontid 2: 8x8
	{ 256, 256, 8, 8, (TEXGEO_SCALE_1|TEXGEO_ASPECT_1x1), TEXLOD_256 },
//	{ 256, 128, 8, 7, (TEXGEO_SCALE_1|TEXGEO_ASPECT_2x1), TEXLOD_256 },

	// fontid 3
	{ 256, 256, 8, 8, (TEXGEO_SCALE_1|TEXGEO_ASPECT_1x1), TEXLOD_256 },
};


// convert font data to texture of 8-bit texels -------------------------------
//
PRIVATE
void CreateFontTextureData8( int fontid, int numvalid, char *texdata, word *uvtab )
{
	ASSERT( ( fontid >= 0 ) && ( fontid < NumLoadedCharsets ) );
	ASSERT( texdata != NULL );
	ASSERT( uvtab != NULL );

	dword*	fgeom = CharsetInfo[ fontid ].geompointer;
	char*	fdata = CharsetInfo[ fontid ].loadeddata;
	int		fontw = CharsetInfo[ fontid ].width;
	int		fonth = CharsetInfo[ fontid ].height;
	int		fsrcw = CharsetInfo[ fontid ].srcwidth;
	size_t	ftsiz = CharsetInfo[ fontid ].datasize;

	int texwidth  = fonttex_info[ fontid ].width;
	int texheight = fonttex_info[ fontid ].height;

	size_t datasize = texwidth * texheight * 1;

	// convert data
	dword  destofs = 0;
	dword  curu    = 0;
	dword  curv    = 0;
	for ( int cidx = 0; cidx < numvalid; cidx++, uvtab+=2 ) {

		byte *src = (byte *) ( fdata   + fgeom[ cidx ] );
		byte *dst = (byte *) ( texdata + destofs );

		// convert to alpha/intensity char
		for ( int row = fonth; row > 0; row-- ) {
			for ( int col = fontw; col > 0; col-- ) {
				ASSERT( src < (byte*)( fdata + ftsiz ) );
				ASSERT( dst < (byte*)( texdata + datasize ) );
				byte v = *src++;
				*dst++ = ( v != 0x00 ) ? 0xff : 0x00;
			}
			src += fsrcw    - fontw;
			dst += texwidth - fontw;
		}

		uvtab[ 0 ] = (word) curu;
		uvtab[ 1 ] = (word) curv;

		// advance in texture
		curu    += fontw * 2;
		destofs += fontw * 2;
		if ( curu > (dword)(texwidth - fontw * 2) ) {
			curu    = 0;
			curv   += fonth * 2;
			destofs = curv * texwidth;
		}

		ASSERT( curv <= (dword)(texheight - fonth * 2) );
		ASSERT( destofs < datasize - fontw * fonth * 2 );
	}
}


// convert font data to texture of 32-bit texels ------------------------------
//
PRIVATE
void CreateFontTextureData32( int fontid, int numvalid, char *texdata, word *uvtab )
{
	ASSERT( ( fontid >= 0 ) && ( fontid < NumLoadedCharsets ) );
	ASSERT( texdata != NULL );
	ASSERT( uvtab != NULL );

	dword*	fgeom = CharsetInfo[ fontid ].geompointer;
	char*	fdata = CharsetInfo[ fontid ].loadeddata;
	int		fontw = CharsetInfo[ fontid ].width;
	int		fonth = CharsetInfo[ fontid ].height;
	int		fsrcw = CharsetInfo[ fontid ].srcwidth;
	size_t	ftsiz = CharsetInfo[ fontid ].datasize;

	int texwidth  = fonttex_info[ fontid ].width;
	int texheight = fonttex_info[ fontid ].height;

	size_t datasize = texwidth * texheight * 4;

	// convert data
	dword  destofs = 0;
	dword  curu    = 0;
	dword  curv    = 0;
	for ( int cidx = 0; cidx < numvalid; cidx++, uvtab+=2 ) {

		byte  *src = (byte  *) ( fdata   + fgeom[ cidx ] );
		dword *dst = (dword *) ( texdata + destofs );

		// convert to rgba char
		for ( int row = fonth; row > 0; row-- ) {
			for ( int col = fontw; col > 0; col-- ) {
				ASSERT( src < (byte*)fdata + ftsiz );
				ASSERT( dst < (dword*)texdata + datasize );
				byte v = *src++;
				*dst++ = ( v != 0x00 ) ? 0xffffffff : 0x00000000;
			}
			src += fsrcw    - fontw;
			dst += texwidth - fontw;
		}

		//NOTE:
		// we need not take any swapping into account here, since
		// we use only 0xffffffff and 0x00000000 as texel values.

		uvtab[ 0 ] = (word) curu;
		uvtab[ 1 ] = (word) curv;

		// advance in texture
		curu    += fontw * 2;
		destofs += fontw * 2 * 4;
		if ( curu > (dword)(texwidth - fontw * 2) ) {
			curu    = 0;
			curv   += fonth * 2;
			destofs = curv * texwidth * 4;
		}

		ASSERT( curv <= (dword)(texheight - fonth * 2) );
		ASSERT( destofs < datasize - fontw * fonth * 2 * 4 );
	}
}


// convert font data to texture -----------------------------------------------
//
PRIVATE
int CreateFontTexture( int fontid )
{
	ASSERT( ( fontid >= 0 ) && ( fontid < NumLoadedCharsets ) );

	// use texture only for 4x9 and 8x8 font
	if ( /*( fontid != 1 ) &&*/ ( fontid != 2 ) )
		return FALSE;

	ASSERT( CharsetInfo[ fontid ].fonttexture == NULL );
	dword *geometry	= CharsetInfo[ fontid ].geompointer;

	// determine number of valid chars
	char startchar = ' ';
	if ( ( geometry != Char16x16Geom ) && ( geometry != CharGoldGeom  ) &&
		 ( geometry != Char04x09Geom ) && ( geometry != Char08x08Geom ) ) {
		startchar = (char) SWAP_32( *( geometry - 1 ) );
	}
	int numvalid = 128 - startchar;

	//CAVEAT:
	// this assumes that after the startchar
	// all chars will be valid!!

	// determine format and texelsize
	dword  texformat = TEXFMT_STANDARD;
	size_t texelsize = 1;
	if ( ColorSetupFlags & COLORSETUP_FONT_TO_TEXTURE_ALPHA ) {
		texformat = TEXFMT_ALPHA_8;
		texelsize = 1;
	} else if ( ColorSetupFlags & COLORSETUP_FONT_TO_TEXTURE_INTENSITY ) {
		texformat = TEXFMT_INTENSITY_8;
		texelsize = 1;
	} else if ( ColorSetupFlags & COLORSETUP_FONT_TO_TEXTURE_LUMINANCE ) {
		texformat = TEXFMT_LUMINANCE_8;
		texelsize = 1;
	} else if ( ColorSetupFlags & COLORSETUP_FONT_TO_TEXTURE_RGBA ) {
		texformat = TEXFMT_RGBA_8888;
		texelsize = 4;
	} else {
		ASSERT( 0 );
	}

	int texwidth  = fonttex_info[ fontid ].width;
	int texheight = fonttex_info[ fontid ].height;

	size_t headsize = sizeof( TextureMap ) + 8;
	size_t geomsize = 128 * sizeof( word ) * 2;
	size_t datasize = texwidth * texheight * texelsize;

	// alloc texture header and data
	char *dst = (char *) ALLOCMEM( headsize + geomsize + datasize );
	if ( dst == NULL ) {
		OUTOFMEM( no_font_mem );
	}

	// init header
	memset( dst, 0, headsize + geomsize + datasize );
	strcpy( dst + sizeof( TextureMap ), "TEXFONT" );

	// set pointers
	TextureMap *texmap = (TextureMap *) dst;
	CharsetInfo[ fontid ].charsetpointer = dst;
	CharsetInfo[ fontid ].fonttexture    = texmap;

	char *texdata = (char*)texmap + headsize + geomsize;
	word *uvtab   = (word *)( (char*)texmap + headsize ) + startchar * 2;

	// init texture header
	texmap->Flags		= TEXFLG_EXT_GEOMETRY | TEXFLG_LODRANGE_VALID;
	texmap->Geometry	= fonttex_info[ fontid ].geometry;
	texmap->Width		= fonttex_info[ fontid ].width_log2;
	texmap->Height		= fonttex_info[ fontid ].height_log2;
	texmap->LOD_small	= fonttex_info[ fontid ].lod;
	texmap->LOD_large	= fonttex_info[ fontid ].lod;
	texmap->BitMap		= texdata;
	texmap->TexPalette	= NULL;
	texmap->TexMapName	= "auto_font";
	texmap->TexelFormat	= texformat;

	// use conversion function corresponding to texel size
	if ( texelsize == 1 ) {
		CreateFontTextureData8( fontid, numvalid, texdata, uvtab );
	} else {
		CreateFontTextureData32( fontid, numvalid, texdata, uvtab );
	}

	return TRUE;
}


// special setup for bitmaps that may be used as alpha textures ---------------
//
int SetupBitmapAlphaTextures( int bmid )
{
	ASSERT( ( bmid >= 0 ) && ( bmid < NumLoadedBitmaps ) );

	//NOTE:
	// this is actually just a hack to accommodate OpenGL
	// drivers where GL_ALPHA textures are broken (Rage128).

	// special case for base lensflare bitmap
	if ( bmid != BM_LENSFLARE1 )
		return FALSE;

	int w = BitmapInfo[ bmid ].width;
	int h = BitmapInfo[ bmid ].height;
	char *src = BitmapInfo[ bmid ].loadeddata;

	colrgba_s *dst = (colrgba_s *) ALLOCMEM( w * h * 4 );
	if ( dst == NULL )
		OUTOFMEM( no_bitmap_mem );
	BitmapInfo[ bmid ].bitmappointer = (char *) dst;

	for ( int pix = w * h; pix > 0; pix--, dst++ ) {
		byte p = *src++;
		dst->R = 255;
		dst->G = 255;
		dst->B = 255;
		dst->A = p;
	}

	return TRUE;
}


// convert single bitmap to destination color format/model --------------------
//
void SetupSingleBitmapColors( int bmid )
{
	ASSERT( ( bmid >= 0 ) && ( bmid < NumLoadedBitmaps ) );

	char *src = BitmapInfo[ bmid ].loadeddata;

	// free previous conversion data
	if ( BitmapInfo[ bmid ].bitmappointer != src ) {
		ASSERT( BitmapInfo[ bmid ].bitmappointer != NULL );
		FREEMEM( BitmapInfo[ bmid ].bitmappointer );
		BitmapInfo[ bmid ].bitmappointer = src;
	}

	if ( bmid < BM_CONTROLFILE_NUMBER ) {

		// handle case when alpha textures must be avoided
		if ( ColorSetupFlags & COLORSETUP_ALPHA_8_TO_RGBA_8888 ) {
			if ( SetupBitmapAlphaTextures( bmid ) )
				return;
		}

		// determine whether conversion should be done for this bitmap
		if ( ColorSetupFlags & COLORSETUP_32BPP_CONVERT_SCALE_BITMAPS ) {
			if ( ( ( bmid > 24 ) && ( bmid < 29 ) ) || ( bmid > 52 ) )
				return;
		} else if ( bmid != 24 ) {
			if ( ( ( bmid > 11 ) && ( bmid < 29 ) ) || ( bmid > 42 ) )
				return;
		}
	}

	int w = BitmapInfo[ bmid ].width;
	int h = BitmapInfo[ bmid ].height;

	dword *dst = (dword *) ALLOCMEM( w * h * 4 );
	if ( dst == NULL )
		OUTOFMEM( no_bitmap_mem );
	BitmapInfo[ bmid ].bitmappointer = (char *) dst;

	if ( ColorSetupFlags & COLORSETUP_32BPP_BITMAP_YINVERSE ) {

		// flip bitmap vertically while converting
		if ( ColorSetupFlags & COLORSETUP_SWAP_BITMAPS_32BPP ) {
			src += w * h - w;
			for ( int row = h; row > 0; row-- ) {
				for ( int col = w; col > 0; col-- ) {
					byte  v = *src++;
					dword p = COLINDX_TO_VISUAL( v );
					*dst++ = FORCESWAP_32( p );
				}
				src -= w * 2;
			}
		} else {
			src += w * h - w;
			for ( int row = h; row > 0; row-- ) {
				for ( int col = w; col > 0; col-- ) {
					byte v = *src++;
					*dst++ = COLINDX_TO_VISUAL( v );
				}
				src -= w * 2;
			}
		}

	} else {

		if ( ColorSetupFlags & COLORSETUP_SWAP_BITMAPS_32BPP ) {
			for ( int pix = w * h; pix > 0; pix-- ) {
				byte  v = *src++;
				dword p = COLINDX_TO_VISUAL( v );
				*dst++ = FORCESWAP_32( p );
			}
		} else {
			for ( int pix = w * h; pix > 0; pix-- ) {
				byte v = *src++;
				*dst++ = COLINDX_TO_VISUAL( v );
			}
		}
	}
}


// convert single font to destination color format/model ----------------------
//
void SetupSingleCharsetColors( int ftid )
{
	ASSERT( ( ftid >= 0 ) && ( ftid < NumLoadedCharsets ) );

	if ( ftid != 4 ) {

		char *src = CharsetInfo[ ftid ].loadeddata;
		int blocksize = CharsetInfo[ ftid ].datasize;

		// reset font texture pointer
		if ( CharsetInfo[ ftid ].fonttexture != NULL ) {

			ASSERT( (void*)CharsetInfo[ ftid ].charsetpointer ==
					(void*)CharsetInfo[ ftid ].fonttexture );

			// the corresponding mem will be released as
			// standard conversion data
			CharsetInfo[ ftid ].fonttexture = NULL;
		}

		// free previous conversion data
		if ( CharsetInfo[ ftid ].charsetpointer != src ) {
			ASSERT( CharsetInfo[ ftid ].charsetpointer != NULL );
			FREEMEM( CharsetInfo[ ftid ].charsetpointer );
			CharsetInfo[ ftid ].charsetpointer = NULL;
		}

		// check for texture fonts
		if ( ColorSetupFlags & COLORSETUP_FONT_DO_CONVERSION ) {

			// convert font to texture if possible
			if ( CreateFontTexture( ftid ) ) {

				// skip standard conversion code only if
				// texture conversion actually succeeded
				return;
			}
		}

		// color depth conversion

		dword *dst = (dword *) ALLOCMEM( blocksize * 4 );
		if ( dst == NULL )
			OUTOFMEM( no_font_mem );
		CharsetInfo[ ftid ].charsetpointer = (char *) dst;

		if ( ( ColorSetupFlags & COLORSETUP_32BPP_FONT_YINVERSE ) &&
			 ( ftid != 5 ) && ( ftid != 6 ) ) {

			int w = CharsetInfo[ ftid ].srcwidth;
			int h = blocksize / w;
			int rh = CharsetInfo[ ftid ].height;
			int frows = h / rh;

			// flip font rows vertically while converting
			if ( ColorSetupFlags & COLORSETUP_SWAP_FONTS_32BPP ) {
				for ( int frow = frows; frow > 0; frow-- ) {
					src += w * rh - w;
					for ( int row = rh; row > 0; row-- ) {
						for ( int col = w; col > 0; col-- ) {
							byte  v = *src++;
							dword p = COLINDX_TO_VISUAL( v );
							*dst++ = FORCESWAP_32( p );
						}
						src -= w * 2;
					}
					src += w * rh + w;
				}
			} else {
				for ( int frow = frows; frow > 0; frow-- ) {
					src += w * rh - w;
					for ( int row = rh; row > 0; row-- ) {
						for ( int col = w; col > 0; col-- ) {
							byte v = *src++;
							*dst++ = COLINDX_TO_VISUAL( v );
						}
						src -= w * 2;
					}
					src += w * rh + w;
				}
			}

		} else {

			if ( ColorSetupFlags & COLORSETUP_SWAP_FONTS_32BPP ) {
				for ( int pix = blocksize; pix > 0; pix-- ) {
					byte  v = *src++;
					dword p = COLINDX_TO_VISUAL( v );
					*dst++ = FORCESWAP_32( p );
				}
			} else {
				for ( int pix = blocksize; pix > 0; pix-- ) {
					byte v = *src++;
					*dst++ = COLINDX_TO_VISUAL( v );
				}
			}
		}
	}
}


// create bitmaps for 16bit and 32bit color depths, respectively --------------
//
void SetupBitmapColors()
{
	// setup bitmaps
	for ( int bmid = 0; bmid < NumLoadedBitmaps; bmid++ ) {
		SetupSingleBitmapColors( bmid );
	}

	// setup charsets
	for ( int ftid = 0; ftid < NumLoadedCharsets; ftid++ ) {
		SetupSingleCharsetColors( ftid );
	}
}


// init color maps (alloc mem, calc mappings, etc.) ---------------------------
//
void InitColorMaps()
{
	if ( !color_init_done ) {

		// calc mapping tables
		MSGPUT( "Calculating colors..." );
		SetupTranslationPalette( PaletteMem + PAL_GAME );
		MSGOUT( "done." );

		color_init_done = TRUE;
	}
}


// kill color maps (free resources) -------------------------------------------
//
void KillColorMaps()
{
	if ( color_init_done ) {
		color_init_done = FALSE;
	}
}