|
|
1 | #include <string.h> |
1 | #include <math.h> |
2 | #include <math.h> |
2 | #include "../config.h" |
3 | #include "../config.h" |
3 | #include "rxvt.h" |
4 | #include "rxvt.h" |
4 | |
5 | |
5 | #if HAVE_IMG |
6 | #if HAVE_IMG |
6 | |
7 | |
7 | #define float_to_component(d) ((d) * 65535.99) |
8 | typedef rxvt_img::nv nv; |
8 | |
9 | |
|
|
10 | namespace |
|
|
11 | { |
|
|
12 | |
|
|
13 | struct mat3x3 |
|
|
14 | { |
|
|
15 | nv v[3][3]; |
|
|
16 | |
|
|
17 | mat3x3 () |
|
|
18 | { |
|
|
19 | } |
|
|
20 | |
|
|
21 | mat3x3 (nv matrix[3][3]) |
|
|
22 | { |
|
|
23 | memcpy (v, matrix, sizeof (v)); |
|
|
24 | } |
|
|
25 | |
|
|
26 | mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33) |
|
|
27 | { |
|
|
28 | v[0][0] = v11; v[0][1] = v12; v[0][2] = v13; |
|
|
29 | v[1][0] = v21; v[1][1] = v22; v[1][2] = v23; |
|
|
30 | v[2][0] = v31; v[2][1] = v32; v[2][2] = v33; |
|
|
31 | } |
|
|
32 | |
|
|
33 | mat3x3 invert (); |
|
|
34 | |
|
|
35 | nv *operator [](int i) { return &v[i][0]; } |
|
|
36 | const nv *operator [](int i) const { return &v[i][0]; } |
|
|
37 | |
|
|
38 | // quite inefficient, hopefully gcc pulls the w calc out of any loops |
|
|
39 | nv apply1 (int i, nv x, nv y) |
|
|
40 | { |
|
|
41 | mat3x3 &m = *this; |
|
|
42 | |
|
|
43 | nv v = m[i][0] * x + m[i][1] * y + m[i][2]; |
|
|
44 | nv w = m[2][0] * x + m[2][1] * y + m[2][2]; |
|
|
45 | |
|
|
46 | return v * (1. / w); |
|
|
47 | } |
|
|
48 | |
|
|
49 | static mat3x3 translate (nv x, nv y); |
|
|
50 | }; |
|
|
51 | |
|
|
52 | mat3x3 |
|
|
53 | mat3x3::invert () |
|
|
54 | { |
|
|
55 | mat3x3 &m = *this; |
|
|
56 | mat3x3 inv; |
|
|
57 | |
|
|
58 | nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2]; |
|
|
59 | nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1]; |
|
|
60 | nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2]; |
|
|
61 | |
|
|
62 | nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2); |
|
|
63 | |
|
|
64 | inv[0][0] = invdet * s0; |
|
|
65 | inv[0][1] = invdet * s1; |
|
|
66 | inv[0][2] = invdet * s2; |
|
|
67 | |
|
|
68 | inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]); |
|
|
69 | inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]); |
|
|
70 | inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]); |
|
|
71 | |
|
|
72 | inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]); |
|
|
73 | inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]); |
|
|
74 | inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]); |
|
|
75 | |
|
|
76 | return inv; |
|
|
77 | } |
|
|
78 | |
|
|
79 | static mat3x3 |
|
|
80 | operator *(const mat3x3 &a, const mat3x3 &b) |
|
|
81 | { |
|
|
82 | mat3x3 r; |
|
|
83 | |
|
|
84 | for (int i = 0; i < 3; ++i) |
|
|
85 | for (int j = 0; j < 3; ++j) |
|
|
86 | r[i][j] = a[i][0] * b[0][j] |
|
|
87 | + a[i][1] * b[1][j] |
|
|
88 | + a[i][2] * b[2][j]; |
|
|
89 | |
|
|
90 | return r; |
|
|
91 | } |
|
|
92 | |
|
|
93 | mat3x3 |
|
|
94 | mat3x3::translate (nv x, nv y) |
|
|
95 | { |
|
|
96 | return mat3x3 ( |
|
|
97 | 1, 0, x, |
|
|
98 | 0, 1, y, |
|
|
99 | 0, 0, 1 |
|
|
100 | ); |
|
|
101 | } |
|
|
102 | |
|
|
103 | } |
|
|
104 | |
|
|
105 | #if 0 |
|
|
106 | struct pict |
|
|
107 | { |
|
|
108 | Display *dpy; |
|
|
109 | Picture pic; |
|
|
110 | |
|
|
111 | operator Picture () const |
|
|
112 | { |
|
|
113 | return pic; |
|
|
114 | } |
|
|
115 | |
|
|
116 | pict () |
|
|
117 | : pic (0) |
|
|
118 | { |
|
|
119 | } |
|
|
120 | |
|
|
121 | pict (rxvt_img *img, XRenderPictFormat *format = 0) |
|
|
122 | : dpy (img->s->display->dpy) |
|
|
123 | { |
|
|
124 | XRenderPictureAttributes pa; |
|
|
125 | pa.repeat = img->repeat; |
|
|
126 | pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); |
|
|
127 | } |
|
|
128 | |
|
|
129 | ~pict () |
|
|
130 | { |
|
|
131 | if (pic) |
|
|
132 | XRenderFreePicture (dpy, pic); |
|
|
133 | } |
|
|
134 | }; |
|
|
135 | #endif |
|
|
136 | |
|
|
137 | static XRenderPictFormat * |
|
|
138 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
139 | { |
|
|
140 | if (format->direct.alphaMask) |
|
|
141 | return format; // already has alpha |
|
|
142 | |
|
|
143 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
144 | if (format->type == PictTypeDirect) |
|
|
145 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
146 | if (f->direct.alphaMask |
|
|
147 | && f->type == PictTypeDirect |
|
|
148 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
149 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
150 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
151 | return f; |
|
|
152 | |
|
|
153 | // should be a very good fallback |
|
|
154 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
155 | } |
|
|
156 | |
9 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
157 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
10 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false) |
158 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
|
|
159 | pm(0), ref(0) |
11 | { |
160 | { |
12 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
13 | } |
161 | } |
14 | |
162 | |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
163 | rxvt_img::rxvt_img (const rxvt_img &img) |
16 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
164 | : s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), ref(img.ref) |
17 | { |
165 | { |
|
|
166 | ++ref->cnt; |
18 | } |
167 | } |
19 | |
168 | |
20 | rxvt_img * |
169 | rxvt_img * |
21 | rxvt_img::new_from_root (rxvt_screen *s) |
170 | rxvt_img::new_from_root (rxvt_screen *s) |
22 | { |
171 | { |
… | |
… | |
37 | return 0; |
186 | return 0; |
38 | |
187 | |
39 | rxvt_img *img = new rxvt_img ( |
188 | rxvt_img *img = new rxvt_img ( |
40 | s, |
189 | s, |
41 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
190 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
|
|
191 | 0, |
|
|
192 | 0, |
42 | root_pm_w, |
193 | root_pm_w, |
43 | root_pm_h, |
194 | root_pm_h |
44 | root_pixmap |
|
|
45 | ); |
195 | ); |
46 | |
196 | |
47 | img->shared = true; |
197 | img->pm = root_pixmap; |
|
|
198 | img->ref = new pixref (root_pm_w, root_pm_h); |
|
|
199 | img->ref->ours = false; |
|
|
200 | |
|
|
201 | return img; |
|
|
202 | } |
|
|
203 | |
|
|
204 | # if HAVE_PIXBUF |
|
|
205 | |
|
|
206 | rxvt_img * |
|
|
207 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
|
|
208 | { |
|
|
209 | Display *dpy = s->display->dpy; |
|
|
210 | |
|
|
211 | int width = gdk_pixbuf_get_width (pb); |
|
|
212 | int height = gdk_pixbuf_get_height (pb); |
|
|
213 | |
|
|
214 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
|
|
215 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
|
|
216 | |
|
|
217 | // since we require rgb24/argb32 formats from xrender we assume |
|
|
218 | // that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
|
|
219 | |
|
|
220 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
221 | |
|
|
222 | XImage xi; |
|
|
223 | |
|
|
224 | xi.width = width; |
|
|
225 | xi.height = height; |
|
|
226 | xi.xoffset = 0; |
|
|
227 | xi.format = ZPixmap; |
|
|
228 | xi.byte_order = ImageByteOrder (dpy); |
|
|
229 | xi.bitmap_unit = 0; //XY only, unused |
|
|
230 | xi.bitmap_bit_order = 0; //XY only, unused |
|
|
231 | xi.bitmap_pad = BitmapPad (dpy); |
|
|
232 | xi.depth = 32; |
|
|
233 | xi.bytes_per_line = 0; |
|
|
234 | xi.bits_per_pixel = 32; //Z only |
|
|
235 | xi.red_mask = 0x00000000; //Z only, unused |
|
|
236 | xi.green_mask = 0x00000000; //Z only, unused |
|
|
237 | xi.blue_mask = 0x00000000; //Z only, unused |
|
|
238 | xi.obdata = 0; // probably unused |
|
|
239 | |
|
|
240 | bool byte_order_mismatch = byte_order != xi.byte_order; |
|
|
241 | |
|
|
242 | if (!XInitImage (&xi)) |
|
|
243 | rxvt_fatal ("unable to initialise ximage, please report.\n"); |
|
|
244 | |
|
|
245 | if (height > INT_MAX / xi.bytes_per_line) |
|
|
246 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
|
|
247 | |
|
|
248 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
|
|
249 | |
|
|
250 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
|
|
251 | bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb); |
|
|
252 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
|
|
253 | |
|
|
254 | char *line = xi.data; |
|
|
255 | |
|
|
256 | for (int y = 0; y < height; y++) |
|
|
257 | { |
|
|
258 | unsigned char *src = row; |
|
|
259 | uint32_t *dst = (uint32_t *)line; |
|
|
260 | |
|
|
261 | if (!pb_has_alpha) |
|
|
262 | for (int x = 0; x < width; x++) |
|
|
263 | { |
|
|
264 | uint8_t r = *src++; |
|
|
265 | uint8_t g = *src++; |
|
|
266 | uint8_t b = *src++; |
|
|
267 | |
|
|
268 | uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; |
|
|
269 | |
|
|
270 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
|
|
271 | v = ecb_bswap32 (v); |
|
|
272 | |
|
|
273 | *dst++ = v; |
|
|
274 | } |
|
|
275 | else |
|
|
276 | for (int x = 0; x < width; x++) |
|
|
277 | { |
|
|
278 | uint32_t v = *(uint32_t *)src; src += 4; |
|
|
279 | |
|
|
280 | if (ecb_big_endian ()) |
|
|
281 | v = ecb_bswap32 (v); |
|
|
282 | |
|
|
283 | v = ecb_rotl32 (v, 8); // abgr to bgra |
|
|
284 | |
|
|
285 | if (!byte_order_mismatch) |
|
|
286 | v = ecb_bswap32 (v); |
|
|
287 | |
|
|
288 | *dst++ = v; |
|
|
289 | } |
|
|
290 | |
|
|
291 | row += rowstride; |
|
|
292 | line += xi.bytes_per_line; |
|
|
293 | } |
|
|
294 | |
|
|
295 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); |
|
|
296 | img->alloc (); |
|
|
297 | |
|
|
298 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
|
|
299 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
|
|
300 | XFreeGC (dpy, gc); |
|
|
301 | |
|
|
302 | free (xi.data); |
48 | |
303 | |
49 | return img; |
304 | return img; |
50 | } |
305 | } |
51 | |
306 | |
52 | rxvt_img * |
307 | rxvt_img * |
… | |
… | |
56 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
311 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
57 | |
312 | |
58 | if (!pb) |
313 | if (!pb) |
59 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
314 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
60 | |
315 | |
61 | rxvt_img *img = new rxvt_img ( |
316 | rxvt_img *img = new_from_pixbuf (s, pb); |
62 | s, |
|
|
63 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
|
|
64 | gdk_pixbuf_get_width (pb), |
|
|
65 | gdk_pixbuf_get_height (pb) |
|
|
66 | ); |
|
|
67 | |
|
|
68 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
|
|
69 | |
317 | |
70 | g_object_unref (pb); |
318 | g_object_unref (pb); |
71 | |
319 | |
72 | return img; |
320 | return img; |
73 | } |
321 | } |
74 | |
322 | |
|
|
323 | # endif |
|
|
324 | |
|
|
325 | void |
|
|
326 | rxvt_img::destroy () |
|
|
327 | { |
|
|
328 | if (--ref->cnt) |
|
|
329 | return; |
|
|
330 | |
|
|
331 | if (pm && ref->ours) |
|
|
332 | XFreePixmap (s->display->dpy, pm); |
|
|
333 | |
|
|
334 | delete ref; |
|
|
335 | } |
|
|
336 | |
75 | rxvt_img::~rxvt_img () |
337 | rxvt_img::~rxvt_img () |
76 | { |
338 | { |
77 | if (!shared) |
339 | destroy (); |
78 | XFreePixmap (s->display->dpy, pm); |
340 | } |
|
|
341 | |
|
|
342 | void |
|
|
343 | rxvt_img::alloc () |
|
|
344 | { |
|
|
345 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
346 | ref = new pixref (w, h); |
|
|
347 | } |
|
|
348 | |
|
|
349 | Picture |
|
|
350 | rxvt_img::picture () |
|
|
351 | { |
|
|
352 | Display *dpy = s->display->dpy; |
|
|
353 | |
|
|
354 | XRenderPictureAttributes pa; |
|
|
355 | pa.repeat = repeat; |
|
|
356 | Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
357 | |
|
|
358 | return pic; |
79 | } |
359 | } |
80 | |
360 | |
81 | void |
361 | void |
82 | rxvt_img::unshare () |
362 | rxvt_img::unshare () |
83 | { |
363 | { |
84 | if (!shared) |
364 | if (ref->cnt == 1 && ref->ours) |
85 | return; |
365 | return; |
86 | |
366 | |
87 | rxvt_img *img = clone (); |
367 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
|
|
368 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
369 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
|
|
370 | XFreeGC (s->display->dpy, gc); |
88 | |
371 | |
89 | ::swap (pm , img->pm); |
372 | destroy (); |
90 | ::swap (shared, img->shared); |
373 | |
|
|
374 | pm = pm2; |
|
|
375 | ref = new pixref (ref->w, ref->h); |
|
|
376 | } |
|
|
377 | |
|
|
378 | void |
|
|
379 | rxvt_img::fill (const rgba &c) |
|
|
380 | { |
|
|
381 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
|
|
382 | |
|
|
383 | Display *dpy = s->display->dpy; |
|
|
384 | Picture src = picture (); |
|
|
385 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
|
|
386 | XRenderFreePicture (dpy, src); |
|
|
387 | } |
|
|
388 | |
|
|
389 | void |
|
|
390 | rxvt_img::add_alpha () |
|
|
391 | { |
|
|
392 | if (format->direct.alphaMask) |
|
|
393 | return; |
|
|
394 | |
|
|
395 | Display *dpy = s->display->dpy; |
|
|
396 | |
|
|
397 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
|
|
398 | img->alloc (); |
|
|
399 | |
|
|
400 | Picture src = picture (); |
|
|
401 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
402 | |
|
|
403 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
404 | |
|
|
405 | XRenderFreePicture (dpy, src); |
|
|
406 | XRenderFreePicture (dpy, dst); |
|
|
407 | |
|
|
408 | ::swap (img->ref, ref); |
|
|
409 | ::swap (img->pm , pm ); |
91 | |
410 | |
92 | delete img; |
411 | delete img; |
93 | } |
412 | } |
94 | |
413 | |
95 | void |
|
|
96 | rxvt_img::fill (const rxvt_color &c) |
|
|
97 | { |
|
|
98 | XGCValues gcv; |
|
|
99 | gcv.foreground = c; |
|
|
100 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
|
|
101 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
|
|
102 | XFreeGC (s->display->dpy, gc); |
|
|
103 | } |
|
|
104 | |
|
|
105 | static void |
414 | static void |
106 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
415 | get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params) |
107 | { |
416 | { |
108 | double sigma = radius / 2.0; |
417 | nv sigma = radius / 2.0; |
109 | double scale = sqrt (2.0 * M_PI) * sigma; |
418 | nv scale = sqrt (2.0 * M_PI) * sigma; |
110 | double sum = 0.0; |
419 | nv sum = 0.0; |
111 | |
420 | |
112 | for (int i = 0; i < width; i++) |
421 | for (int i = 0; i < width; i++) |
113 | { |
422 | { |
114 | double x = i - width / 2; |
423 | nv x = i - width / 2; |
115 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
424 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
116 | sum += kernel[i]; |
425 | sum += kernel[i]; |
117 | } |
426 | } |
118 | |
427 | |
119 | params[0] = XDoubleToFixed (width); |
428 | params[0] = XDoubleToFixed (width); |
… | |
… | |
129 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
438 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
130 | return clone (); |
439 | return clone (); |
131 | |
440 | |
132 | Display *dpy = s->display->dpy; |
441 | Display *dpy = s->display->dpy; |
133 | int size = max (rh, rv) * 2 + 1; |
442 | int size = max (rh, rv) * 2 + 1; |
134 | double *kernel = (double *)malloc (size * sizeof (double)); |
443 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
135 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
444 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
136 | rxvt_img *img = new rxvt_img (s, format, w, h); |
445 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
|
|
446 | img->alloc (); |
137 | |
447 | |
138 | XRenderPictureAttributes pa; |
448 | XRenderPictureAttributes pa; |
139 | |
|
|
140 | pa.repeat = RepeatPad; |
449 | pa.repeat = RepeatPad; |
141 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
450 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
142 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
451 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
143 | |
452 | |
144 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
453 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
145 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
454 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
146 | XFreePixmap (dpy, tmp_pm); |
455 | XFreePixmap (dpy, tmp_pm); |
147 | |
456 | |
… | |
… | |
163 | |
472 | |
164 | size = rv * 2 + 1; |
473 | size = rv * 2 + 1; |
165 | get_gaussian_kernel (rv, size, kernel, params); |
474 | get_gaussian_kernel (rv, size, kernel, params); |
166 | ::swap (params[0], params[1]); |
475 | ::swap (params[0], params[1]); |
167 | |
476 | |
168 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
477 | XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); |
169 | XRenderComposite (dpy, |
478 | XRenderComposite (dpy, |
170 | PictOpSrc, |
479 | PictOpSrc, |
171 | tmp, |
480 | tmp, |
172 | None, |
481 | None, |
173 | dst, |
482 | dst, |
… | |
… | |
177 | w, h); |
486 | w, h); |
178 | } |
487 | } |
179 | |
488 | |
180 | free (kernel); |
489 | free (kernel); |
181 | free (params); |
490 | free (params); |
|
|
491 | |
182 | XRenderFreePicture (dpy, src); |
492 | XRenderFreePicture (dpy, src); |
183 | XRenderFreePicture (dpy, dst); |
493 | XRenderFreePicture (dpy, dst); |
184 | XRenderFreePicture (dpy, tmp); |
494 | XRenderFreePicture (dpy, tmp); |
185 | |
495 | |
186 | return img; |
496 | return img; |
187 | } |
497 | } |
188 | |
498 | |
189 | static Picture |
499 | static Picture |
190 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
500 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
191 | { |
501 | { |
192 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
502 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
193 | |
503 | |
194 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
504 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
195 | XRenderPictureAttributes pa; |
505 | XRenderPictureAttributes pa; |
196 | pa.repeat = True; |
506 | pa.repeat = RepeatNormal; |
|
|
507 | pa.component_alpha = component_alpha; |
197 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
508 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
198 | |
509 | |
199 | XFreePixmap (dpy, pixmap); |
510 | XFreePixmap (dpy, pixmap); |
200 | |
511 | |
201 | return mask; |
512 | return mask; |
202 | } |
513 | } |
203 | |
514 | |
|
|
515 | static void |
|
|
516 | extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) |
|
|
517 | { |
|
|
518 | int32_t x = clamp (c, cl0, cl1); |
|
|
519 | c -= x; |
|
|
520 | xc = x; |
|
|
521 | } |
|
|
522 | |
|
|
523 | static bool |
|
|
524 | extract (int32_t cl0, int32_t cl1, int32_t &r, int32_t &g, int32_t &b, int32_t &a, unsigned short &xr, unsigned short &xg, unsigned short &xb, unsigned short &xa) |
|
|
525 | { |
|
|
526 | extract (cl0, cl1, r, xr); |
|
|
527 | extract (cl0, cl1, g, xg); |
|
|
528 | extract (cl0, cl1, b, xb); |
|
|
529 | extract (cl0, cl1, a, xa); |
|
|
530 | |
|
|
531 | return xr | xg | xb | xa; |
|
|
532 | } |
|
|
533 | |
204 | void |
534 | void |
205 | rxvt_img::brightness (double r, double g, double b, double a) |
535 | rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) |
206 | { |
536 | { |
|
|
537 | unshare (); |
|
|
538 | |
207 | Display *dpy = s->display->dpy; |
539 | Display *dpy = s->display->dpy; |
208 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
209 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
540 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
210 | |
541 | |
|
|
542 | // loop should not be needed for brightness, as only -1..1 makes sense |
|
|
543 | //while (r | g | b | a) |
|
|
544 | { |
|
|
545 | unsigned short xr, xg, xb, xa; |
211 | XRenderColor mask_c; |
546 | XRenderColor mask_c; |
212 | mask_c.red = float_to_component (r); |
547 | |
213 | mask_c.green = float_to_component (g); |
548 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
214 | mask_c.blue = float_to_component (b); |
549 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
215 | mask_c.alpha = float_to_component (a); |
550 | |
|
|
551 | if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
552 | { |
|
|
553 | XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
|
|
554 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
555 | mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing |
|
|
556 | mask_c.green = -mask_c.green; |
|
|
557 | mask_c.blue = -mask_c.blue; |
|
|
558 | mask_c.alpha = -mask_c.alpha; |
|
|
559 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
|
|
560 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
561 | } |
|
|
562 | } |
|
|
563 | |
|
|
564 | XRenderFreePicture (dpy, dst); |
|
|
565 | } |
|
|
566 | |
|
|
567 | void |
|
|
568 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
|
|
569 | { |
|
|
570 | if (r < 0 || g < 0 || b < 0 || a < 0) |
|
|
571 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
|
|
572 | |
|
|
573 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
|
|
574 | img->alloc (); |
|
|
575 | img->fill (rgba (0, 0, 0, 0)); |
|
|
576 | |
|
|
577 | // premultiply (yeah, these are not exact, sue me or fix it) |
|
|
578 | r = (r * (a >> 8)) >> 8; |
|
|
579 | g = (g * (a >> 8)) >> 8; |
|
|
580 | b = (b * (a >> 8)) >> 8; |
|
|
581 | |
|
|
582 | Display *dpy = s->display->dpy; |
|
|
583 | |
|
|
584 | Picture src = picture (); |
|
|
585 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
|
|
586 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
|
|
587 | |
|
|
588 | //TODO: this operator does not yet implement some useful contrast |
|
|
589 | while (r | g | b | a) |
|
|
590 | { |
|
|
591 | unsigned short xr, xg, xb, xa; |
|
|
592 | XRenderColor mask_c; |
|
|
593 | |
|
|
594 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
595 | { |
|
|
596 | XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
|
|
597 | XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
598 | } |
|
|
599 | } |
|
|
600 | |
|
|
601 | XRenderFreePicture (dpy, mul); |
|
|
602 | XRenderFreePicture (dpy, dst); |
|
|
603 | XRenderFreePicture (dpy, src); |
|
|
604 | |
|
|
605 | ::swap (img->ref, ref); |
|
|
606 | ::swap (img->pm , pm ); |
|
|
607 | |
|
|
608 | delete img; |
|
|
609 | } |
|
|
610 | |
|
|
611 | void |
|
|
612 | rxvt_img::draw (rxvt_img *img, int op, nv mask) |
|
|
613 | { |
|
|
614 | unshare (); |
|
|
615 | |
|
|
616 | Display *dpy = s->display->dpy; |
|
|
617 | Picture src = img->picture (); |
|
|
618 | Picture dst = picture (); |
|
|
619 | Picture mask_p = 0; |
|
|
620 | |
|
|
621 | if (mask != 1.) |
|
|
622 | { |
|
|
623 | mask_p = create_xrender_mask (dpy, img->pm, False, False); |
|
|
624 | XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; |
216 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
625 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
626 | } |
217 | |
627 | |
218 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
628 | XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); |
219 | |
629 | |
220 | XRenderFreePicture (dpy, src); |
630 | XRenderFreePicture (dpy, src); |
221 | XRenderFreePicture (dpy, dst); |
631 | XRenderFreePicture (dpy, dst); |
222 | } |
|
|
223 | |
632 | |
224 | void |
633 | if (mask_p) |
225 | rxvt_img::contrast (double r, double g, double b, double a) |
634 | XRenderFreePicture (dpy, mask_p); |
226 | { |
635 | } |
227 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
228 | return; |
|
|
229 | |
636 | |
|
|
637 | rxvt_img * |
|
|
638 | rxvt_img::clone () |
|
|
639 | { |
|
|
640 | return new rxvt_img (*this); |
|
|
641 | } |
|
|
642 | |
|
|
643 | rxvt_img * |
|
|
644 | rxvt_img::reify () |
|
|
645 | { |
|
|
646 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
|
|
647 | return clone (); |
|
|
648 | |
230 | Display *dpy = s->display->dpy; |
649 | Display *dpy = s->display->dpy; |
231 | Picture src = create_xrender_mask (dpy, pm, True); |
650 | |
|
|
651 | // add an alpha channel if... |
|
|
652 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
|
|
653 | && (x || y) // we need one because of non-zero offset |
|
|
654 | && repeat == RepeatNone; // and we have no good pixels to fill with |
|
|
655 | |
|
|
656 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
|
|
657 | img->alloc (); |
|
|
658 | |
|
|
659 | Picture src = picture (); |
232 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
660 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
233 | |
661 | |
234 | XRenderColor mask_c; |
662 | if (alpha) |
235 | mask_c.red = float_to_component (r); |
663 | { |
236 | mask_c.green = float_to_component (g); |
664 | XRenderColor rc = { 0, 0, 0, 0 }; |
237 | mask_c.blue = float_to_component (b); |
665 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
238 | mask_c.alpha = float_to_component (a); |
666 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
239 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
667 | } |
240 | |
668 | else |
241 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
669 | XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); |
242 | |
670 | |
243 | XRenderFreePicture (dpy, src); |
671 | XRenderFreePicture (dpy, src); |
244 | XRenderFreePicture (dpy, dst); |
672 | XRenderFreePicture (dpy, dst); |
245 | } |
|
|
246 | |
|
|
247 | bool |
|
|
248 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
|
|
249 | { |
|
|
250 | Display *dpy = s->display->dpy; |
|
|
251 | |
|
|
252 | if (s->visual->c_class != TrueColor) |
|
|
253 | return false; |
|
|
254 | |
|
|
255 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
|
|
256 | |
|
|
257 | red_mask = (uint32_t)format->direct.redMask << format->direct.red; |
|
|
258 | green_mask = (uint32_t)format->direct.greenMask << format->direct.green; |
|
|
259 | blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; |
|
|
260 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
|
|
261 | |
|
|
262 | int width_r = ecb_popcount32 (red_mask); |
|
|
263 | int width_g = ecb_popcount32 (green_mask); |
|
|
264 | int width_b = ecb_popcount32 (blue_mask); |
|
|
265 | int width_a = ecb_popcount32 (alpha_mask); |
|
|
266 | |
|
|
267 | if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) |
|
|
268 | return false; |
|
|
269 | |
|
|
270 | int sh_r = ecb_ctz32 (red_mask); |
|
|
271 | int sh_g = ecb_ctz32 (green_mask); |
|
|
272 | int sh_b = ecb_ctz32 (blue_mask); |
|
|
273 | int sh_a = ecb_ctz32 (alpha_mask); |
|
|
274 | |
|
|
275 | if (width > 32767 || height > 32767) |
|
|
276 | return false; |
|
|
277 | |
|
|
278 | XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, |
|
|
279 | width, height, 32, 0); |
|
|
280 | if (!ximage) |
|
|
281 | return false; |
|
|
282 | |
|
|
283 | if (height > INT_MAX / ximage->bytes_per_line |
|
|
284 | || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) |
|
|
285 | { |
|
|
286 | XDestroyImage (ximage); |
|
|
287 | return false; |
|
|
288 | } |
|
|
289 | |
|
|
290 | GC gc = XCreateGC (dpy, pm, 0, 0); |
|
|
291 | |
|
|
292 | ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
293 | |
|
|
294 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
|
|
295 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
|
|
296 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
|
|
297 | char *line = ximage->data; |
|
|
298 | |
|
|
299 | for (int y = 0; y < height; y++) |
|
|
300 | { |
|
|
301 | for (int x = 0; x < width; x++) |
|
|
302 | { |
|
|
303 | unsigned char *pixel = row + x * channels; |
|
|
304 | uint32_t value; |
|
|
305 | unsigned char r, g, b, a; |
|
|
306 | |
|
|
307 | if (channels == 4) |
|
|
308 | { |
|
|
309 | a = pixel[3]; |
|
|
310 | r = pixel[0] * a / 0xff; |
|
|
311 | g = pixel[1] * a / 0xff; |
|
|
312 | b = pixel[2] * a / 0xff; |
|
|
313 | } |
|
|
314 | else |
|
|
315 | { |
|
|
316 | a = 0xff; |
|
|
317 | r = pixel[0]; |
|
|
318 | g = pixel[1]; |
|
|
319 | b = pixel[2]; |
|
|
320 | } |
|
|
321 | |
|
|
322 | value = ((r >> (8 - width_r)) << sh_r) |
|
|
323 | | ((g >> (8 - width_g)) << sh_g) |
|
|
324 | | ((b >> (8 - width_b)) << sh_b) |
|
|
325 | | ((a >> (8 - width_a)) << sh_a); |
|
|
326 | |
|
|
327 | if (ximage->bits_per_pixel == 32) |
|
|
328 | ((uint32_t *)line)[x] = value; |
|
|
329 | else |
|
|
330 | XPutPixel (ximage, x, y, value); |
|
|
331 | } |
|
|
332 | |
|
|
333 | row += rowstride; |
|
|
334 | line += ximage->bytes_per_line; |
|
|
335 | } |
|
|
336 | |
|
|
337 | XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); |
|
|
338 | XDestroyImage (ximage); |
|
|
339 | XFreeGC (dpy, gc); |
|
|
340 | |
|
|
341 | return true; |
|
|
342 | } |
|
|
343 | |
|
|
344 | rxvt_img * |
|
|
345 | rxvt_img::clone () |
|
|
346 | { |
|
|
347 | rxvt_img *img = new rxvt_img (s, format, w, h); |
|
|
348 | |
|
|
349 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
350 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
|
|
351 | XFreeGC (s->display->dpy, gc); |
|
|
352 | |
673 | |
353 | return img; |
674 | return img; |
354 | } |
675 | } |
355 | |
676 | |
356 | rxvt_img * |
677 | rxvt_img * |
357 | rxvt_img::sub_rect (int x, int y, int width, int height) |
678 | rxvt_img::sub_rect (int x, int y, int width, int height) |
358 | { |
679 | { |
|
|
680 | rxvt_img *img = clone (); |
|
|
681 | |
|
|
682 | img->x -= x; |
|
|
683 | img->y -= y; |
|
|
684 | |
|
|
685 | if (w != width || h != height) |
|
|
686 | { |
|
|
687 | img->w = width; |
|
|
688 | img->h = height; |
|
|
689 | |
|
|
690 | rxvt_img *img2 = img->reify (); |
|
|
691 | delete img; |
|
|
692 | img = img2; |
|
|
693 | } |
|
|
694 | |
|
|
695 | return img; |
|
|
696 | } |
|
|
697 | |
|
|
698 | rxvt_img * |
|
|
699 | rxvt_img::transform (nv matrix[3][3]) |
|
|
700 | { |
|
|
701 | // calculate new pixel bounding box coordinates |
|
|
702 | nv r[2], rmin[2], rmax[2]; |
|
|
703 | |
|
|
704 | mat3x3 m (matrix); |
|
|
705 | |
|
|
706 | for (int i = 0; i < 2; ++i) |
|
|
707 | { |
|
|
708 | nv v; |
|
|
709 | |
|
|
710 | v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v; |
|
|
711 | v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
712 | v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
713 | v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
714 | } |
|
|
715 | |
|
|
716 | float sx = rmin [0] - x; |
|
|
717 | float sy = rmin [1] - y; |
|
|
718 | |
|
|
719 | // TODO: adjust matrix for subpixel accuracy |
|
|
720 | int nx = floor (rmin [0]); |
|
|
721 | int ny = floor (rmin [1]); |
|
|
722 | |
|
|
723 | int new_width = ceil (rmax [0] - rmin [0]); |
|
|
724 | int new_height = ceil (rmax [1] - rmin [1]); |
|
|
725 | |
|
|
726 | m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y); |
|
|
727 | |
|
|
728 | mat3x3 inv = m.invert (); |
|
|
729 | |
359 | rxvt_img *img = new rxvt_img (s, format, width, height); |
730 | rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat); |
|
|
731 | img->alloc (); |
360 | |
732 | |
361 | Display *dpy = s->display->dpy; |
733 | Display *dpy = s->display->dpy; |
362 | XRenderPictureAttributes pa; |
734 | Picture src = picture (); |
363 | pa.repeat = repeat; |
|
|
364 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
365 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
735 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
366 | |
736 | |
|
|
737 | XTransform xfrm; |
|
|
738 | |
|
|
739 | for (int i = 0; i < 3; ++i) |
|
|
740 | for (int j = 0; j < 3; ++j) |
|
|
741 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
|
|
742 | |
|
|
743 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
|
|
744 | XRenderSetPictureTransform (dpy, src, &xfrm); |
367 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
745 | XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
368 | |
746 | |
369 | XRenderFreePicture (dpy, src); |
747 | XRenderFreePicture (dpy, src); |
370 | XRenderFreePicture (dpy, dst); |
748 | XRenderFreePicture (dpy, dst); |
371 | |
749 | |
372 | return img; |
750 | return img; |
373 | } |
751 | } |
374 | |
752 | |
375 | rxvt_img * |
753 | rxvt_img * |
376 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
754 | rxvt_img::scale (int new_width, int new_height) |
377 | { |
755 | { |
378 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
756 | if (w == new_width && h == new_height) |
|
|
757 | return clone (); |
379 | |
758 | |
380 | Display *dpy = s->display->dpy; |
759 | nv matrix[3][3] = { |
381 | XRenderPictureAttributes pa; |
760 | { new_width / (nv)w, 0, 0 }, |
|
|
761 | { 0, new_height / (nv)h, 0 }, |
|
|
762 | { 0, 0, 1 } |
|
|
763 | }; |
|
|
764 | |
|
|
765 | int old_repeat_mode = repeat; |
|
|
766 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
|
|
767 | |
|
|
768 | rxvt_img *img = transform (matrix); |
|
|
769 | |
|
|
770 | repeat = old_repeat_mode; |
382 | pa.repeat = repeat; |
771 | img->repeat = repeat; |
383 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
772 | |
|
|
773 | return img; |
|
|
774 | } |
|
|
775 | |
|
|
776 | rxvt_img * |
|
|
777 | rxvt_img::rotate (int cx, int cy, nv phi) |
|
|
778 | { |
|
|
779 | nv s = sin (phi); |
|
|
780 | nv c = cos (phi); |
|
|
781 | |
|
|
782 | nv matrix[3][3] = { |
|
|
783 | #if 0 |
|
|
784 | { c, -s, cx - c * cx + s * cy }, |
|
|
785 | { s, c, cy - s * cx - c * cy }, |
|
|
786 | { 0, 0, 1 } |
|
|
787 | #else |
|
|
788 | { c, -s, 0 }, |
|
|
789 | { s, c, 0 }, |
|
|
790 | { 0, 0, 1 } |
|
|
791 | #endif |
|
|
792 | }; |
|
|
793 | |
|
|
794 | move (-cx, -cy); |
|
|
795 | rxvt_img *img = transform (matrix); |
|
|
796 | move ( cx, cy); |
|
|
797 | img->move (cx, cy); |
|
|
798 | |
|
|
799 | return img; |
|
|
800 | } |
|
|
801 | |
|
|
802 | rxvt_img * |
|
|
803 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
|
|
804 | { |
|
|
805 | if (new_format == format) |
|
|
806 | return clone (); |
|
|
807 | |
|
|
808 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
|
|
809 | img->alloc (); |
|
|
810 | |
|
|
811 | Display *dpy = s->display->dpy; |
|
|
812 | Picture src = picture (); |
384 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
813 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
814 | int op = PictOpSrc; |
385 | |
815 | |
386 | XTransform xfrm; |
816 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
|
|
817 | { |
|
|
818 | // does it have to be that complicated |
|
|
819 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
|
|
820 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
387 | |
821 | |
388 | for (int i = 0; i < 3; ++i) |
822 | op = PictOpOver; |
389 | for (int j = 0; j < 3; ++j) |
823 | } |
390 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
|
|
391 | |
824 | |
392 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
|
|
393 | XRenderSetPictureTransform (dpy, src, &xfrm); |
|
|
394 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
825 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
395 | |
826 | |
396 | XRenderFreePicture (dpy, src); |
827 | XRenderFreePicture (dpy, src); |
397 | XRenderFreePicture (dpy, dst); |
828 | XRenderFreePicture (dpy, dst); |
398 | |
829 | |
399 | return img; |
830 | return img; |
400 | } |
831 | } |
401 | |
832 | |
402 | rxvt_img * |
833 | rxvt_img * |
403 | rxvt_img::scale (int new_width, int new_height) |
|
|
404 | { |
|
|
405 | double matrix[9] = { |
|
|
406 | w / (double)new_width, 0, 0, |
|
|
407 | 0, h / (double)new_height, 0, |
|
|
408 | 0, 0, 1 |
|
|
409 | }; |
|
|
410 | |
|
|
411 | return transform (new_width, new_height, matrix); |
|
|
412 | } |
|
|
413 | |
|
|
414 | rxvt_img * |
|
|
415 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
|
|
416 | { |
|
|
417 | double s = sin (phi); |
|
|
418 | double c = cos (phi); |
|
|
419 | |
|
|
420 | double matrix[9] = { |
|
|
421 | c, -s, -c * x + s * y + x, |
|
|
422 | s, c, -s * x - c * y + y, |
|
|
423 | 0, 0, 1 |
|
|
424 | }; |
|
|
425 | |
|
|
426 | return transform (new_width, new_height, matrix); |
|
|
427 | } |
|
|
428 | |
|
|
429 | rxvt_img * |
|
|
430 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
|
|
431 | { |
|
|
432 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
|
|
433 | |
|
|
434 | Display *dpy = s->display->dpy; |
|
|
435 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
436 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
437 | int op = PictOpSrc; |
|
|
438 | |
|
|
439 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
|
|
440 | { |
|
|
441 | // does it have to be that complicated |
|
|
442 | rgba c; |
|
|
443 | bg.get (c); |
|
|
444 | |
|
|
445 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
|
|
446 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
|
|
447 | |
|
|
448 | op = PictOpOver; |
|
|
449 | } |
|
|
450 | |
|
|
451 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
452 | |
|
|
453 | XRenderFreePicture (dpy, src); |
|
|
454 | XRenderFreePicture (dpy, dst); |
|
|
455 | |
|
|
456 | return img; |
|
|
457 | } |
|
|
458 | |
|
|
459 | rxvt_img * |
|
|
460 | rxvt_img::blend (rxvt_img *img, double factor) |
834 | rxvt_img::blend (rxvt_img *img, nv factor) |
461 | { |
835 | { |
462 | rxvt_img *img2 = clone (); |
836 | rxvt_img *img2 = clone (); |
463 | Display *dpy = s->display->dpy; |
837 | Display *dpy = s->display->dpy; |
464 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
838 | Picture src = img->picture (); |
465 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
839 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
466 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
840 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
467 | |
841 | |
468 | XRenderColor mask_c; |
842 | XRenderColor mask_c; |
469 | |
843 | |
470 | mask_c.alpha = float_to_component (factor); |
844 | mask_c.alpha = float_to_component (factor); |
471 | mask_c.red = |
845 | mask_c.red = |