4 unsigned int d_8to24table[256];
5 //byte d_15to8table[32768];
6 byte host_basepal[768];
9 cvar_t vid_gamma = {CVAR_SAVE, "vid_gamma", "1"};
10 cvar_t vid_brightness = {CVAR_SAVE, "vid_brightness", "1"};
11 cvar_t vid_contrast = {CVAR_SAVE, "vid_contrast", "1"};
13 void Palette_Setup8to24(void)
19 out = (byte *) 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 Palette_Setup15to8(void)
40 for (i = 0;i < 32768;i++)
42 r = ((i & 0x001F) << 3)+4;
43 g = ((i & 0x03E0) >> 2)+4;
44 b = ((i & 0x7C00) >> 7)+4;
45 pal = (unsigned char *)d_8to24table;
46 for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
51 j = r1*r1+g1*g1+b1*b1;
63 void BuildGammaTable8(float prescale, float gamma, float scale, float base, byte *out)
68 gamma = bound(0.1, gamma, 5.0);
69 if (gamma == 1) // LordHavoc: dodge the math
71 for (i = 0;i < 256;i++)
73 adjusted = (int) (i * prescale * scale + base * 255.0);
74 out[i] = bound(0, adjusted, 255);
79 invgamma = 1.0 / gamma;
81 for (i = 0;i < 256;i++)
83 d = pow((double) i * prescale, invgamma) * scale + base;
84 adjusted = (int) (255.0 * d);
85 out[i] = bound(0, adjusted, 255);
90 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
95 gamma = bound(0.1, gamma, 5.0);
96 if (gamma == 1) // LordHavoc: dodge the math
98 for (i = 0;i < 256;i++)
100 adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
101 out[i] = bound(0, adjusted, 65535);
106 invgamma = 1.0 / gamma;
108 for (i = 0;i < 256;i++)
110 d = pow((double) i * prescale, invgamma) * scale + base;
111 adjusted = (int) (65535.0 * d);
112 out[i] = bound(0, adjusted, 65535);
117 qboolean hardwaregammasupported = false;
118 void VID_UpdateGamma(qboolean force)
120 static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1, cachelighthalf = -1;
122 // LordHavoc: don't mess with gamma tables if running dedicated
123 if (cls.state == ca_dedicated)
126 if (!force && vid_gamma.value == cachegamma && vid_brightness.value == cachebrightness && vid_contrast.value == cachecontrast && lighthalf == cachelighthalf)
129 if (vid_gamma.value < 0.1)
130 Cvar_SetValue("vid_gamma", 0.1);
131 if (vid_gamma.value > 5.0)
132 Cvar_SetValue("vid_gamma", 5.0);
134 if (vid_brightness.value < 1.0)
135 Cvar_SetValue("vid_brightness", 1.0);
136 if (vid_brightness.value > 5.0)
137 Cvar_SetValue("vid_brightness", 5.0);
139 if (vid_contrast.value < 0.2)
140 Cvar_SetValue("vid_contrast", 0.2);
141 if (vid_contrast.value > 1)
142 Cvar_SetValue("vid_contrast", 1);
144 cachegamma = vid_gamma.value;
145 cachebrightness = vid_brightness.value;
146 cachecontrast = vid_contrast.value;
147 cachelighthalf = lighthalf;
149 hardwaregammasupported = VID_SetGamma((cachelighthalf ? 2.0f : 1.0f), cachegamma, cachebrightness * cachecontrast, 1 - cachecontrast);
150 if (!hardwaregammasupported)
151 Con_Printf("Hardware gamma not supported.\n");
154 void Gamma_Init(void)
156 Cvar_RegisterVariable(&vid_gamma);
157 Cvar_RegisterVariable(&vid_brightness);
158 Cvar_RegisterVariable(&vid_contrast);
161 void Palette_Init(void)
164 pal = (byte *)COM_LoadFile ("gfx/palette.lmp", false);
166 Sys_Error ("Couldn't load gfx/palette.lmp");
167 memcpy(host_basepal, pal, 765);
169 host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
170 Palette_Setup8to24();
171 // Palette_Setup15to8();