4 unsigned int d_8to24table[256];
5 //byte d_15to8table[32768];
6 byte host_basepal[768];
9 static float texture_gamma = 1.0;
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"};
15 void Palette_Setup8to24(void)
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++)
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, byte *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 void Texture_Gamma (void)
125 if ((i = COM_CheckParm("-gamma")))
126 texture_gamma = atof(com_argv[i+1]);
127 texture_gamma = bound(0.1, texture_gamma, 5.0);
129 if (texture_gamma == 1) // LordHavoc: dodge the math
131 for (i = 0;i < 256;i++)
136 invgamma = 1.0 / texture_gamma;
137 for (i = 0;i < 256;i++)
139 adjusted = (int) ((255.0 * pow((double) i / 255.0, invgamma)) + 0.5);
140 texgamma[i] = bound(0, adjusted, 255);
145 qboolean hardwaregammasupported = false;
146 void VID_UpdateGamma(qboolean force)
148 static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1, cachelighthalf = -1;
150 // LordHavoc: don't mess with gamma tables if running dedicated
154 if (!force && vid_gamma.value == cachegamma && vid_brightness.value == cachebrightness && vid_contrast.value == cachecontrast && lighthalf == cachelighthalf)
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);
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);
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);
172 cachegamma = vid_gamma.value;
173 cachebrightness = vid_brightness.value;
174 cachecontrast = vid_contrast.value;
175 cachelighthalf = lighthalf;
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");
182 void Gamma_Init(void)
184 Cvar_RegisterVariable(&vid_gamma);
185 Cvar_RegisterVariable(&vid_brightness);
186 Cvar_RegisterVariable(&vid_contrast);
189 void Palette_Init(void)
192 pal = (byte *)COM_LoadMallocFile ("gfx/palette.lmp", false);
194 Sys_Error ("Couldn't load gfx/palette.lmp");
195 memcpy(host_basepal, pal, 765);
197 host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
198 Palette_Setup8to24();
199 // Palette_Setup15to8();