C GTK+ : Copying an image over another one

Articles may may have files attached at the end of the post

GTK+ offers a nice toolkit through the GDK library to make image manipulation quite easy.

This page will go through the process of modifying an image but putting another on on top as an overlay. In this example, we will be copying an MSN icon on top of a "busy" icon.
Another use case could be to watermark images.

Here is the bit of code used in this example, and the result: GTK image overlayGTK image overlay

The original file is available in a .tar.gz at the bottom of this article

  1. /**
  2.  * compile with:
  3.  * gcc -Wall -Werror -g -o -DGDK_PIXBUF_DISABLE_DEPRECATED -DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -o gtkoverlay gtkoverlay.c `pkg-config --cflags gtk+-2.0 --libs`
  4.  * Available under GPLv2
  5.  */
  6.  
  7. #define PT_MSN      "images/im-msn.svg"
  8. #define ST_BUSY     "images/empathy-busy.svg"
  9.  
  10. #include <gtk/gtk.h>
  11.  
  12. int
  13. main (int argc, char **argv)
  14. {
  15.   GtkWidget           *window;
  16.   GtkWidget           *img;            /* result img */
  17.   GdkPixbuf           *pixb1, *pixb2;  /* destination, source */
  18.   int                  ls, ss;         /* large size, small size */
  19.  
  20.   gtk_init (&argc, &argv);
  21.  
  22.   window = img = NULL;
  23.   ls = 32;
  24.   ss = 24;
  25.   pixb1 = gdk_pixbuf_new_from_file_at_size (ST_BUSY, ls, ls, NULL);
  26.   pixb2 = gdk_pixbuf_new_from_file_at_size (PT_MSN, ss, ss, NULL);
  27.  
  28.  
  29.   if( !pixb1 || !pixb2 ){
  30.     fprintf (stderr, "Error loading one of the image!\n");
  31.     if( pixb1 ) g_object_unref( G_OBJECT (pixb1) );
  32.     else g_object_unref( G_OBJECT (pixb2) );
  33.     return 1;
  34.   }
  35.  
  36.   gdk_pixbuf_composite( pixb2, pixb1,                  /* src, dst */
  37.                           0, ls - ss,                  /* destx, desty */
  38.                           ss, ss,                      /* destwdth, destheight */
  39.                           0, ls - ss,                  /* offset_x, offset_y */
  40.                           1, 1,                        /* scale_x, scale_y */
  41.                           GDK_INTERP_BILINEAR, 255 );  /* interp_type, overall_alpha */
  42.  
  43.   img = gtk_image_new_from_pixbuf (pixb1);
  44.  
  45.   g_object_unref( G_OBJECT (pixb1) );
  46.   g_object_unref( G_OBJECT (pixb2) );
  47.  
  48.   window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
  49.   gtk_container_add( GTK_CONTAINER (window), img );
  50.   gtk_widget_show_all( window );
  51.   gtk_main();
  52.  
  53.   return 0;
  54. }

The tricky part is with gdk_pixbuf_composite which is best explained on the api itself.

AttachmentSize
gtk-img-overlay.tar_.gz5.24 KB