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

# User Rev Content
1 sasha 1.1 /*----------------------------------------------------------------------*
2 ayin 1.4 * File: background.C - former xpm.C
3 sasha 1.1 *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6 root 1.147 * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de>
7 sasha 1.1 * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net>
8 sf-exg 1.209 * Copyright (c) 2010-2012 Emanuele Giaquinta <e.giaquinta@glauco.it>
9 sasha 1.1 *
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 sf-exg 1.200 #include <math.h>
26 sasha 1.1 #include "../config.h" /* NECESSARY */
27     #include "rxvt.h" /* NECESSARY */
28    
29 sf-exg 1.109 #if XRENDER
30     # include <X11/extensions/Xrender.h>
31     #endif
32    
33 sf-exg 1.118 #ifndef FilterConvolution
34     #define FilterConvolution "convolution"
35     #endif
36    
37 sf-exg 1.195 #ifndef RepeatPad
38     #define RepeatPad True
39     #endif
40    
41 sasha 1.1 #ifdef HAVE_BG_PIXMAP
42 sf-exg 1.188 # 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 root 1.14 void
61 sf-exg 1.142 rxvt_term::bg_destroy ()
62 sasha 1.11 {
63 root 1.232 # if BG_IMAGE_FROM_FILE
64 sf-exg 1.233 fimage.destroy ();
65 sf-exg 1.221 # endif
66    
67 sf-exg 1.142 if (bg_pixmap)
68     XFreePixmap (dpy, bg_pixmap);
69 sasha 1.11 }
70 sasha 1.2
71 sasha 1.1 bool
72 sf-exg 1.142 rxvt_term::bg_set_position (int x, int y)
73 sf-exg 1.121 {
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 sf-exg 1.142 rxvt_term::bg_window_size_sensitive ()
87 sasha 1.1 {
88 root 1.232 # if ENABLE_TRANSPARENCY
89 sf-exg 1.143 if (bg_flags & BG_IS_TRANSPARENT)
90 sasha 1.13 return true;
91     # endif
92    
93 root 1.232 # if BG_IMAGE_FROM_FILE
94 sf-exg 1.233 if (fimage.flags & IM_IS_SET)
95 sasha 1.1 {
96 sf-exg 1.233 if ((fimage.flags & IM_IS_SIZE_SENSITIVE)
97     || fimage.width () > szHint.width
98     || fimage.height () > szHint.height)
99 sasha 1.1 return true;
100     }
101     # endif
102 sasha 1.13
103     return false;
104     }
105    
106 sf-exg 1.37 bool
107 sf-exg 1.142 rxvt_term::bg_window_position_sensitive ()
108 sasha 1.13 {
109 root 1.232 # if ENABLE_TRANSPARENCY
110 sf-exg 1.143 if (bg_flags & BG_IS_TRANSPARENT)
111 sasha 1.1 return true;
112     # endif
113 sasha 1.13
114 root 1.232 # if BG_IMAGE_FROM_FILE
115 sf-exg 1.233 if (fimage.flags & IM_IS_SET)
116 sasha 1.13 {
117 sf-exg 1.233 if (fimage.flags & IM_ROOT_ALIGN)
118 sasha 1.13 return true;
119     }
120     # endif
121    
122 sasha 1.1 return false;
123 sf-exg 1.117 }
124 sasha 1.1
125 root 1.232 # if BG_IMAGE_FROM_FILE
126 sasha 1.1 static inline int
127     make_align_position (int align, int window_size, int image_size)
128     {
129 sf-exg 1.66 if (align >= 0 && align <= 100)
130 sf-exg 1.206 return lerp (0, window_size - image_size, align);
131 sf-exg 1.205 else if (align > 100)
132 sf-exg 1.206 return lerp (window_size - image_size, window_size, align - 100);
133 sf-exg 1.205 else
134 sf-exg 1.206 return lerp (-image_size, 0, align + 100);
135 sasha 1.1 }
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 sf-exg 1.66 dst_pos = pos;
142 sasha 1.1 dst_size = size;
143 sf-exg 1.60 if (pos < 0)
144 sasha 1.1 {
145     src_pos = -pos;
146 sf-exg 1.66 dst_pos = 0;
147 sasha 1.1 dst_size += pos;
148     }
149    
150 sf-exg 1.197 min_it (dst_size, target_size - dst_pos);
151 sasha 1.1 return src_pos;
152     }
153    
154 sf-exg 1.219 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 sasha 1.1 bool
190 sf-exg 1.214 rxvt_image::set_geometry (const char *geom, bool update)
191 sasha 1.1 {
192 sf-exg 1.110 bool changed = false;
193     int geom_flags = 0;
194 sf-exg 1.198 int x = h_align;
195     int y = v_align;
196     unsigned int w = h_scale;
197     unsigned int h = v_scale;
198 sf-exg 1.219 uint8_t new_flags = 0;
199 sasha 1.1
200     if (geom == NULL)
201     return false;
202    
203 sf-exg 1.163 if (geom[0])
204 sf-exg 1.122 {
205 sf-exg 1.163 char **arr = rxvt_strsplit (':', geom);
206 sf-exg 1.131
207 sf-exg 1.173 for (int i = 0; arr[i]; i++)
208 sasha 1.1 {
209 sf-exg 1.219 if (!strncasecmp (arr[i], "style=", 6))
210 sf-exg 1.160 {
211 sf-exg 1.219 parse_style (arr[i] + 6, x, y, w, h, new_flags);
212 sf-exg 1.160 geom_flags = WidthValue|HeightValue|XValue|YValue;
213     }
214     else if (!strcasecmp (arr[i], "op=tile"))
215 sf-exg 1.214 new_flags |= IM_TILE;
216 sf-exg 1.176 else if (!strcasecmp (arr[i], "op=keep-aspect"))
217 sf-exg 1.214 new_flags |= IM_KEEP_ASPECT;
218 sf-exg 1.176 else if (!strcasecmp (arr[i], "op=root-align"))
219 sf-exg 1.214 new_flags |= IM_ROOT_ALIGN;
220 sf-exg 1.160
221     // deprecated
222     else if (!strcasecmp (arr[i], "tile"))
223 sf-exg 1.122 {
224 sf-exg 1.214 new_flags |= IM_TILE;
225 sf-exg 1.122 w = h = noScale;
226     geom_flags |= WidthValue|HeightValue;
227     }
228 sf-exg 1.133 else if (!strcasecmp (arr[i], "propscale"))
229 sf-exg 1.122 {
230 sf-exg 1.214 new_flags |= IM_KEEP_ASPECT;
231 sf-exg 1.177 w = h = windowScale;
232     geom_flags |= WidthValue|HeightValue;
233 sf-exg 1.122 }
234 sf-exg 1.161 else if (!strcasecmp (arr[i], "hscale"))
235     {
236 sf-exg 1.214 new_flags |= IM_TILE;
237 sf-exg 1.161 w = windowScale;
238     h = noScale;
239     geom_flags |= WidthValue|HeightValue;
240     }
241     else if (!strcasecmp (arr[i], "vscale"))
242     {
243 sf-exg 1.214 new_flags |= IM_TILE;
244 sf-exg 1.161 h = windowScale;
245     w = noScale;
246     geom_flags |= WidthValue|HeightValue;
247     }
248 sf-exg 1.133 else if (!strcasecmp (arr[i], "scale"))
249 sf-exg 1.122 {
250 sf-exg 1.161 w = h = windowScale;
251 sf-exg 1.122 geom_flags |= WidthValue|HeightValue;
252     }
253 sf-exg 1.161 else if (!strcasecmp (arr[i], "auto"))
254     {
255     w = h = windowScale;
256     x = y = centerAlign;
257     geom_flags |= WidthValue|HeightValue|XValue|YValue;
258     }
259 sf-exg 1.133 else if (!strcasecmp (arr[i], "root"))
260 sf-exg 1.122 {
261 sf-exg 1.214 new_flags |= IM_TILE|IM_ROOT_ALIGN;
262 sf-exg 1.122 w = h = noScale;
263     geom_flags |= WidthValue|HeightValue;
264     }
265 sf-exg 1.163
266     else
267     geom_flags |= XParseGeometry (arr[i], &x, &y, &w, &h);
268 sf-exg 1.131 } /* done parsing ops */
269 root 1.26
270 sf-exg 1.131 rxvt_free_strsplit (arr);
271 sf-exg 1.122 }
272 sasha 1.1
273 sf-exg 1.214 new_flags |= flags & ~IM_GEOMETRY_FLAGS;
274 sf-exg 1.185
275 sf-exg 1.163 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 sf-exg 1.198 clamp_it (x, -100, 200);
291     clamp_it (y, -100, 200);
292    
293 sf-exg 1.214 if (flags != new_flags
294 sf-exg 1.198 || h_scale != w
295     || v_scale != h
296     || h_align != x
297     || v_align != y)
298 sasha 1.1 {
299 sf-exg 1.214 flags = new_flags;
300 sf-exg 1.198 h_scale = w;
301     v_scale = h;
302     h_align = x;
303     v_align = y;
304 sf-exg 1.110 changed = true;
305 sasha 1.1 }
306 root 1.26
307 sf-exg 1.225 if (is_size_sensitive ())
308 sf-exg 1.214 flags |= IM_IS_SIZE_SENSITIVE;
309     else
310     flags &= ~IM_IS_SIZE_SENSITIVE;
311    
312 sf-exg 1.110 return changed;
313 sasha 1.1 }
314    
315 sf-exg 1.59 void
316 sf-exg 1.214 rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
317 sf-exg 1.59 {
318 sf-exg 1.214 int image_width = image.width ();
319     int image_height = image.height ();
320 sf-exg 1.142 int target_width = szHint.width;
321     int target_height = szHint.height;
322 sf-exg 1.217 int h_scale = min (image.h_scale, 32767 * 100 / target_width);
323     int v_scale = min (image.v_scale, 32767 * 100 / target_height);
324 sf-exg 1.59
325 sf-exg 1.217 w = h_scale * target_width / 100;
326     h = v_scale * target_height / 100;
327 sf-exg 1.164
328 sf-exg 1.214 if (image.flags & IM_KEEP_ASPECT)
329 sf-exg 1.62 {
330 sf-exg 1.164 float scale = (float)w / image_width;
331     min_it (scale, (float)h / image_height);
332 sf-exg 1.62 w = image_width * scale + 0.5;
333     h = image_height * scale + 0.5;
334     }
335 sf-exg 1.59
336 sf-exg 1.68 if (!w) w = image_width;
337     if (!h) h = image_height;
338    
339 sf-exg 1.214 if (image.flags & IM_ROOT_ALIGN)
340 sf-exg 1.59 {
341 sf-exg 1.121 x = -target_x;
342     y = -target_y;
343 sf-exg 1.59 }
344 sf-exg 1.63 else
345     {
346 sf-exg 1.214 x = make_align_position (image.h_align, target_width, w);
347     y = make_align_position (image.v_align, target_height, h);
348 sf-exg 1.63 }
349 sf-exg 1.59 }
350    
351 root 1.232 # if HAVE_PIXBUF
352 sf-exg 1.55 bool
353 sf-exg 1.142 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 sf-exg 1.218 unsigned int width, unsigned int height, bool argb)
356 sf-exg 1.127 {
357     XImage *ximage;
358 sf-exg 1.210 char *line;
359 sf-exg 1.207 int width_r, width_g, width_b, width_a;
360     int sh_r, sh_g, sh_b, sh_a;
361 sf-exg 1.218 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
362 sf-exg 1.127 int rowstride;
363     int channels;
364     unsigned char *row;
365    
366     if (visual->c_class != TrueColor)
367     return false;
368    
369 sf-exg 1.218 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 sf-exg 1.207 #if XRENDER
382 sf-exg 1.218 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
383     if (format)
384     alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
385     else
386 sf-exg 1.207 #endif
387 sf-exg 1.218 alpha_mask = 0;
388     }
389 sf-exg 1.207
390 sf-exg 1.218 width_r = ecb_popcount32 (red_mask);
391     width_g = ecb_popcount32 (green_mask);
392     width_b = ecb_popcount32 (blue_mask);
393 sf-exg 1.207 width_a = ecb_popcount32 (alpha_mask);
394 sf-exg 1.127
395 sf-exg 1.207 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
396 sf-exg 1.127 return false;
397    
398 sf-exg 1.218 sh_r = ecb_ctz32 (red_mask);
399     sh_g = ecb_ctz32 (green_mask);
400     sh_b = ecb_ctz32 (blue_mask);
401 sf-exg 1.207 sh_a = ecb_ctz32 (alpha_mask);
402 sf-exg 1.127
403 sf-exg 1.210 if (width > 32767 || height > 32767)
404 sf-exg 1.127 return false;
405    
406 sf-exg 1.218 ximage = XCreateImage (dpy, visual, argb ? 32 : depth, ZPixmap, 0, 0,
407 sf-exg 1.210 width, height, 32, 0);
408     if (!ximage)
409 sf-exg 1.127 return false;
410    
411 sf-exg 1.210 if (height > INT_MAX / ximage->bytes_per_line
412     || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
413 sf-exg 1.127 {
414 sf-exg 1.210 XDestroyImage (ximage);
415 sf-exg 1.127 return false;
416     }
417    
418 sf-exg 1.155 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
419 sf-exg 1.127
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 sf-exg 1.210 line = ximage->data;
424 sf-exg 1.127
425 sf-exg 1.207 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 sf-exg 1.127 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 sf-exg 1.207 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 sf-exg 1.127
458 sf-exg 1.207 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 sf-exg 1.127
463 sf-exg 1.210 if (ximage->bits_per_pixel == 32)
464 sf-exg 1.127 ((uint32_t *)line)[x] = value;
465     else
466 sf-exg 1.210 XPutPixel (ximage, x, y, value);
467 sf-exg 1.127 }
468    
469     row += rowstride;
470     line += ximage->bytes_per_line;
471     }
472    
473 sf-exg 1.142 XPutImage (dpy, pixmap, gc, ximage, 0, 0, dst_x, dst_y, width, height);
474 sf-exg 1.127 XDestroyImage (ximage);
475     return true;
476     }
477    
478     bool
479 sf-exg 1.215 rxvt_term::render_image (rxvt_image &image)
480 sf-exg 1.55 {
481 sf-exg 1.214 GdkPixbuf *pixbuf = image.pixbuf;
482 sf-exg 1.55 if (!pixbuf)
483     return false;
484    
485 sf-exg 1.215 bool need_blend = bg_flags & BG_IS_VALID;
486    
487     if (need_blend
488 sf-exg 1.230 && !(display->flags & DISPLAY_HAS_RENDER))
489 sf-exg 1.55 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 sf-exg 1.142 int target_width = szHint.width;
497     int target_height = szHint.height;
498 sf-exg 1.55 int new_pmap_width = target_width;
499     int new_pmap_height = target_height;
500    
501     int x = 0;
502     int y = 0;
503 sf-exg 1.59 int w = 0;
504     int h = 0;
505 sf-exg 1.55
506 sf-exg 1.214 get_image_geometry (image, w, h, x, y);
507 sf-exg 1.55
508 sf-exg 1.214 if (!(image.flags & IM_ROOT_ALIGN)
509 sf-exg 1.67 && (x >= target_width
510     || y >= target_height
511 sf-exg 1.172 || x + w <= 0
512     || y + h <= 0))
513 sf-exg 1.55 return false;
514    
515     result = pixbuf;
516    
517 sf-exg 1.172 if (w != image_width
518     || h != image_height)
519 sf-exg 1.55 {
520     result = gdk_pixbuf_scale_simple (pixbuf,
521 sf-exg 1.68 w, h,
522 sf-exg 1.55 GDK_INTERP_BILINEAR);
523     }
524    
525 sf-exg 1.151 if (!result)
526     return false;
527    
528 sf-exg 1.55 bool ret = false;
529    
530 sf-exg 1.152 XGCValues gcv;
531     GC gc;
532 sf-exg 1.215 Pixmap tmp_pixmap;
533 sf-exg 1.55
534 sf-exg 1.152 image_width = gdk_pixbuf_get_width (result);
535     image_height = gdk_pixbuf_get_height (result);
536 sf-exg 1.55
537 sf-exg 1.215 if (need_blend)
538 sf-exg 1.218 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
539 sf-exg 1.152 else
540     {
541 sf-exg 1.233 if (image.flags & IM_TILE)
542 sf-exg 1.58 {
543 sf-exg 1.152 new_pmap_width = min (image_width, target_width);
544     new_pmap_height = min (image_height, target_height);
545 sf-exg 1.58 }
546 sf-exg 1.55
547 sf-exg 1.218 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 sf-exg 1.152 }
560 sf-exg 1.55
561 sf-exg 1.154 gcv.foreground = pix_colors[Color_bg];
562 sf-exg 1.218 gc = XCreateGC (dpy, tmp_pixmap, GCForeground, &gcv);
563 sf-exg 1.154
564     if (gc)
565 sf-exg 1.152 {
566 sf-exg 1.214 if (image.flags & IM_TILE)
567 sf-exg 1.154 {
568 sf-exg 1.218 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
569 sf-exg 1.154 pixbuf_to_pixmap (result, tile, gc,
570     0, 0,
571     0, 0,
572 sf-exg 1.218 image_width, image_height, need_blend);
573 sf-exg 1.154
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 sf-exg 1.55
580 sf-exg 1.218 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
581 sf-exg 1.154 XFreePixmap (dpy, tile);
582     }
583     else
584 sf-exg 1.55 {
585 sf-exg 1.154 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 sf-exg 1.55
591 sf-exg 1.154 if (dst_x > 0 || dst_y > 0
592     || dst_x + dst_width < new_pmap_width
593     || dst_y + dst_height < new_pmap_height)
594 sf-exg 1.218 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
595 sf-exg 1.154
596     if (dst_x < new_pmap_width && dst_y < new_pmap_height)
597 sf-exg 1.218 pixbuf_to_pixmap (result, tmp_pixmap, gc,
598 sf-exg 1.154 src_x, src_y,
599     dst_x, dst_y,
600 sf-exg 1.218 dst_width, dst_height, need_blend);
601 sf-exg 1.154 }
602 sf-exg 1.55
603 sf-exg 1.226 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 sf-exg 1.154 #if XRENDER
609 sf-exg 1.215 if (need_blend)
610 sf-exg 1.154 {
611 sf-exg 1.218 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
612 sf-exg 1.190 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
613 sf-exg 1.55
614 sf-exg 1.218 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0);
615 sf-exg 1.55
616 sf-exg 1.191 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
617 sf-exg 1.152
618 sf-exg 1.188 Picture mask = create_xrender_mask (dpy, vt, False, False);
619 sf-exg 1.79
620 sf-exg 1.154 XRenderColor mask_c;
621 sf-exg 1.79
622 sf-exg 1.223 mask_c.alpha = image.alpha;
623 sf-exg 1.192 mask_c.red =
624     mask_c.green =
625     mask_c.blue = 0;
626 sf-exg 1.154 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
627 sf-exg 1.192
628 sf-exg 1.154 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 sf-exg 1.152 #endif
635 sf-exg 1.79
636 sf-exg 1.154 XFreeGC (dpy, gc);
637 sf-exg 1.79
638 sf-exg 1.154 ret = true;
639 sf-exg 1.152 }
640 sf-exg 1.79
641 sf-exg 1.152 if (result != pixbuf)
642     g_object_unref (result);
643 sf-exg 1.55
644 sf-exg 1.215 if (need_blend)
645     XFreePixmap (dpy, tmp_pixmap);
646 sf-exg 1.55
647     return ret;
648     }
649     # endif /* HAVE_PIXBUF */
650    
651 sf-exg 1.220 rxvt_image::rxvt_image ()
652     {
653 sf-exg 1.224 alpha = 0xffff;
654     flags = 0;
655 sf-exg 1.220 h_scale =
656 sf-exg 1.224 v_scale = defaultScale;
657 sf-exg 1.220 h_align =
658 sf-exg 1.224 v_align = defaultAlign;
659 sf-exg 1.220
660 root 1.232 # if HAVE_PIXBUF
661 sf-exg 1.221 pixbuf = 0;
662 sf-exg 1.220 # endif
663     }
664    
665 sasha 1.1 bool
666 sf-exg 1.220 rxvt_image::set_file_geometry (const char *file)
667 sasha 1.1 {
668 sf-exg 1.107 if (!file || !*file)
669     return false;
670 sasha 1.1
671 sf-exg 1.167 const char *p = strchr (file, ';');
672    
673     if (p)
674 sasha 1.1 {
675 sf-exg 1.107 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 root 1.25
682 sf-exg 1.220 bool ret = set_file (file);
683     alpha = 0x8000;
684 sf-exg 1.224 if (ret)
685     set_geometry (p ? p + 1 : "");
686 sf-exg 1.220 return ret;
687     }
688    
689     bool
690     rxvt_image::set_file (const char *file)
691     {
692     bool ret = false;
693    
694 root 1.232 # if HAVE_PIXBUF
695 sf-exg 1.107 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
696     if (image)
697     {
698     if (pixbuf)
699     g_object_unref (pixbuf);
700 sf-exg 1.221 pixbuf = image;
701 sf-exg 1.167 ret = true;
702 sf-exg 1.107 }
703 sf-exg 1.55 # endif
704 root 1.25
705 sf-exg 1.167 if (ret)
706 sf-exg 1.224 flags |= IM_IS_SET;
707 sf-exg 1.167
708     return ret;
709 sasha 1.1 }
710    
711 ayin 1.12 # endif /* BG_IMAGE_FROM_FILE */
712 sasha 1.1
713 ayin 1.10 bool
714 sf-exg 1.226 image_effects::set_blur (const char *geom)
715 sasha 1.1 {
716 sf-exg 1.110 bool changed = false;
717 sasha 1.1 unsigned int hr, vr;
718     int junk;
719     int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr);
720    
721 sf-exg 1.52 if (!(geom_flags & WidthValue))
722 sasha 1.1 hr = 1;
723 sf-exg 1.52 if (!(geom_flags & HeightValue))
724 sasha 1.1 vr = hr;
725    
726 sf-exg 1.80 min_it (hr, 128);
727     min_it (vr, 128);
728    
729 sasha 1.1 if (h_blurRadius != hr)
730     {
731 sf-exg 1.110 changed = true;
732 sasha 1.1 h_blurRadius = hr;
733     }
734    
735     if (v_blurRadius != vr)
736     {
737 sf-exg 1.110 changed = true;
738 sasha 1.1 v_blurRadius = vr;
739     }
740 ayin 1.10
741 sf-exg 1.110 return changed;
742 sasha 1.1 }
743    
744     bool
745 sf-exg 1.226 image_effects::set_tint (const rxvt_color &new_tint)
746 sasha 1.1 {
747 sf-exg 1.226 if (!tint_set || tint != new_tint)
748 sasha 1.1 {
749     tint = new_tint;
750 sf-exg 1.226 tint_set = true;
751 sf-exg 1.203
752 sasha 1.1 return true;
753     }
754 root 1.20
755 sasha 1.1 return false;
756     }
757    
758     bool
759 sf-exg 1.226 image_effects::set_shade (const char *shade_str)
760 sasha 1.1 {
761 sf-exg 1.186 int new_shade = atoi (shade_str);
762 sasha 1.1
763 sf-exg 1.75 clamp_it (new_shade, -100, 200);
764     if (new_shade < 0)
765 root 1.20 new_shade = 200 - (100 + new_shade);
766 sasha 1.1
767     if (new_shade != shade)
768     {
769     shade = new_shade;
770     return true;
771     }
772 root 1.20
773 sasha 1.1 return false;
774     }
775    
776 sf-exg 1.101 #if XRENDER
777 sf-exg 1.80 static void
778     get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
779     {
780 sf-exg 1.89 double sigma = radius / 2.0;
781     double scale = sqrt (2.0 * M_PI) * sigma;
782     double sum = 0.0;
783 sf-exg 1.80
784 sf-exg 1.89 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 sf-exg 1.80
791 sf-exg 1.89 params[0] = XDoubleToFixed (width);
792     params[1] = XDoubleToFixed (1);
793 sf-exg 1.80
794 sf-exg 1.89 for (int i = 0; i < width; i++)
795     params[i+2] = XDoubleToFixed (kernel[i] / sum);
796 sf-exg 1.80 }
797 sf-exg 1.84 #endif
798 sf-exg 1.80
799     bool
800 sf-exg 1.226 rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height, bool argb, int h_blurRadius, int v_blurRadius)
801 sf-exg 1.80 {
802     bool ret = false;
803 sf-exg 1.101 #if XRENDER
804 sf-exg 1.230 if (!(display->flags & DISPLAY_HAS_RENDER_CONV))
805 sf-exg 1.181 return false;
806    
807 sf-exg 1.80 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 sf-exg 1.195 XRenderPictureAttributes pa;
812 sf-exg 1.226 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
813     : XRenderFindVisualFormat (dpy, visual);
814 sf-exg 1.80
815 sf-exg 1.195 pa.repeat = RepeatPad;
816     Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
817 sf-exg 1.196 Pixmap tmp = XCreatePixmap (dpy, pixmap, width, height, depth);
818     Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
819     XFreePixmap (dpy, tmp);
820 sf-exg 1.80
821 sf-exg 1.154 if (kernel && params)
822 sf-exg 1.80 {
823 sf-exg 1.193 size = h_blurRadius * 2 + 1;
824     get_gaussian_kernel (h_blurRadius, size, kernel, params);
825 sf-exg 1.80
826 sf-exg 1.193 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 sf-exg 1.196 ::swap (src, dst);
838    
839 sf-exg 1.193 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 sf-exg 1.80
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 sf-exg 1.64 bool
866 sf-exg 1.226 rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height, bool argb, rxvt_color &tint, bool tint_set, int shade)
867 sf-exg 1.64 {
868     bool ret = false;
869    
870 sf-exg 1.226 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 sf-exg 1.64 {
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 sf-exg 1.77 gc = XCreateGC (dpy, pixmap, GCFillStyle | GCForeground | GCFunction, &gcv);
890 sf-exg 1.64 if (gc)
891     {
892 sf-exg 1.70 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height);
893 sf-exg 1.64 ret = true;
894     XFreeGC (dpy, gc);
895     }
896     }
897 sf-exg 1.181 # if XRENDER
898 sf-exg 1.230 else if (display->flags & DISPLAY_HAS_RENDER)
899 sf-exg 1.64 {
900 sf-exg 1.75 if (shade <= 100)
901 sf-exg 1.64 {
902 root 1.148 c.r = c.r * shade / 100;
903     c.g = c.g * shade / 100;
904     c.b = c.b * shade / 100;
905 sf-exg 1.64 }
906 sf-exg 1.75 else
907 sf-exg 1.64 {
908 root 1.148 c.r = c.r * (200 - shade) / 100;
909     c.g = c.g * (200 - shade) / 100;
910     c.b = c.b * (200 - shade) / 100;
911 sf-exg 1.64 }
912    
913 sf-exg 1.226 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
914     : XRenderFindVisualFormat (dpy, visual);
915 sf-exg 1.64
916 sf-exg 1.191 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0);
917 sf-exg 1.64
918 sf-exg 1.188 Picture overlay_pic = create_xrender_mask (dpy, pixmap, True, False);
919 sf-exg 1.64
920 sf-exg 1.188 Picture mask_pic = create_xrender_mask (dpy, pixmap, True, True);
921 sf-exg 1.64
922 sf-exg 1.154 XRenderColor mask_c;
923 sf-exg 1.64
924 sf-exg 1.154 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 sf-exg 1.192
936 sf-exg 1.154 XRenderComposite (dpy, PictOpOver, overlay_pic, mask_pic, back_pic, 0, 0, 0, 0, 0, 0, width, height);
937 sf-exg 1.64
938 sf-exg 1.154 if (shade > 100)
939     {
940 sf-exg 1.64 mask_c.alpha = 0;
941 sf-exg 1.192 mask_c.red =
942     mask_c.green =
943     mask_c.blue = 0xffff * (shade - 100) / 100;
944 sf-exg 1.154 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
945 sf-exg 1.125
946 sf-exg 1.154 XRenderComposite (dpy, PictOpOver, overlay_pic, None, back_pic, 0, 0, 0, 0, 0, 0, width, height);
947     }
948 sf-exg 1.125
949 sf-exg 1.154 ret = true;
950 sf-exg 1.64
951     XRenderFreePicture (dpy, mask_pic);
952     XRenderFreePicture (dpy, overlay_pic);
953     XRenderFreePicture (dpy, back_pic);
954 sf-exg 1.181 }
955 sf-exg 1.64 # endif
956    
957     return ret;
958     }
959    
960 root 1.232 # if ENABLE_TRANSPARENCY
961 sf-exg 1.149 /*
962 sf-exg 1.100 * 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 sasha 1.1 * our window.
965     */
966 sf-exg 1.202 bool
967 sf-exg 1.228 rxvt_term::render_root_image ()
968 sasha 1.1 {
969 sf-exg 1.202 bool ret = false;
970 sasha 1.1
971 sf-exg 1.39 /* root dimensions may change from call to call - but Display structure should
972 sasha 1.1 * be always up-to-date, so let's use it :
973     */
974 sf-exg 1.142 int screen = display->screen;
975 sf-exg 1.103 int root_depth = DefaultDepth (dpy, screen);
976 sasha 1.1 int root_width = DisplayWidth (dpy, screen);
977     int root_height = DisplayHeight (dpy, screen);
978 sf-exg 1.103 unsigned int root_pmap_width, root_pmap_height;
979 sf-exg 1.142 int window_width = szHint.width;
980     int window_height = szHint.height;
981 sasha 1.1 int sx, sy;
982     XGCValues gcv;
983 sf-exg 1.81 GC gc;
984 sasha 1.1
985 sf-exg 1.121 sx = target_x;
986     sy = target_y;
987 sasha 1.1
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 sf-exg 1.103 // 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 sf-exg 1.142 allowedxerror = -1;
1001 sf-exg 1.103
1002     if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pmap_width, &root_pmap_height, &udummy, &udummy))
1003     root_pixmap = None;
1004    
1005 sf-exg 1.142 allowedxerror = 0;
1006 sf-exg 1.103 }
1007    
1008     Pixmap recoded_root_pmap = root_pixmap;
1009    
1010 sf-exg 1.142 if (root_pixmap != None && root_depth != depth)
1011 sf-exg 1.103 {
1012     #if XRENDER
1013 sf-exg 1.230 if (display->flags & DISPLAY_HAS_RENDER)
1014 sf-exg 1.108 {
1015 sf-exg 1.152 recoded_root_pmap = XCreatePixmap (dpy, vt, root_pmap_width, root_pmap_height, depth);
1016    
1017 sf-exg 1.154 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, screen));
1018 sf-exg 1.191 Picture src = XRenderCreatePicture (dpy, root_pixmap, src_format, 0, 0);
1019 sf-exg 1.108
1020 sf-exg 1.154 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, visual);
1021 sf-exg 1.191 Picture dst = XRenderCreatePicture (dpy, recoded_root_pmap, dst_format, 0, 0);
1022 sf-exg 1.103
1023 sf-exg 1.154 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, root_pmap_width, root_pmap_height);
1024 sf-exg 1.103
1025 sf-exg 1.154 XRenderFreePicture (dpy, src);
1026     XRenderFreePicture (dpy, dst);
1027 sf-exg 1.108 }
1028 sf-exg 1.103 else
1029 sf-exg 1.108 #endif
1030 sf-exg 1.151 recoded_root_pmap = None;
1031 sf-exg 1.103 }
1032    
1033 sf-exg 1.151 if (recoded_root_pmap == None)
1034 sf-exg 1.81 return 0;
1035    
1036 sf-exg 1.142 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 sf-exg 1.135 }
1046 sasha 1.1
1047 sf-exg 1.154 /* straightforward pixmap copy */
1048 mikachu 1.178 while (sx < 0) sx += root_pmap_width;
1049     while (sy < 0) sy += root_pmap_height;
1050 sf-exg 1.154
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 sf-exg 1.202 ret = true;
1061 sf-exg 1.226 bool need_blur = root_effects.need_blur ();
1062     bool need_tint = root_effects.need_tint ();
1063 sasha 1.1
1064 sf-exg 1.227 if (need_blur)
1065 sf-exg 1.150 {
1066 sf-exg 1.227 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 sf-exg 1.154 {
1081 sf-exg 1.227 /* 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 sf-exg 1.154 }
1087 sf-exg 1.227 }
1088 sf-exg 1.189
1089     XFreeGC (dpy, gc);
1090 sasha 1.1 }
1091 ayin 1.10
1092 sf-exg 1.103 if (recoded_root_pmap != root_pixmap)
1093     XFreePixmap (dpy, recoded_root_pmap);
1094    
1095 sf-exg 1.202 return ret;
1096 sasha 1.1 }
1097    
1098 sf-exg 1.98 void
1099 sf-exg 1.142 rxvt_term::bg_set_root_pixmap ()
1100 sasha 1.1 {
1101 sf-exg 1.231 Pixmap new_root_pixmap = display->get_pixmap_property (xa[XA_XROOTPMAP_ID]);
1102 sasha 1.1 if (new_root_pixmap == None)
1103 sf-exg 1.231 new_root_pixmap = display->get_pixmap_property (xa[XA_ESETROOT_PMAP_ID]);
1104 sasha 1.1
1105 sf-exg 1.98 root_pixmap = new_root_pixmap;
1106 sasha 1.1 }
1107     # endif /* ENABLE_TRANSPARENCY */
1108    
1109 sf-exg 1.234 void
1110 sf-exg 1.142 rxvt_term::bg_render ()
1111 sasha 1.1 {
1112 sf-exg 1.235 if (bg_flags & BG_INHIBIT_RENDER)
1113     return;
1114    
1115 sf-exg 1.142 bg_invalidate ();
1116 root 1.232 # if ENABLE_TRANSPARENCY
1117 sf-exg 1.143 if (bg_flags & BG_IS_TRANSPARENT)
1118 sasha 1.1 {
1119     /* we need to re-generate transparency pixmap in that case ! */
1120 sf-exg 1.228 if (render_root_image ())
1121 sf-exg 1.199 bg_flags |= BG_IS_VALID;
1122 sasha 1.1 }
1123     # endif
1124    
1125 root 1.232 # if BG_IMAGE_FROM_FILE
1126 sf-exg 1.233 if (fimage.flags & IM_IS_SET)
1127 sasha 1.1 {
1128 sf-exg 1.233 if (render_image (fimage))
1129 sf-exg 1.143 bg_flags |= BG_IS_VALID;
1130 sasha 1.1 }
1131 sf-exg 1.46 # endif
1132 sasha 1.7
1133 sf-exg 1.143 if (!(bg_flags & BG_IS_VALID))
1134 sasha 1.1 {
1135 sf-exg 1.142 if (bg_pixmap != None)
1136 sasha 1.1 {
1137 sf-exg 1.142 XFreePixmap (dpy, bg_pixmap);
1138     bg_pixmap = None;
1139 sasha 1.1 }
1140     }
1141    
1142 sf-exg 1.142 scr_recolour (false);
1143 sf-exg 1.143 bg_flags |= BG_NEEDS_REFRESH;
1144 sasha 1.1
1145 sf-exg 1.142 bg_valid_since = ev::now ();
1146 sasha 1.1 }
1147    
1148 sf-exg 1.106 void
1149 sf-exg 1.142 rxvt_term::bg_init ()
1150 sasha 1.1 {
1151 root 1.232 #if BG_IMAGE_FROM_FILE
1152 sf-exg 1.220 if (rs[Rs_backgroundPixmap])
1153     {
1154 sf-exg 1.233 if (fimage.set_file_geometry (rs[Rs_backgroundPixmap])
1155     && !bg_window_position_sensitive ())
1156     update_background ();
1157 sf-exg 1.220 }
1158     #endif
1159 sasha 1.1 }
1160    
1161 sf-exg 1.169 /* based on code from aterm-0.4.2 */
1162 sasha 1.1
1163 sf-exg 1.179 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 sf-exg 1.183 void
1176 sf-exg 1.226 rxvt_term::tint_ximage (XImage *ximage, rxvt_color &tint, bool tint_set, int shade)
1177 sasha 1.1 {
1178 sf-exg 1.209 unsigned int size_r, size_g, size_b;
1179 sasha 1.1 int sh_r, sh_g, sh_b;
1180 sf-exg 1.94 uint32_t mask_r, mask_g, mask_b;
1181     uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1182 sf-exg 1.180 unsigned short low;
1183 sf-exg 1.155 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1184 sasha 1.1
1185 sf-exg 1.126 if (visual->c_class != TrueColor || ximage->format != ZPixmap) return;
1186 sasha 1.1
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 sf-exg 1.209 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 sasha 1.1
1209 sf-exg 1.194 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1210 sf-exg 1.183
1211 sf-exg 1.226 if (tint_set)
1212 sf-exg 1.194 tint.get (c);
1213 sf-exg 1.183
1214 sasha 1.1 /* prepare limits for color transformation (each channel is handled separately) */
1215 sf-exg 1.87 if (shade > 100)
1216     {
1217 sf-exg 1.184 c.r = c.r * (200 - shade) / 100;
1218     c.g = c.g * (200 - shade) / 100;
1219     c.b = c.b * (200 - shade) / 100;
1220 sf-exg 1.125
1221 sf-exg 1.183 low = 0xffff * (shade - 100) / 100;
1222 sf-exg 1.87 }
1223     else
1224     {
1225 sf-exg 1.184 c.r = c.r * shade / 100;
1226     c.g = c.g * shade / 100;
1227     c.b = c.b * shade / 100;
1228 sasha 1.1
1229 sf-exg 1.180 low = 0;
1230 sf-exg 1.87 }
1231 sasha 1.1
1232     /* fill our lookup tables */
1233 sf-exg 1.184 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 sasha 1.1
1237     /* apply table to input image (replacing colors by newly calculated ones) */
1238 sf-exg 1.126 if (ximage->bits_per_pixel == 32
1239     && ximage->byte_order == host_byte_order)
1240 sf-exg 1.93 {
1241 sf-exg 1.208 char *line = ximage->data;
1242 sf-exg 1.93
1243 sf-exg 1.208 for (int y = 0; y < ximage->height; y++)
1244 sf-exg 1.87 {
1245 sf-exg 1.208 uint32_t *p = (uint32_t *)line;
1246     for (int x = 0; x < ximage->width; x++)
1247 sf-exg 1.87 {
1248 sf-exg 1.209 *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 sf-exg 1.208 p++;
1252 sf-exg 1.87 }
1253 sf-exg 1.208 line += ximage->bytes_per_line;
1254 sasha 1.1 }
1255 sf-exg 1.93 }
1256     else
1257     {
1258 sf-exg 1.126 for (int y = 0; y < ximage->height; y++)
1259     for (int x = 0; x < ximage->width; x++)
1260 sf-exg 1.93 {
1261 sf-exg 1.126 unsigned long pixel = XGetPixel (ximage, x, y);
1262 sf-exg 1.93 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 sf-exg 1.126 XPutPixel (ximage, x, y, pixel);
1266 sf-exg 1.93 }
1267 sasha 1.1 }
1268    
1269     free (lookup);
1270     }
1271 sf-exg 1.226
1272     #endif /* HAVE_BG_PIXMAP */