[cairo] render in grayscale

Kalle Vahlman kalle.vahlman at gmail.com
Tue Sep 22 09:10:03 PDT 2009

2009/9/22 Per Hermansson <hermansson.per at bredband.net>:
> 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);
> cairo_paint(cr);

clear to black

> pattern = cairo_pattern_create_rgb(1, 1, 1);

create an opaque white pattern

> cairo_set_source_surface(cr, target, 0, 0);

source is the poppler-rendered image...

> cairo_mask(cr, pattern);

...and this paints it using the fully opaque white pattern as the
mask, in effect blocking everything from the source.

What you need to do is reverse the operation, ie. use the opaque white
pattern as the source:

  cairo_set_source_rgb(cr, 1, 1, 1);

and use the poppler-rendered alpha surface as the mask:

  pattern = cairo_pattern_create_for_surface(target);
  cairo_mask(cr, pattern);

Kalle Vahlman, zuh at iki.fi
Powered by http://movial.com
Interesting stuff at http://sandbox.movial.com
See also http://syslog.movial.fi

More information about the cairo mailing list