… | |
… | |
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 | |
6 | |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
8 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), |
8 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
9 | pm(0), refcnt(0) |
9 | pm(0), ref(0) |
10 | { |
10 | { |
11 | } |
11 | } |
12 | |
12 | |
13 | rxvt_img::rxvt_img (const rxvt_img &img) |
13 | rxvt_img::rxvt_img (const rxvt_img &img) |
14 | : s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), refcnt(img.refcnt) |
14 | : s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), ref(img.ref) |
15 | { |
15 | { |
16 | if (refcnt) |
|
|
17 | ++*refcnt; |
16 | ++ref->cnt; |
18 | } |
17 | } |
19 | |
18 | |
20 | #if 0 |
19 | #if 0 |
21 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
20 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
22 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
21 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
… | |
… | |
44 | return 0; |
43 | return 0; |
45 | |
44 | |
46 | rxvt_img *img = new rxvt_img ( |
45 | rxvt_img *img = new rxvt_img ( |
47 | s, |
46 | s, |
48 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
47 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
|
|
48 | 0, |
|
|
49 | 0, |
49 | root_pm_w, |
50 | root_pm_w, |
50 | root_pm_h |
51 | root_pm_h |
51 | ); |
52 | ); |
52 | |
53 | |
53 | img->pm = root_pixmap; |
54 | img->pm = root_pixmap; |
|
|
55 | img->ref = new pixref (root_pm_w, root_pm_h); |
|
|
56 | img->ref->ours = false; |
54 | |
57 | |
55 | return img; |
58 | return img; |
56 | } |
59 | } |
57 | |
60 | |
58 | rxvt_img * |
61 | rxvt_img * |
… | |
… | |
65 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
68 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
66 | |
69 | |
67 | rxvt_img *img = new rxvt_img ( |
70 | rxvt_img *img = new rxvt_img ( |
68 | s, |
71 | s, |
69 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
72 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
|
|
73 | 0, |
|
|
74 | 0, |
70 | gdk_pixbuf_get_width (pb), |
75 | gdk_pixbuf_get_width (pb), |
71 | gdk_pixbuf_get_height (pb) |
76 | gdk_pixbuf_get_height (pb) |
72 | ); |
77 | ); |
73 | img->alloc (); |
78 | img->alloc (); |
74 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
79 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
… | |
… | |
79 | } |
84 | } |
80 | |
85 | |
81 | void |
86 | void |
82 | rxvt_img::destroy () |
87 | rxvt_img::destroy () |
83 | { |
88 | { |
84 | if (!refcnt || --*refcnt) |
89 | if (--ref->cnt) |
85 | return; |
90 | return; |
86 | |
91 | |
87 | if (pm) |
92 | if (pm && ref->ours) |
88 | XFreePixmap (s->display->dpy, pm); |
93 | XFreePixmap (s->display->dpy, pm); |
89 | |
94 | |
90 | delete refcnt; |
95 | delete ref; |
91 | } |
96 | } |
92 | |
97 | |
93 | rxvt_img::~rxvt_img () |
98 | rxvt_img::~rxvt_img () |
94 | { |
99 | { |
95 | destroy (); |
100 | destroy (); |
… | |
… | |
97 | |
102 | |
98 | void |
103 | void |
99 | rxvt_img::alloc () |
104 | rxvt_img::alloc () |
100 | { |
105 | { |
101 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
106 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
102 | refcnt = new int (1); |
107 | ref = new pixref (w, h); |
|
|
108 | } |
|
|
109 | |
|
|
110 | Picture |
|
|
111 | rxvt_img::src_picture () |
|
|
112 | { |
|
|
113 | Display *dpy = s->display->dpy; |
|
|
114 | |
|
|
115 | XRenderPictureAttributes pa; |
|
|
116 | pa.repeat = repeat; |
|
|
117 | Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
118 | |
|
|
119 | return pic; |
103 | } |
120 | } |
104 | |
121 | |
105 | void |
122 | void |
106 | rxvt_img::unshare () |
123 | rxvt_img::unshare () |
107 | { |
124 | { |
108 | if (refcnt && *refcnt == 1) |
125 | if (ref->cnt == 1 && ref->ours) |
109 | return; |
126 | return; |
110 | |
127 | |
|
|
128 | //TODO: maybe should reify instead |
111 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
129 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
112 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
130 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
113 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
131 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
114 | XFreeGC (s->display->dpy, gc); |
132 | XFreeGC (s->display->dpy, gc); |
115 | |
133 | |
116 | destroy (); |
134 | destroy (); |
117 | |
135 | |
118 | pm = pm2; |
136 | pm = pm2; |
119 | refcnt = new int (1); |
137 | ref = new pixref (ref->w, ref->h); |
120 | } |
138 | } |
121 | |
139 | |
122 | void |
140 | void |
123 | rxvt_img::fill (const rxvt_color &c) |
141 | rxvt_img::fill (const rxvt_color &c) |
124 | { |
142 | { |
… | |
… | |
158 | |
176 | |
159 | Display *dpy = s->display->dpy; |
177 | Display *dpy = s->display->dpy; |
160 | int size = max (rh, rv) * 2 + 1; |
178 | int size = max (rh, rv) * 2 + 1; |
161 | double *kernel = (double *)malloc (size * sizeof (double)); |
179 | double *kernel = (double *)malloc (size * sizeof (double)); |
162 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
180 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
163 | rxvt_img *img = new rxvt_img (s, format, w, h); |
181 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
164 | img->alloc (); |
182 | img->alloc (); |
165 | |
183 | |
|
|
184 | Picture src = src_picture (); |
|
|
185 | |
166 | XRenderPictureAttributes pa; |
186 | XRenderPictureAttributes pa; |
167 | |
|
|
168 | pa.repeat = RepeatPad; |
187 | pa.repeat = RepeatPad; |
169 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
|
|
170 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
188 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
171 | |
189 | |
172 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
190 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
173 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
191 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
174 | XFreePixmap (dpy, tmp_pm); |
192 | XFreePixmap (dpy, tmp_pm); |
… | |
… | |
374 | { |
392 | { |
375 | return new rxvt_img (*this); |
393 | return new rxvt_img (*this); |
376 | } |
394 | } |
377 | |
395 | |
378 | rxvt_img * |
396 | rxvt_img * |
|
|
397 | rxvt_img::reify () |
|
|
398 | { |
|
|
399 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
|
|
400 | return clone (); |
|
|
401 | |
|
|
402 | Display *dpy = s->display->dpy; |
|
|
403 | |
|
|
404 | bool alpha = !format->direct.alphaMask |
|
|
405 | && (x || y) |
|
|
406 | && repeat == RepeatNone; |
|
|
407 | |
|
|
408 | rxvt_img *img = new rxvt_img (s, alpha ? XRenderFindStandardFormat (dpy, PictStandardARGB32) : format, 0, 0, w, h, repeat); |
|
|
409 | img->alloc (); |
|
|
410 | |
|
|
411 | Picture src = src_picture (); |
|
|
412 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
413 | |
|
|
414 | if (alpha) |
|
|
415 | { |
|
|
416 | XRenderColor rc = { 0, 0, 0, 0 }; |
|
|
417 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
|
|
418 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
|
|
419 | } |
|
|
420 | else |
|
|
421 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
|
|
422 | |
|
|
423 | XRenderFreePicture (dpy, src); |
|
|
424 | XRenderFreePicture (dpy, dst); |
|
|
425 | |
|
|
426 | return img; |
|
|
427 | } |
|
|
428 | |
|
|
429 | rxvt_img * |
379 | rxvt_img::sub_rect (int x, int y, int width, int height) |
430 | rxvt_img::sub_rect (int x, int y, int width, int height) |
380 | { |
431 | { |
|
|
432 | rxvt_img *img = clone (); |
|
|
433 | |
|
|
434 | img->x += x; |
|
|
435 | img->y += y; |
|
|
436 | |
|
|
437 | if (w != width || h != height) |
|
|
438 | { |
|
|
439 | img->w = width; |
|
|
440 | img->h = height; |
|
|
441 | |
|
|
442 | rxvt_img *img2 = img->reify (); |
|
|
443 | delete img; |
|
|
444 | img = img2; |
|
|
445 | } |
|
|
446 | |
|
|
447 | return img; |
|
|
448 | } |
|
|
449 | |
|
|
450 | rxvt_img * |
|
|
451 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
|
|
452 | { |
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
453 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
382 | img->alloc (); |
454 | img->alloc (); |
383 | |
455 | |
384 | Display *dpy = s->display->dpy; |
456 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
457 | Picture src = src_picture (); |
386 | pa.repeat = repeat; |
|
|
387 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
388 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
458 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
389 | |
|
|
390 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
|
|
391 | |
|
|
392 | XRenderFreePicture (dpy, src); |
|
|
393 | XRenderFreePicture (dpy, dst); |
|
|
394 | |
|
|
395 | return img; |
|
|
396 | } |
|
|
397 | |
|
|
398 | rxvt_img * |
|
|
399 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
|
|
400 | { |
|
|
401 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
|
|
402 | img->alloc (); |
|
|
403 | |
|
|
404 | Display *dpy = s->display->dpy; |
|
|
405 | XRenderPictureAttributes pa; |
|
|
406 | pa.repeat = repeat; |
|
|
407 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
408 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
409 | |
459 | |
410 | XTransform xfrm; |
460 | XTransform xfrm; |
411 | |
461 | |
412 | for (int i = 0; i < 3; ++i) |
462 | for (int i = 0; i < 3; ++i) |
413 | for (int j = 0; j < 3; ++j) |
463 | for (int j = 0; j < 3; ++j) |
414 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
464 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
415 | |
465 | |
|
|
466 | xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO |
|
|
467 | xfrm.matrix [0][3] += XDoubleToFixed (y); |
|
|
468 | |
416 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
469 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
417 | XRenderSetPictureTransform (dpy, src, &xfrm); |
470 | XRenderSetPictureTransform (dpy, src, &xfrm); |
418 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
471 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
419 | |
472 | |
420 | XRenderFreePicture (dpy, src); |
473 | XRenderFreePicture (dpy, src); |
… | |
… | |
424 | } |
477 | } |
425 | |
478 | |
426 | rxvt_img * |
479 | rxvt_img * |
427 | rxvt_img::scale (int new_width, int new_height) |
480 | rxvt_img::scale (int new_width, int new_height) |
428 | { |
481 | { |
|
|
482 | if (w == new_width && h == new_height) |
|
|
483 | return clone (); |
|
|
484 | |
429 | double matrix[9] = { |
485 | double matrix[9] = { |
430 | w / (double)new_width, 0, 0, |
486 | w / (double)new_width, 0, 0, |
431 | 0, h / (double)new_height, 0, |
487 | 0, h / (double)new_height, 0, |
432 | 0, 0, 1 |
488 | 0, 0, 1 |
433 | }; |
489 | }; |
434 | |
490 | |
|
|
491 | int old_repeat_mode = repeat; |
|
|
492 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
|
|
493 | |
435 | return transform (new_width, new_height, matrix); |
494 | rxvt_img *img = transform (new_width, new_height, matrix); |
|
|
495 | |
|
|
496 | repeat = old_repeat_mode; |
|
|
497 | img->repeat = repeat; |
|
|
498 | |
|
|
499 | return img; |
436 | } |
500 | } |
437 | |
501 | |
438 | rxvt_img * |
502 | rxvt_img * |
439 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
503 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
440 | { |
504 | { |
… | |
… | |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
518 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
455 | { |
519 | { |
456 | if (new_format == format) |
520 | if (new_format == format) |
457 | return clone (); |
521 | return clone (); |
458 | |
522 | |
459 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
523 | rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h, repeat); |
460 | img->alloc (); |
524 | img->alloc (); |
461 | |
525 | |
|
|
526 | printf ("convert %d to %d\n", format->direct.alphaMask, new_format->direct.alphaMask);//D |
|
|
527 | |
462 | Display *dpy = s->display->dpy; |
528 | Display *dpy = s->display->dpy; |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
529 | Picture src = src_picture (); |
464 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
530 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
465 | int op = PictOpSrc; |
531 | int op = PictOpSrc; |
466 | |
532 | |
467 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
533 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
468 | { |
534 | { |
… | |
… | |
487 | rxvt_img * |
553 | rxvt_img * |
488 | rxvt_img::blend (rxvt_img *img, double factor) |
554 | rxvt_img::blend (rxvt_img *img, double factor) |
489 | { |
555 | { |
490 | rxvt_img *img2 = clone (); |
556 | rxvt_img *img2 = clone (); |
491 | Display *dpy = s->display->dpy; |
557 | Display *dpy = s->display->dpy; |
492 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
558 | Picture src = img->src_picture (); |
493 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
559 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
494 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
560 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
495 | |
561 | |
496 | XRenderColor mask_c; |
562 | XRenderColor mask_c; |
497 | |
563 | |