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.78 by sf-exg, Mon Jun 11 12:08:30 2012 UTC vs.
Revision 1.83 by root, Thu Jun 14 18:06:15 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
7static XRenderPictFormat *
8find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
9{
10 if (format->direct.alphaMask)
11 return format; // already has alpha
12
13 // try to find a suitable alpha format, one bit alpha is enough for our purposes
14 if (format->type == PictTypeDirect)
15 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
16 if (f->direct.alphaMask
17 && f->type == PictTypeDirect
18 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
19 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
20 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
21 return f;
22
23 // should be a very good fallback
24 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
25}
6 26
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) 27rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
8: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), 28: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), ref(0) 29 pm(0), ref(0)
10{ 30{
224 pm = pm2; 244 pm = pm2;
225 ref = new pixref (ref->w, ref->h); 245 ref = new pixref (ref->w, ref->h);
226} 246}
227 247
228void 248void
229rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
230{ 250{
231 rgba cc;
232 c.get (cc);
233 XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
234 252
235 Display *dpy = s->display->dpy; 253 Display *dpy = s->display->dpy;
236 Picture src = src_picture (); 254 Picture src = src_picture ();
237 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); 255 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
238 XRenderFreePicture (dpy, src); 256 XRenderFreePicture (dpy, src);
257}
258
259void
260rxvt_img::add_alpha ()
261{
262 if (format->direct.alphaMask)
263 return;
264
265 Display *dpy = s->display->dpy;
266
267 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
268 img->alloc ();
269
270 Picture src = src_picture ();
271 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
272
273 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
274
275 XRenderFreePicture (dpy, src);
276 XRenderFreePicture (dpy, dst);
277
278 ::swap (img->ref, ref);
279 ::swap (img->pm , pm );
280
281 delete img;
239} 282}
240 283
241static void 284static void
242get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
243{ 286{
397 if (r < 0 || g < 0 || b < 0 || a < 0) 440 if (r < 0 || g < 0 || b < 0 || a < 0)
398 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 441 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
399 442
400 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 443 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
401 img->alloc (); 444 img->alloc ();
402 445 img->fill (rgba (0, 0, 0, 0));
403 {
404 rxvt_color empty;
405 empty.set (s, rgba (0, 0, 0, 0));
406 img->fill (empty);
407 }
408 446
409 // premultiply (yeah, these are not exact, sue me or fix it) 447 // premultiply (yeah, these are not exact, sue me or fix it)
410 r = (r * (a >> 8)) >> 8; 448 r = (r * (a >> 8)) >> 8;
411 g = (g * (a >> 8)) >> 8; 449 g = (g * (a >> 8)) >> 8;
412 b = (b * (a >> 8)) >> 8; 450 b = (b * (a >> 8)) >> 8;
444rxvt_img::clone () 482rxvt_img::clone ()
445{ 483{
446 return new rxvt_img (*this); 484 return new rxvt_img (*this);
447} 485}
448 486
449static XRenderPictFormat *
450find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
451{
452 if (format->direct.alphaMask)
453 return format; // already has alpha
454
455 // try to find a suitable alpha format, one bit alpha is enough for our purposes
456 if (format->type == PictTypeDirect)
457 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
458 if (f->direct.alphaMask
459 && f->type == PictTypeDirect
460 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
461 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
462 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
463 return f;
464
465 // should be a very good fallback
466 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
467}
468
469rxvt_img * 487rxvt_img *
470rxvt_img::reify () 488rxvt_img::reify ()
471{ 489{
472 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
473 return clone (); 491 return clone ();
519 } 537 }
520 538
521 return img; 539 return img;
522} 540}
523 541
524rxvt_img * 542static void
525rxvt_img::transform (int new_width, int new_height, double matrix[9]) 543mat_invert (double mat[3][3], double (&inv)[3][3])
526{ 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 *
574rxvt_img::transform (double matrix[3][3])
575{
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
581 double d [2], rmin[2], rmax[2];
582
583 for (int i = 0; i < 2; ++i)
584 {
585 double v;
586 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);
588 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);
590 }
591
592 int dx = floor (rmin [0]);
593 int dy = floor (rmin [1]);
594
595 int new_width = ceil (rmax [0] - dx);
596 int new_height = ceil (rmax [1] - dy);
597
598 double inv[3][3];
599 mat_invert (matrix, inv);
600
527 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); 601 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
528 img->alloc (); 602 img->alloc ();
529 603
530 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
531 Picture src = src_picture (); 605 Picture src = src_picture ();
532 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
533 607
534 XTransform xfrm; 608 XTransform xfrm;
535 609
536 for (int i = 0; i < 3; ++i) 610 for (int i = 0; i < 3; ++i)
537 for (int j = 0; j < 3; ++j) 611 for (int j = 0; j < 3; ++j)
538 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
539
540 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
541 xfrm.matrix [1][2] -= XDoubleToFixed (y);
542 613
543 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
544 XRenderSetPictureTransform (dpy, src, &xfrm); 615 XRenderSetPictureTransform (dpy, src, &xfrm);
545 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 616 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
546 617
547 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
548 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
549 620
550 return img; 621 return img;
554rxvt_img::scale (int new_width, int new_height) 625rxvt_img::scale (int new_width, int new_height)
555{ 626{
556 if (w == new_width && h == new_height) 627 if (w == new_width && h == new_height)
557 return clone (); 628 return clone ();
558 629
559 double matrix[9] = { 630 double matrix[3][3] = {
560 w / (double)new_width, 0, 0, 631 { new_width / (double)w, 0, 0 },
561 0, h / (double)new_height, 0, 632 { 0, new_height / (double)h, 0 },
562 0, 0, 1 633 { 0, 0, 1 }
563 }; 634 };
564 635
565 int old_repeat_mode = repeat; 636 int old_repeat_mode = repeat;
566 repeat = RepeatPad; // not right, but xrender can't properly scale it seems 637 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
567 638
568 rxvt_img *img = transform (new_width, new_height, matrix); 639 rxvt_img *img = transform (matrix);
569 640
570 repeat = old_repeat_mode; 641 repeat = old_repeat_mode;
571 img->repeat = repeat; 642 img->repeat = repeat;
572 643
573 return img; 644 return img;
574} 645}
575 646
576rxvt_img * 647rxvt_img *
577rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 648rxvt_img::rotate (int cx, int cy, double phi)
578{ 649{
579 double s = sin (phi); 650 double s = sin (phi);
580 double c = cos (phi); 651 double c = cos (phi);
581 652
582 double matrix[9] = { 653 double matrix[3][3] = {
583 c, -s, -c * x + s * y + x, 654 { c, -s, cx - c * cx + s * cy },
584 s, c, -s * x - c * y + y, 655 { s, c, cy - s * cx - c * cy },
585 0, 0, 1 656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
586 }; 660 };
587 661
588 return transform (new_width, new_height, matrix); 662 //move (-cx, -cy);
589} 663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
590 666
667 return img;
668}
669
591rxvt_img * 670rxvt_img *
592rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
593{ 672{
594 if (new_format == format) 673 if (new_format == format)
595 return clone (); 674 return clone ();
596 675
597 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 676 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
603 int op = PictOpSrc; 682 int op = PictOpSrc;
604 683
605 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
606 { 685 {
607 // does it have to be that complicated 686 // does it have to be that complicated
608 rgba c;
609 bg.get (c);
610
611 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
612 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
613 689
614 op = PictOpOver; 690 op = PictOpOver;
615 } 691 }
616 692

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines