sekrit-twc / zimg

Scaling, colorspace conversion, and dithering library
Do What The F*ck You Want To Public License
405 stars 77 forks source link

Color space conversion happens under premultiplied alpha #202

Open LoadingByte opened 1 year ago

LoadingByte commented 1 year ago

As far as I can infer from the code and have observed empirically, zimg premultiplies alpha before performing color space conversion. This has unexpected consequences when the conversion is nonlinear. For example, when we instruct zimg to convert the straight-alpha sample $[R, G, B, \alpha]$ from a gamma 2.2 TRC to a linear TRC, it performs the following steps:

This behavior caught me by surprise, and it seems strange and unintuitive. I don't feel like the alpha should creep into the chroma when just converting color spaces. And to further increase the confusion, the amount of alpha that creeps in depends on the color spaces between which you're converting.

I'm not an expert in the field though, so I wanted to ask you: is this intended behavior, or a bug?

LoadingByte commented 1 year ago

After some more reading, all sources indeed seem to agree that color space conversion should happen under straight alpha. So I think I'm not going insane.

Still, I wonder why no one has reported this bug in zimg before, especially seeing zimg's popularity (thanks for the amazing library btw!). Maybe it's a regression? I tested on 3.0.4, but looking at the code, the problem is still there in 3.0.5 and the latest master HEAD.