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