VOGONS


First post, by kekko

User metadata
Rank Oldbie
Rank
Oldbie

I've some games a bit too dark that don't support native gamma
correction; so i wrote some modifications to render.cpp to increase
gamma. Is a fast modification so is far from perfect.
I did it for myself, but if anyone is interested this is the code to add:

***************************
//addition to render structure
[..]
} frameskip;

struct {
Bitu count;
Bitu max;
} gamma;

PalData pal;
[..]

***************************

//addition to Check_Palette
[..]
Bit8u b=render.pal.rgb.blue;

// adds gamma to channels
if (r+render.gamma.count>255) r=255; else r+=render.gamma.count;
if (g+render.gamma.count>255) g=255; else g+=render.gamma.count;
if (b+render.gamma.count>255) b=255; else b+=render.gamma.count;

render.pal.lookup.bpp32=GFX_GetRGB(r,g,b);
[..]


***************************

// added new functions IncreaseGamma and DecreaseGamma

static void IncreaseGamma(void) {
if (render.gamma.count<render.gamma.max) {
render.gamma.count++;
LOG_MSG("Gamma at %d",render.gamma.count);
for (Bitu i=0;i<=255;i++) {
Bit8u r=render.pal.rgb.red;
Bit8u g=render.pal.rgb.green;
Bit8u b=render.pal.rgb.blue;
if (r+render.gamma.count>255) r=255; else r+=render.gamma.count;
if (g+render.gamma.count>255) g=255; else g+=render.gamma.count;
if (b+render.gamma.count>255) b=255; else b+=render.gamma.count;
render.pal.lookup.bpp32=GFX_GetRGB(r,g,b);
}
}
}

static void DecreaseGamma(void) {
if (render.gamma.count>0) {
render.gamma.count--;
LOG_MSG("Gamma at %d",render.gamma.count);
for (Bitu i=0;i<=255;i++) {
Bit8u r=render.pal.rgb.red;
Bit8u g=render.pal.rgb.green;
Bit8u b=render.pal.rgb.blue;
if (r+render.gamma.count>255) r=255; else r+=render.gamma.count;
if (g+render.gamma.count>255) g=255; else g+=render.gamma.count;
if (b+render.gamma.count>255) b=255; else b+=render.gamma.count;
render.pal.lookup.bpp32=GFX_GetRGB(r,g,b);
}
}
}

***************************

// additions to RENDER_Init

void RENDER_Init(Section * sec) {
Section_prop * section=static_cast<Section_prop *>(sec);

// added gamma initialization
render.gamma.count=0;
render.gamma.max=30;

render.pal.first=256;
render.pal.last=0;

[..]

KEYBOARD_AddEvent(KBD_f7,KBD_MOD_CTRL,DecreaseFrameSkip);
KEYBOARD_AddEvent(KBD_f8,KBD_MOD_CTRL,IncreaseFrameSkip);
GFX_SetTitle(-1,render.frameskip.max);

//added two new kb events ( ALT+F7 and ALT+F8 )
KEYBOARD_AddEvent(KBD_f7,KBD_MOD_ALT,DecreaseGamma);
KEYBOARD_AddEvent(KBD_f8,KBD_MOD_ALT,IncreaseGamma);

}

Reply 1 of 5, by canadacow

User metadata
Rank Member
Rank
Member

While this no doubt brightens the screen, this isn't true gamma correction. The formula for gamma correction is non-linear. I haven't got a lot of time, otherwise I'd explain further here. However, wikipedia and other Internet resources explain calculating gamma correction pretty clearly.

Reply 3 of 5, by miargep

User metadata
Rank Newbie
Rank
Newbie

I read the wikipedia.org article on "Gamma_correction".
So, here's my interpretation: let's say your r,g,b values are in variables named r,g,b (in range [0;255]) and your desired gamma correction value for each of the colors is gamma_r,gamma_g,gamma_b. Then,
you should draw a gamma corrected pixel with r_c,g_c,b_c values of: (with ** being the power function)
r_c = (r/255)**(1/gamma_r)*255;
g_c = (g/255)**(1/gamma_g)*255;
b_c = (b/255)**(1/gamma_b)*255;
this gives you a gamma corrected pixel. alternatively, you can substitute the gamma_{r|g|b}-value with only one gamma value (although I think three gamma-correction values are better than just one 😀 )