ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.81 by root, Tue Jun 12 19:00:57 2012 UTC vs.
Revision 1.85 by root, Thu Jun 14 18:59:39 2012 UTC

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
8struct 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
39static XRenderPictFormat *
40find_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
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) 59rxvt_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{
195 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);
196 ref = new pixref (w, h); 248 ref = new pixref (w, h);
197} 249}
198 250
199Picture 251Picture
200rxvt_img::src_picture () 252rxvt_img::picture ()
201{ 253{
202 Display *dpy = s->display->dpy; 254 Display *dpy = s->display->dpy;
203 255
204 XRenderPictureAttributes pa; 256 XRenderPictureAttributes pa;
205 pa.repeat = repeat; 257 pa.repeat = repeat;
224 pm = pm2; 276 pm = pm2;
225 ref = new pixref (ref->w, ref->h); 277 ref = new pixref (ref->w, ref->h);
226} 278}
227 279
228void 280void
229rxvt_img::fill (const rxvt_color &c) 281rxvt_img::fill (const rgba &c)
230{ 282{
231 rgba cc;
232 c.get (cc);
233 XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; 283 XRenderColor rc = { c.r, c.g, c.b, c.a };
234 284
235 Display *dpy = s->display->dpy; 285 Display *dpy = s->display->dpy;
236 Picture src = src_picture (); 286 Picture src = picture ();
237 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); 287 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
238 XRenderFreePicture (dpy, src); 288 XRenderFreePicture (dpy, src);
239} 289}
240 290
291void
292rxvt_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;
314}
315
241static void 316static void
242get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 317get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
243{ 318{
244 double sigma = radius / 2.0; 319 rxvt_img::nv sigma = radius / 2.0;
245 double scale = sqrt (2.0 * M_PI) * sigma; 320 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
246 double sum = 0.0; 321 rxvt_img::nv sum = 0.0;
247 322
248 for (int i = 0; i < width; i++) 323 for (int i = 0; i < width; i++)
249 { 324 {
250 double x = i - width / 2; 325 rxvt_img::nv x = i - width / 2;
251 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 326 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
252 sum += kernel[i]; 327 sum += kernel[i];
253 } 328 }
254 329
255 params[0] = XDoubleToFixed (width); 330 params[0] = XDoubleToFixed (width);
265 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 340 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
266 return clone (); 341 return clone ();
267 342
268 Display *dpy = s->display->dpy; 343 Display *dpy = s->display->dpy;
269 int size = max (rh, rv) * 2 + 1; 344 int size = max (rh, rv) * 2 + 1;
270 double *kernel = (double *)malloc (size * sizeof (double)); 345 nv *kernel = (nv *)malloc (size * sizeof (nv));
271 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 346 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
272 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);
273 img->alloc (); 348 img->alloc ();
274 349
275 XRenderPictureAttributes pa; 350 XRenderPictureAttributes pa;
397 if (r < 0 || g < 0 || b < 0 || a < 0) 472 if (r < 0 || g < 0 || b < 0 || a < 0)
398 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 473 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
399 474
400 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 475 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
401 img->alloc (); 476 img->alloc ();
402 477 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 478
409 // premultiply (yeah, these are not exact, sue me or fix it) 479 // premultiply (yeah, these are not exact, sue me or fix it)
410 r = (r * (a >> 8)) >> 8; 480 r = (r * (a >> 8)) >> 8;
411 g = (g * (a >> 8)) >> 8; 481 g = (g * (a >> 8)) >> 8;
412 b = (b * (a >> 8)) >> 8; 482 b = (b * (a >> 8)) >> 8;
413 483
414 Display *dpy = s->display->dpy; 484 Display *dpy = s->display->dpy;
415 485
416 Picture src = src_picture (); 486 Picture src = picture ();
417 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); 487 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
418 Picture mul = create_xrender_mask (dpy, pm, True, True); 488 Picture mul = create_xrender_mask (dpy, pm, True, True);
419 489
420 //TODO: this operator does not yet implement some useful contrast 490 //TODO: this operator does not yet implement some useful contrast
421 while (r | g | b | a) 491 while (r | g | b | a)
438 ::swap (img->pm , pm ); 508 ::swap (img->pm , pm );
439 509
440 delete img; 510 delete img;
441} 511}
442 512
513void
514rxvt_img::draw (rxvt_img *img, int op, nv mask)
515{
516 unshare ();
517
518 Display *dpy = s->display->dpy;
519 Picture src = img->picture ();
520 Picture dst = picture ();
521 Picture mask_p = 0;
522
523 if (mask != 1.)
524 {
525 mask_p = create_xrender_mask (dpy, img->pm, False, False);
526 XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) };
527 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
528 }
529
530 XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h);
531
532 XRenderFreePicture (dpy, src);
533 XRenderFreePicture (dpy, dst);
534
535 if (mask)
536 XRenderFreePicture (dpy, mask_p);
537}
538
443rxvt_img * 539rxvt_img *
444rxvt_img::clone () 540rxvt_img::clone ()
445{ 541{
446 return new rxvt_img (*this); 542 return new rxvt_img (*this);
447}
448
449static XRenderPictFormat *
450find_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} 543}
468 544
469rxvt_img * 545rxvt_img *
470rxvt_img::reify () 546rxvt_img::reify ()
471{ 547{
480 && repeat == RepeatNone; // and we have no good pixels to fill with 556 && repeat == RepeatNone; // and we have no good pixels to fill with
481 557
482 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);
483 img->alloc (); 559 img->alloc ();
484 560
485 Picture src = src_picture (); 561 Picture src = picture ();
486 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 562 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
487 563
488 if (alpha) 564 if (alpha)
489 { 565 {
490 XRenderColor rc = { 0, 0, 0, 0 }; 566 XRenderColor rc = { 0, 0, 0, 0 };
520 596
521 return img; 597 return img;
522} 598}
523 599
524static void 600static void
525mat_invert (double mat[3][3], double (&inv)[3][3]) 601mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
526{ 602{
527 double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; 603 rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
528 double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; 604 rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
529 double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; 605 rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
530 606
531 double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); 607 rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
532 608
533 inv [0][0] = invdet * s0; 609 inv [0][0] = invdet * s0;
534 inv [0][1] = invdet * s1; 610 inv [0][1] = invdet * s1;
535 inv [0][2] = invdet * s2; 611 inv [0][2] = invdet * s2;
536 612
541 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); 617 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
542 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); 618 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
543 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); 619 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
544} 620}
545 621
546static double 622static rxvt_img::nv
547mat_apply (double mat[3][3], int i, double x, double y) 623mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y)
548{ 624{
549 double v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; 625 rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
550 double w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; 626 rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
551 627
552 return v * (1. / w); 628 return v * (1. / w);
553} 629}
554 630
555rxvt_img * 631rxvt_img *
556rxvt_img::transform (double matrix[3][3]) 632rxvt_img::transform (nv matrix[3][3])
557{ 633{
558 // find new offset 634 // find new offset
559 int ox = mat_apply (matrix, 0, x, y); 635 int ox = mat_apply (matrix, 0, x, y);
560 int oy = mat_apply (matrix, 1, x, y); 636 int oy = mat_apply (matrix, 1, x, y);
561 637
562 // calculate new pixel bounding box coordinates 638 // calculate new pixel bounding box coordinates
563 double d [2], rmin[2], rmax[2]; 639 nv d [2], rmin[2], rmax[2];
564 640
565 for (int i = 0; i < 2; ++i) 641 for (int i = 0; i < 2; ++i)
566 { 642 {
567 double v; 643 nv v;
568 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v; 644 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
569 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); 645 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
570 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v); 646 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v);
571 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v); 647 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v);
572 } 648 }
575 int dy = floor (rmin [1]); 651 int dy = floor (rmin [1]);
576 652
577 int new_width = ceil (rmax [0] - dx); 653 int new_width = ceil (rmax [0] - dx);
578 int new_height = ceil (rmax [1] - dy); 654 int new_height = ceil (rmax [1] - dy);
579 655
580 double inv[3][3]; 656 nv inv[3][3];
581 mat_invert (matrix, inv); 657 mat_invert (matrix, inv);
582 658
583 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat); 659 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
584 img->alloc (); 660 img->alloc ();
585 661
586 Display *dpy = s->display->dpy; 662 Display *dpy = s->display->dpy;
587 Picture src = src_picture (); 663 Picture src = picture ();
588 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 664 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
589 665
590 XTransform xfrm; 666 XTransform xfrm;
591 667
592 for (int i = 0; i < 3; ++i) 668 for (int i = 0; i < 3; ++i)
607rxvt_img::scale (int new_width, int new_height) 683rxvt_img::scale (int new_width, int new_height)
608{ 684{
609 if (w == new_width && h == new_height) 685 if (w == new_width && h == new_height)
610 return clone (); 686 return clone ();
611 687
612 double matrix[3][3] = { 688 nv matrix[3][3] = {
613 { new_width / (double)w, 0, 0 }, 689 { new_width / (nv)w, 0, 0 },
614 { 0, new_height / (double)h, 0 }, 690 { 0, new_height / (nv)h, 0 },
615 { 0, 0, 1 } 691 { 0, 0, 1 }
616 }; 692 };
617 693
618 int old_repeat_mode = repeat; 694 int old_repeat_mode = repeat;
619 repeat = RepeatPad; // not right, but xrender can't properly scale it seems 695 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
620 696
625 701
626 return img; 702 return img;
627} 703}
628 704
629rxvt_img * 705rxvt_img *
630rxvt_img::rotate (int cx, int cy, double phi) 706rxvt_img::rotate (int cx, int cy, nv phi)
631{ 707{
632 double s = sin (phi); 708 nv s = sin (phi);
633 double c = cos (phi); 709 nv c = cos (phi);
634 710
635 double matrix[3][3] = { 711 nv matrix[3][3] = {
636 { c, -s, cx - c * cx + s * cy }, 712 { c, -s, cx - c * cx + s * cy },
637 { s, c, cy - s * cx - c * cy }, 713 { s, c, cy - s * cx - c * cy },
638 { 0, 0, 1 } 714 { 0, 0, 1 }
639 //{ c, -s, 0 }, 715 //{ c, -s, 0 },
640 //{ s, c, 0 }, 716 //{ s, c, 0 },
648 724
649 return img; 725 return img;
650} 726}
651 727
652rxvt_img * 728rxvt_img *
653rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 729rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
654{ 730{
655 if (new_format == format) 731 if (new_format == format)
656 return clone (); 732 return clone ();
657 733
658 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 734 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
659 img->alloc (); 735 img->alloc ();
660 736
661 Display *dpy = s->display->dpy; 737 Display *dpy = s->display->dpy;
662 Picture src = src_picture (); 738 Picture src = picture ();
663 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 739 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
664 int op = PictOpSrc; 740 int op = PictOpSrc;
665 741
666 if (format->direct.alphaMask && !new_format->direct.alphaMask) 742 if (format->direct.alphaMask && !new_format->direct.alphaMask)
667 { 743 {
668 // does it have to be that complicated 744 // 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 }; 745 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
673 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 746 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
674 747
675 op = PictOpOver; 748 op = PictOpOver;
676 } 749 }
677 750
682 755
683 return img; 756 return img;
684} 757}
685 758
686rxvt_img * 759rxvt_img *
687rxvt_img::blend (rxvt_img *img, double factor) 760rxvt_img::blend (rxvt_img *img, nv factor)
688{ 761{
689 rxvt_img *img2 = clone (); 762 rxvt_img *img2 = clone ();
690 Display *dpy = s->display->dpy; 763 Display *dpy = s->display->dpy;
691 Picture src = img->src_picture (); 764 Picture src = img->picture ();
692 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 765 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
693 Picture mask = create_xrender_mask (dpy, img->pm, False, False); 766 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
694 767
695 XRenderColor mask_c; 768 XRenderColor mask_c;
696 769

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines