Skip to content

Commit 63e4422

Browse files
committed
Cleaned up the old gamma table adjustment code a little.
1 parent b554c7c commit 63e4422

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

source/arm11/open_agb_firm.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,19 @@ static Result loadGbaRom(const char *const path, u32 *const romSizeOut)
158158

159159
static void adjustGammaTableForGba(void)
160160
{
161-
const float gbaGamma = g_oafConfig.gbaGamma;
162-
const float lcdGamma = g_oafConfig.lcdGamma;
163-
const float contrast = g_oafConfig.contrast;
164-
const float brightness = g_oafConfig.brightness;
161+
// Credits for this algo go to Extrems.
162+
const float targetGamma = g_oafConfig.gbaGamma;
163+
const float lcdGamma = 1.f / g_oafConfig.lcdGamma;
164+
const float contrast = g_oafConfig.contrast;
165+
const float brightness = g_oafConfig.brightness / contrast;
166+
const float contrastInTargetGamma = powf(contrast, targetGamma);
165167
for(u32 i = 0; i < 256; i++)
166168
{
167-
// Credits for this algo go to Extrems.
168-
// Originally from Game Boy Interface Standard Edition for the GameCube.
169-
u32 res = powf(powf(contrast, gbaGamma) * powf((float)i / 255.0f + brightness / contrast, gbaGamma),
170-
1.0f / lcdGamma) * 255.0f;
169+
// Adjust i with brightness and convert to target gamma.
170+
const float adjusted = powf((float)i / 255 + brightness, targetGamma);
171+
172+
// Apply contrast, convert to LCD gamma and clamp.
173+
const u32 res = clamp_s32(powf(contrastInTargetGamma * adjusted, lcdGamma) * 255, 0, 255);
171174

172175
// Same adjustment for red/green/blue.
173176
REG_LCD_PDC0_GTBL_FIFO = res<<16 | res<<8 | res;

0 commit comments

Comments
 (0)