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.89 by root, Thu Jun 14 19:44:31 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}
6 99
7#if 0 100#if 0
8struct pict 101struct pict
9{ 102{
10 Display *dpy; 103 Display *dpy;
312 405
313 delete img; 406 delete img;
314} 407}
315 408
316static void 409static void
317get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) 410get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params)
318{ 411{
319 rxvt_img::nv sigma = radius / 2.0; 412 nv sigma = radius / 2.0;
320 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; 413 nv scale = sqrt (2.0 * M_PI) * sigma;
321 rxvt_img::nv sum = 0.0; 414 nv sum = 0.0;
322 415
323 for (int i = 0; i < width; i++) 416 for (int i = 0; i < width; i++)
324 { 417 {
325 rxvt_img::nv x = i - width / 2; 418 nv x = i - width / 2;
326 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 419 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
327 sum += kernel[i]; 420 sum += kernel[i];
328 } 421 }
329 422
330 params[0] = XDoubleToFixed (width); 423 params[0] = XDoubleToFixed (width);
595 } 688 }
596 689
597 return img; 690 return img;
598} 691}
599 692
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 * 693rxvt_img *
632rxvt_img::transform (nv matrix[3][3]) 694rxvt_img::transform (nv matrix[3][3])
633{ 695{
634 // calculate new pixel bounding box coordinates 696 // calculate new pixel bounding box coordinates
635 nv rmin[2], rmax[2]; 697 nv r[2], rmin[2], rmax[2];
698
699 mat3x3 m (matrix);
636 700
637 for (int i = 0; i < 2; ++i) 701 for (int i = 0; i < 2; ++i)
638 { 702 {
639 nv v; 703 nv v;
704
640 v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; 705 v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v;
641 v = mat_apply (matrix, i, w+x, 0+y); 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);
642 v = mat_apply (matrix, i, 0+x, h+y); 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);
643 v = mat_apply (matrix, i, w+x, h+y); 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);
644 } 709 }
710
711 float sx = rmin [0] - x;
712 float sy = rmin [1] - y;
645 713
646 // TODO: adjust matrix for subpixel accuracy 714 // TODO: adjust matrix for subpixel accuracy
647 int dx = floor (rmin [0]); 715 int nx = floor (rmin [0]);
648 int dy = floor (rmin [1]); 716 int ny = floor (rmin [1]);
649 717
650 int new_width = ceil (rmax [0] - dx); 718 int new_width = ceil (rmax [0] - rmin [0]);
651 int new_height = ceil (rmax [1] - dy); 719 int new_height = ceil (rmax [1] - rmin [1]);
652 720
653 nv inv[3][3]; 721 m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y);
654 mat_invert (matrix, inv);
655 722
723 mat3x3 inv = m.invert ();
724
656 rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat); 725 rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat);
657 img->alloc (); 726 img->alloc ();
658 727
659 Display *dpy = s->display->dpy; 728 Display *dpy = s->display->dpy;
660 Picture src = picture (); 729 Picture src = picture ();
661 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 730 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
666 for (int j = 0; j < 3; ++j) 735 for (int j = 0; j < 3; ++j)
667 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); 736 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
668 737
669 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 738 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
670 XRenderSetPictureTransform (dpy, src, &xfrm); 739 XRenderSetPictureTransform (dpy, src, &xfrm);
671 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
672 751
673 XRenderFreePicture (dpy, src); 752 XRenderFreePicture (dpy, src);
674 XRenderFreePicture (dpy, dst); 753 XRenderFreePicture (dpy, dst);
675 754
676 return img; 755 return img;
704{ 783{
705 nv s = sin (phi); 784 nv s = sin (phi);
706 nv c = cos (phi); 785 nv c = cos (phi);
707 786
708 nv matrix[3][3] = { 787 nv matrix[3][3] = {
788#if 0
709 { c, -s, cx - c * cx + s * cy + 200 }, 789 { c, -s, cx - c * cx + s * cy },
710 { s, c, cy - s * cx - c * cy }, 790 { s, c, cy - s * cx - c * cy },
711 { 0, 0, 1 } 791 { 0, 0, 1 }
792#else
712 //{ c, -s, 0 }, 793 { c, -s, 0 },
713 //{ s, c, 0 }, 794 { s, c, 0 },
714 //{ 0, 0, 1 } 795 { 0, 0, 1 }
796#endif
715 }; 797 };
716 798
717 //move (-cx, -cy); 799 move (-cx, -cy);
718 rxvt_img *img = transform (matrix); 800 rxvt_img *img = transform (matrix);
719 //move ( cx, cy); 801 move ( cx, cy);
720 //img->move (cx, cy); 802 img->move (cx, cy);
721 803
722 return img; 804 return img;
723} 805}
724 806
725rxvt_img * 807rxvt_img *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines