context.drawImage has some incorrect pixel data
Reported by
theclowe...@gmail.com,
Sep 25 2016
|
|||
Issue descriptionUserAgent: 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
,
Sep 26 2016
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?
,
Sep 28 2016
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...
,
Sep 28 2016
For example, a way to say: if the alpha is 0, get just the RGB without alpha?
,
Sep 28 2016
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?
,
Sep 28 2016
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.
,
Sep 28 2016
If you used WebGL instead of 2D canvas, you would have all the knobs you need.
,
Sep 28 2016
Thanks @xidac and @junov - I'll give the WebGL method a try!
,
Sep 28 2016
closing this bug for now. Thanks. |
|||
►
Sign in to add a comment |
|||
Comment 1 by kojii@chromium.org
, Sep 26 2016