ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
(Generate patch)

Comparing rxvt-unicode/src/background.C (file contents):
Revision 1.201 by sf-exg, Sat Feb 4 21:47:06 2012 UTC vs.
Revision 1.207 by sf-exg, Thu May 10 21:48:55 2012 UTC

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;
427 XDestroyImage (ximage); 462 XDestroyImage (ximage);
428 return true; 463 return true;
429} 464}
430 465
431bool 466bool
432rxvt_term::render_image (unsigned long tr_flags) 467rxvt_term::render_image (bool transparent)
433{ 468{
434 if (!pixbuf) 469 if (!pixbuf)
435 return false; 470 return false;
436 471
437 if (tr_flags 472 if (transparent
438 && !(bg_flags & BG_HAS_RENDER)) 473 && !(bg_flags & BG_HAS_RENDER))
439 return false; 474 return false;
440 475
441 GdkPixbuf *result; 476 GdkPixbuf *result;
442 477
482 Pixmap root_pmap; 517 Pixmap root_pmap;
483 518
484 image_width = gdk_pixbuf_get_width (result); 519 image_width = gdk_pixbuf_get_width (result);
485 image_height = gdk_pixbuf_get_height (result); 520 image_height = gdk_pixbuf_get_height (result);
486 521
487 if (tr_flags) 522 if (transparent)
488 { 523 {
489 root_pmap = bg_pixmap; 524 root_pmap = bg_pixmap;
490 bg_pixmap = None; 525 bg_pixmap = None;
491 } 526 }
492 else 527 else
550 dst_x, dst_y, 585 dst_x, dst_y,
551 dst_width, dst_height); 586 dst_width, dst_height);
552 } 587 }
553 588
554#if XRENDER 589#if XRENDER
555 if (tr_flags) 590 if (transparent)
556 { 591 {
557 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 592 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
558 593
559 Picture src = XRenderCreatePicture (dpy, root_pmap, format, 0, 0); 594 Picture src = XRenderCreatePicture (dpy, root_pmap, format, 0, 0);
560 595
584 } 619 }
585 620
586 if (result != pixbuf) 621 if (result != pixbuf)
587 g_object_unref (result); 622 g_object_unref (result);
588 623
589 if (tr_flags) 624 if (transparent)
590 XFreePixmap (dpy, root_pmap); 625 XFreePixmap (dpy, root_pmap);
591 626
592 return ret; 627 return ret;
593} 628}
594# endif /* HAVE_PIXBUF */ 629# endif /* HAVE_PIXBUF */
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
922/* 954/*
923 * Builds a pixmap of the same size as the terminal window that contains 955 * Builds a pixmap of the same size as the terminal window that contains
924 * the tiled portion of the root pixmap that is supposed to be covered by 956 * the tiled portion of the root pixmap that is supposed to be covered by
925 * our window. 957 * our window.
926 */ 958 */
927unsigned long 959bool
928rxvt_term::make_transparency_pixmap () 960rxvt_term::make_transparency_pixmap ()
929{ 961{
930 unsigned long result = 0; 962 bool ret = false;
931 963
932 /* root dimensions may change from call to call - but Display structure should 964 /* root dimensions may change from call to call - but Display structure should
933 * be always up-to-date, so let's use it : 965 * be always up-to-date, so let's use it :
934 */ 966 */
935 int screen = display->screen; 967 int screen = display->screen;
1016 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 1048 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1017 1049
1018 if (gc) 1050 if (gc)
1019 { 1051 {
1020 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); 1052 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1021 result |= BG_IS_VALID | (bg_flags & BG_EFFECTS_FLAGS); 1053 ret = true;
1054 unsigned long tr_flags = bg_flags & BG_EFFECTS_FLAGS;
1022 1055
1023 if (!(bg_flags & BG_CLIENT_RENDER)) 1056 if (!(bg_flags & BG_CLIENT_RENDER))
1024 { 1057 {
1025 if (bg_flags & BG_NEEDS_BLUR) 1058 if (bg_flags & BG_NEEDS_BLUR)
1026 { 1059 {
1027 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth)) 1060 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth))
1028 result &= ~BG_NEEDS_BLUR; 1061 tr_flags &= ~BG_NEEDS_BLUR;
1029 } 1062 }
1030 if (bg_flags & BG_NEEDS_TINT) 1063 if (bg_flags & BG_NEEDS_TINT)
1031 { 1064 {
1032 if (tint_pixmap (bg_pixmap, visual, window_width, window_height)) 1065 if (tint_pixmap (bg_pixmap, visual, window_width, window_height))
1033 result &= ~BG_NEEDS_TINT; 1066 tr_flags &= ~BG_NEEDS_TINT;
1034 } 1067 }
1035 if (result & BG_NEEDS_TINT) 1068 if (tr_flags & BG_NEEDS_TINT)
1036 { 1069 {
1037 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap); 1070 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1038 if (ximage) 1071 if (ximage)
1039 { 1072 {
1040 /* our own client-side tinting */ 1073 /* our own client-side tinting */
1050 } 1083 }
1051 1084
1052 if (recoded_root_pmap != root_pixmap) 1085 if (recoded_root_pmap != root_pixmap)
1053 XFreePixmap (dpy, recoded_root_pmap); 1086 XFreePixmap (dpy, recoded_root_pmap);
1054 1087
1055 return result; 1088 return ret;
1056} 1089}
1057 1090
1058void 1091void
1059rxvt_term::bg_set_root_pixmap () 1092rxvt_term::bg_set_root_pixmap ()
1060{ 1093{
1067# endif /* ENABLE_TRANSPARENCY */ 1100# endif /* ENABLE_TRANSPARENCY */
1068 1101
1069bool 1102bool
1070rxvt_term::bg_render () 1103rxvt_term::bg_render ()
1071{ 1104{
1072 unsigned long tr_flags = 0; 1105 bool transparent = false;
1073 1106
1074 bg_invalidate (); 1107 bg_invalidate ();
1075# ifdef ENABLE_TRANSPARENCY 1108# ifdef ENABLE_TRANSPARENCY
1076 if (bg_flags & BG_IS_TRANSPARENT) 1109 if (bg_flags & BG_IS_TRANSPARENT)
1077 { 1110 {
1078 /* we need to re-generate transparency pixmap in that case ! */ 1111 /* we need to re-generate transparency pixmap in that case ! */
1079 tr_flags = make_transparency_pixmap (); 1112 transparent = make_transparency_pixmap ();
1080 if (tr_flags) 1113 if (transparent)
1081 bg_flags |= BG_IS_VALID; 1114 bg_flags |= BG_IS_VALID;
1082 } 1115 }
1083# endif 1116# endif
1084 1117
1085# ifdef BG_IMAGE_FROM_FILE 1118# ifdef BG_IMAGE_FROM_FILE
1086 if ((bg_flags & BG_IS_FROM_FILE) 1119 if (bg_flags & BG_IS_FROM_FILE)
1087 || (tr_flags & BG_EFFECTS_FLAGS))
1088 { 1120 {
1089 if (render_image (tr_flags)) 1121 if (render_image (transparent))
1090 bg_flags |= BG_IS_VALID; 1122 bg_flags |= BG_IS_VALID;
1091 } 1123 }
1092# endif 1124# endif
1093 1125
1094 if (!(bg_flags & BG_IS_VALID)) 1126 if (!(bg_flags & BG_IS_VALID))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines