1 #include <stdint.h>
2 #include <cairo.h>
3 #include "cairo_util.h"
4 #if HAVE_GDK_PIXBUF
5 #include <gdk-pixbuf/gdk-pixbuf.h>
6 #endif
7
cairo_set_source_u32(cairo_t * cairo,uint32_t color)8 void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
9 cairo_set_source_rgba(cairo,
10 (color >> (3*8) & 0xFF) / 255.0,
11 (color >> (2*8) & 0xFF) / 255.0,
12 (color >> (1*8) & 0xFF) / 255.0,
13 (color >> (0*8) & 0xFF) / 255.0);
14 }
15
to_cairo_subpixel_order(enum wl_output_subpixel subpixel)16 cairo_subpixel_order_t to_cairo_subpixel_order(enum wl_output_subpixel subpixel) {
17 switch (subpixel) {
18 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
19 return CAIRO_SUBPIXEL_ORDER_RGB;
20 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
21 return CAIRO_SUBPIXEL_ORDER_BGR;
22 case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
23 return CAIRO_SUBPIXEL_ORDER_VRGB;
24 case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
25 return CAIRO_SUBPIXEL_ORDER_VBGR;
26 default:
27 return CAIRO_SUBPIXEL_ORDER_DEFAULT;
28 }
29 return CAIRO_SUBPIXEL_ORDER_DEFAULT;
30 }
31
32 #if HAVE_GDK_PIXBUF
gdk_cairo_image_surface_create_from_pixbuf(const GdkPixbuf * gdkbuf)33 cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *gdkbuf) {
34 int chan = gdk_pixbuf_get_n_channels(gdkbuf);
35 if (chan < 3) {
36 return NULL;
37 }
38
39 const guint8* gdkpix = gdk_pixbuf_read_pixels(gdkbuf);
40 if (!gdkpix) {
41 return NULL;
42 }
43 gint w = gdk_pixbuf_get_width(gdkbuf);
44 gint h = gdk_pixbuf_get_height(gdkbuf);
45 int stride = gdk_pixbuf_get_rowstride(gdkbuf);
46
47 cairo_format_t fmt = (chan == 3) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32;
48 cairo_surface_t * cs = cairo_image_surface_create (fmt, w, h);
49 cairo_surface_flush (cs);
50 if ( !cs || cairo_surface_status(cs) != CAIRO_STATUS_SUCCESS) {
51 return NULL;
52 }
53
54 int cstride = cairo_image_surface_get_stride(cs);
55 unsigned char * cpix = cairo_image_surface_get_data(cs);
56
57 if (chan == 3) {
58 int i;
59 for (i = h; i; --i) {
60 const guint8 *gp = gdkpix;
61 unsigned char *cp = cpix;
62 const guint8* end = gp + 3*w;
63 while (gp < end) {
64 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
65 cp[0] = gp[2];
66 cp[1] = gp[1];
67 cp[2] = gp[0];
68 #else
69 cp[1] = gp[0];
70 cp[2] = gp[1];
71 cp[3] = gp[2];
72 #endif
73 gp += 3;
74 cp += 4;
75 }
76 gdkpix += stride;
77 cpix += cstride;
78 }
79 } else {
80 /* premul-color = alpha/255 * color/255 * 255 = (alpha*color)/255
81 * (z/255) = z/256 * 256/255 = z/256 (1 + 1/255)
82 * = z/256 + (z/256)/255 = (z + z/255)/256
83 * # recurse once
84 * = (z + (z + z/255)/256)/256
85 * = (z + z/256 + z/256/255) / 256
86 * # only use 16bit uint operations, loose some precision,
87 * # result is floored.
88 * -> (z + z>>8)>>8
89 * # add 0x80/255 = 0.5 to convert floor to round
90 * => (z+0x80 + (z+0x80)>>8 ) >> 8
91 * ------
92 * tested as equal to lround(z/255.0) for uint z in [0..0xfe02]
93 */
94 #define PREMUL_ALPHA(x,a,b,z) \
95 G_STMT_START { z = a * b + 0x80; x = (z + (z >> 8)) >> 8; } \
96 G_STMT_END
97 int i;
98 for (i = h; i; --i) {
99 const guint8 *gp = gdkpix;
100 unsigned char *cp = cpix;
101 const guint8* end = gp + 4*w;
102 guint z1, z2, z3;
103 while (gp < end) {
104 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
105 PREMUL_ALPHA(cp[0], gp[2], gp[3], z1);
106 PREMUL_ALPHA(cp[1], gp[1], gp[3], z2);
107 PREMUL_ALPHA(cp[2], gp[0], gp[3], z3);
108 cp[3] = gp[3];
109 #else
110 PREMUL_ALPHA(cp[1], gp[0], gp[3], z1);
111 PREMUL_ALPHA(cp[2], gp[1], gp[3], z2);
112 PREMUL_ALPHA(cp[3], gp[2], gp[3], z3);
113 cp[0] = gp[3];
114 #endif
115 gp += 4;
116 cp += 4;
117 }
118 gdkpix += stride;
119 cpix += cstride;
120 }
121 #undef PREMUL_ALPHA
122 }
123 cairo_surface_mark_dirty(cs);
124 return cs;
125 }
126 #endif // HAVE_GDK_PIXBUF
127