google / wuffs

Wrangling Untrusted File Formats Safely
Other
4.06k stars 129 forks source link

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

Open HSNB opened 3 months ago

HSNB commented 3 months ago

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 commented 3 months ago

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.