[cairo] problems to downscale and center an image

Behdad Esfahbod behdad at behdad.org
Fri Dec 1 11:16:11 PST 2006

On Fri, 2006-12-01 at 13:34 -0500, Claudio Saavedra wrote:
> Hi all,
> I don't know too much about cairo, so I stepped in a problem when
> implementing GtkPrint'ing through cairo for Eye of GNOME. Hope you guys
> could give me a hand.
> The desired behavior in EOG is to downscale images which are bigger than
> the paper size, and center them in the page. I am doing this using
> something like this (code simplified):
>   width = gdk_pixbuf_get_width (pixbuf);
>   height = gdk_pixbuf_get_height (pixbuf);
>   cr = gtk_print_context_get_cairo_context (context);
>   p_width = gtk_print_context_get_width (context);
>   p_height = gtk_print_context_get_height (context);
>   cairo_push_group (cr);
>     scale_factor = ((gdouble)p_width)/width;
>     cairo_scale (cr, scale_factor, scale_factor);
>     cairo_translate (cr, 0, (p_height/scale_factor - height)/2);
>     gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
>     cairo_paint (cr);
>   cairo_pop_group_to_source (cr);
>   cairo_paint (cr);
> The result is the image beautifully centered in the page, but the
> quality is pretty bad, and the image looks very pixeled. I opened a EOG
> bug to track this[1], and you can find attached to it a PDF file with
> the output illustrating the problem[2].
> Is this the wrong way to downscale the image and center it on the cairo
> context? Could you give me some hints?

Just remove the cairo_push/pop_group.  Why do you need that?  Using
those currently results into an image fallback, so your large image will
be pasted in a smaller image, and embedded in the PS/PDF.  You can set
the resolution of that fallback, using
cairo_surface_set_fallback_resolution(), but unless you really want to
force a conversion, you should remove that and let cairo embed the
original image directly.


> Thank you very much,
> Claudio
> [1] http://bugzilla.gnome.org/show_bug.cgi?id=381358
> [2] http://bugzilla.gnome.org/attachment.cgi?id=77501

"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759

More information about the cairo mailing list