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.80 by root, Tue Jun 12 18:25:57 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 ();
558 // find new offset 576 // find new offset
559 int ox = mat_apply (matrix, 0, x, y); 577 int ox = mat_apply (matrix, 0, x, y);
560 int oy = mat_apply (matrix, 1, x, y); 578 int oy = mat_apply (matrix, 1, x, y);
561 579
562 // calculate new pixel bounding box coordinates 580 // calculate new pixel bounding box coordinates
563 double rmin[2], rmax[2]; 581 double d [2], rmin[2], rmax[2];
564 582
565 for (int i = 0; i < 2; ++i) 583 for (int i = 0; i < 2; ++i)
566 { 584 {
567 double v; 585 double v;
568 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; 586 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
569 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); 587 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
570 v = mat_apply (matrix, i, 0, h); 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);
571 v = mat_apply (matrix, i, w, 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);
572 } 590 }
573 591
578 int new_height = ceil (rmax [1] - dy); 596 int new_height = ceil (rmax [1] - dy);
579 597
580 double inv[3][3]; 598 double inv[3][3];
581 mat_invert (matrix, inv); 599 mat_invert (matrix, inv);
582 600
583 rxvt_img *img = new rxvt_img (s, format, ox - dx, oy - dy, 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);
584 img->alloc (); 602 img->alloc ();
585 603
586 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
587 Picture src = src_picture (); 605 Picture src = src_picture ();
588 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
631{ 649{
632 double s = sin (phi); 650 double s = sin (phi);
633 double c = cos (phi); 651 double c = cos (phi);
634 652
635 double matrix[3][3] = { 653 double matrix[3][3] = {
636 //{ c, -s, cx - c * cx + s * cy }, 654 { c, -s, cx - c * cx + s * cy },
637 //{ s, c, cy - s * cx - c * cy }, 655 { s, c, cy - s * cx - c * cy },
638 //{ 0, 0, 1 } 656 { 0, 0, 1 }
639 { c, -s, 0 }, 657 //{ c, -s, 0 },
640 { s, c, 0 }, 658 //{ s, c, 0 },
641 { 0, 0, 1 } 659 //{ 0, 0, 1 }
642 }; 660 };
643 661
644 move (-cx, -cy); 662 //move (-cx, -cy);
645 rxvt_img *img = transform (matrix); 663 rxvt_img *img = transform (matrix);
646 move ( cx, cy); 664 //move ( cx, cy);
647 img->move (cx, cy); 665 //img->move (cx, cy);
648 666
649 return img; 667 return img;
650} 668}
651 669
652rxvt_img * 670rxvt_img *
653rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
654{ 672{
655 if (new_format == format) 673 if (new_format == format)
656 return clone (); 674 return clone ();
657 675
658 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);
664 int op = PictOpSrc; 682 int op = PictOpSrc;
665 683
666 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
667 { 685 {
668 // does it have to be that complicated 686 // does it have to be that complicated
669 rgba c;
670 bg.get (c);
671
672 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
673 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
674 689
675 op = PictOpOver; 690 op = PictOpOver;
676 } 691 }
677 692

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines