]> icculus.org git repositories - divverent/darkplaces.git/blob - palette.c
Fixed windows input (bad,bad hack)
[divverent/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 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
114 {
115         int i, adjusted;
116         double invgamma, d;
117
118         gamma = bound(0.1, gamma, 5.0);
119         if (gamma == 1) // LordHavoc: dodge the math
120         {
121                 for (i = 0;i < 256;i++)
122                 {
123                         adjusted = (int) (i * prescale * scale + base * 255.0);
124                         out[i] = bound(0, adjusted, 255);
125                 }
126         }
127         else
128         {
129                 invgamma = 1.0 / gamma;
130                 prescale /= 255.0f;
131                 for (i = 0;i < 256;i++)
132                 {
133                         d = pow((double) i * prescale, invgamma) * scale + base;
134                         adjusted = (int) (255.0 * d);
135                         out[i] = bound(0, adjusted, 255);
136                 }
137         }
138 }
139
140 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
141 {
142         int i, adjusted;
143         double invgamma, d;
144
145         gamma = bound(0.1, gamma, 5.0);
146         if (gamma == 1) // LordHavoc: dodge the math
147         {
148                 for (i = 0;i < 256;i++)
149                 {
150                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
151                         out[i] = bound(0, adjusted, 65535);
152                 }
153         }
154         else
155         {
156                 invgamma = 1.0 / gamma;
157                 prescale /= 255.0f;
158                 for (i = 0;i < 256;i++)
159                 {
160                         d = pow((double) i * prescale, invgamma) * scale + base;
161                         adjusted = (int) (65535.0 * d);
162                         out[i] = bound(0, adjusted, 65535);
163                 }
164         }
165 }
166
167 void Palette_Init(void)
168 {
169         int i;
170         float gamma, scale, base;
171         qbyte *pal;
172         qbyte temp[256];
173         pal = (qbyte *)FS_LoadFile ("gfx/palette.lmp", false);
174         if (!pal)
175                 Sys_Error ("Couldn't load gfx/palette.lmp");
176         memcpy(host_basepal, pal, 765);
177         Mem_Free(pal);
178         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
179
180         gamma = 1;
181         scale = 1;
182         base = 0;
183         i = COM_CheckParm("-texgamma");
184         if (i)
185                 gamma = atof(com_argv[i + 1]);
186         i = COM_CheckParm("-texcontrast");
187         if (i)
188                 scale = atof(com_argv[i + 1]);
189         i = COM_CheckParm("-texbrightness");
190         if (i)
191                 base = atof(com_argv[i + 1]);
192         gamma = bound(0.01, gamma, 10.0);
193         scale = bound(0.01, scale, 10.0);
194         base = bound(0, base, 0.95);
195
196         BuildGammaTable8(1.0f, gamma, scale, base, temp);
197         for (i = 3;i < 765;i++)
198                 host_basepal[i] = temp[host_basepal[i]];
199
200         Palette_Setup8to24();
201 }
202