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.70 by sf-exg, Tue Oct 5 20:22:25 2010 UTC vs.
Revision 1.122 by sf-exg, Sat Nov 20 11:30:58 2010 UTC

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 <pcg@goof.com>
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 * 9 *
9 * 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
10 * 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
11 * the Free Software Foundation; either version 2 of the License, or 12 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 13 * (at your option) any later version.
19 * 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
20 * along with this program; if not, write to the Free Software 21 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *---------------------------------------------------------------------*/ 23 *---------------------------------------------------------------------*/
23 24
25#include <cmath>
24#include "../config.h" /* NECESSARY */ 26#include "../config.h" /* NECESSARY */
25#include "rxvt.h" /* NECESSARY */ 27#include "rxvt.h" /* NECESSARY */
28
29#if XRENDER
30# include <X11/extensions/Xrender.h>
31#endif
32
33#ifndef FilterConvolution
34#define FilterConvolution "convolution"
35#endif
26 36
27#define DO_TIMING_TEST 0 37#define DO_TIMING_TEST 0
28 38
29#if DO_TIMING_TEST 39#if DO_TIMING_TEST
30# include <sys/time.h> 40# include <sys/time.h>
56 * 66 *
57 * WxH+X+Y Set scaling to W% by H%, and position to X% by Y%. 67 * WxH+X+Y Set scaling to W% by H%, and position to X% by Y%.
58 * W and H are percentages of the terminal window size. 68 * W and H are percentages of the terminal window size.
59 * X and Y are also percentages; e.g., +50+50 centers 69 * X and Y are also percentages; e.g., +50+50 centers
60 * the image in the window. 70 * the image in the window.
61 * WxH+X Assumes Y == X
62 * WxH Assumes Y == X == 50 (centers the image)
63 * W+X+Y Assumes H == W
64 * W+X Assumes H == W and Y == X
65 * W Assumes H == W and Y == X == 50
66 *
67 * Adjusting position only :
68 * =+X+Y Set position to X% by Y% (absolute).
69 * =+X Set position to X% by X%.
70 * +X+Y Adjust position horizontally X% and vertically Y%
71 * from current position (relative).
72 * +X Adjust position horizontally X% and vertically X%
73 * from current position.
74 *
75 * Adjusting scale only :
76 * Wx0 Multiply horizontal scaling factor by W%
77 * 0xH Multiply vertical scaling factor by H%
78 * 0x0 No scaling (show image at normal size).
79 * 71 *
80 * Pixmap Operations : (should be prepended by a colon) 72 * Pixmap Operations : (should be prepended by a colon)
81 * tile Tile image. Scaling/position modifiers above will affect 73 * tile Tile image. Scaling/position modifiers above will affect
82 * the tile size and origin. 74 * the tile size and origin.
83 * propscale When scaling, scale proportionally. That is, maintain the 75 * propscale When scaling, scale proportionally. That is, maintain the
104#ifdef BG_IMAGE_FROM_FILE 96#ifdef BG_IMAGE_FROM_FILE
105 have_image = false; 97 have_image = false;
106 h_scale = v_scale = 0; 98 h_scale = v_scale = 0;
107 h_align = v_align = 0; 99 h_align = v_align = 0;
108#endif 100#endif
101#ifdef ENABLE_TRANSPARENCY
102 shade = 100;
103#endif
109 flags = 0; 104 flags = 0;
110 pixmap = None; 105 pixmap = None;
111 valid_since = invalid_since = 0; 106 valid_since = invalid_since = 0;
112 target = 0; 107 target = 0;
108 target_x = target_y = 0;
113} 109}
114 110
115void 111void
116bgPixmap_t::destroy () 112bgPixmap_t::destroy ()
117{ 113{
128 if (pixmap && target) 124 if (pixmap && target)
129 XFreePixmap (target->dpy, pixmap); 125 XFreePixmap (target->dpy, pixmap);
130} 126}
131 127
132bool 128bool
129bgPixmap_t::set_position (int x, int y)
130{
131
132 if (target_x != x
133 || target_y != y)
134 {
135 target_x = x;
136 target_y = y;
137 return true;
138 }
139 return false;
140}
141
142bool
133bgPixmap_t::window_size_sensitive () 143bgPixmap_t::window_size_sensitive ()
134{ 144{
135# ifdef ENABLE_TRANSPARENCY 145# ifdef ENABLE_TRANSPARENCY
136 if (flags & isTransparent) 146 if (flags & isTransparent)
137 return true; 147 return true;
163 return true; 173 return true;
164 } 174 }
165# endif 175# endif
166 176
167 return false; 177 return false;
168}; 178}
169 179
170bool bgPixmap_t::need_client_side_rendering () 180bool bgPixmap_t::need_client_side_rendering ()
171{ 181{
172# ifdef HAVE_AFTERIMAGE 182# ifdef HAVE_AFTERIMAGE
173 if (original_asim) 183 if (original_asim)
174 return true; 184 return true;
175# endif
176# ifdef ENABLE_TRANSPARENCY
177 if (flags & isTransparent)
178 {
179# ifdef HAVE_AFTERIMAGE // can't blur without libAI anyways
180 if ((flags & blurNeeded) && !(flags & blurServerSide))
181 return true;
182# endif
183 if ((flags & tintNeeded) && !(flags & tintServerSide))
184 return true;
185 }
186# endif 185# endif
187 return false; 186 return false;
188} 187}
189 188
190# ifdef BG_IMAGE_FROM_FILE 189# ifdef BG_IMAGE_FROM_FILE
228 int diff = window_size - image_size; 227 int diff = window_size - image_size;
229 int smaller = min (image_size, window_size); 228 int smaller = min (image_size, window_size);
230 229
231 if (align >= 0 && align <= 100) 230 if (align >= 0 && align <= 100)
232 return diff * align / 100; 231 return diff * align / 100;
233 else if (align > 100 && align <= 200 ) 232 else if (align > 100 && align <= 200)
234 return ((align - 100) * smaller / 100) + window_size - smaller; 233 return ((align - 100) * smaller / 100) + window_size - smaller;
235 else if (align >= -100 && align < 0) 234 else if (align >= -100 && align < 0)
236 return ((align + 100) * smaller / 100) - image_size; 235 return ((align + 100) * smaller / 100) - image_size;
237 return 0; 236 return 0;
238} 237}
254 dst_size = target_size - dst_pos; 253 dst_size = target_size - dst_pos;
255 return src_pos; 254 return src_pos;
256} 255}
257 256
258bool 257bool
259bgPixmap_t::set_geometry (const char *geom) 258bgPixmap_t::set_geometry (const char *geom, bool update)
260{ 259{
260 bool changed = false;
261 int geom_flags = 0, changed = 0; 261 int geom_flags = 0;
262 int x = 0, y = 0; 262 int x = 0, y = 0;
263 unsigned int w = 0, h = 0; 263 unsigned int w = 0, h = 0;
264 unsigned int n; 264 unsigned int n;
265 unsigned long new_flags = (flags & (~geometryFlags)); 265 unsigned long new_flags = (flags & (~geometryFlags));
266 const char *p; 266 const char *ops;
267# define MAXLEN_GEOM 256 /* could be longer than regular geometry string */ 267# define MAXLEN_GEOM 256 /* could be longer than regular geometry string */
268 268
269 if (geom == NULL) 269 if (geom == NULL)
270 return false; 270 return false;
271 271
272 char str[MAXLEN_GEOM]; 272 char str[MAXLEN_GEOM];
273 273
274 while (isspace(*geom)) ++geom;
275 if ((p = strchr (geom, ';')) == NULL)
276 p = strchr (geom, '\0'); 274 ops = strchr (geom, ':');
277 275 if (ops == NULL)
276 n = strlen (geom);
277 else
278 n = (p - geom); 278 n = ops - geom;
279
279 if (n < MAXLEN_GEOM) 280 if (n >= MAXLEN_GEOM)
280 { 281 return false;
281 char *ops;
282 new_flags |= geometrySet;
283 282
284 memcpy (str, geom, n); 283 memcpy (str, geom, n);
285 str[n] = '\0'; 284 str[n] = '\0';
286 if (str[0] == ':') 285 rxvt_strtrim (str);
287 ops = &str[0];
288 else if (str[0] != 'x' && str[0] != 'X' && isalpha(str[0]))
289 ops = &str[0];
290 else
291 {
292 char *tmp;
293 ops = strchr (str, ':');
294 if (ops != NULL)
295 {
296 for (tmp = ops-1; tmp >= str && isspace(*tmp); --tmp);
297 *(++tmp) = '\0';
298 if (ops == tmp) ++ops;
299 }
300 }
301 286
302 if (ops > str || ops == NULL) 287 if (str[0])
303 { 288 {
304 /* we have geometry string - let's handle it prior to applying ops */ 289 /* we have geometry string - let's handle it prior to applying ops */
305 geom_flags = XParseGeometry (str, &x, &y, &w, &h); 290 geom_flags = XParseGeometry (str, &x, &y, &w, &h);
306
307 if ((geom_flags & XValue) && !(geom_flags & YValue))
308 {
309 y = x;
310 geom_flags |= YValue;
311 }
312
313 if (flags & geometrySet)
314 {
315 /* new geometry is an adjustment to the old one ! */
316 if ((geom_flags & WidthValue) && (geom_flags & HeightValue))
317 {
318 if (w == 0 && h != 0)
319 {
320 w = h_scale;
321 h = (v_scale * h) / 100;
322 }
323 else if (h == 0 && w != 0)
324 {
325 w = (h_scale * w) / 100;
326 h = v_scale;
327 }
328 }
329 if (geom_flags & XValue)
330 {
331 if (str[0] != '=')
332 {
333 y += v_align;
334 x += h_align;
335 }
336 }
337 }
338 else /* setting up geometry from scratch */
339 {
340 if (!(geom_flags & XValue))
341 {
342 /* use default geometry - centered */
343 x = y = defaultAlign;
344 }
345 else if (!(geom_flags & YValue))
346 y = x;
347
348 if ((geom_flags & (WidthValue|HeightValue)) == 0)
349 {
350 /* use default geometry - scaled */
351 w = h = defaultScale;
352 }
353 else if (geom_flags & WidthValue)
354 {
355 if (!(geom_flags & HeightValue))
356 h = w;
357 }
358 else
359 w = h;
360 }
361 } /* done parsing geometry string */ 291 } /* done parsing geometry string */
362 else if (!(flags & geometrySet)) 292
363 { 293 if (!update)
364 /* default geometry - scaled and centered */ 294 {
295 if (!(geom_flags & XValue))
365 x = y = defaultAlign; 296 x = y = defaultAlign;
297 else if (!(geom_flags & YValue))
298 y = x;
299
300 if (!(geom_flags & (WidthValue|HeightValue)))
366 w = h = defaultScale; 301 w = h = defaultScale;
367 } 302 else if (!(geom_flags & HeightValue))
303 h = w;
304 else if (!(geom_flags & WidthValue))
305 w = h;
368 306
369 if (!(flags & geometrySet))
370 geom_flags |= WidthValue|HeightValue|XValue|YValue; 307 geom_flags |= WidthValue|HeightValue|XValue|YValue;
308 }
371 309
372 if (ops) 310 if (ops)
373 { 311 {
374 while (*ops) 312 while (*ops)
375 { 313 {
376 while (*ops == ':' || isspace(*ops)) ++ops; 314 while (*ops == ':' || isspace(*ops)) ++ops;
377 315
378# define CHECK_GEOM_OPS(op_str) (strncasecmp (ops, (op_str), sizeof (op_str) - 1) == 0) 316# define CHECK_GEOM_OPS(op_str) (strncasecmp (ops, (op_str), sizeof (op_str) - 1) == 0)
379 if (CHECK_GEOM_OPS ("tile")) 317 if (CHECK_GEOM_OPS ("tile"))
380 { 318 {
381 w = h = noScale; 319 w = h = noScale;
382 geom_flags |= WidthValue|HeightValue; 320 geom_flags |= WidthValue|HeightValue;
383 } 321 }
384 else if (CHECK_GEOM_OPS ("propscale")) 322 else if (CHECK_GEOM_OPS ("propscale"))
385 { 323 {
386 new_flags |= propScale; 324 new_flags |= propScale;
387 } 325 }
388 else if (CHECK_GEOM_OPS ("hscale")) 326 else if (CHECK_GEOM_OPS ("hscale"))
389 { 327 {
390 if (w == 0) w = windowScale; 328 if (w == 0) w = windowScale;
391 329
392 h = noScale; 330 h = noScale;
393 geom_flags |= WidthValue|HeightValue; 331 geom_flags |= WidthValue|HeightValue;
394 } 332 }
395 else if (CHECK_GEOM_OPS ("vscale")) 333 else if (CHECK_GEOM_OPS ("vscale"))
396 { 334 {
397 if (h == 0) h = windowScale; 335 if (h == 0) h = windowScale;
398 336
399 w = noScale; 337 w = noScale;
400 geom_flags |= WidthValue|HeightValue; 338 geom_flags |= WidthValue|HeightValue;
401 } 339 }
402 else if (CHECK_GEOM_OPS ("scale")) 340 else if (CHECK_GEOM_OPS ("scale"))
403 { 341 {
404 if (h == 0) h = windowScale; 342 if (h == 0) h = windowScale;
405 if (w == 0) w = windowScale; 343 if (w == 0) w = windowScale;
406 344
407 geom_flags |= WidthValue|HeightValue; 345 geom_flags |= WidthValue|HeightValue;
408 } 346 }
409 else if (CHECK_GEOM_OPS ("auto")) 347 else if (CHECK_GEOM_OPS ("auto"))
410 { 348 {
411 w = h = windowScale; 349 w = h = windowScale;
412 x = y = centerAlign; 350 x = y = centerAlign;
413 geom_flags |= WidthValue|HeightValue|XValue|YValue; 351 geom_flags |= WidthValue|HeightValue|XValue|YValue;
414 } 352 }
415 else if (CHECK_GEOM_OPS ("root")) 353 else if (CHECK_GEOM_OPS ("root"))
416 { 354 {
417 new_flags |= rootAlign; 355 new_flags |= rootAlign;
418 w = h = noScale; 356 w = h = noScale;
419 geom_flags |= WidthValue|HeightValue; 357 geom_flags |= WidthValue|HeightValue;
420 } 358 }
421# undef CHECK_GEOM_OPS 359# undef CHECK_GEOM_OPS
422 360
423 while (*ops != ':' && *ops != '\0') ++ops; 361 while (*ops != ':' && *ops != '\0') ++ops;
424 } /* done parsing ops */ 362 } /* done parsing ops */
425 } 363 }
426 364
427 if (check_set_scale_value (geom_flags, WidthValue, h_scale, w)) ++changed; 365 if (check_set_scale_value (geom_flags, WidthValue, h_scale, w)) changed = true;
428 if (check_set_scale_value (geom_flags, HeightValue, v_scale, h)) ++changed; 366 if (check_set_scale_value (geom_flags, HeightValue, v_scale, h)) changed = true;
429 if (check_set_align_value (geom_flags, XValue, h_align, x)) ++changed; 367 if (check_set_align_value (geom_flags, XValue, h_align, x)) changed = true;
430 if (check_set_align_value (geom_flags, YValue, v_align, y)) ++changed; 368 if (check_set_align_value (geom_flags, YValue, v_align, y)) changed = true;
431 }
432 369
433 if (new_flags != flags) 370 if (new_flags != flags)
434 { 371 {
435 flags = new_flags; 372 flags = new_flags;
436 changed++; 373 changed = true;
437 } 374 }
438 375
439 //fprintf (stderr, "flags = %lX, scale = %ux%u, align=%+d%+d\n",
440 // flags, h_scale, v_scale, h_align, v_align);
441 return (changed > 0); 376 return changed;
442} 377}
443 378
444void 379void
445bgPixmap_t::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y) 380bgPixmap_t::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y)
446{ 381{
463 if (!w) w = image_width; 398 if (!w) w = image_width;
464 if (!h) h = image_height; 399 if (!h) h = image_height;
465 400
466 if (flags & rootAlign) 401 if (flags & rootAlign)
467 { 402 {
468 target->get_window_origin (x, y);
469 x = -x; 403 x = -target_x;
470 y = -y; 404 y = -target_y;
471 } 405 }
472 else 406 else
473 { 407 {
474 x = make_align_position (h_align, target_width, w); 408 x = make_align_position (h_align, target_width, w);
475 y = make_align_position (v_align, target_height, h); 409 y = make_align_position (v_align, target_height, h);
476 } 410 }
477 411
478 flags &= ~sizeSensitive; 412 flags &= ~sizeSensitive;
479 if (h_scale != 0 || v_scale != 0 413 if ((flags & propScale) || h_scale || v_scale
480 || h_align != 0 || v_align != 0 414 || (!(flags & rootAlign) && (h_align || v_align))
481 || w > target_width || h > target_height) 415 || w > target_width || h > target_height)
482 flags |= sizeSensitive; 416 flags |= sizeSensitive;
483} 417}
484 418
485# ifdef HAVE_AFTERIMAGE 419# ifdef HAVE_AFTERIMAGE
499 background = pixmap2ximage (target->asv, pixmap, 0, 0, pmap_width, pmap_height, AllPlanes, 100); 433 background = pixmap2ximage (target->asv, pixmap, 0, 0, pmap_width, pmap_height, AllPlanes, 100);
500 434
501 if (!(background_flags & transpPmapTinted) && (flags & tintNeeded)) 435 if (!(background_flags & transpPmapTinted) && (flags & tintNeeded))
502 { 436 {
503 ShadingInfo as_shade; 437 ShadingInfo as_shade;
504 as_shade.shading = (shade == 0) ? 100 : shade; 438 as_shade.shading = shade;
505 439
506 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC); 440 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
507 if (flags & tintSet) 441 if (flags & tintSet)
508 tint.get (c); 442 tint.get (c);
509 as_shade.tintColor.red = c.r; 443 as_shade.tintColor.red = c.r;
511 as_shade.tintColor.blue = c.b; 445 as_shade.tintColor.blue = c.b;
512 446
513 background_tint = shading2tint32 (&as_shade); 447 background_tint = shading2tint32 (&as_shade);
514 } 448 }
515 449
516 if (!(background_flags & transpPmapBlured) && (flags & blurNeeded) && background != NULL) 450 if (!(background_flags & transpPmapBlurred) && (flags & blurNeeded) && background != NULL)
517 { 451 {
518 ASImage *tmp = blur_asimage_gauss (target->asv, background, h_blurRadius, v_blurRadius, 0xFFFFFFFF, 452 ASImage *tmp = blur_asimage_gauss (target->asv, background, h_blurRadius, v_blurRadius, 0xFFFFFFFF,
519 (original_asim == NULL || tint == TINT_LEAVE_SAME) ? ASA_XImage : ASA_ASImage, 453 (original_asim == NULL || tint == TINT_LEAVE_SAME) ? ASA_XImage : ASA_ASImage,
520 100, ASIMAGE_QUALITY_DEFAULT); 454 100, ASIMAGE_QUALITY_DEFAULT);
521 if (tmp) 455 if (tmp)
535 469
536 int x = 0; 470 int x = 0;
537 int y = 0; 471 int y = 0;
538 int w = 0; 472 int w = 0;
539 int h = 0; 473 int h = 0;
540
541 TIMING_TEST_START (asim);
542 474
543 if (original_asim) 475 if (original_asim)
544 get_image_geometry (original_asim->width, original_asim->height, w, h, x, y); 476 get_image_geometry (original_asim->width, original_asim->height, w, h, x, y);
545 477
546 if (!original_asim 478 if (!original_asim
616 layers[0].clip_width = target_width; 548 layers[0].clip_width = target_width;
617 layers[0].clip_height = target_height; 549 layers[0].clip_height = target_height;
618 layers[0].tint = background_tint; 550 layers[0].tint = background_tint;
619 layers[1].im = result; 551 layers[1].im = result;
620 552
621 if (w <= 0) 553 if (h_scale == 0 || v_scale == 0)
622 { 554 {
623 /* tile horizontally */ 555 /* tile horizontally */
624 while (x > 0) x -= (int)result->width; 556 while (x > 0) x -= (int)result->width;
625 layers[1].dst_x = x; 557 layers[1].dst_x = x;
626 layers[1].clip_width = result->width+target_width; 558 layers[1].clip_width = result->width+target_width;
630 /* clip horizontally */ 562 /* clip horizontally */
631 layers[1].dst_x = x; 563 layers[1].dst_x = x;
632 layers[1].clip_width = result->width; 564 layers[1].clip_width = result->width;
633 } 565 }
634 566
635 if (h <= 0) 567 if (h_scale == 0 || v_scale == 0)
636 { 568 {
637 while (y > 0) y -= (int)result->height; 569 while (y > 0) y -= (int)result->height;
638 layers[1].dst_y = y; 570 layers[1].dst_y = y;
639 layers[1].clip_height = result->height + target_height; 571 layers[1].clip_height = result->height + target_height;
640 } 572 }
663 } 595 }
664 596
665 free (layers); 597 free (layers);
666 } 598 }
667 } 599 }
668 TIMING_TEST_PRINT_RESULT (asim);
669 600
670 bool ret = false; 601 bool ret = false;
671 602
672 if (result) 603 if (result)
673 { 604 {
674 XGCValues gcv; 605 XGCValues gcv;
675 GC gc; 606 GC gc;
676 607
677 if (pixmap)
678 {
679 if (pmap_width != new_pmap_width
680 || pmap_height != new_pmap_height
681 || pmap_depth != target->depth)
682 {
683 XFreePixmap (target->dpy, pixmap);
684 pixmap = None;
685 }
686 }
687
688 /* create Pixmap */ 608 /* create Pixmap */
689 if (pixmap == None) 609 if (pixmap == None
610 || pmap_width != new_pmap_width
611 || pmap_height != new_pmap_height
612 || pmap_depth != target->depth)
690 { 613 {
614 if (pixmap)
615 XFreePixmap (target->dpy, pixmap);
691 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth); 616 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth);
692 pmap_width = new_pmap_width; 617 pmap_width = new_pmap_width;
693 pmap_height = new_pmap_height; 618 pmap_height = new_pmap_height;
694 pmap_depth = target->depth; 619 pmap_depth = target->depth;
695 } 620 }
719 644
720 if (result != background && result != original_asim) 645 if (result != background && result != original_asim)
721 destroy_asimage (&result); 646 destroy_asimage (&result);
722 647
723 XFreeGC (target->dpy, gc); 648 XFreeGC (target->dpy, gc);
724 TIMING_TEST_PRINT_RESULT (asim);
725 649
726 ret = true; 650 ret = true;
727 } 651 }
728 652
729 if (background) 653 if (background)
741 return false; 665 return false;
742 666
743 if (!pixbuf) 667 if (!pixbuf)
744 return false; 668 return false;
745 669
746 // TODO: add alpha blending
747 if (background_flags) 670 if (background_flags
671 && !(flags & HAS_RENDER))
748 return false; 672 return false;
749 673
750 GdkPixbuf *result; 674 GdkPixbuf *result;
751 675
752 int image_width = gdk_pixbuf_get_width (pixbuf); 676 int image_width = gdk_pixbuf_get_width (pixbuf);
785 709
786 if (result) 710 if (result)
787 { 711 {
788 XGCValues gcv; 712 XGCValues gcv;
789 GC gc; 713 GC gc;
714 Pixmap root_pmap;
790 715
791 image_width = gdk_pixbuf_get_width (result); 716 image_width = gdk_pixbuf_get_width (result);
792 image_height = gdk_pixbuf_get_height (result); 717 image_height = gdk_pixbuf_get_height (result);
793 718
719 if (background_flags)
720 {
721 root_pmap = pixmap;
722 pixmap = None;
723 }
724 else
725 {
794 if (h_scale == 0 || v_scale == 0) 726 if (h_scale == 0 || v_scale == 0)
795 { 727 {
796 new_pmap_width = min (image_width, target_width); 728 new_pmap_width = min (image_width, target_width);
797 new_pmap_height = min (image_height, target_height); 729 new_pmap_height = min (image_height, target_height);
798 }
799
800 if (pixmap)
801 {
802 if (pmap_width != new_pmap_width
803 || pmap_height != new_pmap_height
804 || pmap_depth != target->depth)
805 { 730 }
806 XFreePixmap (target->dpy, pixmap);
807 pixmap = None;
808 } 731 }
809 }
810 732
811 if (pixmap == None) 733 if (pixmap == None
734 || pmap_width != new_pmap_width
735 || pmap_height != new_pmap_height
736 || pmap_depth != target->depth)
812 { 737 {
738 if (pixmap)
739 XFreePixmap (target->dpy, pixmap);
813 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth); 740 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth);
814 pmap_width = new_pmap_width; 741 pmap_width = new_pmap_width;
815 pmap_height = new_pmap_height; 742 pmap_height = new_pmap_height;
816 pmap_depth = target->depth; 743 pmap_depth = target->depth;
817 } 744 }
858 dst_width, dst_height, 785 dst_width, dst_height,
859 XLIB_RGB_DITHER_NONE, 786 XLIB_RGB_DITHER_NONE,
860 0, 0); 787 0, 0);
861 } 788 }
862 789
790#if XRENDER
791 if (background_flags)
792 {
793 Display *dpy = target->dpy;
794 XRenderPictureAttributes pa;
795
796 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, target->visual);
797 Picture src = XRenderCreatePicture (dpy, root_pmap, src_format, 0, &pa);
798
799 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, target->visual);
800 Picture dst = XRenderCreatePicture (dpy, pixmap, dst_format, 0, &pa);
801
802 pa.repeat = True;
803 Pixmap mask_pmap = XCreatePixmap (dpy, target->vt, 1, 1, 8);
804 XRenderPictFormat *mask_format = XRenderFindStandardFormat (dpy, PictStandardA8);
805 Picture mask = XRenderCreatePicture (dpy, mask_pmap, mask_format, CPRepeat, &pa);
806 XFreePixmap (dpy, mask_pmap);
807
808 if (src && dst && mask)
809 {
810 XRenderColor mask_c;
811
812 mask_c.alpha = 0x8000;
813 mask_c.red = 0;
814 mask_c.green = 0;
815 mask_c.blue = 0;
816 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
817 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, target_width, target_height);
818 }
819
820 XRenderFreePicture (dpy, src);
821 XRenderFreePicture (dpy, dst);
822 XRenderFreePicture (dpy, mask);
823
824 XFreePixmap (dpy, root_pmap);
825 }
826#endif
827
863 if (result != pixbuf) 828 if (result != pixbuf)
864 g_object_unref (result); 829 g_object_unref (result);
865 830
866 XFreeGC (target->dpy, gc); 831 XFreeGC (target->dpy, gc);
867 832
873# endif /* HAVE_PIXBUF */ 838# endif /* HAVE_PIXBUF */
874 839
875bool 840bool
876bgPixmap_t::set_file (const char *file) 841bgPixmap_t::set_file (const char *file)
877{ 842{
878 assert (file); 843 if (!file || !*file)
844 return false;
879 845
880 if (*file)
881 {
882 if (const char *p = strchr (file, ';')) 846 if (const char *p = strchr (file, ';'))
883 { 847 {
884 size_t len = p - file; 848 size_t len = p - file;
885 char *f = rxvt_temp_buf<char> (len + 1); 849 char *f = rxvt_temp_buf<char> (len + 1);
886 memcpy (f, file, len); 850 memcpy (f, file, len);
887 f[len] = '\0'; 851 f[len] = '\0';
888 file = f; 852 file = f;
889 } 853 }
890 854
891# ifdef HAVE_AFTERIMAGE 855# ifdef HAVE_AFTERIMAGE
892 if (!target->asimman) 856 if (!target->asimman)
893 target->asimman = create_generic_imageman (target->rs[Rs_path]); 857 target->asimman = create_generic_imageman (target->rs[Rs_path]);
894 original_asim = get_asimage (target->asimman, file, 0xFFFFFFFF, 100); 858 ASImage *image = get_asimage (target->asimman, file, 0xFFFFFFFF, 100);
859 if (image)
860 {
895 if (original_asim) 861 if (original_asim)
862 safe_asimage_destroy (original_asim);
863 original_asim = image;
896 have_image = true; 864 have_image = true;
897 return have_image; 865 return true;
866 }
898# endif 867# endif
899 868
900# ifdef HAVE_PIXBUF 869# ifdef HAVE_PIXBUF
901 pixbuf = gdk_pixbuf_new_from_file (file, NULL); 870 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
871 if (image)
872 {
902 if (pixbuf) 873 if (pixbuf)
874 g_object_unref (pixbuf);
875 pixbuf = image;
903 have_image = true; 876 have_image = true;
904 return have_image; 877 return true;
878 }
905# endif 879# endif
906 }
907 880
908 return false; 881 return false;
909} 882}
910 883
911# endif /* BG_IMAGE_FROM_FILE */ 884# endif /* BG_IMAGE_FROM_FILE */
924} 897}
925 898
926bool 899bool
927bgPixmap_t::set_blur_radius (const char *geom) 900bgPixmap_t::set_blur_radius (const char *geom)
928{ 901{
929 int changed = 0; 902 bool changed = false;
930 unsigned int hr, vr; 903 unsigned int hr, vr;
931 int junk; 904 int junk;
932 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr); 905 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr);
933 906
934 if (!(geom_flags & WidthValue)) 907 if (!(geom_flags & WidthValue))
935 hr = 1; 908 hr = 1;
936 if (!(geom_flags & HeightValue)) 909 if (!(geom_flags & HeightValue))
937 vr = hr; 910 vr = hr;
938 911
912 min_it (hr, 128);
913 min_it (vr, 128);
914
939 if (h_blurRadius != hr) 915 if (h_blurRadius != hr)
940 { 916 {
941 ++changed; 917 changed = true;
942 h_blurRadius = hr; 918 h_blurRadius = hr;
943 } 919 }
944 920
945 if (v_blurRadius != vr) 921 if (v_blurRadius != vr)
946 { 922 {
947 ++changed; 923 changed = true;
948 v_blurRadius = vr; 924 v_blurRadius = vr;
949 } 925 }
950 926
951 if (v_blurRadius == 0 && h_blurRadius == 0) 927 if (v_blurRadius == 0 && h_blurRadius == 0)
952 flags &= ~blurNeeded; 928 flags &= ~blurNeeded;
953 else 929 else
954 flags |= blurNeeded; 930 flags |= blurNeeded;
955 931
956 return (changed > 0); 932 return changed;
957} 933}
958 934
959static inline unsigned long 935static inline unsigned long
960compute_tint_shade_flags (rxvt_color *tint, int shade) 936compute_tint_shade_flags (rxvt_color *tint, int shade)
961{ 937{
962 unsigned long flags = 0; 938 unsigned long flags = 0;
963 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC); 939 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
964 bool has_shade = (shade > 0 && shade < 100) || (shade > 100 && shade < 200); 940 bool has_shade = shade != 100;
965 941
966 if (tint) 942 if (tint)
967 { 943 {
968 tint->get (c); 944 tint->get (c);
969# define IS_COMPONENT_WHOLESOME(cmp) ((cmp) <= 0x000700 || (cmp) >= 0x00f700) 945# define IS_COMPONENT_WHOLESOME(cmp) ((cmp) <= 0x000700 || (cmp) >= 0x00f700)
946 if (!has_shade
970 if (!has_shade && IS_COMPONENT_WHOLESOME (c.r) 947 && IS_COMPONENT_WHOLESOME (c.r)
971 && IS_COMPONENT_WHOLESOME (c.g) 948 && IS_COMPONENT_WHOLESOME (c.g)
972 && IS_COMPONENT_WHOLESOME (c.b)) 949 && IS_COMPONENT_WHOLESOME (c.b))
973 flags |= bgPixmap_t::tintWholesome; 950 flags |= bgPixmap_t::tintWholesome;
974# undef IS_COMPONENT_WHOLESOME 951# undef IS_COMPONENT_WHOLESOME
975 } 952 }
983 { 960 {
984 flags |= bgPixmap_t::tintNeeded; 961 flags |= bgPixmap_t::tintNeeded;
985 } 962 }
986 } 963 }
987 964
988 if (flags & bgPixmap_t::tintNeeded)
989 {
990 if (flags & bgPixmap_t::tintWholesome)
991 flags |= bgPixmap_t::tintServerSide;
992 else
993 {
994#if XFT
995 flags |= bgPixmap_t::tintServerSide;
996#endif
997 }
998 }
999
1000 return flags; 965 return flags;
1001} 966}
1002 967
1003bool 968bool
1004bgPixmap_t::set_tint (rxvt_color &new_tint) 969bgPixmap_t::set_tint (rxvt_color &new_tint)
1005{ 970{
1006 if (tint != new_tint) 971 if (!(flags & tintSet) || tint != new_tint)
1007 { 972 {
1008 unsigned long new_flags = compute_tint_shade_flags (&new_tint, shade); 973 unsigned long new_flags = compute_tint_shade_flags (&new_tint, shade);
1009 tint = new_tint; 974 tint = new_tint;
1010 flags = (flags & ~tintFlags) | new_flags | tintSet; 975 flags = (flags & ~tintFlags) | new_flags | tintSet;
1011 return true; 976 return true;
1029} 994}
1030 995
1031bool 996bool
1032bgPixmap_t::set_shade (const char *shade_str) 997bgPixmap_t::set_shade (const char *shade_str)
1033{ 998{
1034 int new_shade = (shade_str) ? atoi (shade_str) : 0; 999 int new_shade = (shade_str) ? atoi (shade_str) : 100;
1035 1000
1036 if (new_shade < 0 && new_shade > -100) 1001 clamp_it (new_shade, -100, 200);
1002 if (new_shade < 0)
1037 new_shade = 200 - (100 + new_shade); 1003 new_shade = 200 - (100 + new_shade);
1038 else if (new_shade == 100)
1039 new_shade = 0;
1040 1004
1041 if (new_shade != shade) 1005 if (new_shade != shade)
1042 { 1006 {
1043 unsigned long new_flags = compute_tint_shade_flags ((flags & tintSet) ? &tint : NULL, new_shade); 1007 unsigned long new_flags = compute_tint_shade_flags ((flags & tintSet) ? &tint : NULL, new_shade);
1044 shade = new_shade; 1008 shade = new_shade;
1047 } 1011 }
1048 1012
1049 return false; 1013 return false;
1050} 1014}
1051 1015
1016#if XRENDER
1017static void
1018get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
1019{
1020 double sigma = radius / 2.0;
1021 double scale = sqrt (2.0 * M_PI) * sigma;
1022 double sum = 0.0;
1023
1024 for (int i = 0; i < width; i++)
1025 {
1026 double x = i - width / 2;
1027 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
1028 sum += kernel[i];
1029 }
1030
1031 params[0] = XDoubleToFixed (width);
1032 params[1] = XDoubleToFixed (1);
1033
1034 for (int i = 0; i < width; i++)
1035 params[i+2] = XDoubleToFixed (kernel[i] / sum);
1036}
1037#endif
1038
1052bool 1039bool
1040bgPixmap_t::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height)
1041{
1042 bool ret = false;
1043#if XRENDER
1044 int size = max (h_blurRadius, v_blurRadius) * 2 + 1;
1045 double *kernel = (double *)malloc (size * sizeof (double));
1046 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
1047
1048 Display *dpy = target->dpy;
1049 XRenderPictureAttributes pa;
1050 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
1051
1052 Picture src = XRenderCreatePicture (dpy, pixmap, format, 0, &pa);
1053 Picture dst = XRenderCreatePicture (dpy, pixmap, format, 0, &pa);
1054
1055 if (kernel && params && src && dst)
1056 {
1057 if (h_blurRadius)
1058 {
1059 size = h_blurRadius * 2 + 1;
1060 get_gaussian_kernel (h_blurRadius, size, kernel, params);
1061
1062 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
1063 XRenderComposite (dpy,
1064 PictOpSrc,
1065 src,
1066 None,
1067 dst,
1068 0, 0,
1069 0, 0,
1070 0, 0,
1071 width, height);
1072 }
1073
1074 if (v_blurRadius)
1075 {
1076 size = v_blurRadius * 2 + 1;
1077 get_gaussian_kernel (v_blurRadius, size, kernel, params);
1078 swap (params[0], params[1]);
1079
1080 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
1081 XRenderComposite (dpy,
1082 PictOpSrc,
1083 src,
1084 None,
1085 dst,
1086 0, 0,
1087 0, 0,
1088 0, 0,
1089 width, height);
1090 }
1091
1092 ret = true;
1093 }
1094
1095 free (kernel);
1096 free (params);
1097 XRenderFreePicture (dpy, src);
1098 XRenderFreePicture (dpy, dst);
1099#endif
1100 return ret;
1101}
1102
1103bool
1053bgPixmap_t::tint_pixmap (Pixmap pixmap, Window root, int width, int height) 1104bgPixmap_t::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height)
1054{ 1105{
1055 Display *dpy = target->dpy; 1106 Display *dpy = target->dpy;
1056 bool ret = false; 1107 bool ret = false;
1057 1108
1058 if (flags & tintWholesome) 1109 if (flags & tintWholesome)
1064 * performance improvements, as we eliminate XImage transfer 1115 * performance improvements, as we eliminate XImage transfer
1065 */ 1116 */
1066 gcv.foreground = Pixel (tint); 1117 gcv.foreground = Pixel (tint);
1067 gcv.function = GXand; 1118 gcv.function = GXand;
1068 gcv.fill_style = FillSolid; 1119 gcv.fill_style = FillSolid;
1069 gc = XCreateGC (dpy, root, GCFillStyle | GCForeground | GCFunction, &gcv); 1120 gc = XCreateGC (dpy, pixmap, GCFillStyle | GCForeground | GCFunction, &gcv);
1070 if (gc) 1121 if (gc)
1071 { 1122 {
1072 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height); 1123 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height);
1073 ret = true; 1124 ret = true;
1074 XFreeGC (dpy, gc); 1125 XFreeGC (dpy, gc);
1075 } 1126 }
1076 } 1127 }
1077 else 1128 else
1078 { 1129 {
1079# if XFT 1130# if XRENDER
1080 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC); 1131 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
1081 1132
1082 if (flags & tintSet) 1133 if (flags & tintSet)
1083 tint.get (c); 1134 tint.get (c);
1084 1135
1085 if (shade > 0 && shade < 100) 1136 if (shade <= 100)
1086 { 1137 {
1087 c.r = (c.r * shade) / 100; 1138 c.r = (c.r * shade) / 100;
1088 c.g = (c.g * shade) / 100; 1139 c.g = (c.g * shade) / 100;
1089 c.b = (c.b * shade) / 100; 1140 c.b = (c.b * shade) / 100;
1090 } 1141 }
1091 else if (shade > 100 && shade < 200) 1142 else
1092 {
1093 c.r = (c.r * (200 - shade)) / 100;
1094 c.g = (c.g * (200 - shade)) / 100;
1095 c.b = (c.b * (200 - shade)) / 100;
1096 } 1143 {
1144 c.r = ((0xffff - c.r) * (200 - shade)) / 100;
1145 c.g = ((0xffff - c.g) * (200 - shade)) / 100;
1146 c.b = ((0xffff - c.b) * (200 - shade)) / 100;
1147 }
1097 1148
1098 XRenderPictFormat pf; 1149 XRenderPictFormat *solid_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
1099 pf.type = PictTypeDirect;
1100 pf.depth = 32;
1101 pf.direct.redMask = 0xff;
1102 pf.direct.greenMask = 0xff;
1103 pf.direct.blueMask = 0xff;
1104 pf.direct.alphaMask = 0xff;
1105
1106 XRenderPictFormat *solid_format = XRenderFindFormat (dpy, 1150 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
1107 (PictFormatType|
1108 PictFormatDepth|
1109 PictFormatRedMask|
1110 PictFormatGreenMask|
1111 PictFormatBlueMask|
1112 PictFormatAlphaMask),
1113 &pf,
1114 0);
1115 XRenderPictFormat *root_format = XRenderFindVisualFormat (dpy, DefaultVisualOfScreen (ScreenOfDisplay (dpy, target->display->screen)));
1116 XRenderPictureAttributes pa; 1151 XRenderPictureAttributes pa;
1117 1152
1118 Picture back_pic = XRenderCreatePicture (dpy, pixmap, root_format, 0, &pa); 1153 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, &pa);
1119 1154
1120 pa.repeat = True; 1155 pa.repeat = True;
1121 1156
1122 Pixmap overlay_pmap = XCreatePixmap (dpy, root, 1, 1, 32); 1157 Pixmap overlay_pmap = XCreatePixmap (dpy, pixmap, 1, 1, 32);
1123 Picture overlay_pic = XRenderCreatePicture (dpy, overlay_pmap, solid_format, CPRepeat, &pa); 1158 Picture overlay_pic = XRenderCreatePicture (dpy, overlay_pmap, solid_format, CPRepeat, &pa);
1124 XFreePixmap (dpy, overlay_pmap); 1159 XFreePixmap (dpy, overlay_pmap);
1125 1160
1126 pa.component_alpha = True; 1161 pa.component_alpha = True;
1127 Pixmap mask_pmap = XCreatePixmap (dpy, root, 1, 1, 32); 1162 Pixmap mask_pmap = XCreatePixmap (dpy, pixmap, 1, 1, 32);
1128 Picture mask_pic = XRenderCreatePicture (dpy, mask_pmap, solid_format, CPRepeat|CPComponentAlpha, &pa); 1163 Picture mask_pic = XRenderCreatePicture (dpy, mask_pmap, solid_format, CPRepeat|CPComponentAlpha, &pa);
1129 XFreePixmap (dpy, mask_pmap); 1164 XFreePixmap (dpy, mask_pmap);
1130 1165
1131 if (mask_pic && overlay_pic && back_pic) 1166 if (mask_pic && overlay_pic && back_pic)
1132 { 1167 {
1133 XRenderColor mask_c; 1168 XRenderColor mask_c;
1134 1169
1135 memset (&mask_c, (shade > 100) ? 0xFF : 0x0, sizeof (mask_c)); 1170 mask_c.red = mask_c.green = mask_c.blue = shade > 100 ? 0xffff : 0;
1136 mask_c.alpha = 0xffff; 1171 mask_c.alpha = 0xffff;
1137 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1); 1172 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
1138 1173
1139 mask_c.alpha = 0; 1174 mask_c.alpha = 0;
1140 mask_c.red = 0xffff - c.r; 1175 mask_c.red = 0xffff - c.r;
1146 } 1181 }
1147 1182
1148 XRenderFreePicture (dpy, mask_pic); 1183 XRenderFreePicture (dpy, mask_pic);
1149 XRenderFreePicture (dpy, overlay_pic); 1184 XRenderFreePicture (dpy, overlay_pic);
1150 XRenderFreePicture (dpy, back_pic); 1185 XRenderFreePicture (dpy, back_pic);
1151# if DO_TIMING_TEST
1152 XSync (dpy, False);
1153# endif
1154# endif 1186# endif
1155 } 1187 }
1156 1188
1157 return ret; 1189 return ret;
1158} 1190}
1159 1191
1160/* make_transparency_pixmap() 1192/* make_transparency_pixmap()
1161 * Builds a pixmap sized the same as terminal window, with depth same as the root window 1193 * Builds a pixmap of the same size as the terminal window that contains
1162 * that pixmap contains tiled portion of the root pixmap that is supposed to be covered by 1194 * the tiled portion of the root pixmap that is supposed to be covered by
1163 * our window. 1195 * our window.
1164 */ 1196 */
1165unsigned long 1197unsigned long
1166bgPixmap_t::make_transparency_pixmap () 1198bgPixmap_t::make_transparency_pixmap ()
1167{ 1199{
1171 return 0; 1203 return 0;
1172 1204
1173 /* root dimensions may change from call to call - but Display structure should 1205 /* root dimensions may change from call to call - but Display structure should
1174 * be always up-to-date, so let's use it : 1206 * be always up-to-date, so let's use it :
1175 */ 1207 */
1176 Window root = target->display->root;
1177 int screen = target->display->screen; 1208 int screen = target->display->screen;
1178 Display *dpy = target->dpy; 1209 Display *dpy = target->dpy;
1210 int root_depth = DefaultDepth (dpy, screen);
1179 int root_width = DisplayWidth (dpy, screen); 1211 int root_width = DisplayWidth (dpy, screen);
1180 int root_height = DisplayHeight (dpy, screen); 1212 int root_height = DisplayHeight (dpy, screen);
1181 unsigned int root_pmap_width, root_pmap_height; 1213 unsigned int root_pmap_width, root_pmap_height;
1182 int window_width = target->szHint.width; 1214 int window_width = target->szHint.width;
1183 int window_height = target->szHint.height; 1215 int window_height = target->szHint.height;
1184 int sx, sy; 1216 int sx, sy;
1185 XGCValues gcv; 1217 XGCValues gcv;
1218 GC gc;
1186 1219
1187 TIMING_TEST_START (tp); 1220 sx = target_x;
1188 target->get_window_origin (sx, sy); 1221 sy = target_y;
1189 1222
1190 /* check if we are outside of the visible part of the virtual screen : */ 1223 /* check if we are outside of the visible part of the virtual screen : */
1191 if (sx + window_width <= 0 || sy + window_height <= 0 1224 if (sx + window_width <= 0 || sy + window_height <= 0
1192 || sx >= root_width || sy >= root_height) 1225 || sx >= root_width || sy >= root_height)
1193 return 0; 1226 return 0;
1194 1227
1228 // validate root pixmap and get its size
1195 if (root_pixmap != None) 1229 if (root_pixmap != None)
1196 { 1230 {
1197 /* we want to validate the pixmap and get it's size at the same time : */ 1231 Window wdummy;
1198 int junk; 1232 int idummy;
1199 unsigned int ujunk; 1233 unsigned int udummy;
1200 /* root pixmap may be bad - allow a error */ 1234
1201 target->allowedxerror = -1; 1235 target->allowedxerror = -1;
1202 1236
1203 if (!XGetGeometry (dpy, root_pixmap, &root, &junk, &junk, &root_pmap_width, &root_pmap_height, &ujunk, &ujunk)) 1237 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pmap_width, &root_pmap_height, &udummy, &udummy))
1204 root_pixmap = None; 1238 root_pixmap = None;
1205 1239
1206 target->allowedxerror = 0; 1240 target->allowedxerror = 0;
1207 } 1241 }
1208 1242
1243 Pixmap recoded_root_pmap = root_pixmap;
1244
1245 if (root_pixmap != None && root_depth != target->depth)
1246 {
1247#if XRENDER
1248 if (flags & HAS_RENDER)
1249 {
1250 XRenderPictureAttributes pa;
1251
1252 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, screen));
1253 Picture src = XRenderCreatePicture (dpy, root_pixmap, src_format, 0, &pa);
1254
1255 recoded_root_pmap = XCreatePixmap (dpy, target->vt, root_pmap_width, root_pmap_height, target->depth);
1256 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, target->visual);
1257 Picture dst = XRenderCreatePicture (dpy, recoded_root_pmap, dst_format, 0, &pa);
1258
1259 if (src && dst)
1260 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, root_pmap_width, root_pmap_height);
1261 else
1262 {
1263 XFreePixmap (dpy, recoded_root_pmap);
1264 root_pixmap = None;
1265 }
1266
1267 XRenderFreePicture (dpy, src);
1268 XRenderFreePicture (dpy, dst);
1269 }
1270 else
1271#endif
1272 root_pixmap = None;
1273 }
1274
1275 if (root_pixmap == None)
1276 return 0;
1277
1209 Pixmap tiled_root_pmap = XCreatePixmap (dpy, root, window_width, window_height, root_depth); 1278 Pixmap tiled_root_pmap = XCreatePixmap (dpy, target->vt, window_width, window_height, target->depth);
1210 GC gc = NULL;
1211 1279
1212 if (tiled_root_pmap == None) /* something really bad happened - abort */ 1280 if (tiled_root_pmap == None) /* something really bad happened - abort */
1213 return 0; 1281 return 0;
1214 1282
1215 if (root_pixmap == None)
1216 {
1217 /* use tricks to obtain the root background image :*/
1218 /* we want to create Overrideredirect window overlapping out window
1219 with background type of Parent Relative and then grab it */
1220 XSetWindowAttributes attr;
1221 Window src;
1222 bool success = false;
1223
1224 attr.background_pixmap = ParentRelative;
1225 attr.backing_store = Always;
1226 attr.event_mask = ExposureMask;
1227 attr.override_redirect = True;
1228 src = XCreateWindow (dpy, root, sx, sy, window_width, window_height, 0,
1229 CopyFromParent, CopyFromParent, CopyFromParent,
1230 CWBackPixmap|CWBackingStore|CWOverrideRedirect|CWEventMask,
1231 &attr);
1232
1233 if (src != None)
1234 {
1235 XEvent event;
1236 int ev_count = 0;
1237 XGrabServer (dpy);
1238 XMapRaised (dpy, src);
1239 XSync (dpy, False);
1240
1241 /* XSync should get window where it's properly exposed,
1242 * but to be on the safe side - let's check for the actual event to arrive : */
1243 while (XCheckWindowEvent (dpy, src, ExposureMask, &event))
1244 ++ev_count;
1245
1246 if (ev_count > 0);
1247 {
1248 /* hooray! - we can grab the image! */
1249 gc = XCreateGC (dpy, root, 0, NULL);
1250 if (gc)
1251 {
1252 XCopyArea (dpy, src, tiled_root_pmap, gc, 0, 0, window_width, window_height, 0, 0);
1253 success = true;
1254 }
1255 }
1256
1257 XDestroyWindow (dpy, src);
1258 XUngrabServer (dpy);
1259 //fprintf (stderr, "%s:%d: ev_count = %d\n", __FUNCTION__, __LINE__, ev_count);
1260 }
1261
1262 if (!success)
1263 {
1264 XFreePixmap (dpy, tiled_root_pmap);
1265 tiled_root_pmap = None;
1266 }
1267 else
1268 result |= transpPmapTiled;
1269 }
1270 else
1271 {
1272 /* straightforward pixmap copy */ 1283 /* straightforward pixmap copy */
1273 gcv.tile = root_pixmap;
1274 gcv.fill_style = FillTiled;
1275
1276 while (sx < 0) sx += (int)root_width; 1284 while (sx < 0) sx += (int)root_width;
1277 while (sy < 0) sy += (int)root_height; 1285 while (sy < 0) sy += (int)root_height;
1278 1286
1287 gcv.tile = recoded_root_pmap;
1288 gcv.fill_style = FillTiled;
1279 gcv.ts_x_origin = -sx; 1289 gcv.ts_x_origin = -sx;
1280 gcv.ts_y_origin = -sy; 1290 gcv.ts_y_origin = -sy;
1281 gc = XCreateGC (dpy, root, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 1291 gc = XCreateGC (dpy, target->vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1282 1292
1283 if (gc) 1293 if (gc)
1284 { 1294 {
1285 XFillRectangle (dpy, tiled_root_pmap, gc, 0, 0, window_width, window_height); 1295 XFillRectangle (dpy, tiled_root_pmap, gc, 0, 0, window_width, window_height);
1286 result |= transpPmapTiled; 1296 result |= transpPmapTiled;
1287 } 1297 XFreeGC (dpy, gc);
1288 }
1289 TIMING_TEST_PRINT_RESULT (tp);
1290 1298
1291 if (tiled_root_pmap != None)
1292 {
1293 if (!need_client_side_rendering ()) 1299 if (!need_client_side_rendering ())
1294 { 1300 {
1301 if ((flags & blurNeeded)
1302 && (flags & HAS_RENDER_CONV))
1303 {
1304 if (blur_pixmap (tiled_root_pmap, target->visual, window_width, window_height))
1305 result |= transpPmapBlurred;
1306 }
1295 if ((flags & tintNeeded)) 1307 if ((flags & tintNeeded)
1308 && (flags & (tintWholesome | HAS_RENDER)))
1296 { 1309 {
1297 if (tint_pixmap (tiled_root_pmap, root, window_width, window_height)) 1310 if (tint_pixmap (tiled_root_pmap, target->visual, window_width, window_height))
1298 result |= transpPmapTinted; 1311 result |= transpPmapTinted;
1299 } 1312 }
1300 } /* server side rendering completed */ 1313 } /* server side rendering completed */
1301 1314
1302 if (pixmap) 1315 if (pixmap)
1303 XFreePixmap (dpy, pixmap); 1316 XFreePixmap (dpy, pixmap);
1304 1317
1305 pixmap = tiled_root_pmap; 1318 pixmap = tiled_root_pmap;
1306 pmap_width = window_width; 1319 pmap_width = window_width;
1307 pmap_height = window_height; 1320 pmap_height = window_height;
1308 pmap_depth = root_depth; 1321 pmap_depth = target->depth;
1309 } 1322 }
1323 else
1324 XFreePixmap (dpy, tiled_root_pmap);
1310 1325
1311 if (gc) 1326 if (recoded_root_pmap != root_pixmap)
1312 XFreeGC (dpy, gc); 1327 XFreePixmap (dpy, recoded_root_pmap);
1313
1314 TIMING_TEST_PRINT_RESULT (tp);
1315 1328
1316 return result; 1329 return result;
1317} 1330}
1318 1331
1319bool 1332void
1320bgPixmap_t::set_root_pixmap () 1333bgPixmap_t::set_root_pixmap ()
1321{ 1334{
1322 Pixmap new_root_pixmap = target->get_pixmap_property (XA_XROOTPMAP_ID); 1335 Pixmap new_root_pixmap = target->get_pixmap_property (XA_XROOTPMAP_ID);
1323 if (new_root_pixmap == None) 1336 if (new_root_pixmap == None)
1324 new_root_pixmap = target->get_pixmap_property (XA_ESETROOT_PMAP_ID); 1337 new_root_pixmap = target->get_pixmap_property (XA_ESETROOT_PMAP_ID);
1325 1338
1326 if (new_root_pixmap != root_pixmap)
1327 {
1328 root_pixmap = new_root_pixmap; 1339 root_pixmap = new_root_pixmap;
1329 return true;
1330 }
1331
1332 return false;
1333} 1340}
1334# endif /* ENABLE_TRANSPARENCY */ 1341# endif /* ENABLE_TRANSPARENCY */
1335 1342
1336# ifndef HAVE_AFTERIMAGE 1343#if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE)
1337static void ShadeXImage(rxvt_term *term, XImage *srcImage, int shade, int rm, int gm, int bm); 1344static void ShadeXImage(Visual *visual, XImage *srcImage, int shade, const rgba &c);
1338# endif 1345# endif
1339 1346
1340bool 1347bool
1341bgPixmap_t::render () 1348bgPixmap_t::render ()
1342{ 1349{
1343 unsigned long background_flags = 0; 1350 unsigned long background_flags = 0;
1344 1351
1345 if (target == NULL) 1352 if (target == NULL)
1346 return false; 1353 return false;
1347
1348 TIMING_TEST_START (tp);
1349 1354
1350 invalidate (); 1355 invalidate ();
1351# ifdef ENABLE_TRANSPARENCY 1356# ifdef ENABLE_TRANSPARENCY
1352 if (flags & isTransparent) 1357 if (flags & isTransparent)
1353 { 1358 {
1354 /* we need to re-generate transparency pixmap in that case ! */ 1359 /* we need to re-generate transparency pixmap in that case ! */
1355 background_flags = make_transparency_pixmap (); 1360 background_flags = make_transparency_pixmap ();
1356 if (background_flags == 0) 1361 if (background_flags == 0)
1357 return false; 1362 return false;
1358 else if ((background_flags & transpTransformations) == (flags & transpTransformations) 1363 else if ((background_flags & transpTransformations) == (flags & transpTransformations))
1359 && pmap_depth == target->depth)
1360 flags = flags & ~isInvalid; 1364 flags &= ~isInvalid;
1361 } 1365 }
1362# endif 1366# endif
1363 1367
1364# ifdef BG_IMAGE_FROM_FILE 1368# ifdef BG_IMAGE_FROM_FILE
1365 if (have_image 1369 if (have_image
1366 || (background_flags & transpTransformations) != (flags & transpTransformations)) 1370 || (background_flags & transpTransformations) != (flags & transpTransformations))
1367 { 1371 {
1368 if (render_image (background_flags)) 1372 if (render_image (background_flags))
1369 flags = flags & ~isInvalid; 1373 flags &= ~isInvalid;
1370 } 1374 }
1371# endif 1375# endif
1372 1376
1377# if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE)
1373 XImage *result = NULL; 1378 XImage *result = NULL;
1374 1379
1375 if (background_flags && (flags & isInvalid)) 1380 if (background_flags && (flags & isInvalid))
1376 { 1381 {
1377 result = XGetImage (target->dpy, pixmap, 0, 0, pmap_width, pmap_height, AllPlanes, ZPixmap); 1382 result = XGetImage (target->dpy, pixmap, 0, 0, pmap_width, pmap_height, AllPlanes, ZPixmap);
1378 } 1383 }
1379 1384
1380 if (result) 1385 if (result)
1381 { 1386 {
1382# if !defined(HAVE_AFTERIMAGE) && !XFT
1383 /* our own client-side tinting */ 1387 /* our own client-side tinting */
1384 /* ATTENTION: We ASSUME that XFT will let us do all the tinting necessary server-side.
1385 This may need to be changed in need_client_side_rendering() logic is altered !!! */
1386 if (!(background_flags & transpPmapTinted) && (flags & tintNeeded)) 1388 if (!(background_flags & transpPmapTinted) && (flags & tintNeeded))
1387 { 1389 {
1388 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC); 1390 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC);
1389 if (flags & tintSet) 1391 if (flags & tintSet)
1390 tint.get (c); 1392 tint.get (c);
1391 ShadeXImage (target, result, shade, c.r, c.g, c.b); 1393 ShadeXImage (DefaultVisual (target->dpy, target->display->screen), result, shade, c);
1392 } 1394 }
1393# endif
1394 1395
1395 GC gc = XCreateGC (target->dpy, target->vt, 0UL, NULL); 1396 GC gc = XCreateGC (target->dpy, target->vt, 0UL, NULL);
1396 1397
1397 if (gc) 1398 if (gc)
1398 { 1399 {
1399 if (/*pmap_depth != target->depth &&*/ pixmap != None)
1400 {
1401 XFreePixmap (target->dpy, pixmap);
1402 pixmap = None;
1403 }
1404
1405 if (pixmap == None)
1406 {
1407 pixmap = XCreatePixmap (target->dpy, target->vt, result->width, result->height, target->depth);
1408 pmap_width = result->width;
1409 pmap_height = result->height;
1410 pmap_depth = target->depth;
1411 }
1412
1413 if (pmap_depth != result->depth)
1414 {
1415 /* Bad Match error will ensue ! stupid X !!!! */
1416 if (result->depth == 24 && pmap_depth == 32)
1417 result->depth = 32;
1418 else if (result->depth == 32 && pmap_depth == 24)
1419 result->depth = 24;
1420 else
1421 {
1422 /* TODO: implement image recoding */
1423 }
1424 }
1425
1426 if (pmap_depth == result->depth)
1427 XPutImage (target->dpy, pixmap, gc, result, 0, 0, 0, 0, result->width, result->height); 1400 XPutImage (target->dpy, pixmap, gc, result, 0, 0, 0, 0, result->width, result->height);
1428 1401
1429 XFreeGC (target->dpy, gc); 1402 XFreeGC (target->dpy, gc);
1430 flags = flags & ~isInvalid; 1403 flags &= ~isInvalid;
1431 } 1404 }
1432 1405
1433 XDestroyImage (result); 1406 XDestroyImage (result);
1434 } 1407 }
1408# endif
1435 1409
1436 if (flags & isInvalid) 1410 if (flags & isInvalid)
1437 { 1411 {
1438 if (pixmap != None) 1412 if (pixmap != None)
1439 { 1413 {
1442 } 1416 }
1443 } 1417 }
1444 1418
1445 apply (); 1419 apply ();
1446 1420
1447 XSync (target->dpy, False);
1448 valid_since = ev::now (); 1421 valid_since = ev::now ();
1449 1422
1450 TIMING_TEST_PRINT_RESULT (tp);
1451
1452 return true; 1423 return true;
1453} 1424}
1454 1425
1455bool 1426void
1456bgPixmap_t::set_target (rxvt_term *new_target) 1427bgPixmap_t::set_target (rxvt_term *new_target)
1457{ 1428{
1458 if (new_target)
1459 if (target != new_target)
1460 {
1461 target = new_target; 1429 target = new_target;
1462# ifdef ENABLE_TRANSPARENCY 1430
1463 root_depth = DefaultDepthOfScreen (ScreenOfDisplay (target->dpy, target->display->screen)); 1431 flags &= ~(HAS_RENDER | HAS_RENDER_CONV);
1432#if XRENDER
1433 int major, minor;
1434 if (XRenderQueryVersion (target->dpy, &major, &minor))
1435 flags |= HAS_RENDER;
1436 XFilters *filters = XRenderQueryFilters (target->dpy, target->vt);
1437 if (filters)
1438 {
1439 for (int i = 0; i < filters->nfilter; i++)
1440 if (!strcmp (filters->filter[i], FilterConvolution))
1441 flags |= HAS_RENDER_CONV;
1442
1443 XFree (filters);
1444 }
1464# endif 1445#endif
1465 return true;
1466 }
1467
1468 return false;
1469} 1446}
1470 1447
1471void 1448void
1472bgPixmap_t::apply () 1449bgPixmap_t::apply ()
1473{ 1450{
1474 if (target) 1451 if (target == NULL)
1475 { 1452 return;
1476 flags &= ~isVtOrigin;
1477 1453
1478 if (pixmap != None) 1454 if (pixmap != None)
1479 { 1455 {
1480 /* set target's background to pixmap */ 1456 /* set target's background to pixmap */
1481# ifdef ENABLE_TRANSPARENCY 1457# ifdef ENABLE_TRANSPARENCY
1482 if (flags & isTransparent) 1458 if (flags & isTransparent)
1483 { 1459 {
1484 XSetWindowBackgroundPixmap (target->dpy, target->parent[0], pixmap); 1460 XSetWindowBackgroundPixmap (target->dpy, target->parent[0], pixmap);
1485 XSetWindowBackgroundPixmap (target->dpy, target->vt, ParentRelative); 1461 XSetWindowBackgroundPixmap (target->dpy, target->vt, ParentRelative);
1486 1462
1487 if (target->scrollBar.win) 1463 if (target->scrollBar.win)
1488 XSetWindowBackgroundPixmap (target->dpy, target->scrollBar.win, ParentRelative); 1464 XSetWindowBackgroundPixmap (target->dpy, target->scrollBar.win, ParentRelative);
1489 } 1465 }
1490 else 1466 else
1491# endif 1467# endif
1492 { 1468 {
1493 flags |= isVtOrigin;
1494 /* force old pixmap dereference in case it was transparent before :*/ 1469 /* force old pixmap dereference in case it was transparent before :*/
1495 XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_border]);
1496 XSetWindowBackgroundPixmap (target->dpy, target->vt, pixmap);
1497 /* do we also need to set scrollbar's background here ? */
1498
1499 if (target->scrollBar.win)
1500 XSetWindowBackground (target->dpy, target->scrollBar.win, target->pix_colors[Color_border]);
1501 }
1502 }
1503 else
1504 {
1505 /* set target background to a pixel */
1506 XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_border]); 1470 XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_border]);
1507 XSetWindowBackground (target->dpy, target->vt, target->pix_colors[Color_bg]); 1471 XSetWindowBackgroundPixmap (target->dpy, target->vt, pixmap);
1508 /* do we also need to set scrollbar's background here ? */ 1472 /* do we also need to set scrollbar's background here ? */
1473
1509 if (target->scrollBar.win) 1474 if (target->scrollBar.win)
1510 XSetWindowBackground (target->dpy, target->scrollBar.win, target->pix_colors[Color_border]); 1475 XSetWindowBackground (target->dpy, target->scrollBar.win, target->pix_colors[Color_border]);
1511 } 1476 }
1477 }
1478 else
1479 {
1480 /* set target background to a pixel */
1481 XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_border]);
1482 XSetWindowBackground (target->dpy, target->vt, target->pix_colors[Color_bg]);
1483 /* do we also need to set scrollbar's background here ? */
1484 if (target->scrollBar.win)
1485 XSetWindowBackground (target->dpy, target->scrollBar.win, target->pix_colors[Color_border]);
1486 }
1512 1487
1513 /* don't want Expose on the parent or vt. It is better to use 1488 /* don't want Expose on the parent or vt. It is better to use
1514 scr_touch or we get a great deal of flicker otherwise: */ 1489 scr_touch or we get a great deal of flicker otherwise: */
1515 XClearWindow (target->dpy, target->parent[0]); 1490 XClearWindow (target->dpy, target->parent[0]);
1516 1491
1517 if (target->scrollBar.state && target->scrollBar.win) 1492 if (target->scrollBar.state && target->scrollBar.win)
1518 { 1493 {
1519 target->scrollBar.state = STATE_IDLE; 1494 target->scrollBar.state = STATE_IDLE;
1520 target->scrollBar.show (0); 1495 target->scrollBar.show (0);
1521 } 1496 }
1522 1497
1523 target->want_refresh = 1; 1498 target->want_refresh = 1;
1524 flags |= hasChanged; 1499 flags |= hasChanged;
1525 }
1526} 1500}
1527 1501
1528#endif /* HAVE_BG_PIXMAP */ 1502#endif /* HAVE_BG_PIXMAP */
1529 1503
1530#if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) && !XFT 1504#if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE)
1531/* taken from aterm-0.4.2 */ 1505/* taken from aterm-0.4.2 */
1532 1506
1533typedef uint32_t RUINT32T;
1534
1535static void 1507static void
1536ShadeXImage(rxvt_term *term, XImage *srcImage, int shade, int rm, int gm, int bm) 1508ShadeXImage(Visual *visual, XImage *srcImage, int shade, const rgba &c)
1537{ 1509{
1538 int sh_r, sh_g, sh_b; 1510 int sh_r, sh_g, sh_b;
1539 RUINT32T mask_r, mask_g, mask_b; 1511 uint32_t mask_r, mask_g, mask_b;
1540 RUINT32T *lookup, *lookup_r, *lookup_g, *lookup_b; 1512 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1541 unsigned int lower_lim_r, lower_lim_g, lower_lim_b; 1513 rgba low;
1542 unsigned int upper_lim_r, upper_lim_g, upper_lim_b; 1514 rgba high;
1543 int i; 1515 int i;
1516 int host_byte_order = byteorder.big_endian () ? MSBFirst : LSBFirst;
1544 1517
1545 Visual *visual = term->visual;
1546
1547 if (visual->c_class != TrueColor || srcImage->format != ZPixmap) return ; 1518 if (visual->c_class != TrueColor || srcImage->format != ZPixmap) return;
1548
1549 if (shade == 0)
1550 shade = 100;
1551 1519
1552 /* for convenience */ 1520 /* for convenience */
1553 mask_r = visual->red_mask; 1521 mask_r = visual->red_mask;
1554 mask_g = visual->green_mask; 1522 mask_g = visual->green_mask;
1555 mask_b = visual->blue_mask; 1523 mask_b = visual->blue_mask;
1556 1524
1557 /* boring lookup table pre-initialization */ 1525 /* boring lookup table pre-initialization */
1558 switch (srcImage->bits_per_pixel) { 1526 switch (srcImage->depth)
1527 {
1559 case 15: 1528 case 15:
1560 if ((mask_r != 0x7c00) || 1529 if ((mask_r != 0x7c00) ||
1561 (mask_g != 0x03e0) || 1530 (mask_g != 0x03e0) ||
1562 (mask_b != 0x001f)) 1531 (mask_b != 0x001f))
1563 return; 1532 return;
1564 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(32+32+32)); 1533 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+32+32));
1565 lookup_r = lookup; 1534 lookup_r = lookup;
1566 lookup_g = lookup+32; 1535 lookup_g = lookup+32;
1567 lookup_b = lookup+32+32; 1536 lookup_b = lookup+32+32;
1568 sh_r = 10; 1537 sh_r = 10;
1569 sh_g = 5; 1538 sh_g = 5;
1570 sh_b = 0; 1539 sh_b = 0;
1571 break; 1540 break;
1572 case 16: 1541 case 16:
1573 if ((mask_r != 0xf800) || 1542 if ((mask_r != 0xf800) ||
1574 (mask_g != 0x07e0) || 1543 (mask_g != 0x07e0) ||
1575 (mask_b != 0x001f)) 1544 (mask_b != 0x001f))
1576 return; 1545 return;
1577 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(32+64+32)); 1546 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+64+32));
1578 lookup_r = lookup; 1547 lookup_r = lookup;
1579 lookup_g = lookup+32; 1548 lookup_g = lookup+32;
1580 lookup_b = lookup+32+64; 1549 lookup_b = lookup+32+64;
1581 sh_r = 11; 1550 sh_r = 11;
1582 sh_g = 5; 1551 sh_g = 5;
1583 sh_b = 0; 1552 sh_b = 0;
1584 break; 1553 break;
1585 case 24: 1554 case 24:
1586 if ((mask_r != 0xff0000) || 1555 if ((mask_r != 0xff0000) ||
1587 (mask_g != 0x00ff00) || 1556 (mask_g != 0x00ff00) ||
1588 (mask_b != 0x0000ff)) 1557 (mask_b != 0x0000ff))
1589 return; 1558 return;
1590 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(256+256+256)); 1559 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1591 lookup_r = lookup; 1560 lookup_r = lookup;
1592 lookup_g = lookup+256; 1561 lookup_g = lookup+256;
1593 lookup_b = lookup+256+256; 1562 lookup_b = lookup+256+256;
1594 sh_r = 16; 1563 sh_r = 16;
1595 sh_g = 8; 1564 sh_g = 8;
1596 sh_b = 0; 1565 sh_b = 0;
1597 break; 1566 break;
1598 case 32: 1567 case 32:
1599 if ((mask_r != 0xff0000) || 1568 if ((mask_r != 0xff0000) ||
1600 (mask_g != 0x00ff00) || 1569 (mask_g != 0x00ff00) ||
1601 (mask_b != 0x0000ff)) 1570 (mask_b != 0x0000ff))
1602 return; 1571 return;
1603 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(256+256+256)); 1572 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1604 lookup_r = lookup; 1573 lookup_r = lookup;
1605 lookup_g = lookup+256; 1574 lookup_g = lookup+256;
1606 lookup_b = lookup+256+256; 1575 lookup_b = lookup+256+256;
1607 sh_r = 16; 1576 sh_r = 16;
1608 sh_g = 8; 1577 sh_g = 8;
1609 sh_b = 0; 1578 sh_b = 0;
1610 break; 1579 break;
1611 default: 1580 default:
1612 return; /* we do not support this color depth */ 1581 return; /* we do not support this color depth */
1613 } 1582 }
1614 1583
1615 /* prepare limits for color transformation (each channel is handled separately) */ 1584 /* prepare limits for color transformation (each channel is handled separately) */
1616 if (shade < 0) { 1585 if (shade > 100)
1586 {
1617 shade = -shade; 1587 shade = 200 - shade;
1618 if (shade < 0) shade = 0;
1619 if (shade > 100) shade = 100;
1620 1588
1621 lower_lim_r = 65535-rm; 1589 high.r = (65535 - c.r) * shade / 100;
1622 lower_lim_g = 65535-gm; 1590 high.g = (65535 - c.g) * shade / 100;
1623 lower_lim_b = 65535-bm; 1591 high.b = (65535 - c.b) * shade / 100;
1624 1592
1625 lower_lim_r = 65535-(unsigned int)(((RUINT32T)lower_lim_r)*((RUINT32T)shade)/100); 1593 low.r = 65535 - high.r;
1626 lower_lim_g = 65535-(unsigned int)(((RUINT32T)lower_lim_g)*((RUINT32T)shade)/100); 1594 low.g = 65535 - high.g;
1627 lower_lim_b = 65535-(unsigned int)(((RUINT32T)lower_lim_b)*((RUINT32T)shade)/100); 1595 low.b = 65535 - high.b;
1628
1629 upper_lim_r = upper_lim_g = upper_lim_b = 65535;
1630 } else {
1631 if (shade < 0) shade = 0;
1632 if (shade > 100) shade = 100;
1633
1634 lower_lim_r = lower_lim_g = lower_lim_b = 0;
1635
1636 upper_lim_r = (unsigned int)((((RUINT32T)rm)*((RUINT32T)shade))/100);
1637 upper_lim_g = (unsigned int)((((RUINT32T)gm)*((RUINT32T)shade))/100);
1638 upper_lim_b = (unsigned int)((((RUINT32T)bm)*((RUINT32T)shade))/100);
1639 } 1596 }
1640 1597 else
1641 /* switch red and blue bytes if necessary, we need it for some weird XServers like XFree86 3.3.3.1 */
1642 if ((srcImage->bits_per_pixel == 24) && (mask_r >= 0xFF0000 ))
1643 { 1598 {
1644 unsigned int tmp; 1599 high.r = c.r * shade / 100;
1600 high.g = c.g * shade / 100;
1601 high.b = c.b * shade / 100;
1645 1602
1646 tmp = lower_lim_r; 1603 low.r = low.g = low.b = 0;
1647 lower_lim_r = lower_lim_b;
1648 lower_lim_b = tmp;
1649
1650 tmp = upper_lim_r;
1651 upper_lim_r = upper_lim_b;
1652 upper_lim_b = tmp;
1653 } 1604 }
1654 1605
1655 /* fill our lookup tables */ 1606 /* fill our lookup tables */
1656 for (i = 0; i <= mask_r>>sh_r; i++) 1607 for (i = 0; i <= mask_r>>sh_r; i++)
1657 { 1608 {
1658 RUINT32T tmp; 1609 uint32_t tmp;
1659 tmp = ((RUINT32T)i)*((RUINT32T)(upper_lim_r-lower_lim_r)); 1610 tmp = i * high.r;
1660 tmp += ((RUINT32T)(mask_r>>sh_r))*((RUINT32T)lower_lim_r); 1611 tmp += (mask_r>>sh_r) * low.r;
1661 lookup_r[i] = (tmp/65535)<<sh_r; 1612 lookup_r[i] = (tmp/65535)<<sh_r;
1662 } 1613 }
1663 for (i = 0; i <= mask_g>>sh_g; i++) 1614 for (i = 0; i <= mask_g>>sh_g; i++)
1664 { 1615 {
1665 RUINT32T tmp; 1616 uint32_t tmp;
1666 tmp = ((RUINT32T)i)*((RUINT32T)(upper_lim_g-lower_lim_g)); 1617 tmp = i * high.g;
1667 tmp += ((RUINT32T)(mask_g>>sh_g))*((RUINT32T)lower_lim_g); 1618 tmp += (mask_g>>sh_g) * low.g;
1668 lookup_g[i] = (tmp/65535)<<sh_g; 1619 lookup_g[i] = (tmp/65535)<<sh_g;
1669 } 1620 }
1670 for (i = 0; i <= mask_b>>sh_b; i++) 1621 for (i = 0; i <= mask_b>>sh_b; i++)
1671 { 1622 {
1672 RUINT32T tmp; 1623 uint32_t tmp;
1673 tmp = ((RUINT32T)i)*((RUINT32T)(upper_lim_b-lower_lim_b)); 1624 tmp = i * high.b;
1674 tmp += ((RUINT32T)(mask_b>>sh_b))*((RUINT32T)lower_lim_b); 1625 tmp += (mask_b>>sh_b) * low.b;
1675 lookup_b[i] = (tmp/65535)<<sh_b; 1626 lookup_b[i] = (tmp/65535)<<sh_b;
1676 } 1627 }
1677 1628
1678 /* apply table to input image (replacing colors by newly calculated ones) */ 1629 /* apply table to input image (replacing colors by newly calculated ones) */
1679 switch (srcImage->bits_per_pixel) 1630 if (srcImage->bits_per_pixel == 32
1631 && (srcImage->depth == 24 || srcImage->depth == 32)
1632 && srcImage->byte_order == host_byte_order)
1680 { 1633 {
1681 case 15:
1682 {
1683 unsigned short *p1, *pf, *p, *pl; 1634 uint32_t *p1, *pf, *p, *pl;
1684 p1 = (unsigned short *) srcImage->data; 1635 p1 = (uint32_t *) srcImage->data;
1685 pf = (unsigned short *) (srcImage->data + srcImage->height * srcImage->bytes_per_line); 1636 pf = (uint32_t *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
1637
1686 while (p1 < pf) 1638 while (p1 < pf)
1687 { 1639 {
1688 p = p1; 1640 p = p1;
1689 pl = p1 + srcImage->width; 1641 pl = p1 + srcImage->width;
1690 for (; p < pl; p++) 1642 for (; p < pl; p++)
1691 { 1643 {
1692 *p = lookup_r[(*p & 0x7c00)>>10] |
1693 lookup_g[(*p & 0x03e0)>> 5] |
1694 lookup_b[(*p & 0x001f)];
1695 }
1696 p1 = (unsigned short *) ((char *) p1 + srcImage->bytes_per_line);
1697 }
1698 break;
1699 }
1700 case 16:
1701 {
1702 unsigned short *p1, *pf, *p, *pl;
1703 p1 = (unsigned short *) srcImage->data;
1704 pf = (unsigned short *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
1705 while (p1 < pf)
1706 {
1707 p = p1;
1708 pl = p1 + srcImage->width;
1709 for (; p < pl; p++)
1710 {
1711 *p = lookup_r[(*p & 0xf800)>>11] |
1712 lookup_g[(*p & 0x07e0)>> 5] |
1713 lookup_b[(*p & 0x001f)];
1714 }
1715 p1 = (unsigned short *) ((char *) p1 + srcImage->bytes_per_line);
1716 }
1717 break;
1718 }
1719 case 24:
1720 {
1721 unsigned char *p1, *pf, *p, *pl;
1722 p1 = (unsigned char *) srcImage->data;
1723 pf = (unsigned char *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
1724 while (p1 < pf)
1725 {
1726 p = p1;
1727 pl = p1 + srcImage->width * 3;
1728 for (; p < pl; p += 3)
1729 {
1730 p[0] = lookup_r[(p[0] & 0xff0000)>>16];
1731 p[1] = lookup_r[(p[1] & 0x00ff00)>> 8];
1732 p[2] = lookup_r[(p[2] & 0x0000ff)];
1733 }
1734 p1 = (unsigned char *) ((char *) p1 + srcImage->bytes_per_line);
1735 }
1736 break;
1737 }
1738 case 32:
1739 {
1740 RUINT32T *p1, *pf, *p, *pl;
1741 p1 = (RUINT32T *) srcImage->data;
1742 pf = (RUINT32T *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
1743
1744 while (p1 < pf)
1745 {
1746 p = p1;
1747 pl = p1 + srcImage->width;
1748 for (; p < pl; p++)
1749 {
1750 *p = lookup_r[(*p & 0xff0000)>>16] | 1644 *p = lookup_r[(*p & 0xff0000) >> 16] |
1751 lookup_g[(*p & 0x00ff00)>> 8] | 1645 lookup_g[(*p & 0x00ff00) >> 8] |
1752 lookup_b[(*p & 0x0000ff)] | 1646 lookup_b[(*p & 0x0000ff)] |
1753 (*p & ~0xffffff); 1647 (*p & 0xff000000);
1648 }
1649 p1 = (uint32_t *) ((char *) p1 + srcImage->bytes_per_line);
1650 }
1651 }
1652 else
1653 {
1654 for (int y = 0; y < srcImage->height; y++)
1655 for (int x = 0; x < srcImage->width; x++)
1656 {
1657 unsigned long pixel = XGetPixel (srcImage, x, y);
1658 pixel = lookup_r[(pixel & mask_r) >> sh_r] |
1659 lookup_g[(pixel & mask_g) >> sh_g] |
1660 lookup_b[(pixel & mask_b) >> sh_b];
1661 XPutPixel (srcImage, x, y, pixel);
1754 } 1662 }
1755 p1 = (RUINT32T *) ((char *) p1 + srcImage->bytes_per_line);
1756 } 1663 }
1757 break;
1758 }
1759 }
1760 1664
1761 free (lookup); 1665 free (lookup);
1762} 1666}
1763#endif /* defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) */ 1667#endif /* defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines