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.112 by root, Tue Sep 17 18:31:32 2019 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
402{ 402{
403 GError *err = 0; 403 GError *err = 0;
404 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); 404 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
405 405
406 if (!pb) 406 if (!pb)
407 try
408 {
407 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); 409 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
410 }
411 catch (...)
412 {
413 g_error_free (err);
414 throw;
415 }
408 416
409 rxvt_img *img = new_from_pixbuf (s, pb); 417 rxvt_img *img = new_from_pixbuf (s, pb);
410 418
411 g_object_unref (pb); 419 g_object_unref (pb);
412 420
420{ 428{
421 if (--ref->cnt) 429 if (--ref->cnt)
422 return; 430 return;
423 431
424 if (pm && ref->ours) 432 if (pm && ref->ours)
425 XFreePixmap (s->dpy, pm); 433 XFreePixmap (d->dpy, pm);
426 434
427 delete ref; 435 delete ref;
428} 436}
429 437
430rxvt_img::~rxvt_img () 438rxvt_img::~rxvt_img ()
433} 441}
434 442
435void 443void
436rxvt_img::alloc () 444rxvt_img::alloc ()
437{ 445{
438 pm = XCreatePixmap (s->dpy, s->display->root, w, h, format->depth); 446 pm = XCreatePixmap (d->dpy, d->root, w, h, format->depth);
439 ref = new pixref (w, h); 447 ref = new pixref (w, h);
440} 448}
441 449
442rxvt_img * 450rxvt_img *
443rxvt_img::new_empty () 451rxvt_img::new_empty ()
444{ 452{
445 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 453 rxvt_img *img = new rxvt_img (d, format, x, y, w, h, repeat);
446 img->alloc (); 454 img->alloc ();
447 455
448 return img; 456 return img;
449} 457}
450 458
451Picture 459Picture
452rxvt_img::picture () 460rxvt_img::picture ()
453{ 461{
454 Display *dpy = s->dpy; 462 Display *dpy = d->dpy;
455 463
456 XRenderPictureAttributes pa; 464 XRenderPictureAttributes pa;
457 pa.repeat = repeat; 465 pa.repeat = repeat;
458 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 466 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
459 467
464rxvt_img::unshare () 472rxvt_img::unshare ()
465{ 473{
466 if (ref->cnt == 1 && ref->ours) 474 if (ref->cnt == 1 && ref->ours)
467 return; 475 return;
468 476
469 Pixmap pm2 = XCreatePixmap (s->dpy, s->display->root, ref->w, ref->h, format->depth); 477 Pixmap pm2 = XCreatePixmap (d->dpy, d->root, ref->w, ref->h, format->depth);
470 GC gc = XCreateGC (s->dpy, pm, 0, 0); 478 GC gc = XCreateGC (d->dpy, pm, 0, 0);
471 XCopyArea (s->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); 479 XCopyArea (d->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
472 XFreeGC (s->dpy, gc); 480 XFreeGC (d->dpy, gc);
473 481
474 destroy (); 482 destroy ();
475 483
476 pm = pm2; 484 pm = pm2;
477 ref = new pixref (ref->w, ref->h); 485 ref = new pixref (ref->w, ref->h);
478} 486}
479 487
480void 488void
489rxvt_img::fill (const rgba &c, int x, int y, int w, int h)
490{
491 XRenderColor rc = { c.r, c.g, c.b, c.a };
492
493 Display *dpy = d->dpy;
494 Picture src = picture ();
495 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, x, y, w, h);
496 XRenderFreePicture (dpy, src);
497}
498
499void
481rxvt_img::fill (const rgba &c) 500rxvt_img::fill (const rgba &c)
482{ 501{
483 XRenderColor rc = { c.r, c.g, c.b, c.a }; 502 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} 503}
490 504
491void 505void
492rxvt_img::add_alpha () 506rxvt_img::add_alpha ()
493{ 507{
494 if (format->direct.alphaMask) 508 if (format->direct.alphaMask)
495 return; 509 return;
496 510
497 composer cc (this, new rxvt_img (s, find_alpha_format_for (s->dpy, format), x, y, w, h, repeat)); 511 composer cc (this, new rxvt_img (d, find_alpha_format_for (d->dpy, format), x, y, w, h, repeat));
498 512
499 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h); 513 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
500 514
501 rxvt_img *img = cc; 515 rxvt_img *img = cc;
502 516
528} 542}
529 543
530rxvt_img * 544rxvt_img *
531rxvt_img::blur (int rh, int rv) 545rxvt_img::blur (int rh, int rv)
532{ 546{
533 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 547 if (!(d->flags & DISPLAY_HAS_RENDER_CONV))
534 return clone (); 548 return clone ();
535 549
536 Display *dpy = s->dpy; 550 Display *dpy = d->dpy;
537 int size = max (rh, rv) * 2 + 1; 551 int size = max (rh, rv) * 2 + 1;
538 nv *kernel = (nv *)malloc (size * sizeof (nv)); 552 nv *kernel = (nv *)malloc (size * sizeof (nv));
539 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 553 XFixed *params = rxvt_temp_buf<XFixed> (size + 2);
540 rxvt_img *img = new_empty (); 554 rxvt_img *img = new_empty ();
541 555
542 XRenderPictureAttributes pa; 556 XRenderPictureAttributes pa;
543 pa.repeat = RepeatPad; 557 pa.repeat = RepeatPad;
544 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 558 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
579 0, 0, 593 0, 0,
580 w, h); 594 w, h);
581 } 595 }
582 596
583 free (kernel); 597 free (kernel);
584 free (params);
585 598
586 XRenderFreePicture (dpy, src); 599 XRenderFreePicture (dpy, src);
587 XRenderFreePicture (dpy, dst); 600 XRenderFreePicture (dpy, dst);
588 XRenderFreePicture (dpy, tmp); 601 XRenderFreePicture (dpy, tmp);
589 602
590 return img; 603 return img;
604}
605
606rxvt_img *
607rxvt_img::muladd (nv mul, nv add)
608{
609 // STEP 1: double the image width, fill all odd columns with white (==1)
610
611 composer cc (this, new rxvt_img (d, format, 0, 0, w * 2, h, repeat));
612
613 // why the hell does XRenderSetPictureTransform want a writable matrix :(
614 // that keeps us from just static const'ing this matrix.
615 XTransform h_double = {
616 0x08000, 0, 0,
617 0, 0x10000, 0,
618 0, 0, 0x10000
619 };
620
621 XRenderSetPictureFilter (cc.dpy, cc.src, "nearest", 0, 0);
622 XRenderSetPictureTransform (cc.dpy, cc.src, &h_double);
623 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
624
625 cc.mask (false, 2, 1);
626
627 static const XRenderColor c0 = { 0, 0, 0, 0 };
628 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c0, 0, 0, 1, 1);
629 static const XRenderColor c1 = { 65535, 65535, 65535, 65535 };
630 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c1, 1, 0, 1, 1);
631
632 Picture white = XRenderCreateSolidFill (cc.dpy, &c1);
633
634 XRenderComposite (cc.dpy, PictOpOver, white, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
635
636 XRenderFreePicture (cc.dpy, white);
637
638 // STEP 2: convolve the image with a 3x1 filter
639 // a 2x1 filter would obviously suffice, but given the total lack of specification
640 // for xrender, I expect different xrender implementations to randomly diverge.
641 // we also halve the image, and hope for the best (again, for lack of specs).
642 composer cc2 (cc.dstimg);
643
644 XFixed kernel [] = {
645 XDoubleToFixed (3), XDoubleToFixed (1),
646 XDoubleToFixed (0), XDoubleToFixed (mul), XDoubleToFixed (add)
647 };
648
649 XTransform h_halve = {
650 0x20000, 0, 0,
651 0, 0x10000, 0,
652 0, 0, 0x10000
653 };
654
655 XRenderSetPictureFilter (cc.dpy, cc2.src, "nearest", 0, 0);
656 XRenderSetPictureTransform (cc.dpy, cc2.src, &h_halve);
657 XRenderSetPictureFilter (cc.dpy, cc2.src, FilterConvolution, kernel, ecb_array_length (kernel));
658
659 XRenderComposite (cc.dpy, PictOpSrc, cc2.src, None, cc2.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
660
661 return cc2;
591} 662}
592 663
593ecb_noinline static void 664ecb_noinline static void
594extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) 665extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
595{ 666{
612void 683void
613rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) 684rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
614{ 685{
615 unshare (); 686 unshare ();
616 687
617 Display *dpy = s->dpy; 688 Display *dpy = d->dpy;
618 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 689 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
619 690
620 // loop should not be needed for brightness, as only -1..1 makes sense 691 // loop should not be needed for brightness, as only -1..1 makes sense
621 //while (r | g | b | a) 692 //while (r | g | b | a)
622 { 693 {
701rxvt_img::reify () 772rxvt_img::reify ()
702{ 773{
703 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 774 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
704 return clone (); 775 return clone ();
705 776
706 Display *dpy = s->dpy;
707
708 // add an alpha channel if... 777 // add an alpha channel if...
709 bool alpha = !format->direct.alphaMask // pixmap has none yet 778 bool alpha = !format->direct.alphaMask // pixmap has none yet
710 && (x || y) // we need one because of non-zero offset 779 && (x || y) // we need one because of non-zero offset
711 && repeat == RepeatNone; // and we have no good pixels to fill with 780 && repeat == RepeatNone; // and we have no good pixels to fill with
712 781
713 composer cc (this, new rxvt_img (s, alpha ? find_alpha_format_for (s->dpy, format) : format, 782 composer cc (this, new rxvt_img (d, alpha ? find_alpha_format_for (d->dpy, format) : format,
714 0, 0, w, h, repeat)); 783 0, 0, w, h, repeat));
715 784
716 if (alpha) 785 if (repeat == RepeatNone)
717 { 786 {
718 XRenderColor rc = { 0, 0, 0, 0 }; 787 XRenderColor rc = { 0, 0, 0, 0 };
719 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 788 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); 789 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
721 } 790 }
780 int new_width = ceil (rmax [0] - rmin [0]); 849 int new_width = ceil (rmax [0] - rmin [0]);
781 int new_height = ceil (rmax [1] - rmin [1]); 850 int new_height = ceil (rmax [1] - rmin [1]);
782 851
783 mat3x3 inv = (mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y)).inverse (); 852 mat3x3 inv = (mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y)).inverse ();
784 853
785 composer cc (this, new rxvt_img (s, format, nx, ny, new_width, new_height, repeat)); 854 composer cc (this, new rxvt_img (d, format, nx, ny, new_width, new_height, repeat));
786 855
787 XTransform xfrm; 856 XTransform xfrm;
788 857
789 for (int i = 0; i < 3; ++i) 858 for (int i = 0; i < 3; ++i)
790 for (int j = 0; j < 3; ++j) 859 for (int j = 0; j < 3; ++j)
829rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) 898rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
830{ 899{
831 if (new_format == format) 900 if (new_format == format)
832 return clone (); 901 return clone ();
833 902
834 composer cc (this, new rxvt_img (s, new_format, x, y, w, h, repeat)); 903 composer cc (this, new rxvt_img (d, new_format, x, y, w, h, repeat));
835 904
836 int op = PictOpSrc; 905 int op = PictOpSrc;
837 906
838 if (format->direct.alphaMask && !new_format->direct.alphaMask) 907 if (format->direct.alphaMask && !new_format->direct.alphaMask)
839 { 908 {
860 929
861 return cc; 930 return cc;
862} 931}
863 932
864rxvt_img * 933rxvt_img *
934rxvt_img::shade (nv factor, rgba c)
935{
936 clamp_it (factor, -1., 1.);
937 factor++;
938
939 if (factor > 1)
940 {
941 c.r = c.r * (2 - factor);
942 c.g = c.g * (2 - factor);
943 c.b = c.b * (2 - factor);
944 }
945 else
946 {
947 c.r = c.r * factor;
948 c.g = c.g * factor;
949 c.b = c.b * factor;
950 }
951
952 rxvt_img *img = this->tint (c);
953
954 if (factor > 1)
955 {
956 c.a = 0xffff;
957 c.r =
958 c.g =
959 c.b = 0xffff * (factor - 1);
960
961 img->brightness (c.r, c.g, c.b, c.a);
962 }
963
964 return img;
965}
966
967rxvt_img *
865rxvt_img::filter (const char *name, int nparams, nv *params) 968rxvt_img::filter (const char *name, int nparams, nv *params)
866{ 969{
867 composer cc (this); 970 composer cc (this);
868 971
869 XFixed *xparams = rxvt_temp_buf<XFixed> (nparams); 972 XFixed *xparams = rxvt_temp_buf<XFixed> (nparams);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines