-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minor LodePNG optimizations #173
base: master
Are you sure you want to change the base?
Conversation
Also correct typo and update build command to link SDL2 and not SDL
@@ -3256,33 +3256,28 @@ static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, | |||
if(mode->colortype == LCT_GREY) { | |||
if(mode->bitdepth == 8) { | |||
*r = *g = *b = in[i]; | |||
if(mode->key_defined && *r == mode->key_r) *a = 0; | |||
else *a = 255; | |||
*a = 255 * !(mode->key_defined && *r == mode->key_r); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Why use multiplication where it is unnecessary?:
*a = ((mode->key_defined) && (*r == mode->key_r)) ? 0u : 255u;
Or use array?:
unsigned char wb[2] = {255, 0);
...
*a = wb[(int)((mode->key_defined) && (*r == mode->key_r))];
(for const time).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each conversion from if-else statement to multiplication in this case removes one jump instruction, creating code that has a few less branches and is also slightly smaller
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Encoding speeds up by ~1% when compiled with -O3 on Clang 15.0.0
LodePNG code size is decreased by about 1.5kB when compiled with MSYS2 CLANG64 Clang 15.0.0
Decoding (RGB and non-alpha grey colortypes) and encoding (any colortype) should be about 1-2% faster
A warning is fixed in lodepng_benchmark.cpp so it can successfully be compiled with the recommended build command