Thank you James,
I found what I wanted in one of the links: 
http://live.gnome.org/GtkCairoIntegration

I had previously seen this page, but didn't see a small code snippet in 
python that does what I want. I've translated it in C, and it work now: 
I can convert a pixbuf to something where I can draw in with cairo.

For the curious ones, here's a simple example on how to do that.

Thanks again,
--
Luis

#include <glib.h>
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

int main (void)
{
        GdkPixbuf *pixbuf;
        gint width, height;
        cairo_format_t format;
        cairo_surface_t *surface;
        cairo_t *cr;

        g_type_init ();

        pixbuf = gdk_pixbuf_new_from_file ("test.png", NULL);
        g_assert (pixbuf != NULL);
        
        format = (gdk_pixbuf_get_has_alpha (pixbuf)) ? CAIRO_FORMAT_ARGB32 : 
CAIRO_FORMAT_RGB24;
        width = gdk_pixbuf_get_width (pixbuf);
        height = gdk_pixbuf_get_height (pixbuf);

        surface = cairo_image_surface_create (format, width, height);
        g_assert (surface != NULL);
        cr = cairo_create (surface);

        /* Draw the pixbuf */
        gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
        cairo_paint (cr);

        /* Draw a red rectangle */
        cairo_set_source_rgb (cr, 1, 0, 0);
        cairo_rectangle (cr, width * .25, height * .25, width *.5, height *.5);
        cairo_fill (cr);

        /* Save to a PNG file */
        cairo_surface_write_to_png (surface, "output.png");

        cairo_surface_destroy (surface);
        cairo_destroy (cr);

        return 0;
}


James Scott Jr a écrit :
> Ok,
> 
> http://cairographics.org/examples/
> http://live.gnome.org/GtkCairoIntegration
> 
> These links have some examples of using everything from a raw image to a
> pdf as the backend in cairo from the command line -- without the use of
> gtk.
> 
> Hope this fits your need better than my last response.
> 
> James,
> 
> 
> On Thu, 2008-05-29 at 03:18 +0200, Luis Menina wrote:
>> Hi list!
>>
>> I'm trying to find a way to draw in a GdkPixbuf (for example draw a 
>> rectangle on the pixbuf) and save the result to a file afterwards.
>>
>> I found in devhelp the gdk_draw_* functions, but to use them I need a 
>> GdkPixmap. gdk_pixmap_new requires a GdkDrawable. The problem is that 
>> I'm developping a command line application, so I don't use GTK, only 
>> glib, gdk, and gdk-pixbuf. Even creating the GdkPixmap with a NULL 
>> GdkDrawable, I can't workaround the problem, because the documentation 
>> of gdk_draw_pixbuf tells me that "All windows have a colormap, however, 
>> pixmaps only have colormap by default if they were created with a 
>> non-NULL window argument. Otherwise a colormap must be set on them with 
>> gdk_drawable_set_colormap()". Trying to get a colormap requires other 
>> stuff, which requires other stuff, and so on...
>>
>> As this seems to be quite old-fashioned way of doing things, I tried to 
>> see if cairo could do the trick. But it seems there's no way to draw to 
>> a GdkPixbuf with cairo (I'm unfamiliar with cairo).
>>
>> So, does anyone have an hint on how to draw and save an image from a 
>> GdkPixbuf, with gdk or cairo ?
>>
>> Thanks for you help,
>>
>> Luis
>> _______________________________________________
>> gtk-app-devel-list mailing list
>> gtk-app-devel-list@gnome.org
>> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
> 
> 
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to