|
|
1 | #include <string.h> |
1 | #include <math.h> |
2 | #include <math.h> |
2 | #include "../config.h" |
3 | #include "../config.h" |
3 | #include "rxvt.h" |
4 | #include "rxvt.h" |
4 | |
5 | |
5 | #if HAVE_IMG |
6 | #if HAVE_IMG |
|
|
7 | |
|
|
8 | typedef rxvt_img::nv nv; |
|
|
9 | |
|
|
10 | namespace |
|
|
11 | { |
|
|
12 | |
|
|
13 | struct mat3x3 |
|
|
14 | { |
|
|
15 | nv v[3][3]; |
|
|
16 | |
|
|
17 | mat3x3 () |
|
|
18 | { |
|
|
19 | } |
|
|
20 | |
|
|
21 | mat3x3 (nv matrix[3][3]) |
|
|
22 | { |
|
|
23 | memcpy (v, matrix, sizeof (v)); |
|
|
24 | } |
|
|
25 | |
|
|
26 | mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33) |
|
|
27 | { |
|
|
28 | v[0][0] = v11; v[0][1] = v12; v[0][2] = v13; |
|
|
29 | v[1][0] = v21; v[1][1] = v22; v[1][2] = v23; |
|
|
30 | v[2][0] = v31; v[2][1] = v32; v[2][2] = v33; |
|
|
31 | } |
|
|
32 | |
|
|
33 | mat3x3 invert (); |
|
|
34 | |
|
|
35 | nv *operator [](int i) { return &v[i][0]; } |
|
|
36 | const nv *operator [](int i) const { return &v[i][0]; } |
|
|
37 | |
|
|
38 | // quite inefficient, hopefully gcc pulls the w calc out of any loops |
|
|
39 | nv apply1 (int i, nv x, nv y) |
|
|
40 | { |
|
|
41 | mat3x3 &m = *this; |
|
|
42 | |
|
|
43 | nv v = m[i][0] * x + m[i][1] * y + m[i][2]; |
|
|
44 | nv w = m[2][0] * x + m[2][1] * y + m[2][2]; |
|
|
45 | |
|
|
46 | return v * (1. / w); |
|
|
47 | } |
|
|
48 | |
|
|
49 | static mat3x3 translate (nv x, nv y); |
|
|
50 | }; |
|
|
51 | |
|
|
52 | mat3x3 |
|
|
53 | mat3x3::invert () |
|
|
54 | { |
|
|
55 | mat3x3 &m = *this; |
|
|
56 | mat3x3 inv; |
|
|
57 | |
|
|
58 | nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2]; |
|
|
59 | nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1]; |
|
|
60 | nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2]; |
|
|
61 | |
|
|
62 | nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2); |
|
|
63 | |
|
|
64 | inv[0][0] = invdet * s0; |
|
|
65 | inv[0][1] = invdet * s1; |
|
|
66 | inv[0][2] = invdet * s2; |
|
|
67 | |
|
|
68 | inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]); |
|
|
69 | inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]); |
|
|
70 | inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]); |
|
|
71 | |
|
|
72 | inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]); |
|
|
73 | inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]); |
|
|
74 | inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]); |
|
|
75 | |
|
|
76 | return inv; |
|
|
77 | } |
|
|
78 | |
|
|
79 | static mat3x3 |
|
|
80 | operator *(const mat3x3 &a, const mat3x3 &b) |
|
|
81 | { |
|
|
82 | mat3x3 r; |
|
|
83 | |
|
|
84 | for (int i = 0; i < 3; ++i) |
|
|
85 | for (int j = 0; j < 3; ++j) |
|
|
86 | r[i][j] = a[i][0] * b[0][j] |
|
|
87 | + a[i][1] * b[1][j] |
|
|
88 | + a[i][2] * b[2][j]; |
|
|
89 | |
|
|
90 | return r; |
|
|
91 | } |
|
|
92 | |
|
|
93 | mat3x3 |
|
|
94 | mat3x3::translate (nv x, nv y) |
|
|
95 | { |
|
|
96 | return mat3x3 ( |
|
|
97 | 1, 0, x, |
|
|
98 | 0, 1, y, |
|
|
99 | 0, 0, 1 |
|
|
100 | ); |
|
|
101 | } |
|
|
102 | |
|
|
103 | } |
|
|
104 | |
|
|
105 | #if 0 |
|
|
106 | struct pict |
|
|
107 | { |
|
|
108 | Display *dpy; |
|
|
109 | Picture pic; |
|
|
110 | |
|
|
111 | operator Picture () const |
|
|
112 | { |
|
|
113 | return pic; |
|
|
114 | } |
|
|
115 | |
|
|
116 | pict () |
|
|
117 | : pic (0) |
|
|
118 | { |
|
|
119 | } |
|
|
120 | |
|
|
121 | pict (rxvt_img *img, XRenderPictFormat *format = 0) |
|
|
122 | : dpy (img->s->display->dpy) |
|
|
123 | { |
|
|
124 | XRenderPictureAttributes pa; |
|
|
125 | pa.repeat = img->repeat; |
|
|
126 | pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); |
|
|
127 | } |
|
|
128 | |
|
|
129 | ~pict () |
|
|
130 | { |
|
|
131 | if (pic) |
|
|
132 | XRenderFreePicture (dpy, pic); |
|
|
133 | } |
|
|
134 | }; |
|
|
135 | #endif |
|
|
136 | |
|
|
137 | static XRenderPictFormat * |
|
|
138 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
139 | { |
|
|
140 | if (format->direct.alphaMask) |
|
|
141 | return format; // already has alpha |
|
|
142 | |
|
|
143 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
144 | if (format->type == PictTypeDirect) |
|
|
145 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
146 | if (f->direct.alphaMask |
|
|
147 | && f->type == PictTypeDirect |
|
|
148 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
149 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
150 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
151 | return f; |
|
|
152 | |
|
|
153 | // should be a very good fallback |
|
|
154 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
155 | } |
6 | |
156 | |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
157 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
8 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
158 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
9 | pm(0), ref(0) |
159 | pm(0), ref(0) |
10 | { |
160 | { |
… | |
… | |
49 | img->ref->ours = false; |
199 | img->ref->ours = false; |
50 | |
200 | |
51 | return img; |
201 | return img; |
52 | } |
202 | } |
53 | |
203 | |
|
|
204 | # if HAVE_PIXBUF |
|
|
205 | |
54 | rxvt_img * |
206 | rxvt_img * |
55 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
207 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
56 | { |
208 | { |
57 | Display *dpy = s->display->dpy; |
209 | Display *dpy = s->display->dpy; |
58 | |
210 | |
… | |
… | |
62 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
214 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
63 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
215 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
64 | |
216 | |
65 | // since we require rgb24/argb32 formats from xrender we assume |
217 | // since we require rgb24/argb32 formats from xrender we assume |
66 | // that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
218 | // that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
67 | int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; |
|
|
68 | |
219 | |
69 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
220 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
70 | |
221 | |
71 | XImage xi; |
222 | XImage xi; |
72 | |
223 | |
… | |
… | |
76 | xi.format = ZPixmap; |
227 | xi.format = ZPixmap; |
77 | xi.byte_order = ImageByteOrder (dpy); |
228 | xi.byte_order = ImageByteOrder (dpy); |
78 | xi.bitmap_unit = 0; //XY only, unused |
229 | xi.bitmap_unit = 0; //XY only, unused |
79 | xi.bitmap_bit_order = 0; //XY only, unused |
230 | xi.bitmap_bit_order = 0; //XY only, unused |
80 | xi.bitmap_pad = BitmapPad (dpy); |
231 | xi.bitmap_pad = BitmapPad (dpy); |
81 | xi.depth = depth; |
232 | xi.depth = 32; |
82 | xi.bytes_per_line = 0; |
233 | xi.bytes_per_line = 0; |
83 | xi.bits_per_pixel = 32; //Z only |
234 | xi.bits_per_pixel = 32; //Z only |
84 | xi.red_mask = 0x00000000; //Z only, unused |
235 | xi.red_mask = 0x00000000; //Z only, unused |
85 | xi.green_mask = 0x00000000; //Z only, unused |
236 | xi.green_mask = 0x00000000; //Z only, unused |
86 | xi.blue_mask = 0x00000000; //Z only, unused |
237 | xi.blue_mask = 0x00000000; //Z only, unused |
… | |
… | |
95 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
246 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
96 | |
247 | |
97 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
248 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
98 | |
249 | |
99 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
250 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
100 | |
251 | bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb); |
101 | assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb)); |
|
|
102 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
252 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
|
|
253 | |
103 | char *line = xi.data; |
254 | char *line = xi.data; |
104 | |
255 | |
105 | for (int y = 0; y < height; y++) |
256 | for (int y = 0; y < height; y++) |
106 | { |
257 | { |
107 | unsigned char *src = row; |
258 | unsigned char *src = row; |
108 | uint32_t *dst = (uint32_t *)line; |
259 | uint32_t *dst = (uint32_t *)line; |
109 | |
260 | |
110 | if (depth == 24) |
261 | if (!pb_has_alpha) |
111 | for (int x = 0; x < width; x++) |
262 | for (int x = 0; x < width; x++) |
112 | { |
263 | { |
113 | uint8_t r = *src++; |
264 | uint8_t r = *src++; |
114 | uint8_t g = *src++; |
265 | uint8_t g = *src++; |
115 | uint8_t b = *src++; |
266 | uint8_t b = *src++; |
116 | |
267 | |
117 | uint32_t v = (r << 16) | (g << 8) | b; |
268 | uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; |
118 | |
269 | |
119 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
270 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
120 | v = ecb_bswap32 (v); |
271 | v = ecb_bswap32 (v); |
121 | |
272 | |
122 | *dst++ = v; |
273 | *dst++ = v; |
… | |
… | |
139 | |
290 | |
140 | row += rowstride; |
291 | row += rowstride; |
141 | line += xi.bytes_per_line; |
292 | line += xi.bytes_per_line; |
142 | } |
293 | } |
143 | |
294 | |
144 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); |
295 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); |
145 | img->alloc (); |
296 | img->alloc (); |
146 | |
297 | |
147 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
298 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
148 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
299 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
149 | XFreeGC (dpy, gc); |
300 | XFreeGC (dpy, gc); |
… | |
… | |
167 | g_object_unref (pb); |
318 | g_object_unref (pb); |
168 | |
319 | |
169 | return img; |
320 | return img; |
170 | } |
321 | } |
171 | |
322 | |
|
|
323 | # endif |
|
|
324 | |
172 | void |
325 | void |
173 | rxvt_img::destroy () |
326 | rxvt_img::destroy () |
174 | { |
327 | { |
175 | if (--ref->cnt) |
328 | if (--ref->cnt) |
176 | return; |
329 | return; |
… | |
… | |
192 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
345 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
193 | ref = new pixref (w, h); |
346 | ref = new pixref (w, h); |
194 | } |
347 | } |
195 | |
348 | |
196 | Picture |
349 | Picture |
197 | rxvt_img::src_picture () |
350 | rxvt_img::picture () |
198 | { |
351 | { |
199 | Display *dpy = s->display->dpy; |
352 | Display *dpy = s->display->dpy; |
200 | |
353 | |
201 | XRenderPictureAttributes pa; |
354 | XRenderPictureAttributes pa; |
202 | pa.repeat = repeat; |
355 | pa.repeat = repeat; |
… | |
… | |
221 | pm = pm2; |
374 | pm = pm2; |
222 | ref = new pixref (ref->w, ref->h); |
375 | ref = new pixref (ref->w, ref->h); |
223 | } |
376 | } |
224 | |
377 | |
225 | void |
378 | void |
226 | rxvt_img::fill (const rxvt_color &c) |
379 | rxvt_img::fill (const rgba &c) |
227 | { |
380 | { |
228 | XGCValues gcv; |
381 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
229 | gcv.foreground = c; |
382 | |
230 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
383 | Display *dpy = s->display->dpy; |
231 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
384 | Picture src = picture (); |
232 | XFreeGC (s->display->dpy, gc); |
385 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
|
|
386 | XRenderFreePicture (dpy, src); |
|
|
387 | } |
|
|
388 | |
|
|
389 | void |
|
|
390 | rxvt_img::add_alpha () |
|
|
391 | { |
|
|
392 | if (format->direct.alphaMask) |
|
|
393 | return; |
|
|
394 | |
|
|
395 | Display *dpy = s->display->dpy; |
|
|
396 | |
|
|
397 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
|
|
398 | img->alloc (); |
|
|
399 | |
|
|
400 | Picture src = picture (); |
|
|
401 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
402 | |
|
|
403 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
404 | |
|
|
405 | XRenderFreePicture (dpy, src); |
|
|
406 | XRenderFreePicture (dpy, dst); |
|
|
407 | |
|
|
408 | ::swap (img->ref, ref); |
|
|
409 | ::swap (img->pm , pm ); |
|
|
410 | |
|
|
411 | delete img; |
233 | } |
412 | } |
234 | |
413 | |
235 | static void |
414 | static void |
236 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
415 | get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params) |
237 | { |
416 | { |
238 | double sigma = radius / 2.0; |
417 | nv sigma = radius / 2.0; |
239 | double scale = sqrt (2.0 * M_PI) * sigma; |
418 | nv scale = sqrt (2.0 * M_PI) * sigma; |
240 | double sum = 0.0; |
419 | nv sum = 0.0; |
241 | |
420 | |
242 | for (int i = 0; i < width; i++) |
421 | for (int i = 0; i < width; i++) |
243 | { |
422 | { |
244 | double x = i - width / 2; |
423 | nv x = i - width / 2; |
245 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
424 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
246 | sum += kernel[i]; |
425 | sum += kernel[i]; |
247 | } |
426 | } |
248 | |
427 | |
249 | params[0] = XDoubleToFixed (width); |
428 | params[0] = XDoubleToFixed (width); |
… | |
… | |
259 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
438 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
260 | return clone (); |
439 | return clone (); |
261 | |
440 | |
262 | Display *dpy = s->display->dpy; |
441 | Display *dpy = s->display->dpy; |
263 | int size = max (rh, rv) * 2 + 1; |
442 | int size = max (rh, rv) * 2 + 1; |
264 | double *kernel = (double *)malloc (size * sizeof (double)); |
443 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
265 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
444 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
266 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
445 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
267 | img->alloc (); |
446 | img->alloc (); |
268 | |
447 | |
269 | XRenderPictureAttributes pa; |
448 | XRenderPictureAttributes pa; |
… | |
… | |
307 | w, h); |
486 | w, h); |
308 | } |
487 | } |
309 | |
488 | |
310 | free (kernel); |
489 | free (kernel); |
311 | free (params); |
490 | free (params); |
|
|
491 | |
312 | XRenderFreePicture (dpy, src); |
492 | XRenderFreePicture (dpy, src); |
313 | XRenderFreePicture (dpy, dst); |
493 | XRenderFreePicture (dpy, dst); |
314 | XRenderFreePicture (dpy, tmp); |
494 | XRenderFreePicture (dpy, tmp); |
315 | |
495 | |
316 | return img; |
496 | return img; |
317 | } |
497 | } |
318 | |
498 | |
319 | static Picture |
499 | static Picture |
320 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
500 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
321 | { |
501 | { |
322 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
502 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
323 | |
503 | |
324 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
504 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
325 | XRenderPictureAttributes pa; |
505 | XRenderPictureAttributes pa; |
326 | pa.repeat = True; |
506 | pa.repeat = RepeatNormal; |
|
|
507 | pa.component_alpha = component_alpha; |
327 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
508 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
328 | |
509 | |
329 | XFreePixmap (dpy, pixmap); |
510 | XFreePixmap (dpy, pixmap); |
330 | |
511 | |
331 | return mask; |
512 | return mask; |
332 | } |
513 | } |
… | |
… | |
356 | unshare (); |
537 | unshare (); |
357 | |
538 | |
358 | Display *dpy = s->display->dpy; |
539 | Display *dpy = s->display->dpy; |
359 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
540 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
360 | |
541 | |
|
|
542 | // loop should not be needed for brightness, as only -1..1 makes sense |
361 | while (r | g | b | a) |
543 | //while (r | g | b | a) |
362 | { |
544 | { |
363 | unsigned short xr, xg, xb, xa; |
545 | unsigned short xr, xg, xb, xa; |
364 | XRenderColor mask_c; |
546 | XRenderColor mask_c; |
365 | |
547 | |
366 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
548 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
… | |
… | |
368 | |
550 | |
369 | if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
551 | if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
370 | { |
552 | { |
371 | XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
553 | XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
372 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
554 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
555 | mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing |
|
|
556 | mask_c.green = -mask_c.green; |
|
|
557 | mask_c.blue = -mask_c.blue; |
|
|
558 | mask_c.alpha = -mask_c.alpha; |
373 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
559 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
374 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
560 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
375 | } |
561 | } |
376 | } |
562 | } |
377 | |
563 | |
378 | |
|
|
379 | XRenderFreePicture (dpy, dst); |
564 | XRenderFreePicture (dpy, dst); |
380 | } |
565 | } |
381 | |
566 | |
382 | void |
567 | void |
383 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
568 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
384 | { |
569 | { |
385 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
570 | if (r < 0 || g < 0 || b < 0 || a < 0) |
386 | { |
571 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
387 | rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); |
572 | |
388 | return; |
573 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
|
|
574 | img->alloc (); |
|
|
575 | img->fill (rgba (0, 0, 0, 0)); |
|
|
576 | |
|
|
577 | // premultiply (yeah, these are not exact, sue me or fix it) |
|
|
578 | r = (r * (a >> 8)) >> 8; |
|
|
579 | g = (g * (a >> 8)) >> 8; |
|
|
580 | b = (b * (a >> 8)) >> 8; |
|
|
581 | |
|
|
582 | Display *dpy = s->display->dpy; |
|
|
583 | |
|
|
584 | Picture src = picture (); |
|
|
585 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
|
|
586 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
|
|
587 | |
|
|
588 | //TODO: this operator does not yet implement some useful contrast |
|
|
589 | while (r | g | b | a) |
389 | } |
590 | { |
|
|
591 | unsigned short xr, xg, xb, xa; |
|
|
592 | XRenderColor mask_c; |
390 | |
593 | |
|
|
594 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
595 | { |
|
|
596 | XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
|
|
597 | XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
598 | } |
|
|
599 | } |
|
|
600 | |
|
|
601 | XRenderFreePicture (dpy, mul); |
|
|
602 | XRenderFreePicture (dpy, dst); |
|
|
603 | XRenderFreePicture (dpy, src); |
|
|
604 | |
|
|
605 | ::swap (img->ref, ref); |
|
|
606 | ::swap (img->pm , pm ); |
|
|
607 | |
|
|
608 | delete img; |
|
|
609 | } |
|
|
610 | |
|
|
611 | void |
|
|
612 | rxvt_img::draw (rxvt_img *img, int op, nv mask) |
|
|
613 | { |
391 | unshare (); |
614 | unshare (); |
392 | |
615 | |
393 | Display *dpy = s->display->dpy; |
616 | Display *dpy = s->display->dpy; |
394 | Picture src = create_xrender_mask (dpy, pm, True); |
617 | Picture src = img->picture (); |
395 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
618 | Picture dst = picture (); |
396 | |
619 | Picture mask_p = 0; |
397 | XRenderColor mask_c; |
620 | |
398 | mask_c.red = r; |
621 | if (mask != 1.) |
399 | mask_c.green = g; |
622 | { |
400 | mask_c.blue = b; |
623 | mask_p = create_xrender_mask (dpy, img->pm, False, False); |
401 | mask_c.alpha = a; |
624 | XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; |
402 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
625 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
626 | } |
403 | |
627 | |
404 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
628 | XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); |
405 | |
629 | |
406 | XRenderFreePicture (dpy, src); |
630 | XRenderFreePicture (dpy, src); |
407 | XRenderFreePicture (dpy, dst); |
631 | XRenderFreePicture (dpy, dst); |
|
|
632 | |
|
|
633 | if (mask_p) |
|
|
634 | XRenderFreePicture (dpy, mask_p); |
408 | } |
635 | } |
409 | |
636 | |
410 | rxvt_img * |
637 | rxvt_img * |
411 | rxvt_img::clone () |
638 | rxvt_img::clone () |
412 | { |
639 | { |
413 | return new rxvt_img (*this); |
640 | return new rxvt_img (*this); |
414 | } |
|
|
415 | |
|
|
416 | static XRenderPictFormat * |
|
|
417 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
418 | { |
|
|
419 | if (format->direct.alphaMask) |
|
|
420 | return format; // already has alpha |
|
|
421 | |
|
|
422 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
423 | if (format->type == PictTypeDirect) |
|
|
424 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
425 | if (f->direct.alphaMask |
|
|
426 | && f->type == PictTypeDirect |
|
|
427 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
428 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
429 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
430 | return f; |
|
|
431 | |
|
|
432 | // should be a very good fallback |
|
|
433 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
434 | } |
641 | } |
435 | |
642 | |
436 | rxvt_img * |
643 | rxvt_img * |
437 | rxvt_img::reify () |
644 | rxvt_img::reify () |
438 | { |
645 | { |
… | |
… | |
447 | && repeat == RepeatNone; // and we have no good pixels to fill with |
654 | && repeat == RepeatNone; // and we have no good pixels to fill with |
448 | |
655 | |
449 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
656 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
450 | img->alloc (); |
657 | img->alloc (); |
451 | |
658 | |
452 | Picture src = src_picture (); |
659 | Picture src = picture (); |
453 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
660 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
454 | |
661 | |
455 | if (alpha) |
662 | if (alpha) |
456 | { |
663 | { |
457 | XRenderColor rc = { 0, 0, 0, 0 }; |
664 | XRenderColor rc = { 0, 0, 0, 0 }; |
458 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
665 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
459 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
666 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
460 | } |
667 | } |
461 | else |
668 | else |
462 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
669 | XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); |
463 | |
670 | |
464 | XRenderFreePicture (dpy, src); |
671 | XRenderFreePicture (dpy, src); |
465 | XRenderFreePicture (dpy, dst); |
672 | XRenderFreePicture (dpy, dst); |
466 | |
673 | |
467 | return img; |
674 | return img; |
… | |
… | |
470 | rxvt_img * |
677 | rxvt_img * |
471 | rxvt_img::sub_rect (int x, int y, int width, int height) |
678 | rxvt_img::sub_rect (int x, int y, int width, int height) |
472 | { |
679 | { |
473 | rxvt_img *img = clone (); |
680 | rxvt_img *img = clone (); |
474 | |
681 | |
475 | img->x += x; |
682 | img->x -= x; |
476 | img->y += y; |
683 | img->y -= y; |
477 | |
684 | |
478 | if (w != width || h != height) |
685 | if (w != width || h != height) |
479 | { |
686 | { |
480 | img->w = width; |
687 | img->w = width; |
481 | img->h = height; |
688 | img->h = height; |
… | |
… | |
487 | |
694 | |
488 | return img; |
695 | return img; |
489 | } |
696 | } |
490 | |
697 | |
491 | rxvt_img * |
698 | rxvt_img * |
492 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
699 | rxvt_img::transform (nv matrix[3][3]) |
493 | { |
700 | { |
|
|
701 | // calculate new pixel bounding box coordinates |
|
|
702 | nv r[2], rmin[2], rmax[2]; |
|
|
703 | |
|
|
704 | mat3x3 m (matrix); |
|
|
705 | |
|
|
706 | for (int i = 0; i < 2; ++i) |
|
|
707 | { |
|
|
708 | nv v; |
|
|
709 | |
|
|
710 | v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v; |
|
|
711 | v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
712 | v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
713 | v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
|
|
714 | } |
|
|
715 | |
|
|
716 | float sx = rmin [0] - x; |
|
|
717 | float sy = rmin [1] - y; |
|
|
718 | |
|
|
719 | // TODO: adjust matrix for subpixel accuracy |
|
|
720 | int nx = floor (rmin [0]); |
|
|
721 | int ny = floor (rmin [1]); |
|
|
722 | |
|
|
723 | int new_width = ceil (rmax [0] - rmin [0]); |
|
|
724 | int new_height = ceil (rmax [1] - rmin [1]); |
|
|
725 | |
|
|
726 | m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y); |
|
|
727 | |
|
|
728 | mat3x3 inv = m.invert (); |
|
|
729 | |
494 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
730 | rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat); |
495 | img->alloc (); |
731 | img->alloc (); |
496 | |
732 | |
497 | Display *dpy = s->display->dpy; |
733 | Display *dpy = s->display->dpy; |
498 | Picture src = src_picture (); |
734 | Picture src = picture (); |
499 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
735 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
500 | |
736 | |
501 | XTransform xfrm; |
737 | XTransform xfrm; |
502 | |
738 | |
503 | for (int i = 0; i < 3; ++i) |
739 | for (int i = 0; i < 3; ++i) |
504 | for (int j = 0; j < 3; ++j) |
740 | for (int j = 0; j < 3; ++j) |
505 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
741 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
506 | |
|
|
507 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
|
|
508 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
509 | |
742 | |
510 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
743 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
511 | XRenderSetPictureTransform (dpy, src, &xfrm); |
744 | XRenderSetPictureTransform (dpy, src, &xfrm); |
512 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
745 | XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
|
|
746 | #if 1 |
|
|
747 | { |
|
|
748 | XRenderColor rc = { 65535,0,0,65535 }; |
|
|
749 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height); |
|
|
750 | }{ |
|
|
751 | XRenderColor rc = { 0,0,0,65535 }; |
|
|
752 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2); |
|
|
753 | } |
|
|
754 | XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
|
|
755 | #endif |
513 | |
756 | |
514 | XRenderFreePicture (dpy, src); |
757 | XRenderFreePicture (dpy, src); |
515 | XRenderFreePicture (dpy, dst); |
758 | XRenderFreePicture (dpy, dst); |
516 | |
759 | |
517 | return img; |
760 | return img; |
… | |
… | |
521 | rxvt_img::scale (int new_width, int new_height) |
764 | rxvt_img::scale (int new_width, int new_height) |
522 | { |
765 | { |
523 | if (w == new_width && h == new_height) |
766 | if (w == new_width && h == new_height) |
524 | return clone (); |
767 | return clone (); |
525 | |
768 | |
526 | double matrix[9] = { |
769 | nv matrix[3][3] = { |
527 | w / (double)new_width, 0, 0, |
770 | { new_width / (nv)w, 0, 0 }, |
528 | 0, h / (double)new_height, 0, |
771 | { 0, new_height / (nv)h, 0 }, |
529 | 0, 0, 1 |
772 | { 0, 0, 1 } |
530 | }; |
773 | }; |
531 | |
774 | |
532 | int old_repeat_mode = repeat; |
775 | int old_repeat_mode = repeat; |
533 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
776 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
534 | |
777 | |
535 | rxvt_img *img = transform (new_width, new_height, matrix); |
778 | rxvt_img *img = transform (matrix); |
536 | |
779 | |
537 | repeat = old_repeat_mode; |
780 | repeat = old_repeat_mode; |
538 | img->repeat = repeat; |
781 | img->repeat = repeat; |
539 | |
782 | |
540 | return img; |
783 | return img; |
541 | } |
784 | } |
542 | |
785 | |
543 | rxvt_img * |
786 | rxvt_img * |
544 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
787 | rxvt_img::rotate (int cx, int cy, nv phi) |
545 | { |
788 | { |
546 | double s = sin (phi); |
789 | nv s = sin (phi); |
547 | double c = cos (phi); |
790 | nv c = cos (phi); |
548 | |
791 | |
549 | double matrix[9] = { |
792 | nv matrix[3][3] = { |
|
|
793 | #if 0 |
550 | c, -s, -c * x + s * y + x, |
794 | { c, -s, cx - c * cx + s * cy }, |
551 | s, c, -s * x - c * y + y, |
795 | { s, c, cy - s * cx - c * cy }, |
552 | 0, 0, 1 |
796 | { 0, 0, 1 } |
|
|
797 | #else |
|
|
798 | { c, -s, 0 }, |
|
|
799 | { s, c, 0 }, |
|
|
800 | { 0, 0, 1 } |
|
|
801 | #endif |
553 | }; |
802 | }; |
554 | |
803 | |
555 | return transform (new_width, new_height, matrix); |
804 | move (-cx, -cy); |
556 | } |
805 | rxvt_img *img = transform (matrix); |
|
|
806 | move ( cx, cy); |
|
|
807 | img->move (cx, cy); |
557 | |
808 | |
|
|
809 | return img; |
|
|
810 | } |
|
|
811 | |
558 | rxvt_img * |
812 | rxvt_img * |
559 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
813 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
560 | { |
814 | { |
561 | if (new_format == format) |
815 | if (new_format == format) |
562 | return clone (); |
816 | return clone (); |
563 | |
817 | |
564 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
818 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
565 | img->alloc (); |
819 | img->alloc (); |
566 | |
820 | |
567 | Display *dpy = s->display->dpy; |
821 | Display *dpy = s->display->dpy; |
568 | Picture src = src_picture (); |
822 | Picture src = picture (); |
569 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
823 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
570 | int op = PictOpSrc; |
824 | int op = PictOpSrc; |
571 | |
825 | |
572 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
826 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
573 | { |
827 | { |
574 | // does it have to be that complicated |
828 | // does it have to be that complicated |
575 | rgba c; |
|
|
576 | bg.get (c); |
|
|
577 | |
|
|
578 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
829 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
579 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
830 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
580 | |
831 | |
581 | op = PictOpOver; |
832 | op = PictOpOver; |
582 | } |
833 | } |
583 | |
834 | |
… | |
… | |
588 | |
839 | |
589 | return img; |
840 | return img; |
590 | } |
841 | } |
591 | |
842 | |
592 | rxvt_img * |
843 | rxvt_img * |
593 | rxvt_img::blend (rxvt_img *img, double factor) |
844 | rxvt_img::blend (rxvt_img *img, nv factor) |
594 | { |
845 | { |
595 | rxvt_img *img2 = clone (); |
846 | rxvt_img *img2 = clone (); |
596 | Display *dpy = s->display->dpy; |
847 | Display *dpy = s->display->dpy; |
597 | Picture src = img->src_picture (); |
848 | Picture src = img->picture (); |
598 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
849 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
599 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
850 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
600 | |
851 | |
601 | XRenderColor mask_c; |
852 | XRenderColor mask_c; |
602 | |
853 | |
603 | mask_c.alpha = float_to_component (factor); |
854 | mask_c.alpha = float_to_component (factor); |
604 | mask_c.red = |
855 | mask_c.red = |