gtk-utils.c revision 9ad247e8
1#include <gtk/gtk.h>
2#include <config.h>
3#include "../test/utils.h"
4#include "gtk-utils.h"
5
6GdkPixbuf *
7pixbuf_from_argb32 (uint32_t *bits,
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 i;
17
18    for (i = 0; i < height; ++i)
19    {
20	uint32_t *src_row = &bits[i * (stride / 4)];
21	uint32_t *dst_row = p_bits + i * (p_stride / 4);
22
23	a8r8g8b8_to_rgba_np (dst_row, src_row, width);
24    }
25
26    return pixbuf;
27}
28
29static gboolean
30on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
31{
32    GdkPixbuf *pixbuf = data;
33
34    gdk_draw_pixbuf (widget->window, NULL,
35		     pixbuf, 0, 0, 0, 0,
36		     gdk_pixbuf_get_width (pixbuf),
37		     gdk_pixbuf_get_height (pixbuf),
38		     GDK_RGB_DITHER_NONE,
39		     0, 0);
40
41    return TRUE;
42}
43
44void
45show_image (pixman_image_t *image)
46{
47    GtkWidget *window;
48    GdkPixbuf *pixbuf;
49    int width, height;
50    int argc;
51    char **argv;
52    char *arg0 = g_strdup ("pixman-test-program");
53    pixman_format_code_t format;
54    pixman_image_t *copy;
55
56    argc = 1;
57    argv = (char **)&arg0;
58
59    gtk_init (&argc, &argv);
60
61    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
62    width = pixman_image_get_width (image);
63    height = pixman_image_get_height (image);
64
65    gtk_window_set_default_size (GTK_WINDOW (window), width, height);
66
67    format = pixman_image_get_format (image);
68
69    /* Three cases:
70     *
71     *  - image is a8r8g8b8_sRGB: we will display without modification
72     *    under the assumption that the monitor is sRGB
73     *
74     *  - image is a8r8g8b8: we will display without modification
75     *    under the assumption that whoever created the image
76     *    probably did it wrong by using sRGB inputs
77     *
78     *  - other: we will convert to a8r8g8b8 under the assumption that
79     *    whoever created the image probably did it wrong.
80     */
81    switch (format)
82    {
83    case PIXMAN_a8r8g8b8_sRGB:
84    case PIXMAN_a8r8g8b8:
85	copy = pixman_image_ref (image);
86	break;
87
88    default:
89	copy = pixman_image_create_bits (PIXMAN_a8r8g8b8,
90					 width, height, NULL, -1);
91	pixman_image_composite32 (PIXMAN_OP_SRC,
92				  image, NULL, copy,
93				  0, 0, 0, 0, 0, 0,
94				  width, height);
95	break;
96    }
97
98    pixbuf = pixbuf_from_argb32 (pixman_image_get_data (copy),
99				 width, height,
100				 pixman_image_get_stride (copy));
101
102    g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
103    g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
104
105    gtk_widget_show (window);
106
107    gtk_main ();
108}
109