4 unsigned int d_8to24table[256];
5 qbyte host_basepal[768];
7 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
8 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
9 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
10 cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
11 cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
13 void Palette_Setup8to24(void)
19 out = (qbyte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
20 for (i=0 ; i<255 ; i++)
27 d_8to24table[255] = 0; // completely transparent black
31 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
36 gamma = bound(0.1, gamma, 5.0);
37 if (gamma == 1) // LordHavoc: dodge the math
39 for (i = 0;i < 256;i++)
41 adjusted = (int) (i * prescale * scale + base * 255.0);
42 out[i] = bound(0, adjusted, 255);
47 invgamma = 1.0 / gamma;
49 for (i = 0;i < 256;i++)
51 d = pow((double) i * prescale, invgamma) * scale + base;
52 adjusted = (int) (255.0 * d);
53 out[i] = bound(0, adjusted, 255);
58 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
63 gamma = bound(0.1, gamma, 5.0);
64 if (gamma == 1) // LordHavoc: dodge the math
66 for (i = 0;i < 256;i++)
68 adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
69 out[i] = bound(0, adjusted, 65535);
74 invgamma = 1.0 / gamma;
76 for (i = 0;i < 256;i++)
78 d = pow((double) i * prescale, invgamma) * scale + base;
79 adjusted = (int) (65535.0 * d);
80 out[i] = bound(0, adjusted, 65535);
85 qboolean hardwaregammasupported = false;
86 void VID_UpdateGamma(qboolean force)
88 static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
89 static int cacheoverbrightbits = -1, cachehwgamma = -1;
91 // LordHavoc: don't mess with gamma tables if running dedicated
92 if (cls.state == ca_dedicated)
96 && v_gamma.value == cachegamma
97 && v_contrast.value == cachecontrast
98 && v_brightness.value == cachebrightness
99 && v_overbrightbits.integer == cacheoverbrightbits
100 && v_hwgamma.value == cachehwgamma)
103 if (v_gamma.value < 0.1)
104 Cvar_SetValue("v_gamma", 0.1);
105 if (v_gamma.value > 5.0)
106 Cvar_SetValue("v_gamma", 5.0);
108 if (v_contrast.value < 0.5)
109 Cvar_SetValue("v_contrast", 0.5);
110 if (v_contrast.value > 5.0)
111 Cvar_SetValue("v_contrast", 5.0);
113 if (v_brightness.value < 0)
114 Cvar_SetValue("v_brightness", 0);
115 if (v_brightness.value > 0.8)
116 Cvar_SetValue("v_brightness", 0.8);
118 cachegamma = v_gamma.value;
119 cachecontrast = v_contrast.value;
120 cachebrightness = v_brightness.value;
121 cacheoverbrightbits = v_overbrightbits.integer;
123 hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
124 if (!hardwaregammasupported)
126 Con_Printf("Hardware gamma not supported.\n");
127 Cvar_SetValue("v_hwgamma", 0);
129 cachehwgamma = v_hwgamma.integer;
132 void Gamma_Init(void)
134 Cvar_RegisterVariable(&v_gamma);
135 Cvar_RegisterVariable(&v_brightness);
136 Cvar_RegisterVariable(&v_contrast);
137 Cvar_RegisterVariable(&v_hwgamma);
138 Cvar_RegisterVariable(&v_overbrightbits);
141 void Palette_Init(void)
144 float gamma, scale, base;
147 pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
149 Sys_Error ("Couldn't load gfx/palette.lmp");
150 memcpy(host_basepal, pal, 765);
152 host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
157 i = COM_CheckParm("-texgamma");
159 gamma = atof(com_argv[i + 1]);
160 i = COM_CheckParm("-texcontrast");
162 scale = atof(com_argv[i + 1]);
163 i = COM_CheckParm("-texbrightness");
165 base = atof(com_argv[i + 1]);
166 gamma = bound(0.01, gamma, 10.0);
167 scale = bound(0.01, scale, 10.0);
168 base = bound(0, base, 0.95);
170 BuildGammaTable8(1.0f, gamma, scale, base, temp);
171 for (i = 3;i < 765;i++)
172 host_basepal[i] = temp[host_basepal[i]];
174 Palette_Setup8to24();