aboutsummaryrefslogtreecommitdiff
path: root/pixman/demos/gtk-utils.c
blob: d7e946dedcb8039876bdd75f806c3da1824e6668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <gtk/gtk.h>
#include <config.h>
#include "../test/utils.h"
#include "gtk-utils.h"

pixman_image_t *
pixman_image_from_file (const char *filename, pixman_format_code_t format)
{
    GdkPixbuf *pixbuf;
    pixman_image_t *image;
    int width, height;
    uint32_t *data, *d;
    uint8_t *gdk_data;
    int n_channels;
    int j, i;
    int stride;

    if (!(pixbuf = gdk_pixbuf_new_from_file (filename, NULL)))
	return NULL;

    image = NULL;

    width = gdk_pixbuf_get_width (pixbuf);
    height = gdk_pixbuf_get_height (pixbuf);
    n_channels = gdk_pixbuf_get_n_channels (pixbuf);
    gdk_data = gdk_pixbuf_get_pixels (pixbuf);
    stride = gdk_pixbuf_get_rowstride (pixbuf);

    if (!(data = malloc (width * height * sizeof (uint32_t))))
	goto out;

    d = data;
    for (j = 0; j < height; ++j)
    {
	uint8_t *gdk_line = gdk_data;

	for (i = 0; i < width; ++i)
	{
	    int r, g, b, a;
	    uint32_t pixel;

	    r = gdk_line[0];
	    g = gdk_line[1];
	    b = gdk_line[2];

	    if (n_channels == 4)
		a = gdk_line[3];
	    else
		a = 0xff;

	    r = (r * a + 127) / 255;
	    g = (g * a + 127) / 255;
	    b = (b * a + 127) / 255;

	    pixel = (a << 24) | (r << 16) | (g << 8) | b;

	    *d++ = pixel;
	    gdk_line += n_channels;
	}

	gdk_data += stride;
    }

    image = pixman_image_create_bits (
	format, width, height, data, width * 4);

out:
    g_object_unref (pixbuf);
    return image;
}

GdkPixbuf *
pixbuf_from_argb32 (uint32_t *bits,
		    int width,
		    int height,
		    int stride)
{
    GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
					8, width, height);
    int p_stride = gdk_pixbuf_get_rowstride (pixbuf);
    guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf);
    int i;

    for (i = 0; i < height; ++i)
    {
	uint32_t *src_row = &bits[i * (stride / 4)];
	uint32_t *dst_row = p_bits + i * (p_stride / 4);

	a8r8g8b8_to_rgba_np (dst_row, src_row, width);
    }

    return pixbuf;
}

static gboolean
on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
{
    GdkPixbuf *pixbuf = data;
    
    gdk_draw_pixbuf (widget->window, NULL,
		     pixbuf, 0, 0, 0, 0,
		     gdk_pixbuf_get_width (pixbuf),
		     gdk_pixbuf_get_height (pixbuf),
		     GDK_RGB_DITHER_NONE,
		     0, 0);
    
    return TRUE;
}

void
show_image (pixman_image_t *image)
{
    GtkWidget *window;
    GdkPixbuf *pixbuf;
    int width, height;
    int argc;
    char **argv;
    char *arg0 = g_strdup ("pixman-test-program");
    pixman_format_code_t format;
    pixman_image_t *copy;

    argc = 1;
    argv = (char **)&arg0;

    gtk_init (&argc, &argv);
    
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    width = pixman_image_get_width (image);
    height = pixman_image_get_height (image);

    gtk_window_set_default_size (GTK_WINDOW (window), width, height);

    format = pixman_image_get_format (image);

    /* Three cases:
     *
     *  - image is a8r8g8b8_sRGB: we will display without modification
     *    under the assumption that the monitor is sRGB
     *
     *  - image is a8r8g8b8: we will display without modification
     *    under the assumption that whoever created the image
     *    probably did it wrong by using sRGB inputs
     *
     *  - other: we will convert to a8r8g8b8 under the assumption that
     *    whoever created the image probably did it wrong.
     */
    switch (format)
    {
    case PIXMAN_a8r8g8b8_sRGB:
    case PIXMAN_a8r8g8b8:
	copy = pixman_image_ref (image);
	break;

    default:
	copy = pixman_image_create_bits (PIXMAN_a8r8g8b8,
					 width, height, NULL, -1);
	pixman_image_composite32 (PIXMAN_OP_SRC,
				  image, NULL, copy,
				  0, 0, 0, 0, 0, 0,
				  width, height);
	break;
    }

    pixbuf = pixbuf_from_argb32 (pixman_image_get_data (copy),
				 width, height,
				 pixman_image_get_stride (copy));
    
    g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
    g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
    
    gtk_widget_show (window);
    
    gtk_main ();
}