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

PNG's are stored in RGB order but Wuffs returns BGR/BGRA? #141

Open
HSNB opened this issue Mar 23, 2024 · 1 comment
Open

PNG's are stored in RGB order but Wuffs returns BGR/BGRA? #141

HSNB opened this issue Mar 23, 2024 · 1 comment

Comments

@HSNB
Copy link

HSNB commented Mar 23, 2024

According to the PNG spec: Pixels are always stored in RGBA order:

Pixels are always stored in RGBA order

However Wuffs reports the pixel format as BGR(A).

Am I missing something here or is performance lost by swizzling? 😯

@nigeltao
Copy link
Collaborator

nigeltao commented Mar 23, 2024

Technically, yes, but:

  1. I'd expect it to be very cheap, especially compared to the time spent in DEFLATE decompression. Swizzling is very friendly to SIMD acceleration.
  2. If your PNG is 3-channel (no alpha) but your display's native format is 4-channel then you're going to have to swizzle anyway: "convert RGB to RGBA" and "convert RGB to BGRA" is exactly the same SIMD code, just with a different byte-swap pattern. Or if your PNG is 4-channel RGBA but your display is 4-channel BGRA then, again, you're going to have to swizzle anyway.
  3. In the Wuffs API, the caller (application code), not the callee (Wuffs library), creates the pixel buffer. The app can override the suggested imgcfg.pixcfg.pixfmt when they create that pixel buffer, if they really want to.

On the other hand, Wuffs' standard library isn't just a PNG decoder. It can decode multiple image file formats (all with the same API) and it's simpler for the API caller if Wuffs standardizes on one order (independent of the actual file format) and we've chosen the one that Win32-x86 uses: BGRA.

nigeltao added a commit that referenced this issue Mar 27, 2024
PNG (as a source pixel format) is RGB or RGBA order. Wuffs' std/png
benchmarks, like the other image formats, decode to BGRA order. *If*
those benchmarks are altered to target RGBA instead, we get:

name                                                       old speed     new speed     delta

wuffs_png_decode_image_40k_24bpp/clang14                   279MB/s ± 0%  293MB/s ± 0%  +5.11%  (p=0.008 n=5+5)
wuffs_png_decode_image_4002k_24bpp/clang14                 289MB/s ± 0%  303MB/s ± 0%  +4.89%  (p=0.008 n=5+5)

wuffs_png_decode_image_40k_24bpp/gcc12                     311MB/s ± 0%  332MB/s ± 0%  +6.84%  (p=0.008 n=5+5)
wuffs_png_decode_image_4002k_24bpp/gcc12                   325MB/s ± 0%  339MB/s ± 0%  +4.49%  (p=0.008 n=5+5)

When starting from 3-channel RGB, ending with RGBA is now as fast as
ending with BGRA.

Updates #141
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants