… | |
… | |
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 | |
|
|
80 | g_object_unref (pb); |
|
|
81 | |
|
|
82 | return img; |
|
|
83 | } |
|
|
84 | |
|
|
85 | void |
|
|
86 | rxvt_img::destroy () |
|
|
87 | { |
|
|
88 | if (!refcnt || --*refcnt) |
70 | return img; |
89 | return; |
|
|
90 | |
|
|
91 | if (pm) |
|
|
92 | XFreePixmap (s->display->dpy, pm); |
|
|
93 | |
|
|
94 | delete refcnt; |
71 | } |
95 | } |
72 | |
96 | |
73 | rxvt_img::~rxvt_img () |
97 | rxvt_img::~rxvt_img () |
74 | { |
98 | { |
75 | if (!shared) |
99 | destroy (); |
76 | 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); |
77 | } |
107 | } |
78 | |
108 | |
79 | void |
109 | void |
80 | rxvt_img::unshare () |
110 | rxvt_img::unshare () |
81 | { |
111 | { |
82 | if (!shared) |
112 | if (refcnt && *refcnt == 1) |
83 | return; |
113 | return; |
84 | |
114 | |
85 | 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); |
86 | |
119 | |
87 | ::swap (pm , img->pm); |
120 | destroy (); |
88 | ::swap (shared, img->shared); |
|
|
89 | |
121 | |
90 | delete img; |
122 | pm = pm2; |
|
|
123 | refcnt = new int (1); |
91 | } |
124 | } |
92 | |
125 | |
93 | void |
126 | void |
94 | rxvt_img::fill (const rxvt_color &c) |
127 | rxvt_img::fill (const rxvt_color &c) |
95 | { |
128 | { |
… | |
… | |
129 | |
162 | |
130 | Display *dpy = s->display->dpy; |
163 | Display *dpy = s->display->dpy; |
131 | int size = max (rh, rv) * 2 + 1; |
164 | int size = max (rh, rv) * 2 + 1; |
132 | double *kernel = (double *)malloc (size * sizeof (double)); |
165 | double *kernel = (double *)malloc (size * sizeof (double)); |
133 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
166 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
134 | 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 (); |
135 | |
169 | |
136 | XRenderPictureAttributes pa; |
170 | XRenderPictureAttributes pa; |
137 | |
171 | |
138 | pa.repeat = RepeatPad; |
172 | pa.repeat = RepeatPad; |
139 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
173 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
… | |
… | |
198 | |
232 | |
199 | return mask; |
233 | return mask; |
200 | } |
234 | } |
201 | |
235 | |
202 | void |
236 | void |
203 | 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) |
204 | { |
238 | { |
205 | Display *dpy = s->display->dpy; |
239 | Display *dpy = s->display->dpy; |
206 | Picture src = create_xrender_mask (dpy, pm, True); |
240 | Picture src = create_xrender_mask (dpy, pm, True); |
207 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
241 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
208 | |
242 | |
209 | XRenderColor mask_c; |
243 | XRenderColor mask_c; |
210 | mask_c.red = float_to_component (r); |
244 | mask_c.red = r; |
211 | mask_c.green = float_to_component (g); |
245 | mask_c.green = g; |
212 | mask_c.blue = float_to_component (b); |
246 | mask_c.blue = b; |
213 | mask_c.alpha = float_to_component (a); |
247 | mask_c.alpha = a; |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
248 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
215 | |
249 | |
216 | 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); |
217 | |
251 | |
218 | XRenderFreePicture (dpy, src); |
252 | XRenderFreePicture (dpy, src); |
219 | XRenderFreePicture (dpy, dst); |
253 | XRenderFreePicture (dpy, dst); |
220 | } |
254 | } |
221 | |
255 | |
222 | void |
256 | void |
223 | 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) |
224 | { |
258 | { |
225 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
259 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
226 | return; |
260 | return; |
227 | |
261 | |
228 | Display *dpy = s->display->dpy; |
262 | Display *dpy = s->display->dpy; |
229 | Picture src = create_xrender_mask (dpy, pm, True); |
263 | Picture src = create_xrender_mask (dpy, pm, True); |
230 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
264 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
231 | |
265 | |
232 | XRenderColor mask_c; |
266 | XRenderColor mask_c; |
233 | mask_c.red = float_to_component (r); |
267 | mask_c.red = r; |
234 | mask_c.green = float_to_component (g); |
268 | mask_c.green = g; |
235 | mask_c.blue = float_to_component (b); |
269 | mask_c.blue = b; |
236 | mask_c.alpha = float_to_component (a); |
270 | mask_c.alpha = a; |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
271 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
238 | |
272 | |
239 | 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); |
240 | |
274 | |
241 | XRenderFreePicture (dpy, src); |
275 | XRenderFreePicture (dpy, src); |
… | |
… | |
291 | |
325 | |
292 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
326 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
293 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
327 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
294 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
328 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
295 | char *line = ximage->data; |
329 | char *line = ximage->data; |
296 | |
|
|
297 | rgba c (0, 0, 0); |
|
|
298 | |
|
|
299 | if (channels == 4 && alpha_mask == 0) |
|
|
300 | { |
|
|
301 | //pix_colors[Color_bg].get (c); |
|
|
302 | //TODO |
|
|
303 | c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D |
|
|
304 | c.r >>= 8; |
|
|
305 | c.g >>= 8; |
|
|
306 | c.b >>= 8; |
|
|
307 | } |
|
|
308 | |
330 | |
309 | for (int y = 0; y < height; y++) |
331 | for (int y = 0; y < height; y++) |
310 | { |
332 | { |
311 | for (int x = 0; x < width; x++) |
333 | for (int x = 0; x < width; x++) |
312 | { |
334 | { |
… | |
… | |
315 | unsigned char r, g, b, a; |
337 | unsigned char r, g, b, a; |
316 | |
338 | |
317 | if (channels == 4) |
339 | if (channels == 4) |
318 | { |
340 | { |
319 | a = pixel[3]; |
341 | a = pixel[3]; |
320 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
342 | r = pixel[0] * a / 0xff; |
321 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
343 | g = pixel[1] * a / 0xff; |
322 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
344 | b = pixel[2] * a / 0xff; |
323 | } |
345 | } |
324 | else |
346 | else |
325 | { |
347 | { |
326 | a = 0xff; |
348 | a = 0xff; |
327 | r = pixel[0]; |
349 | r = pixel[0]; |
… | |
… | |
352 | } |
374 | } |
353 | |
375 | |
354 | rxvt_img * |
376 | rxvt_img * |
355 | rxvt_img::clone () |
377 | rxvt_img::clone () |
356 | { |
378 | { |
|
|
379 | return new rxvt_img (*this); |
|
|
380 | } |
|
|
381 | |
|
|
382 | rxvt_img * |
|
|
383 | rxvt_img::reify () |
|
|
384 | { |
357 | 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 (); |
358 | |
387 | |
359 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
388 | // todo, if x==0 and y==0 and w==real width we could clone |
360 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
389 | // but that involves an rtt to find pixmap width. |
361 | XFreeGC (s->display->dpy, gc); |
|
|
362 | |
|
|
363 | return img; |
|
|
364 | } |
|
|
365 | |
|
|
366 | rxvt_img * |
|
|
367 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
|
|
368 | { |
|
|
369 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
370 | |
390 | |
371 | Display *dpy = s->display->dpy; |
391 | Display *dpy = s->display->dpy; |
372 | XRenderPictureAttributes pa; |
392 | XRenderPictureAttributes pa; |
373 | pa.repeat = repeat; |
393 | pa.repeat = repeat; |
374 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
394 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
375 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
395 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
376 | |
396 | |
377 | 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); |
378 | |
398 | |
379 | XRenderFreePicture (dpy, src); |
|
|
380 | XRenderFreePicture (dpy, dst); |
399 | XRenderFreePicture (dpy, src); |
|
|
400 | XRenderFreePicture (dpy, dst); |
381 | |
401 | |
382 | return img; |
402 | return img; |
383 | } |
403 | } |
384 | |
404 | |
385 | 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 * |
386 | 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]) |
387 | { |
420 | { |
388 | 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 (); |
389 | |
423 | |
390 | Display *dpy = s->display->dpy; |
424 | Display *dpy = s->display->dpy; |
391 | XRenderPictureAttributes pa; |
425 | XRenderPictureAttributes pa; |
392 | pa.repeat = repeat; |
426 | pa.repeat = repeat; |
393 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
427 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
396 | XTransform xfrm; |
430 | XTransform xfrm; |
397 | |
431 | |
398 | for (int i = 0; i < 3; ++i) |
432 | for (int i = 0; i < 3; ++i) |
399 | for (int j = 0; j < 3; ++j) |
433 | for (int j = 0; j < 3; ++j) |
400 | 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); |
401 | |
438 | |
402 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
439 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
403 | XRenderSetPictureTransform (dpy, src, &xfrm); |
440 | XRenderSetPictureTransform (dpy, src, &xfrm); |
404 | 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); |
405 | |
442 | |
… | |
… | |
420 | |
457 | |
421 | return transform (new_width, new_height, matrix); |
458 | return transform (new_width, new_height, matrix); |
422 | } |
459 | } |
423 | |
460 | |
424 | rxvt_img * |
461 | rxvt_img * |
425 | 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) |
426 | { |
463 | { |
427 | double s = sin (phi); |
464 | double s = sin (phi); |
428 | double c = cos (phi); |
465 | double c = cos (phi); |
429 | |
466 | |
430 | double matrix[9] = { |
467 | double matrix[9] = { |
431 | c, -s, -c * x + s * y + x, |
468 | c, -s, -c * x + s * y + x, |
432 | s, c, -s * x - c * y + y, |
469 | s, c, -s * x - c * y + y, |
433 | 0, 0, 1 |
470 | 0, 0, 1 |
434 | }; |
471 | }; |
435 | |
472 | |
436 | return transform (new_width, new_height, matrix, repeat); |
473 | return transform (new_width, new_height, matrix); |
437 | } |
474 | } |
438 | |
475 | |
439 | rxvt_img * |
476 | rxvt_img * |
440 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
477 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
441 | { |
478 | { |
|
|
479 | if (new_format == format) |
|
|
480 | return clone (); |
|
|
481 | |
442 | 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 (); |
443 | |
484 | |
444 | Display *dpy = s->display->dpy; |
485 | Display *dpy = s->display->dpy; |
445 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
486 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
446 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
487 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
488 | int op = PictOpSrc; |
447 | |
489 | |
|
|
490 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
|
|
491 | { |
|
|
492 | // does it have to be that complicated |
|
|
493 | rgba c; |
|
|
494 | bg.get (c); |
|
|
495 | |
|
|
496 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
|
|
497 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
|
|
498 | |
|
|
499 | op = PictOpOver; |
|
|
500 | } |
|
|
501 | |
448 | XRenderComposite (dpy, PictOpSrc, 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); |
449 | |
503 | |
450 | XRenderFreePicture (dpy, src); |
504 | XRenderFreePicture (dpy, src); |
451 | XRenderFreePicture (dpy, dst); |
505 | XRenderFreePicture (dpy, dst); |
452 | |
506 | |
453 | return img; |
507 | return img; |