… | |
… | |
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 |
… | |
… | |
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; |
… | |
… | |
349 | for (int y = 0; y < height; y++) |
349 | for (int y = 0; y < height; y++) |
350 | { |
350 | { |
351 | unsigned char *src = row; |
351 | unsigned char *src = row; |
352 | uint32_t *dst = (uint32_t *)line; |
352 | uint32_t *dst = (uint32_t *)line; |
353 | |
353 | |
354 | if (!pb_has_alpha) |
|
|
355 | for (int x = 0; x < width; x++) |
354 | for (int x = 0; x < width; x++) |
356 | { |
355 | { |
357 | uint8_t r = *src++; |
356 | uint8_t r = *src++; |
358 | uint8_t g = *src++; |
357 | uint8_t g = *src++; |
359 | uint8_t b = *src++; |
358 | uint8_t b = *src++; |
|
|
359 | uint8_t a = *src; |
360 | |
360 | |
|
|
361 | // this is done so it can be jump-free, but newer gcc's clone inner the loop |
|
|
362 | a = pb_has_alpha ? a : 255; |
|
|
363 | src += pb_has_alpha; |
|
|
364 | |
|
|
365 | r = (r * a + 127) / 255; |
|
|
366 | g = (g * a + 127) / 255; |
|
|
367 | b = (b * a + 127) / 255; |
|
|
368 | |
361 | uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; |
369 | uint32_t v = (a << 24) | (r << 16) | (g << 8) | b; |
362 | |
370 | |
363 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
371 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
364 | v = ecb_bswap32 (v); |
372 | v = ecb_bswap32 (v); |
365 | |
373 | |
366 | *dst++ = v; |
374 | *dst++ = v; |
367 | } |
375 | } |
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 | |
376 | |
384 | row += rowstride; |
377 | row += rowstride; |
385 | line += xi.bytes_per_line; |
378 | line += xi.bytes_per_line; |
386 | } |
379 | } |
387 | |
380 | |
… | |
… | |
540 | return clone (); |
533 | return clone (); |
541 | |
534 | |
542 | Display *dpy = s->dpy; |
535 | Display *dpy = s->dpy; |
543 | int size = max (rh, rv) * 2 + 1; |
536 | int size = max (rh, rv) * 2 + 1; |
544 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
537 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
545 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
538 | XFixed *params = rxvt_temp_buf<XFixed> (size + 2); |
546 | rxvt_img *img = new_empty (); |
539 | rxvt_img *img = new_empty (); |
547 | |
540 | |
548 | XRenderPictureAttributes pa; |
541 | XRenderPictureAttributes pa; |
549 | pa.repeat = RepeatPad; |
542 | pa.repeat = RepeatPad; |
550 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
543 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
585 | 0, 0, |
578 | 0, 0, |
586 | w, h); |
579 | w, h); |
587 | } |
580 | } |
588 | |
581 | |
589 | free (kernel); |
582 | free (kernel); |
590 | free (params); |
|
|
591 | |
583 | |
592 | XRenderFreePicture (dpy, src); |
584 | XRenderFreePicture (dpy, src); |
593 | XRenderFreePicture (dpy, dst); |
585 | XRenderFreePicture (dpy, dst); |
594 | XRenderFreePicture (dpy, tmp); |
586 | XRenderFreePicture (dpy, tmp); |
595 | |
587 | |
596 | return img; |
588 | return img; |
|
|
589 | } |
|
|
590 | |
|
|
591 | rxvt_img * |
|
|
592 | rxvt_img::muladd (nv mul, nv add) |
|
|
593 | { |
|
|
594 | // STEP 1: double the image width, fill all odd columns with white (==1) |
|
|
595 | |
|
|
596 | composer cc (this, new rxvt_img (s, format, 0, 0, w * 2, h, repeat)); |
|
|
597 | |
|
|
598 | // why the hell does XRenderSetPictureTransform want a writable matrix :( |
|
|
599 | // that keeps us from just static const'ing this matrix. |
|
|
600 | XTransform h_double = { |
|
|
601 | 0x08000, 0, 0, |
|
|
602 | 0, 0x10000, 0, |
|
|
603 | 0, 0, 0x10000 |
|
|
604 | }; |
|
|
605 | |
|
|
606 | XRenderSetPictureFilter (cc.dpy, cc.src, "nearest", 0, 0); |
|
|
607 | XRenderSetPictureTransform (cc.dpy, cc.src, &h_double); |
|
|
608 | XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h); |
|
|
609 | |
|
|
610 | cc.mask (false, 2, 1); |
|
|
611 | |
|
|
612 | static const XRenderColor c0 = { 0, 0, 0, 0 }; |
|
|
613 | XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c0, 0, 0, 1, 1); |
|
|
614 | static const XRenderColor c1 = { 65535, 65535, 65535, 65535 }; |
|
|
615 | XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c1, 1, 0, 1, 1); |
|
|
616 | |
|
|
617 | Picture white = XRenderCreateSolidFill (cc.dpy, &c1); |
|
|
618 | |
|
|
619 | XRenderComposite (cc.dpy, PictOpOver, white, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h); |
|
|
620 | |
|
|
621 | XRenderFreePicture (cc.dpy, white); |
|
|
622 | |
|
|
623 | // STEP 2: convolve the image with a 3x1 filter |
|
|
624 | // a 2x1 filter would obviously suffice, but given the total lack of specification |
|
|
625 | // for xrender, I expect different xrender implementations to randomly diverge. |
|
|
626 | // we also halve the image, and hope for the best (again, for lack of specs). |
|
|
627 | composer cc2 (cc.dstimg); |
|
|
628 | |
|
|
629 | XFixed kernel [] = { |
|
|
630 | XDoubleToFixed (3), XDoubleToFixed (1), |
|
|
631 | XDoubleToFixed (0), XDoubleToFixed (mul), XDoubleToFixed (add) |
|
|
632 | }; |
|
|
633 | |
|
|
634 | XTransform h_halve = { |
|
|
635 | 0x20000, 0, 0, |
|
|
636 | 0, 0x10000, 0, |
|
|
637 | 0, 0, 0x10000 |
|
|
638 | }; |
|
|
639 | |
|
|
640 | XRenderSetPictureFilter (cc.dpy, cc2.src, "nearest", 0, 0); |
|
|
641 | XRenderSetPictureTransform (cc.dpy, cc2.src, &h_halve); |
|
|
642 | XRenderSetPictureFilter (cc.dpy, cc2.src, FilterConvolution, kernel, ecb_array_length (kernel)); |
|
|
643 | |
|
|
644 | XRenderComposite (cc.dpy, PictOpSrc, cc2.src, None, cc2.dst, 0, 0, 0, 0, 0, 0, w * 2, h); |
|
|
645 | |
|
|
646 | return cc2; |
597 | } |
647 | } |
598 | |
648 | |
599 | ecb_noinline static void |
649 | ecb_noinline static void |
600 | extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) |
650 | extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) |
601 | { |
651 | { |
… | |
… | |
707 | rxvt_img::reify () |
757 | rxvt_img::reify () |
708 | { |
758 | { |
709 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
759 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
710 | return clone (); |
760 | return clone (); |
711 | |
761 | |
712 | Display *dpy = s->dpy; |
|
|
713 | |
|
|
714 | // add an alpha channel if... |
762 | // add an alpha channel if... |
715 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
763 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
716 | && (x || y) // we need one because of non-zero offset |
764 | && (x || y) // we need one because of non-zero offset |
717 | && repeat == RepeatNone; // and we have no good pixels to fill with |
765 | && repeat == RepeatNone; // and we have no good pixels to fill with |
718 | |
766 | |
719 | composer cc (this, new rxvt_img (s, alpha ? find_alpha_format_for (s->dpy, format) : format, |
767 | composer cc (this, new rxvt_img (s, alpha ? find_alpha_format_for (s->dpy, format) : format, |
720 | 0, 0, w, h, repeat)); |
768 | 0, 0, w, h, repeat)); |
721 | |
769 | |
722 | if (alpha) |
770 | if (repeat == RepeatNone) |
723 | { |
771 | { |
724 | XRenderColor rc = { 0, 0, 0, 0 }; |
772 | XRenderColor rc = { 0, 0, 0, 0 }; |
725 | XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
773 | XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
726 | XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
774 | XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
727 | } |
775 | } |
… | |
… | |
866 | |
914 | |
867 | return cc; |
915 | return cc; |
868 | } |
916 | } |
869 | |
917 | |
870 | rxvt_img * |
918 | rxvt_img * |
|
|
919 | rxvt_img::shade (nv factor, rgba c) |
|
|
920 | { |
|
|
921 | clamp_it (factor, -1., 1.); |
|
|
922 | factor++; |
|
|
923 | |
|
|
924 | if (factor > 1) |
|
|
925 | { |
|
|
926 | c.r = c.r * (2 - factor); |
|
|
927 | c.g = c.g * (2 - factor); |
|
|
928 | c.b = c.b * (2 - factor); |
|
|
929 | } |
|
|
930 | else |
|
|
931 | { |
|
|
932 | c.r = c.r * factor; |
|
|
933 | c.g = c.g * factor; |
|
|
934 | c.b = c.b * factor; |
|
|
935 | } |
|
|
936 | |
|
|
937 | rxvt_img *img = this->tint (c); |
|
|
938 | |
|
|
939 | if (factor > 1) |
|
|
940 | { |
|
|
941 | c.a = 0xffff; |
|
|
942 | c.r = |
|
|
943 | c.g = |
|
|
944 | c.b = 0xffff * (factor - 1); |
|
|
945 | |
|
|
946 | img->brightness (c.r, c.g, c.b, c.a); |
|
|
947 | } |
|
|
948 | |
|
|
949 | return img; |
|
|
950 | } |
|
|
951 | |
|
|
952 | rxvt_img * |
871 | rxvt_img::filter (const char *name, int nparams, nv *params) |
953 | rxvt_img::filter (const char *name, int nparams, nv *params) |
872 | { |
954 | { |
873 | composer cc (this); |
955 | composer cc (this); |
874 | |
956 | |
875 | XFixed *xparams = rxvt_temp_buf<XFixed> (nparams); |
957 | XFixed *xparams = rxvt_temp_buf<XFixed> (nparams); |