1 |
#include <math.h> |
2 |
#include "../config.h" |
3 |
#include "rxvt.h" |
4 |
|
5 |
#if HAVE_IMG |
6 |
|
7 |
#if 0 |
8 |
struct pict |
9 |
{ |
10 |
Display *dpy; |
11 |
Picture pic; |
12 |
|
13 |
operator Picture () const |
14 |
{ |
15 |
return pic; |
16 |
} |
17 |
|
18 |
pict () |
19 |
: pic (0) |
20 |
{ |
21 |
} |
22 |
|
23 |
pict (rxvt_img *img, XRenderPictFormat *format = 0) |
24 |
: dpy (img->s->display->dpy) |
25 |
{ |
26 |
XRenderPictureAttributes pa; |
27 |
pa.repeat = img->repeat; |
28 |
pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); |
29 |
} |
30 |
|
31 |
~pict () |
32 |
{ |
33 |
if (pic) |
34 |
XRenderFreePicture (dpy, pic); |
35 |
} |
36 |
}; |
37 |
#endif |
38 |
|
39 |
static XRenderPictFormat * |
40 |
find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
41 |
{ |
42 |
if (format->direct.alphaMask) |
43 |
return format; // already has alpha |
44 |
|
45 |
// try to find a suitable alpha format, one bit alpha is enough for our purposes |
46 |
if (format->type == PictTypeDirect) |
47 |
for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
48 |
if (f->direct.alphaMask |
49 |
&& f->type == PictTypeDirect |
50 |
&& ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
51 |
&& ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
52 |
&& ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
53 |
return f; |
54 |
|
55 |
// should be a very good fallback |
56 |
return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
57 |
} |
58 |
|
59 |
rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
60 |
: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
61 |
pm(0), ref(0) |
62 |
{ |
63 |
} |
64 |
|
65 |
rxvt_img::rxvt_img (const rxvt_img &img) |
66 |
: 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) |
67 |
{ |
68 |
++ref->cnt; |
69 |
} |
70 |
|
71 |
rxvt_img * |
72 |
rxvt_img::new_from_root (rxvt_screen *s) |
73 |
{ |
74 |
Display *dpy = s->display->dpy; |
75 |
unsigned int root_pm_w, root_pm_h; |
76 |
Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_XROOTPMAP_ID]); |
77 |
if (root_pixmap == None) |
78 |
root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_ESETROOT_PMAP_ID]); |
79 |
|
80 |
if (root_pixmap == None) |
81 |
return 0; |
82 |
|
83 |
Window wdummy; |
84 |
int idummy; |
85 |
unsigned int udummy; |
86 |
|
87 |
if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy)) |
88 |
return 0; |
89 |
|
90 |
rxvt_img *img = new rxvt_img ( |
91 |
s, |
92 |
XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
93 |
0, |
94 |
0, |
95 |
root_pm_w, |
96 |
root_pm_h |
97 |
); |
98 |
|
99 |
img->pm = root_pixmap; |
100 |
img->ref = new pixref (root_pm_w, root_pm_h); |
101 |
img->ref->ours = false; |
102 |
|
103 |
return img; |
104 |
} |
105 |
|
106 |
# if HAVE_PIXBUF |
107 |
|
108 |
rxvt_img * |
109 |
rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
110 |
{ |
111 |
Display *dpy = s->display->dpy; |
112 |
|
113 |
int width = gdk_pixbuf_get_width (pb); |
114 |
int height = gdk_pixbuf_get_height (pb); |
115 |
|
116 |
if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
117 |
rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
118 |
|
119 |
// since we require rgb24/argb32 formats from xrender we assume |
120 |
// that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
121 |
|
122 |
int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
123 |
|
124 |
XImage xi; |
125 |
|
126 |
xi.width = width; |
127 |
xi.height = height; |
128 |
xi.xoffset = 0; |
129 |
xi.format = ZPixmap; |
130 |
xi.byte_order = ImageByteOrder (dpy); |
131 |
xi.bitmap_unit = 0; //XY only, unused |
132 |
xi.bitmap_bit_order = 0; //XY only, unused |
133 |
xi.bitmap_pad = BitmapPad (dpy); |
134 |
xi.depth = 32; |
135 |
xi.bytes_per_line = 0; |
136 |
xi.bits_per_pixel = 32; //Z only |
137 |
xi.red_mask = 0x00000000; //Z only, unused |
138 |
xi.green_mask = 0x00000000; //Z only, unused |
139 |
xi.blue_mask = 0x00000000; //Z only, unused |
140 |
xi.obdata = 0; // probably unused |
141 |
|
142 |
bool byte_order_mismatch = byte_order != xi.byte_order; |
143 |
|
144 |
if (!XInitImage (&xi)) |
145 |
rxvt_fatal ("unable to initialise ximage, please report.\n"); |
146 |
|
147 |
if (height > INT_MAX / xi.bytes_per_line) |
148 |
rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
149 |
|
150 |
xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
151 |
|
152 |
int rowstride = gdk_pixbuf_get_rowstride (pb); |
153 |
bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb); |
154 |
unsigned char *row = gdk_pixbuf_get_pixels (pb); |
155 |
|
156 |
char *line = xi.data; |
157 |
|
158 |
for (int y = 0; y < height; y++) |
159 |
{ |
160 |
unsigned char *src = row; |
161 |
uint32_t *dst = (uint32_t *)line; |
162 |
|
163 |
if (!pb_has_alpha) |
164 |
for (int x = 0; x < width; x++) |
165 |
{ |
166 |
uint8_t r = *src++; |
167 |
uint8_t g = *src++; |
168 |
uint8_t b = *src++; |
169 |
|
170 |
uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; |
171 |
|
172 |
if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
173 |
v = ecb_bswap32 (v); |
174 |
|
175 |
*dst++ = v; |
176 |
} |
177 |
else |
178 |
for (int x = 0; x < width; x++) |
179 |
{ |
180 |
uint32_t v = *(uint32_t *)src; src += 4; |
181 |
|
182 |
if (ecb_big_endian ()) |
183 |
v = ecb_bswap32 (v); |
184 |
|
185 |
v = ecb_rotl32 (v, 8); // abgr to bgra |
186 |
|
187 |
if (!byte_order_mismatch) |
188 |
v = ecb_bswap32 (v); |
189 |
|
190 |
*dst++ = v; |
191 |
} |
192 |
|
193 |
row += rowstride; |
194 |
line += xi.bytes_per_line; |
195 |
} |
196 |
|
197 |
rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); |
198 |
img->alloc (); |
199 |
|
200 |
GC gc = XCreateGC (dpy, img->pm, 0, 0); |
201 |
XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
202 |
XFreeGC (dpy, gc); |
203 |
|
204 |
free (xi.data); |
205 |
|
206 |
return img; |
207 |
} |
208 |
|
209 |
rxvt_img * |
210 |
rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
211 |
{ |
212 |
GError *err = 0; |
213 |
GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
214 |
|
215 |
if (!pb) |
216 |
rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
217 |
|
218 |
rxvt_img *img = new_from_pixbuf (s, pb); |
219 |
|
220 |
g_object_unref (pb); |
221 |
|
222 |
return img; |
223 |
} |
224 |
|
225 |
# endif |
226 |
|
227 |
void |
228 |
rxvt_img::destroy () |
229 |
{ |
230 |
if (--ref->cnt) |
231 |
return; |
232 |
|
233 |
if (pm && ref->ours) |
234 |
XFreePixmap (s->display->dpy, pm); |
235 |
|
236 |
delete ref; |
237 |
} |
238 |
|
239 |
rxvt_img::~rxvt_img () |
240 |
{ |
241 |
destroy (); |
242 |
} |
243 |
|
244 |
void |
245 |
rxvt_img::alloc () |
246 |
{ |
247 |
pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
248 |
ref = new pixref (w, h); |
249 |
} |
250 |
|
251 |
Picture |
252 |
rxvt_img::picture () |
253 |
{ |
254 |
Display *dpy = s->display->dpy; |
255 |
|
256 |
XRenderPictureAttributes pa; |
257 |
pa.repeat = repeat; |
258 |
Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
259 |
|
260 |
return pic; |
261 |
} |
262 |
|
263 |
void |
264 |
rxvt_img::unshare () |
265 |
{ |
266 |
if (ref->cnt == 1 && ref->ours) |
267 |
return; |
268 |
|
269 |
Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
270 |
GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
271 |
XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
272 |
XFreeGC (s->display->dpy, gc); |
273 |
|
274 |
destroy (); |
275 |
|
276 |
pm = pm2; |
277 |
ref = new pixref (ref->w, ref->h); |
278 |
} |
279 |
|
280 |
void |
281 |
rxvt_img::fill (const rgba &c) |
282 |
{ |
283 |
XRenderColor rc = { c.r, c.g, c.b, c.a }; |
284 |
|
285 |
Display *dpy = s->display->dpy; |
286 |
Picture src = picture (); |
287 |
XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
288 |
XRenderFreePicture (dpy, src); |
289 |
} |
290 |
|
291 |
void |
292 |
rxvt_img::add_alpha () |
293 |
{ |
294 |
if (format->direct.alphaMask) |
295 |
return; |
296 |
|
297 |
Display *dpy = s->display->dpy; |
298 |
|
299 |
rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
300 |
img->alloc (); |
301 |
|
302 |
Picture src = picture (); |
303 |
Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
304 |
|
305 |
XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
306 |
|
307 |
XRenderFreePicture (dpy, src); |
308 |
XRenderFreePicture (dpy, dst); |
309 |
|
310 |
::swap (img->ref, ref); |
311 |
::swap (img->pm , pm ); |
312 |
|
313 |
delete img; |
314 |
} |
315 |
|
316 |
static void |
317 |
get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) |
318 |
{ |
319 |
rxvt_img::nv sigma = radius / 2.0; |
320 |
rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; |
321 |
rxvt_img::nv sum = 0.0; |
322 |
|
323 |
for (int i = 0; i < width; i++) |
324 |
{ |
325 |
rxvt_img::nv x = i - width / 2; |
326 |
kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
327 |
sum += kernel[i]; |
328 |
} |
329 |
|
330 |
params[0] = XDoubleToFixed (width); |
331 |
params[1] = XDoubleToFixed (1); |
332 |
|
333 |
for (int i = 0; i < width; i++) |
334 |
params[i+2] = XDoubleToFixed (kernel[i] / sum); |
335 |
} |
336 |
|
337 |
rxvt_img * |
338 |
rxvt_img::blur (int rh, int rv) |
339 |
{ |
340 |
if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
341 |
return clone (); |
342 |
|
343 |
Display *dpy = s->display->dpy; |
344 |
int size = max (rh, rv) * 2 + 1; |
345 |
nv *kernel = (nv *)malloc (size * sizeof (nv)); |
346 |
XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
347 |
rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
348 |
img->alloc (); |
349 |
|
350 |
XRenderPictureAttributes pa; |
351 |
pa.repeat = RepeatPad; |
352 |
Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
353 |
Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
354 |
|
355 |
Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
356 |
Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
357 |
XFreePixmap (dpy, tmp_pm); |
358 |
|
359 |
if (kernel && params) |
360 |
{ |
361 |
size = rh * 2 + 1; |
362 |
get_gaussian_kernel (rh, size, kernel, params); |
363 |
|
364 |
XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
365 |
XRenderComposite (dpy, |
366 |
PictOpSrc, |
367 |
src, |
368 |
None, |
369 |
tmp, |
370 |
0, 0, |
371 |
0, 0, |
372 |
0, 0, |
373 |
w, h); |
374 |
|
375 |
size = rv * 2 + 1; |
376 |
get_gaussian_kernel (rv, size, kernel, params); |
377 |
::swap (params[0], params[1]); |
378 |
|
379 |
XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); |
380 |
XRenderComposite (dpy, |
381 |
PictOpSrc, |
382 |
tmp, |
383 |
None, |
384 |
dst, |
385 |
0, 0, |
386 |
0, 0, |
387 |
0, 0, |
388 |
w, h); |
389 |
} |
390 |
|
391 |
free (kernel); |
392 |
free (params); |
393 |
|
394 |
XRenderFreePicture (dpy, src); |
395 |
XRenderFreePicture (dpy, dst); |
396 |
XRenderFreePicture (dpy, tmp); |
397 |
|
398 |
return img; |
399 |
} |
400 |
|
401 |
static Picture |
402 |
create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
403 |
{ |
404 |
Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
405 |
|
406 |
XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
407 |
XRenderPictureAttributes pa; |
408 |
pa.repeat = RepeatNormal; |
409 |
pa.component_alpha = component_alpha; |
410 |
Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
411 |
|
412 |
XFreePixmap (dpy, pixmap); |
413 |
|
414 |
return mask; |
415 |
} |
416 |
|
417 |
static void |
418 |
extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) |
419 |
{ |
420 |
int32_t x = clamp (c, cl0, cl1); |
421 |
c -= x; |
422 |
xc = x; |
423 |
} |
424 |
|
425 |
static bool |
426 |
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) |
427 |
{ |
428 |
extract (cl0, cl1, r, xr); |
429 |
extract (cl0, cl1, g, xg); |
430 |
extract (cl0, cl1, b, xb); |
431 |
extract (cl0, cl1, a, xa); |
432 |
|
433 |
return xr | xg | xb | xa; |
434 |
} |
435 |
|
436 |
void |
437 |
rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) |
438 |
{ |
439 |
unshare (); |
440 |
|
441 |
Display *dpy = s->display->dpy; |
442 |
Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
443 |
|
444 |
// loop should not be needed for brightness, as only -1..1 makes sense |
445 |
//while (r | g | b | a) |
446 |
{ |
447 |
unsigned short xr, xg, xb, xa; |
448 |
XRenderColor mask_c; |
449 |
|
450 |
if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
451 |
XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
452 |
|
453 |
if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
454 |
{ |
455 |
XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
456 |
XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
457 |
mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing |
458 |
mask_c.green = -mask_c.green; |
459 |
mask_c.blue = -mask_c.blue; |
460 |
mask_c.alpha = -mask_c.alpha; |
461 |
XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
462 |
XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
463 |
} |
464 |
} |
465 |
|
466 |
XRenderFreePicture (dpy, dst); |
467 |
} |
468 |
|
469 |
void |
470 |
rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
471 |
{ |
472 |
if (r < 0 || g < 0 || b < 0 || a < 0) |
473 |
rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
474 |
|
475 |
rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
476 |
img->alloc (); |
477 |
img->fill (rgba (0, 0, 0, 0)); |
478 |
|
479 |
// premultiply (yeah, these are not exact, sue me or fix it) |
480 |
r = (r * (a >> 8)) >> 8; |
481 |
g = (g * (a >> 8)) >> 8; |
482 |
b = (b * (a >> 8)) >> 8; |
483 |
|
484 |
Display *dpy = s->display->dpy; |
485 |
|
486 |
Picture src = picture (); |
487 |
Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
488 |
Picture mul = create_xrender_mask (dpy, pm, True, True); |
489 |
|
490 |
//TODO: this operator does not yet implement some useful contrast |
491 |
while (r | g | b | a) |
492 |
{ |
493 |
unsigned short xr, xg, xb, xa; |
494 |
XRenderColor mask_c; |
495 |
|
496 |
if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
497 |
{ |
498 |
XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
499 |
XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); |
500 |
} |
501 |
} |
502 |
|
503 |
XRenderFreePicture (dpy, mul); |
504 |
XRenderFreePicture (dpy, dst); |
505 |
XRenderFreePicture (dpy, src); |
506 |
|
507 |
::swap (img->ref, ref); |
508 |
::swap (img->pm , pm ); |
509 |
|
510 |
delete img; |
511 |
} |
512 |
|
513 |
void |
514 |
rxvt_img::draw (rxvt_img *img, int op, nv mask) |
515 |
{ |
516 |
unshare (); |
517 |
|
518 |
Display *dpy = s->display->dpy; |
519 |
Picture src = img->picture (); |
520 |
Picture dst = picture (); |
521 |
Picture mask_p = 0; |
522 |
|
523 |
if (mask != 1.) |
524 |
{ |
525 |
mask_p = create_xrender_mask (dpy, img->pm, False, False); |
526 |
XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; |
527 |
XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
528 |
} |
529 |
|
530 |
XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); |
531 |
|
532 |
XRenderFreePicture (dpy, src); |
533 |
XRenderFreePicture (dpy, dst); |
534 |
|
535 |
if (mask_p) |
536 |
XRenderFreePicture (dpy, mask_p); |
537 |
} |
538 |
|
539 |
rxvt_img * |
540 |
rxvt_img::clone () |
541 |
{ |
542 |
return new rxvt_img (*this); |
543 |
} |
544 |
|
545 |
rxvt_img * |
546 |
rxvt_img::reify () |
547 |
{ |
548 |
if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
549 |
return clone (); |
550 |
|
551 |
Display *dpy = s->display->dpy; |
552 |
|
553 |
// add an alpha channel if... |
554 |
bool alpha = !format->direct.alphaMask // pixmap has none yet |
555 |
&& (x || y) // we need one because of non-zero offset |
556 |
&& repeat == RepeatNone; // and we have no good pixels to fill with |
557 |
|
558 |
rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
559 |
img->alloc (); |
560 |
|
561 |
Picture src = picture (); |
562 |
Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
563 |
|
564 |
if (alpha) |
565 |
{ |
566 |
XRenderColor rc = { 0, 0, 0, 0 }; |
567 |
XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
568 |
XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
569 |
} |
570 |
else |
571 |
XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); |
572 |
|
573 |
XRenderFreePicture (dpy, src); |
574 |
XRenderFreePicture (dpy, dst); |
575 |
|
576 |
return img; |
577 |
} |
578 |
|
579 |
rxvt_img * |
580 |
rxvt_img::sub_rect (int x, int y, int width, int height) |
581 |
{ |
582 |
rxvt_img *img = clone (); |
583 |
|
584 |
img->x -= x; |
585 |
img->y -= y; |
586 |
|
587 |
if (w != width || h != height) |
588 |
{ |
589 |
img->w = width; |
590 |
img->h = height; |
591 |
|
592 |
rxvt_img *img2 = img->reify (); |
593 |
delete img; |
594 |
img = img2; |
595 |
} |
596 |
|
597 |
return img; |
598 |
} |
599 |
|
600 |
static void |
601 |
mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) |
602 |
{ |
603 |
rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
604 |
rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; |
605 |
rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
606 |
|
607 |
rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); |
608 |
|
609 |
inv [0][0] = invdet * s0; |
610 |
inv [0][1] = invdet * s1; |
611 |
inv [0][2] = invdet * s2; |
612 |
|
613 |
inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]); |
614 |
inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]); |
615 |
inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]); |
616 |
|
617 |
inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); |
618 |
inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); |
619 |
inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); |
620 |
} |
621 |
|
622 |
static rxvt_img::nv |
623 |
mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) |
624 |
{ |
625 |
rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
626 |
rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; |
627 |
|
628 |
return v * (1. / w); |
629 |
} |
630 |
|
631 |
rxvt_img * |
632 |
rxvt_img::transform (nv matrix[3][3]) |
633 |
{ |
634 |
// calculate new pixel bounding box coordinates |
635 |
nv rmin[2], rmax[2]; |
636 |
|
637 |
for (int i = 0; i < 2; ++i) |
638 |
{ |
639 |
nv v; |
640 |
v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; |
641 |
v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
642 |
v = mat_apply (matrix, i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
643 |
v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
644 |
} |
645 |
|
646 |
int dx = floor (rmin [0]); |
647 |
int dy = floor (rmin [1]); |
648 |
|
649 |
int new_width = ceil (rmax [0] - dx); |
650 |
int new_height = ceil (rmax [1] - dy); |
651 |
|
652 |
nv inv[3][3]; |
653 |
mat_invert (matrix, inv); |
654 |
|
655 |
rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat); |
656 |
img->alloc (); |
657 |
|
658 |
Display *dpy = s->display->dpy; |
659 |
Picture src = picture (); |
660 |
Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
661 |
|
662 |
XTransform xfrm; |
663 |
|
664 |
for (int i = 0; i < 3; ++i) |
665 |
for (int j = 0; j < 3; ++j) |
666 |
xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
667 |
|
668 |
XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
669 |
XRenderSetPictureTransform (dpy, src, &xfrm); |
670 |
XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); |
671 |
|
672 |
XRenderFreePicture (dpy, src); |
673 |
XRenderFreePicture (dpy, dst); |
674 |
|
675 |
return img; |
676 |
} |
677 |
|
678 |
rxvt_img * |
679 |
rxvt_img::scale (int new_width, int new_height) |
680 |
{ |
681 |
if (w == new_width && h == new_height) |
682 |
return clone (); |
683 |
|
684 |
nv matrix[3][3] = { |
685 |
{ new_width / (nv)w, 0, 0 }, |
686 |
{ 0, new_height / (nv)h, 0 }, |
687 |
{ 0, 0, 1 } |
688 |
}; |
689 |
|
690 |
int old_repeat_mode = repeat; |
691 |
repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
692 |
|
693 |
rxvt_img *img = transform (matrix); |
694 |
|
695 |
repeat = old_repeat_mode; |
696 |
img->repeat = repeat; |
697 |
|
698 |
return img; |
699 |
} |
700 |
|
701 |
rxvt_img * |
702 |
rxvt_img::rotate (int cx, int cy, nv phi) |
703 |
{ |
704 |
nv s = sin (phi); |
705 |
nv c = cos (phi); |
706 |
|
707 |
nv matrix[3][3] = { |
708 |
{ c, -s, cx - c * cx + s * cy + 200 }, |
709 |
{ s, c, cy - s * cx - c * cy }, |
710 |
{ 0, 0, 1 } |
711 |
//{ c, -s, 0 }, |
712 |
//{ s, c, 0 }, |
713 |
//{ 0, 0, 1 } |
714 |
}; |
715 |
|
716 |
//move (-cx, -cy); |
717 |
rxvt_img *img = transform (matrix); |
718 |
//move ( cx, cy); |
719 |
//img->move (cx, cy); |
720 |
|
721 |
return img; |
722 |
} |
723 |
|
724 |
rxvt_img * |
725 |
rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
726 |
{ |
727 |
if (new_format == format) |
728 |
return clone (); |
729 |
|
730 |
rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
731 |
img->alloc (); |
732 |
|
733 |
Display *dpy = s->display->dpy; |
734 |
Picture src = picture (); |
735 |
Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
736 |
int op = PictOpSrc; |
737 |
|
738 |
if (format->direct.alphaMask && !new_format->direct.alphaMask) |
739 |
{ |
740 |
// does it have to be that complicated |
741 |
XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
742 |
XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
743 |
|
744 |
op = PictOpOver; |
745 |
} |
746 |
|
747 |
XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
748 |
|
749 |
XRenderFreePicture (dpy, src); |
750 |
XRenderFreePicture (dpy, dst); |
751 |
|
752 |
return img; |
753 |
} |
754 |
|
755 |
rxvt_img * |
756 |
rxvt_img::blend (rxvt_img *img, nv factor) |
757 |
{ |
758 |
rxvt_img *img2 = clone (); |
759 |
Display *dpy = s->display->dpy; |
760 |
Picture src = img->picture (); |
761 |
Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
762 |
Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
763 |
|
764 |
XRenderColor mask_c; |
765 |
|
766 |
mask_c.alpha = float_to_component (factor); |
767 |
mask_c.red = |
768 |
mask_c.green = |
769 |
mask_c.blue = 0; |
770 |
XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
771 |
|
772 |
XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h); |
773 |
|
774 |
XRenderFreePicture (dpy, src); |
775 |
XRenderFreePicture (dpy, dst); |
776 |
XRenderFreePicture (dpy, mask); |
777 |
|
778 |
return img2; |
779 |
} |
780 |
|
781 |
#endif |
782 |
|