… | |
… | |
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 x, int y, int width, int height) |
10 | : s(screen), w(width), h(height), format(format), shared(false) |
8 | : s(screen), x(x), y(y), 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; |
… | |
… | |
37 | return 0; |
44 | return 0; |
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)), |
|
|
49 | 0, |
|
|
50 | 0, |
42 | root_pm_w, |
51 | root_pm_w, |
43 | root_pm_h, |
52 | root_pm_h |
44 | root_pixmap |
|
|
45 | ); |
53 | ); |
46 | |
54 | |
47 | img->shared = true; |
55 | img->pm = root_pixmap; |
48 | |
56 | |
49 | return img; |
57 | return img; |
50 | } |
58 | } |
51 | |
59 | |
52 | rxvt_img * |
60 | rxvt_img * |
… | |
… | |
59 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
67 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
60 | |
68 | |
61 | rxvt_img *img = new rxvt_img ( |
69 | rxvt_img *img = new rxvt_img ( |
62 | s, |
70 | s, |
63 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
71 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
|
|
72 | 0, |
|
|
73 | 0, |
64 | gdk_pixbuf_get_width (pb), |
74 | gdk_pixbuf_get_width (pb), |
65 | gdk_pixbuf_get_height (pb) |
75 | gdk_pixbuf_get_height (pb) |
66 | ); |
76 | ); |
67 | |
77 | img->alloc (); |
68 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
78 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
69 | |
79 | |
70 | g_object_unref (pb); |
80 | g_object_unref (pb); |
71 | |
81 | |
72 | return img; |
82 | return img; |
73 | } |
83 | } |
74 | |
84 | |
|
|
85 | void |
|
|
86 | rxvt_img::destroy () |
|
|
87 | { |
|
|
88 | if (!refcnt || --*refcnt) |
|
|
89 | return; |
|
|
90 | |
|
|
91 | if (pm) |
|
|
92 | XFreePixmap (s->display->dpy, pm); |
|
|
93 | |
|
|
94 | delete refcnt; |
|
|
95 | } |
|
|
96 | |
75 | rxvt_img::~rxvt_img () |
97 | rxvt_img::~rxvt_img () |
76 | { |
98 | { |
77 | if (!shared) |
99 | destroy (); |
78 | XFreePixmap (s->display->dpy, pm); |
100 | } |
|
|
101 | |
|
|
102 | void |
|
|
103 | rxvt_img::alloc () |
|
|
104 | { |
|
|
105 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
106 | refcnt = new int (1); |
79 | } |
107 | } |
80 | |
108 | |
81 | void |
109 | void |
82 | rxvt_img::unshare () |
110 | rxvt_img::unshare () |
83 | { |
111 | { |
84 | if (!shared) |
112 | if (refcnt && *refcnt == 1) |
85 | return; |
113 | return; |
86 | |
114 | |
87 | rxvt_img *img = clone (); |
115 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
116 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
117 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
|
|
118 | XFreeGC (s->display->dpy, gc); |
88 | |
119 | |
89 | ::swap (pm , img->pm); |
120 | destroy (); |
90 | ::swap (shared, img->shared); |
|
|
91 | |
121 | |
92 | delete img; |
122 | pm = pm2; |
|
|
123 | refcnt = new int (1); |
93 | } |
124 | } |
94 | |
125 | |
95 | void |
126 | void |
96 | rxvt_img::fill (const rxvt_color &c) |
127 | rxvt_img::fill (const rxvt_color &c) |
97 | { |
128 | { |
… | |
… | |
131 | |
162 | |
132 | Display *dpy = s->display->dpy; |
163 | Display *dpy = s->display->dpy; |
133 | int size = max (rh, rv) * 2 + 1; |
164 | int size = max (rh, rv) * 2 + 1; |
134 | double *kernel = (double *)malloc (size * sizeof (double)); |
165 | double *kernel = (double *)malloc (size * sizeof (double)); |
135 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
166 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
136 | rxvt_img *img = new rxvt_img (s, format, w, h); |
167 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h); |
|
|
168 | img->alloc (); |
137 | |
169 | |
138 | XRenderPictureAttributes pa; |
170 | XRenderPictureAttributes pa; |
139 | |
171 | |
140 | pa.repeat = RepeatPad; |
172 | pa.repeat = RepeatPad; |
141 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
173 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
… | |
… | |
200 | |
232 | |
201 | return mask; |
233 | return mask; |
202 | } |
234 | } |
203 | |
235 | |
204 | void |
236 | void |
205 | rxvt_img::brightness (double r, double g, double b, double a) |
237 | rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
206 | { |
238 | { |
207 | Display *dpy = s->display->dpy; |
239 | Display *dpy = s->display->dpy; |
208 | Picture src = create_xrender_mask (dpy, pm, True); |
240 | Picture src = create_xrender_mask (dpy, pm, True); |
209 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
241 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
210 | |
242 | |
211 | XRenderColor mask_c; |
243 | XRenderColor mask_c; |
212 | mask_c.red = float_to_component (r); |
244 | mask_c.red = r; |
213 | mask_c.green = float_to_component (g); |
245 | mask_c.green = g; |
214 | mask_c.blue = float_to_component (b); |
246 | mask_c.blue = b; |
215 | mask_c.alpha = float_to_component (a); |
247 | mask_c.alpha = a; |
216 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
248 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
217 | |
249 | |
218 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
250 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
219 | |
251 | |
220 | XRenderFreePicture (dpy, src); |
252 | XRenderFreePicture (dpy, src); |
221 | XRenderFreePicture (dpy, dst); |
253 | XRenderFreePicture (dpy, dst); |
222 | } |
254 | } |
223 | |
255 | |
224 | void |
256 | void |
225 | rxvt_img::contrast (double r, double g, double b, double a) |
257 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
226 | { |
258 | { |
227 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
259 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
228 | return; |
260 | return; |
229 | |
261 | |
230 | Display *dpy = s->display->dpy; |
262 | Display *dpy = s->display->dpy; |
231 | Picture src = create_xrender_mask (dpy, pm, True); |
263 | Picture src = create_xrender_mask (dpy, pm, True); |
232 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
264 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
233 | |
265 | |
234 | XRenderColor mask_c; |
266 | XRenderColor mask_c; |
235 | mask_c.red = float_to_component (r); |
267 | mask_c.red = r; |
236 | mask_c.green = float_to_component (g); |
268 | mask_c.green = g; |
237 | mask_c.blue = float_to_component (b); |
269 | mask_c.blue = b; |
238 | mask_c.alpha = float_to_component (a); |
270 | mask_c.alpha = a; |
239 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
271 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
240 | |
272 | |
241 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
273 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
242 | |
274 | |
243 | XRenderFreePicture (dpy, src); |
275 | XRenderFreePicture (dpy, src); |
… | |
… | |
342 | } |
374 | } |
343 | |
375 | |
344 | rxvt_img * |
376 | rxvt_img * |
345 | rxvt_img::clone () |
377 | rxvt_img::clone () |
346 | { |
378 | { |
|
|
379 | return new rxvt_img (*this); |
|
|
380 | } |
|
|
381 | |
|
|
382 | rxvt_img * |
|
|
383 | rxvt_img::reify () |
|
|
384 | { |
347 | rxvt_img *img = new rxvt_img (s, format, w, h); |
385 | rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h); |
|
|
386 | img->alloc (); |
348 | |
387 | |
349 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
388 | // todo, if x==0 and y==0 and w==real width we could clone |
350 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
389 | // but that involves an rtt to find pixmap width. |
351 | XFreeGC (s->display->dpy, gc); |
|
|
352 | |
|
|
353 | return img; |
|
|
354 | } |
|
|
355 | |
|
|
356 | rxvt_img * |
|
|
357 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
|
|
358 | { |
|
|
359 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
360 | |
390 | |
361 | Display *dpy = s->display->dpy; |
391 | Display *dpy = s->display->dpy; |
362 | XRenderPictureAttributes pa; |
392 | XRenderPictureAttributes pa; |
363 | pa.repeat = repeat; |
393 | pa.repeat = repeat; |
364 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
394 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
365 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
395 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
366 | |
396 | |
367 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
397 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
368 | |
398 | |
369 | XRenderFreePicture (dpy, src); |
|
|
370 | XRenderFreePicture (dpy, dst); |
399 | XRenderFreePicture (dpy, src); |
|
|
400 | XRenderFreePicture (dpy, dst); |
371 | |
401 | |
372 | return img; |
402 | return img; |
373 | } |
403 | } |
374 | |
404 | |
375 | rxvt_img * |
405 | rxvt_img * |
|
|
406 | rxvt_img::sub_rect (int x, int y, int width, int height) |
|
|
407 | { |
|
|
408 | rxvt_img *img = clone (); |
|
|
409 | |
|
|
410 | img->x += x; |
|
|
411 | img->y += y; |
|
|
412 | img->w = width; |
|
|
413 | img->h = height; |
|
|
414 | |
|
|
415 | return img; |
|
|
416 | } |
|
|
417 | |
|
|
418 | rxvt_img * |
376 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
419 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
377 | { |
420 | { |
378 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
421 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height); |
|
|
422 | img->alloc (); |
379 | |
423 | |
380 | Display *dpy = s->display->dpy; |
424 | Display *dpy = s->display->dpy; |
381 | XRenderPictureAttributes pa; |
425 | XRenderPictureAttributes pa; |
382 | pa.repeat = repeat; |
426 | pa.repeat = repeat; |
383 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
427 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
386 | XTransform xfrm; |
430 | XTransform xfrm; |
387 | |
431 | |
388 | for (int i = 0; i < 3; ++i) |
432 | for (int i = 0; i < 3; ++i) |
389 | for (int j = 0; j < 3; ++j) |
433 | for (int j = 0; j < 3; ++j) |
390 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
434 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
|
|
435 | |
|
|
436 | xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO |
|
|
437 | xfrm.matrix [0][3] += XDoubleToFixed (y); |
391 | |
438 | |
392 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
439 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
393 | XRenderSetPictureTransform (dpy, src, &xfrm); |
440 | XRenderSetPictureTransform (dpy, src, &xfrm); |
394 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
441 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
395 | |
442 | |
… | |
… | |
410 | |
457 | |
411 | return transform (new_width, new_height, matrix); |
458 | return transform (new_width, new_height, matrix); |
412 | } |
459 | } |
413 | |
460 | |
414 | rxvt_img * |
461 | rxvt_img * |
415 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
462 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
416 | { |
463 | { |
417 | double s = sin (phi); |
464 | double s = sin (phi); |
418 | double c = cos (phi); |
465 | double c = cos (phi); |
419 | |
466 | |
420 | double matrix[9] = { |
467 | double matrix[9] = { |
421 | c, -s, -c * x + s * y + x, |
468 | c, -s, -c * x + s * y + x, |
422 | s, c, -s * x - c * y + y, |
469 | s, c, -s * x - c * y + y, |
423 | 0, 0, 1 |
470 | 0, 0, 1 |
424 | }; |
471 | }; |
425 | |
472 | |
426 | return transform (new_width, new_height, matrix, repeat); |
473 | return transform (new_width, new_height, matrix); |
427 | } |
474 | } |
428 | |
475 | |
429 | rxvt_img * |
476 | rxvt_img * |
430 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
477 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
431 | { |
478 | { |
|
|
479 | if (new_format == format) |
|
|
480 | return clone (); |
|
|
481 | |
432 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
482 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h); |
|
|
483 | img->alloc (); |
433 | |
484 | |
434 | Display *dpy = s->display->dpy; |
485 | Display *dpy = s->display->dpy; |
435 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
486 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
436 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
487 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
437 | int op = PictOpSrc; |
488 | int op = PictOpSrc; |
438 | |
489 | |
439 | printf ("fa %x,%x fb %x,%x\n", f->direct.alpha, f->direct.alpha_mask, new_format->direct.alpha, new_format->direct.alpha_mask);//D |
|
|
440 | |
|
|
441 | if (f->direct.alpha && !new_format->direct.alpha) |
490 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
442 | { |
491 | { |
443 | // does it have to be that complicated |
492 | // does it have to be that complicated |
444 | rgba c; |
493 | rgba c; |
445 | bg.get (c); |
494 | bg.get (c); |
446 | |
495 | |
447 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
496 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
448 | |
|
|
449 | XRenderFillRectangle (dpy, PictOpSrc, dst, &mask_c, 0, 0, w, h); |
497 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
450 | |
498 | |
451 | op = PictOpOver; |
499 | op = PictOpOver; |
452 | } |
500 | } |
453 | |
501 | |
454 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
502 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |