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

Add an option to decode to a premultiplied form #271

Open
jrmuizel opened this issue Dec 9, 2020 · 6 comments
Open

Add an option to decode to a premultiplied form #271

jrmuizel opened this issue Dec 9, 2020 · 6 comments

Comments

@jrmuizel
Copy link
Contributor

jrmuizel commented Dec 9, 2020

Consumers of PNG images often want the alpha premultiplied. See jrmuizel/raqote#167

@fintelia
Copy link
Contributor

fintelia commented Dec 9, 2020

If you want premultiplied alpha, isn't the right approach to apply it yourself before saving the PNG?

@jrmuizel
Copy link
Contributor Author

jrmuizel commented Dec 9, 2020

No, PNGs are stored using unpremultiplied alpha. Premultiplying before saving will cause other png decoders to apply the alpha twice and won't help with existing images.

@jrmuizel jrmuizel changed the title Decode to a premultiplied form Add an option to decode to a premultiplied form Dec 9, 2020
@jrmuizel
Copy link
Contributor Author

jrmuizel commented Dec 9, 2020

I've clarified the title to make it clearer that this would probably be an optional output transformation.

@HeroicKatora
Copy link
Member

There are at least two case where applying the transformation at decode time, instead of later, is advantageous compared to a post-processing step after the full buffer is decoded. There is no general benefit however, so their implementation mustn't have too much of a complexity overhead.

  • For images with a palette it can be performed on the palette which is possibly faster.
  • For images with tRNS chunks the alpha bit is a single bit. Applying a masking SIMD-AND might be faster than the equivalent full multiplications.

@jrmuizel
Copy link
Contributor Author

Applying at decode time can also avoid cache thrashing on large images.

@yanchith
Copy link

Sorry for the noob question: If png pre-multiplies alpha when decoding, how should this interact with gamma/sRGB? Should the pre-multiply happen in linear space? If so, would png have to gamma-decode, pre-multiply, and then gamma-encode again?

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

4 participants