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

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.99 by root, Mon Jul 2 01:32:26 2012 UTC vs.
Revision 1.109 by sf-exg, Sat Jul 15 08:16:31 2017 UTC

6 * Copyright (c) 2012 Marc Lehmann <schmorp@schmorp.de> 6 * Copyright (c) 2012 Marc Lehmann <schmorp@schmorp.de>
7 * Copyright (c) 2012 Emanuele Giaquinta <e.giaquinta@glauco.it> 7 * Copyright (c) 2012 Emanuele Giaquinta <e.giaquinta@glauco.it>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 * 13 *
14 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
164 if (!this->dstimg) 164 if (!this->dstimg)
165 this->dstimg = srcimg->new_empty (); 165 this->dstimg = srcimg->new_empty ();
166 else if (!this->dstimg->pm) // somewhat unsatisfying 166 else if (!this->dstimg->pm) // somewhat unsatisfying
167 this->dstimg->alloc (); 167 this->dstimg->alloc ();
168 168
169 dpy = srcimg->s->dpy; 169 dpy = srcimg->d->dpy;
170 src = srcimg->picture (); 170 src = srcimg->picture ();
171 dst = this->dstimg->picture (); 171 dst = this->dstimg->picture ();
172 } 172 }
173 173
174 ecb_noinline 174 ecb_noinline
175 void mask (bool rgb = true, int x = 1, int y = 1) 175 void mask (bool rgb = true, int w = 1, int h = 1)
176 { 176 {
177 Pixmap pixmap = XCreatePixmap (dpy, srcimg->pm, x, y, rgb ? 32 : 8); 177 Pixmap pixmap = XCreatePixmap (dpy, srcimg->pm, w, h, rgb ? 32 : 8);
178 178
179 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, rgb ? PictStandardARGB32 : PictStandardA8); 179 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, rgb ? PictStandardARGB32 : PictStandardA8);
180 XRenderPictureAttributes pa; 180 XRenderPictureAttributes pa;
181 pa.repeat = RepeatNormal; 181 pa.repeat = RepeatNormal;
182 pa.component_alpha = rgb; 182 pa.component_alpha = rgb;
188 } 188 }
189 189
190 // CreateSolidFill creates a very very very weird picture 190 // CreateSolidFill creates a very very very weird picture
191 void mask (const rgba &c) 191 void mask (const rgba &c)
192 { 192 {
193 // the casts are needed in C++11 (see 8.5.1)
193 XRenderColor rc = { 194 XRenderColor rc = {
194 c.r * c.a / 65535, 195 (unsigned short)(c.r * c.a / 65535),
195 c.g * c.a / 65535, 196 (unsigned short)(c.g * c.a / 65535),
196 c.b * c.a / 65535, 197 (unsigned short)(c.b * c.a / 65535),
197 c.a 198 c.a
198 }; 199 };
199 msk = XRenderCreateSolidFill (dpy, &rc); 200 msk = XRenderCreateSolidFill (dpy, &rc);
200 ecb_assume (msk); 201 ecb_assume (msk);
201 } 202 }
202 203
203 void fill (const rgba &c) 204 void fill (const rgba &c)
204 { 205 {
205 XRenderColor rc = { 206 XRenderColor rc = {
206 c.r * c.a / 65535, 207 (unsigned short)(c.r * c.a / 65535),
207 c.g * c.a / 65535, 208 (unsigned short)(c.g * c.a / 65535),
208 c.b * c.a / 65535, 209 (unsigned short)(c.b * c.a / 65535),
209 c.a 210 c.a
210 }; 211 };
211 212
212 XRenderFillRectangle (dpy, PictOpSrc, msk, &rc, 0, 0, 1, 1); 213 XRenderFillRectangle (dpy, PictOpSrc, msk, &rc, 0, 0, 1, 1);
213 } 214 }
214 215
246 // should be a very good fallback 247 // should be a very good fallback
247 return XRenderFindStandardFormat (dpy, PictStandardARGB32); 248 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
248} 249}
249 250
250rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) 251rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
251: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), 252: d(screen->display), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
252 pm(0), ref(0) 253 pm(0), ref(0)
253{ 254{
254} 255}
255 256
257rxvt_img::rxvt_img (rxvt_display *display, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
258: d(display), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
259 pm(0), ref(0)
260{
261}
262
256rxvt_img::rxvt_img (const rxvt_img &img) 263rxvt_img::rxvt_img (const rxvt_img &img)
257: s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), ref(img.ref) 264: d(img.d), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), ref(img.ref)
258{ 265{
259 ++ref->cnt; 266 ++ref->cnt;
260} 267}
261 268
262rxvt_img * 269rxvt_img *
349 for (int y = 0; y < height; y++) 356 for (int y = 0; y < height; y++)
350 { 357 {
351 unsigned char *src = row; 358 unsigned char *src = row;
352 uint32_t *dst = (uint32_t *)line; 359 uint32_t *dst = (uint32_t *)line;
353 360
354 if (!pb_has_alpha)
355 for (int x = 0; x < width; x++) 361 for (int x = 0; x < width; x++)
356 { 362 {
357 uint8_t r = *src++; 363 uint8_t r = *src++;
358 uint8_t g = *src++; 364 uint8_t g = *src++;
359 uint8_t b = *src++; 365 uint8_t b = *src++;
366 uint8_t a = *src;
360 367
368 // this is done so it can be jump-free, but newer gcc's clone inner the loop
369 a = pb_has_alpha ? a : 255;
370 src += pb_has_alpha;
371
372 r = (r * a + 127) / 255;
373 g = (g * a + 127) / 255;
374 b = (b * a + 127) / 255;
375
361 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; 376 uint32_t v = (a << 24) | (r << 16) | (g << 8) | b;
362 377
363 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) 378 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
364 v = ecb_bswap32 (v); 379 v = ecb_bswap32 (v);
365 380
366 *dst++ = v; 381 *dst++ = v;
367 } 382 }
368 else
369 for (int x = 0; x < width; x++)
370 {
371 uint32_t v = *(uint32_t *)src; src += 4;
372
373 if (ecb_big_endian ())
374 v = ecb_bswap32 (v);
375
376 v = ecb_rotl32 (v, 8); // abgr to bgra
377
378 if (!byte_order_mismatch)
379 v = ecb_bswap32 (v);
380
381 *dst++ = v;
382 }
383 383
384 row += rowstride; 384 row += rowstride;
385 line += xi.bytes_per_line; 385 line += xi.bytes_per_line;
386 } 386 }
387 387
420{ 420{
421 if (--ref->cnt) 421 if (--ref->cnt)
422 return; 422 return;
423 423
424 if (pm && ref->ours) 424 if (pm && ref->ours)
425 XFreePixmap (s->dpy, pm); 425 XFreePixmap (d->dpy, pm);
426 426
427 delete ref; 427 delete ref;
428} 428}
429 429
430rxvt_img::~rxvt_img () 430rxvt_img::~rxvt_img ()
433} 433}
434 434
435void 435void
436rxvt_img::alloc () 436rxvt_img::alloc ()
437{ 437{
438 pm = XCreatePixmap (s->dpy, s->display->root, w, h, format->depth); 438 pm = XCreatePixmap (d->dpy, d->root, w, h, format->depth);
439 ref = new pixref (w, h); 439 ref = new pixref (w, h);
440} 440}
441 441
442rxvt_img * 442rxvt_img *
443rxvt_img::new_empty () 443rxvt_img::new_empty ()
444{ 444{
445 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 445 rxvt_img *img = new rxvt_img (d, format, x, y, w, h, repeat);
446 img->alloc (); 446 img->alloc ();
447 447
448 return img; 448 return img;
449} 449}
450 450
451Picture 451Picture
452rxvt_img::picture () 452rxvt_img::picture ()
453{ 453{
454 Display *dpy = s->dpy; 454 Display *dpy = d->dpy;
455 455
456 XRenderPictureAttributes pa; 456 XRenderPictureAttributes pa;
457 pa.repeat = repeat; 457 pa.repeat = repeat;
458 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 458 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
459 459
464rxvt_img::unshare () 464rxvt_img::unshare ()
465{ 465{
466 if (ref->cnt == 1 && ref->ours) 466 if (ref->cnt == 1 && ref->ours)
467 return; 467 return;
468 468
469 Pixmap pm2 = XCreatePixmap (s->dpy, s->display->root, ref->w, ref->h, format->depth); 469 Pixmap pm2 = XCreatePixmap (d->dpy, d->root, ref->w, ref->h, format->depth);
470 GC gc = XCreateGC (s->dpy, pm, 0, 0); 470 GC gc = XCreateGC (d->dpy, pm, 0, 0);
471 XCopyArea (s->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); 471 XCopyArea (d->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
472 XFreeGC (s->dpy, gc); 472 XFreeGC (d->dpy, gc);
473 473
474 destroy (); 474 destroy ();
475 475
476 pm = pm2; 476 pm = pm2;
477 ref = new pixref (ref->w, ref->h); 477 ref = new pixref (ref->w, ref->h);
478} 478}
479 479
480void 480void
481rxvt_img::fill (const rgba &c, int x, int y, int w, int h)
482{
483 XRenderColor rc = { c.r, c.g, c.b, c.a };
484
485 Display *dpy = d->dpy;
486 Picture src = picture ();
487 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, x, y, w, h);
488 XRenderFreePicture (dpy, src);
489}
490
491void
481rxvt_img::fill (const rgba &c) 492rxvt_img::fill (const rgba &c)
482{ 493{
483 XRenderColor rc = { c.r, c.g, c.b, c.a }; 494 fill (c, 0, 0, w, h);
484
485 Display *dpy = s->dpy;
486 Picture src = picture ();
487 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
488 XRenderFreePicture (dpy, src);
489} 495}
490 496
491void 497void
492rxvt_img::add_alpha () 498rxvt_img::add_alpha ()
493{ 499{
494 if (format->direct.alphaMask) 500 if (format->direct.alphaMask)
495 return; 501 return;
496 502
497 composer cc (this, new rxvt_img (s, find_alpha_format_for (s->dpy, format), x, y, w, h, repeat)); 503 composer cc (this, new rxvt_img (d, find_alpha_format_for (d->dpy, format), x, y, w, h, repeat));
498 504
499 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h); 505 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
500 506
501 rxvt_img *img = cc; 507 rxvt_img *img = cc;
502 508
528} 534}
529 535
530rxvt_img * 536rxvt_img *
531rxvt_img::blur (int rh, int rv) 537rxvt_img::blur (int rh, int rv)
532{ 538{
533 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 539 if (!(d->flags & DISPLAY_HAS_RENDER_CONV))
534 return clone (); 540 return clone ();
535 541
536 Display *dpy = s->dpy; 542 Display *dpy = d->dpy;
537 int size = max (rh, rv) * 2 + 1; 543 int size = max (rh, rv) * 2 + 1;
538 nv *kernel = (nv *)malloc (size * sizeof (nv)); 544 nv *kernel = (nv *)malloc (size * sizeof (nv));
539 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 545 XFixed *params = rxvt_temp_buf<XFixed> (size + 2);
540 rxvt_img *img = new_empty (); 546 rxvt_img *img = new_empty ();
541 547
542 XRenderPictureAttributes pa; 548 XRenderPictureAttributes pa;
543 pa.repeat = RepeatPad; 549 pa.repeat = RepeatPad;
544 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 550 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
579 0, 0, 585 0, 0,
580 w, h); 586 w, h);
581 } 587 }
582 588
583 free (kernel); 589 free (kernel);
584 free (params);
585 590
586 XRenderFreePicture (dpy, src); 591 XRenderFreePicture (dpy, src);
587 XRenderFreePicture (dpy, dst); 592 XRenderFreePicture (dpy, dst);
588 XRenderFreePicture (dpy, tmp); 593 XRenderFreePicture (dpy, tmp);
589 594
590 return img; 595 return img;
596}
597
598rxvt_img *
599rxvt_img::muladd (nv mul, nv add)
600{
601 // STEP 1: double the image width, fill all odd columns with white (==1)
602
603 composer cc (this, new rxvt_img (d, format, 0, 0, w * 2, h, repeat));
604
605 // why the hell does XRenderSetPictureTransform want a writable matrix :(
606 // that keeps us from just static const'ing this matrix.
607 XTransform h_double = {
608 0x08000, 0, 0,
609 0, 0x10000, 0,
610 0, 0, 0x10000
611 };
612
613 XRenderSetPictureFilter (cc.dpy, cc.src, "nearest", 0, 0);
614 XRenderSetPictureTransform (cc.dpy, cc.src, &h_double);
615 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
616
617 cc.mask (false, 2, 1);
618
619 static const XRenderColor c0 = { 0, 0, 0, 0 };
620 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c0, 0, 0, 1, 1);
621 static const XRenderColor c1 = { 65535, 65535, 65535, 65535 };
622 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c1, 1, 0, 1, 1);
623
624 Picture white = XRenderCreateSolidFill (cc.dpy, &c1);
625
626 XRenderComposite (cc.dpy, PictOpOver, white, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
627
628 XRenderFreePicture (cc.dpy, white);
629
630 // STEP 2: convolve the image with a 3x1 filter
631 // a 2x1 filter would obviously suffice, but given the total lack of specification
632 // for xrender, I expect different xrender implementations to randomly diverge.
633 // we also halve the image, and hope for the best (again, for lack of specs).
634 composer cc2 (cc.dstimg);
635
636 XFixed kernel [] = {
637 XDoubleToFixed (3), XDoubleToFixed (1),
638 XDoubleToFixed (0), XDoubleToFixed (mul), XDoubleToFixed (add)
639 };
640
641 XTransform h_halve = {
642 0x20000, 0, 0,
643 0, 0x10000, 0,
644 0, 0, 0x10000
645 };
646
647 XRenderSetPictureFilter (cc.dpy, cc2.src, "nearest", 0, 0);
648 XRenderSetPictureTransform (cc.dpy, cc2.src, &h_halve);
649 XRenderSetPictureFilter (cc.dpy, cc2.src, FilterConvolution, kernel, ecb_array_length (kernel));
650
651 XRenderComposite (cc.dpy, PictOpSrc, cc2.src, None, cc2.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
652
653 return cc2;
591} 654}
592 655
593ecb_noinline static void 656ecb_noinline static void
594extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) 657extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
595{ 658{
612void 675void
613rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) 676rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
614{ 677{
615 unshare (); 678 unshare ();
616 679
617 Display *dpy = s->dpy; 680 Display *dpy = d->dpy;
618 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 681 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
619 682
620 // loop should not be needed for brightness, as only -1..1 makes sense 683 // loop should not be needed for brightness, as only -1..1 makes sense
621 //while (r | g | b | a) 684 //while (r | g | b | a)
622 { 685 {
701rxvt_img::reify () 764rxvt_img::reify ()
702{ 765{
703 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 766 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
704 return clone (); 767 return clone ();
705 768
706 Display *dpy = s->dpy;
707
708 // add an alpha channel if... 769 // add an alpha channel if...
709 bool alpha = !format->direct.alphaMask // pixmap has none yet 770 bool alpha = !format->direct.alphaMask // pixmap has none yet
710 && (x || y) // we need one because of non-zero offset 771 && (x || y) // we need one because of non-zero offset
711 && repeat == RepeatNone; // and we have no good pixels to fill with 772 && repeat == RepeatNone; // and we have no good pixels to fill with
712 773
713 composer cc (this, new rxvt_img (s, alpha ? find_alpha_format_for (s->dpy, format) : format, 774 composer cc (this, new rxvt_img (d, alpha ? find_alpha_format_for (d->dpy, format) : format,
714 0, 0, w, h, repeat)); 775 0, 0, w, h, repeat));
715 776
716 if (alpha) 777 if (repeat == RepeatNone)
717 { 778 {
718 XRenderColor rc = { 0, 0, 0, 0 }; 779 XRenderColor rc = { 0, 0, 0, 0 };
719 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 780 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
720 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h); 781 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
721 } 782 }
780 int new_width = ceil (rmax [0] - rmin [0]); 841 int new_width = ceil (rmax [0] - rmin [0]);
781 int new_height = ceil (rmax [1] - rmin [1]); 842 int new_height = ceil (rmax [1] - rmin [1]);
782 843
783 mat3x3 inv = (mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y)).inverse (); 844 mat3x3 inv = (mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y)).inverse ();
784 845
785 composer cc (this, new rxvt_img (s, format, nx, ny, new_width, new_height, repeat)); 846 composer cc (this, new rxvt_img (d, format, nx, ny, new_width, new_height, repeat));
786 847
787 XTransform xfrm; 848 XTransform xfrm;
788 849
789 for (int i = 0; i < 3; ++i) 850 for (int i = 0; i < 3; ++i)
790 for (int j = 0; j < 3; ++j) 851 for (int j = 0; j < 3; ++j)
829rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) 890rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
830{ 891{
831 if (new_format == format) 892 if (new_format == format)
832 return clone (); 893 return clone ();
833 894
834 composer cc (this, new rxvt_img (s, new_format, x, y, w, h, repeat)); 895 composer cc (this, new rxvt_img (d, new_format, x, y, w, h, repeat));
835 896
836 int op = PictOpSrc; 897 int op = PictOpSrc;
837 898
838 if (format->direct.alphaMask && !new_format->direct.alphaMask) 899 if (format->direct.alphaMask && !new_format->direct.alphaMask)
839 { 900 {
860 921
861 return cc; 922 return cc;
862} 923}
863 924
864rxvt_img * 925rxvt_img *
926rxvt_img::shade (nv factor, rgba c)
927{
928 clamp_it (factor, -1., 1.);
929 factor++;
930
931 if (factor > 1)
932 {
933 c.r = c.r * (2 - factor);
934 c.g = c.g * (2 - factor);
935 c.b = c.b * (2 - factor);
936 }
937 else
938 {
939 c.r = c.r * factor;
940 c.g = c.g * factor;
941 c.b = c.b * factor;
942 }
943
944 rxvt_img *img = this->tint (c);
945
946 if (factor > 1)
947 {
948 c.a = 0xffff;
949 c.r =
950 c.g =
951 c.b = 0xffff * (factor - 1);
952
953 img->brightness (c.r, c.g, c.b, c.a);
954 }
955
956 return img;
957}
958
959rxvt_img *
865rxvt_img::filter (const char *name, int nparams, nv *params) 960rxvt_img::filter (const char *name, int nparams, nv *params)
866{ 961{
867 composer cc (this); 962 composer cc (this);
868 963
869 XFixed *xparams = rxvt_temp_buf<XFixed> (nparams); 964 XFixed *xparams = rxvt_temp_buf<XFixed> (nparams);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines