Fixed many issues with q2bsp support, it now works properly.
[xonotic/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3 #include "image.h"
4
5 cvar_t r_colormap_palette = {0, "r_colormap_palette", "gfx/colormap_palette.lmp", "name of a palette lmp file to override the shirt/pants colors of player models. It consists of 16 shirt colors, 16 scoreboard shirt colors, 16 pants colors and 16 scoreboard pants colors"};
6
7 unsigned char palette_rgb[256][3];
8 unsigned char palette_rgb_pantscolormap[16][3];
9 unsigned char palette_rgb_shirtcolormap[16][3];
10 unsigned char palette_rgb_pantsscoreboard[16][3];
11 unsigned char palette_rgb_shirtscoreboard[16][3];
12
13 unsigned int palette_bgra_complete[256];
14 unsigned int palette_bgra_font[256];
15 unsigned int palette_bgra_alpha[256];
16 unsigned int palette_bgra_nocolormap[256];
17 unsigned int palette_bgra_nocolormapnofullbrights[256];
18 unsigned int palette_bgra_nofullbrights[256];
19 unsigned int palette_bgra_onlyfullbrights[256];
20 unsigned int palette_bgra_pantsaswhite[256];
21 unsigned int palette_bgra_shirtaswhite[256];
22 unsigned int palette_bgra_transparent[256];
23 unsigned int palette_bgra_embeddedpic[256];
24 unsigned char palette_featureflags[256];
25
26 unsigned int q2palette_bgra_complete[256];
27
28 // John Carmack said the quake palette.lmp can be considered public domain because it is not an important asset to id, so I include it here as a fallback if no external palette file is found.
29 unsigned char host_quakepal[768] =
30 {
31 // marked: colormap colors: cb = (colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
32 // 0x0*
33         0,0,0,        15,15,15,     31,31,31,     47,47,47,     63,63,63,     75,75,75,     91,91,91,     107,107,107,
34         123,123,123,  139,139,139,  155,155,155,  171,171,171,  187,187,187,  203,203,203,  219,219,219,  235,235,235,
35 // 0x1*                                                   0 ^
36         15,11,7,      23,15,11,     31,23,11,     39,27,15,     47,35,19,     55,43,23,     63,47,23,     75,55,27,
37         83,59,27,     91,67,31,     99,75,31,     107,83,31,    115,87,31,    123,95,35,    131,103,35,   143,111,35,
38 // 0x2*                                                   1 ^
39         11,11,15,     19,19,27,     27,27,39,     39,39,51,     47,47,63,     55,55,75,     63,63,87,     71,71,103,
40         79,79,115,    91,91,127,    99,99,139,    107,107,151,  115,115,163,  123,123,175,  131,131,187,  139,139,203,
41 // 0x3*                                                   2 ^
42         0,0,0,        7,7,0,        11,11,0,      19,19,0,      27,27,0,      35,35,0,      43,43,7,      47,47,7,
43         55,55,7,      63,63,7,      71,71,7,      75,75,11,     83,83,11,     91,91,11,     99,99,11,     107,107,15,
44 // 0x4*                                                   3 ^
45         7,0,0,        15,0,0,       23,0,0,       31,0,0,       39,0,0,       47,0,0,       55,0,0,       63,0,0,
46         71,0,0,       79,0,0,       87,0,0,       95,0,0,       103,0,0,      111,0,0,      119,0,0,      127,0,0,
47 // 0x5*                                                   4 ^
48         19,19,0,      27,27,0,      35,35,0,      47,43,0,      55,47,0,      67,55,0,      75,59,7,      87,67,7,
49         95,71,7,      107,75,11,    119,83,15,    131,87,19,    139,91,19,    151,95,27,    163,99,31,    175,103,35,
50 // 0x6*                                                   5 ^
51         35,19,7,      47,23,11,     59,31,15,     75,35,19,     87,43,23,     99,47,31,     115,55,35,    127,59,43,
52         143,67,51,    159,79,51,    175,99,47,    191,119,47,   207,143,43,   223,171,39,   239,203,31,   255,243,27,
53 // 0x7*                                                   6 ^
54         11,7,0,       27,19,0,      43,35,15,     55,43,19,     71,51,27,     83,55,35,     99,63,43,     111,71,51,
55         127,83,63,    139,95,71,    155,107,83,   167,123,95,   183,135,107,  195,147,123,  211,163,139,  227,179,151,
56 // 0x8*                                                   7 ^        v 8
57         171,139,163,  159,127,151,  147,115,135,  139,103,123,  127,91,111,   119,83,99,    107,75,87,    95,63,75,
58         87,55,67,     75,47,55,     67,39,47,     55,31,35,     43,23,27,     35,19,19,     23,11,11,     15,7,7,
59 // 0x9*                                                   9 v
60         187,115,159,  175,107,143,  163,95,131,   151,87,119,   139,79,107,   127,75,95,    115,67,83,    107,59,75,
61         95,51,63,     83,43,55,     71,35,43,     59,31,35,     47,23,27,     35,19,19,     23,11,11,     15,7,7,
62 // 0xA*                                                  10 v
63         219,195,187,  203,179,167,  191,163,155,  175,151,139,  163,135,123,  151,123,111,  135,111,95,   123,99,83,
64         107,87,71,    95,75,59,     83,63,51,     67,51,39,     55,43,31,     39,31,23,     27,19,15,     15,11,7,
65 // 0xB*                                                  11 v
66         111,131,123,  103,123,111,  95,115,103,   87,107,95,    79,99,87,     71,91,79,     63,83,71,     55,75,63,
67         47,67,55,     43,59,47,     35,51,39,     31,43,31,     23,35,23,     15,27,19,     11,19,11,     7,11,7,
68 // 0xC*                                                  12 v
69         255,243,27,   239,223,23,   219,203,19,   203,183,15,   187,167,15,   171,151,11,   155,131,7,    139,115,7,
70         123,99,7,     107,83,0,     91,71,0,      75,55,0,      59,43,0,      43,31,0,      27,15,0,      11,7,0,
71 // 0xD*                                                  13 v
72         0,0,255,      11,11,239,    19,19,223,    27,27,207,    35,35,191,    43,43,175,    47,47,159,    47,47,143,
73         47,47,127,    47,47,111,    47,47,95,     43,43,79,     35,35,63,     27,27,47,     19,19,31,     11,11,15,
74 // 0xE*
75         43,0,0,       59,0,0,       75,7,0,       95,7,0,       111,15,0,     127,23,7,     147,31,7,     163,39,11,
76         183,51,15,    195,75,27,    207,99,43,    219,127,59,   227,151,79,   231,171,95,   239,191,119,  247,211,139,
77 // 0xF*                                                  14 ^
78         167,123,59,   183,155,55,   199,195,55,   231,227,87,   127,191,255,  171,231,255,  215,255,255,  103,0,0,
79         139,0,0,      179,0,0,      215,0,0,      255,0,0,      255,243,147,  255,247,199,  255,255,255,  159,91,83
80 }; //                                                    15 ^
81
82 static void Palette_SetupSpecialPalettes(void)
83 {
84         int i;
85         int fullbright_start, fullbright_end;
86         int pants_start, pants_end;
87         int shirt_start, shirt_end;
88         int reversed_start, reversed_end;
89         int transparentcolor;
90         unsigned char *colormap;
91         fs_offset_t filesize;
92         union
93         {
94                 int i;
95                 unsigned char b[4];
96         }
97         u;
98
99         colormap = FS_LoadFile("gfx/colormap.lmp", tempmempool, true, &filesize);
100         if (colormap && filesize >= 16385)
101                 fullbright_start = 256 - colormap[16384];
102         else
103                 fullbright_start = 256;
104         if (colormap)
105                 Mem_Free(colormap);
106         fullbright_end = 256;
107         pants_start = 96;
108         pants_end = 112;
109         shirt_start = 16;
110         shirt_end = 32;
111         reversed_start = 128;
112         reversed_end = 224;
113         transparentcolor = 255;
114
115         for (i = 0;i < 256;i++)
116                 palette_featureflags[i] = PALETTEFEATURE_STANDARD;
117         for (i = reversed_start;i < reversed_end;i++)
118                 palette_featureflags[i] = PALETTEFEATURE_REVERSED;
119         for (i = pants_start;i < pants_end;i++)
120                 palette_featureflags[i] = PALETTEFEATURE_PANTS;
121         for (i = shirt_start;i < shirt_end;i++)
122                 palette_featureflags[i] = PALETTEFEATURE_SHIRT;
123         for (i = fullbright_start;i < fullbright_end;i++)
124                 palette_featureflags[i] = PALETTEFEATURE_GLOW;
125         palette_featureflags[0] = PALETTEFEATURE_ZERO;
126         palette_featureflags[transparentcolor] = PALETTEFEATURE_TRANSPARENT;
127
128         for (i = 0;i < 256;i++)
129                 palette_bgra_transparent[i] = palette_bgra_complete[i];
130         palette_bgra_transparent[transparentcolor] = 0;
131
132         for (i = 0;i < fullbright_start;i++)
133                 palette_bgra_nofullbrights[i] = palette_bgra_complete[i];
134         for (i = fullbright_start;i < fullbright_end;i++)
135                 palette_bgra_nofullbrights[i] = palette_bgra_complete[0];
136
137         for (i = 0;i < 256;i++)
138                 palette_bgra_onlyfullbrights[i] = 0;
139         for (i = fullbright_start;i < fullbright_end;i++)
140                 palette_bgra_onlyfullbrights[i] = palette_bgra_complete[i];
141
142         for (i = 0;i < 256;i++)
143                 palette_bgra_nocolormapnofullbrights[i] = palette_bgra_complete[i];
144         for (i = pants_start;i < pants_end;i++)
145                 palette_bgra_nocolormapnofullbrights[i] = 0;
146         for (i = shirt_start;i < shirt_end;i++)
147                 palette_bgra_nocolormapnofullbrights[i] = 0;
148         for (i = fullbright_start;i < fullbright_end;i++)
149                 palette_bgra_nocolormapnofullbrights[i] = 0;
150
151         for (i = 0;i < 256;i++)
152                 palette_bgra_nocolormap[i] = palette_bgra_complete[i];
153         for (i = pants_start;i < pants_end;i++)
154                 palette_bgra_nocolormap[i] = 0;
155         for (i = shirt_start;i < shirt_end;i++)
156                 palette_bgra_nocolormap[i] = 0;
157
158         for (i = 0;i < 256;i++)
159                 palette_bgra_pantsaswhite[i] = 0;
160         for (i = pants_start;i < pants_end;i++)
161         {
162                 if (i >= reversed_start && i < reversed_end)
163                         palette_bgra_pantsaswhite[i] = palette_bgra_complete[15 - (i - pants_start)];
164                 else
165                         palette_bgra_pantsaswhite[i] = palette_bgra_complete[i - pants_start];
166         }
167
168         for (i = 0;i < 256;i++)
169                 palette_bgra_shirtaswhite[i] = 0;
170         for (i = shirt_start;i < shirt_end;i++)
171         {
172                 if (i >= reversed_start && i < reversed_end)
173                         palette_bgra_shirtaswhite[i] = palette_bgra_complete[15 - (i - shirt_start)];
174                 else
175                         palette_bgra_shirtaswhite[i] = palette_bgra_complete[i - shirt_start];
176         }
177
178         for (i = 0;i < 256;i++)
179                 palette_bgra_alpha[i] = 0xFFFFFFFF;
180         u.i = 0xFFFFFFFF;
181         u.b[3] = 0;
182         palette_bgra_alpha[transparentcolor] = u.i;
183
184         for (i = 0;i < 256;i++)
185                 palette_bgra_font[i] = palette_bgra_complete[i];
186         palette_bgra_font[0] = 0;
187 }
188
189 static void Palette_LoadQ2Colormap(void)
190 {
191         fs_offset_t filesize;
192         unsigned char * q2colormapfile = FS_LoadFile("pics/colormap.pcx", tempmempool, true, &filesize);
193         if (q2colormapfile && filesize >= 768)
194         {
195                 unsigned char q2palette_rgb[256][3];
196                 unsigned char *out = (unsigned char *) q2palette_bgra_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
197                 int i;
198                 LoadPCX_PaletteOnly(q2colormapfile, filesize, q2palette_rgb[0]);
199                 for (i = 0;i < 256;i++)
200                 {
201                         out[i*4+2] = q2palette_rgb[i][0];
202                         out[i*4+1] = q2palette_rgb[i][1];
203                         out[i*4+0] = q2palette_rgb[i][2];
204                         out[i*4+3] = 255;
205                 }
206                 Mem_Free(q2colormapfile);
207         }
208 }
209
210 void BuildGammaTable8(float prescale, float gamma, float scale, float base, float contrastboost, unsigned char *out, int rampsize)
211 {
212         int i, adjusted;
213         double invgamma;
214         double t, d;
215
216         invgamma = 1.0 / gamma;
217         prescale /= (double) (rampsize - 1);
218         for (i = 0;i < rampsize;i++)
219         {
220                 t = i * prescale;
221                 d = ((contrastboost - 1) * t + 1);
222                 if(d == 0)
223                         t = 0; // we could just as well assume 1 here, depending on which side of the division by zero we want to be
224                 else
225                         t = contrastboost * t / d;
226                 adjusted = (int) (255.0 * (pow(t, invgamma) * scale + base) + 0.5);
227                 out[i] = bound(0, adjusted, 255);
228         }
229 }
230
231 void BuildGammaTable16(float prescale, float gamma, float scale, float base, float contrastboost, unsigned short *out, int rampsize)
232 {
233         int i, adjusted;
234         double invgamma;
235         double t;
236
237         invgamma = 1.0 / gamma;
238         prescale /= (double) (rampsize - 1);
239         for (i = 0;i < rampsize;i++)
240         {
241                 t = i * prescale;
242                 t = contrastboost * t / ((contrastboost - 1) * t + 1);
243                 adjusted = (int) (65535.0 * (pow(t, invgamma) * scale + base) + 0.5);
244                 out[i] = bound(0, adjusted, 65535);
245         }
246 }
247
248 static void Palette_Shutdown(void)
249 {
250 }
251
252 static void Palette_NewMap(void)
253 {
254 }
255
256 static void Palette_Load(void)
257 {
258         int i;
259         unsigned char *out;
260         float gamma, scale, base;
261         fs_offset_t filesize;
262         unsigned char *palfile;
263         unsigned char texturegammaramp[256];
264         union
265         {
266                 unsigned char b[4];
267                 unsigned int i;
268         }
269         bgra;
270
271         gamma = 1;
272         scale = 1;
273         base = 0;
274 // COMMANDLINEOPTION: Client: -texgamma <number> sets the quake palette gamma, allowing you to make quake textures brighter/darker, not recommended
275         i = COM_CheckParm("-texgamma");
276         if (i)
277                 gamma = atof(com_argv[i + 1]);
278 // COMMANDLINEOPTION: Client: -texcontrast <number> sets the quake palette contrast, allowing you to make quake textures brighter/darker, not recommended
279         i = COM_CheckParm("-texcontrast");
280         if (i)
281                 scale = atof(com_argv[i + 1]);
282 // COMMANDLINEOPTION: Client: -texbrightness <number> sets the quake palette brightness (brightness of black), allowing you to make quake textures brighter/darker, not recommended
283         i = COM_CheckParm("-texbrightness");
284         if (i)
285                 base = atof(com_argv[i + 1]);
286         gamma = bound(0.01, gamma, 10.0);
287         scale = bound(0.01, scale, 10.0);
288         base = bound(0, base, 0.95);
289
290         BuildGammaTable8(1.0f, gamma, scale, base, 1, texturegammaramp, 256);
291
292         palfile = (unsigned char *)FS_LoadFile ("gfx/palette.lmp", tempmempool, false, &filesize);
293         if (palfile && filesize >= 768)
294                 memcpy(palette_rgb, palfile, 768);
295         else
296         {
297                 Con_DPrint("Couldn't load gfx/palette.lmp, falling back on internal palette\n");
298                 memcpy(palette_rgb, host_quakepal, 768);
299         }
300         if (palfile)
301                 Mem_Free(palfile);
302
303         out = (unsigned char *) palette_bgra_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
304         for (i = 0;i < 256;i++)
305         {
306                 out[i*4+2] = texturegammaramp[palette_rgb[i][0]];
307                 out[i*4+1] = texturegammaramp[palette_rgb[i][1]];
308                 out[i*4+0] = texturegammaramp[palette_rgb[i][2]];
309                 out[i*4+3] = 255;
310         }
311
312         if(*r_colormap_palette.string)
313                 palfile = (unsigned char *)FS_LoadFile (r_colormap_palette.string, tempmempool, false, &filesize);
314         else
315                 palfile = NULL;
316
317         if (palfile && filesize >= 48*2)
318         {
319                 memcpy(palette_rgb_shirtcolormap[0], palfile, 48);
320                 memcpy(palette_rgb_shirtscoreboard[0], palfile + 48, 48);
321         }
322         else
323         {
324                 for(i = 0;i < 16;i++)
325                 {
326                         VectorCopy(palette_rgb[(i << 4) | ((i >= 8 && i <= 13) ? 0x04 : 0x0C)], palette_rgb_shirtcolormap[i]);
327                         VectorCopy(palette_rgb[(i << 4) | 0x08], palette_rgb_shirtscoreboard[i]);
328                 }
329         }
330
331         if (palfile && filesize >= 48*4)
332         {
333                 memcpy(palette_rgb_pantscolormap[0], palfile + 48*2, 48);
334                 memcpy(palette_rgb_pantsscoreboard[0], palfile + 48*3, 48);
335         }
336         else
337         {
338                 memcpy(palette_rgb_pantscolormap, palette_rgb_shirtcolormap, sizeof(palette_rgb_pantscolormap));
339                 memcpy(palette_rgb_pantsscoreboard, palette_rgb_shirtscoreboard, sizeof(palette_rgb_pantsscoreboard));
340         }
341
342         if(palfile)
343                 Mem_Free(palfile);
344
345         memset(palette_bgra_embeddedpic, 0, sizeof(palette_bgra_embeddedpic));
346         for (i = '1';i <= '7';i++)
347         {
348                 Vector4Set(bgra.b, 255, 255, 255, (i - '0') * 255 / 7);
349                 palette_bgra_embeddedpic[i] = bgra.i;
350         }
351
352         Palette_SetupSpecialPalettes();
353
354         Palette_LoadQ2Colormap();
355 }
356
357 void Palette_Init(void)
358 {
359         R_RegisterModule("Palette", Palette_Load, Palette_Shutdown, Palette_NewMap, NULL, NULL);
360         Cvar_RegisterVariable(&r_colormap_palette);
361         Palette_Load();
362 }