… | |
… | |
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 | #define float_to_component(d) ((d) * 65535.99) |
|
|
8 | |
|
|
9 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
10 | : s(screen), w(width), h(height), format(format), shared(false) |
8 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), |
|
|
9 | pm(0), refcnt(0) |
11 | { |
10 | { |
12 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
13 | } |
11 | } |
14 | |
12 | |
|
|
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) |
|
|
15 | { |
|
|
16 | if (refcnt) |
|
|
17 | ++*refcnt; |
|
|
18 | } |
|
|
19 | |
|
|
20 | #if 0 |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
21 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
16 | : s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) |
22 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
17 | { |
23 | { |
18 | } |
24 | } |
|
|
25 | #endif |
19 | |
26 | |
20 | rxvt_img * |
27 | rxvt_img * |
21 | rxvt_img::new_from_root (rxvt_screen *s) |
28 | rxvt_img::new_from_root (rxvt_screen *s) |
22 | { |
29 | { |
23 | Display *dpy = s->display->dpy; |
30 | Display *dpy = s->display->dpy; |
… | |
… | |
38 | |
45 | |
39 | rxvt_img *img = new rxvt_img ( |
46 | rxvt_img *img = new rxvt_img ( |
40 | s, |
47 | s, |
41 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
48 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
42 | root_pm_w, |
49 | root_pm_w, |
43 | root_pm_h, |
50 | root_pm_h |
44 | root_pixmap |
|
|
45 | ); |
51 | ); |
46 | |
52 | |
47 | img->shared = true; |
53 | img->pm = root_pixmap; |
48 | |
54 | |
49 | return img; |
55 | return img; |
50 | } |
56 | } |
51 | |
57 | |
52 | rxvt_img * |
58 | rxvt_img * |
… | |
… | |
62 | s, |
68 | s, |
63 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
69 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
64 | gdk_pixbuf_get_width (pb), |
70 | gdk_pixbuf_get_width (pb), |
65 | gdk_pixbuf_get_height (pb) |
71 | gdk_pixbuf_get_height (pb) |
66 | ); |
72 | ); |
67 | |
73 | img->alloc (); |
68 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
74 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
69 | |
75 | |
|
|
76 | g_object_unref (pb); |
|
|
77 | |
70 | return img; |
78 | return img; |
71 | } |
79 | } |
72 | |
80 | |
|
|
81 | void |
|
|
82 | rxvt_img::destroy () |
|
|
83 | { |
|
|
84 | if (!refcnt || --*refcnt) |
|
|
85 | return; |
|
|
86 | |
|
|
87 | if (pm) |
|
|
88 | XFreePixmap (s->display->dpy, pm); |
|
|
89 | |
|
|
90 | delete refcnt; |
|
|
91 | } |
|
|
92 | |
73 | rxvt_img::~rxvt_img () |
93 | rxvt_img::~rxvt_img () |
74 | { |
94 | { |
75 | if (!shared) |
95 | destroy (); |
76 | XFreePixmap (s->display->dpy, pm); |
96 | } |
|
|
97 | |
|
|
98 | void |
|
|
99 | rxvt_img::alloc () |
|
|
100 | { |
|
|
101 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
102 | refcnt = new int (1); |
77 | } |
103 | } |
78 | |
104 | |
79 | void |
105 | void |
80 | rxvt_img::unshare () |
106 | rxvt_img::unshare () |
81 | { |
107 | { |
82 | if (!shared) |
108 | if (refcnt && *refcnt == 1) |
83 | return; |
109 | return; |
84 | |
110 | |
85 | rxvt_img *img = clone (); |
111 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
112 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
113 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
|
|
114 | XFreeGC (s->display->dpy, gc); |
86 | |
115 | |
87 | ::swap (pm , img->pm); |
116 | destroy (); |
88 | ::swap (shared, img->shared); |
|
|
89 | |
117 | |
90 | delete img; |
118 | pm = pm2; |
|
|
119 | refcnt = new int (1); |
91 | } |
120 | } |
92 | |
121 | |
93 | void |
122 | void |
94 | rxvt_img::fill (const rxvt_color &c) |
123 | rxvt_img::fill (const rxvt_color &c) |
95 | { |
124 | { |
… | |
… | |
130 | Display *dpy = s->display->dpy; |
159 | Display *dpy = s->display->dpy; |
131 | int size = max (rh, rv) * 2 + 1; |
160 | int size = max (rh, rv) * 2 + 1; |
132 | double *kernel = (double *)malloc (size * sizeof (double)); |
161 | double *kernel = (double *)malloc (size * sizeof (double)); |
133 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
162 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
134 | rxvt_img *img = new rxvt_img (s, format, w, h); |
163 | rxvt_img *img = new rxvt_img (s, format, w, h); |
|
|
164 | img->alloc (); |
135 | |
165 | |
136 | XRenderPictureAttributes pa; |
166 | XRenderPictureAttributes pa; |
137 | |
167 | |
138 | pa.repeat = RepeatPad; |
168 | pa.repeat = RepeatPad; |
139 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
169 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
… | |
… | |
198 | |
228 | |
199 | return mask; |
229 | return mask; |
200 | } |
230 | } |
201 | |
231 | |
202 | void |
232 | void |
203 | rxvt_img::brightness (double r, double g, double b, double a) |
233 | rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
204 | { |
234 | { |
205 | Display *dpy = s->display->dpy; |
235 | Display *dpy = s->display->dpy; |
206 | Picture src = create_xrender_mask (dpy, pm, True); |
236 | Picture src = create_xrender_mask (dpy, pm, True); |
207 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
237 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
208 | |
238 | |
209 | XRenderColor mask_c; |
239 | XRenderColor mask_c; |
210 | mask_c.red = float_to_component (r); |
240 | mask_c.red = r; |
211 | mask_c.green = float_to_component (g); |
241 | mask_c.green = g; |
212 | mask_c.blue = float_to_component (b); |
242 | mask_c.blue = b; |
213 | mask_c.alpha = float_to_component (a); |
243 | mask_c.alpha = a; |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
244 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
215 | |
245 | |
216 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
246 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
217 | |
247 | |
218 | XRenderFreePicture (dpy, src); |
248 | XRenderFreePicture (dpy, src); |
219 | XRenderFreePicture (dpy, dst); |
249 | XRenderFreePicture (dpy, dst); |
220 | } |
250 | } |
221 | |
251 | |
222 | void |
252 | void |
223 | rxvt_img::contrast (double r, double g, double b, double a) |
253 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
224 | { |
254 | { |
225 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
255 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
226 | return; |
256 | return; |
227 | |
257 | |
228 | Display *dpy = s->display->dpy; |
258 | Display *dpy = s->display->dpy; |
229 | Picture src = create_xrender_mask (dpy, pm, True); |
259 | Picture src = create_xrender_mask (dpy, pm, True); |
230 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
260 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
231 | |
261 | |
232 | XRenderColor mask_c; |
262 | XRenderColor mask_c; |
233 | mask_c.red = float_to_component (r); |
263 | mask_c.red = r; |
234 | mask_c.green = float_to_component (g); |
264 | mask_c.green = g; |
235 | mask_c.blue = float_to_component (b); |
265 | mask_c.blue = b; |
236 | mask_c.alpha = float_to_component (a); |
266 | mask_c.alpha = a; |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
267 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
238 | |
268 | |
239 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
269 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
240 | |
270 | |
241 | XRenderFreePicture (dpy, src); |
271 | XRenderFreePicture (dpy, src); |
… | |
… | |
340 | } |
370 | } |
341 | |
371 | |
342 | rxvt_img * |
372 | rxvt_img * |
343 | rxvt_img::clone () |
373 | rxvt_img::clone () |
344 | { |
374 | { |
345 | rxvt_img *img = new rxvt_img (s, format, w, h); |
375 | return new rxvt_img (*this); |
346 | |
|
|
347 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
348 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
|
|
349 | XFreeGC (s->display->dpy, gc); |
|
|
350 | |
|
|
351 | return img; |
|
|
352 | } |
376 | } |
353 | |
377 | |
354 | rxvt_img * |
378 | rxvt_img * |
355 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
379 | rxvt_img::sub_rect (int x, int y, int width, int height) |
356 | { |
380 | { |
357 | rxvt_img *img = new rxvt_img (s, format, width, height); |
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
382 | img->alloc (); |
358 | |
383 | |
359 | Display *dpy = s->display->dpy; |
384 | Display *dpy = s->display->dpy; |
360 | XRenderPictureAttributes pa; |
385 | XRenderPictureAttributes pa; |
361 | pa.repeat = repeat; |
386 | pa.repeat = repeat; |
362 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
387 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
369 | |
394 | |
370 | return img; |
395 | return img; |
371 | } |
396 | } |
372 | |
397 | |
373 | rxvt_img * |
398 | rxvt_img * |
374 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
399 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
375 | { |
400 | { |
376 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
401 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
|
|
402 | img->alloc (); |
377 | |
403 | |
378 | Display *dpy = s->display->dpy; |
404 | Display *dpy = s->display->dpy; |
379 | XRenderPictureAttributes pa; |
405 | XRenderPictureAttributes pa; |
380 | pa.repeat = repeat; |
406 | pa.repeat = repeat; |
381 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
407 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
408 | |
434 | |
409 | return transform (new_width, new_height, matrix); |
435 | return transform (new_width, new_height, matrix); |
410 | } |
436 | } |
411 | |
437 | |
412 | rxvt_img * |
438 | rxvt_img * |
413 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
439 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
414 | { |
440 | { |
415 | double s = sin (phi); |
441 | double s = sin (phi); |
416 | double c = cos (phi); |
442 | double c = cos (phi); |
417 | |
443 | |
418 | double matrix[9] = { |
444 | double matrix[9] = { |
419 | c, -s, -c * x + s * y + x, |
445 | c, -s, -c * x + s * y + x, |
420 | s, c, -s * x - c * y + y, |
446 | s, c, -s * x - c * y + y, |
421 | 0, 0, 1 |
447 | 0, 0, 1 |
422 | }; |
448 | }; |
423 | |
449 | |
424 | return transform (new_width, new_height, matrix, repeat); |
450 | return transform (new_width, new_height, matrix); |
425 | } |
451 | } |
426 | |
452 | |
427 | rxvt_img * |
453 | rxvt_img * |
428 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
429 | { |
455 | { |
|
|
456 | if (new_format == format) |
|
|
457 | return clone (); |
|
|
458 | |
430 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
459 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
|
|
460 | img->alloc (); |
431 | |
461 | |
432 | Display *dpy = s->display->dpy; |
462 | Display *dpy = s->display->dpy; |
433 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
434 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
464 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
465 | int op = PictOpSrc; |
435 | |
466 | |
|
|
467 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
|
|
468 | { |
|
|
469 | // does it have to be that complicated |
|
|
470 | rgba c; |
|
|
471 | bg.get (c); |
|
|
472 | |
|
|
473 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
|
|
474 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
|
|
475 | |
|
|
476 | op = PictOpOver; |
|
|
477 | } |
|
|
478 | |
436 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
479 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
437 | |
480 | |
438 | XRenderFreePicture (dpy, src); |
481 | XRenderFreePicture (dpy, src); |
439 | XRenderFreePicture (dpy, dst); |
482 | XRenderFreePicture (dpy, dst); |
440 | |
483 | |
441 | return img; |
484 | return img; |