[cairo] antialias for images?

Thomas Stover thomas at wsinnovations.com
Mon Oct 15 07:16:27 PDT 2007


> Date: Mon, 15 Oct 2007 01:24:35 +0200
> From: "Peter Goetz" <peter.gtz2 at gmail.com>
> Subject: [cairo] antialias for images?
> To: cairo at cairographics.org
> Message-ID:
> 	<64acc4780710141624ye4eadcbq4d6a91999c86ca5c at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi all,
> I'm trying to scale and rotate an image and then put it on a surface
> using paint as it is shown in one of the examples.
> However the scaling is not done by something like a bicubic filter but
> just by a linear one, so the picture looks very noisy (see screenshot
> attached) and has a staircase effect on the edges.
> Is there a way to make it look smooth? Some antialias for images? I
> haven't found something in the mail-archives.
> Thanks for your help!
> Peter
>
> Source-code:
>
> #include <cairo.h>
>
> int main (int argc, char *argv[])
> {
>        cairo_surface_t *surface;
>        cairo_t *cr;
>        int w, h;
>        cairo_surface_t *image;
>
>        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 256, 256);
>        cr = cairo_create (surface);
>
>        double M_PI=3.14;
>
>        image = cairo_image_surface_create_from_png ("test.png");
>        w = cairo_image_surface_get_width (image);
>        h = cairo_image_surface_get_height (image);
>
>        cairo_translate (cr, 128.0, 128.0);
>        cairo_rotate (cr, 20* M_PI/180);
>        cairo_scale  (cr, 256.0/w, 256.0/h);
>        cairo_translate (cr, -0.5*w, -0.5*h);
>
>        cairo_set_source_surface (cr, image, 0, 0);
>        cairo_paint (cr);
>
>        cairo_surface_destroy (image);
>
>        cairo_destroy (cr);
>        cairo_surface_write_to_png (surface, "output.png");
>        cairo_surface_destroy (surface);
>
>        return 0;
> }
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: output.png
> Type: image/png
> Size: 47960 bytes
> Desc: not available
> Url : http://lists.cairographics.org/archives/cairo/attachments/20071015/cb14c4d6/attachment.png

Note: I'm new to cairo myself, and this answer is based on nothing but 
intuition.

I think if the source image "test.png" is low-res then there is not much 
that can be done. However, if you start out with a higher resolution 
source image (maybe you are already), then do your rotation and effects 
at a high resolution inside cairo, then as the very last step scale the 
end result down to the smaller size you want - it might look better. In 
other words, first try doing the rotation on a 600x600 file on say a 
1000x100 cairo surface with no other translate or scales, and just see 
what it looks like. If that looks better, then I think it's a matter of 
finding the right sequence of scale/rotate/translate/matrix operations 
that cairo likes to do everything at high res except for the final scale 
down to 256x256.

This sounds like something I would want to do myself, let us know what 
happens.





More information about the cairo mailing list