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