Bug 237082 - createImageBitmap cannot premultiply alpha with ImageData
Summary: createImageBitmap cannot premultiply alpha with ImageData
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Canvas (show other bugs)
Version: Safari 15
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Matt Woodrow
URL:
Keywords: InRadar
Depends on:
Blocks: 182424
  Show dependency treegraph
 
Reported: 2022-02-23 06:00 PST by Ashley Gullen
Modified: 2023-10-27 04:37 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ashley Gullen 2022-02-23 06:00:15 PST
Summary: when calling createImageBitmap() with an ImageData and the option premultiplyAlpha: "premultiply", the resulting ImageBitmap still does not have premultiplied alpha. It appears to work correctly with other overloads using Blob, Image and Canvas.

Repro URL: https://downloads.scirra.com/labs/bugs/imagebitmapalpha/

The demo uses createImageBitmap() premultiplying alpha on Blob, Image, Canvas and ImageData. It uses a WebGL context to read the ImageBitmap pixel data back, via texture upload and readback.

In the ImageData case, the texture pixel data still uses unpremultiplied alpha. Look in the console of the repro page and it will log pass/fail results. The ImageData case fails with pixel data [0, 0, 255, 44], with the expected result [0, 0, 44, 44].
Comment 1 Radar WebKit Bug Importer 2022-02-23 15:36:46 PST
<rdar://problem/89382358>
Comment 2 Brent Fulgham 2023-04-14 09:13:55 PDT
I initially though there might have been a mistake in the optimized code path for premultiplied alpha, but I tried the test case with the slow byte-wise math and the results are identical.
Comment 3 Brent Fulgham 2023-04-14 10:56:34 PDT
Source is unpremultiplied RGBA8, destination for createImageBitmap backing store is premultiplied BGRA8. Colorspace is unchanged.

Although the test case talks about [12, 12], the math actually gives you [12, 3].

Inputs to vImagePremultiplyData_RGBA8888 look correct, e.g. the pixel at location 12, 3 in the source buffer is: 

00 00 FF 2C

The result of premultiplying is:

00 00 2C 2C

Which is correct.
Comment 4 Brent Fulgham 2023-04-14 13:29:31 PDT
The RGBA8 is then permuted to BGRA to go into the IOSurface:

00 00 2C 2C

Becomes

2C 00 00 2C

So the image buffer is properly constructed.

I think the issue is when this buffer is used to populate the textImage (WebGLRenderingContextBase::texImageSource) doesn't have the context that the image data is premultiplied; it looks like it rolls the premultiplication out of the buffer before loading into the texture.
Comment 5 Brent Fulgham 2023-04-17 17:21:32 PDT
Pull request: https://github.com/WebKit/WebKit/pull/12834
Comment 6 Brent Fulgham 2023-04-18 11:33:32 PDT
It doesn't look like WPT has any coverage for createImageBitmap with 'premultiply' set.
Comment 7 Matt Woodrow 2023-04-18 16:25:34 PDT
Pull request: https://github.com/WebKit/WebKit/pull/12891
Comment 8 EWS 2023-04-19 10:59:24 PDT
Committed 263137@main (aa1ea27b114e): <https://commits.webkit.org/263137@main>

Reviewed commits have been landed. Closing PR #12891 and removing active labels.