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