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.87 by root, Thu Jun 14 19:36:35 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;
538 596
539 return img; 597 return img;
540} 598}
541 599
542static void 600static void
543mat_invert (double mat[3][3], double (&inv)[3][3]) 601mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
544{ 602{
545 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];
546 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];
547 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];
548 606
549 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);
550 608
551 inv [0][0] = invdet * s0; 609 inv [0][0] = invdet * s0;
552 inv [0][1] = invdet * s1; 610 inv [0][1] = invdet * s1;
553 inv [0][2] = invdet * s2; 611 inv [0][2] = invdet * s2;
554 612
559 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]);
560 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]);
561 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]);
562} 620}
563 621
564static double 622static rxvt_img::nv
565mat_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)
566{ 624{
567 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];
568 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];
569 627
570 return v * (1. / w); 628 return v * (1. / w);
571} 629}
572 630
573rxvt_img * 631rxvt_img *
574rxvt_img::transform (double matrix[3][3]) 632rxvt_img::transform (nv matrix[3][3])
575{ 633{
576 // find new offset 634 // find new offset
577 int ox = mat_apply (matrix, 0, x, y); 635 int ox = mat_apply (matrix, 0, x, y);
578 int oy = mat_apply (matrix, 1, x, y); 636 int oy = mat_apply (matrix, 1, x, y);
579 637
580 // calculate new pixel bounding box coordinates 638 // calculate new pixel bounding box coordinates
581 double d [2], rmin[2], rmax[2]; 639 nv d [2], rmin[2], rmax[2];
582 640
583 for (int i = 0; i < 2; ++i) 641 for (int i = 0; i < 2; ++i)
584 { 642 {
585 double v; 643 nv v;
586 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;
587 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);
588 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);
589 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);
590 } 648 }
593 int dy = floor (rmin [1]); 651 int dy = floor (rmin [1]);
594 652
595 int new_width = ceil (rmax [0] - dx); 653 int new_width = ceil (rmax [0] - dx);
596 int new_height = ceil (rmax [1] - dy); 654 int new_height = ceil (rmax [1] - dy);
597 655
598 double inv[3][3]; 656 nv inv[3][3];
599 mat_invert (matrix, inv); 657 mat_invert (matrix, inv);
600 658
601 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, dx + d [0] - ox, dy + d [1] - oy, new_width, new_height, repeat);
602 img->alloc (); 660 img->alloc ();
603 661
604 Display *dpy = s->display->dpy; 662 Display *dpy = s->display->dpy;
605 Picture src = src_picture (); 663 Picture src = picture ();
606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 664 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
607 665
608 XTransform xfrm; 666 XTransform xfrm;
609 667
610 for (int i = 0; i < 3; ++i) 668 for (int i = 0; i < 3; ++i)
625rxvt_img::scale (int new_width, int new_height) 683rxvt_img::scale (int new_width, int new_height)
626{ 684{
627 if (w == new_width && h == new_height) 685 if (w == new_width && h == new_height)
628 return clone (); 686 return clone ();
629 687
630 double matrix[3][3] = { 688 nv matrix[3][3] = {
631 { new_width / (double)w, 0, 0 }, 689 { new_width / (nv)w, 0, 0 },
632 { 0, new_height / (double)h, 0 }, 690 { 0, new_height / (nv)h, 0 },
633 { 0, 0, 1 } 691 { 0, 0, 1 }
634 }; 692 };
635 693
636 int old_repeat_mode = repeat; 694 int old_repeat_mode = repeat;
637 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
638 696
643 701
644 return img; 702 return img;
645} 703}
646 704
647rxvt_img * 705rxvt_img *
648rxvt_img::rotate (int cx, int cy, double phi) 706rxvt_img::rotate (int cx, int cy, nv phi)
649{ 707{
650 double s = sin (phi); 708 nv s = sin (phi);
651 double c = cos (phi); 709 nv c = cos (phi);
652 710
653 double matrix[3][3] = { 711 nv matrix[3][3] = {
654 { c, -s, cx - c * cx + s * cy }, 712 { c, -s, cx - c * cx + s * cy },
655 { s, c, cy - s * cx - c * cy }, 713 { s, c, cy - s * cx - c * cy },
656 { 0, 0, 1 } 714 { 0, 0, 1 }
657 //{ c, -s, 0 }, 715 //{ c, -s, 0 },
658 //{ s, c, 0 }, 716 //{ s, c, 0 },
675 733
676 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);
677 img->alloc (); 735 img->alloc ();
678 736
679 Display *dpy = s->display->dpy; 737 Display *dpy = s->display->dpy;
680 Picture src = src_picture (); 738 Picture src = picture ();
681 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 739 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
682 int op = PictOpSrc; 740 int op = PictOpSrc;
683 741
684 if (format->direct.alphaMask && !new_format->direct.alphaMask) 742 if (format->direct.alphaMask && !new_format->direct.alphaMask)
685 { 743 {
697 755
698 return img; 756 return img;
699} 757}
700 758
701rxvt_img * 759rxvt_img *
702rxvt_img::blend (rxvt_img *img, double factor) 760rxvt_img::blend (rxvt_img *img, nv factor)
703{ 761{
704 rxvt_img *img2 = clone (); 762 rxvt_img *img2 = clone ();
705 Display *dpy = s->display->dpy; 763 Display *dpy = s->display->dpy;
706 Picture src = img->src_picture (); 764 Picture src = img->picture ();
707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 765 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
708 Picture mask = create_xrender_mask (dpy, img->pm, False, False); 766 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
709 767
710 XRenderColor mask_c; 768 XRenderColor mask_c;
711 769

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines