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

optimize RGBA to BGRA conversion #234

Merged
merged 1 commit into from
Sep 23, 2023

Conversation

qbnu
Copy link
Contributor

@qbnu qbnu commented Sep 3, 2023

Speed up RGBA -> BGRA conversion by ~25% for JXL and ~150% for HEIF.

Slow:
((value & 0x00FF0000) >> 16) | ((value & 0x000000FF) << 16) | (value & 0xFF00FF00);

Faster:
_rotr(value & 0x00FF00FF, 16) | (value & 0xFF00FF00);
(((value >> 16) | (value << 16)) & 0x00FF00FF) | (value & 0xFF00FF00);
((_byteswap_ulong(value) >> 8) & 0x00FF00FF) | (value & 0xFF00FF00);

Fastest:
_rotr(_byteswap_ulong(value), 8);
_byteswap_ulong(_rotl(value, 8));

I also got rid of the unnecessary memcpys.

For me this saves 50-100ms for large files.

*i = ((*i & 0x00FF0000) >> 16) | ((*i & 0x0000FF00)) | ((*i & 0x000000FF) << 16) | ((*i & 0xFF000000));
uint32_t* data = (uint32_t*)pixels.data();
for (int i = 0; i * sizeof(uint32_t) < size; i++) {
((uint32_t*)pPixelData)[i] = _rotr(_byteswap_ulong(data[i]), 8);
Copy link
Owner

Choose a reason for hiding this comment

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

This is an amazing optimization. wow.

o += nchannels;
*o = _rotr(_byteswap_ulong(*p), 8);
p++;
o++;
Copy link
Owner

Choose a reason for hiding this comment

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

I've read (possibly incorrectly) that preincrement is faster than postincrement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the compiler will optimize it anyway

sylikc added a commit that referenced this pull request Sep 23, 2023
Speed up RGBA -> BGRA conversion by ~25% for JXL and ~150% for HEIF

Merge PR #234 by https://github.com/qbnu
@sylikc sylikc merged commit fdd8da1 into sylikc:master Sep 23, 2023
4 checks passed
@sylikc sylikc added the format support Related to add/remove/change of a specific format support. label Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format support Related to add/remove/change of a specific format support.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants