[cairo] render in grayscale

Per Hermansson hermansson.per at bredband.net
Tue Sep 22 00:56:09 PDT 2009

Behdad Esfahbod wrote:
> On 09/22/2009 03:01 AM, Per Hermansson wrote:
>> Hi
>> I'm using cairo to render a GTK+ widget (using gdk_cairo_create). The
>> rendering is partially done using an external library routine (which I
>> can't change).
>> Sometimes I want the widget to be rendered in grayscale.
>> Is it possible to modifiy cairo before or after the library has done the
>> rendering so that the output appears in gray scale?
>> Per
> You can create an alpha-only temporary surface 
> (cairo_surface_create_similar with content=CAIRO_CONTENT_ALPHA), 
> render to it, and cairo_mask() that with a white rgb pattern on a 
> black background.
> behdad
Thanks for the help and quick reply, I only get a black area when I try 
the code below:

cr = gdk_cairo_create(widget);
target = cairo_surface_create_similar(cairo_get_target(cr), 
CAIRO_CONTENT_ALPHA, width, height);
cr_tmp = cairo_create(target);

poppler_page_render(page, cr_tmp);

cairo_set_source_rgb(cr, 0, 0, 0);
pattern = cairo_pattern_create_rgb(1, 1, 1);
cairo_set_source_surface(cr, target, 0, 0);
cairo_mask(cr, pattern);


More information about the cairo mailing list