4 unsigned int d_8to24table[256];
5 //qbyte d_15to8table[32768];
6 qbyte host_basepal[768];
9 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
10 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
11 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
12 cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
13 cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
15 void Palette_Setup8to24(void)
21 out = (qbyte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
22 for (i=0 ; i<255 ; i++)
29 d_8to24table[255] = 0; // completely transparent black
33 void Palette_Setup15to8(void)
42 for (i = 0;i < 32768;i++)
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)
53 j = r1*r1+g1*g1+b1*b1;
65 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
70 gamma = bound(0.1, gamma, 5.0);
71 if (gamma == 1) // LordHavoc: dodge the math
73 for (i = 0;i < 256;i++)
75 adjusted = (int) (i * prescale * scale + base * 255.0);
76 out[i] = bound(0, adjusted, 255);
81 invgamma = 1.0 / gamma;
83 for (i = 0;i < 256;i++)
85 d = pow((double) i * prescale, invgamma) * scale + base;
86 adjusted = (int) (255.0 * d);
87 out[i] = bound(0, adjusted, 255);
92 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
97 gamma = bound(0.1, gamma, 5.0);
98 if (gamma == 1) // LordHavoc: dodge the math
100 for (i = 0;i < 256;i++)
102 adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
103 out[i] = bound(0, adjusted, 65535);
108 invgamma = 1.0 / gamma;
110 for (i = 0;i < 256;i++)
112 d = pow((double) i * prescale, invgamma) * scale + base;
113 adjusted = (int) (65535.0 * d);
114 out[i] = bound(0, adjusted, 65535);
119 qboolean hardwaregammasupported = false;
120 void VID_UpdateGamma(qboolean force)
122 static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
123 static int cacheoverbrightbits = -1, cachehwgamma = -1;
125 // LordHavoc: don't mess with gamma tables if running dedicated
126 if (cls.state == ca_dedicated)
130 && v_gamma.value == cachegamma
131 && v_contrast.value == cachecontrast
132 && v_brightness.value == cachebrightness
133 && v_overbrightbits.integer == cacheoverbrightbits
134 && v_hwgamma.value == cachehwgamma)
137 if (v_gamma.value < 0.1)
138 Cvar_SetValue("v_gamma", 0.1);
139 if (v_gamma.value > 5.0)
140 Cvar_SetValue("v_gamma", 5.0);
142 if (v_contrast.value < 0.5)
143 Cvar_SetValue("v_contrast", 0.5);
144 if (v_contrast.value > 5.0)
145 Cvar_SetValue("v_contrast", 5.0);
147 if (v_brightness.value < 0)
148 Cvar_SetValue("v_brightness", 0);
149 if (v_brightness.value > 0.8)
150 Cvar_SetValue("v_brightness", 0.8);
152 cachegamma = v_gamma.value;
153 cachecontrast = v_contrast.value;
154 cachebrightness = v_brightness.value;
155 cacheoverbrightbits = v_overbrightbits.integer;
157 hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
158 if (!hardwaregammasupported)
160 Con_Printf("Hardware gamma not supported.\n");
161 Cvar_SetValue("v_hwgamma", 0);
163 cachehwgamma = v_hwgamma.integer;
166 void Gamma_Init(void)
168 Cvar_RegisterVariable(&v_gamma);
169 Cvar_RegisterVariable(&v_brightness);
170 Cvar_RegisterVariable(&v_contrast);
171 Cvar_RegisterVariable(&v_hwgamma);
172 Cvar_RegisterVariable(&v_overbrightbits);
175 void Palette_Init(void)
178 pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
180 Sys_Error ("Couldn't load gfx/palette.lmp");
181 memcpy(host_basepal, pal, 765);
183 host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
184 Palette_Setup8to24();
185 // Palette_Setup15to8();