Skip to content

Commit 57850b8

Browse files
committed
Slightly improve dynamic range by expanding VGA palettes to the full 8-bit range
1 parent 870ea79 commit 57850b8

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/palette.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,18 @@ void JE_loadPals( void )
5151
{
5252
for (int i = 0; i < 256; ++i)
5353
{
54-
palettes[p][i].r = getc(f) << 2;
55-
palettes[p][i].g = getc(f) << 2;
56-
palettes[p][i].b = getc(f) << 2;
54+
// The VGA hardware palette used only 6 bits per component, so the values need to be rescaled to
55+
// 8 bits. The naive way to do this is to simply do (c << 2), padding it with 0's, however this
56+
// makes the maximum value 252 instead of the proper 255. A trick to fix this is to use the upper 2
57+
// bits of the original value instead. This ensures that the value goes to 255 as the original goes
58+
// to 63.
59+
60+
int c = getc(f);
61+
palettes[p][i].r = (c << 2) | (c >> 4);
62+
c = getc(f);
63+
palettes[p][i].g = (c << 2) | (c >> 4);
64+
c = getc(f);
65+
palettes[p][i].b = (c << 2) | (c >> 4);
5766
}
5867
}
5968

0 commit comments

Comments
 (0)