… | |
… | |
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); |
|
|
247 | |
|
|
248 | XRenderFreePicture (dpy, src); |
|
|
249 | XRenderFreePicture (dpy, dst); |
217 | } |
250 | } |
218 | |
251 | |
219 | void |
252 | void |
220 | 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) |
221 | { |
254 | { |
222 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
255 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
223 | return; |
256 | return; |
224 | |
257 | |
225 | Display *dpy = s->display->dpy; |
258 | Display *dpy = s->display->dpy; |
226 | Picture src = create_xrender_mask (dpy, pm, True); |
259 | Picture src = create_xrender_mask (dpy, pm, True); |
227 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
260 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
228 | |
261 | |
229 | XRenderColor mask_c; |
262 | XRenderColor mask_c; |
230 | mask_c.red = float_to_component (r); |
263 | mask_c.red = r; |
231 | mask_c.green = float_to_component (g); |
264 | mask_c.green = g; |
232 | mask_c.blue = float_to_component (b); |
265 | mask_c.blue = b; |
233 | mask_c.alpha = float_to_component (a); |
266 | mask_c.alpha = a; |
234 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
267 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
235 | |
268 | |
236 | 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); |
|
|
270 | |
|
|
271 | XRenderFreePicture (dpy, src); |
|
|
272 | XRenderFreePicture (dpy, dst); |
237 | } |
273 | } |
238 | |
274 | |
239 | bool |
275 | bool |
240 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
276 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
241 | { |
277 | { |
… | |
… | |
285 | |
321 | |
286 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
322 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
287 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
323 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
288 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
324 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
289 | char *line = ximage->data; |
325 | char *line = ximage->data; |
290 | |
|
|
291 | rgba c (0, 0, 0); |
|
|
292 | |
|
|
293 | if (channels == 4 && alpha_mask == 0) |
|
|
294 | { |
|
|
295 | //pix_colors[Color_bg].get (c); |
|
|
296 | //TODO |
|
|
297 | c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D |
|
|
298 | c.r >>= 8; |
|
|
299 | c.g >>= 8; |
|
|
300 | c.b >>= 8; |
|
|
301 | } |
|
|
302 | |
326 | |
303 | for (int y = 0; y < height; y++) |
327 | for (int y = 0; y < height; y++) |
304 | { |
328 | { |
305 | for (int x = 0; x < width; x++) |
329 | for (int x = 0; x < width; x++) |
306 | { |
330 | { |
… | |
… | |
309 | unsigned char r, g, b, a; |
333 | unsigned char r, g, b, a; |
310 | |
334 | |
311 | if (channels == 4) |
335 | if (channels == 4) |
312 | { |
336 | { |
313 | a = pixel[3]; |
337 | a = pixel[3]; |
314 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
338 | r = pixel[0] * a / 0xff; |
315 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
339 | g = pixel[1] * a / 0xff; |
316 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
340 | b = pixel[2] * a / 0xff; |
317 | } |
341 | } |
318 | else |
342 | else |
319 | { |
343 | { |
320 | a = 0xff; |
344 | a = 0xff; |
321 | r = pixel[0]; |
345 | r = pixel[0]; |
… | |
… | |
346 | } |
370 | } |
347 | |
371 | |
348 | rxvt_img * |
372 | rxvt_img * |
349 | rxvt_img::clone () |
373 | rxvt_img::clone () |
350 | { |
374 | { |
351 | rxvt_img *img = new rxvt_img (s, format, w, h); |
375 | return new rxvt_img (*this); |
352 | |
|
|
353 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
354 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
|
|
355 | XFreeGC (s->display->dpy, gc); |
|
|
356 | |
|
|
357 | return img; |
|
|
358 | } |
376 | } |
359 | |
377 | |
360 | rxvt_img * |
378 | rxvt_img * |
361 | 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) |
362 | { |
380 | { |
363 | rxvt_img *img = new rxvt_img (s, format, width, height); |
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
382 | img->alloc (); |
364 | |
383 | |
365 | Display *dpy = s->display->dpy; |
384 | Display *dpy = s->display->dpy; |
366 | XRenderPictureAttributes pa; |
385 | XRenderPictureAttributes pa; |
367 | pa.repeat = repeat; |
386 | pa.repeat = repeat; |
368 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
387 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
375 | |
394 | |
376 | return img; |
395 | return img; |
377 | } |
396 | } |
378 | |
397 | |
379 | rxvt_img * |
398 | rxvt_img * |
380 | 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]) |
381 | { |
400 | { |
382 | 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 (); |
383 | |
403 | |
384 | Display *dpy = s->display->dpy; |
404 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
405 | XRenderPictureAttributes pa; |
386 | pa.repeat = repeat; |
406 | pa.repeat = repeat; |
387 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
407 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
414 | |
434 | |
415 | return transform (new_width, new_height, matrix); |
435 | return transform (new_width, new_height, matrix); |
416 | } |
436 | } |
417 | |
437 | |
418 | rxvt_img * |
438 | rxvt_img * |
419 | 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) |
420 | { |
440 | { |
421 | double s = sin (phi); |
441 | double s = sin (phi); |
422 | double c = cos (phi); |
442 | double c = cos (phi); |
423 | |
443 | |
424 | double matrix[9] = { |
444 | double matrix[9] = { |
425 | c, -s, -c * x + s * y + x, |
445 | c, -s, -c * x + s * y + x, |
426 | s, c, -s * x - c * y + y, |
446 | s, c, -s * x - c * y + y, |
427 | 0, 0, 1 |
447 | 0, 0, 1 |
428 | }; |
448 | }; |
429 | |
449 | |
430 | return transform (new_width, new_height, matrix, repeat); |
450 | return transform (new_width, new_height, matrix); |
431 | } |
451 | } |
432 | |
452 | |
433 | rxvt_img * |
453 | rxvt_img * |
434 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
435 | { |
455 | { |
|
|
456 | if (new_format == format) |
|
|
457 | return clone (); |
|
|
458 | |
436 | 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 (); |
437 | |
461 | |
438 | Display *dpy = s->display->dpy; |
462 | Display *dpy = s->display->dpy; |
439 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
440 | 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; |
441 | |
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 | |
442 | 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); |
443 | |
480 | |
444 | XRenderFreePicture (dpy, src); |
481 | XRenderFreePicture (dpy, src); |
445 | XRenderFreePicture (dpy, dst); |
482 | XRenderFreePicture (dpy, dst); |
446 | |
483 | |
447 | return img; |
484 | return img; |
448 | } |
485 | } |
449 | |
486 | |
|
|
487 | rxvt_img * |
|
|
488 | rxvt_img::blend (rxvt_img *img, double factor) |
|
|
489 | { |
|
|
490 | rxvt_img *img2 = clone (); |
|
|
491 | Display *dpy = s->display->dpy; |
|
|
492 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
493 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
|
|
494 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
|
|
495 | |
|
|
496 | XRenderColor mask_c; |
|
|
497 | |
|
|
498 | mask_c.alpha = float_to_component (factor); |
|
|
499 | mask_c.red = |
|
|
500 | mask_c.green = |
|
|
501 | mask_c.blue = 0; |
|
|
502 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
503 | |
|
|
504 | XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
505 | |
|
|
506 | XRenderFreePicture (dpy, src); |
|
|
507 | XRenderFreePicture (dpy, dst); |
|
|
508 | XRenderFreePicture (dpy, mask); |
|
|
509 | |
|
|
510 | return img2; |
|
|
511 | } |
|
|
512 | |
450 | #endif |
513 | #endif |
451 | |
514 | |