pommicket
pommicket

Reputation: 943

Cairo image blurred when scaled

I have the following cairo code:

cairo_set_source_rgba(cr, 1, 1, 1, 1);
cairo_rectangle(cr, 0, 0, WINDOW_SIZE, WINDOW_SIZE);
cairo_fill(cr);
cairo_scale(cr, 8, 8);
draw_image(cr, "q.png", 5, 5);

And

void draw_image(cairo_t* cr, char* img_name, int x, int y)
{
    cairo_translate(cr, x, y);
    cairo_surface_t* img = cairo_image_surface_create_from_png(img_name);
    cairo_set_source_surface(cr, img, 0, 0);
    cairo_paint(cr);
    cairo_translate(cr, -x, -y);
}

q.png is a 5x5 image: enter image description here

But when the program is run, the image is slightly blurred:
enter image description here

I have already tried

cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);

but it does not work.

Is there any way to fix this problem?

Upvotes: 4

Views: 1302

Answers (1)

andlabs
andlabs

Reputation: 11578

This is because of how the image is scaled up. Instead of setting a source surface directly, create a pattern out of the surface with cairo_pattern_create_for_surface(), call cairo_pattern_set_filter() on it to set the scaling mode, and then call cairo_set_source() to load the pattern. See the documentation for cairo_filter_t for the scaling modes. CAIRO_FILTER_NEAREST, for example, will give you a normal pixel zoom with no blurring or other transformations.

Upvotes: 5

Related Questions