4 unsigned int d_8to24table[256];
5 //qbyte d_15to8table[32768];
6 qbyte host_basepal[768];
8 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
9 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
10 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
11 cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
12 cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
14 void Palette_Setup8to24(void)
20 out = (qbyte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
21 for (i=0 ; i<255 ; i++)
28 d_8to24table[255] = 0; // completely transparent black
32 void Palette_Setup15to8(void)
41 for (i = 0;i < 32768;i++)
43 r = ((i & 0x001F) << 3)+4;
44 g = ((i & 0x03E0) >> 2)+4;
45 b = ((i & 0x7C00) >> 7)+4;
46 pal = (unsigned char *)d_8to24table;
47 for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
52 j = r1*r1+g1*g1+b1*b1;
64 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
69 gamma = bound(0.1, gamma, 5.0);
70 if (gamma == 1) // LordHavoc: dodge the math
72 for (i = 0;i < 256;i++)
74 adjusted = (int) (i * prescale * scale + base * 255.0);
75 out[i] = bound(0, adjusted, 255);
80 invgamma = 1.0 / gamma;
82 for (i = 0;i < 256;i++)
84 d = pow((double) i * prescale, invgamma) * scale + base;
85 adjusted = (int) (255.0 * d);
86 out[i] = bound(0, adjusted, 255);
91 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
96 gamma = bound(0.1, gamma, 5.0);
97 if (gamma == 1) // LordHavoc: dodge the math
99 for (i = 0;i < 256;i++)
101 adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
102 out[i] = bound(0, adjusted, 65535);
107 invgamma = 1.0 / gamma;
109 for (i = 0;i < 256;i++)
111 d = pow((double) i * prescale, invgamma) * scale + base;
112 adjusted = (int) (65535.0 * d);
113 out[i] = bound(0, adjusted, 65535);
118 qboolean hardwaregammasupported = false;
119 void VID_UpdateGamma(qboolean force)
121 static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
122 static int cacheoverbrightbits = -1, cachehwgamma = -1;
124 // LordHavoc: don't mess with gamma tables if running dedicated
125 if (cls.state == ca_dedicated)
129 && v_gamma.value == cachegamma
130 && v_contrast.value == cachecontrast
131 && v_brightness.value == cachebrightness
132 && v_overbrightbits.integer == cacheoverbrightbits
133 && v_hwgamma.value == cachehwgamma)
136 if (v_gamma.value < 0.1)
137 Cvar_SetValue("v_gamma", 0.1);
138 if (v_gamma.value > 5.0)
139 Cvar_SetValue("v_gamma", 5.0);
141 if (v_contrast.value < 0.5)
142 Cvar_SetValue("v_contrast", 0.5);
143 if (v_contrast.value > 5.0)
144 Cvar_SetValue("v_contrast", 5.0);
146 if (v_brightness.value < 0)
147 Cvar_SetValue("v_brightness", 0);
148 if (v_brightness.value > 0.8)
149 Cvar_SetValue("v_brightness", 0.8);
151 cachegamma = v_gamma.value;
152 cachecontrast = v_contrast.value;
153 cachebrightness = v_brightness.value;
154 cacheoverbrightbits = v_overbrightbits.integer;
156 hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
157 if (!hardwaregammasupported)
159 Con_Printf("Hardware gamma not supported.\n");
160 Cvar_SetValue("v_hwgamma", 0);
162 cachehwgamma = v_hwgamma.integer;
165 void Gamma_Init(void)
167 Cvar_RegisterVariable(&v_gamma);
168 Cvar_RegisterVariable(&v_brightness);
169 Cvar_RegisterVariable(&v_contrast);
170 Cvar_RegisterVariable(&v_hwgamma);
171 Cvar_RegisterVariable(&v_overbrightbits);
174 void Palette_Init(void)
177 float gamma, scale, base;
180 pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
182 Sys_Error ("Couldn't load gfx/palette.lmp");
183 memcpy(host_basepal, pal, 765);
185 host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
190 i = COM_CheckParm("-texgamma");
192 gamma = atof(com_argv[i + 1]);
193 i = COM_CheckParm("-texcontrast");
195 scale = atof(com_argv[i + 1]);
196 i = COM_CheckParm("-texbrightness");
198 base = atof(com_argv[i + 1]);
199 gamma = bound(0.01, gamma, 10.0);
200 scale = bound(0.01, scale, 10.0);
201 base = bound(0, base, 0.95);
203 BuildGammaTable8(1.0f, gamma, scale, base, temp);
204 for (i = 3;i < 765;i++)
205 host_basepal[i] = temp[host_basepal[i]];
207 Palette_Setup8to24();
208 // Palette_Setup15to8();