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.83 by root, Thu Jun 14 18:06:15 2012 UTC vs.
Revision 1.92 by root, Fri Jun 15 18:05:15 2012 UTC

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
7
8typedef rxvt_img::nv nv;
9
10struct mat3x3
11{
12 nv v[3][3];
13
14 mat3x3 ()
15 {
16 }
17
18 mat3x3 (nv matrix[3][3])
19 {
20 memcpy (v, matrix, sizeof (v));
21 }
22
23 mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33)
24 {
25 v[0][0] = v11; v[0][1] = v12; v[0][2] = v13;
26 v[1][0] = v21; v[1][1] = v22; v[1][2] = v23;
27 v[2][0] = v31; v[2][1] = v32; v[2][2] = v33;
28 }
29
30 mat3x3 invert ();
31
32 nv *operator [](int i) { return &v[i][0]; }
33 const nv *operator [](int i) const { return &v[i][0]; }
34
35 // quite inefficient, hopefully gcc pulls the w calc out of any loops
36 nv apply1 (int i, nv x, nv y)
37 {
38 mat3x3 &m = *this;
39
40 nv v = m[i][0] * x + m[i][1] * y + m[i][2];
41 nv w = m[2][0] * x + m[2][1] * y + m[2][2];
42
43 return v * (1. / w);
44 }
45
46 static mat3x3 translate (nv x, nv y);
47};
48
49mat3x3
50mat3x3::invert ()
51{
52 mat3x3 &m = *this;
53 mat3x3 inv;
54
55 nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2];
56 nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1];
57 nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2];
58
59 nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2);
60
61 inv[0][0] = invdet * s0;
62 inv[0][1] = invdet * s1;
63 inv[0][2] = invdet * s2;
64
65 inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]);
66 inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]);
67 inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]);
68
69 inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]);
70 inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]);
71 inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]);
72
73 return inv;
74}
75
76static mat3x3
77operator *(const mat3x3 &a, const mat3x3 &b)
78{
79 mat3x3 r;
80
81 for (int i = 0; i < 3; ++i)
82 for (int j = 0; j < 3; ++j)
83 r[i][j] = a[i][0] * b[0][j]
84 + a[i][1] * b[1][j]
85 + a[i][2] * b[2][j];
86
87 return r;
88}
89
90mat3x3
91mat3x3::translate (nv x, nv y)
92{
93 return mat3x3 (
94 1, 0, x,
95 0, 1, y,
96 0, 0, 1
97 );
98}
99
100#if 0
101struct pict
102{
103 Display *dpy;
104 Picture pic;
105
106 operator Picture () const
107 {
108 return pic;
109 }
110
111 pict ()
112 : pic (0)
113 {
114 }
115
116 pict (rxvt_img *img, XRenderPictFormat *format = 0)
117 : dpy (img->s->display->dpy)
118 {
119 XRenderPictureAttributes pa;
120 pa.repeat = img->repeat;
121 pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa);
122 }
123
124 ~pict ()
125 {
126 if (pic)
127 XRenderFreePicture (dpy, pic);
128 }
129};
130#endif
6 131
7static XRenderPictFormat * 132static XRenderPictFormat *
8find_alpha_format_for (Display *dpy, XRenderPictFormat *format) 133find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
9{ 134{
10 if (format->direct.alphaMask) 135 if (format->direct.alphaMask)
215 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 340 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
216 ref = new pixref (w, h); 341 ref = new pixref (w, h);
217} 342}
218 343
219Picture 344Picture
220rxvt_img::src_picture () 345rxvt_img::picture ()
221{ 346{
222 Display *dpy = s->display->dpy; 347 Display *dpy = s->display->dpy;
223 348
224 XRenderPictureAttributes pa; 349 XRenderPictureAttributes pa;
225 pa.repeat = repeat; 350 pa.repeat = repeat;
249rxvt_img::fill (const rgba &c) 374rxvt_img::fill (const rgba &c)
250{ 375{
251 XRenderColor rc = { c.r, c.g, c.b, c.a }; 376 XRenderColor rc = { c.r, c.g, c.b, c.a };
252 377
253 Display *dpy = s->display->dpy; 378 Display *dpy = s->display->dpy;
254 Picture src = src_picture (); 379 Picture src = picture ();
255 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); 380 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
256 XRenderFreePicture (dpy, src); 381 XRenderFreePicture (dpy, src);
257} 382}
258 383
259void 384void
265 Display *dpy = s->display->dpy; 390 Display *dpy = s->display->dpy;
266 391
267 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); 392 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
268 img->alloc (); 393 img->alloc ();
269 394
270 Picture src = src_picture (); 395 Picture src = picture ();
271 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 396 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
272 397
273 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 398 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
274 399
275 XRenderFreePicture (dpy, src); 400 XRenderFreePicture (dpy, src);
280 405
281 delete img; 406 delete img;
282} 407}
283 408
284static void 409static void
285get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 410get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params)
286{ 411{
287 double sigma = radius / 2.0; 412 nv sigma = radius / 2.0;
288 double scale = sqrt (2.0 * M_PI) * sigma; 413 nv scale = sqrt (2.0 * M_PI) * sigma;
289 double sum = 0.0; 414 nv sum = 0.0;
290 415
291 for (int i = 0; i < width; i++) 416 for (int i = 0; i < width; i++)
292 { 417 {
293 double x = i - width / 2; 418 nv x = i - width / 2;
294 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 419 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
295 sum += kernel[i]; 420 sum += kernel[i];
296 } 421 }
297 422
298 params[0] = XDoubleToFixed (width); 423 params[0] = XDoubleToFixed (width);
308 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 433 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
309 return clone (); 434 return clone ();
310 435
311 Display *dpy = s->display->dpy; 436 Display *dpy = s->display->dpy;
312 int size = max (rh, rv) * 2 + 1; 437 int size = max (rh, rv) * 2 + 1;
313 double *kernel = (double *)malloc (size * sizeof (double)); 438 nv *kernel = (nv *)malloc (size * sizeof (nv));
314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 439 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
315 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 440 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
316 img->alloc (); 441 img->alloc ();
317 442
318 XRenderPictureAttributes pa; 443 XRenderPictureAttributes pa;
449 g = (g * (a >> 8)) >> 8; 574 g = (g * (a >> 8)) >> 8;
450 b = (b * (a >> 8)) >> 8; 575 b = (b * (a >> 8)) >> 8;
451 576
452 Display *dpy = s->display->dpy; 577 Display *dpy = s->display->dpy;
453 578
454 Picture src = src_picture (); 579 Picture src = picture ();
455 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); 580 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
456 Picture mul = create_xrender_mask (dpy, pm, True, True); 581 Picture mul = create_xrender_mask (dpy, pm, True, True);
457 582
458 //TODO: this operator does not yet implement some useful contrast 583 //TODO: this operator does not yet implement some useful contrast
459 while (r | g | b | a) 584 while (r | g | b | a)
476 ::swap (img->pm , pm ); 601 ::swap (img->pm , pm );
477 602
478 delete img; 603 delete img;
479} 604}
480 605
606void
607rxvt_img::draw (rxvt_img *img, int op, nv mask)
608{
609 unshare ();
610
611 Display *dpy = s->display->dpy;
612 Picture src = img->picture ();
613 Picture dst = picture ();
614 Picture mask_p = 0;
615
616 if (mask != 1.)
617 {
618 mask_p = create_xrender_mask (dpy, img->pm, False, False);
619 XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) };
620 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
621 }
622
623 XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h);
624
625 XRenderFreePicture (dpy, src);
626 XRenderFreePicture (dpy, dst);
627
628 if (mask_p)
629 XRenderFreePicture (dpy, mask_p);
630}
631
481rxvt_img * 632rxvt_img *
482rxvt_img::clone () 633rxvt_img::clone ()
483{ 634{
484 return new rxvt_img (*this); 635 return new rxvt_img (*this);
485} 636}
498 && repeat == RepeatNone; // and we have no good pixels to fill with 649 && repeat == RepeatNone; // and we have no good pixels to fill with
499 650
500 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); 651 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
501 img->alloc (); 652 img->alloc ();
502 653
503 Picture src = src_picture (); 654 Picture src = picture ();
504 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 655 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
505 656
506 if (alpha) 657 if (alpha)
507 { 658 {
508 XRenderColor rc = { 0, 0, 0, 0 }; 659 XRenderColor rc = { 0, 0, 0, 0 };
509 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 660 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
510 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); 661 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
511 } 662 }
512 else 663 else
513 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); 664 XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h);
514 665
515 XRenderFreePicture (dpy, src); 666 XRenderFreePicture (dpy, src);
516 XRenderFreePicture (dpy, dst); 667 XRenderFreePicture (dpy, dst);
517 668
518 return img; 669 return img;
521rxvt_img * 672rxvt_img *
522rxvt_img::sub_rect (int x, int y, int width, int height) 673rxvt_img::sub_rect (int x, int y, int width, int height)
523{ 674{
524 rxvt_img *img = clone (); 675 rxvt_img *img = clone ();
525 676
526 img->x += x; 677 img->x -= x;
527 img->y += y; 678 img->y -= y;
528 679
529 if (w != width || h != height) 680 if (w != width || h != height)
530 { 681 {
531 img->w = width; 682 img->w = width;
532 img->h = height; 683 img->h = height;
537 } 688 }
538 689
539 return img; 690 return img;
540} 691}
541 692
542static void
543mat_invert (double mat[3][3], double (&inv)[3][3])
544{
545 double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
546 double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
547 double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
548
549 double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
550
551 inv [0][0] = invdet * s0;
552 inv [0][1] = invdet * s1;
553 inv [0][2] = invdet * s2;
554
555 inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
556 inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
557 inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
558
559 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
560 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
561 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
562}
563
564static double
565mat_apply (double mat[3][3], int i, double x, double y)
566{
567 double v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
568 double w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
569
570 return v * (1. / w);
571}
572
573rxvt_img * 693rxvt_img *
574rxvt_img::transform (double matrix[3][3]) 694rxvt_img::transform (nv matrix[3][3])
575{ 695{
576 // find new offset
577 int ox = mat_apply (matrix, 0, x, y);
578 int oy = mat_apply (matrix, 1, x, y);
579
580 // calculate new pixel bounding box coordinates 696 // calculate new pixel bounding box coordinates
581 double d [2], rmin[2], rmax[2]; 697 nv r[2], rmin[2], rmax[2];
698
699 mat3x3 m (matrix);
582 700
583 for (int i = 0; i < 2; ++i) 701 for (int i = 0; i < 2; ++i)
584 { 702 {
585 double v; 703 nv v;
704
586 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v; 705 v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v;
587 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); 706 v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v);
588 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v); 707 v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
589 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v); 708 v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
590 } 709 }
591 710
711 float sx = rmin [0] - x;
712 float sy = rmin [1] - y;
713
714 // TODO: adjust matrix for subpixel accuracy
592 int dx = floor (rmin [0]); 715 int nx = floor (rmin [0]);
593 int dy = floor (rmin [1]); 716 int ny = floor (rmin [1]);
594 717
595 int new_width = ceil (rmax [0] - dx); 718 int new_width = ceil (rmax [0] - rmin [0]);
596 int new_height = ceil (rmax [1] - dy); 719 int new_height = ceil (rmax [1] - rmin [1]);
597 720
598 double inv[3][3]; 721 m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y);
599 mat_invert (matrix, inv);
600 722
723 mat3x3 inv = m.invert ();
724
601 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat); 725 rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat);
602 img->alloc (); 726 img->alloc ();
603 727
604 Display *dpy = s->display->dpy; 728 Display *dpy = s->display->dpy;
605 Picture src = src_picture (); 729 Picture src = picture ();
606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 730 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
607 731
608 XTransform xfrm; 732 XTransform xfrm;
609 733
610 for (int i = 0; i < 3; ++i) 734 for (int i = 0; i < 3; ++i)
611 for (int j = 0; j < 3; ++j) 735 for (int j = 0; j < 3; ++j)
612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); 736 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
613 737
614 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 738 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
615 XRenderSetPictureTransform (dpy, src, &xfrm); 739 XRenderSetPictureTransform (dpy, src, &xfrm);
616 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); 740 XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
741#if 1
742 {
743 XRenderColor rc = { 65535,0,0,65535 };
744 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height);
745 }{
746 XRenderColor rc = { 0,0,0,65535 };
747 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2);
748 }
749 XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
750#endif
617 751
618 XRenderFreePicture (dpy, src); 752 XRenderFreePicture (dpy, src);
619 XRenderFreePicture (dpy, dst); 753 XRenderFreePicture (dpy, dst);
620 754
621 return img; 755 return img;
625rxvt_img::scale (int new_width, int new_height) 759rxvt_img::scale (int new_width, int new_height)
626{ 760{
627 if (w == new_width && h == new_height) 761 if (w == new_width && h == new_height)
628 return clone (); 762 return clone ();
629 763
630 double matrix[3][3] = { 764 nv matrix[3][3] = {
631 { new_width / (double)w, 0, 0 }, 765 { new_width / (nv)w, 0, 0 },
632 { 0, new_height / (double)h, 0 }, 766 { 0, new_height / (nv)h, 0 },
633 { 0, 0, 1 } 767 { 0, 0, 1 }
634 }; 768 };
635 769
636 int old_repeat_mode = repeat; 770 int old_repeat_mode = repeat;
637 repeat = RepeatPad; // not right, but xrender can't properly scale it seems 771 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
638 772
643 777
644 return img; 778 return img;
645} 779}
646 780
647rxvt_img * 781rxvt_img *
648rxvt_img::rotate (int cx, int cy, double phi) 782rxvt_img::rotate (int cx, int cy, nv phi)
649{ 783{
650 double s = sin (phi); 784 nv s = sin (phi);
651 double c = cos (phi); 785 nv c = cos (phi);
652 786
653 double matrix[3][3] = { 787 nv matrix[3][3] = {
788#if 0
654 { c, -s, cx - c * cx + s * cy }, 789 { c, -s, cx - c * cx + s * cy },
655 { s, c, cy - s * cx - c * cy }, 790 { s, c, cy - s * cx - c * cy },
656 { 0, 0, 1 } 791 { 0, 0, 1 }
792#else
657 //{ c, -s, 0 }, 793 { c, -s, 0 },
658 //{ s, c, 0 }, 794 { s, c, 0 },
659 //{ 0, 0, 1 } 795 { 0, 0, 1 }
796#endif
660 }; 797 };
661 798
662 //move (-cx, -cy); 799 move (-cx, -cy);
663 rxvt_img *img = transform (matrix); 800 rxvt_img *img = transform (matrix);
664 //move ( cx, cy); 801 move ( cx, cy);
665 //img->move (cx, cy); 802 img->move (cx, cy);
666 803
667 return img; 804 return img;
668} 805}
669 806
670rxvt_img * 807rxvt_img *
675 812
676 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 813 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
677 img->alloc (); 814 img->alloc ();
678 815
679 Display *dpy = s->display->dpy; 816 Display *dpy = s->display->dpy;
680 Picture src = src_picture (); 817 Picture src = picture ();
681 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 818 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
682 int op = PictOpSrc; 819 int op = PictOpSrc;
683 820
684 if (format->direct.alphaMask && !new_format->direct.alphaMask) 821 if (format->direct.alphaMask && !new_format->direct.alphaMask)
685 { 822 {
697 834
698 return img; 835 return img;
699} 836}
700 837
701rxvt_img * 838rxvt_img *
702rxvt_img::blend (rxvt_img *img, double factor) 839rxvt_img::blend (rxvt_img *img, nv factor)
703{ 840{
704 rxvt_img *img2 = clone (); 841 rxvt_img *img2 = clone ();
705 Display *dpy = s->display->dpy; 842 Display *dpy = s->display->dpy;
706 Picture src = img->src_picture (); 843 Picture src = img->picture ();
707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 844 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
708 Picture mask = create_xrender_mask (dpy, img->pm, False, False); 845 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
709 846
710 XRenderColor mask_c; 847 XRenderColor mask_c;
711 848

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines