ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.114
Committed: Wed Jan 10 06:06:16 2024 UTC (4 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.113: +5 -2 lines
Log Message:
Fix memory leak in rxvt_img::muladd

File Contents

# Content
1 /*----------------------------------------------------------------------*
2 * File: rxvtimg.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2012 Marc Lehmann <schmorp@schmorp.de>
7 * Copyright (c) 2012 Emanuele Giaquinta <e.giaquinta@glauco.it>
8 *
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
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *---------------------------------------------------------------------*/
23
24 #include <string.h>
25 #include <math.h>
26 #include "../config.h"
27 #include "rxvt.h"
28
29 #if HAVE_IMG
30
31 typedef rxvt_img::nv nv;
32
33 namespace
34 {
35 struct mat3x3
36 {
37 nv v[3][3];
38
39 mat3x3 ()
40 {
41 }
42
43 mat3x3 (const nv *matrix)
44 {
45 memcpy (v, matrix, sizeof (v));
46 }
47
48 mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33)
49 {
50 v[0][0] = v11; v[0][1] = v12; v[0][2] = v13;
51 v[1][0] = v21; v[1][1] = v22; v[1][2] = v23;
52 v[2][0] = v31; v[2][1] = v32; v[2][2] = v33;
53 }
54
55 mat3x3 inverse ();
56
57 nv *operator [](int i) { return &v[i][0]; }
58 const nv *operator [](int i) const { return &v[i][0]; }
59
60 operator const nv * () const { return &v[0][0]; }
61 operator nv * () { return &v[0][0]; }
62
63 // quite inefficient, hopefully gcc pulls the w calc out of any loops
64 nv apply1 (int i, nv x, nv y)
65 {
66 mat3x3 &m = *this;
67
68 nv v = m[i][0] * x + m[i][1] * y + m[i][2];
69 nv w = m[2][0] * x + m[2][1] * y + m[2][2];
70
71 return v * (1. / w);
72 }
73
74 static mat3x3 translate (nv x, nv y);
75 static mat3x3 scale (nv s, nv t);
76 static mat3x3 rotate (nv phi);
77 };
78
79 mat3x3
80 mat3x3::inverse ()
81 {
82 mat3x3 &m = *this;
83 mat3x3 inv;
84
85 nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2];
86 nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1];
87 nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2];
88
89 nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2);
90
91 inv[0][0] = invdet * s0;
92 inv[0][1] = invdet * s1;
93 inv[0][2] = invdet * s2;
94
95 inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]);
96 inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]);
97 inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]);
98
99 inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]);
100 inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]);
101 inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]);
102
103 return inv;
104 }
105
106 static mat3x3
107 operator *(const mat3x3 &a, const mat3x3 &b)
108 {
109 mat3x3 r;
110
111 for (int i = 0; i < 3; ++i)
112 for (int j = 0; j < 3; ++j)
113 r[i][j] = a[i][0] * b[0][j]
114 + a[i][1] * b[1][j]
115 + a[i][2] * b[2][j];
116
117 return r;
118 }
119
120 mat3x3
121 mat3x3::translate (nv x, nv y)
122 {
123 return mat3x3 (
124 1, 0, x,
125 0, 1, y,
126 0, 0, 1
127 );
128 }
129
130 mat3x3
131 mat3x3::scale (nv s, nv t)
132 {
133 return mat3x3 (
134 s, 0, 0,
135 0, t, 0,
136 0, 0, 1
137 );
138 }
139
140 // clockwise
141 mat3x3
142 mat3x3::rotate (nv phi)
143 {
144 nv s = sin (phi);
145 nv c = cos (phi);
146
147 return mat3x3 (
148 c, -s, 0,
149 s, c, 0,
150 0, 0, 1
151 );
152 }
153
154 struct composer
155 {
156 rxvt_img *srcimg, *dstimg;
157 Picture src, dst, msk;
158 Display *dpy;
159
160 ecb_noinline
161 composer (rxvt_img *srcimg, rxvt_img *dstimg = 0)
162 : srcimg (srcimg), dstimg (dstimg), msk (0)
163 {
164 if (!this->dstimg)
165 this->dstimg = srcimg->new_empty ();
166 else if (!this->dstimg->pm) // somewhat unsatisfying
167 this->dstimg->alloc ();
168
169 dpy = srcimg->d->dpy;
170 src = srcimg->picture ();
171 dst = this->dstimg->picture ();
172 }
173
174 ecb_noinline
175 void mask (bool rgb = true, int w = 1, int h = 1)
176 {
177 Pixmap pixmap = XCreatePixmap (dpy, srcimg->pm, w, h, rgb ? 32 : 8);
178
179 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, rgb ? PictStandardARGB32 : PictStandardA8);
180 XRenderPictureAttributes pa;
181 pa.repeat = RepeatNormal;
182 pa.component_alpha = rgb;
183 msk = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
184
185 XFreePixmap (dpy, pixmap);
186
187 ecb_assume (msk);
188 }
189
190 // CreateSolidFill creates a very very very weird picture
191 void mask (const rgba &c)
192 {
193 // the casts are needed in C++11 (see 8.5.1)
194 XRenderColor rc = {
195 (unsigned short)(c.r * c.a / 65535),
196 (unsigned short)(c.g * c.a / 65535),
197 (unsigned short)(c.b * c.a / 65535),
198 c.a
199 };
200 msk = XRenderCreateSolidFill (dpy, &rc);
201 ecb_assume (msk);
202 }
203
204 void fill (const rgba &c)
205 {
206 XRenderColor rc = {
207 (unsigned short)(c.r * c.a / 65535),
208 (unsigned short)(c.g * c.a / 65535),
209 (unsigned short)(c.b * c.a / 65535),
210 c.a
211 };
212
213 XRenderFillRectangle (dpy, PictOpSrc, msk, &rc, 0, 0, 1, 1);
214 }
215
216 operator rxvt_img *()
217 {
218 return dstimg;
219 }
220
221 ecb_noinline
222 ~composer ()
223 {
224 XRenderFreePicture (dpy, src);
225 XRenderFreePicture (dpy, dst);
226 if (msk) XRenderFreePicture (dpy, msk);
227 }
228 };
229 }
230
231 static XRenderPictFormat *
232 find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
233 {
234 if (format->direct.alphaMask)
235 return format; // already has alpha
236
237 // try to find a suitable alpha format, one bit alpha is enough for our purposes
238 if (format->type == PictTypeDirect)
239 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
240 if (f->direct.alphaMask
241 && f->type == PictTypeDirect
242 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
243 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
244 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
245 return f;
246
247 // should be a very good fallback
248 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
249 }
250
251 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
252 : d(screen->display), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
253 pm(0), ref(0)
254 {
255 }
256
257 rxvt_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
263 rxvt_img::rxvt_img (const rxvt_img &img)
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)
265 {
266 ++ref->cnt;
267 }
268
269 rxvt_img *
270 rxvt_img::new_from_root (rxvt_screen *s)
271 {
272 Display *dpy = s->dpy;
273 unsigned int root_pm_w, root_pm_h;
274 Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa [XA_XROOTPMAP_ID]);
275 if (root_pixmap == None)
276 root_pixmap = s->display->get_pixmap_property (s->display->xa [XA_ESETROOT_PMAP_ID]);
277
278 if (root_pixmap == None)
279 return 0;
280
281 Window wdummy;
282 int idummy;
283 unsigned int udummy;
284
285 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy))
286 return 0;
287
288 rxvt_img *img = new rxvt_img (
289 s,
290 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
291 0,
292 0,
293 root_pm_w,
294 root_pm_h
295 );
296
297 img->pm = root_pixmap;
298 img->ref = new pixref (root_pm_w, root_pm_h);
299 img->ref->ours = false;
300
301 return img;
302 }
303
304 # if HAVE_PIXBUF
305
306 rxvt_img *
307 rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
308 {
309 Display *dpy = s->dpy;
310
311 int width = gdk_pixbuf_get_width (pb);
312 int height = gdk_pixbuf_get_height (pb);
313
314 if (width > 32767 || height > 32767) // well, we *could* upload in chunks
315 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
316
317 // since we require rgb24/argb32 formats from xrender we assume
318 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender
319
320 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
321
322 XImage xi;
323
324 xi.width = width;
325 xi.height = height;
326 xi.xoffset = 0;
327 xi.format = ZPixmap;
328 xi.byte_order = ImageByteOrder (dpy);
329 xi.bitmap_unit = 0; //XY only, unused
330 xi.bitmap_bit_order = 0; //XY only, unused
331 xi.bitmap_pad = BitmapPad (dpy);
332 xi.depth = 32;
333 xi.bytes_per_line = 0;
334 xi.bits_per_pixel = 32; //Z only
335 xi.red_mask = 0x00000000; //Z only, unused
336 xi.green_mask = 0x00000000; //Z only, unused
337 xi.blue_mask = 0x00000000; //Z only, unused
338 xi.obdata = 0; // probably unused
339
340 bool byte_order_mismatch = byte_order != xi.byte_order;
341
342 if (!XInitImage (&xi))
343 rxvt_fatal ("unable to initialise ximage, please report.\n");
344
345 if (height > INT_MAX / xi.bytes_per_line)
346 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
347
348 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
349
350 int rowstride = gdk_pixbuf_get_rowstride (pb);
351 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
352 unsigned char *row = gdk_pixbuf_get_pixels (pb);
353
354 char *line = xi.data;
355
356 for (int y = 0; y < height; y++)
357 {
358 unsigned char *src = row;
359 uint32_t *dst = (uint32_t *)line;
360
361 for (int x = 0; x < width; x++)
362 {
363 uint8_t r = *src++;
364 uint8_t g = *src++;
365 uint8_t b = *src++;
366 uint8_t a = *src;
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
376 uint32_t v = (a << 24) | (r << 16) | (g << 8) | b;
377
378 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
379 v = ecb_bswap32 (v);
380
381 *dst++ = v;
382 }
383
384 row += rowstride;
385 line += xi.bytes_per_line;
386 }
387
388 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
389 img->alloc ();
390
391 GC gc = XCreateGC (dpy, img->pm, 0, 0);
392 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
393 XFreeGC (dpy, gc);
394
395 free (xi.data);
396
397 return img;
398 }
399
400 rxvt_img *
401 rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
402 {
403 GError *err = 0;
404 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
405
406 if (!pb)
407 try
408 {
409 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
410 }
411 catch (...)
412 {
413 g_error_free (err);
414 throw;
415 }
416
417 rxvt_img *img = new_from_pixbuf (s, pb);
418
419 g_object_unref (pb);
420
421 return img;
422 }
423
424 # endif
425
426 void
427 rxvt_img::destroy ()
428 {
429 if (--ref->cnt)
430 return;
431
432 if (pm && ref->ours)
433 XFreePixmap (d->dpy, pm);
434
435 delete ref;
436 }
437
438 rxvt_img::~rxvt_img ()
439 {
440 destroy ();
441 }
442
443 void
444 rxvt_img::alloc ()
445 {
446 pm = XCreatePixmap (d->dpy, d->root, w, h, format->depth);
447 ref = new pixref (w, h);
448 }
449
450 rxvt_img *
451 rxvt_img::new_empty ()
452 {
453 rxvt_img *img = new rxvt_img (d, format, x, y, w, h, repeat);
454 img->alloc ();
455
456 return img;
457 }
458
459 Picture
460 rxvt_img::picture ()
461 {
462 Display *dpy = d->dpy;
463
464 XRenderPictureAttributes pa;
465 pa.repeat = repeat;
466 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
467
468 return pic;
469 }
470
471 void
472 rxvt_img::unshare ()
473 {
474 if (ref->cnt == 1 && ref->ours)
475 return;
476
477 Pixmap pm2 = XCreatePixmap (d->dpy, d->root, ref->w, ref->h, format->depth);
478 GC gc = XCreateGC (d->dpy, pm, 0, 0);
479 XCopyArea (d->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
480 XFreeGC (d->dpy, gc);
481
482 destroy ();
483
484 pm = pm2;
485 ref = new pixref (ref->w, ref->h);
486 }
487
488 void
489 rxvt_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
499 void
500 rxvt_img::fill (const rgba &c)
501 {
502 fill (c, 0, 0, w, h);
503 }
504
505 void
506 rxvt_img::add_alpha ()
507 {
508 if (format->direct.alphaMask)
509 return;
510
511 composer cc (this, new rxvt_img (d, find_alpha_format_for (d->dpy, format), x, y, w, h, repeat));
512
513 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
514
515 rxvt_img *img = cc;
516
517 ::swap (img->ref, ref);
518 ::swap (img->pm , pm );
519
520 delete img;
521 }
522
523 static void
524 get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params)
525 {
526 nv sigma = radius / 2.0;
527 nv scale = sqrt (2.0 * M_PI) * sigma;
528 nv sum = 0.0;
529
530 for (int i = 0; i < width; i++)
531 {
532 nv x = i - width / 2;
533 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
534 sum += kernel[i];
535 }
536
537 params[0] = XDoubleToFixed (width);
538 params[1] = XDoubleToFixed (1);
539
540 for (int i = 0; i < width; i++)
541 params[i+2] = XDoubleToFixed (kernel[i] / sum);
542 }
543
544 rxvt_img *
545 rxvt_img::blur (int rh, int rv)
546 {
547 if (!(d->flags & DISPLAY_HAS_RENDER_CONV))
548 return clone ();
549
550 Display *dpy = d->dpy;
551 int size = max (rh, rv) * 2 + 1;
552 nv *kernel = (nv *)malloc (size * sizeof (nv));
553 XFixed *params = rxvt_temp_buf<XFixed> (size + 2);
554 rxvt_img *img = new_empty ();
555
556 XRenderPictureAttributes pa;
557 pa.repeat = RepeatPad;
558 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
559 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
560
561 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
562 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
563 XFreePixmap (dpy, tmp_pm);
564
565 if (kernel && params)
566 {
567 size = rh * 2 + 1;
568 get_gaussian_kernel (rh, size, kernel, params);
569
570 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
571 XRenderComposite (dpy,
572 PictOpSrc,
573 src,
574 None,
575 tmp,
576 0, 0,
577 0, 0,
578 0, 0,
579 w, h);
580
581 size = rv * 2 + 1;
582 get_gaussian_kernel (rv, size, kernel, params);
583 ::swap (params[0], params[1]);
584
585 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
586 XRenderComposite (dpy,
587 PictOpSrc,
588 tmp,
589 None,
590 dst,
591 0, 0,
592 0, 0,
593 0, 0,
594 w, h);
595 }
596
597 free (kernel);
598
599 XRenderFreePicture (dpy, src);
600 XRenderFreePicture (dpy, dst);
601 XRenderFreePicture (dpy, tmp);
602
603 return img;
604 }
605
606 rxvt_img *
607 rxvt_img::muladd (nv mul, nv add)
608 {
609 // STEP 1: double the image width, fill all odd columns with white (==1)
610
611 rxvt_img *img = new rxvt_img (d, format, 0, 0, w * 2, h, repeat);
612 composer cc (this, img);
613
614 // why the hell does XRenderSetPictureTransform want a writable matrix :(
615 // that keeps us from just static const'ing this matrix.
616 XTransform h_double = {
617 0x08000, 0, 0,
618 0, 0x10000, 0,
619 0, 0, 0x10000
620 };
621
622 XRenderSetPictureFilter (cc.dpy, cc.src, "nearest", 0, 0);
623 XRenderSetPictureTransform (cc.dpy, cc.src, &h_double);
624 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
625
626 cc.mask (false, 2, 1);
627
628 static const XRenderColor c0 = { 0, 0, 0, 0 };
629 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c0, 0, 0, 1, 1);
630 static const XRenderColor c1 = { 65535, 65535, 65535, 65535 };
631 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &c1, 1, 0, 1, 1);
632
633 Picture white = XRenderCreateSolidFill (cc.dpy, &c1);
634
635 XRenderComposite (cc.dpy, PictOpOver, white, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
636
637 XRenderFreePicture (cc.dpy, white);
638
639 // STEP 2: convolve the image with a 3x1 filter
640 // a 2x1 filter would obviously suffice, but given the total lack of specification
641 // for xrender, I expect different xrender implementations to randomly diverge.
642 // we also halve the image, and hope for the best (again, for lack of specs).
643 composer cc2 (img);
644
645 XFixed kernel [] = {
646 XDoubleToFixed (3), XDoubleToFixed (1),
647 XDoubleToFixed (0), XDoubleToFixed (mul), XDoubleToFixed (add)
648 };
649
650 XTransform h_halve = {
651 0x20000, 0, 0,
652 0, 0x10000, 0,
653 0, 0, 0x10000
654 };
655
656 XRenderSetPictureFilter (cc.dpy, cc2.src, "nearest", 0, 0);
657 XRenderSetPictureTransform (cc.dpy, cc2.src, &h_halve);
658 XRenderSetPictureFilter (cc.dpy, cc2.src, FilterConvolution, kernel, ecb_array_length (kernel));
659
660 XRenderComposite (cc.dpy, PictOpSrc, cc2.src, None, cc2.dst, 0, 0, 0, 0, 0, 0, w * 2, h);
661
662 delete img;
663
664 return cc2;
665 }
666
667 ecb_noinline static void
668 extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
669 {
670 int32_t x = clamp (c, cl0, cl1);
671 c -= x;
672 xc = x;
673 }
674
675 ecb_noinline static bool
676 extract (int32_t cl0, int32_t cl1, int32_t &r, int32_t &g, int32_t &b, int32_t &a, unsigned short &xr, unsigned short &xg, unsigned short &xb, unsigned short &xa)
677 {
678 extract (cl0, cl1, r, xr);
679 extract (cl0, cl1, g, xg);
680 extract (cl0, cl1, b, xb);
681 extract (cl0, cl1, a, xa);
682
683 return xr | xg | xb | xa;
684 }
685
686 void
687 rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
688 {
689 unshare ();
690
691 Display *dpy = d->dpy;
692 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
693
694 // loop should not be needed for brightness, as only -1..1 makes sense
695 //while (r | g | b | a)
696 {
697 XRenderColor mask_c;
698
699 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
700 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
701
702 if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
703 {
704 XRenderColor mask_w = { 65535, 65535, 65535, 65535 };
705 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
706 mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing
707 mask_c.green = -mask_c.green;
708 mask_c.blue = -mask_c.blue;
709 mask_c.alpha = -mask_c.alpha;
710 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
711 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
712 }
713 }
714
715 XRenderFreePicture (dpy, dst);
716 }
717
718 void
719 rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a)
720 {
721 if (r < 0 || g < 0 || b < 0 || a < 0)
722 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
723
724 // premultiply (yeah, these are not exact, sue me or fix it)
725 r = (r * (a >> 8)) >> 8;
726 g = (g * (a >> 8)) >> 8;
727 b = (b * (a >> 8)) >> 8;
728
729 composer cc (this);
730 rxvt_img *img = cc;
731 img->fill (rgba (0, 0, 0, 0));
732
733 cc.mask (true);
734
735 //TODO: this operator does not yet implement some useful contrast
736 while (r | g | b | a)
737 {
738 XRenderColor mask_c;
739
740 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
741 {
742 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &mask_c, 0, 0, 1, 1);
743 XRenderComposite (cc.dpy, PictOpAdd, cc.src, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
744 }
745 }
746
747 ::swap (img->ref, ref);
748 ::swap (img->pm , pm );
749
750 delete img;
751 }
752
753 void
754 rxvt_img::draw (rxvt_img *img, int op, nv mask)
755 {
756 unshare ();
757
758 composer cc (img, this);
759
760 if (mask != 1.)
761 cc.mask (rgba (0, 0, 0, float_to_component (mask)));
762
763 XRenderComposite (cc.dpy, op, cc.src, cc.msk, cc.dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h);
764 }
765
766 rxvt_img *
767 rxvt_img::clone ()
768 {
769 return new rxvt_img (*this);
770 }
771
772 rxvt_img *
773 rxvt_img::reify ()
774 {
775 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
776 return clone ();
777
778 // add an alpha channel if...
779 bool alpha = !format->direct.alphaMask // pixmap has none yet
780 && (x || y) // we need one because of non-zero offset
781 && repeat == RepeatNone; // and we have no good pixels to fill with
782
783 composer cc (this, new rxvt_img (d, alpha ? find_alpha_format_for (d->dpy, format) : format,
784 0, 0, w, h, repeat));
785
786 if (repeat == RepeatNone)
787 {
788 XRenderColor rc = { 0, 0, 0, 0 };
789 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
790 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
791 }
792 else
793 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, -x, -y, 0, 0, 0, 0, w, h);
794
795 return cc;
796 }
797
798 rxvt_img *
799 rxvt_img::sub_rect (int x, int y, int width, int height)
800 {
801 rxvt_img *img = clone ();
802
803 img->x -= x;
804 img->y -= y;
805
806 if (w != width || h != height)
807 {
808 img->w = width;
809 img->h = height;
810
811 rxvt_img *img2 = img->reify ();
812 delete img;
813 img = img2;
814 }
815
816 return img;
817 }
818
819 rxvt_img *
820 rxvt_img::transform (const nv matrix[3][3])
821 {
822 return transform (mat3x3 (&matrix[0][0]));
823 }
824
825 rxvt_img *
826 rxvt_img::transform (const nv *matrix)
827 {
828 mat3x3 m (matrix);
829
830 // calculate new pixel bounding box coordinates
831 nv rmin[2], rmax[2];
832
833 for (int i = 0; i < 2; ++i)
834 {
835 nv v;
836
837 v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v;
838 v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v);
839 v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
840 v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
841 }
842
843 float sx = rmin [0] - x;
844 float sy = rmin [1] - y;
845
846 // TODO: adjust matrix for subpixel accuracy
847 int nx = floor (rmin [0]);
848 int ny = floor (rmin [1]);
849
850 int new_width = ceil (rmax [0] - rmin [0]);
851 int new_height = ceil (rmax [1] - rmin [1]);
852
853 mat3x3 inv = (mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y)).inverse ();
854
855 composer cc (this, new rxvt_img (d, format, nx, ny, new_width, new_height, repeat));
856
857 XTransform xfrm;
858
859 for (int i = 0; i < 3; ++i)
860 for (int j = 0; j < 3; ++j)
861 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
862
863 XRenderSetPictureFilter (cc.dpy, cc.src, "good", 0, 0);
864 XRenderSetPictureTransform (cc.dpy, cc.src, &xfrm);
865 XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
866
867 return cc;
868 }
869
870 rxvt_img *
871 rxvt_img::scale (int new_width, int new_height)
872 {
873 if (w == new_width && h == new_height)
874 return clone ();
875
876 int old_repeat_mode = repeat;
877 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
878
879 rxvt_img *img = transform (mat3x3::scale (new_width / (nv)w, new_height / (nv)h));
880
881 repeat = old_repeat_mode;
882 img->repeat = repeat;
883
884 return img;
885 }
886
887 rxvt_img *
888 rxvt_img::rotate (int cx, int cy, nv phi)
889 {
890 move (-cx, -cy);
891 rxvt_img *img = transform (mat3x3::rotate (phi));
892 move ( cx, cy);
893 img->move (cx, cy);
894
895 return img;
896 }
897
898 rxvt_img *
899 rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
900 {
901 if (new_format == format)
902 return clone ();
903
904 composer cc (this, new rxvt_img (d, new_format, x, y, w, h, repeat));
905
906 int op = PictOpSrc;
907
908 if (format->direct.alphaMask && !new_format->direct.alphaMask)
909 {
910 // does it have to be that complicated
911 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
912 XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);
913
914 op = PictOpOver;
915 }
916
917 XRenderComposite (cc.dpy, op, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
918
919 return cc;
920 }
921
922 rxvt_img *
923 rxvt_img::tint (const rgba &c)
924 {
925 composer cc (this);
926 cc.mask (true);
927 cc.fill (c);
928
929 XRenderComposite (cc.dpy, PictOpSrc, cc.src, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
930
931 return cc;
932 }
933
934 rxvt_img *
935 rxvt_img::shade (nv factor, rgba c)
936 {
937 clamp_it (factor, -1., 1.);
938 factor++;
939
940 if (factor > 1)
941 {
942 c.r = c.r * (2 - factor);
943 c.g = c.g * (2 - factor);
944 c.b = c.b * (2 - factor);
945 }
946 else
947 {
948 c.r = c.r * factor;
949 c.g = c.g * factor;
950 c.b = c.b * factor;
951 }
952
953 rxvt_img *img = this->tint (c);
954
955 if (factor > 1)
956 {
957 c.a = 0xffff;
958 c.r =
959 c.g =
960 c.b = 0xffff * (factor - 1);
961
962 img->brightness (c.r, c.g, c.b, c.a);
963 }
964
965 return img;
966 }
967
968 rxvt_img *
969 rxvt_img::filter (const char *name, int nparams, nv *params)
970 {
971 composer cc (this);
972
973 XFixed *xparams = rxvt_temp_buf<XFixed> (nparams);
974
975 for (int i = 0; i < nparams; ++i)
976 xparams [i] = XDoubleToFixed (params [i]);
977
978 XRenderSetPictureFilter (cc.dpy, cc.src, name, xparams, nparams);
979
980 XRenderComposite (cc.dpy, PictOpSrc, cc.src, 0, cc.dst, 0, 0, 0, 0, 0, 0, w, h);
981
982 return cc;
983 }
984
985 #endif
986