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.101 by root, Mon Jul 2 03:57:53 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
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);
480void 488void
481rxvt_img::fill (const rgba &c, int x, int y, int w, int h) 489rxvt_img::fill (const rgba &c, int x, int y, int w, int h)
482{ 490{
483 XRenderColor rc = { c.r, c.g, c.b, c.a }; 491 XRenderColor rc = { c.r, c.g, c.b, c.a };
484 492
485 Display *dpy = s->dpy; 493 Display *dpy = d->dpy;
486 Picture src = picture (); 494 Picture src = picture ();
487 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, x, y, w, h); 495 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, x, y, w, h);
488 XRenderFreePicture (dpy, src); 496 XRenderFreePicture (dpy, src);
489} 497}
490 498
498rxvt_img::add_alpha () 506rxvt_img::add_alpha ()
499{ 507{
500 if (format->direct.alphaMask) 508 if (format->direct.alphaMask)
501 return; 509 return;
502 510
503 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));
504 512
505 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);
506 514
507 rxvt_img *img = cc; 515 rxvt_img *img = cc;
508 516
534} 542}
535 543
536rxvt_img * 544rxvt_img *
537rxvt_img::blur (int rh, int rv) 545rxvt_img::blur (int rh, int rv)
538{ 546{
539 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 547 if (!(d->flags & DISPLAY_HAS_RENDER_CONV))
540 return clone (); 548 return clone ();
541 549
542 Display *dpy = s->dpy; 550 Display *dpy = d->dpy;
543 int size = max (rh, rv) * 2 + 1; 551 int size = max (rh, rv) * 2 + 1;
544 nv *kernel = (nv *)malloc (size * sizeof (nv)); 552 nv *kernel = (nv *)malloc (size * sizeof (nv));
545 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 553 XFixed *params = rxvt_temp_buf<XFixed> (size + 2);
546 rxvt_img *img = new_empty (); 554 rxvt_img *img = new_empty ();
547 555
548 XRenderPictureAttributes pa; 556 XRenderPictureAttributes pa;
549 pa.repeat = RepeatPad; 557 pa.repeat = RepeatPad;
550 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 558 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
585 0, 0, 593 0, 0,
586 w, h); 594 w, h);
587 } 595 }
588 596
589 free (kernel); 597 free (kernel);
590 free (params);
591 598
592 XRenderFreePicture (dpy, src); 599 XRenderFreePicture (dpy, src);
593 XRenderFreePicture (dpy, dst); 600 XRenderFreePicture (dpy, dst);
594 XRenderFreePicture (dpy, tmp); 601 XRenderFreePicture (dpy, tmp);
595 602
599rxvt_img * 606rxvt_img *
600rxvt_img::muladd (nv mul, nv add) 607rxvt_img::muladd (nv mul, nv add)
601{ 608{
602 // STEP 1: double the image width, fill all odd columns with white (==1) 609 // STEP 1: double the image width, fill all odd columns with white (==1)
603 610
604 composer cc (this, new rxvt_img (s, format, 0, 0, w * 2, h, repeat)); 611 composer cc (this, new rxvt_img (d, format, 0, 0, w * 2, h, repeat));
605 612
606 // why the hell does XRenderSetPictureTransform want a writable matrix :( 613 // why the hell does XRenderSetPictureTransform want a writable matrix :(
607 // that keeps us from just static const'ing this matrix. 614 // that keeps us from just static const'ing this matrix.
608 XTransform h_double = { 615 XTransform h_double = {
609 32768, 0, 0, 616 0x08000, 0, 0,
610 0, 65536, 0, 617 0, 0x10000, 0,
611 0, 0, 65536 618 0, 0, 0x10000
612 }; 619 };
613 620
614 XRenderSetPictureFilter (cc.dpy, cc.src, "nearest", 0, 0); 621 XRenderSetPictureFilter (cc.dpy, cc.src, "nearest", 0, 0);
615 XRenderSetPictureTransform (cc.dpy, cc.src, &h_double); 622 XRenderSetPictureTransform (cc.dpy, cc.src, &h_double);
616 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h); 623 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
638 XDoubleToFixed (3), XDoubleToFixed (1), 645 XDoubleToFixed (3), XDoubleToFixed (1),
639 XDoubleToFixed (0), XDoubleToFixed (mul), XDoubleToFixed (add) 646 XDoubleToFixed (0), XDoubleToFixed (mul), XDoubleToFixed (add)
640 }; 647 };
641 648
642 XTransform h_halve = { 649 XTransform h_halve = {
643 131072, 0, 0, 650 0x20000, 0, 0,
644 0, 65536, 0, 651 0, 0x10000, 0,
645 0, 0, 65536 652 0, 0, 0x10000
646 }; 653 };
647 654
648 XRenderSetPictureFilter (cc.dpy, cc2.src, "nearest", 0, 0); 655 XRenderSetPictureFilter (cc.dpy, cc2.src, "nearest", 0, 0);
649 XRenderSetPictureTransform (cc.dpy, cc2.src, &h_halve); 656 XRenderSetPictureTransform (cc.dpy, cc2.src, &h_halve);
650 XRenderSetPictureFilter (cc.dpy, cc2.src, FilterConvolution, kernel, ecb_array_length (kernel)); 657 XRenderSetPictureFilter (cc.dpy, cc2.src, FilterConvolution, kernel, ecb_array_length (kernel));
676void 683void
677rxvt_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)
678{ 685{
679 unshare (); 686 unshare ();
680 687
681 Display *dpy = s->dpy; 688 Display *dpy = d->dpy;
682 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 689 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
683 690
684 // 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
685 //while (r | g | b | a) 692 //while (r | g | b | a)
686 { 693 {
765rxvt_img::reify () 772rxvt_img::reify ()
766{ 773{
767 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 774 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
768 return clone (); 775 return clone ();
769 776
770 Display *dpy = s->dpy;
771
772 // add an alpha channel if... 777 // add an alpha channel if...
773 bool alpha = !format->direct.alphaMask // pixmap has none yet 778 bool alpha = !format->direct.alphaMask // pixmap has none yet
774 && (x || y) // we need one because of non-zero offset 779 && (x || y) // we need one because of non-zero offset
775 && repeat == RepeatNone; // and we have no good pixels to fill with 780 && repeat == RepeatNone; // and we have no good pixels to fill with
776 781
777 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,
778 0, 0, w, h, repeat)); 783 0, 0, w, h, repeat));
779 784
780 if (alpha) 785 if (repeat == RepeatNone)
781 { 786 {
782 XRenderColor rc = { 0, 0, 0, 0 }; 787 XRenderColor rc = { 0, 0, 0, 0 };
783 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
784 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);
785 } 790 }
844 int new_width = ceil (rmax [0] - rmin [0]); 849 int new_width = ceil (rmax [0] - rmin [0]);
845 int new_height = ceil (rmax [1] - rmin [1]); 850 int new_height = ceil (rmax [1] - rmin [1]);
846 851
847 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 ();
848 853
849 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));
850 855
851 XTransform xfrm; 856 XTransform xfrm;
852 857
853 for (int i = 0; i < 3; ++i) 858 for (int i = 0; i < 3; ++i)
854 for (int j = 0; j < 3; ++j) 859 for (int j = 0; j < 3; ++j)
893rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) 898rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
894{ 899{
895 if (new_format == format) 900 if (new_format == format)
896 return clone (); 901 return clone ();
897 902
898 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));
899 904
900 int op = PictOpSrc; 905 int op = PictOpSrc;
901 906
902 if (format->direct.alphaMask && !new_format->direct.alphaMask) 907 if (format->direct.alphaMask && !new_format->direct.alphaMask)
903 { 908 {
924 929
925 return cc; 930 return cc;
926} 931}
927 932
928rxvt_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 *
929rxvt_img::filter (const char *name, int nparams, nv *params) 968rxvt_img::filter (const char *name, int nparams, nv *params)
930{ 969{
931 composer cc (this); 970 composer cc (this);
932 971
933 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