invert SkImage

205 views
Skip to first unread message

Noel Grandin

unread,
Jul 1, 2023, 10:38:24 AM7/1/23
to skia-discuss
Hi

Newbie question - what is the best way to invert an SkImage? 

    i.e. dest = 1 - src

Thanks, Noel Grandin

craste...@gmail.com

unread,
Jul 1, 2023, 11:10:17 AM7/1/23
to skia-discuss
Do you want to draw it to the screen inverted?
Or do you want to write it to a file with the pixel values inverted?

John Stiles

unread,
Jul 1, 2023, 11:11:00 AM7/1/23
to skia-d...@googlegroups.com
You can do this pretty easily with a Runtime Effect:


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/5903ec65-56f3-4a79-9618-5994ae199a04n%40googlegroups.com.

Michael Katz

unread,
Jul 1, 2023, 6:34:17 PM7/1/23
to skia-discuss
Here's another Runtime Effect but with a custom blender instead of a custom shader:

craste...@gmail.com

unread,
Jul 1, 2023, 8:51:52 PM7/1/23
to skia-discuss
You can use the blendmode "kDifference" if the area being drawn to is white first.

Noel Grandin

unread,
Jul 3, 2023, 11:00:34 AM7/3/23
to skia-discuss
Thanks so much for all the help, that is very useful.

Noel Grandin

unread,
Jul 3, 2023, 11:00:49 AM7/3/23
to skia-discuss
On Saturday, July 1, 2023 at 5:10:17 PM UTC+2 craste...@gmail.com wrote:
Do you want to draw it to the screen inverted?
Or do you want to write it to a file with the pixel values inverted?


I'll be drawing it to the screen. 
I have an existing (single-channel) SkImage, and I want to invert it (because its transparency and I want it to be alpha or vice-versa).
Currently I read it back from the GPU, construct a new one in memory, and push it back to the GPU, which is obviously pretty hideous for performance.

PS. If you think this sounds pretty awful, you should see the rest of my graphics stack :-)
 

craste...@gmail.com

unread,
Jul 3, 2023, 3:10:19 PM7/3/23
to skia-discuss
I also imagine that there is a way to use a color filter with a transformation matrix that reverses the colors by translating the RGB values by 1 and then set the scalars to -1.
Though I have not tried it... And it wouldn't require that you draw the image on top of white.

Or you could use the shader, like in the first example.  Not sure which would be more straight-forward.

Reply all
Reply to author
Forward
0 new messages