… | |
… | |
595 | } |
595 | } |
596 | |
596 | |
597 | return img; |
597 | return img; |
598 | } |
598 | } |
599 | |
599 | |
|
|
600 | typedef rxvt_img::nv matrix[3][3]; |
|
|
601 | |
600 | static void |
602 | static void |
601 | mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) |
603 | mat_invert (matrix mat, rxvt_img::nv (&inv)[3][3]) |
602 | { |
604 | { |
603 | rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
605 | rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
604 | rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; |
606 | rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; |
605 | rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
607 | rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
606 | |
608 | |
… | |
… | |
618 | inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); |
620 | inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); |
619 | inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); |
621 | inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); |
620 | } |
622 | } |
621 | |
623 | |
622 | static rxvt_img::nv |
624 | static rxvt_img::nv |
623 | mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) |
625 | mat_apply (matrix mat, int i, rxvt_img::nv x, rxvt_img::nv y) |
624 | { |
626 | { |
625 | rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
627 | rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
626 | rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; |
628 | rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; |
627 | |
629 | |
628 | return v * (1. / w); |
630 | return v * (1. / w); |
629 | } |
631 | } |
630 | |
632 | |
|
|
633 | static void |
|
|
634 | mat_mult (matrix a, matrix b, matrix r) |
|
|
635 | { |
|
|
636 | for (int i = 0; i < 3; ++i) |
|
|
637 | for (int j = 0; j < 3; ++j) |
|
|
638 | r [i][j] = a [i][0] * b [0][j] |
|
|
639 | + a [i][1] * b [1][j] |
|
|
640 | + a [i][2] * b [2][j]; |
|
|
641 | } |
|
|
642 | |
|
|
643 | static void |
|
|
644 | mat_trans (rxvt_img::nv x, rxvt_img::nv y, matrix mat) |
|
|
645 | { |
|
|
646 | mat [0][0] = 1; mat [0][1] = 0; mat [0][2] = x; |
|
|
647 | mat [1][0] = 0; mat [1][1] = 1; mat [1][2] = y; |
|
|
648 | mat [2][0] = 0; mat [2][1] = 0; mat [2][2] = 1; |
|
|
649 | } |
|
|
650 | |
631 | rxvt_img * |
651 | rxvt_img * |
632 | rxvt_img::transform (nv matrix[3][3]) |
652 | rxvt_img::transform (nv matrix[3][3]) |
633 | { |
653 | { |
634 | // calculate new pixel bounding box coordinates |
654 | // calculate new pixel bounding box coordinates |
635 | nv rmin[2], rmax[2]; |
655 | nv r[2], rmin[2], rmax[2]; |
636 | |
656 | |
637 | for (int i = 0; i < 2; ++i) |
657 | for (int i = 0; i < 2; ++i) |
638 | { |
658 | { |
639 | nv v; |
659 | nv v; |
640 | |
660 | |
641 | v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; |
661 | v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v; |
642 | v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
662 | v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
643 | v = mat_apply (matrix, i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
663 | v = mat_apply (matrix, i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
644 | v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
664 | v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
645 | } |
665 | } |
646 | |
666 | |
|
|
667 | float sx = rmin [0] - x; |
|
|
668 | float sy = rmin [1] - y; |
|
|
669 | |
647 | // TODO: adjust matrix for subpixel accuracy |
670 | // TODO: adjust matrix for subpixel accuracy |
648 | int dx = floor (rmin [0]); |
671 | int nx = floor (rmin [0]); |
649 | int dy = floor (rmin [1]); |
672 | int ny = floor (rmin [1]); |
650 | |
673 | |
651 | int new_width = ceil (rmax [0] - dx); |
674 | int new_width = ceil (rmax [0] - rmin [0]); |
652 | int new_height = ceil (rmax [1] - dy); |
675 | int new_height = ceil (rmax [1] - rmin [1]); |
653 | |
676 | |
654 | nv inv[3][3]; |
677 | ::matrix tr, tmp; |
|
|
678 | mat_trans (x, y, tr); |
|
|
679 | mat_mult (matrix, tr, tmp); |
|
|
680 | mat_trans (-x, -y, tr); |
|
|
681 | mat_mult (tr, tmp, matrix); |
|
|
682 | |
|
|
683 | ::matrix inv; |
655 | mat_invert (matrix, inv); |
684 | mat_invert (matrix, inv); |
656 | |
685 | |
657 | rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat); |
686 | rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat); |
658 | img->alloc (); |
687 | img->alloc (); |
659 | |
688 | |
660 | Display *dpy = s->display->dpy; |
689 | Display *dpy = s->display->dpy; |
661 | Picture src = picture (); |
690 | Picture src = picture (); |
662 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
691 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
… | |
… | |
667 | for (int j = 0; j < 3; ++j) |
696 | for (int j = 0; j < 3; ++j) |
668 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
697 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
669 | |
698 | |
670 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
699 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
671 | XRenderSetPictureTransform (dpy, src, &xfrm); |
700 | XRenderSetPictureTransform (dpy, src, &xfrm); |
672 | XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); |
701 | XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
|
|
702 | #if 1 |
|
|
703 | { |
|
|
704 | XRenderColor rc = { 65535,0,0,65535 }; |
|
|
705 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height); |
|
|
706 | }{ |
|
|
707 | XRenderColor rc = { 0,0,0,65535 }; |
|
|
708 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2); |
|
|
709 | } |
|
|
710 | XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
|
|
711 | #endif |
673 | |
712 | |
674 | XRenderFreePicture (dpy, src); |
713 | XRenderFreePicture (dpy, src); |
675 | XRenderFreePicture (dpy, dst); |
714 | XRenderFreePicture (dpy, dst); |
676 | |
715 | |
677 | return img; |
716 | return img; |
… | |
… | |
705 | { |
744 | { |
706 | nv s = sin (phi); |
745 | nv s = sin (phi); |
707 | nv c = cos (phi); |
746 | nv c = cos (phi); |
708 | |
747 | |
709 | nv matrix[3][3] = { |
748 | nv matrix[3][3] = { |
|
|
749 | #if 0 |
710 | { c, -s, cx - c * cx + s * cy + 200 }, |
750 | { c, -s, cx - c * cx + s * cy }, |
711 | { s, c, cy - s * cx - c * cy }, |
751 | { s, c, cy - s * cx - c * cy }, |
712 | { 0, 0, 1 } |
752 | { 0, 0, 1 } |
|
|
753 | #else |
713 | //{ c, -s, 0 }, |
754 | { c, -s, 0 }, |
714 | //{ s, c, 0 }, |
755 | { s, c, 0 }, |
715 | //{ 0, 0, 1 } |
756 | { 0, 0, 1 } |
|
|
757 | #endif |
716 | }; |
758 | }; |
717 | |
759 | |
718 | //move (-cx, -cy); |
760 | move (-cx, -cy); |
719 | rxvt_img *img = transform (matrix); |
761 | rxvt_img *img = transform (matrix); |
720 | //move ( cx, cy); |
762 | move ( cx, cy); |
721 | //img->move (cx, cy); |
763 | img->move (cx, cy); |
722 | |
764 | |
723 | return img; |
765 | return img; |
724 | } |
766 | } |
725 | |
767 | |
726 | rxvt_img * |
768 | rxvt_img * |