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.85 by root, Thu Jun 14 18:59:39 2012 UTC vs.
Revision 1.93 by root, Fri Jun 15 18:07:24 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
10namespace
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}
6 104
7#if 0 105#if 0
8struct pict 106struct pict
9{ 107{
10 Display *dpy; 108 Display *dpy;
312 410
313 delete img; 411 delete img;
314} 412}
315 413
316static void 414static void
317get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) 415get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params)
318{ 416{
319 rxvt_img::nv sigma = radius / 2.0; 417 nv sigma = radius / 2.0;
320 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; 418 nv scale = sqrt (2.0 * M_PI) * sigma;
321 rxvt_img::nv sum = 0.0; 419 nv sum = 0.0;
322 420
323 for (int i = 0; i < width; i++) 421 for (int i = 0; i < width; i++)
324 { 422 {
325 rxvt_img::nv x = i - width / 2; 423 nv x = i - width / 2;
326 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 424 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
327 sum += kernel[i]; 425 sum += kernel[i];
328 } 426 }
329 427
330 params[0] = XDoubleToFixed (width); 428 params[0] = XDoubleToFixed (width);
530 XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 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);
531 629
532 XRenderFreePicture (dpy, src); 630 XRenderFreePicture (dpy, src);
533 XRenderFreePicture (dpy, dst); 631 XRenderFreePicture (dpy, dst);
534 632
535 if (mask) 633 if (mask_p)
536 XRenderFreePicture (dpy, mask_p); 634 XRenderFreePicture (dpy, mask_p);
537} 635}
538 636
539rxvt_img * 637rxvt_img *
540rxvt_img::clone () 638rxvt_img::clone ()
563 661
564 if (alpha) 662 if (alpha)
565 { 663 {
566 XRenderColor rc = { 0, 0, 0, 0 }; 664 XRenderColor rc = { 0, 0, 0, 0 };
567 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 665 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
568 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); 666 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
569 } 667 }
570 else 668 else
571 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); 669 XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h);
572 670
573 XRenderFreePicture (dpy, src); 671 XRenderFreePicture (dpy, src);
574 XRenderFreePicture (dpy, dst); 672 XRenderFreePicture (dpy, dst);
575 673
576 return img; 674 return img;
579rxvt_img * 677rxvt_img *
580rxvt_img::sub_rect (int x, int y, int width, int height) 678rxvt_img::sub_rect (int x, int y, int width, int height)
581{ 679{
582 rxvt_img *img = clone (); 680 rxvt_img *img = clone ();
583 681
584 img->x += x; 682 img->x -= x;
585 img->y += y; 683 img->y -= y;
586 684
587 if (w != width || h != height) 685 if (w != width || h != height)
588 { 686 {
589 img->w = width; 687 img->w = width;
590 img->h = height; 688 img->h = height;
595 } 693 }
596 694
597 return img; 695 return img;
598} 696}
599 697
600static void
601mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
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
622static rxvt_img::nv
623mat_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
631rxvt_img * 698rxvt_img *
632rxvt_img::transform (nv matrix[3][3]) 699rxvt_img::transform (nv matrix[3][3])
633{ 700{
634 // find new offset
635 int ox = mat_apply (matrix, 0, x, y);
636 int oy = mat_apply (matrix, 1, x, y);
637
638 // calculate new pixel bounding box coordinates 701 // calculate new pixel bounding box coordinates
639 nv d [2], rmin[2], rmax[2]; 702 nv r[2], rmin[2], rmax[2];
703
704 mat3x3 m (matrix);
640 705
641 for (int i = 0; i < 2; ++i) 706 for (int i = 0; i < 2; ++i)
642 { 707 {
643 nv v; 708 nv v;
709
644 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v; 710 v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v;
645 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); 711 v = m.apply1 (i, w+x, 0+y); 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); 712 v = m.apply1 (i, 0+x, h+y); 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); 713 v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
648 } 714 }
649 715
716 float sx = rmin [0] - x;
717 float sy = rmin [1] - y;
718
719 // TODO: adjust matrix for subpixel accuracy
650 int dx = floor (rmin [0]); 720 int nx = floor (rmin [0]);
651 int dy = floor (rmin [1]); 721 int ny = floor (rmin [1]);
652 722
653 int new_width = ceil (rmax [0] - dx); 723 int new_width = ceil (rmax [0] - rmin [0]);
654 int new_height = ceil (rmax [1] - dy); 724 int new_height = ceil (rmax [1] - rmin [1]);
655 725
656 nv inv[3][3]; 726 m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y);
657 mat_invert (matrix, inv);
658 727
728 mat3x3 inv = m.invert ();
729
659 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat); 730 rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat);
660 img->alloc (); 731 img->alloc ();
661 732
662 Display *dpy = s->display->dpy; 733 Display *dpy = s->display->dpy;
663 Picture src = picture (); 734 Picture src = picture ();
664 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 735 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
669 for (int j = 0; j < 3; ++j) 740 for (int j = 0; j < 3; ++j)
670 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); 741 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
671 742
672 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 743 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
673 XRenderSetPictureTransform (dpy, src, &xfrm); 744 XRenderSetPictureTransform (dpy, src, &xfrm);
674 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); 745 XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
746#if 1
747 {
748 XRenderColor rc = { 65535,0,0,65535 };
749 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height);
750 }{
751 XRenderColor rc = { 0,0,0,65535 };
752 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2);
753 }
754 XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
755#endif
675 756
676 XRenderFreePicture (dpy, src); 757 XRenderFreePicture (dpy, src);
677 XRenderFreePicture (dpy, dst); 758 XRenderFreePicture (dpy, dst);
678 759
679 return img; 760 return img;
707{ 788{
708 nv s = sin (phi); 789 nv s = sin (phi);
709 nv c = cos (phi); 790 nv c = cos (phi);
710 791
711 nv matrix[3][3] = { 792 nv matrix[3][3] = {
793#if 0
712 { c, -s, cx - c * cx + s * cy }, 794 { c, -s, cx - c * cx + s * cy },
713 { s, c, cy - s * cx - c * cy }, 795 { s, c, cy - s * cx - c * cy },
714 { 0, 0, 1 } 796 { 0, 0, 1 }
797#else
715 //{ c, -s, 0 }, 798 { c, -s, 0 },
716 //{ s, c, 0 }, 799 { s, c, 0 },
717 //{ 0, 0, 1 } 800 { 0, 0, 1 }
801#endif
718 }; 802 };
719 803
720 //move (-cx, -cy); 804 move (-cx, -cy);
721 rxvt_img *img = transform (matrix); 805 rxvt_img *img = transform (matrix);
722 //move ( cx, cy); 806 move ( cx, cy);
723 //img->move (cx, cy); 807 img->move (cx, cy);
724 808
725 return img; 809 return img;
726} 810}
727 811
728rxvt_img * 812rxvt_img *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines