Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions lodepng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

@zvezdochiot zvezdochiot Sep 18, 2022

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).

Copy link
Author

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this out with godbolt.org, and it looks like the way you write it doesn't matter, the effect is equivalent and it gets compiled to the same thing:

image

So the original with if/else looks more readable I'd say

} else if(mode->bitdepth == 16) {
*r = *g = *b = in[i * 2 + 0];
if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
else *a = 255;
*a = 255 * !(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r);
} else {
unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
size_t j = i * mode->bitdepth;
unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
*r = *g = *b = (value * 255) / highest;
if(mode->key_defined && value == mode->key_r) *a = 0;
else *a = 255;
*a = 255 * !(mode->key_defined && value == mode->key_r);
}
} else if(mode->colortype == LCT_RGB) {
if(mode->bitdepth == 8) {
*r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2];
if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0;
else *a = 255;
*a = 255 * !(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b);
} else {
*r = in[i * 6 + 0];
*g = in[i * 6 + 2];
*b = in[i * 6 + 4];
if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
*a = 255 * !(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
&& 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
&& 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
else *a = 255;
&& 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b);
}
} else if(mode->colortype == LCT_PALETTE) {
unsigned index;
Expand Down Expand Up @@ -3337,21 +3332,21 @@ static void getPixelColorsRGBA8(unsigned char* LODEPNG_RESTRICT buffer, size_t n
if(mode->key_defined) {
buffer -= numpixels * num_channels;
for(i = 0; i != numpixels; ++i, buffer += num_channels) {
if(buffer[0] == mode->key_r) buffer[3] = 0;
buffer[3] *= !(buffer[0] == mode->key_r);
}
}
} else if(mode->bitdepth == 16) {
for(i = 0; i != numpixels; ++i, buffer += num_channels) {
buffer[0] = buffer[1] = buffer[2] = in[i * 2];
buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255;
buffer[3] = 255 * !(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r);
}
} else {
unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
size_t j = 0;
for(i = 0; i != numpixels; ++i, buffer += num_channels) {
unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255;
buffer[3] = 255 * !(mode->key_defined && value == mode->key_r);
}
}
} else if(mode->colortype == LCT_RGB) {
Expand All @@ -3363,18 +3358,18 @@ static void getPixelColorsRGBA8(unsigned char* LODEPNG_RESTRICT buffer, size_t n
if(mode->key_defined) {
buffer -= numpixels * num_channels;
for(i = 0; i != numpixels; ++i, buffer += num_channels) {
if(buffer[0] == mode->key_r && buffer[1]== mode->key_g && buffer[2] == mode->key_b) buffer[3] = 0;
buffer[3] *= !(buffer[0] == mode->key_r && buffer[1]== mode->key_g && buffer[2] == mode->key_b);
}
}
} else {
for(i = 0; i != numpixels; ++i, buffer += num_channels) {
buffer[0] = in[i * 6 + 0];
buffer[1] = in[i * 6 + 2];
buffer[2] = in[i * 6 + 4];
buffer[3] = mode->key_defined
buffer[3] = 255 * !(mode->key_defined
&& 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
&& 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
&& 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255;
&& 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b);
}
}
} else if(mode->colortype == LCT_PALETTE) {
Expand Down Expand Up @@ -3497,17 +3492,15 @@ static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned s
const unsigned char* in, size_t i, const LodePNGColorMode* mode) {
if(mode->colortype == LCT_GREY) {
*r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1];
if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
else *a = 65535;
*a = 65535 * !(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r);
} else if(mode->colortype == LCT_RGB) {
*r = 256u * in[i * 6 + 0] + in[i * 6 + 1];
*g = 256u * in[i * 6 + 2] + in[i * 6 + 3];
*b = 256u * in[i * 6 + 4] + in[i * 6 + 5];
if(mode->key_defined
*a = 65535 *!(mode->key_defined
&& 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
&& 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
&& 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
else *a = 65535;
&& 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b);
} else if(mode->colortype == LCT_GREY_ALPHA) {
*r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1];
*a = 256u * in[i * 4 + 2] + in[i * 4 + 3];
Expand Down