@@ -158,16 +158,19 @@ static Result loadGbaRom(const char *const path, u32 *const romSizeOut)
158
158
159
159
static void adjustGammaTableForGba (void )
160
160
{
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 );
165
167
for (u32 i = 0 ; i < 256 ; i ++ )
166
168
{
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 );
171
174
172
175
// Same adjustment for red/green/blue.
173
176
REG_LCD_PDC0_GTBL_FIFO = res <<16 | res <<8 | res ;
0 commit comments