ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
(Generate patch)

Comparing rxvt-unicode/src/background.C (file contents):
Revision 1.117 by sf-exg, Fri Nov 12 22:00:05 2010 UTC vs.
Revision 1.233 by sf-exg, Wed Jun 6 22:00:20 2012 UTC

1/*----------------------------------------------------------------------* 1/*----------------------------------------------------------------------*
2 * File: background.C - former xpm.C 2 * File: background.C - former xpm.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2008 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de>
7 * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net> 7 * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net>
8 * Copyright (c) 2010 Emanuele Giaquinta <e.giaquinta@glauco.it> 8 * Copyright (c) 2010-2012 Emanuele Giaquinta <e.giaquinta@glauco.it>
9 * 9 *
10 * This program is free software; you can redistribute it and/or modify 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 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 12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. 13 * (at your option) any later version.
20 * You should have received a copy of the GNU General Public License 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 21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *---------------------------------------------------------------------*/ 23 *---------------------------------------------------------------------*/
24 24
25#include <cmath> 25#include <math.h>
26#include "../config.h" /* NECESSARY */ 26#include "../config.h" /* NECESSARY */
27#include "rxvt.h" /* NECESSARY */ 27#include "rxvt.h" /* NECESSARY */
28 28
29#if XRENDER 29#if XRENDER
30# include <X11/extensions/Xrender.h> 30# include <X11/extensions/Xrender.h>
31#endif 31#endif
32 32
33#define DO_TIMING_TEST 0 33#ifndef FilterConvolution
34 34#define FilterConvolution "convolution"
35#if DO_TIMING_TEST
36# include <sys/time.h>
37#define TIMING_TEST_START(id) \
38 struct timeval timing_test_##id##_stv; \
39 gettimeofday (&timing_test_##id##_stv, NULL);
40
41#define TIMING_TEST_PRINT_RESULT(id) \
42 do { \
43 struct timeval tv; \
44 gettimeofday (&tv, NULL); \
45 tv.tv_sec -= (timing_test_##id##_stv).tv_sec; \
46 fprintf (stderr, "%s: %s: %d: elapsed %ld usec\n", #id, __FILE__, __LINE__, \
47 tv.tv_sec * 1000000 + tv.tv_usec - (timing_test_##id##_stv).tv_usec); \
48 } while (0)
49
50#else
51#define TIMING_TEST_START(id) do {} while (0)
52#define TIMING_TEST_PRINT_RESULT(id) do {} while (0)
53#endif 35#endif
54 36
55/* 37#ifndef RepeatPad
56 * Pixmap geometry string interpretation : 38#define RepeatPad True
57 * Each geometry string contains zero or one scale/position 39#endif
58 * adjustment and may optionally be followed by a colon and one or more
59 * colon-delimited pixmap operations.
60 * The following table shows the valid geometry strings and their
61 * effects on the background image :
62 *
63 * WxH+X+Y Set scaling to W% by H%, and position to X% by Y%.
64 * W and H are percentages of the terminal window size.
65 * X and Y are also percentages; e.g., +50+50 centers
66 * the image in the window.
67 * WxH+X Assumes Y == X
68 * WxH Assumes Y == X == 50 (centers the image)
69 * W+X+Y Assumes H == W
70 * W+X Assumes H == W and Y == X
71 * W Assumes H == W and Y == X == 50
72 *
73 * Adjusting position only :
74 * =+X+Y Set position to X% by Y% (absolute).
75 * =+X Set position to X% by X%.
76 * +X+Y Adjust position horizontally X% and vertically Y%
77 * from current position (relative).
78 * +X Adjust position horizontally X% and vertically X%
79 * from current position.
80 *
81 * Adjusting scale only :
82 * Wx0 Multiply horizontal scaling factor by W%
83 * 0xH Multiply vertical scaling factor by H%
84 * 0x0 No scaling (show image at normal size).
85 *
86 * Pixmap Operations : (should be prepended by a colon)
87 * tile Tile image. Scaling/position modifiers above will affect
88 * the tile size and origin.
89 * propscale When scaling, scale proportionally. That is, maintain the
90 * proper aspect ratio for the image. Any portion of the
91 * background not covered by the image is filled with the
92 * current background color.
93 * hscale Scale horizontally, tile vertically ?
94 * vscale Tile horizontally, scale vertically ?
95 * scale Scale both up and down
96 * auto Same as 100x100+50+50
97 */
98 40
99#ifdef HAVE_BG_PIXMAP 41#ifdef HAVE_BG_PIXMAP
100bgPixmap_t::bgPixmap_t () 42# if XRENDER
43static Picture
44create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
101{ 45{
102 // this is basically redundant as bgPixmap_t is only used in 46 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
103 // zero_initialised-derived structs 47
104#ifdef HAVE_AFTERIMAGE 48 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
105 original_asim = NULL; 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}
106#endif 58# endif
107#ifdef HAVE_PIXBUF
108 pixbuf = NULL;
109#endif
110#ifdef BG_IMAGE_FROM_FILE
111 have_image = false;
112 h_scale = v_scale = 0;
113 h_align = v_align = 0;
114#endif
115#ifdef ENABLE_TRANSPARENCY
116 shade = 100;
117#endif
118 flags = 0;
119 pixmap = None;
120 valid_since = invalid_since = 0;
121 target = 0;
122}
123 59
124void 60void
125bgPixmap_t::destroy () 61rxvt_term::bg_destroy ()
126{ 62{
127#ifdef HAVE_AFTERIMAGE 63# if BG_IMAGE_FROM_FILE
128 if (original_asim) 64 fimage.destroy ();
129 safe_asimage_destroy (original_asim);
130#endif 65# endif
131 66
132#ifdef HAVE_PIXBUF 67 if (bg_pixmap)
133 if (pixbuf)
134 g_object_unref (pixbuf);
135#endif
136
137 if (pixmap && target)
138 XFreePixmap (target->dpy, pixmap); 68 XFreePixmap (dpy, bg_pixmap);
139} 69}
140 70
141bool 71bool
142bgPixmap_t::window_size_sensitive () 72rxvt_term::bg_set_position (int x, int y)
143{ 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
85bool
86rxvt_term::bg_window_size_sensitive ()
87{
144# ifdef ENABLE_TRANSPARENCY 88# if ENABLE_TRANSPARENCY
145 if (flags & isTransparent) 89 if (bg_flags & BG_IS_TRANSPARENT)
146 return true; 90 return true;
147# endif 91# endif
148 92
149# ifdef BG_IMAGE_FROM_FILE 93# if BG_IMAGE_FROM_FILE
150 if (have_image) 94 if (fimage.flags & IM_IS_SET)
151 { 95 {
152 if (flags & sizeSensitive) 96 if ((fimage.flags & IM_IS_SIZE_SENSITIVE)
97 || fimage.width () > szHint.width
98 || fimage.height () > szHint.height)
153 return true; 99 return true;
154 } 100 }
155# endif 101# endif
156 102
157 return false; 103 return false;
158} 104}
159 105
160bool 106bool
161bgPixmap_t::window_position_sensitive () 107rxvt_term::bg_window_position_sensitive ()
162{ 108{
163# ifdef ENABLE_TRANSPARENCY 109# if ENABLE_TRANSPARENCY
164 if (flags & isTransparent) 110 if (bg_flags & BG_IS_TRANSPARENT)
165 return true; 111 return true;
166# endif 112# endif
167 113
168# ifdef BG_IMAGE_FROM_FILE 114# if BG_IMAGE_FROM_FILE
169 if (have_image) 115 if (fimage.flags & IM_IS_SET)
170 { 116 {
171 if (flags & rootAlign) 117 if (fimage.flags & IM_ROOT_ALIGN)
172 return true; 118 return true;
173 } 119 }
174# endif 120# endif
175 121
176 return false; 122 return false;
177} 123}
178 124
179bool bgPixmap_t::need_client_side_rendering ()
180{
181# ifdef HAVE_AFTERIMAGE
182 if (original_asim)
183 return true;
184# endif
185 return false;
186}
187
188# ifdef BG_IMAGE_FROM_FILE 125# if BG_IMAGE_FROM_FILE
189static inline bool
190check_set_scale_value (int geom_flags, int flag, unsigned int &scale, unsigned int new_value)
191{
192 if (geom_flags & flag)
193 {
194 if (new_value > 1000)
195 new_value = 1000;
196 if (new_value != scale)
197 {
198 scale = new_value;
199 return true;
200 }
201 }
202 return false;
203}
204
205static inline bool
206check_set_align_value (int geom_flags, int flag, int &align, int new_value)
207{
208 if (geom_flags & flag)
209 {
210 if (new_value < -100)
211 new_value = -100;
212 else if (new_value > 200)
213 new_value = 200;
214 if (new_value != align)
215 {
216 align = new_value;
217 return true;
218 }
219 }
220 return false;
221}
222
223static inline int 126static inline int
224make_align_position (int align, int window_size, int image_size) 127make_align_position (int align, int window_size, int image_size)
225{ 128{
226 int diff = window_size - image_size;
227 int smaller = min (image_size, window_size);
228
229 if (align >= 0 && align <= 100) 129 if (align >= 0 && align <= 100)
230 return diff * align / 100; 130 return lerp (0, window_size - image_size, align);
231 else if (align > 100 && align <= 200) 131 else if (align > 100)
232 return ((align - 100) * smaller / 100) + window_size - smaller; 132 return lerp (window_size - image_size, window_size, align - 100);
233 else if (align >= -100 && align < 0) 133 else
234 return ((align + 100) * smaller / 100) - image_size; 134 return lerp (-image_size, 0, align + 100);
235 return 0;
236} 135}
237 136
238static inline int 137static inline int
239make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) 138make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
240{ 139{
246 src_pos = -pos; 145 src_pos = -pos;
247 dst_pos = 0; 146 dst_pos = 0;
248 dst_size += pos; 147 dst_size += pos;
249 } 148 }
250 149
251 if (dst_pos + dst_size > target_size)
252 dst_size = target_size - dst_pos; 150 min_it (dst_size, target_size - dst_pos);
253 return src_pos; 151 return src_pos;
254} 152}
255 153
154static void
155parse_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
256bool 189bool
257bgPixmap_t::set_geometry (const char *geom) 190rxvt_image::set_geometry (const char *geom, bool update)
258{ 191{
259 bool changed = false; 192 bool changed = false;
260 int geom_flags = 0; 193 int geom_flags = 0;
261 int x = 0, y = 0; 194 int x = h_align;
195 int y = v_align;
262 unsigned int w = 0, h = 0; 196 unsigned int w = h_scale;
263 unsigned int n; 197 unsigned int h = v_scale;
264 unsigned long new_flags = (flags & (~geometryFlags)); 198 uint8_t new_flags = 0;
265 const char *p;
266# define MAXLEN_GEOM 256 /* could be longer than regular geometry string */
267 199
268 if (geom == NULL) 200 if (geom == NULL)
269 return false; 201 return false;
270 202
271 char str[MAXLEN_GEOM]; 203 if (geom[0])
272
273 while (isspace(*geom)) ++geom;
274 if ((p = strchr (geom, ';')) == NULL)
275 p = strchr (geom, '\0');
276
277 n = (p - geom);
278 if (n < MAXLEN_GEOM)
279 { 204 {
280 char *ops; 205 char **arr = rxvt_strsplit (':', geom);
281 new_flags |= geometrySet;
282 206
283 memcpy (str, geom, n); 207 for (int i = 0; arr[i]; i++)
284 str[n] = '\0';
285 if (str[0] == ':')
286 ops = &str[0];
287 else if (str[0] != 'x' && str[0] != 'X' && isalpha(str[0]))
288 ops = &str[0];
289 else
290 { 208 {
291 char *tmp; 209 if (!strncasecmp (arr[i], "style=", 6))
292 ops = strchr (str, ':');
293 if (ops != NULL)
294 { 210 {
295 for (tmp = ops-1; tmp >= str && isspace(*tmp); --tmp); 211 parse_style (arr[i] + 6, x, y, w, h, new_flags);
296 *(++tmp) = '\0'; 212 geom_flags = WidthValue|HeightValue|XValue|YValue;
297 if (ops == tmp) ++ops;
298 } 213 }
299 } 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;
300 220
301 if (ops > str || ops == NULL) 221 // deprecated
302 { 222 else if (!strcasecmp (arr[i], "tile"))
303 /* we have geometry string - let's handle it prior to applying ops */
304 geom_flags = XParseGeometry (str, &x, &y, &w, &h);
305
306 if ((geom_flags & XValue) && !(geom_flags & YValue))
307 { 223 {
308 y = x; 224 new_flags |= IM_TILE;
225 w = h = noScale;
309 geom_flags |= YValue; 226 geom_flags |= WidthValue|HeightValue;
310 } 227 }
311 228 else if (!strcasecmp (arr[i], "propscale"))
312 if (flags & geometrySet)
313 { 229 {
314 /* new geometry is an adjustment to the old one ! */ 230 new_flags |= IM_KEEP_ASPECT;
231 w = h = windowScale;
315 if ((geom_flags & WidthValue) && (geom_flags & HeightValue)) 232 geom_flags |= WidthValue|HeightValue;
316 {
317 if (w == 0 && h != 0)
318 {
319 w = h_scale;
320 h = (v_scale * h) / 100;
321 }
322 else if (h == 0 && w != 0)
323 {
324 w = (h_scale * w) / 100;
325 h = v_scale;
326 }
327 }
328 if (geom_flags & XValue)
329 {
330 if (str[0] != '=')
331 {
332 y += v_align;
333 x += h_align;
334 }
335 }
336 } 233 }
337 else /* setting up geometry from scratch */ 234 else if (!strcasecmp (arr[i], "hscale"))
338 { 235 {
339 if (!(geom_flags & XValue)) 236 new_flags |= IM_TILE;
340 { 237 w = windowScale;
341 /* use default geometry - centered */
342 x = y = defaultAlign;
343 }
344 else if (!(geom_flags & YValue))
345 y = x; 238 h = noScale;
346
347 if ((geom_flags & (WidthValue|HeightValue)) == 0) 239 geom_flags |= WidthValue|HeightValue;
348 {
349 /* use default geometry - scaled */
350 w = h = defaultScale;
351 }
352 else if (geom_flags & WidthValue)
353 {
354 if (!(geom_flags & HeightValue))
355 h = w;
356 }
357 else
358 w = h;
359 } 240 }
360 } /* done parsing geometry string */ 241 else if (!strcasecmp (arr[i], "vscale"))
361 else if (!(flags & geometrySet))
362 {
363 /* default geometry - scaled and centered */
364 x = y = defaultAlign;
365 w = h = defaultScale;
366 }
367
368 if (!(flags & geometrySet))
369 geom_flags |= WidthValue|HeightValue|XValue|YValue;
370
371 if (ops)
372 {
373 while (*ops)
374 { 242 {
375 while (*ops == ':' || isspace(*ops)) ++ops; 243 new_flags |= IM_TILE;
376 244 h = windowScale;
377# define CHECK_GEOM_OPS(op_str) (strncasecmp (ops, (op_str), sizeof (op_str) - 1) == 0)
378 if (CHECK_GEOM_OPS ("tile"))
379 {
380 w = h = noScale; 245 w = noScale;
381 geom_flags |= WidthValue|HeightValue; 246 geom_flags |= WidthValue|HeightValue;
382 } 247 }
383 else if (CHECK_GEOM_OPS ("propscale")) 248 else if (!strcasecmp (arr[i], "scale"))
384 { 249 {
385 new_flags |= propScale;
386 }
387 else if (CHECK_GEOM_OPS ("hscale"))
388 {
389 if (w == 0) w = windowScale;
390
391 h = noScale; 250 w = h = windowScale;
392 geom_flags |= WidthValue|HeightValue; 251 geom_flags |= WidthValue|HeightValue;
393 } 252 }
394 else if (CHECK_GEOM_OPS ("vscale")) 253 else if (!strcasecmp (arr[i], "auto"))
395 { 254 {
396 if (h == 0) h = windowScale;
397
398 w = noScale;
399 geom_flags |= WidthValue|HeightValue;
400 }
401 else if (CHECK_GEOM_OPS ("scale"))
402 {
403 if (h == 0) h = windowScale;
404 if (w == 0) w = windowScale;
405
406 geom_flags |= WidthValue|HeightValue;
407 }
408 else if (CHECK_GEOM_OPS ("auto"))
409 {
410 w = h = windowScale; 255 w = h = windowScale;
411 x = y = centerAlign; 256 x = y = centerAlign;
412 geom_flags |= WidthValue|HeightValue|XValue|YValue; 257 geom_flags |= WidthValue|HeightValue|XValue|YValue;
413 } 258 }
414 else if (CHECK_GEOM_OPS ("root")) 259 else if (!strcasecmp (arr[i], "root"))
415 { 260 {
416 new_flags |= rootAlign; 261 new_flags |= IM_TILE|IM_ROOT_ALIGN;
417 w = h = noScale; 262 w = h = noScale;
418 geom_flags |= WidthValue|HeightValue; 263 geom_flags |= WidthValue|HeightValue;
419 } 264 }
420# undef CHECK_GEOM_OPS
421 265
422 while (*ops != ':' && *ops != '\0') ++ops; 266 else
267 geom_flags |= XParseGeometry (arr[i], &x, &y, &w, &h);
423 } /* done parsing ops */ 268 } /* done parsing ops */
424 }
425 269
426 if (check_set_scale_value (geom_flags, WidthValue, h_scale, w)) changed = true; 270 rxvt_free_strsplit (arr);
427 if (check_set_scale_value (geom_flags, HeightValue, v_scale, h)) changed = true; 271 }
428 if (check_set_align_value (geom_flags, XValue, h_align, x)) changed = true; 272
429 if (check_set_align_value (geom_flags, YValue, v_align, y)) changed = true; 273 new_flags |= flags & ~IM_GEOMETRY_FLAGS;
274
275 if (!update)
430 } 276 {
277 if (!(geom_flags & XValue))
278 x = y = defaultAlign;
279 else if (!(geom_flags & YValue))
280 y = x;
431 281
432 if (new_flags != flags) 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)
433 { 298 {
434 flags = new_flags; 299 flags = new_flags;
300 h_scale = w;
301 v_scale = h;
302 h_align = x;
303 v_align = y;
435 changed = true; 304 changed = true;
436 } 305 }
437 306
307 if (is_size_sensitive ())
308 flags |= IM_IS_SIZE_SENSITIVE;
309 else
310 flags &= ~IM_IS_SIZE_SENSITIVE;
311
438 return changed; 312 return changed;
439} 313}
440 314
441void 315void
442bgPixmap_t::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y) 316rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
443{ 317{
318 int image_width = image.width ();
319 int image_height = image.height ();
444 int target_width = target->szHint.width; 320 int target_width = szHint.width;
445 int target_height = target->szHint.height; 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);
446 324
447 if (flags & propScale) 325 w = h_scale * target_width / 100;
326 h = v_scale * target_height / 100;
327
328 if (image.flags & IM_KEEP_ASPECT)
448 { 329 {
449 float scale = (float)target_width / image_width; 330 float scale = (float)w / image_width;
450 min_it (scale, (float)target_height / image_height); 331 min_it (scale, (float)h / image_height);
451 w = image_width * scale + 0.5; 332 w = image_width * scale + 0.5;
452 h = image_height * scale + 0.5; 333 h = image_height * scale + 0.5;
453 } 334 }
454 else
455 {
456 w = h_scale * target_width / 100;
457 h = v_scale * target_height / 100;
458 }
459 335
460 if (!w) w = image_width; 336 if (!w) w = image_width;
461 if (!h) h = image_height; 337 if (!h) h = image_height;
462 338
463 if (flags & rootAlign) 339 if (image.flags & IM_ROOT_ALIGN)
464 { 340 {
465 target->get_window_origin (x, y);
466 x = -x; 341 x = -target_x;
467 y = -y; 342 y = -target_y;
468 } 343 }
469 else 344 else
470 { 345 {
471 x = make_align_position (h_align, target_width, w); 346 x = make_align_position (image.h_align, target_width, w);
472 y = make_align_position (v_align, target_height, h); 347 y = make_align_position (image.v_align, target_height, h);
473 } 348 }
474
475 flags &= ~sizeSensitive;
476 if ((flags & propScale) || h_scale || v_scale
477 || (!(flags & rootAlign) && (h_align || v_align))
478 || w > target_width || h > target_height)
479 flags |= sizeSensitive;
480} 349}
481 350
482# ifdef HAVE_AFTERIMAGE 351# if HAVE_PIXBUF
483bool 352bool
484bgPixmap_t::render_image (unsigned long background_flags) 353rxvt_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)
485{ 356{
486 if (target == NULL) 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)
487 return false; 367 return false;
488 368
489 target->init_asv (); 369 if (argb)
490
491 ASImage *background = NULL;
492 ARGB32 background_tint = TINT_LEAVE_SAME;
493
494# ifdef ENABLE_TRANSPARENCY
495 if (background_flags)
496 background = pixmap2ximage (target->asv, pixmap, 0, 0, pmap_width, pmap_height, AllPlanes, 100);
497
498 if (!(background_flags & transpPmapTinted) && (flags & tintNeeded))
499 {
500 ShadingInfo as_shade;
501 as_shade.shading = shade;
502
503 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
504 if (flags & tintSet)
505 tint.get (c);
506 as_shade.tintColor.red = c.r;
507 as_shade.tintColor.green = c.g;
508 as_shade.tintColor.blue = c.b;
509
510 background_tint = shading2tint32 (&as_shade);
511 } 370 {
512 371 red_mask = 0xff << 16;
513 if (!(background_flags & transpPmapBlurred) && (flags & blurNeeded) && background != NULL) 372 green_mask = 0xff << 8;
373 blue_mask = 0xff;
374 alpha_mask = 0xff << 24;
514 { 375 }
515 ASImage *tmp = blur_asimage_gauss (target->asv, background, h_blurRadius, v_blurRadius, 0xFFFFFFFF, 376 else
516 (original_asim == NULL || tint == TINT_LEAVE_SAME) ? ASA_XImage : ASA_ASImage, 377 {
517 100, ASIMAGE_QUALITY_DEFAULT); 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);
518 if (tmp) 383 if (format)
519 { 384 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
520 destroy_asimage (&background); 385 else
521 background = tmp; 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++)
522 } 438 {
523 } 439 unsigned char *pixel = row + x * channels;
524# endif 440 uint32_t value;
441 unsigned char r, g, b, a;
525 442
526 ASImage *result = 0; 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 }
527 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
478bool
479rxvt_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
528 int target_width = target->szHint.width; 496 int target_width = szHint.width;
529 int target_height = target->szHint.height; 497 int target_height = szHint.height;
530 int new_pmap_width = target_width; 498 int new_pmap_width = target_width;
531 int new_pmap_height = target_height; 499 int new_pmap_height = target_height;
532 500
533 int x = 0; 501 int x = 0;
534 int y = 0; 502 int y = 0;
535 int w = 0; 503 int w = 0;
536 int h = 0; 504 int h = 0;
537 505
538 if (original_asim) 506 get_image_geometry (image, w, h, x, y);
539 get_image_geometry (original_asim->width, original_asim->height, w, h, x, y);
540 507
541 if (!original_asim 508 if (!(image.flags & IM_ROOT_ALIGN)
542 || (!(flags & rootAlign)
543 && (x >= target_width
544 || y >= target_height
545 || (x + w <= 0)
546 || (y + h <= 0))))
547 {
548 if (background)
549 {
550 new_pmap_width = background->width;
551 new_pmap_height = background->height;
552 result = background;
553
554 if (background_tint != TINT_LEAVE_SAME)
555 {
556 ASImage *tmp = tile_asimage (target->asv, background, 0, 0,
557 target_width, target_height, background_tint,
558 ASA_XImage, 100, ASIMAGE_QUALITY_DEFAULT);
559 if (tmp)
560 result = tmp;
561 }
562 }
563 else
564 new_pmap_width = new_pmap_height = 0;
565 }
566 else
567 {
568 result = original_asim;
569
570 if ((w != original_asim->width)
571 || (h != original_asim->height))
572 {
573 result = scale_asimage (target->asv, original_asim,
574 w, h,
575 background ? ASA_ASImage : ASA_XImage,
576 100, ASIMAGE_QUALITY_DEFAULT);
577 }
578
579 if (background == NULL)
580 {
581 if (h_scale == 0 || v_scale == 0)
582 {
583 /* if tiling - pixmap has to be sized exactly as the image,
584 but there is no need to make it bigger than the window! */
585 new_pmap_width = min (result->width, target_width);
586 new_pmap_height = min (result->height, target_height);
587
588 /* we also need to tile our image in both directions */
589 ASImage *tmp = tile_asimage (target->asv, result,
590 (int)result->width - x,
591 (int)result->height - y,
592 new_pmap_width,
593 new_pmap_height,
594 TINT_LEAVE_SAME, ASA_XImage,
595 100, ASIMAGE_QUALITY_DEFAULT);
596 if (tmp)
597 {
598 if (result != original_asim)
599 destroy_asimage (&result);
600
601 result = tmp;
602 }
603 }
604 }
605 else
606 {
607 /* if blending background and image - pixmap has to be sized same as target window */
608 ASImageLayer *layers = create_image_layers (2);
609
610 layers[0].im = background;
611 layers[0].clip_width = target_width;
612 layers[0].clip_height = target_height;
613 layers[0].tint = background_tint;
614 layers[1].im = result;
615
616 if (h_scale == 0 || v_scale == 0)
617 {
618 /* tile horizontally */
619 while (x > 0) x -= (int)result->width;
620 layers[1].dst_x = x;
621 layers[1].clip_width = result->width+target_width;
622 }
623 else
624 {
625 /* clip horizontally */
626 layers[1].dst_x = x;
627 layers[1].clip_width = result->width;
628 }
629
630 if (h_scale == 0 || v_scale == 0)
631 {
632 while (y > 0) y -= (int)result->height;
633 layers[1].dst_y = y;
634 layers[1].clip_height = result->height + target_height;
635 }
636 else
637 {
638 layers[1].dst_y = y;
639 layers[1].clip_height = result->height;
640 }
641
642 if (target->rs[Rs_blendtype])
643 {
644 layers[1].merge_scanlines = blend_scanlines_name2func (target->rs[Rs_blendtype]);
645 if (layers[1].merge_scanlines == NULL)
646 layers[1].merge_scanlines = alphablend_scanlines;
647 }
648
649 ASImage *tmp = merge_layers (target->asv, layers, 2, target_width, target_height,
650 ASA_XImage, 0, ASIMAGE_QUALITY_DEFAULT);
651
652 if (tmp)
653 {
654 if (result != original_asim)
655 destroy_asimage (&result);
656
657 result = tmp;
658 }
659
660 free (layers);
661 }
662 }
663
664 bool ret = false;
665
666 if (result)
667 {
668 XGCValues gcv;
669 GC gc;
670
671 /* create Pixmap */
672 if (pixmap == None
673 || pmap_width != new_pmap_width
674 || pmap_height != new_pmap_height
675 || pmap_depth != target->depth)
676 {
677 if (pixmap)
678 XFreePixmap (target->dpy, pixmap);
679 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth);
680 pmap_width = new_pmap_width;
681 pmap_height = new_pmap_height;
682 pmap_depth = target->depth;
683 }
684 /* fill with background color (if result's not completely overlapping it) */
685 gcv.foreground = target->pix_colors[Color_bg];
686 gc = XCreateGC (target->dpy, target->vt, GCForeground, &gcv);
687
688 int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0;
689 int dst_width = result->width, dst_height = result->height;
690 if (background == NULL)
691 {
692 if (!(h_scale == 0 || v_scale == 0))
693 {
694 src_x = make_clip_rectangle (x, result->width , new_pmap_width , dst_x, dst_width );
695 src_y = make_clip_rectangle (y, result->height, new_pmap_height, dst_y, dst_height);
696 }
697
698 if (dst_x > 0 || dst_y > 0
699 || dst_x + dst_width < new_pmap_width
700 || dst_y + dst_height < new_pmap_height)
701 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
702 }
703
704 /* put result on pixmap */
705 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
706 asimage2drawable (target->asv, pixmap, result, gc, src_x, src_y, dst_x, dst_y, dst_width, dst_height, True);
707
708 if (result != background && result != original_asim)
709 destroy_asimage (&result);
710
711 XFreeGC (target->dpy, gc);
712
713 ret = true;
714 }
715
716 if (background)
717 destroy_asimage (&background);
718
719 return ret;
720}
721# endif /* HAVE_AFTERIMAGE */
722
723# ifdef HAVE_PIXBUF
724bool
725bgPixmap_t::render_image (unsigned long background_flags)
726{
727 if (target == NULL)
728 return false;
729
730 if (!pixbuf)
731 return false;
732
733 if (background_flags
734 && !(flags & HAS_RENDER))
735 return false;
736
737 GdkPixbuf *result;
738
739 int image_width = gdk_pixbuf_get_width (pixbuf);
740 int image_height = gdk_pixbuf_get_height (pixbuf);
741
742 int target_width = target->szHint.width;
743 int target_height = target->szHint.height;
744 int new_pmap_width = target_width;
745 int new_pmap_height = target_height;
746
747 int x = 0;
748 int y = 0;
749 int w = 0;
750 int h = 0;
751
752 get_image_geometry (image_width, image_height, w, h, x, y);
753
754 if (!(flags & rootAlign)
755 && (x >= target_width 509 && (x >= target_width
756 || y >= target_height 510 || y >= target_height
757 || (x + w <= 0) 511 || x + w <= 0
758 || (y + h <= 0))) 512 || y + h <= 0))
759 return false; 513 return false;
760 514
761 result = pixbuf; 515 result = pixbuf;
762 516
763 if ((w != image_width) 517 if (w != image_width
764 || (h != image_height)) 518 || h != image_height)
765 { 519 {
766 result = gdk_pixbuf_scale_simple (pixbuf, 520 result = gdk_pixbuf_scale_simple (pixbuf,
767 w, h, 521 w, h,
768 GDK_INTERP_BILINEAR); 522 GDK_INTERP_BILINEAR);
769 } 523 }
770 524
525 if (!result)
526 return false;
527
771 bool ret = false; 528 bool ret = false;
772 529
773 if (result)
774 {
775 XGCValues gcv; 530 XGCValues gcv;
776 GC gc; 531 GC gc;
777 Pixmap root_pmap; 532 Pixmap tmp_pixmap;
778 533
779 image_width = gdk_pixbuf_get_width (result); 534 image_width = gdk_pixbuf_get_width (result);
780 image_height = gdk_pixbuf_get_height (result); 535 image_height = gdk_pixbuf_get_height (result);
781 536
782 if (background_flags) 537 if (need_blend)
783 { 538 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
784 root_pmap = pixmap; 539 else
785 pixmap = None; 540 {
541 if (image.flags & IM_TILE)
786 } 542 {
787 else
788 {
789 if (h_scale == 0 || v_scale == 0)
790 {
791 new_pmap_width = min (image_width, target_width); 543 new_pmap_width = min (image_width, target_width);
792 new_pmap_height = min (image_height, target_height); 544 new_pmap_height = min (image_height, target_height);
793 } 545 }
794 }
795 546
796 if (pixmap == None 547 if (bg_pixmap == None
797 || pmap_width != new_pmap_width 548 || bg_pmap_width != new_pmap_width
798 || pmap_height != new_pmap_height 549 || bg_pmap_height != new_pmap_height)
799 || pmap_depth != target->depth)
800 { 550 {
801 if (pixmap) 551 if (bg_pixmap)
802 XFreePixmap (target->dpy, pixmap); 552 XFreePixmap (dpy, bg_pixmap);
803 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth); 553 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth);
804 pmap_width = new_pmap_width; 554 bg_pmap_width = new_pmap_width;
805 pmap_height = new_pmap_height; 555 bg_pmap_height = new_pmap_height;
806 pmap_depth = target->depth;
807 }
808
809 gcv.foreground = target->pix_colors[Color_bg];
810 gc = XCreateGC (target->dpy, target->vt, GCForeground, &gcv);
811
812 if (h_scale == 0 || v_scale == 0)
813 { 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 {
814 Pixmap tile = XCreatePixmap (target->dpy, target->vt, image_width, image_height, target->depth); 568 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
815 gdk_pixbuf_xlib_render_to_drawable (result, tile, gc, 569 pixbuf_to_pixmap (result, tile, gc,
816 0, 0, 570 0, 0,
817 0, 0, 571 0, 0,
818 image_width, image_height, 572 image_width, image_height, need_blend);
819 XLIB_RGB_DITHER_NONE,
820 0, 0);
821 573
822 gcv.tile = tile; 574 gcv.tile = tile;
823 gcv.fill_style = FillTiled; 575 gcv.fill_style = FillTiled;
824 gcv.ts_x_origin = x; 576 gcv.ts_x_origin = x;
825 gcv.ts_y_origin = y; 577 gcv.ts_y_origin = y;
826 XChangeGC (target->dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 578 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
827 579
828 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 580 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
829 XFreePixmap (target->dpy, tile); 581 XFreePixmap (dpy, tile);
830 } 582 }
831 else 583 else
832 { 584 {
833 int src_x, src_y, dst_x, dst_y; 585 int src_x, src_y, dst_x, dst_y;
834 int dst_width, dst_height; 586 int dst_width, dst_height;
837 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height); 589 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height);
838 590
839 if (dst_x > 0 || dst_y > 0 591 if (dst_x > 0 || dst_y > 0
840 || dst_x + dst_width < new_pmap_width 592 || dst_x + dst_width < new_pmap_width
841 || dst_y + dst_height < new_pmap_height) 593 || dst_y + dst_height < new_pmap_height)
842 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 594 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
843 595
844 if (dst_x < new_pmap_width && dst_y < new_pmap_height) 596 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
845 gdk_pixbuf_xlib_render_to_drawable (result, pixmap, gc, 597 pixbuf_to_pixmap (result, tmp_pixmap, gc,
846 src_x, src_y, 598 src_x, src_y,
847 dst_x, dst_y, 599 dst_x, dst_y,
848 dst_width, dst_height, 600 dst_width, dst_height, need_blend);
849 XLIB_RGB_DITHER_NONE,
850 0, 0);
851 } 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);
852 607
853#if XRENDER 608#if XRENDER
854 if (background_flags) 609 if (need_blend)
855 { 610 {
856 Display *dpy = target->dpy;
857 XRenderPictureAttributes pa;
858
859 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, target->visual);
860 Picture src = XRenderCreatePicture (dpy, root_pmap, src_format, 0, &pa);
861
862 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, target->visual);
863 Picture dst = XRenderCreatePicture (dpy, pixmap, dst_format, 0, &pa);
864
865 pa.repeat = True;
866 Pixmap mask_pmap = XCreatePixmap (dpy, target->vt, 1, 1, 8);
867 XRenderPictFormat *mask_format = XRenderFindStandardFormat (dpy, PictStandardA8); 611 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
612 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
613
868 Picture mask = XRenderCreatePicture (dpy, mask_pmap, mask_format, CPRepeat, &pa); 614 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0);
869 XFreePixmap (dpy, mask_pmap);
870 615
871 if (src && dst && mask) 616 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
872 { 617
618 Picture mask = create_xrender_mask (dpy, vt, False, False);
619
873 XRenderColor mask_c; 620 XRenderColor mask_c;
874 621
875 mask_c.alpha = 0x8000; 622 mask_c.alpha = image.alpha;
876 mask_c.red = 0; 623 mask_c.red =
877 mask_c.green = 0; 624 mask_c.green =
878 mask_c.blue = 0; 625 mask_c.blue = 0;
879 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); 626 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
627
880 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, target_width, target_height); 628 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, target_width, target_height);
881 }
882 629
883 XRenderFreePicture (dpy, src); 630 XRenderFreePicture (dpy, src);
884 XRenderFreePicture (dpy, dst); 631 XRenderFreePicture (dpy, dst);
885 XRenderFreePicture (dpy, mask); 632 XRenderFreePicture (dpy, mask);
886
887 XFreePixmap (dpy, root_pmap);
888 } 633 }
889#endif 634#endif
890 635
891 if (result != pixbuf)
892 g_object_unref (result);
893
894 XFreeGC (target->dpy, gc); 636 XFreeGC (dpy, gc);
895 637
896 ret = true; 638 ret = true;
897 } 639 }
898 640
641 if (result != pixbuf)
642 g_object_unref (result);
643
644 if (need_blend)
645 XFreePixmap (dpy, tmp_pixmap);
646
899 return ret; 647 return ret;
900} 648}
901# endif /* HAVE_PIXBUF */ 649# endif /* HAVE_PIXBUF */
902 650
651rxvt_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
903bool 665bool
904bgPixmap_t::set_file (const char *file) 666rxvt_image::set_file_geometry (const char *file)
905{ 667{
906 if (!file || !*file) 668 if (!file || !*file)
907 return false; 669 return false;
908 670
909 if (const char *p = strchr (file, ';')) 671 const char *p = strchr (file, ';');
672
673 if (p)
910 { 674 {
911 size_t len = p - file; 675 size_t len = p - file;
912 char *f = rxvt_temp_buf<char> (len + 1); 676 char *f = rxvt_temp_buf<char> (len + 1);
913 memcpy (f, file, len); 677 memcpy (f, file, len);
914 f[len] = '\0'; 678 f[len] = '\0';
915 file = f; 679 file = f;
916 } 680 }
917 681
918# ifdef HAVE_AFTERIMAGE 682 bool ret = set_file (file);
919 if (!target->asimman) 683 alpha = 0x8000;
920 target->asimman = create_generic_imageman (target->rs[Rs_path]); 684 if (ret)
921 ASImage *image = get_asimage (target->asimman, file, 0xFFFFFFFF, 100); 685 set_geometry (p ? p + 1 : "");
922 if (image) 686 return ret;
923 { 687}
924 if (original_asim)
925 safe_asimage_destroy (original_asim);
926 original_asim = image;
927 have_image = true;
928 return true;
929 }
930# endif
931 688
689bool
690rxvt_image::set_file (const char *file)
691{
692 bool ret = false;
693
932# ifdef HAVE_PIXBUF 694# if HAVE_PIXBUF
933 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL); 695 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
934 if (image) 696 if (image)
935 { 697 {
936 if (pixbuf) 698 if (pixbuf)
937 g_object_unref (pixbuf); 699 g_object_unref (pixbuf);
938 pixbuf = image; 700 pixbuf = image;
939 have_image = true;
940 return true; 701 ret = true;
941 } 702 }
942# endif 703# endif
943 704
705 if (ret)
706 flags |= IM_IS_SET;
707
944 return false; 708 return ret;
945} 709}
946 710
947# endif /* BG_IMAGE_FROM_FILE */ 711# endif /* BG_IMAGE_FROM_FILE */
948 712
949# ifdef ENABLE_TRANSPARENCY
950bool 713bool
951bgPixmap_t::set_transparent ()
952{
953 if (!(flags & isTransparent))
954 {
955 flags |= isTransparent;
956 return true;
957 }
958
959 return false;
960}
961
962bool
963bgPixmap_t::set_blur_radius (const char *geom) 714image_effects::set_blur (const char *geom)
964{ 715{
965 bool changed = false; 716 bool changed = false;
966 unsigned int hr, vr; 717 unsigned int hr, vr;
967 int junk; 718 int junk;
968 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr); 719 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr);
985 { 736 {
986 changed = true; 737 changed = true;
987 v_blurRadius = vr; 738 v_blurRadius = vr;
988 } 739 }
989 740
990 if (v_blurRadius == 0 && h_blurRadius == 0)
991 flags &= ~blurNeeded;
992 else
993 flags |= blurNeeded;
994
995 return changed; 741 return changed;
996} 742}
997 743
998static inline unsigned long
999compute_tint_shade_flags (rxvt_color *tint, int shade)
1000{
1001 unsigned long flags = 0;
1002 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
1003 bool has_shade = shade != 100;
1004
1005 if (tint)
1006 {
1007 tint->get (c);
1008# define IS_COMPONENT_WHOLESOME(cmp) ((cmp) <= 0x000700 || (cmp) >= 0x00f700)
1009 if (!has_shade && IS_COMPONENT_WHOLESOME (c.r)
1010 && IS_COMPONENT_WHOLESOME (c.g)
1011 && IS_COMPONENT_WHOLESOME (c.b))
1012 flags |= bgPixmap_t::tintWholesome;
1013# undef IS_COMPONENT_WHOLESOME
1014 }
1015
1016 if (has_shade)
1017 flags |= bgPixmap_t::tintNeeded;
1018 else if (tint)
1019 {
1020 if ((c.r > 0x000700 || c.g > 0x000700 || c.b > 0x000700)
1021 && (c.r < 0x00f700 || c.g < 0x00f700 || c.b < 0x00f700))
1022 {
1023 flags |= bgPixmap_t::tintNeeded;
1024 }
1025 }
1026
1027 return flags;
1028}
1029
1030bool 744bool
1031bgPixmap_t::set_tint (rxvt_color &new_tint) 745image_effects::set_tint (const rxvt_color &new_tint)
1032{ 746{
1033 if (!(flags & tintSet) || tint != new_tint) 747 if (!tint_set || tint != new_tint)
1034 { 748 {
1035 unsigned long new_flags = compute_tint_shade_flags (&new_tint, shade);
1036 tint = new_tint; 749 tint = new_tint;
1037 flags = (flags & ~tintFlags) | new_flags | tintSet; 750 tint_set = true;
751
1038 return true; 752 return true;
1039 } 753 }
1040 754
1041 return false; 755 return false;
1042} 756}
1043 757
1044bool 758bool
1045bgPixmap_t::unset_tint ()
1046{
1047 unsigned long new_flags = compute_tint_shade_flags (NULL, shade);
1048
1049 if (new_flags != (flags & tintFlags))
1050 {
1051 flags = (flags & ~tintFlags) | new_flags;
1052 return true;
1053 }
1054
1055 return false;
1056}
1057
1058bool
1059bgPixmap_t::set_shade (const char *shade_str) 759image_effects::set_shade (const char *shade_str)
1060{ 760{
1061 int new_shade = (shade_str) ? atoi (shade_str) : 100; 761 int new_shade = atoi (shade_str);
1062 762
1063 clamp_it (new_shade, -100, 200); 763 clamp_it (new_shade, -100, 200);
1064 if (new_shade < 0) 764 if (new_shade < 0)
1065 new_shade = 200 - (100 + new_shade); 765 new_shade = 200 - (100 + new_shade);
1066 766
1067 if (new_shade != shade) 767 if (new_shade != shade)
1068 { 768 {
1069 unsigned long new_flags = compute_tint_shade_flags ((flags & tintSet) ? &tint : NULL, new_shade);
1070 shade = new_shade; 769 shade = new_shade;
1071 flags = (flags & (~tintFlags | tintSet)) | new_flags;
1072 return true; 770 return true;
1073 } 771 }
1074 772
1075 return false; 773 return false;
1076} 774}
1097 params[i+2] = XDoubleToFixed (kernel[i] / sum); 795 params[i+2] = XDoubleToFixed (kernel[i] / sum);
1098} 796}
1099#endif 797#endif
1100 798
1101bool 799bool
1102bgPixmap_t::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 800rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height, bool argb, int h_blurRadius, int v_blurRadius)
1103{ 801{
1104 bool ret = false; 802 bool ret = false;
1105#if XRENDER 803#if XRENDER
804 if (!(display->flags & DISPLAY_HAS_RENDER_CONV))
805 return false;
806
1106 int size = max (h_blurRadius, v_blurRadius) * 2 + 1; 807 int size = max (h_blurRadius, v_blurRadius) * 2 + 1;
1107 double *kernel = (double *)malloc (size * sizeof (double)); 808 double *kernel = (double *)malloc (size * sizeof (double));
1108 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 809 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
1109 810
1110 Display *dpy = target->dpy;
1111 XRenderPictureAttributes pa; 811 XRenderPictureAttributes pa;
1112 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 812 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
813 : XRenderFindVisualFormat (dpy, visual);
1113 814
815 pa.repeat = RepeatPad;
1114 Picture src = XRenderCreatePicture (dpy, pixmap, format, 0, &pa); 816 Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
817 Pixmap tmp = XCreatePixmap (dpy, pixmap, width, height, depth);
1115 Picture dst = XRenderCreatePicture (dpy, pixmap, format, 0, &pa); 818 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
819 XFreePixmap (dpy, tmp);
1116 820
1117 if (kernel && params && src && dst) 821 if (kernel && params)
1118 { 822 {
1119 if (h_blurRadius)
1120 {
1121 size = h_blurRadius * 2 + 1; 823 size = h_blurRadius * 2 + 1;
1122 get_gaussian_kernel (h_blurRadius, size, kernel, params); 824 get_gaussian_kernel (h_blurRadius, size, kernel, params);
1123 825
1124 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 826 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
1125 XRenderComposite (dpy, 827 XRenderComposite (dpy,
1126 PictOpSrc, 828 PictOpSrc,
1127 src, 829 src,
1128 None, 830 None,
1129 dst, 831 dst,
1130 0, 0, 832 0, 0,
1131 0, 0, 833 0, 0,
1132 0, 0, 834 0, 0,
1133 width, height); 835 width, height);
1134 }
1135 836
1136 if (v_blurRadius) 837 ::swap (src, dst);
1137 { 838
1138 size = v_blurRadius * 2 + 1; 839 size = v_blurRadius * 2 + 1;
1139 get_gaussian_kernel (v_blurRadius, size, kernel, params); 840 get_gaussian_kernel (v_blurRadius, size, kernel, params);
1140 swap (params[0], params[1]); 841 ::swap (params[0], params[1]);
1141 842
1142 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 843 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
1143 XRenderComposite (dpy, 844 XRenderComposite (dpy,
1144 PictOpSrc, 845 PictOpSrc,
1145 src, 846 src,
1146 None, 847 None,
1147 dst, 848 dst,
1148 0, 0, 849 0, 0,
1149 0, 0, 850 0, 0,
1150 0, 0, 851 0, 0,
1151 width, height); 852 width, height);
1152 }
1153 853
1154 ret = true; 854 ret = true;
1155 } 855 }
1156 856
1157 free (kernel); 857 free (kernel);
1161#endif 861#endif
1162 return ret; 862 return ret;
1163} 863}
1164 864
1165bool 865bool
1166bgPixmap_t::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 866rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height, bool argb, rxvt_color &tint, bool tint_set, int shade)
1167{ 867{
1168 Display *dpy = target->dpy;
1169 bool ret = false; 868 bool ret = false;
1170 869
1171 if (flags & tintWholesome) 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))
1172 { 879 {
1173 XGCValues gcv; 880 XGCValues gcv;
1174 GC gc; 881 GC gc;
1175 882
1176 /* In this case we can tint image server-side getting significant 883 /* In this case we can tint image server-side getting significant
1185 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height); 892 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height);
1186 ret = true; 893 ret = true;
1187 XFreeGC (dpy, gc); 894 XFreeGC (dpy, gc);
1188 } 895 }
1189 } 896 }
1190 else
1191 {
1192# if XRENDER 897# if XRENDER
1193 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC); 898 else if (display->flags & DISPLAY_HAS_RENDER)
1194 899 {
1195 if (flags & tintSet)
1196 tint.get (c);
1197
1198 if (shade <= 100) 900 if (shade <= 100)
1199 { 901 {
1200 c.r = (c.r * shade) / 100; 902 c.r = c.r * shade / 100;
1201 c.g = (c.g * shade) / 100; 903 c.g = c.g * shade / 100;
1202 c.b = (c.b * shade) / 100; 904 c.b = c.b * shade / 100;
1203 } 905 }
1204 else 906 else
1205 { 907 {
1206 c.r = ((0xffff - c.r) * (200 - shade)) / 100; 908 c.r = c.r * (200 - shade) / 100;
1207 c.g = ((0xffff - c.g) * (200 - shade)) / 100; 909 c.g = c.g * (200 - shade) / 100;
1208 c.b = ((0xffff - c.b) * (200 - shade)) / 100; 910 c.b = c.b * (200 - shade) / 100;
1209 } 911 }
1210 912
1211 XRenderPictFormat *solid_format = XRenderFindStandardFormat (dpy, PictStandardARGB32); 913 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
1212 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 914 : XRenderFindVisualFormat (dpy, visual);
1213 XRenderPictureAttributes pa;
1214 915
1215 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, &pa); 916 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0);
1216 917
1217 pa.repeat = True; 918 Picture overlay_pic = create_xrender_mask (dpy, pixmap, True, False);
1218 919
1219 Pixmap overlay_pmap = XCreatePixmap (dpy, pixmap, 1, 1, 32); 920 Picture mask_pic = create_xrender_mask (dpy, pixmap, True, True);
1220 Picture overlay_pic = XRenderCreatePicture (dpy, overlay_pmap, solid_format, CPRepeat, &pa);
1221 XFreePixmap (dpy, overlay_pmap);
1222 921
1223 pa.component_alpha = True;
1224 Pixmap mask_pmap = XCreatePixmap (dpy, pixmap, 1, 1, 32);
1225 Picture mask_pic = XRenderCreatePicture (dpy, mask_pmap, solid_format, CPRepeat|CPComponentAlpha, &pa);
1226 XFreePixmap (dpy, mask_pmap);
1227
1228 if (mask_pic && overlay_pic && back_pic)
1229 {
1230 XRenderColor mask_c; 922 XRenderColor mask_c;
1231 923
1232 mask_c.red = mask_c.green = mask_c.blue = shade > 100 ? 0xffff : 0;
1233 mask_c.alpha = 0xffff; 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;
1234 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1); 944 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
1235 945
1236 mask_c.alpha = 0;
1237 mask_c.red = 0xffff - c.r;
1238 mask_c.green = 0xffff - c.g;
1239 mask_c.blue = 0xffff - c.b;
1240 XRenderFillRectangle (dpy, PictOpSrc, mask_pic, &mask_c, 0, 0, 1, 1);
1241 XRenderComposite (dpy, PictOpOver, overlay_pic, mask_pic, back_pic, 0, 0, 0, 0, 0, 0, width, height); 946 XRenderComposite (dpy, PictOpOver, overlay_pic, None, back_pic, 0, 0, 0, 0, 0, 0, width, height);
947 }
948
1242 ret = true; 949 ret = true;
1243 }
1244 950
1245 XRenderFreePicture (dpy, mask_pic); 951 XRenderFreePicture (dpy, mask_pic);
1246 XRenderFreePicture (dpy, overlay_pic); 952 XRenderFreePicture (dpy, overlay_pic);
1247 XRenderFreePicture (dpy, back_pic); 953 XRenderFreePicture (dpy, back_pic);
954 }
1248# endif 955# endif
1249 }
1250 956
1251 return ret; 957 return ret;
1252} 958}
1253 959
1254/* make_transparency_pixmap() 960# if ENABLE_TRANSPARENCY
961/*
1255 * Builds a pixmap of the same size as the terminal window that contains 962 * Builds a pixmap of the same size as the terminal window that contains
1256 * the tiled portion of the root pixmap that is supposed to be covered by 963 * the tiled portion of the root pixmap that is supposed to be covered by
1257 * our window. 964 * our window.
1258 */ 965 */
1259unsigned long 966bool
1260bgPixmap_t::make_transparency_pixmap () 967rxvt_term::render_root_image ()
1261{ 968{
1262 unsigned long result = 0; 969 bool ret = false;
1263
1264 if (target == NULL)
1265 return 0;
1266 970
1267 /* root dimensions may change from call to call - but Display structure should 971 /* root dimensions may change from call to call - but Display structure should
1268 * be always up-to-date, so let's use it : 972 * be always up-to-date, so let's use it :
1269 */ 973 */
1270 int screen = target->display->screen; 974 int screen = display->screen;
1271 Display *dpy = target->dpy;
1272 int root_depth = DefaultDepth (dpy, screen); 975 int root_depth = DefaultDepth (dpy, screen);
1273 int root_width = DisplayWidth (dpy, screen); 976 int root_width = DisplayWidth (dpy, screen);
1274 int root_height = DisplayHeight (dpy, screen); 977 int root_height = DisplayHeight (dpy, screen);
1275 unsigned int root_pmap_width, root_pmap_height; 978 unsigned int root_pmap_width, root_pmap_height;
1276 int window_width = target->szHint.width; 979 int window_width = szHint.width;
1277 int window_height = target->szHint.height; 980 int window_height = szHint.height;
1278 int sx, sy; 981 int sx, sy;
1279 XGCValues gcv; 982 XGCValues gcv;
1280 GC gc; 983 GC gc;
1281 984
1282 target->get_window_origin (sx, sy); 985 sx = target_x;
986 sy = target_y;
1283 987
1284 /* check if we are outside of the visible part of the virtual screen : */ 988 /* check if we are outside of the visible part of the virtual screen : */
1285 if (sx + window_width <= 0 || sy + window_height <= 0 989 if (sx + window_width <= 0 || sy + window_height <= 0
1286 || sx >= root_width || sy >= root_height) 990 || sx >= root_width || sy >= root_height)
1287 return 0; 991 return 0;
1291 { 995 {
1292 Window wdummy; 996 Window wdummy;
1293 int idummy; 997 int idummy;
1294 unsigned int udummy; 998 unsigned int udummy;
1295 999
1296 target->allowedxerror = -1; 1000 allowedxerror = -1;
1297 1001
1298 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pmap_width, &root_pmap_height, &udummy, &udummy)) 1002 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pmap_width, &root_pmap_height, &udummy, &udummy))
1299 root_pixmap = None; 1003 root_pixmap = None;
1300 1004
1301 target->allowedxerror = 0; 1005 allowedxerror = 0;
1302 } 1006 }
1303 1007
1304 Pixmap recoded_root_pmap = root_pixmap; 1008 Pixmap recoded_root_pmap = root_pixmap;
1305 1009
1306 if (root_pixmap != None && root_depth != target->depth) 1010 if (root_pixmap != None && root_depth != depth)
1307 { 1011 {
1308#if XRENDER 1012#if XRENDER
1309 if (flags & HAS_RENDER) 1013 if (display->flags & DISPLAY_HAS_RENDER)
1310 { 1014 {
1311 XRenderPictureAttributes pa; 1015 recoded_root_pmap = XCreatePixmap (dpy, vt, root_pmap_width, root_pmap_height, depth);
1312 1016
1313 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, screen)); 1017 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, screen));
1314 Picture src = XRenderCreatePicture (dpy, root_pixmap, src_format, 0, &pa); 1018 Picture src = XRenderCreatePicture (dpy, root_pixmap, src_format, 0, 0);
1315 1019
1316 recoded_root_pmap = XCreatePixmap (dpy, target->vt, root_pmap_width, root_pmap_height, target->depth);
1317 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, target->visual); 1020 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, visual);
1318 Picture dst = XRenderCreatePicture (dpy, recoded_root_pmap, dst_format, 0, &pa); 1021 Picture dst = XRenderCreatePicture (dpy, recoded_root_pmap, dst_format, 0, 0);
1319 1022
1320 if (src && dst)
1321 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, root_pmap_width, root_pmap_height); 1023 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, root_pmap_width, root_pmap_height);
1322 else
1323 {
1324 XFreePixmap (dpy, recoded_root_pmap);
1325 root_pixmap = None;
1326 }
1327 1024
1328 XRenderFreePicture (dpy, src); 1025 XRenderFreePicture (dpy, src);
1329 XRenderFreePicture (dpy, dst); 1026 XRenderFreePicture (dpy, dst);
1330 } 1027 }
1331 else 1028 else
1332#endif 1029#endif
1333 root_pixmap = None; 1030 recoded_root_pmap = None;
1334 } 1031 }
1335 1032
1336 if (root_pixmap == None) 1033 if (recoded_root_pmap == None)
1337 return 0; 1034 return 0;
1338 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);
1339 Pixmap tiled_root_pmap = XCreatePixmap (dpy, target->vt, window_width, window_height, target->depth); 1042 bg_pixmap = XCreatePixmap (dpy, vt, window_width, window_height, depth);
1340 1043 bg_pmap_width = window_width;
1341 if (tiled_root_pmap == None) /* something really bad happened - abort */ 1044 bg_pmap_height = window_height;
1342 return 0; 1045 }
1343 1046
1344 /* straightforward pixmap copy */ 1047 /* straightforward pixmap copy */
1048 while (sx < 0) sx += root_pmap_width;
1049 while (sy < 0) sy += root_pmap_height;
1050
1345 gcv.tile = recoded_root_pmap; 1051 gcv.tile = recoded_root_pmap;
1346 gcv.fill_style = FillTiled; 1052 gcv.fill_style = FillTiled;
1347
1348 while (sx < 0) sx += (int)root_width;
1349 while (sy < 0) sy += (int)root_height;
1350
1351 gcv.ts_x_origin = -sx; 1053 gcv.ts_x_origin = -sx;
1352 gcv.ts_y_origin = -sy; 1054 gcv.ts_y_origin = -sy;
1353 gc = XCreateGC (dpy, target->vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 1055 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1354 1056
1355 if (gc) 1057 if (gc)
1356 { 1058 {
1357 XFillRectangle (dpy, tiled_root_pmap, gc, 0, 0, window_width, window_height); 1059 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1358 result |= transpPmapTiled; 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
1359 XFreeGC (dpy, gc); 1089 XFreeGC (dpy, gc);
1360
1361 if (!need_client_side_rendering ())
1362 {
1363 if ((flags & blurNeeded)
1364 && (flags & HAS_RENDER_CONV))
1365 {
1366 if (blur_pixmap (tiled_root_pmap, target->visual, window_width, window_height))
1367 result |= transpPmapBlurred;
1368 }
1369 if ((flags & tintNeeded)
1370 && (flags & (tintWholesome | HAS_RENDER)))
1371 {
1372 if (tint_pixmap (tiled_root_pmap, target->visual, window_width, window_height))
1373 result |= transpPmapTinted;
1374 }
1375 } /* server side rendering completed */
1376
1377 if (pixmap)
1378 XFreePixmap (dpy, pixmap);
1379
1380 pixmap = tiled_root_pmap;
1381 pmap_width = window_width;
1382 pmap_height = window_height;
1383 pmap_depth = target->depth;
1384 } 1090 }
1385 else
1386 XFreePixmap (dpy, tiled_root_pmap);
1387 1091
1388 if (recoded_root_pmap != root_pixmap) 1092 if (recoded_root_pmap != root_pixmap)
1389 XFreePixmap (dpy, recoded_root_pmap); 1093 XFreePixmap (dpy, recoded_root_pmap);
1390 1094
1391 return result; 1095 return ret;
1392} 1096}
1393 1097
1394void 1098void
1395bgPixmap_t::set_root_pixmap () 1099rxvt_term::bg_set_root_pixmap ()
1396{ 1100{
1397 Pixmap new_root_pixmap = target->get_pixmap_property (XA_XROOTPMAP_ID); 1101 Pixmap new_root_pixmap = display->get_pixmap_property (xa[XA_XROOTPMAP_ID]);
1398 if (new_root_pixmap == None) 1102 if (new_root_pixmap == None)
1399 new_root_pixmap = target->get_pixmap_property (XA_ESETROOT_PMAP_ID); 1103 new_root_pixmap = display->get_pixmap_property (xa[XA_ESETROOT_PMAP_ID]);
1400 1104
1401 root_pixmap = new_root_pixmap; 1105 root_pixmap = new_root_pixmap;
1402} 1106}
1403# endif /* ENABLE_TRANSPARENCY */ 1107# endif /* ENABLE_TRANSPARENCY */
1404 1108
1405#if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) 1109bool
1406static void ShadeXImage(Visual *visual, XImage *srcImage, int shade, const rgba &c); 1110rxvt_term::bg_render ()
1111{
1112 bg_invalidate ();
1113# if ENABLE_TRANSPARENCY
1114 if (bg_flags & BG_IS_TRANSPARENT)
1115 {
1116 /* we need to re-generate transparency pixmap in that case ! */
1117 if (render_root_image ())
1118 bg_flags |= BG_IS_VALID;
1119 }
1407# endif 1120# endif
1408 1121
1409bool 1122# if BG_IMAGE_FROM_FILE
1410bgPixmap_t::render () 1123 if (fimage.flags & IM_IS_SET)
1411{
1412 unsigned long background_flags = 0;
1413
1414 if (target == NULL)
1415 return false;
1416
1417 invalidate ();
1418# ifdef ENABLE_TRANSPARENCY
1419 if (flags & isTransparent)
1420 { 1124 {
1421 /* we need to re-generate transparency pixmap in that case ! */ 1125 if (render_image (fimage))
1422 background_flags = make_transparency_pixmap (); 1126 bg_flags |= BG_IS_VALID;
1423 if (background_flags == 0)
1424 return false;
1425 else if ((background_flags & transpTransformations) == (flags & transpTransformations))
1426 flags = flags & ~isInvalid;
1427 } 1127 }
1428# endif 1128# endif
1429 1129
1430# ifdef BG_IMAGE_FROM_FILE 1130 if (!(bg_flags & BG_IS_VALID))
1431 if (have_image
1432 || (background_flags & transpTransformations) != (flags & transpTransformations))
1433 {
1434 if (render_image (background_flags))
1435 flags = flags & ~isInvalid;
1436 } 1131 {
1437# endif
1438
1439# if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE)
1440 XImage *result = NULL;
1441
1442 if (background_flags && (flags & isInvalid))
1443 {
1444 result = XGetImage (target->dpy, pixmap, 0, 0, pmap_width, pmap_height, AllPlanes, ZPixmap);
1445 }
1446
1447 if (result)
1448 {
1449 /* our own client-side tinting */
1450 if (!(background_flags & transpPmapTinted) && (flags & tintNeeded))
1451 {
1452 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
1453 if (flags & tintSet)
1454 tint.get (c);
1455 ShadeXImage (DefaultVisual (target->dpy, target->display->screen), result, shade, c);
1456 }
1457
1458 GC gc = XCreateGC (target->dpy, target->vt, 0UL, NULL);
1459
1460 if (gc)
1461 {
1462 XPutImage (target->dpy, pixmap, gc, result, 0, 0, 0, 0, result->width, result->height);
1463
1464 XFreeGC (target->dpy, gc);
1465 flags = flags & ~isInvalid;
1466 }
1467
1468 XDestroyImage (result);
1469 }
1470# endif
1471
1472 if (flags & isInvalid)
1473 {
1474 if (pixmap != None) 1132 if (bg_pixmap != None)
1475 { 1133 {
1476 XFreePixmap (target->dpy, pixmap); 1134 XFreePixmap (dpy, bg_pixmap);
1477 pixmap = None; 1135 bg_pixmap = None;
1478 } 1136 }
1479 } 1137 }
1480 1138
1481 apply (); 1139 scr_recolour (false);
1140 bg_flags |= BG_NEEDS_REFRESH;
1482 1141
1483 valid_since = ev::now (); 1142 bg_valid_since = ev::now ();
1484 1143
1485 return true; 1144 return true;
1486} 1145}
1487 1146
1488void 1147void
1489bgPixmap_t::set_target (rxvt_term *new_target) 1148rxvt_term::bg_init ()
1490{ 1149{
1491 target = new_target; 1150#if BG_IMAGE_FROM_FILE
1492 1151 if (rs[Rs_backgroundPixmap])
1493 flags &= ~(HAS_RENDER | HAS_RENDER_CONV);
1494#if XRENDER
1495 int major, minor;
1496 if (XRenderQueryVersion (target->dpy, &major, &minor))
1497 flags |= HAS_RENDER;
1498 XFilters *filters = XRenderQueryFilters (target->dpy, target->vt);
1499 if (filters)
1500 { 1152 {
1501 for (int i = 0; i < filters->nfilter; i++) 1153 if (fimage.set_file_geometry (rs[Rs_backgroundPixmap])
1502 if (!strcmp (filters->filter[i], FilterConvolution)) 1154 && !bg_window_position_sensitive ())
1503 flags |= HAS_RENDER_CONV; 1155 update_background ();
1504
1505 XFree (filters);
1506 } 1156 }
1507#endif 1157#endif
1508} 1158}
1509 1159
1160/* based on code from aterm-0.4.2 */
1161
1162static inline void
1163fill_lut (uint32_t *lookup, uint32_t mask, int sh, unsigned short low, unsigned short high)
1164{
1165 for (int i = 0; i <= mask >> sh; i++)
1166 {
1167 uint32_t tmp;
1168 tmp = i * high;
1169 tmp += (mask >> sh) * low;
1170 lookup[i] = (tmp / 0xffff) << sh;
1171 }
1172}
1173
1510void 1174void
1511bgPixmap_t::apply () 1175rxvt_term::tint_ximage (XImage *ximage, rxvt_color &tint, bool tint_set, int shade)
1512{ 1176{
1513 if (target == NULL) 1177 unsigned int size_r, size_g, size_b;
1514 return;
1515
1516 if (pixmap != None)
1517 {
1518 /* set target's background to pixmap */
1519# ifdef ENABLE_TRANSPARENCY
1520 if (flags & isTransparent)
1521 {
1522 XSetWindowBackgroundPixmap (target->dpy, target->parent[0], pixmap);
1523 XSetWindowBackgroundPixmap (target->dpy, target->vt, ParentRelative);
1524
1525 if (target->scrollBar.win)
1526 XSetWindowBackgroundPixmap (target->dpy, target->scrollBar.win, ParentRelative);
1527 }
1528 else
1529# endif
1530 {
1531 /* force old pixmap dereference in case it was transparent before :*/
1532 XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_border]);
1533 XSetWindowBackgroundPixmap (target->dpy, target->vt, pixmap);
1534 /* do we also need to set scrollbar's background here ? */
1535
1536 if (target->scrollBar.win)
1537 XSetWindowBackground (target->dpy, target->scrollBar.win, target->pix_colors[Color_border]);
1538 }
1539 }
1540 else
1541 {
1542 /* set target background to a pixel */
1543 XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_border]);
1544 XSetWindowBackground (target->dpy, target->vt, target->pix_colors[Color_bg]);
1545 /* do we also need to set scrollbar's background here ? */
1546 if (target->scrollBar.win)
1547 XSetWindowBackground (target->dpy, target->scrollBar.win, target->pix_colors[Color_border]);
1548 }
1549
1550 /* don't want Expose on the parent or vt. It is better to use
1551 scr_touch or we get a great deal of flicker otherwise: */
1552 XClearWindow (target->dpy, target->parent[0]);
1553
1554 if (target->scrollBar.state && target->scrollBar.win)
1555 {
1556 target->scrollBar.state = STATE_IDLE;
1557 target->scrollBar.show (0);
1558 }
1559
1560 target->want_refresh = 1;
1561 flags |= hasChanged;
1562}
1563
1564#endif /* HAVE_BG_PIXMAP */
1565
1566#if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE)
1567/* taken from aterm-0.4.2 */
1568
1569static void
1570ShadeXImage(Visual *visual, XImage *srcImage, int shade, const rgba &c)
1571{
1572 int sh_r, sh_g, sh_b; 1178 int sh_r, sh_g, sh_b;
1573 uint32_t mask_r, mask_g, mask_b; 1179 uint32_t mask_r, mask_g, mask_b;
1574 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1180 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1575 rgba low; 1181 unsigned short low;
1576 rgba high;
1577 int i;
1578 int host_byte_order = byteorder.big_endian () ? MSBFirst : LSBFirst; 1182 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1579 1183
1580 if (visual->c_class != TrueColor || srcImage->format != ZPixmap) return; 1184 if (visual->c_class != TrueColor || ximage->format != ZPixmap) return;
1581 1185
1582 /* for convenience */ 1186 /* for convenience */
1583 mask_r = visual->red_mask; 1187 mask_r = visual->red_mask;
1584 mask_g = visual->green_mask; 1188 mask_g = visual->green_mask;
1585 mask_b = visual->blue_mask; 1189 mask_b = visual->blue_mask;
1586 1190
1587 /* boring lookup table pre-initialization */ 1191 /* boring lookup table pre-initialization */
1588 switch (srcImage->depth) 1192 sh_r = ecb_ctz32 (mask_r);
1589 { 1193 sh_g = ecb_ctz32 (mask_g);
1590 case 15: 1194 sh_b = ecb_ctz32 (mask_b);
1591 if ((mask_r != 0x7c00) || 1195
1592 (mask_g != 0x03e0) || 1196 size_r = mask_r >> sh_r;
1593 (mask_b != 0x001f)) 1197 size_g = mask_g >> sh_g;
1198 size_b = mask_b >> sh_b;
1199
1200 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1594 return; 1201 return;
1595 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+32+32)); 1202
1203 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1596 lookup_r = lookup; 1204 lookup_r = lookup;
1597 lookup_g = lookup+32; 1205 lookup_g = lookup + size_r;
1598 lookup_b = lookup+32+32; 1206 lookup_b = lookup + size_r + size_g;
1599 sh_r = 10; 1207
1600 sh_g = 5; 1208 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1601 sh_b = 0; 1209
1602 break; 1210 if (tint_set)
1603 case 16: 1211 tint.get (c);
1604 if ((mask_r != 0xf800) ||
1605 (mask_g != 0x07e0) ||
1606 (mask_b != 0x001f))
1607 return;
1608 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+64+32));
1609 lookup_r = lookup;
1610 lookup_g = lookup+32;
1611 lookup_b = lookup+32+64;
1612 sh_r = 11;
1613 sh_g = 5;
1614 sh_b = 0;
1615 break;
1616 case 24:
1617 if ((mask_r != 0xff0000) ||
1618 (mask_g != 0x00ff00) ||
1619 (mask_b != 0x0000ff))
1620 return;
1621 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1622 lookup_r = lookup;
1623 lookup_g = lookup+256;
1624 lookup_b = lookup+256+256;
1625 sh_r = 16;
1626 sh_g = 8;
1627 sh_b = 0;
1628 break;
1629 case 32:
1630 if ((mask_r != 0xff0000) ||
1631 (mask_g != 0x00ff00) ||
1632 (mask_b != 0x0000ff))
1633 return;
1634 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1635 lookup_r = lookup;
1636 lookup_g = lookup+256;
1637 lookup_b = lookup+256+256;
1638 sh_r = 16;
1639 sh_g = 8;
1640 sh_b = 0;
1641 break;
1642 default:
1643 return; /* we do not support this color depth */
1644 }
1645 1212
1646 /* prepare limits for color transformation (each channel is handled separately) */ 1213 /* prepare limits for color transformation (each channel is handled separately) */
1647 if (shade > 100) 1214 if (shade > 100)
1648 { 1215 {
1649 shade = 200 - shade; 1216 c.r = c.r * (200 - shade) / 100;
1217 c.g = c.g * (200 - shade) / 100;
1218 c.b = c.b * (200 - shade) / 100;
1650 1219
1651 high.r = (65535 - c.r) * shade / 100; 1220 low = 0xffff * (shade - 100) / 100;
1652 high.g = (65535 - c.g) * shade / 100;
1653 high.b = (65535 - c.b) * shade / 100;
1654
1655 low.r = 65535 - high.r;
1656 low.g = 65535 - high.g;
1657 low.b = 65535 - high.b;
1658 } 1221 }
1659 else 1222 else
1660 { 1223 {
1661 high.r = c.r * shade / 100; 1224 c.r = c.r * shade / 100;
1662 high.g = c.g * shade / 100; 1225 c.g = c.g * shade / 100;
1663 high.b = c.b * shade / 100; 1226 c.b = c.b * shade / 100;
1664 1227
1665 low.r = low.g = low.b = 0; 1228 low = 0;
1666 } 1229 }
1667 1230
1668 /* fill our lookup tables */ 1231 /* fill our lookup tables */
1669 for (i = 0; i <= mask_r>>sh_r; i++) 1232 fill_lut (lookup_r, mask_r, sh_r, low, c.r);
1670 { 1233 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1671 uint32_t tmp; 1234 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1672 tmp = i * high.r;
1673 tmp += (mask_r>>sh_r) * low.r;
1674 lookup_r[i] = (tmp/65535)<<sh_r;
1675 }
1676 for (i = 0; i <= mask_g>>sh_g; i++)
1677 {
1678 uint32_t tmp;
1679 tmp = i * high.g;
1680 tmp += (mask_g>>sh_g) * low.g;
1681 lookup_g[i] = (tmp/65535)<<sh_g;
1682 }
1683 for (i = 0; i <= mask_b>>sh_b; i++)
1684 {
1685 uint32_t tmp;
1686 tmp = i * high.b;
1687 tmp += (mask_b>>sh_b) * low.b;
1688 lookup_b[i] = (tmp/65535)<<sh_b;
1689 }
1690 1235
1691 /* apply table to input image (replacing colors by newly calculated ones) */ 1236 /* apply table to input image (replacing colors by newly calculated ones) */
1692 if (srcImage->bits_per_pixel == 32 1237 if (ximage->bits_per_pixel == 32
1693 && (srcImage->depth == 24 || srcImage->depth == 32)
1694 && srcImage->byte_order == host_byte_order) 1238 && ximage->byte_order == host_byte_order)
1695 { 1239 {
1696 uint32_t *p1, *pf, *p, *pl; 1240 char *line = ximage->data;
1697 p1 = (uint32_t *) srcImage->data;
1698 pf = (uint32_t *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
1699 1241
1700 while (p1 < pf) 1242 for (int y = 0; y < ximage->height; y++)
1701 { 1243 {
1702 p = p1; 1244 uint32_t *p = (uint32_t *)line;
1703 pl = p1 + srcImage->width; 1245 for (int x = 0; x < ximage->width; x++)
1704 for (; p < pl; p++)
1705 { 1246 {
1706 *p = lookup_r[(*p & 0xff0000) >> 16] | 1247 *p = lookup_r[(*p & mask_r) >> sh_r] |
1707 lookup_g[(*p & 0x00ff00) >> 8] | 1248 lookup_g[(*p & mask_g) >> sh_g] |
1708 lookup_b[(*p & 0x0000ff)] | 1249 lookup_b[(*p & mask_b) >> sh_b];
1709 (*p & 0xff000000); 1250 p++;
1710 } 1251 }
1711 p1 = (uint32_t *) ((char *) p1 + srcImage->bytes_per_line); 1252 line += ximage->bytes_per_line;
1712 } 1253 }
1713 } 1254 }
1714 else 1255 else
1715 { 1256 {
1716 for (int y = 0; y < srcImage->height; y++) 1257 for (int y = 0; y < ximage->height; y++)
1717 for (int x = 0; x < srcImage->width; x++) 1258 for (int x = 0; x < ximage->width; x++)
1718 { 1259 {
1719 unsigned long pixel = XGetPixel (srcImage, x, y); 1260 unsigned long pixel = XGetPixel (ximage, x, y);
1720 pixel = lookup_r[(pixel & mask_r) >> sh_r] | 1261 pixel = lookup_r[(pixel & mask_r) >> sh_r] |
1721 lookup_g[(pixel & mask_g) >> sh_g] | 1262 lookup_g[(pixel & mask_g) >> sh_g] |
1722 lookup_b[(pixel & mask_b) >> sh_b]; 1263 lookup_b[(pixel & mask_b) >> sh_b];
1723 XPutPixel (srcImage, x, y, pixel); 1264 XPutPixel (ximage, x, y, pixel);
1724 } 1265 }
1725 } 1266 }
1726 1267
1727 free (lookup); 1268 free (lookup);
1728} 1269}
1729#endif /* defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) */ 1270
1271#endif /* HAVE_BG_PIXMAP */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines