ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
Revision: 1.235
Committed: Thu Jun 7 09:34:51 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.234: +3 -0 lines
Log Message:
Add a flag to inhibit bg image management in core.

File Contents

# Content
1 /*----------------------------------------------------------------------*
2 * File: background.C - former xpm.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de>
7 * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net>
8 * Copyright (c) 2010-2012 Emanuele Giaquinta <e.giaquinta@glauco.it>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *---------------------------------------------------------------------*/
24
25 #include <math.h>
26 #include "../config.h" /* NECESSARY */
27 #include "rxvt.h" /* NECESSARY */
28
29 #if XRENDER
30 # include <X11/extensions/Xrender.h>
31 #endif
32
33 #ifndef FilterConvolution
34 #define FilterConvolution "convolution"
35 #endif
36
37 #ifndef RepeatPad
38 #define RepeatPad True
39 #endif
40
41 #ifdef HAVE_BG_PIXMAP
42 # if XRENDER
43 static Picture
44 create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
45 {
46 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
47
48 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
49 XRenderPictureAttributes pa;
50 pa.repeat = True;
51 pa.component_alpha = component_alpha;
52 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
53
54 XFreePixmap (dpy, pixmap);
55
56 return mask;
57 }
58 # endif
59
60 void
61 rxvt_term::bg_destroy ()
62 {
63 # if BG_IMAGE_FROM_FILE
64 fimage.destroy ();
65 # endif
66
67 if (bg_pixmap)
68 XFreePixmap (dpy, bg_pixmap);
69 }
70
71 bool
72 rxvt_term::bg_set_position (int x, int y)
73 {
74
75 if (target_x != x
76 || target_y != y)
77 {
78 target_x = x;
79 target_y = y;
80 return true;
81 }
82 return false;
83 }
84
85 bool
86 rxvt_term::bg_window_size_sensitive ()
87 {
88 # if ENABLE_TRANSPARENCY
89 if (bg_flags & BG_IS_TRANSPARENT)
90 return true;
91 # endif
92
93 # if BG_IMAGE_FROM_FILE
94 if (fimage.flags & IM_IS_SET)
95 {
96 if ((fimage.flags & IM_IS_SIZE_SENSITIVE)
97 || fimage.width () > szHint.width
98 || fimage.height () > szHint.height)
99 return true;
100 }
101 # endif
102
103 return false;
104 }
105
106 bool
107 rxvt_term::bg_window_position_sensitive ()
108 {
109 # if ENABLE_TRANSPARENCY
110 if (bg_flags & BG_IS_TRANSPARENT)
111 return true;
112 # endif
113
114 # if BG_IMAGE_FROM_FILE
115 if (fimage.flags & IM_IS_SET)
116 {
117 if (fimage.flags & IM_ROOT_ALIGN)
118 return true;
119 }
120 # endif
121
122 return false;
123 }
124
125 # if BG_IMAGE_FROM_FILE
126 static inline int
127 make_align_position (int align, int window_size, int image_size)
128 {
129 if (align >= 0 && align <= 100)
130 return lerp (0, window_size - image_size, align);
131 else if (align > 100)
132 return lerp (window_size - image_size, window_size, align - 100);
133 else
134 return lerp (-image_size, 0, align + 100);
135 }
136
137 static inline int
138 make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
139 {
140 int src_pos = 0;
141 dst_pos = pos;
142 dst_size = size;
143 if (pos < 0)
144 {
145 src_pos = -pos;
146 dst_pos = 0;
147 dst_size += pos;
148 }
149
150 min_it (dst_size, target_size - dst_pos);
151 return src_pos;
152 }
153
154 static void
155 parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags)
156 {
157 if (!strcasecmp (style, "tiled"))
158 {
159 flags = IM_TILE;
160 w = h = noScale;
161 x = y = 0;
162 }
163 else if (!strcasecmp (style, "aspect-stretched"))
164 {
165 flags = IM_KEEP_ASPECT;
166 w = h = windowScale;
167 x = y = centerAlign;
168 }
169 else if (!strcasecmp (style, "stretched"))
170 {
171 flags = 0;
172 w = h = windowScale;
173 x = y = centerAlign;
174 }
175 else if (!strcasecmp (style, "centered"))
176 {
177 flags = 0;
178 w = h = noScale;
179 x = y = centerAlign;
180 }
181 else if (!strcasecmp (style, "root-tiled"))
182 {
183 flags = IM_TILE|IM_ROOT_ALIGN;
184 w = h = noScale;
185 x = y = 0;
186 }
187 }
188
189 bool
190 rxvt_image::set_geometry (const char *geom, bool update)
191 {
192 bool changed = false;
193 int geom_flags = 0;
194 int x = h_align;
195 int y = v_align;
196 unsigned int w = h_scale;
197 unsigned int h = v_scale;
198 uint8_t new_flags = 0;
199
200 if (geom == NULL)
201 return false;
202
203 if (geom[0])
204 {
205 char **arr = rxvt_strsplit (':', geom);
206
207 for (int i = 0; arr[i]; i++)
208 {
209 if (!strncasecmp (arr[i], "style=", 6))
210 {
211 parse_style (arr[i] + 6, x, y, w, h, new_flags);
212 geom_flags = WidthValue|HeightValue|XValue|YValue;
213 }
214 else if (!strcasecmp (arr[i], "op=tile"))
215 new_flags |= IM_TILE;
216 else if (!strcasecmp (arr[i], "op=keep-aspect"))
217 new_flags |= IM_KEEP_ASPECT;
218 else if (!strcasecmp (arr[i], "op=root-align"))
219 new_flags |= IM_ROOT_ALIGN;
220
221 // deprecated
222 else if (!strcasecmp (arr[i], "tile"))
223 {
224 new_flags |= IM_TILE;
225 w = h = noScale;
226 geom_flags |= WidthValue|HeightValue;
227 }
228 else if (!strcasecmp (arr[i], "propscale"))
229 {
230 new_flags |= IM_KEEP_ASPECT;
231 w = h = windowScale;
232 geom_flags |= WidthValue|HeightValue;
233 }
234 else if (!strcasecmp (arr[i], "hscale"))
235 {
236 new_flags |= IM_TILE;
237 w = windowScale;
238 h = noScale;
239 geom_flags |= WidthValue|HeightValue;
240 }
241 else if (!strcasecmp (arr[i], "vscale"))
242 {
243 new_flags |= IM_TILE;
244 h = windowScale;
245 w = noScale;
246 geom_flags |= WidthValue|HeightValue;
247 }
248 else if (!strcasecmp (arr[i], "scale"))
249 {
250 w = h = windowScale;
251 geom_flags |= WidthValue|HeightValue;
252 }
253 else if (!strcasecmp (arr[i], "auto"))
254 {
255 w = h = windowScale;
256 x = y = centerAlign;
257 geom_flags |= WidthValue|HeightValue|XValue|YValue;
258 }
259 else if (!strcasecmp (arr[i], "root"))
260 {
261 new_flags |= IM_TILE|IM_ROOT_ALIGN;
262 w = h = noScale;
263 geom_flags |= WidthValue|HeightValue;
264 }
265
266 else
267 geom_flags |= XParseGeometry (arr[i], &x, &y, &w, &h);
268 } /* done parsing ops */
269
270 rxvt_free_strsplit (arr);
271 }
272
273 new_flags |= flags & ~IM_GEOMETRY_FLAGS;
274
275 if (!update)
276 {
277 if (!(geom_flags & XValue))
278 x = y = defaultAlign;
279 else if (!(geom_flags & YValue))
280 y = x;
281
282 if (!(geom_flags & (WidthValue|HeightValue)))
283 w = h = defaultScale;
284 else if (!(geom_flags & HeightValue))
285 h = w;
286 else if (!(geom_flags & WidthValue))
287 w = h;
288 }
289
290 clamp_it (x, -100, 200);
291 clamp_it (y, -100, 200);
292
293 if (flags != new_flags
294 || h_scale != w
295 || v_scale != h
296 || h_align != x
297 || v_align != y)
298 {
299 flags = new_flags;
300 h_scale = w;
301 v_scale = h;
302 h_align = x;
303 v_align = y;
304 changed = true;
305 }
306
307 if (is_size_sensitive ())
308 flags |= IM_IS_SIZE_SENSITIVE;
309 else
310 flags &= ~IM_IS_SIZE_SENSITIVE;
311
312 return changed;
313 }
314
315 void
316 rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
317 {
318 int image_width = image.width ();
319 int image_height = image.height ();
320 int target_width = szHint.width;
321 int target_height = szHint.height;
322 int h_scale = min (image.h_scale, 32767 * 100 / target_width);
323 int v_scale = min (image.v_scale, 32767 * 100 / target_height);
324
325 w = h_scale * target_width / 100;
326 h = v_scale * target_height / 100;
327
328 if (image.flags & IM_KEEP_ASPECT)
329 {
330 float scale = (float)w / image_width;
331 min_it (scale, (float)h / image_height);
332 w = image_width * scale + 0.5;
333 h = image_height * scale + 0.5;
334 }
335
336 if (!w) w = image_width;
337 if (!h) h = image_height;
338
339 if (image.flags & IM_ROOT_ALIGN)
340 {
341 x = -target_x;
342 y = -target_y;
343 }
344 else
345 {
346 x = make_align_position (image.h_align, target_width, w);
347 y = make_align_position (image.v_align, target_height, h);
348 }
349 }
350
351 # if HAVE_PIXBUF
352 bool
353 rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
354 int src_x, int src_y, int dst_x, int dst_y,
355 unsigned int width, unsigned int height, bool argb)
356 {
357 XImage *ximage;
358 char *line;
359 int width_r, width_g, width_b, width_a;
360 int sh_r, sh_g, sh_b, sh_a;
361 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
362 int rowstride;
363 int channels;
364 unsigned char *row;
365
366 if (visual->c_class != TrueColor)
367 return false;
368
369 if (argb)
370 {
371 red_mask = 0xff << 16;
372 green_mask = 0xff << 8;
373 blue_mask = 0xff;
374 alpha_mask = 0xff << 24;
375 }
376 else
377 {
378 red_mask = visual->red_mask;
379 green_mask = visual->green_mask;
380 blue_mask = visual->blue_mask;
381 #if XRENDER
382 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
383 if (format)
384 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
385 else
386 #endif
387 alpha_mask = 0;
388 }
389
390 width_r = ecb_popcount32 (red_mask);
391 width_g = ecb_popcount32 (green_mask);
392 width_b = ecb_popcount32 (blue_mask);
393 width_a = ecb_popcount32 (alpha_mask);
394
395 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
396 return false;
397
398 sh_r = ecb_ctz32 (red_mask);
399 sh_g = ecb_ctz32 (green_mask);
400 sh_b = ecb_ctz32 (blue_mask);
401 sh_a = ecb_ctz32 (alpha_mask);
402
403 if (width > 32767 || height > 32767)
404 return false;
405
406 ximage = XCreateImage (dpy, visual, argb ? 32 : depth, ZPixmap, 0, 0,
407 width, height, 32, 0);
408 if (!ximage)
409 return false;
410
411 if (height > INT_MAX / ximage->bytes_per_line
412 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
413 {
414 XDestroyImage (ximage);
415 return false;
416 }
417
418 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
419
420 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
421 channels = gdk_pixbuf_get_n_channels (pixbuf);
422 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
423 line = ximage->data;
424
425 rgba c (0, 0, 0);
426
427 if (channels == 4 && alpha_mask == 0)
428 {
429 pix_colors[Color_bg].get (c);
430 c.r >>= 8;
431 c.g >>= 8;
432 c.b >>= 8;
433 }
434
435 for (int y = 0; y < height; y++)
436 {
437 for (int x = 0; x < width; x++)
438 {
439 unsigned char *pixel = row + x * channels;
440 uint32_t value;
441 unsigned char r, g, b, a;
442
443 if (channels == 4)
444 {
445 a = pixel[3];
446 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
447 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
448 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
449 }
450 else
451 {
452 a = 0xff;
453 r = pixel[0];
454 g = pixel[1];
455 b = pixel[2];
456 }
457
458 value = ((r >> (8 - width_r)) << sh_r)
459 | ((g >> (8 - width_g)) << sh_g)
460 | ((b >> (8 - width_b)) << sh_b)
461 | ((a >> (8 - width_a)) << sh_a);
462
463 if (ximage->bits_per_pixel == 32)
464 ((uint32_t *)line)[x] = value;
465 else
466 XPutPixel (ximage, x, y, value);
467 }
468
469 row += rowstride;
470 line += ximage->bytes_per_line;
471 }
472
473 XPutImage (dpy, pixmap, gc, ximage, 0, 0, dst_x, dst_y, width, height);
474 XDestroyImage (ximage);
475 return true;
476 }
477
478 bool
479 rxvt_term::render_image (rxvt_image &image)
480 {
481 GdkPixbuf *pixbuf = image.pixbuf;
482 if (!pixbuf)
483 return false;
484
485 bool need_blend = bg_flags & BG_IS_VALID;
486
487 if (need_blend
488 && !(display->flags & DISPLAY_HAS_RENDER))
489 return false;
490
491 GdkPixbuf *result;
492
493 int image_width = gdk_pixbuf_get_width (pixbuf);
494 int image_height = gdk_pixbuf_get_height (pixbuf);
495
496 int target_width = szHint.width;
497 int target_height = szHint.height;
498 int new_pmap_width = target_width;
499 int new_pmap_height = target_height;
500
501 int x = 0;
502 int y = 0;
503 int w = 0;
504 int h = 0;
505
506 get_image_geometry (image, w, h, x, y);
507
508 if (!(image.flags & IM_ROOT_ALIGN)
509 && (x >= target_width
510 || y >= target_height
511 || x + w <= 0
512 || y + h <= 0))
513 return false;
514
515 result = pixbuf;
516
517 if (w != image_width
518 || h != image_height)
519 {
520 result = gdk_pixbuf_scale_simple (pixbuf,
521 w, h,
522 GDK_INTERP_BILINEAR);
523 }
524
525 if (!result)
526 return false;
527
528 bool ret = false;
529
530 XGCValues gcv;
531 GC gc;
532 Pixmap tmp_pixmap;
533
534 image_width = gdk_pixbuf_get_width (result);
535 image_height = gdk_pixbuf_get_height (result);
536
537 if (need_blend)
538 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
539 else
540 {
541 if (image.flags & IM_TILE)
542 {
543 new_pmap_width = min (image_width, target_width);
544 new_pmap_height = min (image_height, target_height);
545 }
546
547 if (bg_pixmap == None
548 || bg_pmap_width != new_pmap_width
549 || bg_pmap_height != new_pmap_height)
550 {
551 if (bg_pixmap)
552 XFreePixmap (dpy, bg_pixmap);
553 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth);
554 bg_pmap_width = new_pmap_width;
555 bg_pmap_height = new_pmap_height;
556 }
557
558 tmp_pixmap = bg_pixmap;
559 }
560
561 gcv.foreground = pix_colors[Color_bg];
562 gc = XCreateGC (dpy, tmp_pixmap, GCForeground, &gcv);
563
564 if (gc)
565 {
566 if (image.flags & IM_TILE)
567 {
568 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
569 pixbuf_to_pixmap (result, tile, gc,
570 0, 0,
571 0, 0,
572 image_width, image_height, need_blend);
573
574 gcv.tile = tile;
575 gcv.fill_style = FillTiled;
576 gcv.ts_x_origin = x;
577 gcv.ts_y_origin = y;
578 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
579
580 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
581 XFreePixmap (dpy, tile);
582 }
583 else
584 {
585 int src_x, src_y, dst_x, dst_y;
586 int dst_width, dst_height;
587
588 src_x = make_clip_rectangle (x, image_width , new_pmap_width , dst_x, dst_width );
589 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height);
590
591 if (dst_x > 0 || dst_y > 0
592 || dst_x + dst_width < new_pmap_width
593 || dst_y + dst_height < new_pmap_height)
594 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
595
596 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
597 pixbuf_to_pixmap (result, tmp_pixmap, gc,
598 src_x, src_y,
599 dst_x, dst_y,
600 dst_width, dst_height, need_blend);
601 }
602
603 if (image.need_blur ())
604 blur_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.h_blurRadius, image.v_blurRadius);
605 if (image.need_tint ())
606 tint_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.tint, image.tint_set, image.shade);
607
608 #if XRENDER
609 if (need_blend)
610 {
611 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
612 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
613
614 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0);
615
616 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
617
618 Picture mask = create_xrender_mask (dpy, vt, False, False);
619
620 XRenderColor mask_c;
621
622 mask_c.alpha = image.alpha;
623 mask_c.red =
624 mask_c.green =
625 mask_c.blue = 0;
626 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
627
628 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, target_width, target_height);
629
630 XRenderFreePicture (dpy, src);
631 XRenderFreePicture (dpy, dst);
632 XRenderFreePicture (dpy, mask);
633 }
634 #endif
635
636 XFreeGC (dpy, gc);
637
638 ret = true;
639 }
640
641 if (result != pixbuf)
642 g_object_unref (result);
643
644 if (need_blend)
645 XFreePixmap (dpy, tmp_pixmap);
646
647 return ret;
648 }
649 # endif /* HAVE_PIXBUF */
650
651 rxvt_image::rxvt_image ()
652 {
653 alpha = 0xffff;
654 flags = 0;
655 h_scale =
656 v_scale = defaultScale;
657 h_align =
658 v_align = defaultAlign;
659
660 # if HAVE_PIXBUF
661 pixbuf = 0;
662 # endif
663 }
664
665 bool
666 rxvt_image::set_file_geometry (const char *file)
667 {
668 if (!file || !*file)
669 return false;
670
671 const char *p = strchr (file, ';');
672
673 if (p)
674 {
675 size_t len = p - file;
676 char *f = rxvt_temp_buf<char> (len + 1);
677 memcpy (f, file, len);
678 f[len] = '\0';
679 file = f;
680 }
681
682 bool ret = set_file (file);
683 alpha = 0x8000;
684 if (ret)
685 set_geometry (p ? p + 1 : "");
686 return ret;
687 }
688
689 bool
690 rxvt_image::set_file (const char *file)
691 {
692 bool ret = false;
693
694 # if HAVE_PIXBUF
695 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
696 if (image)
697 {
698 if (pixbuf)
699 g_object_unref (pixbuf);
700 pixbuf = image;
701 ret = true;
702 }
703 # endif
704
705 if (ret)
706 flags |= IM_IS_SET;
707
708 return ret;
709 }
710
711 # endif /* BG_IMAGE_FROM_FILE */
712
713 bool
714 image_effects::set_blur (const char *geom)
715 {
716 bool changed = false;
717 unsigned int hr, vr;
718 int junk;
719 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr);
720
721 if (!(geom_flags & WidthValue))
722 hr = 1;
723 if (!(geom_flags & HeightValue))
724 vr = hr;
725
726 min_it (hr, 128);
727 min_it (vr, 128);
728
729 if (h_blurRadius != hr)
730 {
731 changed = true;
732 h_blurRadius = hr;
733 }
734
735 if (v_blurRadius != vr)
736 {
737 changed = true;
738 v_blurRadius = vr;
739 }
740
741 return changed;
742 }
743
744 bool
745 image_effects::set_tint (const rxvt_color &new_tint)
746 {
747 if (!tint_set || tint != new_tint)
748 {
749 tint = new_tint;
750 tint_set = true;
751
752 return true;
753 }
754
755 return false;
756 }
757
758 bool
759 image_effects::set_shade (const char *shade_str)
760 {
761 int new_shade = atoi (shade_str);
762
763 clamp_it (new_shade, -100, 200);
764 if (new_shade < 0)
765 new_shade = 200 - (100 + new_shade);
766
767 if (new_shade != shade)
768 {
769 shade = new_shade;
770 return true;
771 }
772
773 return false;
774 }
775
776 #if XRENDER
777 static void
778 get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
779 {
780 double sigma = radius / 2.0;
781 double scale = sqrt (2.0 * M_PI) * sigma;
782 double sum = 0.0;
783
784 for (int i = 0; i < width; i++)
785 {
786 double x = i - width / 2;
787 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
788 sum += kernel[i];
789 }
790
791 params[0] = XDoubleToFixed (width);
792 params[1] = XDoubleToFixed (1);
793
794 for (int i = 0; i < width; i++)
795 params[i+2] = XDoubleToFixed (kernel[i] / sum);
796 }
797 #endif
798
799 bool
800 rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height, bool argb, int h_blurRadius, int v_blurRadius)
801 {
802 bool ret = false;
803 #if XRENDER
804 if (!(display->flags & DISPLAY_HAS_RENDER_CONV))
805 return false;
806
807 int size = max (h_blurRadius, v_blurRadius) * 2 + 1;
808 double *kernel = (double *)malloc (size * sizeof (double));
809 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
810
811 XRenderPictureAttributes pa;
812 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
813 : XRenderFindVisualFormat (dpy, visual);
814
815 pa.repeat = RepeatPad;
816 Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
817 Pixmap tmp = XCreatePixmap (dpy, pixmap, width, height, depth);
818 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
819 XFreePixmap (dpy, tmp);
820
821 if (kernel && params)
822 {
823 size = h_blurRadius * 2 + 1;
824 get_gaussian_kernel (h_blurRadius, size, kernel, params);
825
826 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
827 XRenderComposite (dpy,
828 PictOpSrc,
829 src,
830 None,
831 dst,
832 0, 0,
833 0, 0,
834 0, 0,
835 width, height);
836
837 ::swap (src, dst);
838
839 size = v_blurRadius * 2 + 1;
840 get_gaussian_kernel (v_blurRadius, size, kernel, params);
841 ::swap (params[0], params[1]);
842
843 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
844 XRenderComposite (dpy,
845 PictOpSrc,
846 src,
847 None,
848 dst,
849 0, 0,
850 0, 0,
851 0, 0,
852 width, height);
853
854 ret = true;
855 }
856
857 free (kernel);
858 free (params);
859 XRenderFreePicture (dpy, src);
860 XRenderFreePicture (dpy, dst);
861 #endif
862 return ret;
863 }
864
865 bool
866 rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height, bool argb, rxvt_color &tint, bool tint_set, int shade)
867 {
868 bool ret = false;
869
870 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
871
872 if (tint_set)
873 tint.get (c);
874
875 if (shade == 100
876 && (c.r <= 0x00ff || c.r >= 0xff00)
877 && (c.g <= 0x00ff || c.g >= 0xff00)
878 && (c.b <= 0x00ff || c.b >= 0xff00))
879 {
880 XGCValues gcv;
881 GC gc;
882
883 /* In this case we can tint image server-side getting significant
884 * performance improvements, as we eliminate XImage transfer
885 */
886 gcv.foreground = Pixel (tint);
887 gcv.function = GXand;
888 gcv.fill_style = FillSolid;
889 gc = XCreateGC (dpy, pixmap, GCFillStyle | GCForeground | GCFunction, &gcv);
890 if (gc)
891 {
892 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height);
893 ret = true;
894 XFreeGC (dpy, gc);
895 }
896 }
897 # if XRENDER
898 else if (display->flags & DISPLAY_HAS_RENDER)
899 {
900 if (shade <= 100)
901 {
902 c.r = c.r * shade / 100;
903 c.g = c.g * shade / 100;
904 c.b = c.b * shade / 100;
905 }
906 else
907 {
908 c.r = c.r * (200 - shade) / 100;
909 c.g = c.g * (200 - shade) / 100;
910 c.b = c.b * (200 - shade) / 100;
911 }
912
913 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
914 : XRenderFindVisualFormat (dpy, visual);
915
916 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0);
917
918 Picture overlay_pic = create_xrender_mask (dpy, pixmap, True, False);
919
920 Picture mask_pic = create_xrender_mask (dpy, pixmap, True, True);
921
922 XRenderColor mask_c;
923
924 mask_c.alpha = 0xffff;
925 mask_c.red =
926 mask_c.green =
927 mask_c.blue = 0;
928 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
929
930 mask_c.alpha = 0;
931 mask_c.red = 0xffff - c.r;
932 mask_c.green = 0xffff - c.g;
933 mask_c.blue = 0xffff - c.b;
934 XRenderFillRectangle (dpy, PictOpSrc, mask_pic, &mask_c, 0, 0, 1, 1);
935
936 XRenderComposite (dpy, PictOpOver, overlay_pic, mask_pic, back_pic, 0, 0, 0, 0, 0, 0, width, height);
937
938 if (shade > 100)
939 {
940 mask_c.alpha = 0;
941 mask_c.red =
942 mask_c.green =
943 mask_c.blue = 0xffff * (shade - 100) / 100;
944 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
945
946 XRenderComposite (dpy, PictOpOver, overlay_pic, None, back_pic, 0, 0, 0, 0, 0, 0, width, height);
947 }
948
949 ret = true;
950
951 XRenderFreePicture (dpy, mask_pic);
952 XRenderFreePicture (dpy, overlay_pic);
953 XRenderFreePicture (dpy, back_pic);
954 }
955 # endif
956
957 return ret;
958 }
959
960 # if ENABLE_TRANSPARENCY
961 /*
962 * Builds a pixmap of the same size as the terminal window that contains
963 * the tiled portion of the root pixmap that is supposed to be covered by
964 * our window.
965 */
966 bool
967 rxvt_term::render_root_image ()
968 {
969 bool ret = false;
970
971 /* root dimensions may change from call to call - but Display structure should
972 * be always up-to-date, so let's use it :
973 */
974 int screen = display->screen;
975 int root_depth = DefaultDepth (dpy, screen);
976 int root_width = DisplayWidth (dpy, screen);
977 int root_height = DisplayHeight (dpy, screen);
978 unsigned int root_pmap_width, root_pmap_height;
979 int window_width = szHint.width;
980 int window_height = szHint.height;
981 int sx, sy;
982 XGCValues gcv;
983 GC gc;
984
985 sx = target_x;
986 sy = target_y;
987
988 /* check if we are outside of the visible part of the virtual screen : */
989 if (sx + window_width <= 0 || sy + window_height <= 0
990 || sx >= root_width || sy >= root_height)
991 return 0;
992
993 // validate root pixmap and get its size
994 if (root_pixmap != None)
995 {
996 Window wdummy;
997 int idummy;
998 unsigned int udummy;
999
1000 allowedxerror = -1;
1001
1002 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pmap_width, &root_pmap_height, &udummy, &udummy))
1003 root_pixmap = None;
1004
1005 allowedxerror = 0;
1006 }
1007
1008 Pixmap recoded_root_pmap = root_pixmap;
1009
1010 if (root_pixmap != None && root_depth != depth)
1011 {
1012 #if XRENDER
1013 if (display->flags & DISPLAY_HAS_RENDER)
1014 {
1015 recoded_root_pmap = XCreatePixmap (dpy, vt, root_pmap_width, root_pmap_height, depth);
1016
1017 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, screen));
1018 Picture src = XRenderCreatePicture (dpy, root_pixmap, src_format, 0, 0);
1019
1020 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, visual);
1021 Picture dst = XRenderCreatePicture (dpy, recoded_root_pmap, dst_format, 0, 0);
1022
1023 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, root_pmap_width, root_pmap_height);
1024
1025 XRenderFreePicture (dpy, src);
1026 XRenderFreePicture (dpy, dst);
1027 }
1028 else
1029 #endif
1030 recoded_root_pmap = None;
1031 }
1032
1033 if (recoded_root_pmap == None)
1034 return 0;
1035
1036 if (bg_pixmap == None
1037 || bg_pmap_width != window_width
1038 || bg_pmap_height != window_height)
1039 {
1040 if (bg_pixmap)
1041 XFreePixmap (dpy, bg_pixmap);
1042 bg_pixmap = XCreatePixmap (dpy, vt, window_width, window_height, depth);
1043 bg_pmap_width = window_width;
1044 bg_pmap_height = window_height;
1045 }
1046
1047 /* straightforward pixmap copy */
1048 while (sx < 0) sx += root_pmap_width;
1049 while (sy < 0) sy += root_pmap_height;
1050
1051 gcv.tile = recoded_root_pmap;
1052 gcv.fill_style = FillTiled;
1053 gcv.ts_x_origin = -sx;
1054 gcv.ts_y_origin = -sy;
1055 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1056
1057 if (gc)
1058 {
1059 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1060 ret = true;
1061 bool need_blur = root_effects.need_blur ();
1062 bool need_tint = root_effects.need_tint ();
1063
1064 if (need_blur)
1065 {
1066 if (blur_pixmap (bg_pixmap, window_width, window_height, false,
1067 root_effects.h_blurRadius, root_effects.v_blurRadius))
1068 need_blur = false;
1069 }
1070 if (need_tint)
1071 {
1072 if (tint_pixmap (bg_pixmap, window_width, window_height, false,
1073 root_effects.tint, root_effects.tint_set, root_effects.shade))
1074 need_tint = false;
1075 }
1076 if (need_tint)
1077 {
1078 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1079 if (ximage)
1080 {
1081 /* our own client-side tinting */
1082 tint_ximage (ximage, root_effects.tint, root_effects.tint_set, root_effects.shade);
1083
1084 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
1085 XDestroyImage (ximage);
1086 }
1087 }
1088
1089 XFreeGC (dpy, gc);
1090 }
1091
1092 if (recoded_root_pmap != root_pixmap)
1093 XFreePixmap (dpy, recoded_root_pmap);
1094
1095 return ret;
1096 }
1097
1098 void
1099 rxvt_term::bg_set_root_pixmap ()
1100 {
1101 Pixmap new_root_pixmap = display->get_pixmap_property (xa[XA_XROOTPMAP_ID]);
1102 if (new_root_pixmap == None)
1103 new_root_pixmap = display->get_pixmap_property (xa[XA_ESETROOT_PMAP_ID]);
1104
1105 root_pixmap = new_root_pixmap;
1106 }
1107 # endif /* ENABLE_TRANSPARENCY */
1108
1109 void
1110 rxvt_term::bg_render ()
1111 {
1112 if (bg_flags & BG_INHIBIT_RENDER)
1113 return;
1114
1115 bg_invalidate ();
1116 # if ENABLE_TRANSPARENCY
1117 if (bg_flags & BG_IS_TRANSPARENT)
1118 {
1119 /* we need to re-generate transparency pixmap in that case ! */
1120 if (render_root_image ())
1121 bg_flags |= BG_IS_VALID;
1122 }
1123 # endif
1124
1125 # if BG_IMAGE_FROM_FILE
1126 if (fimage.flags & IM_IS_SET)
1127 {
1128 if (render_image (fimage))
1129 bg_flags |= BG_IS_VALID;
1130 }
1131 # endif
1132
1133 if (!(bg_flags & BG_IS_VALID))
1134 {
1135 if (bg_pixmap != None)
1136 {
1137 XFreePixmap (dpy, bg_pixmap);
1138 bg_pixmap = None;
1139 }
1140 }
1141
1142 scr_recolour (false);
1143 bg_flags |= BG_NEEDS_REFRESH;
1144
1145 bg_valid_since = ev::now ();
1146 }
1147
1148 void
1149 rxvt_term::bg_init ()
1150 {
1151 #if BG_IMAGE_FROM_FILE
1152 if (rs[Rs_backgroundPixmap])
1153 {
1154 if (fimage.set_file_geometry (rs[Rs_backgroundPixmap])
1155 && !bg_window_position_sensitive ())
1156 update_background ();
1157 }
1158 #endif
1159 }
1160
1161 /* based on code from aterm-0.4.2 */
1162
1163 static inline void
1164 fill_lut (uint32_t *lookup, uint32_t mask, int sh, unsigned short low, unsigned short high)
1165 {
1166 for (int i = 0; i <= mask >> sh; i++)
1167 {
1168 uint32_t tmp;
1169 tmp = i * high;
1170 tmp += (mask >> sh) * low;
1171 lookup[i] = (tmp / 0xffff) << sh;
1172 }
1173 }
1174
1175 void
1176 rxvt_term::tint_ximage (XImage *ximage, rxvt_color &tint, bool tint_set, int shade)
1177 {
1178 unsigned int size_r, size_g, size_b;
1179 int sh_r, sh_g, sh_b;
1180 uint32_t mask_r, mask_g, mask_b;
1181 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1182 unsigned short low;
1183 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1184
1185 if (visual->c_class != TrueColor || ximage->format != ZPixmap) return;
1186
1187 /* for convenience */
1188 mask_r = visual->red_mask;
1189 mask_g = visual->green_mask;
1190 mask_b = visual->blue_mask;
1191
1192 /* boring lookup table pre-initialization */
1193 sh_r = ecb_ctz32 (mask_r);
1194 sh_g = ecb_ctz32 (mask_g);
1195 sh_b = ecb_ctz32 (mask_b);
1196
1197 size_r = mask_r >> sh_r;
1198 size_g = mask_g >> sh_g;
1199 size_b = mask_b >> sh_b;
1200
1201 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1202 return;
1203
1204 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1205 lookup_r = lookup;
1206 lookup_g = lookup + size_r;
1207 lookup_b = lookup + size_r + size_g;
1208
1209 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1210
1211 if (tint_set)
1212 tint.get (c);
1213
1214 /* prepare limits for color transformation (each channel is handled separately) */
1215 if (shade > 100)
1216 {
1217 c.r = c.r * (200 - shade) / 100;
1218 c.g = c.g * (200 - shade) / 100;
1219 c.b = c.b * (200 - shade) / 100;
1220
1221 low = 0xffff * (shade - 100) / 100;
1222 }
1223 else
1224 {
1225 c.r = c.r * shade / 100;
1226 c.g = c.g * shade / 100;
1227 c.b = c.b * shade / 100;
1228
1229 low = 0;
1230 }
1231
1232 /* fill our lookup tables */
1233 fill_lut (lookup_r, mask_r, sh_r, low, c.r);
1234 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1235 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1236
1237 /* apply table to input image (replacing colors by newly calculated ones) */
1238 if (ximage->bits_per_pixel == 32
1239 && ximage->byte_order == host_byte_order)
1240 {
1241 char *line = ximage->data;
1242
1243 for (int y = 0; y < ximage->height; y++)
1244 {
1245 uint32_t *p = (uint32_t *)line;
1246 for (int x = 0; x < ximage->width; x++)
1247 {
1248 *p = lookup_r[(*p & mask_r) >> sh_r] |
1249 lookup_g[(*p & mask_g) >> sh_g] |
1250 lookup_b[(*p & mask_b) >> sh_b];
1251 p++;
1252 }
1253 line += ximage->bytes_per_line;
1254 }
1255 }
1256 else
1257 {
1258 for (int y = 0; y < ximage->height; y++)
1259 for (int x = 0; x < ximage->width; x++)
1260 {
1261 unsigned long pixel = XGetPixel (ximage, x, y);
1262 pixel = lookup_r[(pixel & mask_r) >> sh_r] |
1263 lookup_g[(pixel & mask_g) >> sh_g] |
1264 lookup_b[(pixel & mask_b) >> sh_b];
1265 XPutPixel (ximage, x, y, pixel);
1266 }
1267 }
1268
1269 free (lookup);
1270 }
1271
1272 #endif /* HAVE_BG_PIXMAP */