gtk-utils.c: Don't include pixman-private.h
[profile/ivi/pixman.git] / demos / gtk-utils.c
1 #include <gtk/gtk.h>
2 #include <config.h>
3 #include "gtk-utils.h"
4
5 GdkPixbuf *
6 pixbuf_from_argb32 (uint32_t *bits,
7                     gboolean has_alpha,
8                     int width,
9                     int height,
10                     int stride)
11 {
12     GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
13                                         8, width, height);
14     int p_stride = gdk_pixbuf_get_rowstride (pixbuf);
15     guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf);
16     int w, h;
17     
18     for (h = 0; h < height; ++h)
19     {
20         for (w = 0; w < width; ++w)
21         {
22             uint32_t argb = bits[h * (stride / 4) + w];
23             guint r, g, b, a;
24             char *pb = (char *)p_bits;
25
26             pb += h * p_stride + w * 4;
27
28             r = (argb & 0x00ff0000) >> 16;
29             g = (argb & 0x0000ff00) >> 8;
30             b = (argb & 0x000000ff) >> 0;
31             a = has_alpha? (argb & 0xff000000) >> 24 : 0xff;
32
33             if (a)
34             {
35                 r = (r * 255) / a;
36                 g = (g * 255) / a;
37                 b = (b * 255) / a;
38             }
39
40             if (r > 255) r = 255;
41             if (g > 255) g = 255;
42             if (b > 255) b = 255;
43             
44             pb[0] = r;
45             pb[1] = g;
46             pb[2] = b;
47             pb[3] = a;
48         }
49     }
50     
51     return pixbuf;
52 }
53
54
55 static gboolean
56 on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
57 {
58     GdkPixbuf *pixbuf = data;
59     
60     gdk_draw_pixbuf (widget->window, NULL,
61                      pixbuf, 0, 0, 0, 0,
62                      gdk_pixbuf_get_width (pixbuf),
63                      gdk_pixbuf_get_height (pixbuf),
64                      GDK_RGB_DITHER_NONE,
65                      0, 0);
66     
67     return TRUE;
68 }
69
70 void
71 show_image (pixman_image_t *image)
72 {
73     GtkWidget *window;
74     GdkPixbuf *pixbuf;
75     int width, height, stride;
76     int argc;
77     char **argv;
78     char *arg0 = g_strdup ("pixman-test-program");
79     gboolean has_alpha;
80     pixman_format_code_t format;
81
82     argc = 1;
83     argv = (char **)&arg0;
84
85     gtk_init (&argc, &argv);
86     
87     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
88     width = pixman_image_get_width (image);
89     height = pixman_image_get_height (image);
90     stride = pixman_image_get_stride (image);
91
92     gtk_window_set_default_size (GTK_WINDOW (window), width, height);
93     
94     format = pixman_image_get_format (image);
95     
96     if (format == PIXMAN_a8r8g8b8)
97         has_alpha = TRUE;
98     else if (format == PIXMAN_x8r8g8b8)
99         has_alpha = FALSE;
100     else
101         g_error ("Can't deal with this format: %x\n", format);
102     
103     pixbuf = pixbuf_from_argb32 (pixman_image_get_data (image), has_alpha,
104                                  width, height, stride);
105     
106     g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
107     g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
108     
109     gtk_widget_show (window);
110     
111     gtk_main ();
112 }