[cairo] problems to downscale and center an image

Claudio Saavedra csaavedra at alumnos.utalca.cl
Fri Dec 1 10:34:29 PST 2006

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?

Thank you very much,


[1] http://bugzilla.gnome.org/show_bug.cgi?id=381358
[2] http://bugzilla.gnome.org/attachment.cgi?id=77501
Claudio Saavedra <csaavedra at alumnos.utalca.cl>

More information about the cairo mailing list