#include "quakedef.h"
unsigned int d_8to24table[256];
-//byte d_15to8table[32768];
-byte host_basepal[768];
-byte qgamma[256];
-static float vid_gamma = 1.0;
+//qbyte d_15to8table[32768];
+qbyte host_basepal[768];
+qbyte texgamma[256];
-void Palette_Setup8to24()
+cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
+cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
+cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
+cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
+cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
+
+void Palette_Setup8to24(void)
{
- byte *in, *out;
+ qbyte *in, *out;
unsigned short i;
in = host_basepal;
- out = (byte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
+ out = (qbyte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
for (i=0 ; i<255 ; i++)
{
*out++ = *in++;
}
/*
-void Palette_Setup15to8()
+void Palette_Setup15to8(void)
{
- byte *pal;
+ qbyte *pal;
unsigned r,g,b;
unsigned v;
int r1,g1,b1;
}
*/
-void Palette_Gamma ()
+void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
{
- float inf;
- int i;
+ int i, adjusted;
+ double invgamma, d;
+
+ gamma = bound(0.1, gamma, 5.0);
+ if (gamma == 1) // LordHavoc: dodge the math
+ {
+ for (i = 0;i < 256;i++)
+ {
+ adjusted = (int) (i * prescale * scale + base * 255.0);
+ out[i] = bound(0, adjusted, 255);
+ }
+ }
+ else
+ {
+ invgamma = 1.0 / gamma;
+ prescale /= 255.0f;
+ for (i = 0;i < 256;i++)
+ {
+ d = pow((double) i * prescale, invgamma) * scale + base;
+ adjusted = (int) (255.0 * d);
+ out[i] = bound(0, adjusted, 255);
+ }
+ }
+}
- vid_gamma = 1;
- if ((i = COM_CheckParm("-gamma")))
- vid_gamma = atof(com_argv[i+1]);
+void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
+{
+ int i, adjusted;
+ double invgamma, d;
- if (vid_gamma == 1) // LordHavoc: dodge the math
+ gamma = bound(0.1, gamma, 5.0);
+ if (gamma == 1) // LordHavoc: dodge the math
{
for (i = 0;i < 256;i++)
- qgamma[i] = i;
+ {
+ adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
+ out[i] = bound(0, adjusted, 65535);
+ }
}
else
{
+ invgamma = 1.0 / gamma;
+ prescale /= 255.0f;
for (i = 0;i < 256;i++)
{
- inf = pow((i+1)/256.0, vid_gamma)*255 + 0.5;
- if (inf < 0) inf = 0;
- if (inf > 255) inf = 255;
- qgamma[i] = inf;
+ d = pow((double) i * prescale, invgamma) * scale + base;
+ adjusted = (int) (65535.0 * d);
+ out[i] = bound(0, adjusted, 65535);
}
}
}
-void Palette_Init()
+qboolean hardwaregammasupported = false;
+void VID_UpdateGamma(qboolean force)
+{
+ static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
+ static int cacheoverbrightbits = -1, cachehwgamma = -1;
+
+ // LordHavoc: don't mess with gamma tables if running dedicated
+ if (cls.state == ca_dedicated)
+ return;
+
+ if (!force
+ && v_gamma.value == cachegamma
+ && v_contrast.value == cachecontrast
+ && v_brightness.value == cachebrightness
+ && v_overbrightbits.integer == cacheoverbrightbits
+ && v_hwgamma.value == cachehwgamma)
+ return;
+
+ if (v_gamma.value < 0.1)
+ Cvar_SetValue("v_gamma", 0.1);
+ if (v_gamma.value > 5.0)
+ Cvar_SetValue("v_gamma", 5.0);
+
+ if (v_contrast.value < 0.5)
+ Cvar_SetValue("v_contrast", 0.5);
+ if (v_contrast.value > 5.0)
+ Cvar_SetValue("v_contrast", 5.0);
+
+ if (v_brightness.value < 0)
+ Cvar_SetValue("v_brightness", 0);
+ if (v_brightness.value > 0.8)
+ Cvar_SetValue("v_brightness", 0.8);
+
+ cachegamma = v_gamma.value;
+ cachecontrast = v_contrast.value;
+ cachebrightness = v_brightness.value;
+ cacheoverbrightbits = v_overbrightbits.integer;
+
+ hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
+ if (!hardwaregammasupported)
+ {
+ Con_Printf("Hardware gamma not supported.\n");
+ Cvar_SetValue("v_hwgamma", 0);
+ }
+ cachehwgamma = v_hwgamma.integer;
+}
+
+void Gamma_Init(void)
+{
+ Cvar_RegisterVariable(&v_gamma);
+ Cvar_RegisterVariable(&v_brightness);
+ Cvar_RegisterVariable(&v_contrast);
+ Cvar_RegisterVariable(&v_hwgamma);
+ Cvar_RegisterVariable(&v_overbrightbits);
+}
+
+void Palette_Init(void)
{
- byte *pal;
- pal = (byte *)COM_LoadMallocFile ("gfx/palette.lmp", false);
+ qbyte *pal;
+ pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
if (!pal)
Sys_Error ("Couldn't load gfx/palette.lmp");
memcpy(host_basepal, pal, 765);
- qfree(pal);
+ Mem_Free(pal);
host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
Palette_Setup8to24();
// Palette_Setup15to8();
- Palette_Gamma();
}