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 | static XRenderPictFormat * |
|
|
8 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
9 | { |
|
|
10 | if (format->direct.alphaMask) |
|
|
11 | return format; // already has alpha |
|
|
12 | |
|
|
13 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
14 | if (format->type == PictTypeDirect) |
|
|
15 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
16 | if (f->direct.alphaMask |
|
|
17 | && f->type == PictTypeDirect |
|
|
18 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
19 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
20 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
21 | return f; |
|
|
22 | |
|
|
23 | // should be a very good fallback |
|
|
24 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
25 | } |
6 | |
26 | |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
27 | 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), |
28 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
9 | pm(0), ref(0) |
29 | pm(0), ref(0) |
10 | { |
30 | { |
… | |
… | |
224 | pm = pm2; |
244 | pm = pm2; |
225 | ref = new pixref (ref->w, ref->h); |
245 | ref = new pixref (ref->w, ref->h); |
226 | } |
246 | } |
227 | |
247 | |
228 | void |
248 | void |
229 | rxvt_img::fill (const rxvt_color &c) |
249 | rxvt_img::fill (const rgba &c) |
230 | { |
250 | { |
231 | rgba cc; |
|
|
232 | c.get (cc); |
|
|
233 | XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; |
251 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
234 | |
252 | |
235 | Display *dpy = s->display->dpy; |
253 | Display *dpy = s->display->dpy; |
236 | Picture src = src_picture (); |
254 | Picture src = src_picture (); |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
255 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
238 | XRenderFreePicture (dpy, src); |
256 | XRenderFreePicture (dpy, src); |
|
|
257 | } |
|
|
258 | |
|
|
259 | void |
|
|
260 | rxvt_img::add_alpha () |
|
|
261 | { |
|
|
262 | if (format->direct.alphaMask) |
|
|
263 | return; |
|
|
264 | |
|
|
265 | Display *dpy = s->display->dpy; |
|
|
266 | |
|
|
267 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
|
|
268 | img->alloc (); |
|
|
269 | |
|
|
270 | Picture src = src_picture (); |
|
|
271 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
272 | |
|
|
273 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
274 | |
|
|
275 | XRenderFreePicture (dpy, src); |
|
|
276 | XRenderFreePicture (dpy, dst); |
|
|
277 | |
|
|
278 | ::swap (img->ref, ref); |
|
|
279 | ::swap (img->pm , pm ); |
|
|
280 | |
|
|
281 | delete img; |
239 | } |
282 | } |
240 | |
283 | |
241 | static void |
284 | static void |
242 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
285 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
243 | { |
286 | { |
… | |
… | |
397 | if (r < 0 || g < 0 || b < 0 || a < 0) |
440 | if (r < 0 || g < 0 || b < 0 || a < 0) |
398 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
441 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
399 | |
442 | |
400 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
443 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
401 | img->alloc (); |
444 | img->alloc (); |
402 | |
445 | img->fill (rgba (0, 0, 0, 0)); |
403 | { |
|
|
404 | rxvt_color empty; |
|
|
405 | empty.set (s, rgba (0, 0, 0, 0)); |
|
|
406 | img->fill (empty); |
|
|
407 | } |
|
|
408 | |
446 | |
409 | // premultiply (yeah, these are not exact, sue me or fix it) |
447 | // premultiply (yeah, these are not exact, sue me or fix it) |
410 | r = (r * (a >> 8)) >> 8; |
448 | r = (r * (a >> 8)) >> 8; |
411 | g = (g * (a >> 8)) >> 8; |
449 | g = (g * (a >> 8)) >> 8; |
412 | b = (b * (a >> 8)) >> 8; |
450 | b = (b * (a >> 8)) >> 8; |
… | |
… | |
442 | |
480 | |
443 | rxvt_img * |
481 | rxvt_img * |
444 | rxvt_img::clone () |
482 | rxvt_img::clone () |
445 | { |
483 | { |
446 | return new rxvt_img (*this); |
484 | return new rxvt_img (*this); |
447 | } |
|
|
448 | |
|
|
449 | static XRenderPictFormat * |
|
|
450 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
451 | { |
|
|
452 | if (format->direct.alphaMask) |
|
|
453 | return format; // already has alpha |
|
|
454 | |
|
|
455 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
456 | if (format->type == PictTypeDirect) |
|
|
457 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
458 | if (f->direct.alphaMask |
|
|
459 | && f->type == PictTypeDirect |
|
|
460 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
461 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
462 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
463 | return f; |
|
|
464 | |
|
|
465 | // should be a very good fallback |
|
|
466 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
467 | } |
485 | } |
468 | |
486 | |
469 | rxvt_img * |
487 | rxvt_img * |
470 | rxvt_img::reify () |
488 | rxvt_img::reify () |
471 | { |
489 | { |
… | |
… | |
648 | |
666 | |
649 | return img; |
667 | return img; |
650 | } |
668 | } |
651 | |
669 | |
652 | rxvt_img * |
670 | rxvt_img * |
653 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
671 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
654 | { |
672 | { |
655 | if (new_format == format) |
673 | if (new_format == format) |
656 | return clone (); |
674 | return clone (); |
657 | |
675 | |
658 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
676 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
… | |
… | |
664 | int op = PictOpSrc; |
682 | int op = PictOpSrc; |
665 | |
683 | |
666 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
684 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
667 | { |
685 | { |
668 | // does it have to be that complicated |
686 | // does it have to be that complicated |
669 | rgba c; |
|
|
670 | bg.get (c); |
|
|
671 | |
|
|
672 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
687 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
673 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
688 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
674 | |
689 | |
675 | op = PictOpOver; |
690 | op = PictOpOver; |
676 | } |
691 | } |
677 | |
692 | |