Showing posts with label linux image resize. Show all posts
Showing posts with label linux image resize. Show all posts

Monday, September 26, 2011

PNG Image scaling with Lib Cairo

Just wrote now for a tool and I liked the small utility.


#include "stdio.h"
#include

//#define ENABLE_DEBUG_LOG

#ifdef ENABLE_DEBUG_LOG
void printtImageDetails( cairo_surface_t * image, const char * text)
{
        if(text != NULL)
                printf("%s:\n", text);

        printf("W=%d H=%d\n", cairo_image_surface_get_width (image), cairo_image_surface_get_height (image));
        printf("Format:%d Stride:%d\n", cairo_image_surface_get_format(image), cairo_image_surface_get_stride (image));
}
#else
#define printtImageDetails(__image, __text)
#endif /* ENABLE_DEBUG_LOG */

void imageResize ( const char * sourceName, const char * destName, double ratio)
{
cairo_surface_t *image;
cairo_surface_t *dest;

image = cairo_image_surface_create_from_png (sourceName);
printtImageDetails(image, sourceName); 

dest = cairo_surface_create_similar (image, CAIRO_CONTENT_COLOR_ALPHA,
cairo_image_surface_get_width (image) * ratio,
cairo_image_surface_get_height (image) * ratio);

cairo_t *cr = cairo_create (dest);

cairo_scale (cr, ratio, ratio);
cairo_set_source_rgba (cr, 0, 0, 0, 0);
cairo_set_source_surface (cr, image, 0, 0);
cairo_paint (cr);
cairo_surface_destroy (image);

printtImageDetails(dest, destName);
cairo_surface_write_to_png (dest, destName); 

cairo_destroy (cr);
cairo_surface_destroy (dest);

}/* end method imageResize */