1 | #include <math.h> |
1 | #include <math.h> |
2 | #include "../config.h" |
2 | #include "../config.h" |
3 | #include "rxvt.h" |
3 | #include "rxvt.h" |
4 | |
4 | |
5 | #if HAVE_IMG |
5 | #if HAVE_IMG |
|
|
6 | |
|
|
7 | #if 0 |
|
|
8 | struct pict |
|
|
9 | { |
|
|
10 | Display *dpy; |
|
|
11 | Picture pic; |
|
|
12 | |
|
|
13 | operator Picture () const |
|
|
14 | { |
|
|
15 | return pic; |
|
|
16 | } |
|
|
17 | |
|
|
18 | pict () |
|
|
19 | : pic (0) |
|
|
20 | { |
|
|
21 | } |
|
|
22 | |
|
|
23 | pict (rxvt_img *img, XRenderPictFormat *format = 0) |
|
|
24 | : dpy (img->s->display->dpy) |
|
|
25 | { |
|
|
26 | XRenderPictureAttributes pa; |
|
|
27 | pa.repeat = img->repeat; |
|
|
28 | pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); |
|
|
29 | } |
|
|
30 | |
|
|
31 | ~pict () |
|
|
32 | { |
|
|
33 | if (pic) |
|
|
34 | XRenderFreePicture (dpy, pic); |
|
|
35 | } |
|
|
36 | }; |
|
|
37 | #endif |
|
|
38 | |
|
|
39 | static XRenderPictFormat * |
|
|
40 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
41 | { |
|
|
42 | if (format->direct.alphaMask) |
|
|
43 | return format; // already has alpha |
|
|
44 | |
|
|
45 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
46 | if (format->type == PictTypeDirect) |
|
|
47 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
48 | if (f->direct.alphaMask |
|
|
49 | && f->type == PictTypeDirect |
|
|
50 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
51 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
52 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
53 | return f; |
|
|
54 | |
|
|
55 | // should be a very good fallback |
|
|
56 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
57 | } |
6 | |
58 | |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
59 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
8 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
60 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
9 | pm(0), ref(0) |
61 | pm(0), ref(0) |
10 | { |
62 | { |
… | |
… | |
49 | img->ref->ours = false; |
101 | img->ref->ours = false; |
50 | |
102 | |
51 | return img; |
103 | return img; |
52 | } |
104 | } |
53 | |
105 | |
|
|
106 | # if HAVE_PIXBUF |
|
|
107 | |
54 | rxvt_img * |
108 | rxvt_img * |
55 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
109 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
56 | { |
110 | { |
57 | Display *dpy = s->display->dpy; |
111 | Display *dpy = s->display->dpy; |
58 | |
112 | |
… | |
… | |
62 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
116 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
63 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
117 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
64 | |
118 | |
65 | // since we require rgb24/argb32 formats from xrender we assume |
119 | // since we require rgb24/argb32 formats from xrender we assume |
66 | // that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
120 | // that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
67 | int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; |
|
|
68 | |
121 | |
69 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
122 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
70 | |
123 | |
71 | XImage xi; |
124 | XImage xi; |
72 | |
125 | |
… | |
… | |
76 | xi.format = ZPixmap; |
129 | xi.format = ZPixmap; |
77 | xi.byte_order = ImageByteOrder (dpy); |
130 | xi.byte_order = ImageByteOrder (dpy); |
78 | xi.bitmap_unit = 0; //XY only, unused |
131 | xi.bitmap_unit = 0; //XY only, unused |
79 | xi.bitmap_bit_order = 0; //XY only, unused |
132 | xi.bitmap_bit_order = 0; //XY only, unused |
80 | xi.bitmap_pad = BitmapPad (dpy); |
133 | xi.bitmap_pad = BitmapPad (dpy); |
81 | xi.depth = depth; |
134 | xi.depth = 32; |
82 | xi.bytes_per_line = 0; |
135 | xi.bytes_per_line = 0; |
83 | xi.bits_per_pixel = 32; //Z only |
136 | xi.bits_per_pixel = 32; //Z only |
84 | xi.red_mask = 0x00000000; //Z only, unused |
137 | xi.red_mask = 0x00000000; //Z only, unused |
85 | xi.green_mask = 0x00000000; //Z only, unused |
138 | xi.green_mask = 0x00000000; //Z only, unused |
86 | xi.blue_mask = 0x00000000; //Z only, unused |
139 | xi.blue_mask = 0x00000000; //Z only, unused |
… | |
… | |
95 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
148 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
96 | |
149 | |
97 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
150 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
98 | |
151 | |
99 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
152 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
100 | |
153 | bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb); |
101 | assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb)); |
|
|
102 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
154 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
|
|
155 | |
103 | char *line = xi.data; |
156 | char *line = xi.data; |
104 | |
157 | |
105 | for (int y = 0; y < height; y++) |
158 | for (int y = 0; y < height; y++) |
106 | { |
159 | { |
107 | unsigned char *src = row; |
160 | unsigned char *src = row; |
108 | uint32_t *dst = (uint32_t *)line; |
161 | uint32_t *dst = (uint32_t *)line; |
109 | |
162 | |
110 | if (depth == 24) |
163 | if (!pb_has_alpha) |
111 | for (int x = 0; x < width; x++) |
164 | for (int x = 0; x < width; x++) |
112 | { |
165 | { |
113 | uint8_t r = *src++; |
166 | uint8_t r = *src++; |
114 | uint8_t g = *src++; |
167 | uint8_t g = *src++; |
115 | uint8_t b = *src++; |
168 | uint8_t b = *src++; |
116 | |
169 | |
117 | uint32_t v = (r << 16) | (g << 8) | b; |
170 | uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; |
118 | |
171 | |
119 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
172 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
120 | v = ecb_bswap32 (v); |
173 | v = ecb_bswap32 (v); |
121 | |
174 | |
122 | *dst++ = v; |
175 | *dst++ = v; |
… | |
… | |
139 | |
192 | |
140 | row += rowstride; |
193 | row += rowstride; |
141 | line += xi.bytes_per_line; |
194 | line += xi.bytes_per_line; |
142 | } |
195 | } |
143 | |
196 | |
144 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); |
197 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); |
145 | img->alloc (); |
198 | img->alloc (); |
146 | |
199 | |
147 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
200 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
148 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
201 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
149 | XFreeGC (dpy, gc); |
202 | XFreeGC (dpy, gc); |
… | |
… | |
167 | g_object_unref (pb); |
220 | g_object_unref (pb); |
168 | |
221 | |
169 | return img; |
222 | return img; |
170 | } |
223 | } |
171 | |
224 | |
|
|
225 | # endif |
|
|
226 | |
172 | void |
227 | void |
173 | rxvt_img::destroy () |
228 | rxvt_img::destroy () |
174 | { |
229 | { |
175 | if (--ref->cnt) |
230 | if (--ref->cnt) |
176 | return; |
231 | return; |
… | |
… | |
192 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
247 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
193 | ref = new pixref (w, h); |
248 | ref = new pixref (w, h); |
194 | } |
249 | } |
195 | |
250 | |
196 | Picture |
251 | Picture |
197 | rxvt_img::src_picture () |
252 | rxvt_img::picture () |
198 | { |
253 | { |
199 | Display *dpy = s->display->dpy; |
254 | Display *dpy = s->display->dpy; |
200 | |
255 | |
201 | XRenderPictureAttributes pa; |
256 | XRenderPictureAttributes pa; |
202 | pa.repeat = repeat; |
257 | pa.repeat = repeat; |
… | |
… | |
221 | pm = pm2; |
276 | pm = pm2; |
222 | ref = new pixref (ref->w, ref->h); |
277 | ref = new pixref (ref->w, ref->h); |
223 | } |
278 | } |
224 | |
279 | |
225 | void |
280 | void |
226 | rxvt_img::fill (const rxvt_color &c) |
281 | rxvt_img::fill (const rgba &c) |
227 | { |
282 | { |
228 | XGCValues gcv; |
283 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
229 | gcv.foreground = c; |
284 | |
230 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
285 | Display *dpy = s->display->dpy; |
231 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
286 | Picture src = picture (); |
232 | XFreeGC (s->display->dpy, gc); |
287 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
|
|
288 | XRenderFreePicture (dpy, src); |
|
|
289 | } |
|
|
290 | |
|
|
291 | void |
|
|
292 | rxvt_img::add_alpha () |
|
|
293 | { |
|
|
294 | if (format->direct.alphaMask) |
|
|
295 | return; |
|
|
296 | |
|
|
297 | Display *dpy = s->display->dpy; |
|
|
298 | |
|
|
299 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
|
|
300 | img->alloc (); |
|
|
301 | |
|
|
302 | Picture src = picture (); |
|
|
303 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
304 | |
|
|
305 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
306 | |
|
|
307 | XRenderFreePicture (dpy, src); |
|
|
308 | XRenderFreePicture (dpy, dst); |
|
|
309 | |
|
|
310 | ::swap (img->ref, ref); |
|
|
311 | ::swap (img->pm , pm ); |
|
|
312 | |
|
|
313 | delete img; |
233 | } |
314 | } |
234 | |
315 | |
235 | static void |
316 | static void |
236 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
317 | get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) |
237 | { |
318 | { |
238 | double sigma = radius / 2.0; |
319 | rxvt_img::nv sigma = radius / 2.0; |
239 | double scale = sqrt (2.0 * M_PI) * sigma; |
320 | rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; |
240 | double sum = 0.0; |
321 | rxvt_img::nv sum = 0.0; |
241 | |
322 | |
242 | for (int i = 0; i < width; i++) |
323 | for (int i = 0; i < width; i++) |
243 | { |
324 | { |
244 | double x = i - width / 2; |
325 | rxvt_img::nv x = i - width / 2; |
245 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
326 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
246 | sum += kernel[i]; |
327 | sum += kernel[i]; |
247 | } |
328 | } |
248 | |
329 | |
249 | params[0] = XDoubleToFixed (width); |
330 | params[0] = XDoubleToFixed (width); |
… | |
… | |
259 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
340 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
260 | return clone (); |
341 | return clone (); |
261 | |
342 | |
262 | Display *dpy = s->display->dpy; |
343 | Display *dpy = s->display->dpy; |
263 | int size = max (rh, rv) * 2 + 1; |
344 | int size = max (rh, rv) * 2 + 1; |
264 | double *kernel = (double *)malloc (size * sizeof (double)); |
345 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
265 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
346 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
266 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
347 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
267 | img->alloc (); |
348 | img->alloc (); |
268 | |
349 | |
269 | XRenderPictureAttributes pa; |
350 | XRenderPictureAttributes pa; |
… | |
… | |
307 | w, h); |
388 | w, h); |
308 | } |
389 | } |
309 | |
390 | |
310 | free (kernel); |
391 | free (kernel); |
311 | free (params); |
392 | free (params); |
|
|
393 | |
312 | XRenderFreePicture (dpy, src); |
394 | XRenderFreePicture (dpy, src); |
313 | XRenderFreePicture (dpy, dst); |
395 | XRenderFreePicture (dpy, dst); |
314 | XRenderFreePicture (dpy, tmp); |
396 | XRenderFreePicture (dpy, tmp); |
315 | |
397 | |
316 | return img; |
398 | return img; |
317 | } |
399 | } |
318 | |
400 | |
319 | static Picture |
401 | static Picture |
320 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
402 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
321 | { |
403 | { |
322 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
404 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
323 | |
405 | |
324 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
406 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
325 | XRenderPictureAttributes pa; |
407 | XRenderPictureAttributes pa; |
326 | pa.repeat = True; |
408 | pa.repeat = RepeatNormal; |
|
|
409 | pa.component_alpha = component_alpha; |
327 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
410 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
328 | |
411 | |
329 | XFreePixmap (dpy, pixmap); |
412 | XFreePixmap (dpy, pixmap); |
330 | |
413 | |
331 | return mask; |
414 | return mask; |
332 | } |
415 | } |
… | |
… | |
378 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
461 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
379 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
462 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
380 | } |
463 | } |
381 | } |
464 | } |
382 | |
465 | |
383 | |
|
|
384 | XRenderFreePicture (dpy, dst); |
466 | XRenderFreePicture (dpy, dst); |
385 | } |
467 | } |
386 | |
468 | |
387 | void |
469 | void |
388 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
470 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
389 | { |
471 | { |
390 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
472 | if (r < 0 || g < 0 || b < 0 || a < 0) |
391 | { |
473 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
392 | rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); |
474 | |
393 | return; |
475 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
|
|
476 | img->alloc (); |
|
|
477 | img->fill (rgba (0, 0, 0, 0)); |
|
|
478 | |
|
|
479 | // premultiply (yeah, these are not exact, sue me or fix it) |
|
|
480 | r = (r * (a >> 8)) >> 8; |
|
|
481 | g = (g * (a >> 8)) >> 8; |
|
|
482 | b = (b * (a >> 8)) >> 8; |
|
|
483 | |
|
|
484 | Display *dpy = s->display->dpy; |
|
|
485 | |
|
|
486 | Picture src = picture (); |
|
|
487 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
|
|
488 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
|
|
489 | |
|
|
490 | //TODO: this operator does not yet implement some useful contrast |
|
|
491 | while (r | g | b | a) |
394 | } |
492 | { |
|
|
493 | unsigned short xr, xg, xb, xa; |
|
|
494 | XRenderColor mask_c; |
395 | |
495 | |
|
|
496 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
497 | { |
|
|
498 | XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
|
|
499 | XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
500 | } |
|
|
501 | } |
|
|
502 | |
|
|
503 | XRenderFreePicture (dpy, mul); |
|
|
504 | XRenderFreePicture (dpy, dst); |
|
|
505 | XRenderFreePicture (dpy, src); |
|
|
506 | |
|
|
507 | ::swap (img->ref, ref); |
|
|
508 | ::swap (img->pm , pm ); |
|
|
509 | |
|
|
510 | delete img; |
|
|
511 | } |
|
|
512 | |
|
|
513 | void |
|
|
514 | rxvt_img::draw (rxvt_img *img, int op, nv mask) |
|
|
515 | { |
396 | unshare (); |
516 | unshare (); |
397 | |
517 | |
398 | Display *dpy = s->display->dpy; |
518 | Display *dpy = s->display->dpy; |
399 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
519 | Picture src = img->picture (); |
400 | |
520 | Picture dst = picture (); |
401 | XRenderColor mask_c; |
521 | Picture mask_p = 0; |
402 | mask_c.red = r; |
522 | |
403 | mask_c.green = g; |
523 | if (mask != 1.) |
404 | mask_c.blue = b; |
524 | { |
405 | mask_c.alpha = a; |
525 | mask_p = create_xrender_mask (dpy, img->pm, False, False); |
|
|
526 | XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; |
406 | XRenderFillRectangle (dpy, PictOpMultiply, dst, &mask_c, 0, 0, w, h); |
527 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
528 | } |
407 | |
529 | |
|
|
530 | XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); |
|
|
531 | |
408 | XRenderFreePicture (dpy, dst); |
532 | XRenderFreePicture (dpy, src); |
|
|
533 | XRenderFreePicture (dpy, dst); |
|
|
534 | |
|
|
535 | if (mask_p) |
|
|
536 | XRenderFreePicture (dpy, mask_p); |
409 | } |
537 | } |
410 | |
538 | |
411 | rxvt_img * |
539 | rxvt_img * |
412 | rxvt_img::clone () |
540 | rxvt_img::clone () |
413 | { |
541 | { |
414 | return new rxvt_img (*this); |
542 | return new rxvt_img (*this); |
415 | } |
|
|
416 | |
|
|
417 | static XRenderPictFormat * |
|
|
418 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
419 | { |
|
|
420 | if (format->direct.alphaMask) |
|
|
421 | return format; // already has alpha |
|
|
422 | |
|
|
423 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
424 | if (format->type == PictTypeDirect) |
|
|
425 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
426 | if (f->direct.alphaMask |
|
|
427 | && f->type == PictTypeDirect |
|
|
428 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
429 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
430 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
431 | return f; |
|
|
432 | |
|
|
433 | // should be a very good fallback |
|
|
434 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
435 | } |
543 | } |
436 | |
544 | |
437 | rxvt_img * |
545 | rxvt_img * |
438 | rxvt_img::reify () |
546 | rxvt_img::reify () |
439 | { |
547 | { |
… | |
… | |
448 | && repeat == RepeatNone; // and we have no good pixels to fill with |
556 | && repeat == RepeatNone; // and we have no good pixels to fill with |
449 | |
557 | |
450 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
558 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
451 | img->alloc (); |
559 | img->alloc (); |
452 | |
560 | |
453 | Picture src = src_picture (); |
561 | Picture src = picture (); |
454 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
562 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
455 | |
563 | |
456 | if (alpha) |
564 | if (alpha) |
457 | { |
565 | { |
458 | XRenderColor rc = { 0, 0, 0, 0 }; |
566 | XRenderColor rc = { 0, 0, 0, 0 }; |
459 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
567 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
460 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
568 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
461 | } |
569 | } |
462 | else |
570 | else |
463 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
571 | XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); |
464 | |
572 | |
465 | XRenderFreePicture (dpy, src); |
573 | XRenderFreePicture (dpy, src); |
466 | XRenderFreePicture (dpy, dst); |
574 | XRenderFreePicture (dpy, dst); |
467 | |
575 | |
468 | return img; |
576 | return img; |
… | |
… | |
471 | rxvt_img * |
579 | rxvt_img * |
472 | rxvt_img::sub_rect (int x, int y, int width, int height) |
580 | rxvt_img::sub_rect (int x, int y, int width, int height) |
473 | { |
581 | { |
474 | rxvt_img *img = clone (); |
582 | rxvt_img *img = clone (); |
475 | |
583 | |
476 | img->x += x; |
584 | img->x -= x; |
477 | img->y += y; |
585 | img->y -= y; |
478 | |
586 | |
479 | if (w != width || h != height) |
587 | if (w != width || h != height) |
480 | { |
588 | { |
481 | img->w = width; |
589 | img->w = width; |
482 | img->h = height; |
590 | img->h = height; |
… | |
… | |
487 | } |
595 | } |
488 | |
596 | |
489 | return img; |
597 | return img; |
490 | } |
598 | } |
491 | |
599 | |
492 | rxvt_img * |
600 | static void |
493 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
601 | mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) |
494 | { |
602 | { |
|
|
603 | rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
|
|
604 | rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; |
|
|
605 | rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
|
|
606 | |
|
|
607 | rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); |
|
|
608 | |
|
|
609 | inv [0][0] = invdet * s0; |
|
|
610 | inv [0][1] = invdet * s1; |
|
|
611 | inv [0][2] = invdet * s2; |
|
|
612 | |
|
|
613 | inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]); |
|
|
614 | inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]); |
|
|
615 | inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]); |
|
|
616 | |
|
|
617 | inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); |
|
|
618 | inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); |
|
|
619 | inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); |
|
|
620 | } |
|
|
621 | |
|
|
622 | static rxvt_img::nv |
|
|
623 | mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) |
|
|
624 | { |
|
|
625 | rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
|
|
626 | rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; |
|
|
627 | |
|
|
628 | return v * (1. / w); |
|
|
629 | } |
|
|
630 | |
|
|
631 | rxvt_img * |
|
|
632 | rxvt_img::transform (nv matrix[3][3]) |
|
|
633 | { |
|
|
634 | // calculate new pixel bounding box coordinates |
|
|
635 | nv rmin[2], rmax[2]; |
|
|
636 | |
|
|
637 | for (int i = 0; i < 2; ++i) |
|
|
638 | { |
|
|
639 | nv v; |
|
|
640 | v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; |
|
|
641 | v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
642 | v = mat_apply (matrix, i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
643 | v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
644 | } |
|
|
645 | |
|
|
646 | // TODO: adjust matrix for subpixel accuracy |
|
|
647 | int dx = floor (rmin [0]); |
|
|
648 | int dy = floor (rmin [1]); |
|
|
649 | |
|
|
650 | int new_width = ceil (rmax [0] - dx); |
|
|
651 | int new_height = ceil (rmax [1] - dy); |
|
|
652 | |
|
|
653 | nv inv[3][3]; |
|
|
654 | mat_invert (matrix, inv); |
|
|
655 | |
495 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
656 | rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat); |
496 | img->alloc (); |
657 | img->alloc (); |
497 | |
658 | |
498 | Display *dpy = s->display->dpy; |
659 | Display *dpy = s->display->dpy; |
499 | Picture src = src_picture (); |
660 | Picture src = picture (); |
500 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
661 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
501 | |
662 | |
502 | XTransform xfrm; |
663 | XTransform xfrm; |
503 | |
664 | |
504 | for (int i = 0; i < 3; ++i) |
665 | for (int i = 0; i < 3; ++i) |
505 | for (int j = 0; j < 3; ++j) |
666 | for (int j = 0; j < 3; ++j) |
506 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
667 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
507 | |
|
|
508 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
|
|
509 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
510 | |
668 | |
511 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
669 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
512 | XRenderSetPictureTransform (dpy, src, &xfrm); |
670 | XRenderSetPictureTransform (dpy, src, &xfrm); |
513 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
671 | XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); |
514 | |
672 | |
515 | XRenderFreePicture (dpy, src); |
673 | XRenderFreePicture (dpy, src); |
516 | XRenderFreePicture (dpy, dst); |
674 | XRenderFreePicture (dpy, dst); |
517 | |
675 | |
518 | return img; |
676 | return img; |
… | |
… | |
522 | rxvt_img::scale (int new_width, int new_height) |
680 | rxvt_img::scale (int new_width, int new_height) |
523 | { |
681 | { |
524 | if (w == new_width && h == new_height) |
682 | if (w == new_width && h == new_height) |
525 | return clone (); |
683 | return clone (); |
526 | |
684 | |
527 | double matrix[9] = { |
685 | nv matrix[3][3] = { |
528 | w / (double)new_width, 0, 0, |
686 | { new_width / (nv)w, 0, 0 }, |
529 | 0, h / (double)new_height, 0, |
687 | { 0, new_height / (nv)h, 0 }, |
530 | 0, 0, 1 |
688 | { 0, 0, 1 } |
531 | }; |
689 | }; |
532 | |
690 | |
533 | int old_repeat_mode = repeat; |
691 | int old_repeat_mode = repeat; |
534 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
692 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
535 | |
693 | |
536 | rxvt_img *img = transform (new_width, new_height, matrix); |
694 | rxvt_img *img = transform (matrix); |
537 | |
695 | |
538 | repeat = old_repeat_mode; |
696 | repeat = old_repeat_mode; |
539 | img->repeat = repeat; |
697 | img->repeat = repeat; |
540 | |
698 | |
541 | return img; |
699 | return img; |
542 | } |
700 | } |
543 | |
701 | |
544 | rxvt_img * |
702 | rxvt_img * |
545 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
703 | rxvt_img::rotate (int cx, int cy, nv phi) |
546 | { |
704 | { |
547 | double s = sin (phi); |
705 | nv s = sin (phi); |
548 | double c = cos (phi); |
706 | nv c = cos (phi); |
549 | |
707 | |
550 | double matrix[9] = { |
708 | nv matrix[3][3] = { |
551 | c, -s, -c * x + s * y + x, |
709 | { c, -s, cx - c * cx + s * cy + 200 }, |
552 | s, c, -s * x - c * y + y, |
710 | { s, c, cy - s * cx - c * cy }, |
553 | 0, 0, 1 |
711 | { 0, 0, 1 } |
|
|
712 | //{ c, -s, 0 }, |
|
|
713 | //{ s, c, 0 }, |
|
|
714 | //{ 0, 0, 1 } |
554 | }; |
715 | }; |
555 | |
716 | |
556 | return transform (new_width, new_height, matrix); |
717 | //move (-cx, -cy); |
557 | } |
718 | rxvt_img *img = transform (matrix); |
|
|
719 | //move ( cx, cy); |
|
|
720 | //img->move (cx, cy); |
558 | |
721 | |
|
|
722 | return img; |
|
|
723 | } |
|
|
724 | |
559 | rxvt_img * |
725 | rxvt_img * |
560 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
726 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
561 | { |
727 | { |
562 | if (new_format == format) |
728 | if (new_format == format) |
563 | return clone (); |
729 | return clone (); |
564 | |
730 | |
565 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
731 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
566 | img->alloc (); |
732 | img->alloc (); |
567 | |
733 | |
568 | Display *dpy = s->display->dpy; |
734 | Display *dpy = s->display->dpy; |
569 | Picture src = src_picture (); |
735 | Picture src = picture (); |
570 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
736 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
571 | int op = PictOpSrc; |
737 | int op = PictOpSrc; |
572 | |
738 | |
573 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
739 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
574 | { |
740 | { |
575 | // does it have to be that complicated |
741 | // does it have to be that complicated |
576 | rgba c; |
|
|
577 | bg.get (c); |
|
|
578 | |
|
|
579 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
742 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
580 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
743 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
581 | |
744 | |
582 | op = PictOpOver; |
745 | op = PictOpOver; |
583 | } |
746 | } |
584 | |
747 | |
… | |
… | |
589 | |
752 | |
590 | return img; |
753 | return img; |
591 | } |
754 | } |
592 | |
755 | |
593 | rxvt_img * |
756 | rxvt_img * |
594 | rxvt_img::blend (rxvt_img *img, double factor) |
757 | rxvt_img::blend (rxvt_img *img, nv factor) |
595 | { |
758 | { |
596 | rxvt_img *img2 = clone (); |
759 | rxvt_img *img2 = clone (); |
597 | Display *dpy = s->display->dpy; |
760 | Display *dpy = s->display->dpy; |
598 | Picture src = img->src_picture (); |
761 | Picture src = img->picture (); |
599 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
762 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
600 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
763 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
601 | |
764 | |
602 | XRenderColor mask_c; |
765 | XRenderColor mask_c; |
603 | |
766 | |
604 | mask_c.alpha = float_to_component (factor); |
767 | mask_c.alpha = float_to_component (factor); |
605 | mask_c.red = |
768 | mask_c.red = |