KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

Applying brighten filter changes opacity values as well

Open plrdev opened this issue 13 years ago • 10 comments

I have code that uses the new feature of tweening brightness of an image from 0 to 150 and back. It works quite nicely, except the process modifies alpha (opacity) values that make those pixels glaringly white and they stay that way even after brightness is set back to 0.

Attached is a photo to illustrate: brighten

Left side is the original, right side is with the filter applied. Notice the edges, that is the undesired result.

I checked inside KineticJS code and the actual brightness filter does not touch the alpha values of the pixels, so I am not sure where in the process of applying the filter they are changed. I observed one pixel, with the RGBA values of 0, 0, 0, 128 and setFilterBrightness(100) would set it to 100, 100, 100, 192. After next operation the alpha value will be 224. So increases of 64 and 32. After a while, alpha values go to 255 and are completely shown, causing the effect seen in the picture.

I have a working demo of this effect, but I couldn't get a jsfiddle working due to cross-origin picture data. If needed, I am more than happy to share the project and more details.

plrdev avatar May 21 '13 11:05 plrdev

I tried modifying the following tutorial to tween brightness, and it worked as expected:

http://www.html5canvastutorials.com/kineticjs/html5-canvas-tween-blur-filter-with-kineticjs/

Can you do the same and let me know if you can repo the problem?

Can you post an example on your own website showing the problem? Try to create the simplest demo (I won't have the time to look through a lot of code). As you've pointed out, the Brighten filter doesn't touch alphla values, so I can only suspect that something else is going on in your code.

ericdrowell avatar May 21 '13 14:05 ericdrowell

Thanks Eric for the quick response. Here is a quick and dirty example of what's happening:

http://www.oppimispeli.fi/kinetic_bright/

The forest tile is the actual effect I am going for. You can see the pixels with some opacity to stay white. The green rectangle is a slider to apply brighten effect (0-255) to 2x2 to pixels (scaled 20 times) and up in the left corner it logs the RGBA values of those pixels. On the right there is a gradient image, with the upper half using opacity and lower half using fading to background color. Here you see the effect the best: In the ideal situation, they would work the same. But as you can observe, the gradient part goes super bright and stays that way due to the opacity values getting out of hand. That's at least what I think is happening from observing this from the debugging. clearFilter() get's it back to normal, so I definitely think it's due to the filter, but I'm lost where that happens since the filter isn't touching alpha values.

Hope this helps, let me know if I can be of more help.

plrdev avatar May 21 '13 17:05 plrdev

Eric, did you get a chance to try my example? Sorry to ask again, I know you have your hands full, but it would really help to get confirmation of why this is happening and whether it's something that is gonna get fixed. This is blocking us to use images with alpha + brighten filter, which we use to highlight selection.

plrdev avatar May 30 '13 08:05 plrdev

Hi there! will try my best to look into this soon. As you might guess, I have hundreds of todo items on my plate at any moment in time. I do offer support licenses, if you're interested, which gets you dedicated help and priority bug fixes:

https://github.com/ericdrowell/KineticJS/wiki/Purchase-a-Support-License

stay tuned

ericdrowell avatar May 30 '13 16:05 ericdrowell

by the way, I've added a new Brighten filter tutorial here:

http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-brighten-or-darken-image-tutorial/

which doesn't seem to have any problems with opacity.

I'm closing this ticket for now because I'm just not able to reproduce it. I'll keep an eye out for this, and if I come across it, I'll reopen the ticket.

ericdrowell avatar Jun 13 '13 05:06 ericdrowell

Thank you for the quick example. Of course your example doesn't have problems with opacity, jpeg doesn't have any opacity! I copied the example exactly, added a black background and switched to our test image (png) and I am able to reproduce easily. I can attach the image here and you can see it for yourself easily.

gradients

Going from slider to right (255) and back to center (0) works as expected on the lower bar, since it doesn't have opacity. But the higher bar, which has opacity, gets messed up. Here is a screenshot of the end situation:

example

Both bars should look the same, but as you can see, they clearly are not.

Here is the code (your example with the added background and changed image reference):

http://notepad.cc/hoqoozbe13

plrdev avatar Jun 13 '13 07:06 plrdev

Thanks for the clarification! I didn't realize that the problem was specific to pngs. thank you!

ericdrowell avatar Jun 13 '13 15:06 ericdrowell

The filtered image was being drawn in a temporary canvas where it was previously drawn. With each draw, the alpha-values of the image increased until they hit 255 (effectively removing the alpha channel).

To fix it: I created a clear canvas each time the filter is applied. This fix should be in the latest commit.

ippo615 avatar Aug 24 '13 02:08 ippo615

ah interesting. Do you think it would be a better solution to just clear the existing filter canvas? I'm sure that will perform better than creating a new canvas each time.

ericdrowell avatar Aug 25 '13 06:08 ericdrowell

That was my first attempt - I used something like context.clearRect(0,0,width,height) but it didn't fix the problem. Perhaps I put that code in the wrong spot...

ippo615 avatar Aug 25 '13 14:08 ippo615