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.91 by root, Fri Jun 15 13:21:59 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
6 38
7static XRenderPictFormat * 39static XRenderPictFormat *
8find_alpha_format_for (Display *dpy, XRenderPictFormat *format) 40find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
9{ 41{
10 if (format->direct.alphaMask) 42 if (format->direct.alphaMask)
215 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);
216 ref = new pixref (w, h); 248 ref = new pixref (w, h);
217} 249}
218 250
219Picture 251Picture
220rxvt_img::src_picture () 252rxvt_img::picture ()
221{ 253{
222 Display *dpy = s->display->dpy; 254 Display *dpy = s->display->dpy;
223 255
224 XRenderPictureAttributes pa; 256 XRenderPictureAttributes pa;
225 pa.repeat = repeat; 257 pa.repeat = repeat;
249rxvt_img::fill (const rgba &c) 281rxvt_img::fill (const rgba &c)
250{ 282{
251 XRenderColor rc = { c.r, c.g, c.b, c.a }; 283 XRenderColor rc = { c.r, c.g, c.b, c.a };
252 284
253 Display *dpy = s->display->dpy; 285 Display *dpy = s->display->dpy;
254 Picture src = src_picture (); 286 Picture src = picture ();
255 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); 287 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
256 XRenderFreePicture (dpy, src); 288 XRenderFreePicture (dpy, src);
257} 289}
258 290
259void 291void
265 Display *dpy = s->display->dpy; 297 Display *dpy = s->display->dpy;
266 298
267 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); 299 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
268 img->alloc (); 300 img->alloc ();
269 301
270 Picture src = src_picture (); 302 Picture src = picture ();
271 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 303 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
272 304
273 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 305 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
274 306
275 XRenderFreePicture (dpy, src); 307 XRenderFreePicture (dpy, src);
280 312
281 delete img; 313 delete img;
282} 314}
283 315
284static void 316static void
285get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 317get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
286{ 318{
287 double sigma = radius / 2.0; 319 rxvt_img::nv sigma = radius / 2.0;
288 double scale = sqrt (2.0 * M_PI) * sigma; 320 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
289 double sum = 0.0; 321 rxvt_img::nv sum = 0.0;
290 322
291 for (int i = 0; i < width; i++) 323 for (int i = 0; i < width; i++)
292 { 324 {
293 double x = i - width / 2; 325 rxvt_img::nv x = i - width / 2;
294 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 326 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
295 sum += kernel[i]; 327 sum += kernel[i];
296 } 328 }
297 329
298 params[0] = XDoubleToFixed (width); 330 params[0] = XDoubleToFixed (width);
308 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 340 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
309 return clone (); 341 return clone ();
310 342
311 Display *dpy = s->display->dpy; 343 Display *dpy = s->display->dpy;
312 int size = max (rh, rv) * 2 + 1; 344 int size = max (rh, rv) * 2 + 1;
313 double *kernel = (double *)malloc (size * sizeof (double)); 345 nv *kernel = (nv *)malloc (size * sizeof (nv));
314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 346 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
315 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);
316 img->alloc (); 348 img->alloc ();
317 349
318 XRenderPictureAttributes pa; 350 XRenderPictureAttributes pa;
449 g = (g * (a >> 8)) >> 8; 481 g = (g * (a >> 8)) >> 8;
450 b = (b * (a >> 8)) >> 8; 482 b = (b * (a >> 8)) >> 8;
451 483
452 Display *dpy = s->display->dpy; 484 Display *dpy = s->display->dpy;
453 485
454 Picture src = src_picture (); 486 Picture src = picture ();
455 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); 487 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
456 Picture mul = create_xrender_mask (dpy, pm, True, True); 488 Picture mul = create_xrender_mask (dpy, pm, True, True);
457 489
458 //TODO: this operator does not yet implement some useful contrast 490 //TODO: this operator does not yet implement some useful contrast
459 while (r | g | b | a) 491 while (r | g | b | a)
476 ::swap (img->pm , pm ); 508 ::swap (img->pm , pm );
477 509
478 delete img; 510 delete img;
479} 511}
480 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_p)
536 XRenderFreePicture (dpy, mask_p);
537}
538
481rxvt_img * 539rxvt_img *
482rxvt_img::clone () 540rxvt_img::clone ()
483{ 541{
484 return new rxvt_img (*this); 542 return new rxvt_img (*this);
485} 543}
498 && repeat == RepeatNone; // and we have no good pixels to fill with 556 && repeat == RepeatNone; // and we have no good pixels to fill with
499 557
500 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);
501 img->alloc (); 559 img->alloc ();
502 560
503 Picture src = src_picture (); 561 Picture src = picture ();
504 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 562 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
505 563
506 if (alpha) 564 if (alpha)
507 { 565 {
508 XRenderColor rc = { 0, 0, 0, 0 }; 566 XRenderColor rc = { 0, 0, 0, 0 };
509 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 567 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); 568 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
511 } 569 }
512 else 570 else
513 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); 571 XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h);
514 572
515 XRenderFreePicture (dpy, src); 573 XRenderFreePicture (dpy, src);
516 XRenderFreePicture (dpy, dst); 574 XRenderFreePicture (dpy, dst);
517 575
518 return img; 576 return img;
521rxvt_img * 579rxvt_img *
522rxvt_img::sub_rect (int x, int y, int width, int height) 580rxvt_img::sub_rect (int x, int y, int width, int height)
523{ 581{
524 rxvt_img *img = clone (); 582 rxvt_img *img = clone ();
525 583
526 img->x += x; 584 img->x -= x;
527 img->y += y; 585 img->y -= y;
528 586
529 if (w != width || h != height) 587 if (w != width || h != height)
530 { 588 {
531 img->w = width; 589 img->w = width;
532 img->h = height; 590 img->h = height;
537 } 595 }
538 596
539 return img; 597 return img;
540} 598}
541 599
600typedef rxvt_img::nv matrix[3][3];
601
542static void 602static void
543mat_invert (double mat[3][3], double (&inv)[3][3]) 603mat_invert (matrix mat, rxvt_img::nv (&inv)[3][3])
544{ 604{
545 double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; 605 rxvt_img::nv 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]; 606 rxvt_img::nv 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]; 607 rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
548 608
549 double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); 609 rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
550 610
551 inv [0][0] = invdet * s0; 611 inv [0][0] = invdet * s0;
552 inv [0][1] = invdet * s1; 612 inv [0][1] = invdet * s1;
553 inv [0][2] = invdet * s2; 613 inv [0][2] = invdet * s2;
554 614
559 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); 619 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]); 620 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]); 621 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
562} 622}
563 623
564static double 624static rxvt_img::nv
565mat_apply (double mat[3][3], int i, double x, double y) 625mat_apply (matrix mat, int i, rxvt_img::nv x, rxvt_img::nv y)
566{ 626{
567 double v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; 627 rxvt_img::nv 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]; 628 rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
569 629
570 return v * (1. / w); 630 return v * (1. / w);
571} 631}
572 632
633static void
634mat_mult (matrix a, matrix b, matrix r)
635{
636 for (int i = 0; i < 3; ++i)
637 for (int j = 0; j < 3; ++j)
638 r [i][j] = a [i][0] * b [0][j]
639 + a [i][1] * b [1][j]
640 + a [i][2] * b [2][j];
641}
642
643static void
644mat_trans (rxvt_img::nv x, rxvt_img::nv y, matrix mat)
645{
646 mat [0][0] = 1; mat [0][1] = 0; mat [0][2] = x;
647 mat [1][0] = 0; mat [1][1] = 1; mat [1][2] = y;
648 mat [2][0] = 0; mat [2][1] = 0; mat [2][2] = 1;
649}
650
573rxvt_img * 651rxvt_img *
574rxvt_img::transform (double matrix[3][3]) 652rxvt_img::transform (nv matrix[3][3])
575{ 653{
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 654 // calculate new pixel bounding box coordinates
581 double d [2], rmin[2], rmax[2]; 655 nv r[2], rmin[2], rmax[2];
582 656
583 for (int i = 0; i < 2; ++i) 657 for (int i = 0; i < 2; ++i)
584 { 658 {
585 double v; 659 nv v;
660
586 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v; 661 v = mat_apply (matrix, 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); 662 v = mat_apply (matrix, 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); 663 v = mat_apply (matrix, 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); 664 v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
590 } 665 }
591 666
667 float sx = rmin [0] - x;
668 float sy = rmin [1] - y;
669
670 // TODO: adjust matrix for subpixel accuracy
592 int dx = floor (rmin [0]); 671 int nx = floor (rmin [0]);
593 int dy = floor (rmin [1]); 672 int ny = floor (rmin [1]);
594 673
595 int new_width = ceil (rmax [0] - dx); 674 int new_width = ceil (rmax [0] - rmin [0]);
596 int new_height = ceil (rmax [1] - dy); 675 int new_height = ceil (rmax [1] - rmin [1]);
597 676
598 double inv[3][3]; 677 ::matrix tr, tmp;
678 mat_trans (x, y, tr);
679 mat_mult (matrix, tr, tmp);
680 mat_trans (-x, -y, tr);
681 mat_mult (tr, tmp, matrix);
682
683 ::matrix inv;
599 mat_invert (matrix, inv); 684 mat_invert (matrix, inv);
600 685
601 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat); 686 rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat);
602 img->alloc (); 687 img->alloc ();
603 688
604 Display *dpy = s->display->dpy; 689 Display *dpy = s->display->dpy;
605 Picture src = src_picture (); 690 Picture src = picture ();
606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 691 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
607 692
608 XTransform xfrm; 693 XTransform xfrm;
609 694
610 for (int i = 0; i < 3; ++i) 695 for (int i = 0; i < 3; ++i)
611 for (int j = 0; j < 3; ++j) 696 for (int j = 0; j < 3; ++j)
612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); 697 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
613 698
614 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 699 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
615 XRenderSetPictureTransform (dpy, src, &xfrm); 700 XRenderSetPictureTransform (dpy, src, &xfrm);
616 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); 701 XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
702#if 1
703 {
704 XRenderColor rc = { 65535,0,0,65535 };
705 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height);
706 }{
707 XRenderColor rc = { 0,0,0,65535 };
708 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2);
709 }
710 XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
711#endif
617 712
618 XRenderFreePicture (dpy, src); 713 XRenderFreePicture (dpy, src);
619 XRenderFreePicture (dpy, dst); 714 XRenderFreePicture (dpy, dst);
620 715
621 return img; 716 return img;
625rxvt_img::scale (int new_width, int new_height) 720rxvt_img::scale (int new_width, int new_height)
626{ 721{
627 if (w == new_width && h == new_height) 722 if (w == new_width && h == new_height)
628 return clone (); 723 return clone ();
629 724
630 double matrix[3][3] = { 725 nv matrix[3][3] = {
631 { new_width / (double)w, 0, 0 }, 726 { new_width / (nv)w, 0, 0 },
632 { 0, new_height / (double)h, 0 }, 727 { 0, new_height / (nv)h, 0 },
633 { 0, 0, 1 } 728 { 0, 0, 1 }
634 }; 729 };
635 730
636 int old_repeat_mode = repeat; 731 int old_repeat_mode = repeat;
637 repeat = RepeatPad; // not right, but xrender can't properly scale it seems 732 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
638 733
643 738
644 return img; 739 return img;
645} 740}
646 741
647rxvt_img * 742rxvt_img *
648rxvt_img::rotate (int cx, int cy, double phi) 743rxvt_img::rotate (int cx, int cy, nv phi)
649{ 744{
650 double s = sin (phi); 745 nv s = sin (phi);
651 double c = cos (phi); 746 nv c = cos (phi);
652 747
653 double matrix[3][3] = { 748 nv matrix[3][3] = {
749#if 0
654 { c, -s, cx - c * cx + s * cy }, 750 { c, -s, cx - c * cx + s * cy },
655 { s, c, cy - s * cx - c * cy }, 751 { s, c, cy - s * cx - c * cy },
656 { 0, 0, 1 } 752 { 0, 0, 1 }
753#else
657 //{ c, -s, 0 }, 754 { c, -s, 0 },
658 //{ s, c, 0 }, 755 { s, c, 0 },
659 //{ 0, 0, 1 } 756 { 0, 0, 1 }
757#endif
660 }; 758 };
661 759
662 //move (-cx, -cy); 760 move (-cx, -cy);
663 rxvt_img *img = transform (matrix); 761 rxvt_img *img = transform (matrix);
664 //move ( cx, cy); 762 move ( cx, cy);
665 //img->move (cx, cy); 763 img->move (cx, cy);
666 764
667 return img; 765 return img;
668} 766}
669 767
670rxvt_img * 768rxvt_img *
675 773
676 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 774 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
677 img->alloc (); 775 img->alloc ();
678 776
679 Display *dpy = s->display->dpy; 777 Display *dpy = s->display->dpy;
680 Picture src = src_picture (); 778 Picture src = picture ();
681 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 779 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
682 int op = PictOpSrc; 780 int op = PictOpSrc;
683 781
684 if (format->direct.alphaMask && !new_format->direct.alphaMask) 782 if (format->direct.alphaMask && !new_format->direct.alphaMask)
685 { 783 {
697 795
698 return img; 796 return img;
699} 797}
700 798
701rxvt_img * 799rxvt_img *
702rxvt_img::blend (rxvt_img *img, double factor) 800rxvt_img::blend (rxvt_img *img, nv factor)
703{ 801{
704 rxvt_img *img2 = clone (); 802 rxvt_img *img2 = clone ();
705 Display *dpy = s->display->dpy; 803 Display *dpy = s->display->dpy;
706 Picture src = img->src_picture (); 804 Picture src = img->picture ();
707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 805 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
708 Picture mask = create_xrender_mask (dpy, img->pm, False, False); 806 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
709 807
710 XRenderColor mask_c; 808 XRenderColor mask_c;
711 809

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines