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
|
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "demo.h"
struct device *device_open(int argc, char **argv)
{
struct device *device = 0;
enum {
AUTO,
XLIB,
XIMAGE,
XCB,
GLX,
DRM,
} backend = AUTO;
int n;
for (n = 1; n < argc; n++) {
if (strcmp (argv[n], "--xlib") == 0) {
backend = XLIB;
} else if (strcmp (argv[n], "--xcb") == 0) {
backend = XCB;
} else if (strcmp (argv[n], "--ximage") == 0) {
backend = XIMAGE;
} else if (strcmp (argv[n], "--drm") == 0) {
backend = DRM;
} else if (strcmp (argv[n], "--glx") == 0) {
backend = GLX;
}
}
if (backend == AUTO) {
if (device == 0 && HAVE_DRM)
device = drm_open (argc, argv);
if (device == 0 && HAVE_XCB)
device = xcb_open (argc, argv);
if (device == 0 && HAVE_XLIB)
device = xlib_open (argc, argv);
if (device == 0 && HAVE_XLIB)
device = glx_open (argc, argv);
if (device == 0 && HAVE_XIMAGE)
device = ximage_open (argc, argv);
} else switch (backend) {
case AUTO:
case XLIB:
device = xlib_open (argc, argv);
break;
case XCB:
device = xcb_open (argc, argv);
break;
case XIMAGE:
device = ximage_open (argc, argv);
break;
case GLX:
device = glx_open (argc, argv);
break;
case DRM:
#if HAVE_DRM
device = drm_open (argc, argv);
#endif
break;
}
if (device == 0) {
fprintf(stderr, "Failed to open a drawing device\n");
exit(1);
}
printf("Using backend \"%s\"\n", device->name);
return device;
}
cairo_surface_t *
_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *pixbuf)
{
gint width = gdk_pixbuf_get_width (pixbuf);
gint height = gdk_pixbuf_get_height (pixbuf);
guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
int cairo_stride;
guchar *cairo_pixels;
cairo_format_t format;
cairo_surface_t *surface;
int j;
if (n_channels == 3)
format = CAIRO_FORMAT_RGB24;
else
format = CAIRO_FORMAT_ARGB32;
surface = cairo_image_surface_create(format, width, height);
if (cairo_surface_status(surface))
return surface;
cairo_stride = cairo_image_surface_get_stride (surface);
cairo_pixels = cairo_image_surface_get_data(surface);
for (j = height; j; j--) {
guchar *p = gdk_pixels;
guchar *q = cairo_pixels;
if (n_channels == 3) {
guchar *end = p + 3 * width;
while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
q[0] = p[2];
q[1] = p[1];
q[2] = p[0];
#else
q[1] = p[0];
q[2] = p[1];
q[3] = p[2];
#endif
p += 3;
q += 4;
}
} else {
guchar *end = p + 4 * width;
guint t1,t2,t3;
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
MULT(q[0], p[2], p[3], t1);
MULT(q[1], p[1], p[3], t2);
MULT(q[2], p[0], p[3], t3);
q[3] = p[3];
#else
q[0] = p[3];
MULT(q[1], p[0], p[3], t1);
MULT(q[2], p[1], p[3], t2);
MULT(q[3], p[2], p[3], t3);
#endif
p += 4;
q += 4;
}
#undef MULT
}
gdk_pixels += gdk_rowstride;
cairo_pixels += cairo_stride;
}
return surface;
}
|