]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - palette.c
#if 0'd out a few unused functions
[xonotic/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3
4 unsigned int palette_complete[256];
5 unsigned int palette_nofullbrights[256];
6 unsigned int palette_onlyfullbrights[256];
7 unsigned int palette_nocolormapnofullbrights[256];
8 unsigned int palette_nocolormap[256];
9 unsigned int palette_pantsaswhite[256];
10 unsigned int palette_shirtaswhite[256];
11 unsigned int palette_alpha[256];
12 unsigned int palette_font[256];
13
14 qbyte host_basepal[768];
15
16 void Palette_Setup8to24(void)
17 {
18         int i;
19         int fullbright_start, fullbright_end;
20         int pants_start, pants_end;
21         int shirt_start, shirt_end;
22         int reversed_start, reversed_end;
23         qbyte *in, *out;
24         qbyte *colormap;
25
26         in = host_basepal;
27         out = (qbyte *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
28         for (i = 0;i < 255;i++)
29         {
30                 *out++ = *in++;
31                 *out++ = *in++;
32                 *out++ = *in++;
33                 *out++ = 255;
34         }
35         palette_complete[255] = 0; // completely transparent black
36
37         // FIXME: fullbright_start should be read from colormap.lmp
38         colormap = FS_LoadFile("gfx/colormap.lmp", true);
39         if (colormap && fs_filesize >= 16385)
40                 fullbright_start = 256 - colormap[16384];
41         else
42                 fullbright_start = 256;
43         if (colormap)
44                 Mem_Free(colormap);
45         fullbright_end = 256;
46         pants_start = 96;
47         pants_end = 112;
48         shirt_start = 16;
49         shirt_end = 32;
50         reversed_start = 128;
51         reversed_end = 224;
52
53         for (i = 0;i < fullbright_start;i++)
54                 palette_nofullbrights[i] = palette_complete[i];
55         for (i = fullbright_start;i < 255;i++)
56                 palette_nofullbrights[i] = palette_complete[0];
57         palette_nofullbrights[255] = 0;
58
59         for (i = 0;i < 256;i++)
60                 palette_onlyfullbrights[i] = palette_complete[0];
61         for (i = fullbright_start;i < fullbright_end;i++)
62                 palette_onlyfullbrights[i] = palette_complete[i];
63         palette_onlyfullbrights[255] = 0;
64
65         for (i = 0;i < 256;i++)
66                 palette_nocolormapnofullbrights[i] = palette_complete[i];
67         for (i = pants_start;i < pants_end;i++)
68                 palette_nocolormapnofullbrights[i] = palette_complete[0];
69         for (i = shirt_start;i < shirt_end;i++)
70                 palette_nocolormapnofullbrights[i] = palette_complete[0];
71         for (i = fullbright_start;i < fullbright_end;i++)
72                 palette_nocolormapnofullbrights[i] = palette_complete[0];
73         palette_nocolormapnofullbrights[255] = 0;
74
75         for (i = 0;i < 256;i++)
76                 palette_nocolormap[i] = palette_complete[i];
77         for (i = pants_start;i < pants_end;i++)
78                 palette_nocolormap[i] = palette_complete[0];
79         for (i = shirt_start;i < shirt_end;i++)
80                 palette_nocolormap[i] = palette_complete[0];
81         palette_nocolormap[255] = 0;
82
83         for (i = 0;i < 256;i++)
84                 palette_pantsaswhite[i] = palette_complete[0];
85         for (i = pants_start;i < pants_end;i++)
86         {
87                 if (i >= reversed_start && i < reversed_end)
88                         palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
89                 else
90                         palette_pantsaswhite[i] = palette_complete[i - pants_start];
91         }
92
93         for (i = 0;i < 256;i++)
94                 palette_shirtaswhite[i] = palette_complete[0];
95         for (i = shirt_start;i < shirt_end;i++)
96         {
97                 if (i >= reversed_start && i < reversed_end)
98                         palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
99                 else
100                         palette_shirtaswhite[i] = palette_complete[i - shirt_start];
101         }
102
103         for (i = 0;i < 255;i++)
104                 palette_alpha[i] = 0xFFFFFFFF;
105         palette_alpha[255] = 0;
106
107         palette_font[0] = 0;
108         for (i = 1;i < 255;i++)
109                 palette_font[i] = palette_complete[i];
110         palette_font[255] = 0;
111 }
112
113 #if 0
114 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
115 {
116         int i, adjusted;
117         double invgamma, d;
118
119         gamma = bound(0.1, gamma, 5.0);
120         if (gamma == 1) // LordHavoc: dodge the math
121         {
122                 for (i = 0;i < 256;i++)
123                 {
124                         adjusted = (int) (i * prescale * scale + base * 255.0);
125                         out[i] = bound(0, adjusted, 255);
126                 }
127         }
128         else
129         {
130                 invgamma = 1.0 / gamma;
131                 prescale /= 255.0f;
132                 for (i = 0;i < 256;i++)
133                 {
134                         d = pow((double) i * prescale, invgamma) * scale + base;
135                         adjusted = (int) (255.0 * d);
136                         out[i] = bound(0, adjusted, 255);
137                 }
138         }
139 }
140 #endif
141
142 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
143 {
144         int i, adjusted;
145         double invgamma, d;
146
147         gamma = bound(0.1, gamma, 5.0);
148         if (gamma == 1) // LordHavoc: dodge the math
149         {
150                 for (i = 0;i < 256;i++)
151                 {
152                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
153                         out[i] = bound(0, adjusted, 65535);
154                 }
155         }
156         else
157         {
158                 invgamma = 1.0 / gamma;
159                 prescale /= 255.0f;
160                 for (i = 0;i < 256;i++)
161                 {
162                         d = pow((double) i * prescale, invgamma) * scale + base;
163                         adjusted = (int) (65535.0 * d);
164                         out[i] = bound(0, adjusted, 65535);
165                 }
166         }
167 }
168
169 void Palette_Init(void)
170 {
171         int i;
172         float gamma, scale, base;
173         qbyte *pal;
174         qbyte temp[256];
175         pal = (qbyte *)FS_LoadFile ("gfx/palette.lmp", false);
176         if (!pal)
177                 Sys_Error ("Couldn't load gfx/palette.lmp");
178         memcpy(host_basepal, pal, 765);
179         Mem_Free(pal);
180         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
181
182         gamma = 1;
183         scale = 1;
184         base = 0;
185         i = COM_CheckParm("-texgamma");
186         if (i)
187                 gamma = atof(com_argv[i + 1]);
188         i = COM_CheckParm("-texcontrast");
189         if (i)
190                 scale = atof(com_argv[i + 1]);
191         i = COM_CheckParm("-texbrightness");
192         if (i)
193                 base = atof(com_argv[i + 1]);
194         gamma = bound(0.01, gamma, 10.0);
195         scale = bound(0.01, scale, 10.0);
196         base = bound(0, base, 0.95);
197
198         BuildGammaTable8(1.0f, gamma, scale, base, temp);
199         for (i = 3;i < 765;i++)
200                 host_basepal[i] = temp[host_basepal[i]];
201
202         Palette_Setup8to24();
203 }
204