Combine two RGB and alpha SKimage to use in DrawImage

523 views
Skip to first unread message

David Briard

unread,
Jun 8, 2023, 3:17:06 PM6/8/23
to skia-discuss
Hi, I have a opaque image, and a mask (alpha8) as separate image, to allow to enable/disabled mask easily.

I want do draw my image using the mask with  DrawImage but I am not sure what is the best way to combine them, so that there is no side-effects  with my paint that contains (blur, color filter, drop shadow, opacity, etc...).

I fact I want the same behavior as if the alpha was in the image in the first place.

How can I do that?

Thanks a lot!


John Stiles

unread,
Jun 8, 2023, 3:27:27 PM6/8/23
to skia-discuss
There are probably simpler ways, but you can always write a SkRuntimeShader to do a custom combination like this.

The website only allows a single image-shader input, so it's using the same image for the RGB data and the Alpha data. This just makes it look like an extra-dark version of the image. But it will work properly with two different images as well.
This should combine with any of our other shaders properly and work exactly as if you had a single image.


craste...@gmail.com

unread,
Jun 8, 2023, 5:02:08 PM6/8/23
to skia-discuss
Do you have access to the pixel values?  You could set the color image's alpha values based on the values in the Alpha8 mask.  And then throw away the mask.

David Briard

unread,
Jun 9, 2023, 9:56:56 AM6/9/23
to skia-d...@googlegroups.com
Thank you guys!
I finally found an easy way with ImageFilter and blending mode with SrcIn.
I am still new to Skia and will have a look to shader soon.

I still have questions about rgb and alpha if you don't mind?

I did not find how to do simple stuff such as converting Gray8 bitmap into Alpha8, or 
copying the Alpha8 image into the alpha channel of RGBA bitmap. So I am doing that manually with xy loop.
Is there build-in API to do that? 



--
You received this message because you are subscribed to a topic in the Google Groups "skia-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/skia-discuss/qldvidC4DQM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/561212c9-6eee-45b4-9e56-ad37b7b4e37fn%40googlegroups.com.


--
David

Brian Osman

unread,
Jun 9, 2023, 11:12:56 AM6/9/23
to skia-d...@googlegroups.com
No, there aren't a large set of APIs for doing those kinds of image-combining operations - most of the API is built around drawing operations. But you can often accomplish the same thing with a quick draw to a same-size surface and an appropriate paint (or custom shader). To just do a format conversion, you can use the readPixels interface on SkImage (as well as on SkSurface) to apply conversions like color space and color type. (SkImage also has a reinterpretColorSpaceAndColorType that doesn't do any conversion -- it just pretends that the data is actually in a different format).

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/CAKQGZVZWuwscMQZdDtvZ19zqf-pYtTBNK%3DQf88VG2oJ%2B8wNUGA%40mail.gmail.com.

K. Moon

unread,
Jun 9, 2023, 11:54:49 AM6/9/23
to skia-d...@googlegroups.com
Is there a reinterpretColorSpaceAndColorType()? I only see a reinterpretColorSpace() right now. There are some makeColorTypeAndColorSpace() methods, but they're marked as experimental or legacy. In general, it seems like SkImage makes it hard to reinterpret the color type.

craste...@gmail.com

unread,
Jun 10, 2023, 12:06:26 AM6/10/23
to skia-discuss
If you want to convert a color SkImage with a grayscale / Alpha8 image into a single, new, SkImage where the alpha bits of the new image are specified by the pixels in the grayscale image, then something like this could work:

- Create a CPU surface and give it the same dimensions as the color image supplied.
- Get the SkCanvas from that surface.
- Initialize / fill it with SK_COLOR_TRANSPARENT (or whatever it is called)
- set the current matrix to identity (it probably starts out that way anyway)
- drawImage(the_color_image)
- drawImage(the_gray_image with blendmode SrcIn) (this would manipulate the alpha channel in the surface)
- grab an SkImage by calling SkSurface::makeImageSnapshot()

Now you will have a new image that contains a color image with an alpha channel derived by the alpha8 image. You can throw away the original color and alpha8 images and use this new image instead.

This is not nearly the fastest way to do that, but I think it would work.

Brian Osman

unread,
Jun 10, 2023, 10:03:19 AM6/10/23
to skia-d...@googlegroups.com
Sorry, I was confusing two different APIs -- there is no way to reinterpret color type (that would be a strange operation, and most backends wouldn't support it). There is makeColorTypeAndColorSpace for doing both kinds of conversions at the same time, though.

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/cb07b9a9-5a3d-4a0a-bfd2-8547d80b144cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages