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.209 by sf-exg, Fri May 11 12:21:22 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.
123 123
124# ifdef BG_IMAGE_FROM_FILE 124# ifdef BG_IMAGE_FROM_FILE
125static inline int 125static inline int
126make_align_position (int align, int window_size, int image_size) 126make_align_position (int align, int window_size, int image_size)
127{ 127{
128 int diff = window_size - image_size;
129 int smaller = min (image_size, window_size);
130
131 if (align >= 0 && align <= 100) 128 if (align >= 0 && align <= 100)
132 return diff * align / 100; 129 return lerp (0, window_size - image_size, align);
133 else if (align > 100 && align <= 200) 130 else if (align > 100)
134 return (align - 100) * smaller / 100 + window_size - smaller; 131 return lerp (window_size - image_size, window_size, align - 100);
135 else if (align >= -100 && align < 0) 132 else
136 return (align + 100) * smaller / 100 - image_size; 133 return lerp (-image_size, 0, align + 100);
137 return 0;
138} 134}
139 135
140static inline int 136static inline int
141make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) 137make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
142{ 138{
334 { 330 {
335 x = make_align_position (h_align, target_width, w); 331 x = make_align_position (h_align, target_width, w);
336 y = make_align_position (v_align, target_height, h); 332 y = make_align_position (v_align, target_height, h);
337 } 333 }
338 334
339 bg_flags &= ~BG_IS_SIZE_SENSITIVE;
340 if (!(bg_flags & BG_TILE) 335 if (!(bg_flags & BG_TILE)
341 || h_scale || v_scale 336 || h_scale || v_scale
342 || (!(bg_flags & BG_ROOT_ALIGN) && (h_align || v_align)) 337 || (!(bg_flags & BG_ROOT_ALIGN) && (h_align || v_align))
343 || w > target_width || h > target_height) 338 || image_width > target_width || image_height > target_height)
344 bg_flags |= BG_IS_SIZE_SENSITIVE; 339 bg_flags |= BG_IS_SIZE_SENSITIVE;
340 else
341 bg_flags &= ~BG_IS_SIZE_SENSITIVE;
345} 342}
346 343
347# ifdef HAVE_PIXBUF 344# ifdef HAVE_PIXBUF
348bool 345bool
349rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc, 346rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
351 unsigned int width, unsigned int height) 348 unsigned int width, unsigned int height)
352{ 349{
353 XImage *ximage; 350 XImage *ximage;
354 char *data, *line; 351 char *data, *line;
355 int bytes_per_pixel; 352 int bytes_per_pixel;
356 int width_r, width_g, width_b; 353 int width_r, width_g, width_b, width_a;
357 int sh_r, sh_g, sh_b; 354 int sh_r, sh_g, sh_b, sh_a;
355 uint32_t alpha_mask;
358 int rowstride; 356 int rowstride;
359 int channels; 357 int channels;
360 unsigned char *row; 358 unsigned char *row;
361 359
362 if (visual->c_class != TrueColor) 360 if (visual->c_class != TrueColor)
363 return false; 361 return false;
362
363#if XRENDER
364 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
365 if (format)
366 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
367 else
368#endif
369 alpha_mask = 0;
364 370
365 if (depth == 24 || depth == 32) 371 if (depth == 24 || depth == 32)
366 bytes_per_pixel = 4; 372 bytes_per_pixel = 4;
367 else if (depth == 15 || depth == 16) 373 else if (depth == 15 || depth == 16)
368 bytes_per_pixel = 2; 374 bytes_per_pixel = 2;
370 return false; 376 return false;
371 377
372 width_r = ecb_popcount32 (visual->red_mask); 378 width_r = ecb_popcount32 (visual->red_mask);
373 width_g = ecb_popcount32 (visual->green_mask); 379 width_g = ecb_popcount32 (visual->green_mask);
374 width_b = ecb_popcount32 (visual->blue_mask); 380 width_b = ecb_popcount32 (visual->blue_mask);
381 width_a = ecb_popcount32 (alpha_mask);
375 382
376 if (width_r > 8 || width_g > 8 || width_b > 8) 383 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
377 return false; 384 return false;
378 385
379 sh_r = ecb_ctz32 (visual->red_mask); 386 sh_r = ecb_ctz32 (visual->red_mask);
380 sh_g = ecb_ctz32 (visual->green_mask); 387 sh_g = ecb_ctz32 (visual->green_mask);
381 sh_b = ecb_ctz32 (visual->blue_mask); 388 sh_b = ecb_ctz32 (visual->blue_mask);
389 sh_a = ecb_ctz32 (alpha_mask);
382 390
383 if (width > INT_MAX / height / bytes_per_pixel) 391 if (width > INT_MAX / height / bytes_per_pixel)
384 return false; 392 return false;
385 393
386 data = (char *)malloc (width * height * bytes_per_pixel); 394 data = (char *)malloc (width * height * bytes_per_pixel);
400 rowstride = gdk_pixbuf_get_rowstride (pixbuf); 408 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
401 channels = gdk_pixbuf_get_n_channels (pixbuf); 409 channels = gdk_pixbuf_get_n_channels (pixbuf);
402 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; 410 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
403 line = data; 411 line = data;
404 412
413 rgba c (0, 0, 0);
414
415 if (channels == 4 && alpha_mask == 0)
416 {
417 pix_colors[Color_bg].get (c);
418 c.r >>= 8;
419 c.g >>= 8;
420 c.b >>= 8;
421 }
422
405 for (int y = 0; y < height; y++) 423 for (int y = 0; y < height; y++)
406 { 424 {
407 for (int x = 0; x < width; x++) 425 for (int x = 0; x < width; x++)
408 { 426 {
409 unsigned char *pixel = row + x * channels; 427 unsigned char *pixel = row + x * channels;
410 uint32_t value; 428 uint32_t value;
429 unsigned char r, g, b, a;
411 430
431 if (channels == 4)
432 {
433 a = pixel[3];
434 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
435 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
436 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
437 }
438 else
439 {
440 a = 0xff;
441 r = pixel[0];
442 g = pixel[1];
443 b = pixel[2];
444 }
445
412 value = ((pixel[0] >> (8 - width_r)) << sh_r) 446 value = ((r >> (8 - width_r)) << sh_r)
413 | ((pixel[1] >> (8 - width_g)) << sh_g) 447 | ((g >> (8 - width_g)) << sh_g)
414 | ((pixel[2] >> (8 - width_b)) << sh_b); 448 | ((b >> (8 - width_b)) << sh_b)
449 | ((a >> (8 - width_a)) << sh_a);
415 450
416 if (bytes_per_pixel == 4) 451 if (bytes_per_pixel == 4)
417 ((uint32_t *)line)[x] = value; 452 ((uint32_t *)line)[x] = value;
418 else 453 else
419 ((uint16_t *)line)[x] = value; 454 ((uint16_t *)line)[x] = value;
675 { 710 {
676 changed = true; 711 changed = true;
677 v_blurRadius = vr; 712 v_blurRadius = vr;
678 } 713 }
679 714
680 if (h_blurRadius == 0 || v_blurRadius == 0) 715 if (h_blurRadius && v_blurRadius)
716 bg_flags |= BG_NEEDS_BLUR;
717 else
681 bg_flags &= ~BG_NEEDS_BLUR; 718 bg_flags &= ~BG_NEEDS_BLUR;
682 else
683 bg_flags |= BG_NEEDS_BLUR;
684 719
685 return changed; 720 return changed;
686} 721}
687 722
688void 723void
689rxvt_term::set_tint_shade_flags () 724rxvt_term::set_tint_shade_flags ()
690{ 725{
726 if (shade != 100 || (bg_flags & BG_TINT_SET))
727 bg_flags |= BG_NEEDS_TINT;
728 else
729 bg_flags &= ~BG_NEEDS_TINT;
730}
731
732bool
733rxvt_term::bg_set_tint (rxvt_color &new_tint)
734{
735 if (!(bg_flags & BG_TINT_SET) || tint != new_tint)
736 {
737 tint = new_tint;
738 bg_flags |= BG_TINT_SET;
739
691 rgba c; 740 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); 741 tint.get (c);
699 if (!has_shade
700 && (c.r <= 0x00ff || c.r >= 0xff00) 742 if ((c.r <= 0x00ff || c.r >= 0xff00)
701 && (c.g <= 0x00ff || c.g >= 0xff00) 743 && (c.g <= 0x00ff || c.g >= 0xff00)
702 && (c.b <= 0x00ff || c.b >= 0xff00)) 744 && (c.b <= 0x00ff || c.b >= 0xff00))
703 bg_flags |= BG_TINT_BITAND; 745 bg_flags |= BG_TINT_BITAND;
704 } 746 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; 747 bg_flags &= ~BG_TINT_BITAND;
748
717 set_tint_shade_flags (); 749 set_tint_shade_flags ();
718 return true; 750 return true;
719 } 751 }
720 752
721 return false; 753 return false;
831bool 863bool
832rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 864rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height)
833{ 865{
834 bool ret = false; 866 bool ret = false;
835 867
836 if (bg_flags & BG_TINT_BITAND) 868 if (shade == 100 && (bg_flags & BG_TINT_BITAND))
837 { 869 {
838 XGCValues gcv; 870 XGCValues gcv;
839 GC gc; 871 GC gc;
840 872
841 /* In this case we can tint image server-side getting significant 873 /* In this case we can tint image server-side getting significant
1150} 1182}
1151 1183
1152void 1184void
1153rxvt_term::tint_ximage (Visual *visual, XImage *ximage) 1185rxvt_term::tint_ximage (Visual *visual, XImage *ximage)
1154{ 1186{
1187 unsigned int size_r, size_g, size_b;
1155 int sh_r, sh_g, sh_b; 1188 int sh_r, sh_g, sh_b;
1156 uint32_t mask_r, mask_g, mask_b; 1189 uint32_t mask_r, mask_g, mask_b;
1157 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1190 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1158 unsigned short low; 1191 unsigned short low;
1159 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 1192 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1164 mask_r = visual->red_mask; 1197 mask_r = visual->red_mask;
1165 mask_g = visual->green_mask; 1198 mask_g = visual->green_mask;
1166 mask_b = visual->blue_mask; 1199 mask_b = visual->blue_mask;
1167 1200
1168 /* boring lookup table pre-initialization */ 1201 /* boring lookup table pre-initialization */
1169 switch (ximage->depth) 1202 sh_r = ecb_ctz32 (mask_r);
1170 { 1203 sh_g = ecb_ctz32 (mask_g);
1171 case 15: 1204 sh_b = ecb_ctz32 (mask_b);
1172 if ((mask_r != 0x7c00) || 1205
1173 (mask_g != 0x03e0) || 1206 size_r = mask_r >> sh_r;
1174 (mask_b != 0x001f)) 1207 size_g = mask_g >> sh_g;
1208 size_b = mask_b >> sh_b;
1209
1210 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1175 return; 1211 return;
1176 lookup = (uint32_t *) malloc (sizeof (uint32_t)*(32+32+32)); 1212
1213 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1177 lookup_r = lookup; 1214 lookup_r = lookup;
1178 lookup_g = lookup+32; 1215 lookup_g = lookup + size_r;
1179 lookup_b = lookup+32+32; 1216 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 1217
1227 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); 1218 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1228 1219
1229 if (bg_flags & BG_TINT_SET) 1220 if (bg_flags & BG_TINT_SET)
1230 tint.get (c); 1221 tint.get (c);
1252 fill_lut (lookup_g, mask_g, sh_g, low, c.g); 1243 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1253 fill_lut (lookup_b, mask_b, sh_b, low, c.b); 1244 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1254 1245
1255 /* apply table to input image (replacing colors by newly calculated ones) */ 1246 /* apply table to input image (replacing colors by newly calculated ones) */
1256 if (ximage->bits_per_pixel == 32 1247 if (ximage->bits_per_pixel == 32
1257 && (ximage->depth == 24 || ximage->depth == 32)
1258 && ximage->byte_order == host_byte_order) 1248 && ximage->byte_order == host_byte_order)
1259 { 1249 {
1260 uint32_t *p1, *pf, *p, *pl; 1250 char *line = ximage->data;
1261 p1 = (uint32_t *) ximage->data;
1262 pf = (uint32_t *) (ximage->data + ximage->height * ximage->bytes_per_line);
1263 1251
1264 while (p1 < pf) 1252 for (int y = 0; y < ximage->height; y++)
1265 {
1266 p = p1;
1267 pl = p1 + ximage->width;
1268 for (; p < pl; p++)
1269 { 1253 {
1270 *p = lookup_r[(*p & 0xff0000) >> 16] | 1254 uint32_t *p = (uint32_t *)line;
1271 lookup_g[(*p & 0x00ff00) >> 8] | 1255 for (int x = 0; x < ximage->width; x++)
1272 lookup_b[(*p & 0x0000ff)] |
1273 (*p & 0xff000000);
1274 } 1256 {
1275 p1 = (uint32_t *) ((char *) p1 + ximage->bytes_per_line); 1257 *p = lookup_r[(*p & mask_r) >> sh_r] |
1258 lookup_g[(*p & mask_g) >> sh_g] |
1259 lookup_b[(*p & mask_b) >> sh_b];
1260 p++;
1261 }
1262 line += ximage->bytes_per_line;
1276 } 1263 }
1277 } 1264 }
1278 else 1265 else
1279 { 1266 {
1280 for (int y = 0; y < ximage->height; y++) 1267 for (int y = 0; y < ximage->height; y++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines