New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 650110 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner:
Closed: Sep 2016
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Windows
Pri: 2
Type: Bug



Sign in to add a comment

context.drawImage has some incorrect pixel data

Reported by theclowe...@gmail.com, Sep 25 2016

Issue description

UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36

Steps to reproduce the problem:
1. Draw an image to canvas using ctx.drawImage with image from file
2. Right click the canvas and save the image
3. Compare a hash of the real image and the image saved from canvas
4. Demo here: http://clowerweb.com/picoder-test/ (at the bottom of the page, the right side image is from disk (<img>), and the left side image is that same image rendered to canvas)

What is the expected behavior?
The images should have the exact same pixel data.

What went wrong?
The images don't have the exact same pixel data. There are ~10 pixels that are incorrect.

Did this work before? N/A 

Chrome version: 53.0.2785.116  Channel: stable
OS Version: 10.0
Flash Version: Shockwave Flash 23.0 r0
 
celeste-img-tag.png
34.7 KB View Download
celeste-canvas.png
34.1 KB View Download

Comment 1 by kojii@chromium.org, Sep 26 2016

Components: -Blink Blink>Canvas
Cc: junov@chromium.org
Owner: xidac...@chromium.org
Status: Assigned (was: Unconfirmed)
theclowerweb@:

I used OpenCV to exam these two images, and I found that for those pixels whose values are different, their alpha channel is 0. 

In that case, the behavior is expected. When drawing an image to canvas, the canvas will always generate an image buffer that is premultiplied by alpha. Since those pixels has alpha channel to be 0, the r, g, b value will be lost during the premultiplication.

Does that make sense?
Hi, and thanks for the explanation! Do you know of a workaround, or a way to extract the correct values? Is there a way to turn off the premultiplication, or tell my algorithm that if the alpha is 0, do something to get the correct rgb? I've tried getting the RGBA from PHP instead, but PHP has a weird 7-bit alpha value (0-127, where 127 is fully off and 0 is fully on), and I don't think there's a way to convert it to an accurate 8-bit value...
For example, a way to say: if the alpha is 0, get just the RGB without alpha?
Unfortunately I cannot think of anything with canvas. Maybe using some kind of pre-processing technique on the image, and change alpha value from 0-->255 before uploading the image to canvas?
The other possibility is to use WebGL's texture upload function:
https://developer.mozilla.org/en/docs/Web/API/WebGLRenderingContext/texImage2D

TexImage2D will preserve the alpha channel, but I am not sure if that works for your app or not.

Comment 7 by junov@chromium.org, Sep 28 2016

If you used WebGL instead of 2D canvas, you would have all the knobs you need.

Thanks @xidac and @junov - I'll give the WebGL method a try!
Status: WontFix (was: Assigned)
closing this bug for now. Thanks.

Sign in to add a comment