]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - palette.c
added Math_atov function (ascii to vector), tries to parse any imaginable vector...
[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
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
141 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
142 {
143         int i, adjusted;
144         double invgamma, d;
145
146         gamma = bound(0.1, gamma, 5.0);
147         if (gamma == 1) // LordHavoc: dodge the math
148         {
149                 for (i = 0;i < 256;i++)
150                 {
151                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
152                         out[i] = bound(0, adjusted, 65535);
153                 }
154         }
155         else
156         {
157                 invgamma = 1.0 / gamma;
158                 prescale /= 255.0f;
159                 for (i = 0;i < 256;i++)
160                 {
161                         d = pow((double) i * prescale, invgamma) * scale + base;
162                         adjusted = (int) (65535.0 * d);
163                         out[i] = bound(0, adjusted, 65535);
164                 }
165         }
166 }
167
168 void Palette_Init(void)
169 {
170         int i;
171         float gamma, scale, base;
172         qbyte *pal;
173         qbyte temp[256];
174         pal = (qbyte *)FS_LoadFile ("gfx/palette.lmp", false);
175         if (!pal)
176                 Sys_Error ("Couldn't load gfx/palette.lmp");
177         memcpy(host_basepal, pal, 765);
178         Mem_Free(pal);
179         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
180
181         gamma = 1;
182         scale = 1;
183         base = 0;
184         i = COM_CheckParm("-texgamma");
185         if (i)
186                 gamma = atof(com_argv[i + 1]);
187         i = COM_CheckParm("-texcontrast");
188         if (i)
189                 scale = atof(com_argv[i + 1]);
190         i = COM_CheckParm("-texbrightness");
191         if (i)
192                 base = atof(com_argv[i + 1]);
193         gamma = bound(0.01, gamma, 10.0);
194         scale = bound(0.01, scale, 10.0);
195         base = bound(0, base, 0.95);
196
197         BuildGammaTable8(1.0f, gamma, scale, base, temp);
198         for (i = 3;i < 765;i++)
199                 host_basepal[i] = temp[host_basepal[i]];
200
201         Palette_Setup8to24();
202 }
203