[cairo] Problem painting scaled down images

Stefan Bellon bellon at axivion.com
Fri Oct 26 06:33:52 PDT 2007

Hi all,

I'm trying to create a PDF page with embedded PNG images on that page.
The PNG images can be of different size and are scaled differently on
the page. Scaling is always down-scaling (zoom-out), not magnifying
(zoom-in) and the PNG images are way smaller than the PDF surface size.

I have tried the following way (only the relevant part shown):

        cairo_surface_t *image =
          cairo_image_surface_create_from_png ("small-icon.png");

        cairo_scale (cr, 0.5, 0.5);
        cairo_set_source_surface (cr, image, 0.0, 0.0);

        cairo_paint (cr);

This "kind of works", but instead of painting one "small-icon.png" on
the surface, I get four icons. When I reduce the scale factor to 0.25 I
get 16.

Then I added

          (cairo_get_source (cr), CAIRO_EXTEND_NONE);

Before and/or after the above cairo_set_source_surface call, but this
didn't change anything.

Then I though I'd have to construct the pattern and the matrix myself
and tried it this way:

        cairo_surface_t *image =
          cairo_image_surface_create_from_png ("small-icon.png");

        cairo_pattern_t *pattern =
          cairo_pattern_create_for_surface (image);
        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);

        cairo_matrix_t matrix;
        cairo_matrix_init_scale (&matrix, 2.0, 2.0);
        cairo_pattern_set_matrix (pattern, &matrix);

        cairo_set_source (cr, pattern);

        cairo_paint (cr);

But the result is the same: four tiled icons.

At present I'm completely unsure of how to achieve what I want:
Painting one single PNG image with a scale factor of 0.5.

What am I missing?


Stefan Bellon

More information about the cairo mailing list