<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><br><font face="Arial" size="2"><font size="2">Hi Simon,<br><br>Thanks for the very useful advice...<br><br>I can make the GdkPixbufLoader work if the program does not prepare eventhandlers for area-prepared and area-updated. However, if I connect these they will not trigger and call the handlers. <br><br>The test program is small so I can add it inline.<br><br>Hope someone can see what is wrong...<br><br>Ken<br>P.S. The test program:<br>static cairo_surface_t * imgpng ;<br><br>static unsigned char bigbuf [ 200000 ] ;<br>static GdkPixbuf * pixbuf;<br>static GdkPixbufLoader * loader ;<br><br><br>static GdkPixbuf * makepixbuf ( bool byfilename , const char * filename )<br> {<br> GError * err = NULL ;<br> if ( byfilename )<br> {<br>
return gdk_pixbuf_new_from_file(filename,&err);<br> }<br> // read file content into buffer<br> FILE * f = fopen ( filename, "r");<br> int length = fread (bigbuf, 1, sizeof(bigbuf), f);<br> fclose (f);<br> loader = gdk_pixbuf_loader_new ();<br> gdk_pixbuf_loader_write (loader, bigbuf, length, NULL);<br> gdk_pixbuf_loader_close (loader , &err ) ;<br> //printf ( "pixbuf may not yet be ready...\n" ) ;<br> //return NULL ;<br> // actually it is ready here!!!!!<br> return pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);<br> }<br><br>static void do_drawing(cairo_t *cr)<br> {<br> cairo_set_source_surface(cr, imgpng , 0,40);<br> cairo_paint(cr);<br><br> if ( pixbuf != NULL )<br> {<br>
gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 150);<br> cairo_paint (cr);<br> }<br> }<br><br>static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,<br> gpointer user_data)<br> {<br> cr = gdk_cairo_create(gtk_widget_get_window(widget));<br> do_drawing(cr);<br> cairo_destroy(cr);<br> return FALSE;<br> }<br><br>static void on_area_prep( GdkPixbufLoader *loader, gpointer user_data)<br> {<br> // this is not called<br> printf ( "area-prepared: pixbuf ready\n" ) ;<br> pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);<br> }<br><br><br>static void on_area_update( GdkPixbufLoader *loader,
gint x , gint y , gint width ,<br> gint height , gpointer user_data)<br> {<br> // same here - not called<br> printf ( "area-updated: pixbuf ready\n" ) ;<br> pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);<br> }<br><br>int main(int argc, char *argv[])<br> {<br> GtkWidget *window;<br> GtkWidget *darea;<br><br> gtk_init(&argc, &argv);<br><br> imgpng = cairo_image_surface_create_from_png("sample_7.png");<br> pixbuf = makepixbuf ( false , "parrots.jpg" );<br><br> window = gtk_window_new(GTK_WINDOW_TOPLEVEL);<br><br> darea = gtk_drawing_area_new();<br> gtk_container_add(GTK_CONTAINER(window), darea);<br><br>
g_signal_connect(G_OBJECT(darea), "draw",<br> G_CALLBACK(on_draw_event), NULL);<br> g_signal_connect(G_OBJECT(loader), "area-prepared",<br> G_CALLBACK(on_area_prep), NULL);<br> g_signal_connect(G_OBJECT(loader), "area-updated",<br> G_CALLBACK(on_area_update), NULL);<br> g_signal_connect(window, "destroy",<br> G_CALLBACK(gtk_main_quit), NULL);<br><br> gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);<br>
gtk_window_set_default_size(GTK_WINDOW(window), 400, 90);<br> gtk_window_set_title(GTK_WINDOW(window), "GTK window");<br><br> gtk_widget_show_all(window);<br><br> gtk_main();<br><br> cairo_surface_destroy(imgpng);<br><br> return 0;<br> }<br><br><br><br><br></font> </font></div><div><br></div> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font face="Arial" size="2"> <hr size="1"> <b><span style="font-weight:bold;">From:</span></b> Simon Sapin <simon.sapin@exyr.org><br> <b><span style="font-weight: bold;">To:</span></b> cairo@cairographics.org <br> <b><span style="font-weight: bold;">Sent:</span></b> Friday, 5 April 2013, 15:13<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [cairo] How to output images from data
originating from files?<br> </font> </div> <br>Le 05/04/2013 16:03, Ken Resander a écrit :<br>> I am new to Cairo and have fairly limited knowlege of graphics (Windows<br>> GDI,GDI+ years ago).<br>> I am now on Ubuntu 12.04 and need to render images in png and jpg from<br>> GTK in C/C++.<br>><br>> I know how to do this when specifying .png and .jpg file names:<br>><br>> cairo_surface_t * imgpng =<br>> cairo_image_surface_create_from_png("sample_7.png");<br>> cairo_set_source_surface(cr, imgpng, 0,40);<br>> cairo_paint(cr);<br>><br>> GError * err = NULL;<br>> GdkPixbuf * imgpxb = gdk_pixbuf_new_from_file("parrots.jpg",&err);<br>> gdk_cairo_set_source_pixbuf(cr, imgpxb, 0, 150);<br>> cairo_paint (cr);<br>><br>> My program will receive image data from another computer via standard<br>> protocols. There might be hundreds of small to small-medium images<br>> coming, so it would more efficient
to batch the file content of these<br>> into a huge buffer and pass that across (from the other computer) along<br>> with the image format, buffer offset and length of each chunk.<br>><br>> Of course I could copy a chunk into a tmp file and use the png surface<br>> and gdkpixbuf filename api calls above, but it seems such a shame having<br>> to put the data back into a file for cairo/gdk to read and convert it to<br>> the format needed.<br>><br>> Is there a better way?<br><br>Hi,<br><br>If you have images (not all PNG) in memory, the easiest way to load them <br>without going through temporary files is to use GdkPixbufLoader:<br><br><a href="https://developer.gnome.org/gdk-pixbuf/stable/GdkPixbufLoader.html" target="_blank">https://developer.gnome.org/gdk-pixbuf/stable/GdkPixbufLoader.html</a><br><br>This will give you GdkPixbuf objects that you can convert to cairo <br>patterns with gdk_cairo_set_source_pixbuf, as you do
above.<br><br>Cheers,<br>-- <br>Simon Sapin<br>-- <br>cairo mailing list<br><a ymailto="mailto:cairo@cairographics.org" href="mailto:cairo@cairographics.org">cairo@cairographics.org</a><br><a href="http://lists.cairographics.org/mailman/listinfo/cairo" target="_blank">http://lists.cairographics.org/mailman/listinfo/cairo</a><br><br><br> </div> </div> </div></body></html>