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