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.201 by sf-exg, Sat Feb 4 21:47:06 2012 UTC vs.
Revision 1.215 by sf-exg, Thu May 17 10:31:26 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 bg_flags &= ~BG_IS_SIZE_SENSITIVE;
340 if (!(bg_flags & BG_TILE)
341 || h_scale || v_scale
342 || (!(bg_flags & BG_ROOT_ALIGN) && (h_align || v_align))
343 || w > target_width || h > target_height)
344 bg_flags |= BG_IS_SIZE_SENSITIVE;
345} 340}
346 341
347# ifdef HAVE_PIXBUF 342# ifdef HAVE_PIXBUF
348bool 343bool
349rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc, 344rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
350 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,
351 unsigned int width, unsigned int height) 346 unsigned int width, unsigned int height)
352{ 347{
353 XImage *ximage; 348 XImage *ximage;
354 char *data, *line; 349 char *line;
355 int bytes_per_pixel;
356 int width_r, width_g, width_b; 350 int width_r, width_g, width_b, width_a;
357 int sh_r, sh_g, sh_b; 351 int sh_r, sh_g, sh_b, sh_a;
352 uint32_t alpha_mask;
358 int rowstride; 353 int rowstride;
359 int channels; 354 int channels;
360 unsigned char *row; 355 unsigned char *row;
361 356
362 if (visual->c_class != TrueColor) 357 if (visual->c_class != TrueColor)
363 return false; 358 return false;
364 359
365 if (depth == 24 || depth == 32) 360#if XRENDER
366 bytes_per_pixel = 4; 361 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
367 else if (depth == 15 || depth == 16) 362 if (format)
368 bytes_per_pixel = 2; 363 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
369 else 364 else
370 return false; 365#endif
366 alpha_mask = 0;
371 367
372 width_r = ecb_popcount32 (visual->red_mask); 368 width_r = ecb_popcount32 (visual->red_mask);
373 width_g = ecb_popcount32 (visual->green_mask); 369 width_g = ecb_popcount32 (visual->green_mask);
374 width_b = ecb_popcount32 (visual->blue_mask); 370 width_b = ecb_popcount32 (visual->blue_mask);
371 width_a = ecb_popcount32 (alpha_mask);
375 372
376 if (width_r > 8 || width_g > 8 || width_b > 8) 373 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
377 return false; 374 return false;
378 375
379 sh_r = ecb_ctz32 (visual->red_mask); 376 sh_r = ecb_ctz32 (visual->red_mask);
380 sh_g = ecb_ctz32 (visual->green_mask); 377 sh_g = ecb_ctz32 (visual->green_mask);
381 sh_b = ecb_ctz32 (visual->blue_mask); 378 sh_b = ecb_ctz32 (visual->blue_mask);
379 sh_a = ecb_ctz32 (alpha_mask);
382 380
383 if (width > INT_MAX / height / bytes_per_pixel) 381 if (width > 32767 || height > 32767)
384 return false; 382 return false;
385 383
386 data = (char *)malloc (width * height * bytes_per_pixel); 384 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
387 if (!data) 385 width, height, 32, 0);
386 if (!ximage)
388 return false; 387 return false;
389 388
390 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, data, 389 if (height > INT_MAX / ximage->bytes_per_line
391 width, height, bytes_per_pixel * 8, 0); 390 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
392 if (!ximage)
393 { 391 {
394 free (data); 392 XDestroyImage (ximage);
395 return false; 393 return false;
396 } 394 }
397 395
398 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 396 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
399 397
400 rowstride = gdk_pixbuf_get_rowstride (pixbuf); 398 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
401 channels = gdk_pixbuf_get_n_channels (pixbuf); 399 channels = gdk_pixbuf_get_n_channels (pixbuf);
402 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;
403 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 }
404 412
405 for (int y = 0; y < height; y++) 413 for (int y = 0; y < height; y++)
406 { 414 {
407 for (int x = 0; x < width; x++) 415 for (int x = 0; x < width; x++)
408 { 416 {
409 unsigned char *pixel = row + x * channels; 417 unsigned char *pixel = row + x * channels;
410 uint32_t value; 418 uint32_t value;
419 unsigned char r, g, b, a;
411 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
412 value = ((pixel[0] >> (8 - width_r)) << sh_r) 436 value = ((r >> (8 - width_r)) << sh_r)
413 | ((pixel[1] >> (8 - width_g)) << sh_g) 437 | ((g >> (8 - width_g)) << sh_g)
414 | ((pixel[2] >> (8 - width_b)) << sh_b); 438 | ((b >> (8 - width_b)) << sh_b)
439 | ((a >> (8 - width_a)) << sh_a);
415 440
416 if (bytes_per_pixel == 4) 441 if (ximage->bits_per_pixel == 32)
417 ((uint32_t *)line)[x] = value; 442 ((uint32_t *)line)[x] = value;
418 else 443 else
419 ((uint16_t *)line)[x] = value; 444 XPutPixel (ximage, x, y, value);
420 } 445 }
421 446
422 row += rowstride; 447 row += rowstride;
423 line += ximage->bytes_per_line; 448 line += ximage->bytes_per_line;
424 } 449 }
427 XDestroyImage (ximage); 452 XDestroyImage (ximage);
428 return true; 453 return true;
429} 454}
430 455
431bool 456bool
432rxvt_term::render_image (unsigned long tr_flags) 457rxvt_term::render_image (rxvt_image &image)
433{ 458{
459 GdkPixbuf *pixbuf = image.pixbuf;
434 if (!pixbuf) 460 if (!pixbuf)
435 return false; 461 return false;
436 462
437 if (tr_flags 463 bool need_blend = bg_flags & BG_IS_VALID;
464
465 if (need_blend
438 && !(bg_flags & BG_HAS_RENDER)) 466 && !(bg_flags & BG_HAS_RENDER))
439 return false; 467 return false;
440 468
441 GdkPixbuf *result; 469 GdkPixbuf *result;
442 470
451 int x = 0; 479 int x = 0;
452 int y = 0; 480 int y = 0;
453 int w = 0; 481 int w = 0;
454 int h = 0; 482 int h = 0;
455 483
456 get_image_geometry (image_width, image_height, w, h, x, y); 484 get_image_geometry (image, w, h, x, y);
457 485
458 if (!(bg_flags & BG_ROOT_ALIGN) 486 if (!(image.flags & IM_ROOT_ALIGN)
459 && (x >= target_width 487 && (x >= target_width
460 || y >= target_height 488 || y >= target_height
461 || x + w <= 0 489 || x + w <= 0
462 || y + h <= 0)) 490 || y + h <= 0))
463 return false; 491 return false;
477 505
478 bool ret = false; 506 bool ret = false;
479 507
480 XGCValues gcv; 508 XGCValues gcv;
481 GC gc; 509 GC gc;
482 Pixmap root_pmap; 510 Pixmap tmp_pixmap;
483 511
484 image_width = gdk_pixbuf_get_width (result); 512 image_width = gdk_pixbuf_get_width (result);
485 image_height = gdk_pixbuf_get_height (result); 513 image_height = gdk_pixbuf_get_height (result);
486 514
487 if (tr_flags) 515 if (need_blend)
488 { 516 {
489 root_pmap = bg_pixmap; 517 tmp_pixmap = bg_pixmap;
490 bg_pixmap = None; 518 bg_pixmap = None;
491 } 519 }
492 else 520 else
493 { 521 {
494 if (bg_flags & BG_TILE) 522 if (image.flags & IM_TILE)
495 { 523 {
496 new_pmap_width = min (image_width, target_width); 524 new_pmap_width = min (image_width, target_width);
497 new_pmap_height = min (image_height, target_height); 525 new_pmap_height = min (image_height, target_height);
498 } 526 }
499 } 527 }
512 gcv.foreground = pix_colors[Color_bg]; 540 gcv.foreground = pix_colors[Color_bg];
513 gc = XCreateGC (dpy, vt, GCForeground, &gcv); 541 gc = XCreateGC (dpy, vt, GCForeground, &gcv);
514 542
515 if (gc) 543 if (gc)
516 { 544 {
517 if (bg_flags & BG_TILE) 545 if (image.flags & IM_TILE)
518 { 546 {
519 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth); 547 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth);
520 pixbuf_to_pixmap (result, tile, gc, 548 pixbuf_to_pixmap (result, tile, gc,
521 0, 0, 549 0, 0,
522 0, 0, 550 0, 0,
550 dst_x, dst_y, 578 dst_x, dst_y,
551 dst_width, dst_height); 579 dst_width, dst_height);
552 } 580 }
553 581
554#if XRENDER 582#if XRENDER
555 if (tr_flags) 583 if (need_blend)
556 { 584 {
557 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 585 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
558 586
559 Picture src = XRenderCreatePicture (dpy, root_pmap, format, 0, 0); 587 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, format, 0, 0);
560 588
561 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0); 589 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
562 590
563 Picture mask = create_xrender_mask (dpy, vt, False, False); 591 Picture mask = create_xrender_mask (dpy, vt, False, False);
564 592
584 } 612 }
585 613
586 if (result != pixbuf) 614 if (result != pixbuf)
587 g_object_unref (result); 615 g_object_unref (result);
588 616
589 if (tr_flags) 617 if (need_blend)
590 XFreePixmap (dpy, root_pmap); 618 XFreePixmap (dpy, tmp_pixmap);
591 619
592 return ret; 620 return ret;
593} 621}
594# endif /* HAVE_PIXBUF */ 622# endif /* HAVE_PIXBUF */
595 623
596bool 624bool
597rxvt_term::bg_set_file (const char *file) 625rxvt_image::set_file (const char *file)
598{ 626{
599 if (!file || !*file) 627 if (!file || !*file)
600 return false; 628 return false;
601 629
602 bool ret = false; 630 bool ret = false;
616 if (image) 644 if (image)
617 { 645 {
618 if (pixbuf) 646 if (pixbuf)
619 g_object_unref (pixbuf); 647 g_object_unref (pixbuf);
620 pixbuf = image; 648 pixbuf = image;
621 bg_flags |= BG_IS_FROM_FILE; 649 flags |= IM_IS_SET;
622 ret = true; 650 ret = true;
623 } 651 }
624# endif 652# endif
625 653
626 if (ret) 654 if (ret)
627 { 655 {
628 if (p) 656 if (p)
629 bg_set_geometry (p + 1); 657 set_geometry (p + 1);
630 else 658 else
631 bg_set_default_geometry (); 659 set_default_geometry ();
632 } 660 }
633 661
634 return ret; 662 return ret;
635} 663}
636 664
637# endif /* BG_IMAGE_FROM_FILE */ 665# endif /* BG_IMAGE_FROM_FILE */
638 666
639# ifdef ENABLE_TRANSPARENCY 667# ifdef ENABLE_TRANSPARENCY
640bool
641rxvt_term::bg_set_transparent ()
642{
643 if (!(bg_flags & BG_IS_TRANSPARENT))
644 {
645 bg_flags |= BG_IS_TRANSPARENT;
646 return true;
647 }
648
649 return false;
650}
651
652bool 668bool
653rxvt_term::bg_set_blur (const char *geom) 669rxvt_term::bg_set_blur (const char *geom)
654{ 670{
655 bool changed = false; 671 bool changed = false;
656 unsigned int hr, vr; 672 unsigned int hr, vr;
675 { 691 {
676 changed = true; 692 changed = true;
677 v_blurRadius = vr; 693 v_blurRadius = vr;
678 } 694 }
679 695
680 if (h_blurRadius == 0 || v_blurRadius == 0)
681 bg_flags &= ~BG_NEEDS_BLUR;
682 else
683 bg_flags |= BG_NEEDS_BLUR;
684
685 return changed; 696 return changed;
686} 697}
687 698
688void 699bool
689rxvt_term::set_tint_shade_flags () 700rxvt_term::bg_set_tint (rxvt_color &new_tint)
690{ 701{
702 if (!(bg_flags & BG_TINT_SET) || tint != new_tint)
703 {
704 tint = new_tint;
705 bg_flags |= BG_TINT_SET;
706
691 rgba c; 707 rgba c;
692 bool has_shade = shade != 100;
693
694 bg_flags &= ~BG_TINT_FLAGS;
695
696 if (bg_flags & BG_TINT_SET)
697 {
698 tint.get (c); 708 tint.get (c);
699 if (!has_shade
700 && (c.r <= 0x00ff || c.r >= 0xff00) 709 if ((c.r <= 0x00ff || c.r >= 0xff00)
701 && (c.g <= 0x00ff || c.g >= 0xff00) 710 && (c.g <= 0x00ff || c.g >= 0xff00)
702 && (c.b <= 0x00ff || c.b >= 0xff00)) 711 && (c.b <= 0x00ff || c.b >= 0xff00))
703 bg_flags |= BG_TINT_BITAND; 712 bg_flags |= BG_TINT_BITAND;
704 } 713 else
705
706 if (has_shade || (bg_flags & BG_TINT_SET))
707 bg_flags |= BG_NEEDS_TINT;
708}
709
710bool
711rxvt_term::bg_set_tint (rxvt_color &new_tint)
712{
713 if (!(bg_flags & BG_TINT_SET) || tint != new_tint)
714 {
715 tint = new_tint;
716 bg_flags |= BG_TINT_SET; 714 bg_flags &= ~BG_TINT_BITAND;
717 set_tint_shade_flags (); 715
718 return true; 716 return true;
719 } 717 }
720 718
721 return false; 719 return false;
722} 720}
731 new_shade = 200 - (100 + new_shade); 729 new_shade = 200 - (100 + new_shade);
732 730
733 if (new_shade != shade) 731 if (new_shade != shade)
734 { 732 {
735 shade = new_shade; 733 shade = new_shade;
736 set_tint_shade_flags ();
737 return true; 734 return true;
738 } 735 }
739 736
740 return false; 737 return false;
741} 738}
762 params[i+2] = XDoubleToFixed (kernel[i] / sum); 759 params[i+2] = XDoubleToFixed (kernel[i] / sum);
763} 760}
764#endif 761#endif
765 762
766bool 763bool
767rxvt_term::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height, int depth) 764rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height)
768{ 765{
769 bool ret = false; 766 bool ret = false;
770#if XRENDER 767#if XRENDER
771 if (!(bg_flags & BG_HAS_RENDER_CONV)) 768 if (!(bg_flags & BG_HAS_RENDER_CONV))
772 return false; 769 return false;
827#endif 824#endif
828 return ret; 825 return ret;
829} 826}
830 827
831bool 828bool
832rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 829rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height)
833{ 830{
834 bool ret = false; 831 bool ret = false;
835 832
836 if (bg_flags & BG_TINT_BITAND) 833 if (shade == 100 && (bg_flags & BG_TINT_BITAND))
837 { 834 {
838 XGCValues gcv; 835 XGCValues gcv;
839 GC gc; 836 GC gc;
840 837
841 /* In this case we can tint image server-side getting significant 838 /* In this case we can tint image server-side getting significant
922/* 919/*
923 * Builds a pixmap of the same size as the terminal window that contains 920 * Builds a pixmap of the same size as the terminal window that contains
924 * the tiled portion of the root pixmap that is supposed to be covered by 921 * the tiled portion of the root pixmap that is supposed to be covered by
925 * our window. 922 * our window.
926 */ 923 */
927unsigned long 924bool
928rxvt_term::make_transparency_pixmap () 925rxvt_term::make_transparency_pixmap ()
929{ 926{
930 unsigned long result = 0; 927 bool ret = false;
931 928
932 /* root dimensions may change from call to call - but Display structure should 929 /* root dimensions may change from call to call - but Display structure should
933 * be always up-to-date, so let's use it : 930 * be always up-to-date, so let's use it :
934 */ 931 */
935 int screen = display->screen; 932 int screen = display->screen;
1016 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 1013 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1017 1014
1018 if (gc) 1015 if (gc)
1019 { 1016 {
1020 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); 1017 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1021 result |= BG_IS_VALID | (bg_flags & BG_EFFECTS_FLAGS); 1018 ret = true;
1019 bool need_blur = h_blurRadius && v_blurRadius;
1020 bool need_tint = shade != 100 || (bg_flags & BG_TINT_SET);
1022 1021
1023 if (!(bg_flags & BG_CLIENT_RENDER)) 1022 if (!(bg_flags & BG_CLIENT_RENDER))
1024 { 1023 {
1025 if (bg_flags & BG_NEEDS_BLUR) 1024 if (need_blur)
1026 {
1027 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth))
1028 result &= ~BG_NEEDS_BLUR;
1029 } 1025 {
1030 if (bg_flags & BG_NEEDS_TINT) 1026 if (blur_pixmap (bg_pixmap, window_width, window_height))
1027 need_blur = false;
1031 { 1028 }
1029 if (need_tint)
1030 {
1032 if (tint_pixmap (bg_pixmap, visual, window_width, window_height)) 1031 if (tint_pixmap (bg_pixmap, window_width, window_height))
1033 result &= ~BG_NEEDS_TINT; 1032 need_tint = false;
1034 } 1033 }
1035 if (result & BG_NEEDS_TINT) 1034 if (need_tint)
1036 { 1035 {
1037 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap); 1036 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1038 if (ximage) 1037 if (ximage)
1039 { 1038 {
1040 /* our own client-side tinting */ 1039 /* our own client-side tinting */
1041 tint_ximage (DefaultVisual (dpy, display->screen), ximage); 1040 tint_ximage (ximage);
1042 1041
1043 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); 1042 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
1044 XDestroyImage (ximage); 1043 XDestroyImage (ximage);
1045 } 1044 }
1046 } 1045 }
1050 } 1049 }
1051 1050
1052 if (recoded_root_pmap != root_pixmap) 1051 if (recoded_root_pmap != root_pixmap)
1053 XFreePixmap (dpy, recoded_root_pmap); 1052 XFreePixmap (dpy, recoded_root_pmap);
1054 1053
1055 return result; 1054 return ret;
1056} 1055}
1057 1056
1058void 1057void
1059rxvt_term::bg_set_root_pixmap () 1058rxvt_term::bg_set_root_pixmap ()
1060{ 1059{
1067# endif /* ENABLE_TRANSPARENCY */ 1066# endif /* ENABLE_TRANSPARENCY */
1068 1067
1069bool 1068bool
1070rxvt_term::bg_render () 1069rxvt_term::bg_render ()
1071{ 1070{
1072 unsigned long tr_flags = 0;
1073
1074 bg_invalidate (); 1071 bg_invalidate ();
1075# ifdef ENABLE_TRANSPARENCY 1072# ifdef ENABLE_TRANSPARENCY
1076 if (bg_flags & BG_IS_TRANSPARENT) 1073 if (bg_flags & BG_IS_TRANSPARENT)
1077 { 1074 {
1078 /* we need to re-generate transparency pixmap in that case ! */ 1075 /* we need to re-generate transparency pixmap in that case ! */
1079 tr_flags = make_transparency_pixmap (); 1076 if (make_transparency_pixmap ())
1080 if (tr_flags)
1081 bg_flags |= BG_IS_VALID; 1077 bg_flags |= BG_IS_VALID;
1082 } 1078 }
1083# endif 1079# endif
1084 1080
1085# ifdef BG_IMAGE_FROM_FILE 1081# ifdef BG_IMAGE_FROM_FILE
1086 if ((bg_flags & BG_IS_FROM_FILE) 1082 if (bg_image.flags & IM_IS_SET)
1087 || (tr_flags & BG_EFFECTS_FLAGS))
1088 { 1083 {
1089 if (render_image (tr_flags)) 1084 if (render_image (bg_image))
1090 bg_flags |= BG_IS_VALID; 1085 bg_flags |= BG_IS_VALID;
1091 } 1086 }
1092# endif 1087# endif
1093 1088
1094 if (!(bg_flags & BG_IS_VALID)) 1089 if (!(bg_flags & BG_IS_VALID))
1148 lookup[i] = (tmp / 0xffff) << sh; 1143 lookup[i] = (tmp / 0xffff) << sh;
1149 } 1144 }
1150} 1145}
1151 1146
1152void 1147void
1153rxvt_term::tint_ximage (Visual *visual, XImage *ximage) 1148rxvt_term::tint_ximage (XImage *ximage)
1154{ 1149{
1150 unsigned int size_r, size_g, size_b;
1155 int sh_r, sh_g, sh_b; 1151 int sh_r, sh_g, sh_b;
1156 uint32_t mask_r, mask_g, mask_b; 1152 uint32_t mask_r, mask_g, mask_b;
1157 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1153 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1158 unsigned short low; 1154 unsigned short low;
1159 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 1155 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1164 mask_r = visual->red_mask; 1160 mask_r = visual->red_mask;
1165 mask_g = visual->green_mask; 1161 mask_g = visual->green_mask;
1166 mask_b = visual->blue_mask; 1162 mask_b = visual->blue_mask;
1167 1163
1168 /* boring lookup table pre-initialization */ 1164 /* boring lookup table pre-initialization */
1169 switch (ximage->depth) 1165 sh_r = ecb_ctz32 (mask_r);
1170 { 1166 sh_g = ecb_ctz32 (mask_g);
1171 case 15: 1167 sh_b = ecb_ctz32 (mask_b);
1172 if ((mask_r != 0x7c00) || 1168
1173 (mask_g != 0x03e0) || 1169 size_r = mask_r >> sh_r;
1174 (mask_b != 0x001f)) 1170 size_g = mask_g >> sh_g;
1171 size_b = mask_b >> sh_b;
1172
1173 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1175 return; 1174 return;
1176 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+32+32)); 1175
1176 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1177 lookup_r = lookup; 1177 lookup_r = lookup;
1178 lookup_g = lookup+32; 1178 lookup_g = lookup + size_r;
1179 lookup_b = lookup+32+32; 1179 lookup_b = lookup + size_r + size_g;
1180 sh_r = 10;
1181 sh_g = 5;
1182 sh_b = 0;
1183 break;
1184 case 16:
1185 if ((mask_r != 0xf800) ||
1186 (mask_g != 0x07e0) ||
1187 (mask_b != 0x001f))
1188 return;
1189 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+64+32));
1190 lookup_r = lookup;
1191 lookup_g = lookup+32;
1192 lookup_b = lookup+32+64;
1193 sh_r = 11;
1194 sh_g = 5;
1195 sh_b = 0;
1196 break;
1197 case 24:
1198 if ((mask_r != 0xff0000) ||
1199 (mask_g != 0x00ff00) ||
1200 (mask_b != 0x0000ff))
1201 return;
1202 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1203 lookup_r = lookup;
1204 lookup_g = lookup+256;
1205 lookup_b = lookup+256+256;
1206 sh_r = 16;
1207 sh_g = 8;
1208 sh_b = 0;
1209 break;
1210 case 32:
1211 if ((mask_r != 0xff0000) ||
1212 (mask_g != 0x00ff00) ||
1213 (mask_b != 0x0000ff))
1214 return;
1215 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1216 lookup_r = lookup;
1217 lookup_g = lookup+256;
1218 lookup_b = lookup+256+256;
1219 sh_r = 16;
1220 sh_g = 8;
1221 sh_b = 0;
1222 break;
1223 default:
1224 return; /* we do not support this color depth */
1225 }
1226 1180
1227 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); 1181 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1228 1182
1229 if (bg_flags & BG_TINT_SET) 1183 if (bg_flags & BG_TINT_SET)
1230 tint.get (c); 1184 tint.get (c);
1252 fill_lut (lookup_g, mask_g, sh_g, low, c.g); 1206 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1253 fill_lut (lookup_b, mask_b, sh_b, low, c.b); 1207 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1254 1208
1255 /* apply table to input image (replacing colors by newly calculated ones) */ 1209 /* apply table to input image (replacing colors by newly calculated ones) */
1256 if (ximage->bits_per_pixel == 32 1210 if (ximage->bits_per_pixel == 32
1257 && (ximage->depth == 24 || ximage->depth == 32)
1258 && ximage->byte_order == host_byte_order) 1211 && ximage->byte_order == host_byte_order)
1259 { 1212 {
1260 uint32_t *p1, *pf, *p, *pl; 1213 char *line = ximage->data;
1261 p1 = (uint32_t *) ximage->data;
1262 pf = (uint32_t *) (ximage->data + ximage->height * ximage->bytes_per_line);
1263 1214
1264 while (p1 < pf) 1215 for (int y = 0; y < ximage->height; y++)
1265 {
1266 p = p1;
1267 pl = p1 + ximage->width;
1268 for (; p < pl; p++)
1269 { 1216 {
1270 *p = lookup_r[(*p & 0xff0000) >> 16] | 1217 uint32_t *p = (uint32_t *)line;
1271 lookup_g[(*p & 0x00ff00) >> 8] | 1218 for (int x = 0; x < ximage->width; x++)
1272 lookup_b[(*p & 0x0000ff)] |
1273 (*p & 0xff000000);
1274 } 1219 {
1275 p1 = (uint32_t *) ((char *) p1 + ximage->bytes_per_line); 1220 *p = lookup_r[(*p & mask_r) >> sh_r] |
1221 lookup_g[(*p & mask_g) >> sh_g] |
1222 lookup_b[(*p & mask_b) >> sh_b];
1223 p++;
1224 }
1225 line += ximage->bytes_per_line;
1276 } 1226 }
1277 } 1227 }
1278 else 1228 else
1279 { 1229 {
1280 for (int y = 0; y < ximage->height; y++) 1230 for (int y = 0; y < ximage->height; y++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines