Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a GtkImage from a Cairo surface?

Tags:

c

gtk

I want to be able to make a GtkImage from a Cairo surface (without writing a temp file).

I currently write the surface as PNG to a char array which I then feed to a PixbufLoader to get a Pixbuf which I use to create the GtkImage:

typedef struct
{
    unsigned char *pos;
    unsigned char *end;
} closure_t;

static cairo_status_t
png_to_array (void *closure, const unsigned char *data, unsigned int length)
{
    closure_t *cl = (closure_t *) closure;

    if ((cl->pos + length) > (cl->end))
        return CAIRO_STATUS_WRITE_ERROR;

    memcpy (cl->pos, data, length);
    cl->pos += length;

    return CAIRO_STATUS_SUCCESS;
}


// later in the code
cairo_surface_t *surface = ...

...

// how would i determine the right size?
unsigned char arr[...];
closure_t cl;
GtkWidget *image;
GdkPixbufLoader *pbloader;
GdkPixbuf *pb;

// copy surface png to arr
cl.pos = arr;
cl.end = arr + sizeof(arr);
cairo_surface_write_to_png_stream (surface,
                    (cairo_write_func_t) png_to_array,
                    &cl);

...
// write to pixbufloader, get pixbuf, create image
pbloader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(pbloader, arr, sizeof(arr), NULL);
gdk_pixbuf_loader_close(pbloader, NULL);

pb = gdk_pixbuf_loader_get_pixbuf(pbloader);

image = gtk_image_new_from_pixbuf(pb);
  1. This seems rather cumbersome - isn't there an easier way to do this?
  2. How would I determine the size of the array in my example?
like image 604
sungako1974 Avatar asked Feb 03 '26 21:02

sungako1974


1 Answers

One function will save you a lot of effort here. Look up gdk_pixbuf_get_from_surface. It gets a Pixbuf from a cairo_surface_t. Of coarse, realising as he writes it, that is only available if you use Gdk-3.0, which also means using Gtk+-3.0.

Of coarse if you want to use Gtk+-2.0 then you can create a pixmap, get a cairo_t from it then copy your other cairo_surface_t to it by

cairo_set_source_surface (cr, surface, x0, y0);
cairo_rectangle (cr, x0 + x, y0 + y, width, height);
cairo_fill (cr);

A example of how to create a pixmap is below, I'll let you fill in the rest.

#include <gtk/gtk.h>
#include <cairo/cairo.h>


int main(gint argc, gchar *argv[])
{
    GdkPixmap *pixmap;
    GtkWidget *image;
    GtkWidget *window;
    cairo_t *cr;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);

    pixmap = gdk_pixmap_new(window->window, 100, 100, -1);
    cr = gdk_cairo_create(pixmap);
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);    
    cairo_rectangle(cr, 10, 10, 80, 80);
    cairo_fill(cr);
    cairo_destroy(cr);

    cr = NULL;



    image = gtk_image_new_from_pixmap(pixmap, NULL);

    gtk_container_add(GTK_CONTAINER(window), image);


    gtk_widget_show(image);



    gtk_main();

    return 0;
}
like image 61
James Hurford Avatar answered Feb 05 '26 12:02

James Hurford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!