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.200 by sf-exg, Thu Jan 19 13:33:43 2012 UTC vs.
Revision 1.228 by sf-exg, Thu May 31 20:23:51 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_AFTERIMAGE 63# ifdef BG_IMAGE_FROM_FILE
64 if (original_asim) 64 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
65 safe_asimage_destroy (original_asim); 65 bg_image->destroy ();
66 if (asv)
67 destroy_asvisual (asv, 0);
68 if (asimman)
69 destroy_image_manager (asimman, 0);
70#endif 66# endif
71
72#ifdef HAVE_PIXBUF
73 if (pixbuf)
74 g_object_unref (pixbuf);
75#endif
76 67
77 if (bg_pixmap) 68 if (bg_pixmap)
78 XFreePixmap (dpy, bg_pixmap); 69 XFreePixmap (dpy, bg_pixmap);
79} 70}
80 71
99 if (bg_flags & BG_IS_TRANSPARENT) 90 if (bg_flags & BG_IS_TRANSPARENT)
100 return true; 91 return true;
101# endif 92# endif
102 93
103# ifdef BG_IMAGE_FROM_FILE 94# ifdef BG_IMAGE_FROM_FILE
104 if (bg_flags & BG_IS_FROM_FILE) 95 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
105 { 96 {
106 if (bg_flags & BG_IS_SIZE_SENSITIVE) 97 if ((bg_image->flags & IM_IS_SIZE_SENSITIVE)
98 || bg_image->width () > szHint.width
99 || bg_image->height () > szHint.height)
107 return true; 100 return true;
108 } 101 }
109# endif 102# endif
110 103
111 return false; 104 return false;
118 if (bg_flags & BG_IS_TRANSPARENT) 111 if (bg_flags & BG_IS_TRANSPARENT)
119 return true; 112 return true;
120# endif 113# endif
121 114
122# ifdef BG_IMAGE_FROM_FILE 115# ifdef BG_IMAGE_FROM_FILE
123 if (bg_flags & BG_IS_FROM_FILE) 116 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
124 { 117 {
125 if (bg_flags & BG_ROOT_ALIGN) 118 if (bg_image->flags & IM_ROOT_ALIGN)
126 return true; 119 return true;
127 } 120 }
128# endif 121# endif
129 122
130 return false; 123 return false;
132 125
133# ifdef BG_IMAGE_FROM_FILE 126# ifdef BG_IMAGE_FROM_FILE
134static inline int 127static inline int
135make_align_position (int align, int window_size, int image_size) 128make_align_position (int align, int window_size, int image_size)
136{ 129{
137 int diff = window_size - image_size;
138 int smaller = min (image_size, window_size);
139
140 if (align >= 0 && align <= 100) 130 if (align >= 0 && align <= 100)
141 return diff * align / 100; 131 return lerp (0, window_size - image_size, align);
142 else if (align > 100 && align <= 200) 132 else if (align > 100)
143 return (align - 100) * smaller / 100 + window_size - smaller; 133 return lerp (window_size - image_size, window_size, align - 100);
144 else if (align >= -100 && align < 0) 134 else
145 return (align + 100) * smaller / 100 - image_size; 135 return lerp (-image_size, 0, align + 100);
146 return 0;
147} 136}
148 137
149static inline int 138static inline int
150make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) 139make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
151{ 140{
161 150
162 min_it (dst_size, target_size - dst_pos); 151 min_it (dst_size, target_size - dst_pos);
163 return src_pos; 152 return src_pos;
164} 153}
165 154
155static void
156parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags)
157{
158 if (!strcasecmp (style, "tiled"))
159 {
160 flags = IM_TILE;
161 w = h = noScale;
162 x = y = 0;
163 }
164 else if (!strcasecmp (style, "aspect-stretched"))
165 {
166 flags = IM_KEEP_ASPECT;
167 w = h = windowScale;
168 x = y = centerAlign;
169 }
170 else if (!strcasecmp (style, "stretched"))
171 {
172 flags = 0;
173 w = h = windowScale;
174 x = y = centerAlign;
175 }
176 else if (!strcasecmp (style, "centered"))
177 {
178 flags = 0;
179 w = h = noScale;
180 x = y = centerAlign;
181 }
182 else if (!strcasecmp (style, "root-tiled"))
183 {
184 flags = IM_TILE|IM_ROOT_ALIGN;
185 w = h = noScale;
186 x = y = 0;
187 }
188}
189
166bool 190bool
167rxvt_term::bg_set_geometry (const char *geom, bool update) 191rxvt_image::set_geometry (const char *geom, bool update)
168{ 192{
169 bool changed = false; 193 bool changed = false;
170 int geom_flags = 0; 194 int geom_flags = 0;
171 int x = h_align; 195 int x = h_align;
172 int y = v_align; 196 int y = v_align;
173 unsigned int w = h_scale; 197 unsigned int w = h_scale;
174 unsigned int h = v_scale; 198 unsigned int h = v_scale;
175 unsigned long new_flags = 0; 199 uint8_t new_flags = 0;
176 200
177 if (geom == NULL) 201 if (geom == NULL)
178 return false; 202 return false;
179 203
180 if (geom[0]) 204 if (geom[0])
181 { 205 {
182 char **arr = rxvt_strsplit (':', geom); 206 char **arr = rxvt_strsplit (':', geom);
183 207
184 for (int i = 0; arr[i]; i++) 208 for (int i = 0; arr[i]; i++)
185 { 209 {
186 if (!strcasecmp (arr[i], "style=tiled")) 210 if (!strncasecmp (arr[i], "style=", 6))
187 { 211 {
188 new_flags = BG_TILE; 212 parse_style (arr[i] + 6, x, y, w, h, new_flags);
189 w = h = noScale;
190 x = y = 0;
191 geom_flags = WidthValue|HeightValue|XValue|YValue; 213 geom_flags = WidthValue|HeightValue|XValue|YValue;
192 } 214 }
193 else if (!strcasecmp (arr[i], "style=aspect-stretched"))
194 {
195 new_flags = BG_KEEP_ASPECT;
196 w = h = windowScale;
197 x = y = centerAlign;
198 geom_flags = WidthValue|HeightValue|XValue|YValue;
199 }
200 else if (!strcasecmp (arr[i], "style=stretched"))
201 {
202 new_flags = 0;
203 w = h = windowScale;
204 geom_flags = WidthValue|HeightValue;
205 }
206 else if (!strcasecmp (arr[i], "style=centered"))
207 {
208 new_flags = 0;
209 w = h = noScale;
210 x = y = centerAlign;
211 geom_flags = WidthValue|HeightValue|XValue|YValue;
212 }
213 else if (!strcasecmp (arr[i], "style=root-tiled"))
214 {
215 new_flags = BG_TILE|BG_ROOT_ALIGN;
216 w = h = noScale;
217 geom_flags = WidthValue|HeightValue;
218 }
219 else if (!strcasecmp (arr[i], "op=tile")) 215 else if (!strcasecmp (arr[i], "op=tile"))
220 new_flags |= BG_TILE; 216 new_flags |= IM_TILE;
221 else if (!strcasecmp (arr[i], "op=keep-aspect")) 217 else if (!strcasecmp (arr[i], "op=keep-aspect"))
222 new_flags |= BG_KEEP_ASPECT; 218 new_flags |= IM_KEEP_ASPECT;
223 else if (!strcasecmp (arr[i], "op=root-align")) 219 else if (!strcasecmp (arr[i], "op=root-align"))
224 new_flags |= BG_ROOT_ALIGN; 220 new_flags |= IM_ROOT_ALIGN;
225 221
226 // deprecated 222 // deprecated
227 else if (!strcasecmp (arr[i], "tile")) 223 else if (!strcasecmp (arr[i], "tile"))
228 { 224 {
229 new_flags |= BG_TILE; 225 new_flags |= IM_TILE;
230 w = h = noScale; 226 w = h = noScale;
231 geom_flags |= WidthValue|HeightValue; 227 geom_flags |= WidthValue|HeightValue;
232 } 228 }
233 else if (!strcasecmp (arr[i], "propscale")) 229 else if (!strcasecmp (arr[i], "propscale"))
234 { 230 {
235 new_flags |= BG_KEEP_ASPECT; 231 new_flags |= IM_KEEP_ASPECT;
236 w = h = windowScale; 232 w = h = windowScale;
237 geom_flags |= WidthValue|HeightValue; 233 geom_flags |= WidthValue|HeightValue;
238 } 234 }
239 else if (!strcasecmp (arr[i], "hscale")) 235 else if (!strcasecmp (arr[i], "hscale"))
240 { 236 {
241 new_flags |= BG_TILE; 237 new_flags |= IM_TILE;
242 w = windowScale; 238 w = windowScale;
243 h = noScale; 239 h = noScale;
244 geom_flags |= WidthValue|HeightValue; 240 geom_flags |= WidthValue|HeightValue;
245 } 241 }
246 else if (!strcasecmp (arr[i], "vscale")) 242 else if (!strcasecmp (arr[i], "vscale"))
247 { 243 {
248 new_flags |= BG_TILE; 244 new_flags |= IM_TILE;
249 h = windowScale; 245 h = windowScale;
250 w = noScale; 246 w = noScale;
251 geom_flags |= WidthValue|HeightValue; 247 geom_flags |= WidthValue|HeightValue;
252 } 248 }
253 else if (!strcasecmp (arr[i], "scale")) 249 else if (!strcasecmp (arr[i], "scale"))
261 x = y = centerAlign; 257 x = y = centerAlign;
262 geom_flags |= WidthValue|HeightValue|XValue|YValue; 258 geom_flags |= WidthValue|HeightValue|XValue|YValue;
263 } 259 }
264 else if (!strcasecmp (arr[i], "root")) 260 else if (!strcasecmp (arr[i], "root"))
265 { 261 {
266 new_flags |= BG_TILE|BG_ROOT_ALIGN; 262 new_flags |= IM_TILE|IM_ROOT_ALIGN;
267 w = h = noScale; 263 w = h = noScale;
268 geom_flags |= WidthValue|HeightValue; 264 geom_flags |= WidthValue|HeightValue;
269 } 265 }
270 266
271 else 267 else
273 } /* done parsing ops */ 269 } /* done parsing ops */
274 270
275 rxvt_free_strsplit (arr); 271 rxvt_free_strsplit (arr);
276 } 272 }
277 273
278 new_flags |= bg_flags & ~BG_GEOMETRY_FLAGS; 274 new_flags |= flags & ~IM_GEOMETRY_FLAGS;
279 275
280 if (!update) 276 if (!update)
281 { 277 {
282 if (!(geom_flags & XValue)) 278 if (!(geom_flags & XValue))
283 x = y = defaultAlign; 279 x = y = defaultAlign;
290 h = w; 286 h = w;
291 else if (!(geom_flags & WidthValue)) 287 else if (!(geom_flags & WidthValue))
292 w = h; 288 w = h;
293 } 289 }
294 290
295 min_it (w, 1000);
296 min_it (h, 1000);
297 clamp_it (x, -100, 200); 291 clamp_it (x, -100, 200);
298 clamp_it (y, -100, 200); 292 clamp_it (y, -100, 200);
299 293
300 if (bg_flags != new_flags 294 if (flags != new_flags
301 || h_scale != w 295 || h_scale != w
302 || v_scale != h 296 || v_scale != h
303 || h_align != x 297 || h_align != x
304 || v_align != y) 298 || v_align != y)
305 { 299 {
306 bg_flags = new_flags; 300 flags = new_flags;
307 h_scale = w; 301 h_scale = w;
308 v_scale = h; 302 v_scale = h;
309 h_align = x; 303 h_align = x;
310 v_align = y; 304 v_align = y;
311 changed = true; 305 changed = true;
312 } 306 }
313 307
308 if (is_size_sensitive ())
309 flags |= IM_IS_SIZE_SENSITIVE;
310 else
311 flags &= ~IM_IS_SIZE_SENSITIVE;
312
314 return changed; 313 return changed;
315} 314}
316 315
317void 316void
318rxvt_term::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y) 317rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
319{ 318{
319 int image_width = image.width ();
320 int image_height = image.height ();
320 int target_width = szHint.width; 321 int target_width = szHint.width;
321 int target_height = szHint.height; 322 int target_height = szHint.height;
323 int h_scale = min (image.h_scale, 32767 * 100 / target_width);
324 int v_scale = min (image.v_scale, 32767 * 100 / target_height);
322 325
323 w = h_scale * target_width / 100; 326 w = h_scale * target_width / 100;
324 h = v_scale * target_height / 100; 327 h = v_scale * target_height / 100;
325 328
326 if (bg_flags & BG_KEEP_ASPECT) 329 if (image.flags & IM_KEEP_ASPECT)
327 { 330 {
328 float scale = (float)w / image_width; 331 float scale = (float)w / image_width;
329 min_it (scale, (float)h / image_height); 332 min_it (scale, (float)h / image_height);
330 w = image_width * scale + 0.5; 333 w = image_width * scale + 0.5;
331 h = image_height * scale + 0.5; 334 h = image_height * scale + 0.5;
332 } 335 }
333 336
334 if (!w) w = image_width; 337 if (!w) w = image_width;
335 if (!h) h = image_height; 338 if (!h) h = image_height;
336 339
337 if (bg_flags & BG_ROOT_ALIGN) 340 if (image.flags & IM_ROOT_ALIGN)
338 { 341 {
339 x = -target_x; 342 x = -target_x;
340 y = -target_y; 343 y = -target_y;
341 } 344 }
342 else 345 else
343 { 346 {
344 x = make_align_position (h_align, target_width, w); 347 x = make_align_position (image.h_align, target_width, w);
345 y = make_align_position (v_align, target_height, h); 348 y = make_align_position (image.v_align, target_height, h);
346 }
347
348 bg_flags &= ~BG_IS_SIZE_SENSITIVE;
349 if (!(bg_flags & BG_TILE)
350 || h_scale || v_scale
351 || (!(bg_flags & BG_ROOT_ALIGN) && (h_align || v_align))
352 || w > target_width || h > target_height)
353 bg_flags |= BG_IS_SIZE_SENSITIVE;
354}
355
356# ifdef HAVE_AFTERIMAGE
357bool
358rxvt_term::render_image (unsigned long tr_flags)
359{
360 init_asv ();
361
362 ASImage *background = NULL;
363 ARGB32 background_tint = TINT_LEAVE_SAME;
364
365# ifdef ENABLE_TRANSPARENCY
366 if (tr_flags)
367 background = pixmap2ximage (asv, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, 100);
368
369 if (tr_flags & BG_NEEDS_TINT)
370 { 349 }
371 ShadingInfo as_shade; 350}
372 as_shade.shading = shade;
373 351
374 rgba c (rgba::MAX_CC,rgba::MAX_CC,rgba::MAX_CC); 352# ifdef HAVE_PIXBUF
375 if (bg_flags & BG_TINT_SET) 353bool
376 tint.get (c); 354rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
377 as_shade.tintColor.red = c.r; 355 int src_x, int src_y, int dst_x, int dst_y,
378 as_shade.tintColor.green = c.g; 356 unsigned int width, unsigned int height, bool argb)
379 as_shade.tintColor.blue = c.b; 357{
358 XImage *ximage;
359 char *line;
360 int width_r, width_g, width_b, width_a;
361 int sh_r, sh_g, sh_b, sh_a;
362 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
363 int rowstride;
364 int channels;
365 unsigned char *row;
380 366
381 background_tint = shading2tint32 (&as_shade); 367 if (visual->c_class != TrueColor)
368 return false;
369
370 if (argb)
382 } 371 {
383 372 red_mask = 0xff << 16;
384 if ((tr_flags & BG_NEEDS_BLUR) && background != NULL) 373 green_mask = 0xff << 8;
374 blue_mask = 0xff;
375 alpha_mask = 0xff << 24;
385 { 376 }
386 ASImage *tmp = blur_asimage_gauss (asv, background, h_blurRadius, v_blurRadius, 0xFFFFFFFF, 377 else
387 ASA_XImage, 378 {
388 100, ASIMAGE_QUALITY_DEFAULT); 379 red_mask = visual->red_mask;
380 green_mask = visual->green_mask;
381 blue_mask = visual->blue_mask;
382#if XRENDER
383 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
389 if (tmp) 384 if (format)
390 { 385 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
391 destroy_asimage (&background); 386 else
392 background = tmp; 387#endif
388 alpha_mask = 0;
389 }
390
391 width_r = ecb_popcount32 (red_mask);
392 width_g = ecb_popcount32 (green_mask);
393 width_b = ecb_popcount32 (blue_mask);
394 width_a = ecb_popcount32 (alpha_mask);
395
396 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
397 return false;
398
399 sh_r = ecb_ctz32 (red_mask);
400 sh_g = ecb_ctz32 (green_mask);
401 sh_b = ecb_ctz32 (blue_mask);
402 sh_a = ecb_ctz32 (alpha_mask);
403
404 if (width > 32767 || height > 32767)
405 return false;
406
407 ximage = XCreateImage (dpy, visual, argb ? 32 : depth, ZPixmap, 0, 0,
408 width, height, 32, 0);
409 if (!ximage)
410 return false;
411
412 if (height > INT_MAX / ximage->bytes_per_line
413 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
414 {
415 XDestroyImage (ximage);
416 return false;
417 }
418
419 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
420
421 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
422 channels = gdk_pixbuf_get_n_channels (pixbuf);
423 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
424 line = ximage->data;
425
426 rgba c (0, 0, 0);
427
428 if (channels == 4 && alpha_mask == 0)
429 {
430 pix_colors[Color_bg].get (c);
431 c.r >>= 8;
432 c.g >>= 8;
433 c.b >>= 8;
434 }
435
436 for (int y = 0; y < height; y++)
437 {
438 for (int x = 0; x < width; x++)
393 } 439 {
394 } 440 unsigned char *pixel = row + x * channels;
395# endif 441 uint32_t value;
442 unsigned char r, g, b, a;
396 443
397 ASImage *result = 0; 444 if (channels == 4)
445 {
446 a = pixel[3];
447 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
448 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
449 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
450 }
451 else
452 {
453 a = 0xff;
454 r = pixel[0];
455 g = pixel[1];
456 b = pixel[2];
457 }
458
459 value = ((r >> (8 - width_r)) << sh_r)
460 | ((g >> (8 - width_g)) << sh_g)
461 | ((b >> (8 - width_b)) << sh_b)
462 | ((a >> (8 - width_a)) << sh_a);
463
464 if (ximage->bits_per_pixel == 32)
465 ((uint32_t *)line)[x] = value;
466 else
467 XPutPixel (ximage, x, y, value);
468 }
469
470 row += rowstride;
471 line += ximage->bytes_per_line;
472 }
473
474 XPutImage (dpy, pixmap, gc, ximage, 0, 0, dst_x, dst_y, width, height);
475 XDestroyImage (ximage);
476 return true;
477}
478
479bool
480rxvt_term::render_image (rxvt_image &image)
481{
482 GdkPixbuf *pixbuf = image.pixbuf;
483 if (!pixbuf)
484 return false;
485
486 bool need_blend = bg_flags & BG_IS_VALID;
487
488 if (need_blend
489 && !(bg_flags & BG_HAS_RENDER))
490 return false;
491
492 GdkPixbuf *result;
493
494 int image_width = gdk_pixbuf_get_width (pixbuf);
495 int image_height = gdk_pixbuf_get_height (pixbuf);
398 496
399 int target_width = szHint.width; 497 int target_width = szHint.width;
400 int target_height = szHint.height; 498 int target_height = szHint.height;
401 int new_pmap_width = target_width; 499 int new_pmap_width = target_width;
402 int new_pmap_height = target_height; 500 int new_pmap_height = target_height;
404 int x = 0; 502 int x = 0;
405 int y = 0; 503 int y = 0;
406 int w = 0; 504 int w = 0;
407 int h = 0; 505 int h = 0;
408 506
409 if (original_asim) 507 get_image_geometry (image, w, h, x, y);
410 get_image_geometry (original_asim->width, original_asim->height, w, h, x, y);
411 508
412 if (!original_asim 509 if (!(image.flags & IM_ROOT_ALIGN)
413 || (!(bg_flags & BG_ROOT_ALIGN)
414 && (x >= target_width 510 && (x >= target_width
415 || y >= target_height 511 || y >= target_height
416 || x + w <= 0 512 || x + w <= 0
417 || y + h <= 0))) 513 || y + h <= 0))
418 { 514 return false;
419 if (background)
420 {
421 new_pmap_width = background->width;
422 new_pmap_height = background->height;
423 result = background;
424 515
425 if (background_tint != TINT_LEAVE_SAME) 516 result = pixbuf;
426 { 517
427 ASImage *tmp = tile_asimage (asv, background, 0, 0, 518 if (w != image_width
428 target_width, target_height, background_tint, 519 || h != image_height)
429 ASA_XImage, 100, ASIMAGE_QUALITY_DEFAULT);
430 if (tmp)
431 result = tmp;
432 }
433 }
434 else
435 new_pmap_width = new_pmap_height = 0;
436 } 520 {
521 result = gdk_pixbuf_scale_simple (pixbuf,
522 w, h,
523 GDK_INTERP_BILINEAR);
524 }
525
526 if (!result)
527 return false;
528
529 bool ret = false;
530
531 XGCValues gcv;
532 GC gc;
533 Pixmap tmp_pixmap;
534
535 image_width = gdk_pixbuf_get_width (result);
536 image_height = gdk_pixbuf_get_height (result);
537
538 if (need_blend)
539 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
437 else 540 else
438 { 541 {
439 result = original_asim; 542 // optimise bg pixmap size when tiling, but only if there are no
440 543 // other pixbufs to render. Otherwise, the bg pixmap size must
441 if (w != original_asim->width 544 // be equal to the window size.
442 || h != original_asim->height) 545 if ((image.flags & IM_TILE)
443 { 546 && image_vec.size () == 1)
444 result = scale_asimage (asv, original_asim,
445 w, h,
446 ASA_XImage,
447 100, ASIMAGE_QUALITY_DEFAULT);
448 } 547 {
449 548 new_pmap_width = min (image_width, target_width);
450 if (background == NULL) 549 new_pmap_height = min (image_height, target_height);
451 { 550 }
452 if (bg_flags & BG_TILE)
453 {
454 /* if tiling - pixmap has to be sized exactly as the image,
455 but there is no need to make it bigger than the window! */
456 new_pmap_width = min (result->width, target_width);
457 new_pmap_height = min (result->height, target_height);
458 551
459 /* we also need to tile our image in both directions */
460 ASImage *tmp = tile_asimage (asv, result,
461 (int)result->width - x,
462 (int)result->height - y,
463 new_pmap_width,
464 new_pmap_height,
465 TINT_LEAVE_SAME, ASA_XImage,
466 100, ASIMAGE_QUALITY_DEFAULT);
467 if (tmp)
468 {
469 if (result != original_asim)
470 destroy_asimage (&result);
471
472 result = tmp;
473 }
474 }
475 }
476 else
477 {
478 /* if blending background and image - pixmap has to be sized same as target window */
479 ASImageLayer *layers = create_image_layers (2);
480
481 layers[0].im = background;
482 layers[0].clip_width = target_width;
483 layers[0].clip_height = target_height;
484 layers[0].tint = background_tint;
485 layers[1].im = result;
486
487 if (bg_flags & BG_TILE)
488 {
489 /* tile horizontally */
490 while (x > 0) x -= (int)result->width;
491 layers[1].dst_x = x;
492 layers[1].clip_width = result->width+target_width;
493 }
494 else
495 {
496 /* clip horizontally */
497 layers[1].dst_x = x;
498 layers[1].clip_width = result->width;
499 }
500
501 if (bg_flags & BG_TILE)
502 {
503 while (y > 0) y -= (int)result->height;
504 layers[1].dst_y = y;
505 layers[1].clip_height = result->height + target_height;
506 }
507 else
508 {
509 layers[1].dst_y = y;
510 layers[1].clip_height = result->height;
511 }
512
513 if (rs[Rs_blendtype])
514 {
515 layers[1].merge_scanlines = blend_scanlines_name2func (rs[Rs_blendtype]);
516 if (layers[1].merge_scanlines == NULL)
517 layers[1].merge_scanlines = alphablend_scanlines;
518 }
519
520 ASImage *tmp = merge_layers (asv, layers, 2, target_width, target_height,
521 ASA_XImage, 0, ASIMAGE_QUALITY_DEFAULT);
522
523 if (tmp)
524 {
525 if (result != original_asim)
526 destroy_asimage (&result);
527
528 result = tmp;
529 }
530
531 free (layers);
532 }
533 }
534
535 bool ret = false;
536
537 if (result)
538 {
539 XGCValues gcv;
540 GC gc;
541
542 /* create Pixmap */
543 if (bg_pixmap == None 552 if (bg_pixmap == None
544 || bg_pmap_width != new_pmap_width 553 || bg_pmap_width != new_pmap_width
545 || bg_pmap_height != new_pmap_height) 554 || bg_pmap_height != new_pmap_height)
546 { 555 {
547 if (bg_pixmap) 556 if (bg_pixmap)
548 XFreePixmap (dpy, bg_pixmap); 557 XFreePixmap (dpy, bg_pixmap);
549 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth); 558 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth);
550 bg_pmap_width = new_pmap_width; 559 bg_pmap_width = new_pmap_width;
551 bg_pmap_height = new_pmap_height; 560 bg_pmap_height = new_pmap_height;
552 } 561 }
553 /* fill with background color (if result's not completely overlapping it) */
554 gcv.foreground = pix_colors[Color_bg];
555 gc = XCreateGC (dpy, vt, GCForeground, &gcv);
556 562
557 int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0;
558 int dst_width = result->width, dst_height = result->height;
559 if (background == NULL)
560 {
561 if (!(bg_flags & BG_TILE))
562 {
563 src_x = make_clip_rectangle (x, result->width , new_pmap_width , dst_x, dst_width );
564 src_y = make_clip_rectangle (y, result->height, new_pmap_height, dst_y, dst_height);
565 }
566
567 if (dst_x > 0 || dst_y > 0
568 || dst_x + dst_width < new_pmap_width
569 || dst_y + dst_height < new_pmap_height)
570 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
571 }
572
573 /* put result on pixmap */
574 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
575 asimage2drawable (asv, bg_pixmap, result, gc, src_x, src_y, dst_x, dst_y, dst_width, dst_height, True);
576
577 if (result != background && result != original_asim)
578 destroy_asimage (&result);
579
580 XFreeGC (dpy, gc);
581
582 ret = true;
583 }
584
585 if (background)
586 destroy_asimage (&background);
587
588 return ret;
589}
590# endif /* HAVE_AFTERIMAGE */
591
592# ifdef HAVE_PIXBUF
593bool
594rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
595 int src_x, int src_y, int dst_x, int dst_y,
596 unsigned int width, unsigned int height)
597{
598 XImage *ximage;
599 char *data, *line;
600 int bytes_per_pixel;
601 int width_r, width_g, width_b;
602 int sh_r, sh_g, sh_b;
603 int rowstride;
604 int channels;
605 unsigned char *row;
606
607 if (visual->c_class != TrueColor)
608 return false;
609
610 if (depth == 24 || depth == 32)
611 bytes_per_pixel = 4;
612 else if (depth == 15 || depth == 16)
613 bytes_per_pixel = 2;
614 else
615 return false;
616
617 width_r = ecb_popcount32 (visual->red_mask);
618 width_g = ecb_popcount32 (visual->green_mask);
619 width_b = ecb_popcount32 (visual->blue_mask);
620
621 if (width_r > 8 || width_g > 8 || width_b > 8)
622 return false;
623
624 sh_r = ecb_ctz32 (visual->red_mask);
625 sh_g = ecb_ctz32 (visual->green_mask);
626 sh_b = ecb_ctz32 (visual->blue_mask);
627
628 if (width > INT_MAX / height / bytes_per_pixel)
629 return false;
630
631 data = (char *)malloc (width * height * bytes_per_pixel);
632 if (!data)
633 return false;
634
635 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, data,
636 width, height, bytes_per_pixel * 8, 0);
637 if (!ximage)
638 {
639 free (data);
640 return false;
641 }
642
643 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
644
645 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
646 channels = gdk_pixbuf_get_n_channels (pixbuf);
647 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
648 line = data;
649
650 for (int y = 0; y < height; y++)
651 {
652 for (int x = 0; x < width; x++)
653 {
654 unsigned char *pixel = row + x * channels;
655 uint32_t value;
656
657 value = ((pixel[0] >> (8 - width_r)) << sh_r)
658 | ((pixel[1] >> (8 - width_g)) << sh_g)
659 | ((pixel[2] >> (8 - width_b)) << sh_b);
660
661 if (bytes_per_pixel == 4)
662 ((uint32_t *)line)[x] = value;
663 else
664 ((uint16_t *)line)[x] = value;
665 }
666
667 row += rowstride;
668 line += ximage->bytes_per_line;
669 }
670
671 XPutImage (dpy, pixmap, gc, ximage, 0, 0, dst_x, dst_y, width, height);
672 XDestroyImage (ximage);
673 return true;
674}
675
676bool
677rxvt_term::render_image (unsigned long tr_flags)
678{
679 if (!pixbuf)
680 return false;
681
682 if (tr_flags
683 && !(bg_flags & BG_HAS_RENDER))
684 return false;
685
686 GdkPixbuf *result;
687
688 int image_width = gdk_pixbuf_get_width (pixbuf);
689 int image_height = gdk_pixbuf_get_height (pixbuf);
690
691 int target_width = szHint.width;
692 int target_height = szHint.height;
693 int new_pmap_width = target_width;
694 int new_pmap_height = target_height;
695
696 int x = 0;
697 int y = 0;
698 int w = 0;
699 int h = 0;
700
701 get_image_geometry (image_width, image_height, w, h, x, y);
702
703 if (!(bg_flags & BG_ROOT_ALIGN)
704 && (x >= target_width
705 || y >= target_height
706 || x + w <= 0
707 || y + h <= 0))
708 return false;
709
710 result = pixbuf;
711
712 if (w != image_width
713 || h != image_height)
714 {
715 result = gdk_pixbuf_scale_simple (pixbuf,
716 w, h,
717 GDK_INTERP_BILINEAR);
718 }
719
720 if (!result)
721 return false;
722
723 bool ret = false;
724
725 XGCValues gcv;
726 GC gc;
727 Pixmap root_pmap;
728
729 image_width = gdk_pixbuf_get_width (result);
730 image_height = gdk_pixbuf_get_height (result);
731
732 if (tr_flags)
733 {
734 root_pmap = bg_pixmap; 563 tmp_pixmap = bg_pixmap;
735 bg_pixmap = None;
736 }
737 else
738 {
739 if (bg_flags & BG_TILE)
740 {
741 new_pmap_width = min (image_width, target_width);
742 new_pmap_height = min (image_height, target_height);
743 }
744 }
745
746 if (bg_pixmap == None
747 || bg_pmap_width != new_pmap_width
748 || bg_pmap_height != new_pmap_height)
749 {
750 if (bg_pixmap)
751 XFreePixmap (dpy, bg_pixmap);
752 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth);
753 bg_pmap_width = new_pmap_width;
754 bg_pmap_height = new_pmap_height;
755 } 564 }
756 565
757 gcv.foreground = pix_colors[Color_bg]; 566 gcv.foreground = pix_colors[Color_bg];
758 gc = XCreateGC (dpy, vt, GCForeground, &gcv); 567 gc = XCreateGC (dpy, tmp_pixmap, GCForeground, &gcv);
759 568
760 if (gc) 569 if (gc)
761 { 570 {
762 if (bg_flags & BG_TILE) 571 if (image.flags & IM_TILE)
763 { 572 {
764 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth); 573 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
765 pixbuf_to_pixmap (result, tile, gc, 574 pixbuf_to_pixmap (result, tile, gc,
766 0, 0, 575 0, 0,
767 0, 0, 576 0, 0,
768 image_width, image_height); 577 image_width, image_height, need_blend);
769 578
770 gcv.tile = tile; 579 gcv.tile = tile;
771 gcv.fill_style = FillTiled; 580 gcv.fill_style = FillTiled;
772 gcv.ts_x_origin = x; 581 gcv.ts_x_origin = x;
773 gcv.ts_y_origin = y; 582 gcv.ts_y_origin = y;
774 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 583 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
775 584
776 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 585 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
777 XFreePixmap (dpy, tile); 586 XFreePixmap (dpy, tile);
778 } 587 }
779 else 588 else
780 { 589 {
781 int src_x, src_y, dst_x, dst_y; 590 int src_x, src_y, dst_x, dst_y;
785 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height); 594 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height);
786 595
787 if (dst_x > 0 || dst_y > 0 596 if (dst_x > 0 || dst_y > 0
788 || dst_x + dst_width < new_pmap_width 597 || dst_x + dst_width < new_pmap_width
789 || dst_y + dst_height < new_pmap_height) 598 || dst_y + dst_height < new_pmap_height)
790 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 599 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
791 600
792 if (dst_x < new_pmap_width && dst_y < new_pmap_height) 601 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
793 pixbuf_to_pixmap (result, bg_pixmap, gc, 602 pixbuf_to_pixmap (result, tmp_pixmap, gc,
794 src_x, src_y, 603 src_x, src_y,
795 dst_x, dst_y, 604 dst_x, dst_y,
796 dst_width, dst_height); 605 dst_width, dst_height, need_blend);
797 } 606 }
607
608 if (image.need_blur ())
609 blur_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.h_blurRadius, image.v_blurRadius);
610 if (image.need_tint ())
611 tint_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.tint, image.tint_set, image.shade);
798 612
799#if XRENDER 613#if XRENDER
800 if (tr_flags) 614 if (need_blend)
801 { 615 {
616 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
802 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 617 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
803 618
804 Picture src = XRenderCreatePicture (dpy, root_pmap, format, 0, 0); 619 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0);
805 620
806 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0); 621 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
807 622
808 Picture mask = create_xrender_mask (dpy, vt, False, False); 623 Picture mask = create_xrender_mask (dpy, vt, False, False);
809 624
810 XRenderColor mask_c; 625 XRenderColor mask_c;
811 626
812 mask_c.alpha = 0x8000; 627 mask_c.alpha = image.alpha;
813 mask_c.red = 628 mask_c.red =
814 mask_c.green = 629 mask_c.green =
815 mask_c.blue = 0; 630 mask_c.blue = 0;
816 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); 631 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
817 632
829 } 644 }
830 645
831 if (result != pixbuf) 646 if (result != pixbuf)
832 g_object_unref (result); 647 g_object_unref (result);
833 648
834 if (tr_flags) 649 if (need_blend)
835 XFreePixmap (dpy, root_pmap); 650 XFreePixmap (dpy, tmp_pixmap);
836 651
837 return ret; 652 return ret;
838} 653}
839# endif /* HAVE_PIXBUF */ 654# endif /* HAVE_PIXBUF */
840 655
656# ifndef NO_RESOURCES
657static int
658rxvt_define_image (XrmDatabase *database ecb_unused,
659 XrmBindingList bindings ecb_unused,
660 XrmQuarkList quarks,
661 XrmRepresentation *type ecb_unused,
662 XrmValue *value,
663 XPointer closure ecb_unused)
664{
665 int size;
666
667 for (size = 0; quarks[size] != NULLQUARK; size++)
668 ;
669
670 if (size >= 2)
671 {
672 int id = strtol (XrmQuarkToString (quarks[size-2]), 0, 0);
673 if (id >= 1)
674 GET_R->parse_image (id, XrmQuarkToString (quarks[size-1]), (char *)value->addr);
675 }
676 return False;
677}
678
679void
680rxvt_term::parse_image (int id, const char *type, const char *arg)
681{
682 if (image_vec.size () < id + 1)
683 image_vec.resize (id + 1);
684
685 rxvt_image *image = &image_vec[id];
686}
687# endif
688
689rxvt_image::rxvt_image ()
690{
691 alpha = 0xffff;
692 flags = 0;
693 h_scale =
694 v_scale = defaultScale;
695 h_align =
696 v_align = defaultAlign;
697
698# ifdef HAVE_PIXBUF
699 pixbuf = 0;
700# endif
701}
702
841bool 703bool
842rxvt_term::bg_set_file (const char *file) 704rxvt_image::set_file_geometry (const char *file)
843{ 705{
844 if (!file || !*file) 706 if (!file || !*file)
845 return false; 707 return false;
846 708
847 bool ret = false;
848 const char *p = strchr (file, ';'); 709 const char *p = strchr (file, ';');
849 710
850 if (p) 711 if (p)
851 { 712 {
852 size_t len = p - file; 713 size_t len = p - file;
854 memcpy (f, file, len); 715 memcpy (f, file, len);
855 f[len] = '\0'; 716 f[len] = '\0';
856 file = f; 717 file = f;
857 } 718 }
858 719
859# ifdef HAVE_AFTERIMAGE 720 bool ret = set_file (file);
860 if (!asimman) 721 alpha = 0x8000;
861 asimman = create_generic_imageman (rs[Rs_path]); 722 if (ret)
862 ASImage *image = get_asimage (asimman, file, 0xFFFFFFFF, 100); 723 set_geometry (p ? p + 1 : "");
863 if (image) 724 return ret;
864 { 725}
865 if (original_asim) 726
866 safe_asimage_destroy (original_asim); 727bool
867 original_asim = image; 728rxvt_image::set_file (const char *file)
868 bg_flags |= BG_IS_FROM_FILE | BG_CLIENT_RENDER; 729{
869 ret = true; 730 bool ret = false;
870 }
871# endif
872 731
873# ifdef HAVE_PIXBUF 732# ifdef HAVE_PIXBUF
874 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL); 733 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
875 if (image) 734 if (image)
876 { 735 {
877 if (pixbuf) 736 if (pixbuf)
878 g_object_unref (pixbuf); 737 g_object_unref (pixbuf);
879 pixbuf = image; 738 pixbuf = image;
880 bg_flags |= BG_IS_FROM_FILE;
881 ret = true; 739 ret = true;
882 } 740 }
883# endif 741# endif
884 742
885 if (ret) 743 if (ret)
886 { 744 flags |= IM_IS_SET;
887 if (p)
888 bg_set_geometry (p + 1);
889 else
890 bg_set_default_geometry ();
891 }
892 745
893 return ret; 746 return ret;
894} 747}
895 748
896# endif /* BG_IMAGE_FROM_FILE */ 749# endif /* BG_IMAGE_FROM_FILE */
897 750
898# ifdef ENABLE_TRANSPARENCY
899bool 751bool
900rxvt_term::bg_set_transparent () 752image_effects::set_blur (const char *geom)
901{
902 if (!(bg_flags & BG_IS_TRANSPARENT))
903 {
904 bg_flags |= BG_IS_TRANSPARENT;
905 return true;
906 }
907
908 return false;
909}
910
911bool
912rxvt_term::bg_set_blur (const char *geom)
913{ 753{
914 bool changed = false; 754 bool changed = false;
915 unsigned int hr, vr; 755 unsigned int hr, vr;
916 int junk; 756 int junk;
917 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr); 757 int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr);
934 { 774 {
935 changed = true; 775 changed = true;
936 v_blurRadius = vr; 776 v_blurRadius = vr;
937 } 777 }
938 778
939 if (h_blurRadius == 0 || v_blurRadius == 0)
940 bg_flags &= ~BG_NEEDS_BLUR;
941 else
942 bg_flags |= BG_NEEDS_BLUR;
943
944 return changed; 779 return changed;
945} 780}
946 781
947void
948rxvt_term::set_tint_shade_flags ()
949{
950 rgba c;
951 bool has_shade = shade != 100;
952
953 bg_flags &= ~BG_TINT_FLAGS;
954
955 if (bg_flags & BG_TINT_SET)
956 {
957 tint.get (c);
958 if (!has_shade
959 && (c.r <= 0x00ff || c.r >= 0xff00)
960 && (c.g <= 0x00ff || c.g >= 0xff00)
961 && (c.b <= 0x00ff || c.b >= 0xff00))
962 bg_flags |= BG_TINT_BITAND;
963 }
964
965 if (has_shade || (bg_flags & BG_TINT_SET))
966 bg_flags |= BG_NEEDS_TINT;
967}
968
969bool 782bool
970rxvt_term::bg_set_tint (rxvt_color &new_tint) 783image_effects::set_tint (const rxvt_color &new_tint)
971{ 784{
972 if (!(bg_flags & BG_TINT_SET) || tint != new_tint) 785 if (!tint_set || tint != new_tint)
973 { 786 {
974 tint = new_tint; 787 tint = new_tint;
975 bg_flags |= BG_TINT_SET; 788 tint_set = true;
976 set_tint_shade_flags (); 789
977 return true; 790 return true;
978 } 791 }
979 792
980 return false; 793 return false;
981} 794}
982 795
983bool 796bool
984rxvt_term::bg_set_shade (const char *shade_str) 797image_effects::set_shade (const char *shade_str)
985{ 798{
986 int new_shade = atoi (shade_str); 799 int new_shade = atoi (shade_str);
987 800
988 clamp_it (new_shade, -100, 200); 801 clamp_it (new_shade, -100, 200);
989 if (new_shade < 0) 802 if (new_shade < 0)
990 new_shade = 200 - (100 + new_shade); 803 new_shade = 200 - (100 + new_shade);
991 804
992 if (new_shade != shade) 805 if (new_shade != shade)
993 { 806 {
994 shade = new_shade; 807 shade = new_shade;
995 set_tint_shade_flags ();
996 return true; 808 return true;
997 } 809 }
998 810
999 return false; 811 return false;
1000} 812}
1021 params[i+2] = XDoubleToFixed (kernel[i] / sum); 833 params[i+2] = XDoubleToFixed (kernel[i] / sum);
1022} 834}
1023#endif 835#endif
1024 836
1025bool 837bool
1026rxvt_term::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height, int depth) 838rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height, bool argb, int h_blurRadius, int v_blurRadius)
1027{ 839{
1028 bool ret = false; 840 bool ret = false;
1029#if XRENDER 841#if XRENDER
1030 if (!(bg_flags & BG_HAS_RENDER_CONV)) 842 if (!(bg_flags & BG_HAS_RENDER_CONV))
1031 return false; 843 return false;
1033 int size = max (h_blurRadius, v_blurRadius) * 2 + 1; 845 int size = max (h_blurRadius, v_blurRadius) * 2 + 1;
1034 double *kernel = (double *)malloc (size * sizeof (double)); 846 double *kernel = (double *)malloc (size * sizeof (double));
1035 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 847 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
1036 848
1037 XRenderPictureAttributes pa; 849 XRenderPictureAttributes pa;
1038 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 850 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
851 : XRenderFindVisualFormat (dpy, visual);
1039 852
1040 pa.repeat = RepeatPad; 853 pa.repeat = RepeatPad;
1041 Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 854 Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
1042 Pixmap tmp = XCreatePixmap (dpy, pixmap, width, height, depth); 855 Pixmap tmp = XCreatePixmap (dpy, pixmap, width, height, depth);
1043 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 856 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
1086#endif 899#endif
1087 return ret; 900 return ret;
1088} 901}
1089 902
1090bool 903bool
1091rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 904rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height, bool argb, rxvt_color &tint, bool tint_set, int shade)
1092{ 905{
1093 bool ret = false; 906 bool ret = false;
1094 907
1095 if (bg_flags & BG_TINT_BITAND) 908 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
909
910 if (tint_set)
911 tint.get (c);
912
913 if (shade == 100
914 && (c.r <= 0x00ff || c.r >= 0xff00)
915 && (c.g <= 0x00ff || c.g >= 0xff00)
916 && (c.b <= 0x00ff || c.b >= 0xff00))
1096 { 917 {
1097 XGCValues gcv; 918 XGCValues gcv;
1098 GC gc; 919 GC gc;
1099 920
1100 /* In this case we can tint image server-side getting significant 921 /* In this case we can tint image server-side getting significant
1112 } 933 }
1113 } 934 }
1114# if XRENDER 935# if XRENDER
1115 else if (bg_flags & BG_HAS_RENDER) 936 else if (bg_flags & BG_HAS_RENDER)
1116 { 937 {
1117 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1118
1119 if (bg_flags & BG_TINT_SET)
1120 tint.get (c);
1121
1122 if (shade <= 100) 938 if (shade <= 100)
1123 { 939 {
1124 c.r = c.r * shade / 100; 940 c.r = c.r * shade / 100;
1125 c.g = c.g * shade / 100; 941 c.g = c.g * shade / 100;
1126 c.b = c.b * shade / 100; 942 c.b = c.b * shade / 100;
1130 c.r = c.r * (200 - shade) / 100; 946 c.r = c.r * (200 - shade) / 100;
1131 c.g = c.g * (200 - shade) / 100; 947 c.g = c.g * (200 - shade) / 100;
1132 c.b = c.b * (200 - shade) / 100; 948 c.b = c.b * (200 - shade) / 100;
1133 } 949 }
1134 950
1135 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 951 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
952 : XRenderFindVisualFormat (dpy, visual);
1136 953
1137 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0); 954 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0);
1138 955
1139 Picture overlay_pic = create_xrender_mask (dpy, pixmap, True, False); 956 Picture overlay_pic = create_xrender_mask (dpy, pixmap, True, False);
1140 957
1176# endif 993# endif
1177 994
1178 return ret; 995 return ret;
1179} 996}
1180 997
998# ifdef ENABLE_TRANSPARENCY
1181/* 999/*
1182 * Builds a pixmap of the same size as the terminal window that contains 1000 * Builds a pixmap of the same size as the terminal window that contains
1183 * the tiled portion of the root pixmap that is supposed to be covered by 1001 * the tiled portion of the root pixmap that is supposed to be covered by
1184 * our window. 1002 * our window.
1185 */ 1003 */
1186unsigned long 1004bool
1187rxvt_term::make_transparency_pixmap () 1005rxvt_term::render_root_image ()
1188{ 1006{
1189 unsigned long result = 0; 1007 bool ret = false;
1190 1008
1191 /* root dimensions may change from call to call - but Display structure should 1009 /* root dimensions may change from call to call - but Display structure should
1192 * be always up-to-date, so let's use it : 1010 * be always up-to-date, so let's use it :
1193 */ 1011 */
1194 int screen = display->screen; 1012 int screen = display->screen;
1275 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 1093 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1276 1094
1277 if (gc) 1095 if (gc)
1278 { 1096 {
1279 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); 1097 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1280 result |= BG_IS_VALID | (bg_flags & BG_EFFECTS_FLAGS); 1098 ret = true;
1099 bool need_blur = root_effects.need_blur ();
1100 bool need_tint = root_effects.need_tint ();
1281 1101
1282 if (!(bg_flags & BG_CLIENT_RENDER)) 1102 if (need_blur)
1103 {
1104 if (blur_pixmap (bg_pixmap, window_width, window_height, false,
1105 root_effects.h_blurRadius, root_effects.v_blurRadius))
1106 need_blur = false;
1283 { 1107 }
1284 if (bg_flags & BG_NEEDS_BLUR) 1108 if (need_tint)
1109 {
1110 if (tint_pixmap (bg_pixmap, window_width, window_height, false,
1111 root_effects.tint, root_effects.tint_set, root_effects.shade))
1112 need_tint = false;
1113 }
1114 if (need_tint)
1115 {
1116 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1117 if (ximage)
1285 { 1118 {
1286 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth)) 1119 /* our own client-side tinting */
1287 result &= ~BG_NEEDS_BLUR; 1120 tint_ximage (ximage, root_effects.tint, root_effects.tint_set, root_effects.shade);
1121
1122 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
1123 XDestroyImage (ximage);
1288 } 1124 }
1289 if (bg_flags & BG_NEEDS_TINT)
1290 {
1291 if (tint_pixmap (bg_pixmap, visual, window_width, window_height))
1292 result &= ~BG_NEEDS_TINT;
1293 } 1125 }
1294# ifndef HAVE_AFTERIMAGE
1295 if (result & BG_NEEDS_TINT)
1296 {
1297 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1298 if (ximage)
1299 {
1300 /* our own client-side tinting */
1301 tint_ximage (DefaultVisual (dpy, display->screen), ximage);
1302
1303 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
1304 XDestroyImage (ximage);
1305 }
1306 }
1307# endif
1308 } /* server side rendering completed */
1309 1126
1310 XFreeGC (dpy, gc); 1127 XFreeGC (dpy, gc);
1311 } 1128 }
1312 1129
1313 if (recoded_root_pmap != root_pixmap) 1130 if (recoded_root_pmap != root_pixmap)
1314 XFreePixmap (dpy, recoded_root_pmap); 1131 XFreePixmap (dpy, recoded_root_pmap);
1315 1132
1316 return result; 1133 return ret;
1317} 1134}
1318 1135
1319void 1136void
1320rxvt_term::bg_set_root_pixmap () 1137rxvt_term::bg_set_root_pixmap ()
1321{ 1138{
1328# endif /* ENABLE_TRANSPARENCY */ 1145# endif /* ENABLE_TRANSPARENCY */
1329 1146
1330bool 1147bool
1331rxvt_term::bg_render () 1148rxvt_term::bg_render ()
1332{ 1149{
1333 unsigned long tr_flags = 0;
1334
1335 bg_invalidate (); 1150 bg_invalidate ();
1336# ifdef ENABLE_TRANSPARENCY 1151# ifdef ENABLE_TRANSPARENCY
1337 if (bg_flags & BG_IS_TRANSPARENT) 1152 if (bg_flags & BG_IS_TRANSPARENT)
1338 { 1153 {
1339 /* we need to re-generate transparency pixmap in that case ! */ 1154 /* we need to re-generate transparency pixmap in that case ! */
1340 tr_flags = make_transparency_pixmap (); 1155 if (render_root_image ())
1341 if (tr_flags)
1342 bg_flags |= BG_IS_VALID; 1156 bg_flags |= BG_IS_VALID;
1343 } 1157 }
1344# endif 1158# endif
1345 1159
1346# ifdef BG_IMAGE_FROM_FILE 1160# ifdef BG_IMAGE_FROM_FILE
1347 if ((bg_flags & BG_IS_FROM_FILE) 1161 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
1348 || (tr_flags & BG_EFFECTS_FLAGS))
1349 { 1162 {
1350 if (render_image (tr_flags)) 1163 if (render_image (*bg_image))
1351 bg_flags |= BG_IS_VALID; 1164 bg_flags |= BG_IS_VALID;
1352 } 1165 }
1353# endif 1166# endif
1354 1167
1355 if (!(bg_flags & BG_IS_VALID)) 1168 if (!(bg_flags & BG_IS_VALID))
1370} 1183}
1371 1184
1372void 1185void
1373rxvt_term::bg_init () 1186rxvt_term::bg_init ()
1374{ 1187{
1375#ifdef ENABLE_TRANSPARENCY
1376 shade = 100;
1377#endif
1378
1379 bg_flags &= ~(BG_HAS_RENDER | BG_HAS_RENDER_CONV); 1188 bg_flags &= ~(BG_HAS_RENDER | BG_HAS_RENDER_CONV);
1380#if XRENDER 1189#if XRENDER
1381 int major, minor; 1190 int major, minor;
1382 if (XRenderQueryVersion (dpy, &major, &minor)) 1191 if (XRenderQueryVersion (dpy, &major, &minor))
1383 bg_flags |= BG_HAS_RENDER; 1192 bg_flags |= BG_HAS_RENDER;
1389 bg_flags |= BG_HAS_RENDER_CONV; 1198 bg_flags |= BG_HAS_RENDER_CONV;
1390 1199
1391 XFree (filters); 1200 XFree (filters);
1392 } 1201 }
1393#endif 1202#endif
1394}
1395 1203
1396#endif /* HAVE_BG_PIXMAP */ 1204#ifdef BG_IMAGE_FROM_FILE
1205 if (rs[Rs_backgroundPixmap])
1206 {
1207 rxvt_image *image = new_image ();
1208 if (!image->set_file_geometry (rs[Rs_backgroundPixmap]))
1209 image_vec.pop_back ();
1210 }
1397 1211
1398#if defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) 1212# ifndef NO_RESOURCES
1213 find_resources ("image", "Image", XrmEnumAllLevels, rxvt_define_image);
1214 vector<rxvt_image>::iterator bg_image = image_vec.begin ();
1215 while (bg_image != image_vec.end ())
1216 {
1217 if (!(bg_image->flags & IM_IS_SET))
1218 bg_image = image_vec.erase (bg_image);
1219 else
1220 {
1221 if (bg_image->is_size_sensitive ())
1222 bg_image->flags |= IM_IS_SIZE_SENSITIVE;
1223
1224 bg_image++;
1225 }
1226 }
1227# endif
1228
1229 if (image_vec.size () > 0
1230 && !bg_window_position_sensitive ())
1231 update_background ();
1232#endif
1233}
1234
1399/* based on code from aterm-0.4.2 */ 1235/* based on code from aterm-0.4.2 */
1400 1236
1401static inline void 1237static inline void
1402fill_lut (uint32_t *lookup, uint32_t mask, int sh, unsigned short low, unsigned short high) 1238fill_lut (uint32_t *lookup, uint32_t mask, int sh, unsigned short low, unsigned short high)
1403{ 1239{
1409 lookup[i] = (tmp / 0xffff) << sh; 1245 lookup[i] = (tmp / 0xffff) << sh;
1410 } 1246 }
1411} 1247}
1412 1248
1413void 1249void
1414rxvt_term::tint_ximage (Visual *visual, XImage *ximage) 1250rxvt_term::tint_ximage (XImage *ximage, rxvt_color &tint, bool tint_set, int shade)
1415{ 1251{
1252 unsigned int size_r, size_g, size_b;
1416 int sh_r, sh_g, sh_b; 1253 int sh_r, sh_g, sh_b;
1417 uint32_t mask_r, mask_g, mask_b; 1254 uint32_t mask_r, mask_g, mask_b;
1418 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1255 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1419 unsigned short low; 1256 unsigned short low;
1420 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 1257 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1425 mask_r = visual->red_mask; 1262 mask_r = visual->red_mask;
1426 mask_g = visual->green_mask; 1263 mask_g = visual->green_mask;
1427 mask_b = visual->blue_mask; 1264 mask_b = visual->blue_mask;
1428 1265
1429 /* boring lookup table pre-initialization */ 1266 /* boring lookup table pre-initialization */
1430 switch (ximage->depth) 1267 sh_r = ecb_ctz32 (mask_r);
1431 { 1268 sh_g = ecb_ctz32 (mask_g);
1432 case 15: 1269 sh_b = ecb_ctz32 (mask_b);
1433 if ((mask_r != 0x7c00) || 1270
1434 (mask_g != 0x03e0) || 1271 size_r = mask_r >> sh_r;
1435 (mask_b != 0x001f)) 1272 size_g = mask_g >> sh_g;
1273 size_b = mask_b >> sh_b;
1274
1275 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1436 return; 1276 return;
1437 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+32+32)); 1277
1278 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1438 lookup_r = lookup; 1279 lookup_r = lookup;
1439 lookup_g = lookup+32; 1280 lookup_g = lookup + size_r;
1440 lookup_b = lookup+32+32; 1281 lookup_b = lookup + size_r + size_g;
1441 sh_r = 10;
1442 sh_g = 5;
1443 sh_b = 0;
1444 break;
1445 case 16:
1446 if ((mask_r != 0xf800) ||
1447 (mask_g != 0x07e0) ||
1448 (mask_b != 0x001f))
1449 return;
1450 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+64+32));
1451 lookup_r = lookup;
1452 lookup_g = lookup+32;
1453 lookup_b = lookup+32+64;
1454 sh_r = 11;
1455 sh_g = 5;
1456 sh_b = 0;
1457 break;
1458 case 24:
1459 if ((mask_r != 0xff0000) ||
1460 (mask_g != 0x00ff00) ||
1461 (mask_b != 0x0000ff))
1462 return;
1463 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1464 lookup_r = lookup;
1465 lookup_g = lookup+256;
1466 lookup_b = lookup+256+256;
1467 sh_r = 16;
1468 sh_g = 8;
1469 sh_b = 0;
1470 break;
1471 case 32:
1472 if ((mask_r != 0xff0000) ||
1473 (mask_g != 0x00ff00) ||
1474 (mask_b != 0x0000ff))
1475 return;
1476 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(256+256+256));
1477 lookup_r = lookup;
1478 lookup_g = lookup+256;
1479 lookup_b = lookup+256+256;
1480 sh_r = 16;
1481 sh_g = 8;
1482 sh_b = 0;
1483 break;
1484 default:
1485 return; /* we do not support this color depth */
1486 }
1487 1282
1488 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); 1283 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1489 1284
1490 if (bg_flags & BG_TINT_SET) 1285 if (tint_set)
1491 tint.get (c); 1286 tint.get (c);
1492 1287
1493 /* prepare limits for color transformation (each channel is handled separately) */ 1288 /* prepare limits for color transformation (each channel is handled separately) */
1494 if (shade > 100) 1289 if (shade > 100)
1495 { 1290 {
1513 fill_lut (lookup_g, mask_g, sh_g, low, c.g); 1308 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1514 fill_lut (lookup_b, mask_b, sh_b, low, c.b); 1309 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1515 1310
1516 /* apply table to input image (replacing colors by newly calculated ones) */ 1311 /* apply table to input image (replacing colors by newly calculated ones) */
1517 if (ximage->bits_per_pixel == 32 1312 if (ximage->bits_per_pixel == 32
1518 && (ximage->depth == 24 || ximage->depth == 32)
1519 && ximage->byte_order == host_byte_order) 1313 && ximage->byte_order == host_byte_order)
1520 { 1314 {
1521 uint32_t *p1, *pf, *p, *pl; 1315 char *line = ximage->data;
1522 p1 = (uint32_t *) ximage->data;
1523 pf = (uint32_t *) (ximage->data + ximage->height * ximage->bytes_per_line);
1524 1316
1525 while (p1 < pf) 1317 for (int y = 0; y < ximage->height; y++)
1526 { 1318 {
1527 p = p1; 1319 uint32_t *p = (uint32_t *)line;
1528 pl = p1 + ximage->width; 1320 for (int x = 0; x < ximage->width; x++)
1529 for (; p < pl; p++)
1530 { 1321 {
1531 *p = lookup_r[(*p & 0xff0000) >> 16] | 1322 *p = lookup_r[(*p & mask_r) >> sh_r] |
1532 lookup_g[(*p & 0x00ff00) >> 8] | 1323 lookup_g[(*p & mask_g) >> sh_g] |
1533 lookup_b[(*p & 0x0000ff)] | 1324 lookup_b[(*p & mask_b) >> sh_b];
1534 (*p & 0xff000000); 1325 p++;
1535 } 1326 }
1536 p1 = (uint32_t *) ((char *) p1 + ximage->bytes_per_line); 1327 line += ximage->bytes_per_line;
1537 } 1328 }
1538 } 1329 }
1539 else 1330 else
1540 { 1331 {
1541 for (int y = 0; y < ximage->height; y++) 1332 for (int y = 0; y < ximage->height; y++)
1549 } 1340 }
1550 } 1341 }
1551 1342
1552 free (lookup); 1343 free (lookup);
1553} 1344}
1554#endif /* defined(ENABLE_TRANSPARENCY) && !defined(HAVE_AFTERIMAGE) */ 1345
1346#endif /* HAVE_BG_PIXMAP */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines