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 | |
|
|
7 | #if 0 |
|
|
8 | struct pict |
|
|
9 | { |
|
|
10 | Display *dpy; |
|
|
11 | Picture pic; |
|
|
12 | |
|
|
13 | operator Picture () const |
|
|
14 | { |
|
|
15 | return pic; |
|
|
16 | } |
|
|
17 | |
|
|
18 | pict () |
|
|
19 | : pic (0) |
|
|
20 | { |
|
|
21 | } |
|
|
22 | |
|
|
23 | pict (rxvt_img *img, XRenderPictFormat *format = 0) |
|
|
24 | : dpy (img->s->display->dpy) |
|
|
25 | { |
|
|
26 | XRenderPictureAttributes pa; |
|
|
27 | pa.repeat = img->repeat; |
|
|
28 | pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); |
|
|
29 | } |
|
|
30 | |
|
|
31 | ~pict () |
|
|
32 | { |
|
|
33 | if (pic) |
|
|
34 | XRenderFreePicture (dpy, pic); |
|
|
35 | } |
|
|
36 | }; |
|
|
37 | #endif |
6 | |
38 | |
7 | static XRenderPictFormat * |
39 | static XRenderPictFormat * |
8 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
40 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
9 | { |
41 | { |
10 | if (format->direct.alphaMask) |
42 | if (format->direct.alphaMask) |
… | |
… | |
215 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
247 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
216 | ref = new pixref (w, h); |
248 | ref = new pixref (w, h); |
217 | } |
249 | } |
218 | |
250 | |
219 | Picture |
251 | Picture |
220 | rxvt_img::src_picture () |
252 | rxvt_img::picture () |
221 | { |
253 | { |
222 | Display *dpy = s->display->dpy; |
254 | Display *dpy = s->display->dpy; |
223 | |
255 | |
224 | XRenderPictureAttributes pa; |
256 | XRenderPictureAttributes pa; |
225 | pa.repeat = repeat; |
257 | pa.repeat = repeat; |
… | |
… | |
249 | rxvt_img::fill (const rgba &c) |
281 | rxvt_img::fill (const rgba &c) |
250 | { |
282 | { |
251 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
283 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
252 | |
284 | |
253 | Display *dpy = s->display->dpy; |
285 | Display *dpy = s->display->dpy; |
254 | Picture src = src_picture (); |
286 | Picture src = picture (); |
255 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
287 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
256 | XRenderFreePicture (dpy, src); |
288 | XRenderFreePicture (dpy, src); |
257 | } |
289 | } |
258 | |
290 | |
259 | void |
291 | void |
… | |
… | |
265 | Display *dpy = s->display->dpy; |
297 | Display *dpy = s->display->dpy; |
266 | |
298 | |
267 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
299 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
268 | img->alloc (); |
300 | img->alloc (); |
269 | |
301 | |
270 | Picture src = src_picture (); |
302 | Picture src = picture (); |
271 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
303 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
272 | |
304 | |
273 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
305 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
274 | |
306 | |
275 | XRenderFreePicture (dpy, src); |
307 | XRenderFreePicture (dpy, src); |
… | |
… | |
280 | |
312 | |
281 | delete img; |
313 | delete img; |
282 | } |
314 | } |
283 | |
315 | |
284 | static void |
316 | static void |
285 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
317 | get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) |
286 | { |
318 | { |
287 | double sigma = radius / 2.0; |
319 | rxvt_img::nv sigma = radius / 2.0; |
288 | double scale = sqrt (2.0 * M_PI) * sigma; |
320 | rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; |
289 | double sum = 0.0; |
321 | rxvt_img::nv sum = 0.0; |
290 | |
322 | |
291 | for (int i = 0; i < width; i++) |
323 | for (int i = 0; i < width; i++) |
292 | { |
324 | { |
293 | double x = i - width / 2; |
325 | rxvt_img::nv x = i - width / 2; |
294 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
326 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
295 | sum += kernel[i]; |
327 | sum += kernel[i]; |
296 | } |
328 | } |
297 | |
329 | |
298 | params[0] = XDoubleToFixed (width); |
330 | params[0] = XDoubleToFixed (width); |
… | |
… | |
308 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
340 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
309 | return clone (); |
341 | return clone (); |
310 | |
342 | |
311 | Display *dpy = s->display->dpy; |
343 | Display *dpy = s->display->dpy; |
312 | int size = max (rh, rv) * 2 + 1; |
344 | int size = max (rh, rv) * 2 + 1; |
313 | double *kernel = (double *)malloc (size * sizeof (double)); |
345 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
314 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
346 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
315 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
347 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
316 | img->alloc (); |
348 | img->alloc (); |
317 | |
349 | |
318 | XRenderPictureAttributes pa; |
350 | XRenderPictureAttributes pa; |
… | |
… | |
449 | g = (g * (a >> 8)) >> 8; |
481 | g = (g * (a >> 8)) >> 8; |
450 | b = (b * (a >> 8)) >> 8; |
482 | b = (b * (a >> 8)) >> 8; |
451 | |
483 | |
452 | Display *dpy = s->display->dpy; |
484 | Display *dpy = s->display->dpy; |
453 | |
485 | |
454 | Picture src = src_picture (); |
486 | Picture src = picture (); |
455 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
487 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
456 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
488 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
457 | |
489 | |
458 | //TODO: this operator does not yet implement some useful contrast |
490 | //TODO: this operator does not yet implement some useful contrast |
459 | while (r | g | b | a) |
491 | while (r | g | b | a) |
… | |
… | |
476 | ::swap (img->pm , pm ); |
508 | ::swap (img->pm , pm ); |
477 | |
509 | |
478 | delete img; |
510 | delete img; |
479 | } |
511 | } |
480 | |
512 | |
|
|
513 | void |
|
|
514 | rxvt_img::draw (rxvt_img *img, int op, nv mask) |
|
|
515 | { |
|
|
516 | unshare (); |
|
|
517 | |
|
|
518 | Display *dpy = s->display->dpy; |
|
|
519 | Picture src = img->picture (); |
|
|
520 | Picture dst = picture (); |
|
|
521 | Picture mask_p = 0; |
|
|
522 | |
|
|
523 | if (mask != 1.) |
|
|
524 | { |
|
|
525 | mask_p = create_xrender_mask (dpy, img->pm, False, False); |
|
|
526 | XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; |
|
|
527 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
528 | } |
|
|
529 | |
|
|
530 | XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); |
|
|
531 | |
|
|
532 | XRenderFreePicture (dpy, src); |
|
|
533 | XRenderFreePicture (dpy, dst); |
|
|
534 | |
|
|
535 | if (mask_p) |
|
|
536 | XRenderFreePicture (dpy, mask_p); |
|
|
537 | } |
|
|
538 | |
481 | rxvt_img * |
539 | rxvt_img * |
482 | rxvt_img::clone () |
540 | rxvt_img::clone () |
483 | { |
541 | { |
484 | return new rxvt_img (*this); |
542 | return new rxvt_img (*this); |
485 | } |
543 | } |
… | |
… | |
498 | && repeat == RepeatNone; // and we have no good pixels to fill with |
556 | && repeat == RepeatNone; // and we have no good pixels to fill with |
499 | |
557 | |
500 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
558 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
501 | img->alloc (); |
559 | img->alloc (); |
502 | |
560 | |
503 | Picture src = src_picture (); |
561 | Picture src = picture (); |
504 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
562 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
505 | |
563 | |
506 | if (alpha) |
564 | if (alpha) |
507 | { |
565 | { |
508 | XRenderColor rc = { 0, 0, 0, 0 }; |
566 | XRenderColor rc = { 0, 0, 0, 0 }; |
509 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
567 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
510 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
568 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
511 | } |
569 | } |
512 | else |
570 | else |
513 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
571 | XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); |
514 | |
572 | |
515 | XRenderFreePicture (dpy, src); |
573 | XRenderFreePicture (dpy, src); |
516 | XRenderFreePicture (dpy, dst); |
574 | XRenderFreePicture (dpy, dst); |
517 | |
575 | |
518 | return img; |
576 | return img; |
… | |
… | |
521 | rxvt_img * |
579 | rxvt_img * |
522 | rxvt_img::sub_rect (int x, int y, int width, int height) |
580 | rxvt_img::sub_rect (int x, int y, int width, int height) |
523 | { |
581 | { |
524 | rxvt_img *img = clone (); |
582 | rxvt_img *img = clone (); |
525 | |
583 | |
526 | img->x += x; |
584 | img->x -= x; |
527 | img->y += y; |
585 | img->y -= y; |
528 | |
586 | |
529 | if (w != width || h != height) |
587 | if (w != width || h != height) |
530 | { |
588 | { |
531 | img->w = width; |
589 | img->w = width; |
532 | img->h = height; |
590 | img->h = height; |
… | |
… | |
538 | |
596 | |
539 | return img; |
597 | return img; |
540 | } |
598 | } |
541 | |
599 | |
542 | static void |
600 | static void |
543 | mat_invert (double mat[3][3], double (&inv)[3][3]) |
601 | mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) |
544 | { |
602 | { |
545 | double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
603 | rxvt_img::nv 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]; |
604 | rxvt_img::nv 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]; |
605 | rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
548 | |
606 | |
549 | double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); |
607 | rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); |
550 | |
608 | |
551 | inv [0][0] = invdet * s0; |
609 | inv [0][0] = invdet * s0; |
552 | inv [0][1] = invdet * s1; |
610 | inv [0][1] = invdet * s1; |
553 | inv [0][2] = invdet * s2; |
611 | inv [0][2] = invdet * s2; |
554 | |
612 | |
… | |
… | |
559 | inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); |
617 | 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]); |
618 | 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]); |
619 | inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); |
562 | } |
620 | } |
563 | |
621 | |
564 | static double |
622 | static rxvt_img::nv |
565 | mat_apply (double mat[3][3], int i, double x, double y) |
623 | mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) |
566 | { |
624 | { |
567 | double v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
625 | rxvt_img::nv 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]; |
626 | rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; |
569 | |
627 | |
570 | return v * (1. / w); |
628 | return v * (1. / w); |
571 | } |
629 | } |
572 | |
630 | |
573 | rxvt_img * |
631 | rxvt_img * |
574 | rxvt_img::transform (double matrix[3][3]) |
632 | rxvt_img::transform (nv matrix[3][3]) |
575 | { |
633 | { |
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 |
634 | // calculate new pixel bounding box coordinates |
581 | double d [2], rmin[2], rmax[2]; |
635 | nv rmin[2], rmax[2]; |
582 | |
636 | |
583 | for (int i = 0; i < 2; ++i) |
637 | for (int i = 0; i < 2; ++i) |
584 | { |
638 | { |
585 | double v; |
639 | nv v; |
586 | v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v; |
640 | v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; |
587 | v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); |
641 | v = mat_apply (matrix, i, w+x, 0+y); 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); |
642 | v = mat_apply (matrix, i, 0+x, h+y); 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); |
643 | v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
590 | } |
644 | } |
591 | |
645 | |
592 | int dx = floor (rmin [0]); |
646 | int dx = floor (rmin [0]); |
593 | int dy = floor (rmin [1]); |
647 | int dy = floor (rmin [1]); |
594 | |
648 | |
595 | int new_width = ceil (rmax [0] - dx); |
649 | int new_width = ceil (rmax [0] - dx); |
596 | int new_height = ceil (rmax [1] - dy); |
650 | int new_height = ceil (rmax [1] - dy); |
597 | |
651 | |
598 | double inv[3][3]; |
652 | nv inv[3][3]; |
599 | mat_invert (matrix, inv); |
653 | mat_invert (matrix, inv); |
600 | |
654 | |
601 | rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat); |
655 | rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat); |
602 | img->alloc (); |
656 | img->alloc (); |
603 | |
657 | |
604 | Display *dpy = s->display->dpy; |
658 | Display *dpy = s->display->dpy; |
605 | Picture src = src_picture (); |
659 | Picture src = picture (); |
606 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
660 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
607 | |
661 | |
608 | XTransform xfrm; |
662 | XTransform xfrm; |
609 | |
663 | |
610 | for (int i = 0; i < 3; ++i) |
664 | for (int i = 0; i < 3; ++i) |
… | |
… | |
625 | rxvt_img::scale (int new_width, int new_height) |
679 | rxvt_img::scale (int new_width, int new_height) |
626 | { |
680 | { |
627 | if (w == new_width && h == new_height) |
681 | if (w == new_width && h == new_height) |
628 | return clone (); |
682 | return clone (); |
629 | |
683 | |
630 | double matrix[3][3] = { |
684 | nv matrix[3][3] = { |
631 | { new_width / (double)w, 0, 0 }, |
685 | { new_width / (nv)w, 0, 0 }, |
632 | { 0, new_height / (double)h, 0 }, |
686 | { 0, new_height / (nv)h, 0 }, |
633 | { 0, 0, 1 } |
687 | { 0, 0, 1 } |
634 | }; |
688 | }; |
635 | |
689 | |
636 | int old_repeat_mode = repeat; |
690 | int old_repeat_mode = repeat; |
637 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
691 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
638 | |
692 | |
… | |
… | |
643 | |
697 | |
644 | return img; |
698 | return img; |
645 | } |
699 | } |
646 | |
700 | |
647 | rxvt_img * |
701 | rxvt_img * |
648 | rxvt_img::rotate (int cx, int cy, double phi) |
702 | rxvt_img::rotate (int cx, int cy, nv phi) |
649 | { |
703 | { |
650 | double s = sin (phi); |
704 | nv s = sin (phi); |
651 | double c = cos (phi); |
705 | nv c = cos (phi); |
652 | |
706 | |
653 | double matrix[3][3] = { |
707 | nv matrix[3][3] = { |
654 | { c, -s, cx - c * cx + s * cy }, |
708 | { c, -s, cx - c * cx + s * cy + 200 }, |
655 | { s, c, cy - s * cx - c * cy }, |
709 | { s, c, cy - s * cx - c * cy }, |
656 | { 0, 0, 1 } |
710 | { 0, 0, 1 } |
657 | //{ c, -s, 0 }, |
711 | //{ c, -s, 0 }, |
658 | //{ s, c, 0 }, |
712 | //{ s, c, 0 }, |
659 | //{ 0, 0, 1 } |
713 | //{ 0, 0, 1 } |
… | |
… | |
675 | |
729 | |
676 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
730 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
677 | img->alloc (); |
731 | img->alloc (); |
678 | |
732 | |
679 | Display *dpy = s->display->dpy; |
733 | Display *dpy = s->display->dpy; |
680 | Picture src = src_picture (); |
734 | Picture src = picture (); |
681 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
735 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
682 | int op = PictOpSrc; |
736 | int op = PictOpSrc; |
683 | |
737 | |
684 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
738 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
685 | { |
739 | { |
… | |
… | |
697 | |
751 | |
698 | return img; |
752 | return img; |
699 | } |
753 | } |
700 | |
754 | |
701 | rxvt_img * |
755 | rxvt_img * |
702 | rxvt_img::blend (rxvt_img *img, double factor) |
756 | rxvt_img::blend (rxvt_img *img, nv factor) |
703 | { |
757 | { |
704 | rxvt_img *img2 = clone (); |
758 | rxvt_img *img2 = clone (); |
705 | Display *dpy = s->display->dpy; |
759 | Display *dpy = s->display->dpy; |
706 | Picture src = img->src_picture (); |
760 | Picture src = img->picture (); |
707 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
761 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
708 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
762 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
709 | |
763 | |
710 | XRenderColor mask_c; |
764 | XRenderColor mask_c; |
711 | |
765 | |