Drawing Grayscale Images as colored transparency

435 views
Skip to first unread message

sha...@infograph.com

unread,
May 18, 2016, 3:58:00 PM5/18/16
to skia-discuss
I have monochrome / grayscale images that need to be rendered in a special way.  Where the 8bit value of 255 draws as some color (with complete opacity) specified at render-time, while 0 means to draw the color with complete transparency. And all the shades of gray in between specify the appropriate degree of transparency.
Example using purple:

I tried building my images with the color type "kAlpha_8_SkColorType" and it seemed to be performing the behavior I wanted.  

However, it wasn't obeying the SkPaint's setFilterQuality() with a value of kMedium_SkFilterQuality or better.  There was no bilinear interpolation. No mip-mapping being done. Looked like nearest-neighbor.

So, I tried the experiment using kGray_8_SkColorType instead.  This obeyed the filter quality value, but drew opaque shades of gray.

Is there some kind of color filter or image filter that could produce the effect I was going for?
Is there a way to enable higher filter quality on alpha-8 images?

Jeremy Roman

unread,
May 18, 2016, 5:08:40 PM5/18/16
to skia-d...@googlegroups.com
(Disclaimer: I'm not on the Skia team.)

I assume you're using SkColorFilter::MakeModeFilter for this effect? You didn't specify.

When I try to construct roughly your example (except with a circle-in-square stamp), I do get what seems to be the right effect with N32, at least, and of course using saveLayer to avoid explicitly making a separate bitmap (Skia may make one when drawing, but it knows more about the CTM, etc., at that time). I'm not sure what's up with Alpha_8 on the CPU; I'd also have thought you could use whatever source color type you please (as long as it has alpha).


This sounds like a bug in Skia to me.

--
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 post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

sha...@infograph.com

unread,
May 18, 2016, 6:23:12 PM5/18/16
to skia-discuss
Thanks for the the fiddle example.  I have altered it to show it better, by adding scale and using lines to show the sampling better.
I wasn't using SkColorFilter::MakeModeFilter, I was just specifying the color in SkPaint::setColor.
But in order to make the 32 bit version colorize the output, the MakeModeFilter is needed


Here is the 32 bit version. You can see how the line is bilinear filtered and, one assumes, mipmapped.

Here is the Alpha 8 version:


When I tried it with kGray_8_SkColorType, I got a runtime error in fiddle.

While it works ok in my code.


Another notable thing: the GPU rendering of the 32 bit version looked like the alpha 8 version. No quality filtering.

Brian Osman

unread,
May 19, 2016, 11:14:09 AM5/19/16
to skia-d...@googlegroups.com
Hi,
It looks like there may be a few different issues here:

  • The issue with Alpha8 not filtering on the CPU backend is definitely a bug. I've got a change to fix that, and it should land later today. (https://codereview.chromium.org/1988023009/).
  • The runtime error with Gray8 is because the code is explicitly creating a raster surface of the requested type (for the "stamp"). Unfortunately, we don't have CPU blitters for Gray8 (only N32, Alpha8, 565, and F16), although for what you're trying to do, Alpha8 with a color is probably the most correct/appropriate configuration of the pipeline.
  • The GPU 32-bit version is really strange. I ran that code locally, and it works correctly. I'm not sure what specific GPU is used on the machines that do the fiddle rendering, but I'm going to ask around here and see if that could be part of the problem.
-Brian

Brian Osman

unread,
May 19, 2016, 11:18:15 AM5/19/16
to skia-d...@googlegroups.com
... on that last point: The "GPU" fiddles are rendered using GPU emulation (Mesa), so the results are not necessarily accurate or representative.

sha...@infograph.com

unread,
May 19, 2016, 12:57:11 PM5/19/16
to skia-discuss
Thanks man!  I tried your fix on my local copy and Alpha8 works like a charm now:

Shawn Riordan

unread,
Jul 21, 2016, 2:08:10 PM7/21/16
to skia-discuss
Was this fix backed out?  Or has it not yet been accepted?
Reply all
Reply to author
Forward
0 new messages