Search code examples
cairopycairo

How to disable linear interpolation / smoothing / anti-aliasing when drawing an image in Cairo?


I want to draw a low-resolution image onto another surface in Cairo with upscaling. For instance, I have a 2x3 image which looks like this when zoomed in:

enter image description here

Drawing this image onto another surface with a zoom level of 10 results in:

enter image description here

The rendering code closely follows this example, i.e., all surface/context settings are at default. Apparently, Cairo applies some interpolation by default. What I want is to keep the blocky/pixelated look of the original image.

How can I control the type of interpolation in Cairo? In other libraries, there are commonly options to set interpolation to from cubic over linear to nearest. Is there an equivalent in Cairo?


Solution

  • I think you might be looking for something like cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);. This call would be done after cairo_set_source_surface().