1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <gtk/gtk.h>
void
gdk_pixbuf_copy_area_with_alpha (const GdkPixbuf *src_pixbuf,
guint src_x, guint src_y, guint width, guint height,
GdkPixbuf *dest_pixbuf, guint dest_x, guint dest_y)
{
g_return_if_fail (GDK_IS_PIXBUF (src_pixbuf));
g_return_if_fail (GDK_IS_PIXBUF (dest_pixbuf));
if (!gdk_pixbuf_get_has_alpha (src_pixbuf) || !gdk_pixbuf_get_has_alpha (src_pixbuf)) return;
gint dest_width = width-src_x;
gint dest_height = height-src_y;
if (dest_width<0) return;
if (dest_height<0) return;
if (dest_x + dest_width > gdk_pixbuf_get_width (dest_pixbuf))
dest_width = gdk_pixbuf_get_width (dest_pixbuf) - dest_x;
if (dest_y + dest_height > gdk_pixbuf_get_height (dest_pixbuf))
dest_height = gdk_pixbuf_get_height (dest_pixbuf) - dest_y;
guchar *src_pixels = gdk_pixbuf_get_pixels (src_pixbuf);
guchar *dest_pixels = gdk_pixbuf_get_pixels (dest_pixbuf);
gint src_rowstride = gdk_pixbuf_get_rowstride (src_pixbuf);
gint dest_rowstride = gdk_pixbuf_get_rowstride (dest_pixbuf);
for (gint y=src_y; y < dest_height; y++, dest_y++)
for (gint x=src_x; x < dest_width; x++)
{
if (src_pixels [x*4 + y*src_rowstride + 3]==255)
{
dest_pixels [(dest_x+(x-src_x))*4 + dest_y*dest_rowstride] = src_pixels [x*4 + y*src_rowstride];
dest_pixels [(dest_x+(x-src_x))*4 + dest_y*dest_rowstride + 1] = src_pixels [x*4 + y*src_rowstride + 1];
dest_pixels [(dest_x+(x-src_x))*4 + dest_y*dest_rowstride + 2] = src_pixels [x*4 + y*src_rowstride + 2];
dest_pixels [(dest_x+(x-src_x))*4 + dest_y*dest_rowstride + 3] = src_pixels [x*4 + y*src_rowstride + 3];
}
}
}
gint
main(int argc, gchar **argv)
{
gtk_init (&argc, &argv);
GdkPixbuf *pixbuf_piece = gdk_pixbuf_new_from_file ("./linux.jpg", NULL);
GdkPixbuf *pixbuf_bg = gdk_pixbuf_new_from_file ("./fond.jpg", NULL);
GdkPixbuf *pixbuf_tmp = gdk_pixbuf_add_alpha (pixbuf_piece, TRUE, 255, 255, 255);
GdkPixbuf *pixbuf_tmp2 = gdk_pixbuf_add_alpha (pixbuf_bg, FALSE, 0, 0, 0);
gdk_pixbuf_copy_area_with_alpha (pixbuf_tmp, 0, 0, 240, 240, pixbuf_tmp2, 10, 10);
GtkWidget *_im = gtk_image_new_from_pixbuf (pixbuf_tmp2);
GtkWidget *mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add (GTK_CONTAINER(mainwindow), _im);
// Affectation des différents signaux
g_signal_connect (G_OBJECT(mainwindow), "destroy", G_CALLBACK (gtk_main_quit), NULL);
// Affichage du tout.
gtk_widget_show_all(mainwindow);
// Boucle principale
gtk_main();
return 0;
} |
Partager