]> icculus.org git repositories - divverent/darkplaces.git/blob - palette.c
fix stupid crash
[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 // 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.
15 unsigned char host_quakepal[768] =
16 {
17 0,0,0,15,15,15,31,31,31,47,47,47,63,63,63,75,75,75,91,91,91,107,107,107,123,123,123,139,139,139,155,155,155,171,171,171,187,187,187,203,203,203,219,219,219,235,235,235,15,11,7,23,15,11,31,23,11,39,27,15,47,35,19,55,43,23,63,47,23,75,55,27,83,59,27,91,67,31,99,75,31,107,83,31,115,87,31,123,95,35,131,103,35,143,111,35,11,11,15,19,19,27,27,27,39,39,39,51,47,47,63,55,55,75,63,63,87,71,71,103,79,79,115,91,91,127,99,99,
18 139,107,107,151,115,115,163,123,123,175,131,131,187,139,139,203,0,0,0,7,7,0,11,11,0,19,19,0,27,27,0,35,35,0,43,43,7,47,47,7,55,55,7,63,63,7,71,71,7,75,75,11,83,83,11,91,91,11,99,99,11,107,107,15,7,0,0,15,0,0,23,0,0,31,0,0,39,0,0,47,0,0,55,0,0,63,0,0,71,0,0,79,0,0,87,0,0,95,0,0,103,0,0,111,0,0,119,0,0,127,0,0,19,19,0,27,27,0,35,35,0,47,43,0,55,47,0,67,
19 55,0,75,59,7,87,67,7,95,71,7,107,75,11,119,83,15,131,87,19,139,91,19,151,95,27,163,99,31,175,103,35,35,19,7,47,23,11,59,31,15,75,35,19,87,43,23,99,47,31,115,55,35,127,59,43,143,67,51,159,79,51,175,99,47,191,119,47,207,143,43,223,171,39,239,203,31,255,243,27,11,7,0,27,19,0,43,35,15,55,43,19,71,51,27,83,55,35,99,63,43,111,71,51,127,83,63,139,95,71,155,107,83,167,123,95,183,135,107,195,147,123,211,163,139,227,179,151,
20 171,139,163,159,127,151,147,115,135,139,103,123,127,91,111,119,83,99,107,75,87,95,63,75,87,55,67,75,47,55,67,39,47,55,31,35,43,23,27,35,19,19,23,11,11,15,7,7,187,115,159,175,107,143,163,95,131,151,87,119,139,79,107,127,75,95,115,67,83,107,59,75,95,51,63,83,43,55,71,35,43,59,31,35,47,23,27,35,19,19,23,11,11,15,7,7,219,195,187,203,179,167,191,163,155,175,151,139,163,135,123,151,123,111,135,111,95,123,99,83,107,87,71,95,75,59,83,63,
21 51,67,51,39,55,43,31,39,31,23,27,19,15,15,11,7,111,131,123,103,123,111,95,115,103,87,107,95,79,99,87,71,91,79,63,83,71,55,75,63,47,67,55,43,59,47,35,51,39,31,43,31,23,35,23,15,27,19,11,19,11,7,11,7,255,243,27,239,223,23,219,203,19,203,183,15,187,167,15,171,151,11,155,131,7,139,115,7,123,99,7,107,83,0,91,71,0,75,55,0,59,43,0,43,31,0,27,15,0,11,7,0,0,0,255,11,11,239,19,19,223,27,27,207,35,35,191,43,
22 43,175,47,47,159,47,47,143,47,47,127,47,47,111,47,47,95,43,43,79,35,35,63,27,27,47,19,19,31,11,11,15,43,0,0,59,0,0,75,7,0,95,7,0,111,15,0,127,23,7,147,31,7,163,39,11,183,51,15,195,75,27,207,99,43,219,127,59,227,151,79,231,171,95,239,191,119,247,211,139,167,123,59,183,155,55,199,195,55,231,227,87,127,191,255,171,231,255,215,255,255,103,0,0,139,0,0,179,0,0,215,0,0,255,0,0,255,243,147,255,247,199,255,255,255,159,91,83
23 };
24
25 void Palette_SetupSpecialPalettes(void)
26 {
27         int i;
28         int fullbright_start, fullbright_end;
29         int pants_start, pants_end;
30         int shirt_start, shirt_end;
31         int reversed_start, reversed_end;
32         unsigned char *colormap;
33         fs_offset_t filesize;
34
35         colormap = FS_LoadFile("gfx/colormap.lmp", tempmempool, true, NULL);
36         if (colormap && filesize >= 16385)
37                 fullbright_start = 256 - colormap[16384];
38         else
39                 fullbright_start = 256;
40         if (colormap)
41                 Mem_Free(colormap);
42         fullbright_end = 256;
43         pants_start = 96;
44         pants_end = 112;
45         shirt_start = 16;
46         shirt_end = 32;
47         reversed_start = 128;
48         reversed_end = 224;
49
50         for (i = 0;i < fullbright_start;i++)
51                 palette_nofullbrights[i] = palette_complete[i];
52         for (i = fullbright_start;i < 255;i++)
53                 palette_nofullbrights[i] = palette_complete[0];
54         palette_nofullbrights[255] = 0;
55
56         for (i = 0;i < 256;i++)
57                 palette_onlyfullbrights[i] = palette_complete[0];
58         for (i = fullbright_start;i < fullbright_end;i++)
59                 palette_onlyfullbrights[i] = palette_complete[i];
60         palette_onlyfullbrights[255] = 0;
61
62         for (i = 0;i < 256;i++)
63                 palette_nocolormapnofullbrights[i] = palette_complete[i];
64         for (i = pants_start;i < pants_end;i++)
65                 palette_nocolormapnofullbrights[i] = palette_complete[0];
66         for (i = shirt_start;i < shirt_end;i++)
67                 palette_nocolormapnofullbrights[i] = palette_complete[0];
68         for (i = fullbright_start;i < fullbright_end;i++)
69                 palette_nocolormapnofullbrights[i] = palette_complete[0];
70         palette_nocolormapnofullbrights[255] = 0;
71
72         for (i = 0;i < 256;i++)
73                 palette_nocolormap[i] = palette_complete[i];
74         for (i = pants_start;i < pants_end;i++)
75                 palette_nocolormap[i] = palette_complete[0];
76         for (i = shirt_start;i < shirt_end;i++)
77                 palette_nocolormap[i] = palette_complete[0];
78         palette_nocolormap[255] = 0;
79
80         for (i = 0;i < 256;i++)
81                 palette_pantsaswhite[i] = palette_complete[0];
82         for (i = pants_start;i < pants_end;i++)
83         {
84                 if (i >= reversed_start && i < reversed_end)
85                         palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
86                 else
87                         palette_pantsaswhite[i] = palette_complete[i - pants_start];
88         }
89
90         for (i = 0;i < 256;i++)
91                 palette_shirtaswhite[i] = palette_complete[0];
92         for (i = shirt_start;i < shirt_end;i++)
93         {
94                 if (i >= reversed_start && i < reversed_end)
95                         palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
96                 else
97                         palette_shirtaswhite[i] = palette_complete[i - shirt_start];
98         }
99
100         for (i = 0;i < 255;i++)
101                 palette_alpha[i] = 0xFFFFFFFF;
102         palette_alpha[255] = 0;
103
104         palette_font[0] = 0;
105         for (i = 1;i < 255;i++)
106                 palette_font[i] = palette_complete[i];
107         palette_font[255] = 0;
108 }
109
110 void BuildGammaTable8(float prescale, float gamma, float scale, float base, unsigned char *out)
111 {
112         int i, adjusted;
113         double invgamma, d;
114
115         gamma = bound(0.1, gamma, 5.0);
116         if (gamma == 1) // LordHavoc: dodge the math
117         {
118                 for (i = 0;i < 256;i++)
119                 {
120                         adjusted = (int) (i * prescale * scale + base * 255.0);
121                         out[i] = bound(0, adjusted, 255);
122                 }
123         }
124         else
125         {
126                 invgamma = 1.0 / gamma;
127                 prescale /= 255.0f;
128                 for (i = 0;i < 256;i++)
129                 {
130                         d = pow((double) i * prescale, invgamma) * scale + base;
131                         adjusted = (int) (255.0 * d);
132                         out[i] = bound(0, adjusted, 255);
133                 }
134         }
135 }
136
137 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
138 {
139         int i, adjusted;
140         double invgamma, d;
141
142         gamma = bound(0.1, gamma, 5.0);
143         if (gamma == 1) // LordHavoc: dodge the math
144         {
145                 for (i = 0;i < 256;i++)
146                 {
147                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
148                         out[i] = bound(0, adjusted, 65535);
149                 }
150         }
151         else
152         {
153                 invgamma = 1.0 / gamma;
154                 prescale /= 255.0f;
155                 for (i = 0;i < 256;i++)
156                 {
157                         d = pow((double) i * prescale, invgamma) * scale + base;
158                         adjusted = (int) (65535.0 * d);
159                         out[i] = bound(0, adjusted, 65535);
160                 }
161         }
162 }
163
164 void Palette_Init(void)
165 {
166         int i;
167         float gamma, scale, base;
168         fs_offset_t filesize;
169         unsigned char *in, *out, *palfile;
170         unsigned char texturegammaramp[256];
171
172         gamma = 1;
173         scale = 1;
174         base = 0;
175 // COMMANDLINEOPTION: Client: -texgamma <number> sets the quake palette gamma, allowing you to make quake textures brighter/darker, not recommended
176         i = COM_CheckParm("-texgamma");
177         if (i)
178                 gamma = atof(com_argv[i + 1]);
179 // COMMANDLINEOPTION: Client: -texcontrast <number> sets the quake palette contrast, allowing you to make quake textures brighter/darker, not recommended
180         i = COM_CheckParm("-texcontrast");
181         if (i)
182                 scale = atof(com_argv[i + 1]);
183 // COMMANDLINEOPTION: Client: -texbrightness <number> sets the quake palette brightness (brightness of black), allowing you to make quake textures brighter/darker, not recommended
184         i = COM_CheckParm("-texbrightness");
185         if (i)
186                 base = atof(com_argv[i + 1]);
187         gamma = bound(0.01, gamma, 10.0);
188         scale = bound(0.01, scale, 10.0);
189         base = bound(0, base, 0.95);
190
191         BuildGammaTable8(1.0f, gamma, scale, base, texturegammaramp);
192
193         palfile = (unsigned char *)FS_LoadFile ("gfx/palette.lmp", tempmempool, false, &filesize);
194         if (palfile && filesize >= 768)
195                 in = palfile;
196         else
197         {
198                 Con_DPrint("Couldn't load gfx/palette.lmp, falling back on internal palette\n");
199                 in = host_quakepal;
200         }
201         out = (unsigned char *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
202         for (i = 0;i < 255;i++)
203         {
204                 *out++ = texturegammaramp[*in++];
205                 *out++ = texturegammaramp[*in++];
206                 *out++ = texturegammaramp[*in++];
207                 *out++ = 255;
208         }
209         palette_complete[255] = 0; // completely transparent black
210         if (palfile)
211                 Mem_Free(palfile);
212
213         Palette_SetupSpecialPalettes();
214 }
215