1 | #include <math.h> |
1 | #include <math.h> |
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 | |
|
|
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 | } |
6 | |
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 | { |
… | |
… | |
49 | img->ref->ours = false; |
69 | img->ref->ours = false; |
50 | |
70 | |
51 | return img; |
71 | return img; |
52 | } |
72 | } |
53 | |
73 | |
|
|
74 | # if HAVE_PIXBUF |
|
|
75 | |
54 | rxvt_img * |
76 | rxvt_img * |
55 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
77 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
56 | { |
78 | { |
57 | Display *dpy = s->display->dpy; |
79 | Display *dpy = s->display->dpy; |
58 | |
80 | |
… | |
… | |
62 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
84 | 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"); |
85 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
64 | |
86 | |
65 | // since we require rgb24/argb32 formats from xrender we assume |
87 | // 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 |
88 | // 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 | |
89 | |
69 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
90 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
70 | |
91 | |
71 | XImage xi; |
92 | XImage xi; |
72 | |
93 | |
… | |
… | |
76 | xi.format = ZPixmap; |
97 | xi.format = ZPixmap; |
77 | xi.byte_order = ImageByteOrder (dpy); |
98 | xi.byte_order = ImageByteOrder (dpy); |
78 | xi.bitmap_unit = 0; //XY only, unused |
99 | xi.bitmap_unit = 0; //XY only, unused |
79 | xi.bitmap_bit_order = 0; //XY only, unused |
100 | xi.bitmap_bit_order = 0; //XY only, unused |
80 | xi.bitmap_pad = BitmapPad (dpy); |
101 | xi.bitmap_pad = BitmapPad (dpy); |
81 | xi.depth = depth; |
102 | xi.depth = 32; |
82 | xi.bytes_per_line = 0; |
103 | xi.bytes_per_line = 0; |
83 | xi.bits_per_pixel = 32; //Z only |
104 | xi.bits_per_pixel = 32; //Z only |
84 | xi.red_mask = 0x00000000; //Z only, unused |
105 | xi.red_mask = 0x00000000; //Z only, unused |
85 | xi.green_mask = 0x00000000; //Z only, unused |
106 | xi.green_mask = 0x00000000; //Z only, unused |
86 | xi.blue_mask = 0x00000000; //Z only, unused |
107 | xi.blue_mask = 0x00000000; //Z only, unused |
… | |
… | |
95 | 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"); |
96 | |
117 | |
97 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
118 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
98 | |
119 | |
99 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
120 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
100 | |
121 | 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); |
122 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
|
|
123 | |
103 | char *line = xi.data; |
124 | char *line = xi.data; |
104 | |
125 | |
105 | for (int y = 0; y < height; y++) |
126 | for (int y = 0; y < height; y++) |
106 | { |
127 | { |
107 | unsigned char *src = row; |
128 | unsigned char *src = row; |
108 | uint32_t *dst = (uint32_t *)line; |
129 | uint32_t *dst = (uint32_t *)line; |
109 | |
130 | |
110 | if (depth == 24) |
131 | if (!pb_has_alpha) |
111 | for (int x = 0; x < width; x++) |
132 | for (int x = 0; x < width; x++) |
112 | { |
133 | { |
113 | uint8_t r = *src++; |
134 | uint8_t r = *src++; |
114 | uint8_t g = *src++; |
135 | uint8_t g = *src++; |
115 | uint8_t b = *src++; |
136 | uint8_t b = *src++; |
116 | |
137 | |
117 | uint32_t v = (r << 16) | (g << 8) | b; |
138 | uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; |
118 | |
139 | |
119 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
140 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
120 | v = ecb_bswap32 (v); |
141 | v = ecb_bswap32 (v); |
121 | |
142 | |
122 | *dst++ = v; |
143 | *dst++ = v; |
… | |
… | |
139 | |
160 | |
140 | row += rowstride; |
161 | row += rowstride; |
141 | line += xi.bytes_per_line; |
162 | line += xi.bytes_per_line; |
142 | } |
163 | } |
143 | |
164 | |
144 | 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); |
145 | img->alloc (); |
166 | img->alloc (); |
146 | |
167 | |
147 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
168 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
148 | 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); |
149 | XFreeGC (dpy, gc); |
170 | XFreeGC (dpy, gc); |
… | |
… | |
166 | |
187 | |
167 | g_object_unref (pb); |
188 | g_object_unref (pb); |
168 | |
189 | |
169 | return img; |
190 | return img; |
170 | } |
191 | } |
|
|
192 | |
|
|
193 | # endif |
171 | |
194 | |
172 | void |
195 | void |
173 | rxvt_img::destroy () |
196 | rxvt_img::destroy () |
174 | { |
197 | { |
175 | if (--ref->cnt) |
198 | if (--ref->cnt) |
… | |
… | |
221 | pm = pm2; |
244 | pm = pm2; |
222 | ref = new pixref (ref->w, ref->h); |
245 | ref = new pixref (ref->w, ref->h); |
223 | } |
246 | } |
224 | |
247 | |
225 | void |
248 | void |
226 | rxvt_img::fill (const rxvt_color &c) |
249 | rxvt_img::fill (const rgba &c) |
227 | { |
250 | { |
228 | XGCValues gcv; |
251 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
229 | gcv.foreground = c; |
252 | |
230 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
253 | Display *dpy = s->display->dpy; |
231 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
254 | Picture src = src_picture (); |
232 | 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; |
233 | } |
282 | } |
234 | |
283 | |
235 | static void |
284 | static void |
236 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
285 | get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) |
237 | { |
286 | { |
238 | double sigma = radius / 2.0; |
287 | rxvt_img::nv sigma = radius / 2.0; |
239 | double scale = sqrt (2.0 * M_PI) * sigma; |
288 | rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; |
240 | double sum = 0.0; |
289 | rxvt_img::nv sum = 0.0; |
241 | |
290 | |
242 | for (int i = 0; i < width; i++) |
291 | for (int i = 0; i < width; i++) |
243 | { |
292 | { |
244 | double x = i - width / 2; |
293 | rxvt_img::nv x = i - width / 2; |
245 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
294 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
246 | sum += kernel[i]; |
295 | sum += kernel[i]; |
247 | } |
296 | } |
248 | |
297 | |
249 | params[0] = XDoubleToFixed (width); |
298 | params[0] = XDoubleToFixed (width); |
… | |
… | |
259 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
308 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
260 | return clone (); |
309 | return clone (); |
261 | |
310 | |
262 | Display *dpy = s->display->dpy; |
311 | Display *dpy = s->display->dpy; |
263 | int size = max (rh, rv) * 2 + 1; |
312 | int size = max (rh, rv) * 2 + 1; |
264 | double *kernel = (double *)malloc (size * sizeof (double)); |
313 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
265 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
314 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
266 | 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); |
267 | img->alloc (); |
316 | img->alloc (); |
268 | |
317 | |
269 | XRenderPictureAttributes pa; |
318 | XRenderPictureAttributes pa; |
… | |
… | |
307 | w, h); |
356 | w, h); |
308 | } |
357 | } |
309 | |
358 | |
310 | free (kernel); |
359 | free (kernel); |
311 | free (params); |
360 | free (params); |
|
|
361 | |
312 | XRenderFreePicture (dpy, src); |
362 | XRenderFreePicture (dpy, src); |
313 | XRenderFreePicture (dpy, dst); |
363 | XRenderFreePicture (dpy, dst); |
314 | XRenderFreePicture (dpy, tmp); |
364 | XRenderFreePicture (dpy, tmp); |
315 | |
365 | |
316 | return img; |
366 | return img; |
317 | } |
367 | } |
318 | |
368 | |
319 | static Picture |
369 | static Picture |
320 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
370 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
321 | { |
371 | { |
322 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
372 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
323 | |
373 | |
324 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
374 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
325 | XRenderPictureAttributes pa; |
375 | XRenderPictureAttributes pa; |
326 | pa.repeat = True; |
376 | pa.repeat = RepeatNormal; |
|
|
377 | pa.component_alpha = component_alpha; |
327 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
378 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
328 | |
379 | |
329 | XFreePixmap (dpy, pixmap); |
380 | XFreePixmap (dpy, pixmap); |
330 | |
381 | |
331 | return mask; |
382 | return mask; |
332 | } |
383 | } |
333 | |
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 | |
334 | void |
404 | void |
335 | rxvt_img::brightness (uint16_t r, uint16_t g, uint16_t b, uint16_t a) |
405 | rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) |
336 | { |
406 | { |
|
|
407 | unshare (); |
|
|
408 | |
337 | Display *dpy = s->display->dpy; |
409 | Display *dpy = s->display->dpy; |
338 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
339 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
410 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
340 | |
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; |
341 | XRenderColor mask_c; |
416 | XRenderColor mask_c; |
342 | mask_c.red = r; |
417 | |
343 | 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)) |
344 | mask_c.blue = b; |
419 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
345 | 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 | { |
346 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
466 | XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
347 | |
|
|
348 | 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 | } |
349 | |
470 | |
|
|
471 | XRenderFreePicture (dpy, mul); |
|
|
472 | XRenderFreePicture (dpy, dst); |
350 | XRenderFreePicture (dpy, src); |
473 | XRenderFreePicture (dpy, src); |
351 | XRenderFreePicture (dpy, dst); |
|
|
352 | } |
|
|
353 | |
474 | |
354 | void |
475 | ::swap (img->ref, ref); |
355 | rxvt_img::contrast (uint16_t r, uint16_t g, uint16_t b, uint16_t a) |
476 | ::swap (img->pm , pm ); |
356 | { |
|
|
357 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
358 | { |
|
|
359 | rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); |
|
|
360 | return; |
|
|
361 | } |
|
|
362 | |
477 | |
363 | Display *dpy = s->display->dpy; |
478 | delete img; |
364 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
365 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
366 | |
|
|
367 | XRenderColor mask_c; |
|
|
368 | mask_c.red = r; |
|
|
369 | mask_c.green = g; |
|
|
370 | mask_c.blue = b; |
|
|
371 | mask_c.alpha = a; |
|
|
372 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
373 | |
|
|
374 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
375 | |
|
|
376 | XRenderFreePicture (dpy, src); |
|
|
377 | XRenderFreePicture (dpy, dst); |
|
|
378 | } |
479 | } |
379 | |
480 | |
380 | rxvt_img * |
481 | rxvt_img * |
381 | rxvt_img::clone () |
482 | rxvt_img::clone () |
382 | { |
483 | { |
383 | return new rxvt_img (*this); |
484 | return new rxvt_img (*this); |
384 | } |
|
|
385 | |
|
|
386 | static XRenderPictFormat * |
|
|
387 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
388 | { |
|
|
389 | if (format->direct.alphaMask) |
|
|
390 | return format; // already has alpha |
|
|
391 | |
|
|
392 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
393 | if (format->type == PictTypeDirect) |
|
|
394 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
395 | if (f->direct.alphaMask |
|
|
396 | && f->type == PictTypeDirect |
|
|
397 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
398 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
399 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
400 | return f; |
|
|
401 | |
|
|
402 | // should be a very good fallback |
|
|
403 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
404 | } |
485 | } |
405 | |
486 | |
406 | rxvt_img * |
487 | rxvt_img * |
407 | rxvt_img::reify () |
488 | rxvt_img::reify () |
408 | { |
489 | { |
… | |
… | |
456 | } |
537 | } |
457 | |
538 | |
458 | return img; |
539 | return img; |
459 | } |
540 | } |
460 | |
541 | |
461 | rxvt_img * |
542 | static void |
462 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
543 | mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) |
463 | { |
544 | { |
|
|
545 | rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
|
|
546 | rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; |
|
|
547 | rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
|
|
548 | |
|
|
549 | rxvt_img::nv 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 rxvt_img::nv |
|
|
565 | mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) |
|
|
566 | { |
|
|
567 | rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
|
|
568 | rxvt_img::nv 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 (nv 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 | nv d [2], rmin[2], rmax[2]; |
|
|
582 | |
|
|
583 | for (int i = 0; i < 2; ++i) |
|
|
584 | { |
|
|
585 | nv 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 | nv inv[3][3]; |
|
|
599 | mat_invert (matrix, inv); |
|
|
600 | |
464 | 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); |
465 | img->alloc (); |
602 | img->alloc (); |
466 | |
603 | |
467 | Display *dpy = s->display->dpy; |
604 | Display *dpy = s->display->dpy; |
468 | Picture src = src_picture (); |
605 | Picture src = src_picture (); |
469 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
606 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
470 | |
607 | |
471 | XTransform xfrm; |
608 | XTransform xfrm; |
472 | |
609 | |
473 | for (int i = 0; i < 3; ++i) |
610 | for (int i = 0; i < 3; ++i) |
474 | for (int j = 0; j < 3; ++j) |
611 | for (int j = 0; j < 3; ++j) |
475 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
612 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
476 | |
|
|
477 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
|
|
478 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
479 | |
613 | |
480 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
614 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
481 | XRenderSetPictureTransform (dpy, src, &xfrm); |
615 | XRenderSetPictureTransform (dpy, src, &xfrm); |
482 | 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); |
483 | |
617 | |
484 | XRenderFreePicture (dpy, src); |
618 | XRenderFreePicture (dpy, src); |
485 | XRenderFreePicture (dpy, dst); |
619 | XRenderFreePicture (dpy, dst); |
486 | |
620 | |
487 | return img; |
621 | return img; |
… | |
… | |
491 | rxvt_img::scale (int new_width, int new_height) |
625 | rxvt_img::scale (int new_width, int new_height) |
492 | { |
626 | { |
493 | if (w == new_width && h == new_height) |
627 | if (w == new_width && h == new_height) |
494 | return clone (); |
628 | return clone (); |
495 | |
629 | |
496 | double matrix[9] = { |
630 | nv matrix[3][3] = { |
497 | w / (double)new_width, 0, 0, |
631 | { new_width / (nv)w, 0, 0 }, |
498 | 0, h / (double)new_height, 0, |
632 | { 0, new_height / (nv)h, 0 }, |
499 | 0, 0, 1 |
633 | { 0, 0, 1 } |
500 | }; |
634 | }; |
501 | |
635 | |
502 | int old_repeat_mode = repeat; |
636 | int old_repeat_mode = repeat; |
503 | 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 |
504 | |
638 | |
505 | rxvt_img *img = transform (new_width, new_height, matrix); |
639 | rxvt_img *img = transform (matrix); |
506 | |
640 | |
507 | repeat = old_repeat_mode; |
641 | repeat = old_repeat_mode; |
508 | img->repeat = repeat; |
642 | img->repeat = repeat; |
509 | |
643 | |
510 | return img; |
644 | return img; |
511 | } |
645 | } |
512 | |
646 | |
513 | rxvt_img * |
647 | rxvt_img * |
514 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
648 | rxvt_img::rotate (int cx, int cy, nv phi) |
515 | { |
649 | { |
516 | double s = sin (phi); |
650 | nv s = sin (phi); |
517 | double c = cos (phi); |
651 | nv c = cos (phi); |
518 | |
652 | |
519 | double matrix[9] = { |
653 | nv matrix[3][3] = { |
520 | c, -s, -c * x + s * y + x, |
654 | { c, -s, cx - c * cx + s * cy }, |
521 | s, c, -s * x - c * y + y, |
655 | { s, c, cy - s * cx - c * cy }, |
522 | 0, 0, 1 |
656 | { 0, 0, 1 } |
|
|
657 | //{ c, -s, 0 }, |
|
|
658 | //{ s, c, 0 }, |
|
|
659 | //{ 0, 0, 1 } |
523 | }; |
660 | }; |
524 | |
661 | |
525 | return transform (new_width, new_height, matrix); |
662 | //move (-cx, -cy); |
526 | } |
663 | rxvt_img *img = transform (matrix); |
|
|
664 | //move ( cx, cy); |
|
|
665 | //img->move (cx, cy); |
527 | |
666 | |
|
|
667 | return img; |
|
|
668 | } |
|
|
669 | |
528 | rxvt_img * |
670 | rxvt_img * |
529 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
671 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
530 | { |
672 | { |
531 | if (new_format == format) |
673 | if (new_format == format) |
532 | return clone (); |
674 | return clone (); |
533 | |
675 | |
534 | 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); |
… | |
… | |
540 | int op = PictOpSrc; |
682 | int op = PictOpSrc; |
541 | |
683 | |
542 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
684 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
543 | { |
685 | { |
544 | // does it have to be that complicated |
686 | // does it have to be that complicated |
545 | rgba c; |
|
|
546 | bg.get (c); |
|
|
547 | |
|
|
548 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
687 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
549 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
688 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
550 | |
689 | |
551 | op = PictOpOver; |
690 | op = PictOpOver; |
552 | } |
691 | } |
553 | |
692 | |
… | |
… | |
558 | |
697 | |
559 | return img; |
698 | return img; |
560 | } |
699 | } |
561 | |
700 | |
562 | rxvt_img * |
701 | rxvt_img * |
563 | rxvt_img::blend (rxvt_img *img, double factor) |
702 | rxvt_img::blend (rxvt_img *img, nv factor) |
564 | { |
703 | { |
565 | rxvt_img *img2 = clone (); |
704 | rxvt_img *img2 = clone (); |
566 | Display *dpy = s->display->dpy; |
705 | Display *dpy = s->display->dpy; |
567 | Picture src = img->src_picture (); |
706 | Picture src = img->src_picture (); |
568 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
707 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
569 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
708 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
570 | |
709 | |
571 | XRenderColor mask_c; |
710 | XRenderColor mask_c; |
572 | |
711 | |
573 | mask_c.alpha = float_to_component (factor); |
712 | mask_c.alpha = float_to_component (factor); |
574 | mask_c.red = |
713 | mask_c.red = |