upload tizen 2.0 source
[framework/graphics/pixman.git] / demos / gtk-utils.c
1 #include <gtk/gtk.h>
2 #include <config.h>
3 #include "../test/utils.h"
4 #include "gtk-utils.h"
5
6 GdkPixbuf *
7 pixbuf_from_argb32 (uint32_t *bits,
8                     gboolean has_alpha,
9                     int width,
10                     int height,
11                     int stride)
12 {
13     GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
14                                         8, width, height);
15     int p_stride = gdk_pixbuf_get_rowstride (pixbuf);
16     guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf);
17     int i;
18
19     for (i = 0; i < height; ++i)
20     {
21         uint32_t *src_row = &bits[i * (stride / 4)];
22         uint32_t *dst_row = p_bits + i * (p_stride / 4);
23
24         a8r8g8b8_to_rgba_np (dst_row, src_row, width);
25     }
26
27     return pixbuf;
28 }
29
30 static gboolean
31 on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
32 {
33     GdkPixbuf *pixbuf = data;
34     
35     gdk_draw_pixbuf (widget->window, NULL,
36                      pixbuf, 0, 0, 0, 0,
37                      gdk_pixbuf_get_width (pixbuf),
38                      gdk_pixbuf_get_height (pixbuf),
39                      GDK_RGB_DITHER_NONE,
40                      0, 0);
41     
42     return TRUE;
43 }
44
45 void
46 show_image (pixman_image_t *image)
47 {
48     GtkWidget *window;
49     GdkPixbuf *pixbuf;
50     int width, height, stride;
51     int argc;
52     char **argv;
53     char *arg0 = g_strdup ("pixman-test-program");
54     gboolean has_alpha;
55     pixman_format_code_t format;
56
57     argc = 1;
58     argv = (char **)&arg0;
59
60     gtk_init (&argc, &argv);
61     
62     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
63     width = pixman_image_get_width (image);
64     height = pixman_image_get_height (image);
65     stride = pixman_image_get_stride (image);
66
67     gtk_window_set_default_size (GTK_WINDOW (window), width, height);
68     
69     format = pixman_image_get_format (image);
70     
71     if (format == PIXMAN_a8r8g8b8)
72         has_alpha = TRUE;
73     else if (format == PIXMAN_x8r8g8b8)
74         has_alpha = FALSE;
75     else
76         g_error ("Can't deal with this format: %x\n", format);
77     
78     pixbuf = pixbuf_from_argb32 (pixman_image_get_data (image), has_alpha,
79                                  width, height, stride);
80     
81     g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
82     g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
83     
84     gtk_widget_show (window);
85     
86     gtk_main ();
87 }