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.82 by root, Thu Jun 14 17:06:57 2012 UTC

224 pm = pm2; 224 pm = pm2;
225 ref = new pixref (ref->w, ref->h); 225 ref = new pixref (ref->w, ref->h);
226} 226}
227 227
228void 228void
229rxvt_img::fill (const rxvt_color &c) 229rxvt_img::fill (const rgba &c)
230{ 230{
231 rgba cc;
232 c.get (cc);
233 XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; 231 XRenderColor rc = { c.r, c.g, c.b, c.a };
234 232
235 Display *dpy = s->display->dpy; 233 Display *dpy = s->display->dpy;
236 Picture src = src_picture (); 234 Picture src = src_picture ();
237 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); 235 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
238 XRenderFreePicture (dpy, src); 236 XRenderFreePicture (dpy, src);
397 if (r < 0 || g < 0 || b < 0 || a < 0) 395 if (r < 0 || g < 0 || b < 0 || a < 0)
398 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 396 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
399 397
400 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 398 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
401 img->alloc (); 399 img->alloc ();
402 400 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 401
409 // premultiply (yeah, these are not exact, sue me or fix it) 402 // premultiply (yeah, these are not exact, sue me or fix it)
410 r = (r * (a >> 8)) >> 8; 403 r = (r * (a >> 8)) >> 8;
411 g = (g * (a >> 8)) >> 8; 404 g = (g * (a >> 8)) >> 8;
412 b = (b * (a >> 8)) >> 8; 405 b = (b * (a >> 8)) >> 8;
519 } 512 }
520 513
521 return img; 514 return img;
522} 515}
523 516
524rxvt_img * 517static void
525rxvt_img::transform (int new_width, int new_height, double matrix[9]) 518mat_invert (double mat[3][3], double (&inv)[3][3])
526{ 519{
520 double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
521 double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
522 double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
523
524 double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
525
526 inv [0][0] = invdet * s0;
527 inv [0][1] = invdet * s1;
528 inv [0][2] = invdet * s2;
529
530 inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
531 inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
532 inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
533
534 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
535 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
536 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
537}
538
539static double
540mat_apply (double mat[3][3], int i, double x, double y)
541{
542 double v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
543 double w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
544
545 return v * (1. / w);
546}
547
548rxvt_img *
549rxvt_img::transform (double matrix[3][3])
550{
551 // find new offset
552 int ox = mat_apply (matrix, 0, x, y);
553 int oy = mat_apply (matrix, 1, x, y);
554
555 // calculate new pixel bounding box coordinates
556 double d [2], rmin[2], rmax[2];
557
558 for (int i = 0; i < 2; ++i)
559 {
560 double v;
561 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
562 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
563 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v);
564 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v);
565 }
566
567 int dx = floor (rmin [0]);
568 int dy = floor (rmin [1]);
569
570 int new_width = ceil (rmax [0] - dx);
571 int new_height = ceil (rmax [1] - dy);
572
573 double inv[3][3];
574 mat_invert (matrix, inv);
575
527 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); 576 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
528 img->alloc (); 577 img->alloc ();
529 578
530 Display *dpy = s->display->dpy; 579 Display *dpy = s->display->dpy;
531 Picture src = src_picture (); 580 Picture src = src_picture ();
532 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 581 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
533 582
534 XTransform xfrm; 583 XTransform xfrm;
535 584
536 for (int i = 0; i < 3; ++i) 585 for (int i = 0; i < 3; ++i)
537 for (int j = 0; j < 3; ++j) 586 for (int j = 0; j < 3; ++j)
538 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 587 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 588
543 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 589 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
544 XRenderSetPictureTransform (dpy, src, &xfrm); 590 XRenderSetPictureTransform (dpy, src, &xfrm);
545 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 591 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
546 592
547 XRenderFreePicture (dpy, src); 593 XRenderFreePicture (dpy, src);
548 XRenderFreePicture (dpy, dst); 594 XRenderFreePicture (dpy, dst);
549 595
550 return img; 596 return img;
554rxvt_img::scale (int new_width, int new_height) 600rxvt_img::scale (int new_width, int new_height)
555{ 601{
556 if (w == new_width && h == new_height) 602 if (w == new_width && h == new_height)
557 return clone (); 603 return clone ();
558 604
559 double matrix[9] = { 605 double matrix[3][3] = {
560 w / (double)new_width, 0, 0, 606 { new_width / (double)w, 0, 0 },
561 0, h / (double)new_height, 0, 607 { 0, new_height / (double)h, 0 },
562 0, 0, 1 608 { 0, 0, 1 }
563 }; 609 };
564 610
565 int old_repeat_mode = repeat; 611 int old_repeat_mode = repeat;
566 repeat = RepeatPad; // not right, but xrender can't properly scale it seems 612 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
567 613
568 rxvt_img *img = transform (new_width, new_height, matrix); 614 rxvt_img *img = transform (matrix);
569 615
570 repeat = old_repeat_mode; 616 repeat = old_repeat_mode;
571 img->repeat = repeat; 617 img->repeat = repeat;
572 618
573 return img; 619 return img;
574} 620}
575 621
576rxvt_img * 622rxvt_img *
577rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 623rxvt_img::rotate (int cx, int cy, double phi)
578{ 624{
579 double s = sin (phi); 625 double s = sin (phi);
580 double c = cos (phi); 626 double c = cos (phi);
581 627
582 double matrix[9] = { 628 double matrix[3][3] = {
583 c, -s, -c * x + s * y + x, 629 { c, -s, cx - c * cx + s * cy },
584 s, c, -s * x - c * y + y, 630 { s, c, cy - s * cx - c * cy },
585 0, 0, 1 631 { 0, 0, 1 }
632 //{ c, -s, 0 },
633 //{ s, c, 0 },
634 //{ 0, 0, 1 }
586 }; 635 };
587 636
588 return transform (new_width, new_height, matrix); 637 //move (-cx, -cy);
589} 638 rxvt_img *img = transform (matrix);
639 //move ( cx, cy);
640 //img->move (cx, cy);
590 641
642 return img;
643}
644
591rxvt_img * 645rxvt_img *
592rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 646rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
593{ 647{
594 if (new_format == format) 648 if (new_format == format)
595 return clone (); 649 return clone ();
596 650
597 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 651 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
603 int op = PictOpSrc; 657 int op = PictOpSrc;
604 658
605 if (format->direct.alphaMask && !new_format->direct.alphaMask) 659 if (format->direct.alphaMask && !new_format->direct.alphaMask)
606 { 660 {
607 // does it have to be that complicated 661 // 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 }; 662 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
612 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 663 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
613 664
614 op = PictOpOver; 665 op = PictOpOver;
615 } 666 }
616 667

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines