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.202 by sf-exg, Sat Feb 25 22:20:47 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 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 (bool transparent) 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 (transparent 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 (transparent) 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 (transparent) 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 (transparent) 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;
622 ret = true; 649 ret = true;
623 } 650 }
624# endif 651# endif
625 652
626 if (ret) 653 if (ret)
627 { 654 {
655 flags = IM_IS_SET | IM_IS_SIZE_SENSITIVE;
656 h_scale = v_scale = defaultScale;
657 h_align = v_align = defaultAlign;
658
628 if (p) 659 if (p)
629 bg_set_geometry (p + 1); 660 set_geometry (p + 1);
630 else
631 bg_set_default_geometry ();
632 } 661 }
633 662
634 return ret; 663 return ret;
635} 664}
636 665
637# endif /* BG_IMAGE_FROM_FILE */ 666# endif /* BG_IMAGE_FROM_FILE */
638 667
639# ifdef ENABLE_TRANSPARENCY 668# 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 669bool
653rxvt_term::bg_set_blur (const char *geom) 670rxvt_term::bg_set_blur (const char *geom)
654{ 671{
655 bool changed = false; 672 bool changed = false;
656 unsigned int hr, vr; 673 unsigned int hr, vr;
675 { 692 {
676 changed = true; 693 changed = true;
677 v_blurRadius = vr; 694 v_blurRadius = vr;
678 } 695 }
679 696
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; 697 return changed;
686} 698}
687 699
688void 700bool
689rxvt_term::set_tint_shade_flags () 701rxvt_term::bg_set_tint (rxvt_color &new_tint)
690{ 702{
703 if (!(bg_flags & BG_TINT_SET) || tint != new_tint)
704 {
705 tint = new_tint;
706 bg_flags |= BG_TINT_SET;
707
691 rgba c; 708 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); 709 tint.get (c);
699 if (!has_shade
700 && (c.r <= 0x00ff || c.r >= 0xff00) 710 if ((c.r <= 0x00ff || c.r >= 0xff00)
701 && (c.g <= 0x00ff || c.g >= 0xff00) 711 && (c.g <= 0x00ff || c.g >= 0xff00)
702 && (c.b <= 0x00ff || c.b >= 0xff00)) 712 && (c.b <= 0x00ff || c.b >= 0xff00))
703 bg_flags |= BG_TINT_BITAND; 713 bg_flags |= BG_TINT_BITAND;
704 } 714 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; 715 bg_flags &= ~BG_TINT_BITAND;
717 set_tint_shade_flags (); 716
718 return true; 717 return true;
719 } 718 }
720 719
721 return false; 720 return false;
722} 721}
731 new_shade = 200 - (100 + new_shade); 730 new_shade = 200 - (100 + new_shade);
732 731
733 if (new_shade != shade) 732 if (new_shade != shade)
734 { 733 {
735 shade = new_shade; 734 shade = new_shade;
736 set_tint_shade_flags ();
737 return true; 735 return true;
738 } 736 }
739 737
740 return false; 738 return false;
741} 739}
762 params[i+2] = XDoubleToFixed (kernel[i] / sum); 760 params[i+2] = XDoubleToFixed (kernel[i] / sum);
763} 761}
764#endif 762#endif
765 763
766bool 764bool
767rxvt_term::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height, int depth) 765rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height)
768{ 766{
769 bool ret = false; 767 bool ret = false;
770#if XRENDER 768#if XRENDER
771 if (!(bg_flags & BG_HAS_RENDER_CONV)) 769 if (!(bg_flags & BG_HAS_RENDER_CONV))
772 return false; 770 return false;
827#endif 825#endif
828 return ret; 826 return ret;
829} 827}
830 828
831bool 829bool
832rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 830rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height)
833{ 831{
834 bool ret = false; 832 bool ret = false;
835 833
836 if (bg_flags & BG_TINT_BITAND) 834 if (shade == 100 && (bg_flags & BG_TINT_BITAND))
837 { 835 {
838 XGCValues gcv; 836 XGCValues gcv;
839 GC gc; 837 GC gc;
840 838
841 /* In this case we can tint image server-side getting significant 839 /* In this case we can tint image server-side getting significant
1017 1015
1018 if (gc) 1016 if (gc)
1019 { 1017 {
1020 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); 1018 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1021 ret = true; 1019 ret = true;
1022 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);
1023 1022
1024 if (!(bg_flags & BG_CLIENT_RENDER)) 1023 if (!(bg_flags & BG_CLIENT_RENDER))
1025 { 1024 {
1026 if (bg_flags & BG_NEEDS_BLUR) 1025 if (need_blur)
1027 {
1028 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth))
1029 tr_flags &= ~BG_NEEDS_BLUR;
1030 } 1026 {
1031 if (bg_flags & BG_NEEDS_TINT) 1027 if (blur_pixmap (bg_pixmap, window_width, window_height))
1028 need_blur = false;
1032 { 1029 }
1030 if (need_tint)
1031 {
1033 if (tint_pixmap (bg_pixmap, visual, window_width, window_height)) 1032 if (tint_pixmap (bg_pixmap, window_width, window_height))
1034 tr_flags &= ~BG_NEEDS_TINT; 1033 need_tint = false;
1035 } 1034 }
1036 if (tr_flags & BG_NEEDS_TINT) 1035 if (need_tint)
1037 { 1036 {
1038 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);
1039 if (ximage) 1038 if (ximage)
1040 { 1039 {
1041 /* our own client-side tinting */ 1040 /* our own client-side tinting */
1042 tint_ximage (DefaultVisual (dpy, display->screen), ximage); 1041 tint_ximage (ximage);
1043 1042
1044 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);
1045 XDestroyImage (ximage); 1044 XDestroyImage (ximage);
1046 } 1045 }
1047 } 1046 }
1068# endif /* ENABLE_TRANSPARENCY */ 1067# endif /* ENABLE_TRANSPARENCY */
1069 1068
1070bool 1069bool
1071rxvt_term::bg_render () 1070rxvt_term::bg_render ()
1072{ 1071{
1073 bool transparent = false;
1074
1075 bg_invalidate (); 1072 bg_invalidate ();
1076# ifdef ENABLE_TRANSPARENCY 1073# ifdef ENABLE_TRANSPARENCY
1077 if (bg_flags & BG_IS_TRANSPARENT) 1074 if (bg_flags & BG_IS_TRANSPARENT)
1078 { 1075 {
1079 /* we need to re-generate transparency pixmap in that case ! */ 1076 /* we need to re-generate transparency pixmap in that case ! */
1080 transparent = make_transparency_pixmap (); 1077 if (make_transparency_pixmap ())
1081 if (transparent)
1082 bg_flags |= BG_IS_VALID; 1078 bg_flags |= BG_IS_VALID;
1083 } 1079 }
1084# endif 1080# endif
1085 1081
1086# ifdef BG_IMAGE_FROM_FILE 1082# ifdef BG_IMAGE_FROM_FILE
1087 if (bg_flags & BG_IS_FROM_FILE) 1083 if (bg_image.flags & IM_IS_SET)
1088 { 1084 {
1089 if (render_image (transparent)) 1085 if (render_image (bg_image))
1090 bg_flags |= BG_IS_VALID; 1086 bg_flags |= BG_IS_VALID;
1091 } 1087 }
1092# endif 1088# endif
1093 1089
1094 if (!(bg_flags & BG_IS_VALID)) 1090 if (!(bg_flags & BG_IS_VALID))
1148 lookup[i] = (tmp / 0xffff) << sh; 1144 lookup[i] = (tmp / 0xffff) << sh;
1149 } 1145 }
1150} 1146}
1151 1147
1152void 1148void
1153rxvt_term::tint_ximage (Visual *visual, XImage *ximage) 1149rxvt_term::tint_ximage (XImage *ximage)
1154{ 1150{
1151 unsigned int size_r, size_g, size_b;
1155 int sh_r, sh_g, sh_b; 1152 int sh_r, sh_g, sh_b;
1156 uint32_t mask_r, mask_g, mask_b; 1153 uint32_t mask_r, mask_g, mask_b;
1157 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1154 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1158 unsigned short low; 1155 unsigned short low;
1159 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 1156 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1164 mask_r = visual->red_mask; 1161 mask_r = visual->red_mask;
1165 mask_g = visual->green_mask; 1162 mask_g = visual->green_mask;
1166 mask_b = visual->blue_mask; 1163 mask_b = visual->blue_mask;
1167 1164
1168 /* boring lookup table pre-initialization */ 1165 /* boring lookup table pre-initialization */
1169 switch (ximage->depth) 1166 sh_r = ecb_ctz32 (mask_r);
1170 { 1167 sh_g = ecb_ctz32 (mask_g);
1171 case 15: 1168 sh_b = ecb_ctz32 (mask_b);
1172 if ((mask_r != 0x7c00) || 1169
1173 (mask_g != 0x03e0) || 1170 size_r = mask_r >> sh_r;
1174 (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)
1175 return; 1175 return;
1176 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));
1177 lookup_r = lookup; 1178 lookup_r = lookup;
1178 lookup_g = lookup+32; 1179 lookup_g = lookup + size_r;
1179 lookup_b = lookup+32+32; 1180 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 1181
1227 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); 1182 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1228 1183
1229 if (bg_flags & BG_TINT_SET) 1184 if (bg_flags & BG_TINT_SET)
1230 tint.get (c); 1185 tint.get (c);
1252 fill_lut (lookup_g, mask_g, sh_g, low, c.g); 1207 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1253 fill_lut (lookup_b, mask_b, sh_b, low, c.b); 1208 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1254 1209
1255 /* apply table to input image (replacing colors by newly calculated ones) */ 1210 /* apply table to input image (replacing colors by newly calculated ones) */
1256 if (ximage->bits_per_pixel == 32 1211 if (ximage->bits_per_pixel == 32
1257 && (ximage->depth == 24 || ximage->depth == 32)
1258 && ximage->byte_order == host_byte_order) 1212 && ximage->byte_order == host_byte_order)
1259 { 1213 {
1260 uint32_t *p1, *pf, *p, *pl; 1214 char *line = ximage->data;
1261 p1 = (uint32_t *) ximage->data;
1262 pf = (uint32_t *) (ximage->data + ximage->height * ximage->bytes_per_line);
1263 1215
1264 while (p1 < pf) 1216 for (int y = 0; y < ximage->height; y++)
1265 {
1266 p = p1;
1267 pl = p1 + ximage->width;
1268 for (; p < pl; p++)
1269 { 1217 {
1270 *p = lookup_r[(*p & 0xff0000) >> 16] | 1218 uint32_t *p = (uint32_t *)line;
1271 lookup_g[(*p & 0x00ff00) >> 8] | 1219 for (int x = 0; x < ximage->width; x++)
1272 lookup_b[(*p & 0x0000ff)] |
1273 (*p & 0xff000000);
1274 } 1220 {
1275 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;
1276 } 1227 }
1277 } 1228 }
1278 else 1229 else
1279 { 1230 {
1280 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