]> icculus.org git repositories - divverent/darkplaces.git/blob - palette.c
forgot to put a newline at the end of the file
[divverent/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3
4 unsigned int d_8to24table[256];
5 //byte d_15to8table[32768];
6 byte host_basepal[768];
7 byte texgamma[256];
8
9 static float texture_gamma = 1.0;
10
11 cvar_t vid_gamma = {CVAR_SAVE, "vid_gamma", "1"};
12 cvar_t vid_brightness = {CVAR_SAVE, "vid_brightness", "1"};
13 cvar_t vid_contrast = {CVAR_SAVE, "vid_contrast", "1"};
14
15 void Palette_Setup8to24(void)
16 {
17         byte *in, *out;
18         unsigned short i;
19
20         in = host_basepal;
21         out = (byte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
22         for (i=0 ; i<255 ; i++)
23         {
24                 *out++ = *in++;
25                 *out++ = *in++;
26                 *out++ = *in++;
27                 *out++ = 255;
28         }
29         d_8to24table[255] = 0; // completely transparent black
30 }
31
32 /*
33 void    Palette_Setup15to8(void)
34 {
35         byte    *pal;
36         unsigned r,g,b;
37         unsigned v;
38         int     r1,g1,b1;
39         int             j,k,l;
40         unsigned short i;
41
42         for (i = 0;i < 32768;i++)
43         {
44                 r = ((i & 0x001F) << 3)+4;
45                 g = ((i & 0x03E0) >> 2)+4;
46                 b = ((i & 0x7C00) >> 7)+4;
47                 pal = (unsigned char *)d_8to24table;
48                 for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
49                 {
50                         r1 = r - pal[0];
51                         g1 = g - pal[1];
52                         b1 = b - pal[2];
53                         j = r1*r1+g1*g1+b1*b1;
54                         if (j < l)
55                         {
56                                 k = v;
57                                 l = j;
58                         }
59                 }
60                 d_15to8table[i] = k;
61         }
62 }
63 */
64
65 void BuildGammaTable8(float prescale, float gamma, float scale, float base, byte *out)
66 {
67         int i, adjusted;
68         double invgamma, d;
69
70         gamma = bound(0.1, gamma, 5.0);
71         if (gamma == 1) // LordHavoc: dodge the math
72         {
73                 for (i = 0;i < 256;i++)
74                 {
75                         adjusted = (int) (i * prescale * scale + base * 255.0);
76                         out[i] = bound(0, adjusted, 255);
77                 }
78         }
79         else
80         {
81                 invgamma = 1.0 / gamma;
82                 prescale /= 255.0f;
83                 for (i = 0;i < 256;i++)
84                 {
85                         d = pow((double) i * prescale, invgamma) * scale + base;
86                         adjusted = (int) (255.0 * d);
87                         out[i] = bound(0, adjusted, 255);
88                 }
89         }
90 }
91
92 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
93 {
94         int i, adjusted;
95         double invgamma, d;
96
97         gamma = bound(0.1, gamma, 5.0);
98         if (gamma == 1) // LordHavoc: dodge the math
99         {
100                 for (i = 0;i < 256;i++)
101                 {
102                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
103                         out[i] = bound(0, adjusted, 65535);
104                 }
105         }
106         else
107         {
108                 invgamma = 1.0 / gamma;
109                 prescale /= 255.0f;
110                 for (i = 0;i < 256;i++)
111                 {
112                         d = pow((double) i * prescale, invgamma) * scale + base;
113                         adjusted = (int) (65535.0 * d);
114                         out[i] = bound(0, adjusted, 65535);
115                 }
116         }
117 }
118
119 void Texture_Gamma (void)
120 {
121         int i, adjusted;
122         double invgamma;
123
124         texture_gamma = 1;
125         if ((i = COM_CheckParm("-gamma")))
126                 texture_gamma = atof(com_argv[i+1]);
127         texture_gamma = bound(0.1, texture_gamma, 5.0);
128
129         if (texture_gamma == 1) // LordHavoc: dodge the math
130         {
131                 for (i = 0;i < 256;i++)
132                         texgamma[i] = i;
133         }
134         else
135         {
136                 invgamma = 1.0 / texture_gamma;
137                 for (i = 0;i < 256;i++)
138                 {
139                         adjusted = (int) ((255.0 * pow((double) i / 255.0, invgamma)) + 0.5);
140                         texgamma[i] = bound(0, adjusted, 255);
141                 }
142         }
143 }
144
145 qboolean hardwaregammasupported = false;
146 void VID_UpdateGamma(qboolean force)
147 {
148         static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1, cachelighthalf = -1;
149
150         // LordHavoc: don't mess with gamma tables if running dedicated
151         if (isDedicated)
152                 return;
153
154         if (!force && vid_gamma.value == cachegamma && vid_brightness.value == cachebrightness && vid_contrast.value == cachecontrast && lighthalf == cachelighthalf)
155                 return;
156
157         if (vid_gamma.value < 0.1)
158                 Cvar_SetValue("vid_gamma", 0.1);
159         if (vid_gamma.value > 5.0)
160                 Cvar_SetValue("vid_gamma", 5.0);
161
162         if (vid_brightness.value < 1.0)
163                 Cvar_SetValue("vid_brightness", 1.0);
164         if (vid_brightness.value > 5.0)
165                 Cvar_SetValue("vid_brightness", 5.0);
166
167         if (vid_contrast.value < 0.2)
168                 Cvar_SetValue("vid_contrast", 0.2);
169         if (vid_contrast.value > 1)
170                 Cvar_SetValue("vid_contrast", 1);
171
172         cachegamma = vid_gamma.value;
173         cachebrightness = vid_brightness.value;
174         cachecontrast = vid_contrast.value;
175         cachelighthalf = lighthalf;
176
177         hardwaregammasupported = VID_SetGamma((cachelighthalf ? 2.0f : 1.0f), cachegamma, cachebrightness * cachecontrast, 1 - cachecontrast);
178         if (!hardwaregammasupported)
179                 Con_Printf("Hardware gamma not supported.\n");
180 }
181
182 void Gamma_Init(void)
183 {
184         Cvar_RegisterVariable(&vid_gamma);
185         Cvar_RegisterVariable(&vid_brightness);
186         Cvar_RegisterVariable(&vid_contrast);
187 }
188
189 void Palette_Init(void)
190 {
191         byte *pal;
192         pal = (byte *)COM_LoadMallocFile ("gfx/palette.lmp", false);
193         if (!pal)
194                 Sys_Error ("Couldn't load gfx/palette.lmp");
195         memcpy(host_basepal, pal, 765);
196         qfree(pal);
197         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
198         Palette_Setup8to24();
199 //      Palette_Setup15to8();
200         Texture_Gamma();
201 }