Created
March 4, 2012 10:12
-
-
Save lilliemarck/1971905 to your computer and use it in GitHub Desktop.
Color depth reduction without floating point math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The bit reduction formulas converts the colors to give a minimal error after converting back to 8 bit. The sum of the signed errors is 0 and the sum of absolute diffs are as small as possible. | |
The formulas that convert back to 8 bit are done using bit shifting which introduces a rounding error for some color values. However bit shifting creates nice symmetric patterns and seems to be how hardware does it. | |
8 bit -> 4 bit -> 8 bit | |
c4 = (c8 + 8) / 17 | |
c8 = c4 << 4 | c4 | |
8 bit -> 5 bit -> 8 bit | |
c5 = (31 * c8 + 127) / 255 | |
c8 = c5 << 3 | c5 >> 2 | |
8 bit -> 6 bit -> 8 bit | |
c6 = (63 * c8 + 127) / 255 | |
c8 = c6 << 2 | c6 >> 4; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment