int main(int argc, char **argv) { GtkWidget *image; GdkBitmap *mask; GdkPixbuf *pixbuf; GdkGC *gc; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL); gtk_window_set_decorated (GTK_WINDOW (window), FALSE); pixbuf = gdk_pixbuf_new_from_file ("romeo.png", NULL); if (!pixbuf) { g_error ("Could not load image"); } mask = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1); gdk_pixbuf_render_threshold_alpha (pixbuf, mask, 0, 0, 0, 0, -1, -1, 127); gtk_widget_shape_combine_mask (window, mask, 0, 0); image = gtk_image_new_from_pixbuf (pixbuf); gtk_container_add (GTK_CONTAINER (window), image); gtk_widget_show_all (window); g_timeout_add (10, turn, NULL); gtk_main (); }
GdkBitmap * load_and_scale_bitmap (const gchar *file, gfloat x_ratio, gfloat y_ratio) { GdkPixbuf *pixbuf = NULL; GdkBitmap *bitmap; pixbuf = _load_and_scale_pixbuf(file, x_ratio, y_ratio); if (!pixbuf) return NULL; bitmap = gdk_pixmap_new(NULL, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), 1); gdk_pixbuf_render_threshold_alpha(pixbuf, bitmap, 0, 0, 0, 0, -1, -1, 128); g_object_unref(pixbuf); return bitmap; }
static gboolean xfwmPixmapDrawFromGdkPixbuf (xfwmPixmap * pm, GdkPixbuf *pixbuf) { GdkPixmap *dest_pixmap; GdkPixmap *dest_bitmap; GdkVisual *gvisual; GdkColormap *cmap; gint width, height; gint dest_x, dest_y; gint alpha_threshold; g_return_val_if_fail (pm != NULL, FALSE); g_return_val_if_fail (pm->pixmap != None, FALSE); g_return_val_if_fail (pm->mask != None, FALSE); dest_pixmap = gdk_xid_table_lookup (pm->pixmap); if (dest_pixmap) { g_object_ref (G_OBJECT (dest_pixmap)); } else { dest_pixmap = gdk_pixmap_foreign_new (pm->pixmap); } if (!dest_pixmap) { g_warning ("Cannot get pixmap"); return FALSE; } dest_bitmap = gdk_xid_table_lookup (pm->mask); if (dest_bitmap) { g_object_ref (G_OBJECT (dest_bitmap)); } else { dest_bitmap = gdk_pixmap_foreign_new (pm->mask); } if (!dest_bitmap) { g_warning ("Cannot get bitmap"); g_object_unref (dest_pixmap); return FALSE; } gvisual = gdk_screen_get_system_visual (pm->screen_info->gscr); cmap = gdk_x11_colormap_foreign_new (gvisual, pm->screen_info->cmap); if (!cmap) { g_warning ("Cannot create colormap"); g_object_unref (dest_pixmap); g_object_unref (dest_bitmap); return FALSE; } width = MIN (gdk_pixbuf_get_width (pixbuf), pm->width); height = MIN (gdk_pixbuf_get_height (pixbuf), pm->height); dest_x = (pm->width - width) / 2; dest_y = (pm->height - height) / 2; gdk_drawable_set_colormap (GDK_DRAWABLE (dest_pixmap), cmap); gdk_draw_pixbuf (GDK_DRAWABLE (dest_pixmap), NULL, pixbuf, 0, 0, dest_x, dest_y, width, height, GDK_RGB_DITHER_NONE, 0, 0); alpha_threshold = (gdk_pixbuf_get_has_alpha (pixbuf) ? 0xFF : 0); gdk_pixbuf_render_threshold_alpha (pixbuf, dest_bitmap, 0, 0, dest_x, dest_y, width, height, alpha_threshold); g_object_unref (cmap); g_object_unref (dest_pixmap); g_object_unref (dest_bitmap); return TRUE; }