Subject: Re: bug#59242: 29.0.50; Poor quality for WebP images

From: Po Lu <[email-masked]>
Date: Sun, 13 Nov 2022 17:44:22 +0800

Evgeny Zajcev <[email-masked]> writes:

> WebP images with alpha channel renders in poor quality in the Emacs.
> See http://lgarc.narod.ru/pics/emacs-webp-poor-quality.png
>
> If I convert webp image to png, quality is ok in resulting image.
> Also, if I add webp as native image under MacOS (requires Emacs patch)
> then quality is also ok.
>
> Looks like partial transparency is incorrectly applied for webp images.

Stefan, I see the following code in image.c:

	  /* An alpha channel associates variable transparency with an
	     image.  WebP allows up to 256 levels of partial transparency.
	     We handle this like with PNG (which see), using the frame's
	     background color to combine the image with.  */
	  if (features.has_alpha || anim)
	    {
	      if (mask_img)
		PUT_PIXEL (mask_img, x, y, *p > 0 ? PIX_MASK_DRAW : PIX_MASK_RETAIN);

however, you seem to have misunderstood the meaning of the PNG code: PNG
images can either have a real alpha channel (where the PNG library does
the blending AFAIK) or a simple on-off transparency switch, which is the
only case where this kind of masking is appropriate.

As a questionable optimization, Emacs also uses masking when only alpha
channels of 0 or 255 are present in a PNG image that has a real alpha
channel.

So if the webp library does not blend itself, I think we will have to do
that for it.  Am I missing anything here?