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.76 by root, Sun Jun 10 10:29:26 2012 UTC vs.
Revision 1.84 by root, Thu Jun 14 18:19:11 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{
50 70
51 return img; 71 return img;
52} 72}
53 73
54# if HAVE_PIXBUF 74# if HAVE_PIXBUF
75
55rxvt_img * 76rxvt_img *
56rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) 77rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
57{ 78{
58 Display *dpy = s->display->dpy; 79 Display *dpy = s->display->dpy;
59 80
166 187
167 g_object_unref (pb); 188 g_object_unref (pb);
168 189
169 return img; 190 return img;
170} 191}
192
171# endif 193# endif
172 194
173void 195void
174rxvt_img::destroy () 196rxvt_img::destroy ()
175{ 197{
222 pm = pm2; 244 pm = pm2;
223 ref = new pixref (ref->w, ref->h); 245 ref = new pixref (ref->w, ref->h);
224} 246}
225 247
226void 248void
227rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
228{ 250{
229 XGCValues gcv; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
230 gcv.foreground = c; 252
231 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 253 Display *dpy = s->display->dpy;
232 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 254 Picture src = src_picture ();
233 XFreeGC (s->display->dpy, gc); 255 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
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;
234} 282}
235 283
236static void 284static void
237get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
238{ 286{
239 double sigma = radius / 2.0; 287 rxvt_img::nv sigma = radius / 2.0;
240 double scale = sqrt (2.0 * M_PI) * sigma; 288 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
241 double sum = 0.0; 289 rxvt_img::nv sum = 0.0;
242 290
243 for (int i = 0; i < width; i++) 291 for (int i = 0; i < width; i++)
244 { 292 {
245 double x = i - width / 2; 293 rxvt_img::nv x = i - width / 2;
246 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 294 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
247 sum += kernel[i]; 295 sum += kernel[i];
248 } 296 }
249 297
250 params[0] = XDoubleToFixed (width); 298 params[0] = XDoubleToFixed (width);
260 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 308 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
261 return clone (); 309 return clone ();
262 310
263 Display *dpy = s->display->dpy; 311 Display *dpy = s->display->dpy;
264 int size = max (rh, rv) * 2 + 1; 312 int size = max (rh, rv) * 2 + 1;
265 double *kernel = (double *)malloc (size * sizeof (double)); 313 nv *kernel = (nv *)malloc (size * sizeof (nv));
266 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
267 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 315 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
268 img->alloc (); 316 img->alloc ();
269 317
270 XRenderPictureAttributes pa; 318 XRenderPictureAttributes pa;
308 w, h); 356 w, h);
309 } 357 }
310 358
311 free (kernel); 359 free (kernel);
312 free (params); 360 free (params);
361
313 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
314 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
315 XRenderFreePicture (dpy, tmp); 364 XRenderFreePicture (dpy, tmp);
316 365
317 return img; 366 return img;
391 if (r < 0 || g < 0 || b < 0 || a < 0) 440 if (r < 0 || g < 0 || b < 0 || a < 0)
392 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 441 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
393 442
394 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);
395 img->alloc (); 444 img->alloc ();
396 445 img->fill (rgba (0, 0, 0, 0));
397 {
398 rxvt_color empty;
399 empty.set (s, rgba (0, 0, 0, 0));
400 img->fill (empty);
401 }
402 446
403 // premultiply (yeah, these are not exact, sue me or fix it) 447 // premultiply (yeah, these are not exact, sue me or fix it)
404 r = (r * (a >> 8)) >> 8; 448 r = (r * (a >> 8)) >> 8;
405 g = (g * (a >> 8)) >> 8; 449 g = (g * (a >> 8)) >> 8;
406 b = (b * (a >> 8)) >> 8; 450 b = (b * (a >> 8)) >> 8;
438rxvt_img::clone () 482rxvt_img::clone ()
439{ 483{
440 return new rxvt_img (*this); 484 return new rxvt_img (*this);
441} 485}
442 486
443static XRenderPictFormat *
444find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
445{
446 if (format->direct.alphaMask)
447 return format; // already has alpha
448
449 // try to find a suitable alpha format, one bit alpha is enough for our purposes
450 if (format->type == PictTypeDirect)
451 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
452 if (f->direct.alphaMask
453 && f->type == PictTypeDirect
454 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
455 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
456 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
457 return f;
458
459 // should be a very good fallback
460 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
461}
462
463rxvt_img * 487rxvt_img *
464rxvt_img::reify () 488rxvt_img::reify ()
465{ 489{
466 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
467 return clone (); 491 return clone ();
513 } 537 }
514 538
515 return img; 539 return img;
516} 540}
517 541
518rxvt_img * 542static void
519rxvt_img::transform (int new_width, int new_height, double matrix[9]) 543mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
520{ 544{
545 rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
546 rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
547 rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
548
549 rxvt_img::nv 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 rxvt_img::nv
565mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y)
566{
567 rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
568 rxvt_img::nv 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 (nv 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 nv d [2], rmin[2], rmax[2];
582
583 for (int i = 0; i < 2; ++i)
584 {
585 nv 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 nv inv[3][3];
599 mat_invert (matrix, inv);
600
521 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);
522 img->alloc (); 602 img->alloc ();
523 603
524 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
525 Picture src = src_picture (); 605 Picture src = src_picture ();
526 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
527 607
528 XTransform xfrm; 608 XTransform xfrm;
529 609
530 for (int i = 0; i < 3; ++i) 610 for (int i = 0; i < 3; ++i)
531 for (int j = 0; j < 3; ++j) 611 for (int j = 0; j < 3; ++j)
532 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
533
534 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
535 xfrm.matrix [1][2] -= XDoubleToFixed (y);
536 613
537 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
538 XRenderSetPictureTransform (dpy, src, &xfrm); 615 XRenderSetPictureTransform (dpy, src, &xfrm);
539 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);
540 617
541 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
542 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
543 620
544 return img; 621 return img;
548rxvt_img::scale (int new_width, int new_height) 625rxvt_img::scale (int new_width, int new_height)
549{ 626{
550 if (w == new_width && h == new_height) 627 if (w == new_width && h == new_height)
551 return clone (); 628 return clone ();
552 629
553 double matrix[9] = { 630 nv matrix[3][3] = {
554 w / (double)new_width, 0, 0, 631 { new_width / (nv)w, 0, 0 },
555 0, h / (double)new_height, 0, 632 { 0, new_height / (nv)h, 0 },
556 0, 0, 1 633 { 0, 0, 1 }
557 }; 634 };
558 635
559 int old_repeat_mode = repeat; 636 int old_repeat_mode = repeat;
560 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems 637 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
561 638
562 rxvt_img *img = transform (new_width, new_height, matrix); 639 rxvt_img *img = transform (matrix);
563 640
564 repeat = old_repeat_mode; 641 repeat = old_repeat_mode;
565 img->repeat = repeat; 642 img->repeat = repeat;
566 643
567 return img; 644 return img;
568} 645}
569 646
570rxvt_img * 647rxvt_img *
571rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 648rxvt_img::rotate (int cx, int cy, nv phi)
572{ 649{
573 double s = sin (phi); 650 nv s = sin (phi);
574 double c = cos (phi); 651 nv c = cos (phi);
575 652
576 double matrix[9] = { 653 nv matrix[3][3] = {
577 c, -s, -c * x + s * y + x, 654 { c, -s, cx - c * cx + s * cy },
578 s, c, -s * x - c * y + y, 655 { s, c, cy - s * cx - c * cy },
579 0, 0, 1 656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
580 }; 660 };
581 661
582 return transform (new_width, new_height, matrix); 662 //move (-cx, -cy);
583} 663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
584 666
667 return img;
668}
669
585rxvt_img * 670rxvt_img *
586rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
587{ 672{
588 if (new_format == format) 673 if (new_format == format)
589 return clone (); 674 return clone ();
590 675
591 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);
597 int op = PictOpSrc; 682 int op = PictOpSrc;
598 683
599 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
600 { 685 {
601 // does it have to be that complicated 686 // does it have to be that complicated
602 rgba c;
603 bg.get (c);
604
605 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
606 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
607 689
608 op = PictOpOver; 690 op = PictOpOver;
609 } 691 }
610 692
615 697
616 return img; 698 return img;
617} 699}
618 700
619rxvt_img * 701rxvt_img *
620rxvt_img::blend (rxvt_img *img, double factor) 702rxvt_img::blend (rxvt_img *img, nv factor)
621{ 703{
622 rxvt_img *img2 = clone (); 704 rxvt_img *img2 = clone ();
623 Display *dpy = s->display->dpy; 705 Display *dpy = s->display->dpy;
624 Picture src = img->src_picture (); 706 Picture src = img->src_picture ();
625 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines