[cairo] problems to downscale and center an image

Claudio Saavedra csaavedra at alumnos.utalca.cl
Mon Dec 4 14:35:40 PST 2006


Hi Behdad, 

On Fri, 2006-12-01 at 14:16 -0500, Behdad Esfahbod wrote:
> > 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. 

Thanks. That indeed fixes the aliasing problem, but then I get the image
tiled on the page instead of only centered. See here[1] for a lowres of
the result.

I used a group because I thought that this way cairo would respect the
white frame around the image (and it did), but without the group cairo
seems to tile the source pixbuf everywhere into the page.

Thanks,

Claudio

[1] http://www.gnome.org/~csaavedr/images/eog-image-print-lowres.png
-- 
Claudio Saavedra <csaavedra at alumnos.utalca.cl>



More information about the cairo mailing list