[cairo] Hi all and a couple questions...

Chris Wilson chris at chris-wilson.co.uk
Thu Oct 30 03:11:22 PDT 2008

On Thu, 2008-10-30 at 10:24 +0100, J.A. Magallón wrote:
> What I suspect I should do is to paint in white filtered by my grayscale
> alpha to get to the destination surface. But I can't get it right.
> My code is basicly like this:
>         // Clear the region to get some base color to draw on top
>         gdk_cairo_region(cr,ev->region);
>         //cairo_fill(cr);
> 	// set the source alpha, image() is A8
>         cairo_set_source_surface(cr,(cairo_surface_t*)grabber->image(),0,0);
>         // set the source color
>         cairo_set_operator(cr,CAIRO_OPERATOR_OVER);
>         cairo_paint(cr);
> The above code paints the image in negative.
> What should I use as base color/alpha to fill the surface, and what
> operator to paint the image the right way ?

If you change the operator to CAIRO_OPERATOR_DEST_OVER, that should work
as intended. The way I would do it, is to use the A8 image as a mask:
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_mask_surface (cr, grabber->image(), 0, 0);

> And other question is: How could I disable image interpolation in
> cairo_paint() ? I want to zoom my image and see big square pixels, not
> the (fancy, btw) interpolation that cairo does by default ?

Image interpolation is controlled through the pattern filtering, which
defaults to bilinear interpolation (it actually default to GOOD which is
mapped to bilinear currently). So in order to change it using your
method, you would add a
 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
before your paint.

Using the mask however, we can no longer use the convenience function as
we need to tweak the pattern before use:

	cairo_pattern_t pattern;
	pattern = cairo_pattern_create_for_surface (grabber->image());
	cairo_pattern_set_filter pattern, CAIRO_FILTER_NEAREST);
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_mask (cr, pattern);
	cairo_pattern_destroy (pattern);

Hope this helps and you continue to have fun using Cairo!

More information about the cairo mailing list