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.204 by sf-exg, Tue Apr 10 15:19:22 2012 UTC vs.
Revision 1.216 by sf-exg, Sun May 20 16:23:42 2012 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 <schmorp@schmorp.de> 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.
58# endif 58# endif
59 59
60void 60void
61rxvt_term::bg_destroy () 61rxvt_term::bg_destroy ()
62{ 62{
63#ifdef HAVE_PIXBUF
64 if (pixbuf)
65 g_object_unref (pixbuf);
66#endif
67
68 if (bg_pixmap) 63 if (bg_pixmap)
69 XFreePixmap (dpy, bg_pixmap); 64 XFreePixmap (dpy, bg_pixmap);
70} 65}
71 66
72bool 67bool
90 if (bg_flags & BG_IS_TRANSPARENT) 85 if (bg_flags & BG_IS_TRANSPARENT)
91 return true; 86 return true;
92# endif 87# endif
93 88
94# ifdef BG_IMAGE_FROM_FILE 89# ifdef BG_IMAGE_FROM_FILE
95 if (bg_flags & BG_IS_FROM_FILE) 90 if (bg_image.flags & IM_IS_SET)
96 { 91 {
97 if (bg_flags & BG_IS_SIZE_SENSITIVE) 92 if ((bg_image.flags & IM_IS_SIZE_SENSITIVE)
93 || bg_image.width () > szHint.width
94 || bg_image.height () > szHint.height)
98 return true; 95 return true;
99 } 96 }
100# endif 97# endif
101 98
102 return false; 99 return false;
109 if (bg_flags & BG_IS_TRANSPARENT) 106 if (bg_flags & BG_IS_TRANSPARENT)
110 return true; 107 return true;
111# endif 108# endif
112 109
113# ifdef BG_IMAGE_FROM_FILE 110# ifdef BG_IMAGE_FROM_FILE
114 if (bg_flags & BG_IS_FROM_FILE) 111 if (bg_image.flags & IM_IS_SET)
115 { 112 {
116 if (bg_flags & BG_ROOT_ALIGN) 113 if (bg_image.flags & IM_ROOT_ALIGN)
117 return true; 114 return true;
118 } 115 }
119# endif 116# endif
120 117
121 return false; 118 return false;
123 120
124# ifdef BG_IMAGE_FROM_FILE 121# ifdef BG_IMAGE_FROM_FILE
125static inline int 122static inline int
126make_align_position (int align, int window_size, int image_size) 123make_align_position (int align, int window_size, int image_size)
127{ 124{
128 int diff = window_size - image_size;
129 int smaller = min (image_size, window_size);
130
131 if (align >= 0 && align <= 100) 125 if (align >= 0 && align <= 100)
132 return diff * align / 100; 126 return lerp (0, window_size - image_size, align);
133 else if (align > 100 && align <= 200) 127 else if (align > 100)
134 return (align - 100) * smaller / 100 + window_size - smaller; 128 return lerp (window_size - image_size, window_size, align - 100);
135 else if (align >= -100 && align < 0) 129 else
136 return (align + 100) * smaller / 100 - image_size; 130 return lerp (-image_size, 0, align + 100);
137 return 0;
138} 131}
139 132
140static inline int 133static inline int
141make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) 134make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
142{ 135{
153 min_it (dst_size, target_size - dst_pos); 146 min_it (dst_size, target_size - dst_pos);
154 return src_pos; 147 return src_pos;
155} 148}
156 149
157bool 150bool
158rxvt_term::bg_set_geometry (const char *geom, bool update) 151rxvt_image::set_geometry (const char *geom, bool update)
159{ 152{
160 bool changed = false; 153 bool changed = false;
161 int geom_flags = 0; 154 int geom_flags = 0;
162 int x = h_align; 155 int x = h_align;
163 int y = v_align; 156 int y = v_align;
174 167
175 for (int i = 0; arr[i]; i++) 168 for (int i = 0; arr[i]; i++)
176 { 169 {
177 if (!strcasecmp (arr[i], "style=tiled")) 170 if (!strcasecmp (arr[i], "style=tiled"))
178 { 171 {
179 new_flags = BG_TILE; 172 new_flags = IM_TILE;
180 w = h = noScale; 173 w = h = noScale;
181 x = y = 0; 174 x = y = 0;
182 geom_flags = WidthValue|HeightValue|XValue|YValue; 175 geom_flags = WidthValue|HeightValue|XValue|YValue;
183 } 176 }
184 else if (!strcasecmp (arr[i], "style=aspect-stretched")) 177 else if (!strcasecmp (arr[i], "style=aspect-stretched"))
185 { 178 {
186 new_flags = BG_KEEP_ASPECT; 179 new_flags = IM_KEEP_ASPECT;
187 w = h = windowScale; 180 w = h = windowScale;
188 x = y = centerAlign; 181 x = y = centerAlign;
189 geom_flags = WidthValue|HeightValue|XValue|YValue; 182 geom_flags = WidthValue|HeightValue|XValue|YValue;
190 } 183 }
191 else if (!strcasecmp (arr[i], "style=stretched")) 184 else if (!strcasecmp (arr[i], "style=stretched"))
201 x = y = centerAlign; 194 x = y = centerAlign;
202 geom_flags = WidthValue|HeightValue|XValue|YValue; 195 geom_flags = WidthValue|HeightValue|XValue|YValue;
203 } 196 }
204 else if (!strcasecmp (arr[i], "style=root-tiled")) 197 else if (!strcasecmp (arr[i], "style=root-tiled"))
205 { 198 {
206 new_flags = BG_TILE|BG_ROOT_ALIGN; 199 new_flags = IM_TILE|IM_ROOT_ALIGN;
207 w = h = noScale; 200 w = h = noScale;
208 geom_flags = WidthValue|HeightValue; 201 geom_flags = WidthValue|HeightValue;
209 } 202 }
210 else if (!strcasecmp (arr[i], "op=tile")) 203 else if (!strcasecmp (arr[i], "op=tile"))
211 new_flags |= BG_TILE; 204 new_flags |= IM_TILE;
212 else if (!strcasecmp (arr[i], "op=keep-aspect")) 205 else if (!strcasecmp (arr[i], "op=keep-aspect"))
213 new_flags |= BG_KEEP_ASPECT; 206 new_flags |= IM_KEEP_ASPECT;
214 else if (!strcasecmp (arr[i], "op=root-align")) 207 else if (!strcasecmp (arr[i], "op=root-align"))
215 new_flags |= BG_ROOT_ALIGN; 208 new_flags |= IM_ROOT_ALIGN;
216 209
217 // deprecated 210 // deprecated
218 else if (!strcasecmp (arr[i], "tile")) 211 else if (!strcasecmp (arr[i], "tile"))
219 { 212 {
220 new_flags |= BG_TILE; 213 new_flags |= IM_TILE;
221 w = h = noScale; 214 w = h = noScale;
222 geom_flags |= WidthValue|HeightValue; 215 geom_flags |= WidthValue|HeightValue;
223 } 216 }
224 else if (!strcasecmp (arr[i], "propscale")) 217 else if (!strcasecmp (arr[i], "propscale"))
225 { 218 {
226 new_flags |= BG_KEEP_ASPECT; 219 new_flags |= IM_KEEP_ASPECT;
227 w = h = windowScale; 220 w = h = windowScale;
228 geom_flags |= WidthValue|HeightValue; 221 geom_flags |= WidthValue|HeightValue;
229 } 222 }
230 else if (!strcasecmp (arr[i], "hscale")) 223 else if (!strcasecmp (arr[i], "hscale"))
231 { 224 {
232 new_flags |= BG_TILE; 225 new_flags |= IM_TILE;
233 w = windowScale; 226 w = windowScale;
234 h = noScale; 227 h = noScale;
235 geom_flags |= WidthValue|HeightValue; 228 geom_flags |= WidthValue|HeightValue;
236 } 229 }
237 else if (!strcasecmp (arr[i], "vscale")) 230 else if (!strcasecmp (arr[i], "vscale"))
238 { 231 {
239 new_flags |= BG_TILE; 232 new_flags |= IM_TILE;
240 h = windowScale; 233 h = windowScale;
241 w = noScale; 234 w = noScale;
242 geom_flags |= WidthValue|HeightValue; 235 geom_flags |= WidthValue|HeightValue;
243 } 236 }
244 else if (!strcasecmp (arr[i], "scale")) 237 else if (!strcasecmp (arr[i], "scale"))
252 x = y = centerAlign; 245 x = y = centerAlign;
253 geom_flags |= WidthValue|HeightValue|XValue|YValue; 246 geom_flags |= WidthValue|HeightValue|XValue|YValue;
254 } 247 }
255 else if (!strcasecmp (arr[i], "root")) 248 else if (!strcasecmp (arr[i], "root"))
256 { 249 {
257 new_flags |= BG_TILE|BG_ROOT_ALIGN; 250 new_flags |= IM_TILE|IM_ROOT_ALIGN;
258 w = h = noScale; 251 w = h = noScale;
259 geom_flags |= WidthValue|HeightValue; 252 geom_flags |= WidthValue|HeightValue;
260 } 253 }
261 254
262 else 255 else
264 } /* done parsing ops */ 257 } /* done parsing ops */
265 258
266 rxvt_free_strsplit (arr); 259 rxvt_free_strsplit (arr);
267 } 260 }
268 261
269 new_flags |= bg_flags & ~BG_GEOMETRY_FLAGS; 262 new_flags |= flags & ~IM_GEOMETRY_FLAGS;
270 263
271 if (!update) 264 if (!update)
272 { 265 {
273 if (!(geom_flags & XValue)) 266 if (!(geom_flags & XValue))
274 x = y = defaultAlign; 267 x = y = defaultAlign;
286 min_it (w, 1000); 279 min_it (w, 1000);
287 min_it (h, 1000); 280 min_it (h, 1000);
288 clamp_it (x, -100, 200); 281 clamp_it (x, -100, 200);
289 clamp_it (y, -100, 200); 282 clamp_it (y, -100, 200);
290 283
291 if (bg_flags != new_flags 284 if (flags != new_flags
292 || h_scale != w 285 || h_scale != w
293 || v_scale != h 286 || v_scale != h
294 || h_align != x 287 || h_align != x
295 || v_align != y) 288 || v_align != y)
296 { 289 {
297 bg_flags = new_flags; 290 flags = new_flags;
298 h_scale = w; 291 h_scale = w;
299 v_scale = h; 292 v_scale = h;
300 h_align = x; 293 h_align = x;
301 v_align = y; 294 v_align = y;
302 changed = true; 295 changed = true;
303 } 296 }
304 297
298 if (!(flags & IM_TILE)
299 || h_scale || v_scale
300 || (!(flags & IM_ROOT_ALIGN) && (h_align || v_align)))
301 flags |= IM_IS_SIZE_SENSITIVE;
302 else
303 flags &= ~IM_IS_SIZE_SENSITIVE;
304
305 return changed; 305 return changed;
306} 306}
307 307
308void 308void
309rxvt_term::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y) 309rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
310{ 310{
311 int image_width = image.width ();
312 int image_height = image.height ();
311 int target_width = szHint.width; 313 int target_width = szHint.width;
312 int target_height = szHint.height; 314 int target_height = szHint.height;
313 315
314 w = h_scale * target_width / 100; 316 w = image.h_scale * target_width / 100;
315 h = v_scale * target_height / 100; 317 h = image.v_scale * target_height / 100;
316 318
317 if (bg_flags & BG_KEEP_ASPECT) 319 if (image.flags & IM_KEEP_ASPECT)
318 { 320 {
319 float scale = (float)w / image_width; 321 float scale = (float)w / image_width;
320 min_it (scale, (float)h / image_height); 322 min_it (scale, (float)h / image_height);
321 w = image_width * scale + 0.5; 323 w = image_width * scale + 0.5;
322 h = image_height * scale + 0.5; 324 h = image_height * scale + 0.5;
323 } 325 }
324 326
325 if (!w) w = image_width; 327 if (!w) w = image_width;
326 if (!h) h = image_height; 328 if (!h) h = image_height;
327 329
328 if (bg_flags & BG_ROOT_ALIGN) 330 if (image.flags & IM_ROOT_ALIGN)
329 { 331 {
330 x = -target_x; 332 x = -target_x;
331 y = -target_y; 333 y = -target_y;
332 } 334 }
333 else 335 else
334 { 336 {
335 x = make_align_position (h_align, target_width, w); 337 x = make_align_position (image.h_align, target_width, w);
336 y = make_align_position (v_align, target_height, h); 338 y = make_align_position (image.v_align, target_height, h);
337 } 339 }
338
339 if (!(bg_flags & BG_TILE)
340 || h_scale || v_scale
341 || (!(bg_flags & BG_ROOT_ALIGN) && (h_align || v_align))
342 || image_width > target_width || image_height > target_height)
343 bg_flags |= BG_IS_SIZE_SENSITIVE;
344 else
345 bg_flags &= ~BG_IS_SIZE_SENSITIVE;
346} 340}
347 341
348# ifdef HAVE_PIXBUF 342# ifdef HAVE_PIXBUF
349bool 343bool
350rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc, 344rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
351 int src_x, int src_y, int dst_x, int dst_y, 345 int src_x, int src_y, int dst_x, int dst_y,
352 unsigned int width, unsigned int height) 346 unsigned int width, unsigned int height)
353{ 347{
354 XImage *ximage; 348 XImage *ximage;
355 char *data, *line; 349 char *line;
356 int bytes_per_pixel;
357 int width_r, width_g, width_b; 350 int width_r, width_g, width_b, width_a;
358 int sh_r, sh_g, sh_b; 351 int sh_r, sh_g, sh_b, sh_a;
352 uint32_t alpha_mask;
359 int rowstride; 353 int rowstride;
360 int channels; 354 int channels;
361 unsigned char *row; 355 unsigned char *row;
362 356
363 if (visual->c_class != TrueColor) 357 if (visual->c_class != TrueColor)
364 return false; 358 return false;
365 359
366 if (depth == 24 || depth == 32) 360#if XRENDER
367 bytes_per_pixel = 4; 361 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
368 else if (depth == 15 || depth == 16) 362 if (format)
369 bytes_per_pixel = 2; 363 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
370 else 364 else
371 return false; 365#endif
366 alpha_mask = 0;
372 367
373 width_r = ecb_popcount32 (visual->red_mask); 368 width_r = ecb_popcount32 (visual->red_mask);
374 width_g = ecb_popcount32 (visual->green_mask); 369 width_g = ecb_popcount32 (visual->green_mask);
375 width_b = ecb_popcount32 (visual->blue_mask); 370 width_b = ecb_popcount32 (visual->blue_mask);
371 width_a = ecb_popcount32 (alpha_mask);
376 372
377 if (width_r > 8 || width_g > 8 || width_b > 8) 373 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
378 return false; 374 return false;
379 375
380 sh_r = ecb_ctz32 (visual->red_mask); 376 sh_r = ecb_ctz32 (visual->red_mask);
381 sh_g = ecb_ctz32 (visual->green_mask); 377 sh_g = ecb_ctz32 (visual->green_mask);
382 sh_b = ecb_ctz32 (visual->blue_mask); 378 sh_b = ecb_ctz32 (visual->blue_mask);
379 sh_a = ecb_ctz32 (alpha_mask);
383 380
384 if (width > INT_MAX / height / bytes_per_pixel) 381 if (width > 32767 || height > 32767)
385 return false; 382 return false;
386 383
387 data = (char *)malloc (width * height * bytes_per_pixel); 384 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
388 if (!data) 385 width, height, 32, 0);
386 if (!ximage)
389 return false; 387 return false;
390 388
391 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, data, 389 if (height > INT_MAX / ximage->bytes_per_line
392 width, height, bytes_per_pixel * 8, 0); 390 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
393 if (!ximage)
394 { 391 {
395 free (data); 392 XDestroyImage (ximage);
396 return false; 393 return false;
397 } 394 }
398 395
399 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 396 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
400 397
401 rowstride = gdk_pixbuf_get_rowstride (pixbuf); 398 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
402 channels = gdk_pixbuf_get_n_channels (pixbuf); 399 channels = gdk_pixbuf_get_n_channels (pixbuf);
403 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; 400 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
404 line = data; 401 line = ximage->data;
402
403 rgba c (0, 0, 0);
404
405 if (channels == 4 && alpha_mask == 0)
406 {
407 pix_colors[Color_bg].get (c);
408 c.r >>= 8;
409 c.g >>= 8;
410 c.b >>= 8;
411 }
405 412
406 for (int y = 0; y < height; y++) 413 for (int y = 0; y < height; y++)
407 { 414 {
408 for (int x = 0; x < width; x++) 415 for (int x = 0; x < width; x++)
409 { 416 {
410 unsigned char *pixel = row + x * channels; 417 unsigned char *pixel = row + x * channels;
411 uint32_t value; 418 uint32_t value;
419 unsigned char r, g, b, a;
412 420
421 if (channels == 4)
422 {
423 a = pixel[3];
424 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
425 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
426 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
427 }
428 else
429 {
430 a = 0xff;
431 r = pixel[0];
432 g = pixel[1];
433 b = pixel[2];
434 }
435
413 value = ((pixel[0] >> (8 - width_r)) << sh_r) 436 value = ((r >> (8 - width_r)) << sh_r)
414 | ((pixel[1] >> (8 - width_g)) << sh_g) 437 | ((g >> (8 - width_g)) << sh_g)
415 | ((pixel[2] >> (8 - width_b)) << sh_b); 438 | ((b >> (8 - width_b)) << sh_b)
439 | ((a >> (8 - width_a)) << sh_a);
416 440
417 if (bytes_per_pixel == 4) 441 if (ximage->bits_per_pixel == 32)
418 ((uint32_t *)line)[x] = value; 442 ((uint32_t *)line)[x] = value;
419 else 443 else
420 ((uint16_t *)line)[x] = value; 444 XPutPixel (ximage, x, y, value);
421 } 445 }
422 446
423 row += rowstride; 447 row += rowstride;
424 line += ximage->bytes_per_line; 448 line += ximage->bytes_per_line;
425 } 449 }
428 XDestroyImage (ximage); 452 XDestroyImage (ximage);
429 return true; 453 return true;
430} 454}
431 455
432bool 456bool
433rxvt_term::render_image (bool transparent) 457rxvt_term::render_image (rxvt_image &image)
434{ 458{
459 GdkPixbuf *pixbuf = image.pixbuf;
435 if (!pixbuf) 460 if (!pixbuf)
436 return false; 461 return false;
437 462
438 if (transparent 463 bool need_blend = bg_flags & BG_IS_VALID;
464
465 if (need_blend
439 && !(bg_flags & BG_HAS_RENDER)) 466 && !(bg_flags & BG_HAS_RENDER))
440 return false; 467 return false;
441 468
442 GdkPixbuf *result; 469 GdkPixbuf *result;
443 470
452 int x = 0; 479 int x = 0;
453 int y = 0; 480 int y = 0;
454 int w = 0; 481 int w = 0;
455 int h = 0; 482 int h = 0;
456 483
457 get_image_geometry (image_width, image_height, w, h, x, y); 484 get_image_geometry (image, w, h, x, y);
458 485
459 if (!(bg_flags & BG_ROOT_ALIGN) 486 if (!(image.flags & IM_ROOT_ALIGN)
460 && (x >= target_width 487 && (x >= target_width
461 || y >= target_height 488 || y >= target_height
462 || x + w <= 0 489 || x + w <= 0
463 || y + h <= 0)) 490 || y + h <= 0))
464 return false; 491 return false;
478 505
479 bool ret = false; 506 bool ret = false;
480 507
481 XGCValues gcv; 508 XGCValues gcv;
482 GC gc; 509 GC gc;
483 Pixmap root_pmap; 510 Pixmap tmp_pixmap;
484 511
485 image_width = gdk_pixbuf_get_width (result); 512 image_width = gdk_pixbuf_get_width (result);
486 image_height = gdk_pixbuf_get_height (result); 513 image_height = gdk_pixbuf_get_height (result);
487 514
488 if (transparent) 515 if (need_blend)
489 { 516 {
490 root_pmap = bg_pixmap; 517 tmp_pixmap = bg_pixmap;
491 bg_pixmap = None; 518 bg_pixmap = None;
492 } 519 }
493 else 520 else
494 { 521 {
495 if (bg_flags & BG_TILE) 522 if (image.flags & IM_TILE)
496 { 523 {
497 new_pmap_width = min (image_width, target_width); 524 new_pmap_width = min (image_width, target_width);
498 new_pmap_height = min (image_height, target_height); 525 new_pmap_height = min (image_height, target_height);
499 } 526 }
500 } 527 }
513 gcv.foreground = pix_colors[Color_bg]; 540 gcv.foreground = pix_colors[Color_bg];
514 gc = XCreateGC (dpy, vt, GCForeground, &gcv); 541 gc = XCreateGC (dpy, vt, GCForeground, &gcv);
515 542
516 if (gc) 543 if (gc)
517 { 544 {
518 if (bg_flags & BG_TILE) 545 if (image.flags & IM_TILE)
519 { 546 {
520 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth); 547 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth);
521 pixbuf_to_pixmap (result, tile, gc, 548 pixbuf_to_pixmap (result, tile, gc,
522 0, 0, 549 0, 0,
523 0, 0, 550 0, 0,
551 dst_x, dst_y, 578 dst_x, dst_y,
552 dst_width, dst_height); 579 dst_width, dst_height);
553 } 580 }
554 581
555#if XRENDER 582#if XRENDER
556 if (transparent) 583 if (need_blend)
557 { 584 {
558 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 585 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
559 586
560 Picture src = XRenderCreatePicture (dpy, root_pmap, format, 0, 0); 587 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, format, 0, 0);
561 588
562 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0); 589 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
563 590
564 Picture mask = create_xrender_mask (dpy, vt, False, False); 591 Picture mask = create_xrender_mask (dpy, vt, False, False);
565 592
585 } 612 }
586 613
587 if (result != pixbuf) 614 if (result != pixbuf)
588 g_object_unref (result); 615 g_object_unref (result);
589 616
590 if (transparent) 617 if (need_blend)
591 XFreePixmap (dpy, root_pmap); 618 XFreePixmap (dpy, tmp_pixmap);
592 619
593 return ret; 620 return ret;
594} 621}
595# endif /* HAVE_PIXBUF */ 622# endif /* HAVE_PIXBUF */
596 623
597bool 624bool
598rxvt_term::bg_set_file (const char *file) 625rxvt_image::set_file (const char *file)
599{ 626{
600 if (!file || !*file) 627 if (!file || !*file)
601 return false; 628 return false;
602 629
603 bool ret = false; 630 bool ret = false;
617 if (image) 644 if (image)
618 { 645 {
619 if (pixbuf) 646 if (pixbuf)
620 g_object_unref (pixbuf); 647 g_object_unref (pixbuf);
621 pixbuf = image; 648 pixbuf = image;
622 bg_flags |= BG_IS_FROM_FILE;
623 ret = true; 649 ret = true;
624 } 650 }
625# endif 651# endif
626 652
627 if (ret) 653 if (ret)
628 { 654 {
655 flags = IM_IS_SET | IM_IS_SIZE_SENSITIVE;
656 h_scale = v_scale = defaultScale;
657 h_align = v_align = defaultAlign;
658
629 if (p) 659 if (p)
630 bg_set_geometry (p + 1); 660 set_geometry (p + 1);
631 else
632 bg_set_default_geometry ();
633 } 661 }
634 662
635 return ret; 663 return ret;
636} 664}
637 665
638# endif /* BG_IMAGE_FROM_FILE */ 666# endif /* BG_IMAGE_FROM_FILE */
639 667
640# ifdef ENABLE_TRANSPARENCY 668# ifdef ENABLE_TRANSPARENCY
641bool
642rxvt_term::bg_set_transparent ()
643{
644 if (!(bg_flags & BG_IS_TRANSPARENT))
645 {
646 bg_flags |= BG_IS_TRANSPARENT;
647 return true;
648 }
649
650 return false;
651}
652
653bool 669bool
654rxvt_term::bg_set_blur (const char *geom) 670rxvt_term::bg_set_blur (const char *geom)
655{ 671{
656 bool changed = false; 672 bool changed = false;
657 unsigned int hr, vr; 673 unsigned int hr, vr;
676 { 692 {
677 changed = true; 693 changed = true;
678 v_blurRadius = vr; 694 v_blurRadius = vr;
679 } 695 }
680 696
681 if (h_blurRadius && v_blurRadius)
682 bg_flags |= BG_NEEDS_BLUR;
683 else
684 bg_flags &= ~BG_NEEDS_BLUR;
685
686 return changed; 697 return changed;
687}
688
689void
690rxvt_term::set_tint_shade_flags ()
691{
692 if (shade != 100 || (bg_flags & BG_TINT_SET))
693 bg_flags |= BG_NEEDS_TINT;
694 else
695 bg_flags &= ~BG_NEEDS_TINT;
696} 698}
697 699
698bool 700bool
699rxvt_term::bg_set_tint (rxvt_color &new_tint) 701rxvt_term::bg_set_tint (rxvt_color &new_tint)
700{ 702{
710 && (c.b <= 0x00ff || c.b >= 0xff00)) 712 && (c.b <= 0x00ff || c.b >= 0xff00))
711 bg_flags |= BG_TINT_BITAND; 713 bg_flags |= BG_TINT_BITAND;
712 else 714 else
713 bg_flags &= ~BG_TINT_BITAND; 715 bg_flags &= ~BG_TINT_BITAND;
714 716
715 set_tint_shade_flags ();
716 return true; 717 return true;
717 } 718 }
718 719
719 return false; 720 return false;
720} 721}
729 new_shade = 200 - (100 + new_shade); 730 new_shade = 200 - (100 + new_shade);
730 731
731 if (new_shade != shade) 732 if (new_shade != shade)
732 { 733 {
733 shade = new_shade; 734 shade = new_shade;
734 set_tint_shade_flags ();
735 return true; 735 return true;
736 } 736 }
737 737
738 return false; 738 return false;
739} 739}
760 params[i+2] = XDoubleToFixed (kernel[i] / sum); 760 params[i+2] = XDoubleToFixed (kernel[i] / sum);
761} 761}
762#endif 762#endif
763 763
764bool 764bool
765rxvt_term::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height, int depth) 765rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height)
766{ 766{
767 bool ret = false; 767 bool ret = false;
768#if XRENDER 768#if XRENDER
769 if (!(bg_flags & BG_HAS_RENDER_CONV)) 769 if (!(bg_flags & BG_HAS_RENDER_CONV))
770 return false; 770 return false;
825#endif 825#endif
826 return ret; 826 return ret;
827} 827}
828 828
829bool 829bool
830rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 830rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height)
831{ 831{
832 bool ret = false; 832 bool ret = false;
833 833
834 if (shade == 100 && (bg_flags & BG_TINT_BITAND)) 834 if (shade == 100 && (bg_flags & BG_TINT_BITAND))
835 { 835 {
1015 1015
1016 if (gc) 1016 if (gc)
1017 { 1017 {
1018 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); 1018 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1019 ret = true; 1019 ret = true;
1020 unsigned long tr_flags = bg_flags & BG_EFFECTS_FLAGS; 1020 bool need_blur = h_blurRadius && v_blurRadius;
1021 bool need_tint = shade != 100 || (bg_flags & BG_TINT_SET);
1021 1022
1022 if (!(bg_flags & BG_CLIENT_RENDER)) 1023 if (!(bg_flags & BG_CLIENT_RENDER))
1023 { 1024 {
1024 if (bg_flags & BG_NEEDS_BLUR) 1025 if (need_blur)
1025 {
1026 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth))
1027 tr_flags &= ~BG_NEEDS_BLUR;
1028 } 1026 {
1029 if (bg_flags & BG_NEEDS_TINT) 1027 if (blur_pixmap (bg_pixmap, window_width, window_height))
1028 need_blur = false;
1030 { 1029 }
1030 if (need_tint)
1031 {
1031 if (tint_pixmap (bg_pixmap, visual, window_width, window_height)) 1032 if (tint_pixmap (bg_pixmap, window_width, window_height))
1032 tr_flags &= ~BG_NEEDS_TINT; 1033 need_tint = false;
1033 } 1034 }
1034 if (tr_flags & BG_NEEDS_TINT) 1035 if (need_tint)
1035 { 1036 {
1036 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap); 1037 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1037 if (ximage) 1038 if (ximage)
1038 { 1039 {
1039 /* our own client-side tinting */ 1040 /* our own client-side tinting */
1040 tint_ximage (DefaultVisual (dpy, display->screen), ximage); 1041 tint_ximage (ximage);
1041 1042
1042 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); 1043 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
1043 XDestroyImage (ximage); 1044 XDestroyImage (ximage);
1044 } 1045 }
1045 } 1046 }
1066# endif /* ENABLE_TRANSPARENCY */ 1067# endif /* ENABLE_TRANSPARENCY */
1067 1068
1068bool 1069bool
1069rxvt_term::bg_render () 1070rxvt_term::bg_render ()
1070{ 1071{
1071 bool transparent = false;
1072
1073 bg_invalidate (); 1072 bg_invalidate ();
1074# ifdef ENABLE_TRANSPARENCY 1073# ifdef ENABLE_TRANSPARENCY
1075 if (bg_flags & BG_IS_TRANSPARENT) 1074 if (bg_flags & BG_IS_TRANSPARENT)
1076 { 1075 {
1077 /* we need to re-generate transparency pixmap in that case ! */ 1076 /* we need to re-generate transparency pixmap in that case ! */
1078 transparent = make_transparency_pixmap (); 1077 if (make_transparency_pixmap ())
1079 if (transparent)
1080 bg_flags |= BG_IS_VALID; 1078 bg_flags |= BG_IS_VALID;
1081 } 1079 }
1082# endif 1080# endif
1083 1081
1084# ifdef BG_IMAGE_FROM_FILE 1082# ifdef BG_IMAGE_FROM_FILE
1085 if (bg_flags & BG_IS_FROM_FILE) 1083 if (bg_image.flags & IM_IS_SET)
1086 { 1084 {
1087 if (render_image (transparent)) 1085 if (render_image (bg_image))
1088 bg_flags |= BG_IS_VALID; 1086 bg_flags |= BG_IS_VALID;
1089 } 1087 }
1090# endif 1088# endif
1091 1089
1092 if (!(bg_flags & BG_IS_VALID)) 1090 if (!(bg_flags & BG_IS_VALID))
1146 lookup[i] = (tmp / 0xffff) << sh; 1144 lookup[i] = (tmp / 0xffff) << sh;
1147 } 1145 }
1148} 1146}
1149 1147
1150void 1148void
1151rxvt_term::tint_ximage (Visual *visual, XImage *ximage) 1149rxvt_term::tint_ximage (XImage *ximage)
1152{ 1150{
1151 unsigned int size_r, size_g, size_b;
1153 int sh_r, sh_g, sh_b; 1152 int sh_r, sh_g, sh_b;
1154 uint32_t mask_r, mask_g, mask_b; 1153 uint32_t mask_r, mask_g, mask_b;
1155 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1154 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1156 unsigned short low; 1155 unsigned short low;
1157 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 1156 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1162 mask_r = visual->red_mask; 1161 mask_r = visual->red_mask;
1163 mask_g = visual->green_mask; 1162 mask_g = visual->green_mask;
1164 mask_b = visual->blue_mask; 1163 mask_b = visual->blue_mask;
1165 1164
1166 /* boring lookup table pre-initialization */ 1165 /* boring lookup table pre-initialization */
1167 switch (ximage->depth) 1166 sh_r = ecb_ctz32 (mask_r);
1168 { 1167 sh_g = ecb_ctz32 (mask_g);
1169 case 15: 1168 sh_b = ecb_ctz32 (mask_b);
1170 if ((mask_r != 0x7c00) || 1169
1171 (mask_g != 0x03e0) || 1170 size_r = mask_r >> sh_r;
1172 (mask_b != 0x001f)) 1171 size_g = mask_g >> sh_g;
1172 size_b = mask_b >> sh_b;
1173
1174 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1173 return; 1175 return;
1174 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+32+32)); 1176
1177 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1175 lookup_r = lookup; 1178 lookup_r = lookup;
1176 lookup_g = lookup+32; 1179 lookup_g = lookup + size_r;
1177 lookup_b = lookup+32+32; 1180 lookup_b = lookup + size_r + size_g;
1178 sh_r = 10;
1179 sh_g = 5;
1180 sh_b = 0;
1181 break;
1182 case 16:
1183 if ((mask_r != 0xf800) ||
1184 (mask_g != 0x07e0) ||
1185 (mask_b != 0x001f))
1186 return;
1187 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+64+32));
1188 lookup_r = lookup;
1189 lookup_g = lookup+32;
1190 lookup_b = lookup+32+64;
1191 sh_r = 11;
1192 sh_g = 5;
1193 sh_b = 0;
1194 break;
1195 case 24:
1196 if ((mask_r != 0xff0000) ||
1197 (mask_g != 0x00ff00) ||
1198 (mask_b != 0x0000ff))
1199 return;
1200 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1201 lookup_r = lookup;
1202 lookup_g = lookup+256;
1203 lookup_b = lookup+256+256;
1204 sh_r = 16;
1205 sh_g = 8;
1206 sh_b = 0;
1207 break;
1208 case 32:
1209 if ((mask_r != 0xff0000) ||
1210 (mask_g != 0x00ff00) ||
1211 (mask_b != 0x0000ff))
1212 return;
1213 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1214 lookup_r = lookup;
1215 lookup_g = lookup+256;
1216 lookup_b = lookup+256+256;
1217 sh_r = 16;
1218 sh_g = 8;
1219 sh_b = 0;
1220 break;
1221 default:
1222 return; /* we do not support this color depth */
1223 }
1224 1181
1225 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); 1182 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1226 1183
1227 if (bg_flags & BG_TINT_SET) 1184 if (bg_flags & BG_TINT_SET)
1228 tint.get (c); 1185 tint.get (c);
1250 fill_lut (lookup_g, mask_g, sh_g, low, c.g); 1207 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1251 fill_lut (lookup_b, mask_b, sh_b, low, c.b); 1208 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1252 1209
1253 /* apply table to input image (replacing colors by newly calculated ones) */ 1210 /* apply table to input image (replacing colors by newly calculated ones) */
1254 if (ximage->bits_per_pixel == 32 1211 if (ximage->bits_per_pixel == 32
1255 && (ximage->depth == 24 || ximage->depth == 32)
1256 && ximage->byte_order == host_byte_order) 1212 && ximage->byte_order == host_byte_order)
1257 { 1213 {
1258 uint32_t *p1, *pf, *p, *pl; 1214 char *line = ximage->data;
1259 p1 = (uint32_t *) ximage->data;
1260 pf = (uint32_t *) (ximage->data + ximage->height * ximage->bytes_per_line);
1261 1215
1262 while (p1 < pf) 1216 for (int y = 0; y < ximage->height; y++)
1263 {
1264 p = p1;
1265 pl = p1 + ximage->width;
1266 for (; p < pl; p++)
1267 { 1217 {
1268 *p = lookup_r[(*p & 0xff0000) >> 16] | 1218 uint32_t *p = (uint32_t *)line;
1269 lookup_g[(*p & 0x00ff00) >> 8] | 1219 for (int x = 0; x < ximage->width; x++)
1270 lookup_b[(*p & 0x0000ff)] |
1271 (*p & 0xff000000);
1272 } 1220 {
1273 p1 = (uint32_t *) ((char *) p1 + ximage->bytes_per_line); 1221 *p = lookup_r[(*p & mask_r) >> sh_r] |
1222 lookup_g[(*p & mask_g) >> sh_g] |
1223 lookup_b[(*p & mask_b) >> sh_b];
1224 p++;
1225 }
1226 line += ximage->bytes_per_line;
1274 } 1227 }
1275 } 1228 }
1276 else 1229 else
1277 { 1230 {
1278 for (int y = 0; y < ximage->height; y++) 1231 for (int y = 0; y < ximage->height; y++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines