accessing a SkCanvas' pixels' color at given coordinates

361 views
Skip to first unread message

grun...@gmail.com

unread,
Dec 18, 2015, 9:04:34 AM12/18/15
to skia-discuss
Hi,

how is it possible to find out for SkCanvas, which color a specific pixel has?
Is this also possible for canvas, that use Layers or shaders to get the end-result color after putting everything together?

Mike Reed

unread,
Dec 18, 2015, 9:17:34 AM12/18/15
to skia-d...@googlegroups.com
canvas->readPixels(...)

You can restrict the area to just one pixel if you like

--
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.

grun...@gmail.com

unread,
Dec 18, 2015, 12:11:59 PM12/18/15
to skia-discuss
The code below works for a not GPU backed canvas, but not for a canvas backed by EGL.
Any ide what could have gone wrong?

canvas->drawColor(0xFF0000FF);
canvas->drawPoint(100,100,0xFFFF0000);

canvas->flush();
// for GPU version additionally eglSwapBuffers(...)
int pixel;
canvas->readPixels(SkImageInfo::MakeN32Premul(1, 1),&pixel,4,100,100);

printf("pixel: %x\n",pixel);
// for non-GPU it prints 0xffff0000, for GPU 0xff0000ff



Cary Clark

unread,
Dec 18, 2015, 12:29:51 PM12/18/15
to skia-d...@googlegroups.com
Drawing at 100,100 is drawing at a location on the pixel grid, which is equidistant from 4 pixel centers. While Skia's raster engine defaults to drawing the pixel center that is to the left and down (i.e., the pixel center at 100.5, 100.5)), various OpenGL drivers round differently.

Try drawing at 100.5,100.5, the pixel center, to see if that gives you consistent results.

grun...@gmail.com

unread,
Jan 15, 2016, 12:02:05 PM1/15/16
to skia-discuss
Drawing at 100,100 is drawing at a location on the pixel grid, which is equidistant from 4 pixel centers. While Skia's raster engine defaults to drawing the pixel center that is to the left and down (i.e., the pixel center at 100.5, 100.5)), various OpenGL drivers round differently.

Try drawing at 100.5,100.5, the pixel center, to see if that gives you consistent results.
Drawing at  100.5,100.5 (and reading from 100,100) does work, but I cannot influenze, where the pixels are drawn,
just from where there are read.

After some trying out, it seems to be sufficient to substract 1 from the coordinates read (0.5 would also do).
canvas->readPixels(SkImageInfo::MakeN32Premul(1, 1),&pixel,4,100-1,100-1); // (also works with any other coordinates when neither X nor Y are 0)

Also drawing a pixel to 0/0 is not visible, but drawing it to 640/480 is, so it looks like an off by one error (screen resolution is
640x480).

Reply all
Reply to author
Forward
0 new messages