… | |
… | |
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 | |
|
|
76 | rxvt_img * |
|
|
77 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
|
|
78 | { |
|
|
79 | Display *dpy = s->display->dpy; |
|
|
80 | |
|
|
81 | int width = gdk_pixbuf_get_width (pb); |
|
|
82 | int height = gdk_pixbuf_get_height (pb); |
|
|
83 | |
|
|
84 | if (width > 32767 || height > 32767) // well, we *could* upload in chunks |
|
|
85 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); |
|
|
86 | |
|
|
87 | // since we require rgb24/argb32 formats from xrender we assume |
|
|
88 | // that both 24 and 32 bpp MUST be supported by any screen that supports xrender |
|
|
89 | |
|
|
90 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
91 | |
|
|
92 | XImage xi; |
|
|
93 | |
|
|
94 | xi.width = width; |
|
|
95 | xi.height = height; |
|
|
96 | xi.xoffset = 0; |
|
|
97 | xi.format = ZPixmap; |
|
|
98 | xi.byte_order = ImageByteOrder (dpy); |
|
|
99 | xi.bitmap_unit = 0; //XY only, unused |
|
|
100 | xi.bitmap_bit_order = 0; //XY only, unused |
|
|
101 | xi.bitmap_pad = BitmapPad (dpy); |
|
|
102 | xi.depth = 32; |
|
|
103 | xi.bytes_per_line = 0; |
|
|
104 | xi.bits_per_pixel = 32; //Z only |
|
|
105 | xi.red_mask = 0x00000000; //Z only, unused |
|
|
106 | xi.green_mask = 0x00000000; //Z only, unused |
|
|
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; |
|
|
111 | |
|
|
112 | if (!XInitImage (&xi)) |
|
|
113 | rxvt_fatal ("unable to initialise ximage, please report.\n"); |
|
|
114 | |
|
|
115 | if (height > INT_MAX / xi.bytes_per_line) |
|
|
116 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
|
|
117 | |
|
|
118 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
|
|
119 | |
|
|
120 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
|
|
121 | bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb); |
|
|
122 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
|
|
123 | |
|
|
124 | char *line = xi.data; |
|
|
125 | |
|
|
126 | for (int y = 0; y < height; y++) |
|
|
127 | { |
|
|
128 | unsigned char *src = row; |
|
|
129 | uint32_t *dst = (uint32_t *)line; |
|
|
130 | |
|
|
131 | if (!pb_has_alpha) |
|
|
132 | for (int x = 0; x < width; x++) |
|
|
133 | { |
|
|
134 | uint8_t r = *src++; |
|
|
135 | uint8_t g = *src++; |
|
|
136 | uint8_t b = *src++; |
|
|
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 | |
|
|
143 | *dst++ = v; |
|
|
144 | } |
|
|
145 | else |
|
|
146 | for (int x = 0; x < width; x++) |
|
|
147 | { |
|
|
148 | uint32_t v = *(uint32_t *)src; src += 4; |
|
|
149 | |
|
|
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; |
|
|
159 | } |
|
|
160 | |
|
|
161 | row += rowstride; |
|
|
162 | line += xi.bytes_per_line; |
|
|
163 | } |
|
|
164 | |
|
|
165 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); |
|
|
166 | img->alloc (); |
|
|
167 | |
|
|
168 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
|
|
169 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
|
|
170 | XFreeGC (dpy, gc); |
|
|
171 | |
|
|
172 | free (xi.data); |
|
|
173 | |
|
|
174 | return img; |
|
|
175 | } |
|
|
176 | |
61 | rxvt_img * |
177 | rxvt_img * |
62 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
178 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
63 | { |
179 | { |
64 | GError *err = 0; |
180 | GError *err = 0; |
65 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
181 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
66 | |
182 | |
67 | if (!pb) |
183 | if (!pb) |
68 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
184 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
69 | |
185 | |
70 | rxvt_img *img = new rxvt_img ( |
186 | rxvt_img *img = new_from_pixbuf (s, pb); |
71 | s, |
|
|
72 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
|
|
73 | 0, |
|
|
74 | 0, |
|
|
75 | gdk_pixbuf_get_width (pb), |
|
|
76 | gdk_pixbuf_get_height (pb) |
|
|
77 | ); |
|
|
78 | img->alloc (); |
|
|
79 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
|
|
80 | |
187 | |
81 | g_object_unref (pb); |
188 | g_object_unref (pb); |
82 | |
189 | |
83 | return img; |
190 | return img; |
84 | } |
191 | } |
|
|
192 | |
|
|
193 | # endif |
85 | |
194 | |
86 | void |
195 | void |
87 | rxvt_img::destroy () |
196 | rxvt_img::destroy () |
88 | { |
197 | { |
89 | if (--ref->cnt) |
198 | if (--ref->cnt) |
… | |
… | |
123 | rxvt_img::unshare () |
232 | rxvt_img::unshare () |
124 | { |
233 | { |
125 | if (ref->cnt == 1 && ref->ours) |
234 | if (ref->cnt == 1 && ref->ours) |
126 | return; |
235 | return; |
127 | |
236 | |
128 | //TODO: maybe should reify instead |
|
|
129 | 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); |
130 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
238 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
131 | 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); |
132 | XFreeGC (s->display->dpy, gc); |
240 | XFreeGC (s->display->dpy, gc); |
133 | |
241 | |
… | |
… | |
136 | pm = pm2; |
244 | pm = pm2; |
137 | ref = new pixref (ref->w, ref->h); |
245 | ref = new pixref (ref->w, ref->h); |
138 | } |
246 | } |
139 | |
247 | |
140 | void |
248 | void |
141 | rxvt_img::fill (const rxvt_color &c) |
249 | rxvt_img::fill (const rgba &c) |
142 | { |
250 | { |
143 | XGCValues gcv; |
251 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
144 | gcv.foreground = c; |
252 | |
145 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
253 | Display *dpy = s->display->dpy; |
146 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
254 | Picture src = src_picture (); |
147 | 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; |
148 | } |
282 | } |
149 | |
283 | |
150 | static void |
284 | static void |
151 | 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) |
152 | { |
286 | { |
153 | double sigma = radius / 2.0; |
287 | rxvt_img::nv sigma = radius / 2.0; |
154 | double scale = sqrt (2.0 * M_PI) * sigma; |
288 | rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; |
155 | double sum = 0.0; |
289 | rxvt_img::nv sum = 0.0; |
156 | |
290 | |
157 | for (int i = 0; i < width; i++) |
291 | for (int i = 0; i < width; i++) |
158 | { |
292 | { |
159 | double x = i - width / 2; |
293 | rxvt_img::nv x = i - width / 2; |
160 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
294 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
161 | sum += kernel[i]; |
295 | sum += kernel[i]; |
162 | } |
296 | } |
163 | |
297 | |
164 | params[0] = XDoubleToFixed (width); |
298 | params[0] = XDoubleToFixed (width); |
… | |
… | |
174 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
308 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
175 | return clone (); |
309 | return clone (); |
176 | |
310 | |
177 | Display *dpy = s->display->dpy; |
311 | Display *dpy = s->display->dpy; |
178 | int size = max (rh, rv) * 2 + 1; |
312 | int size = max (rh, rv) * 2 + 1; |
179 | double *kernel = (double *)malloc (size * sizeof (double)); |
313 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
180 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
314 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
181 | 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); |
182 | img->alloc (); |
316 | img->alloc (); |
183 | |
317 | |
184 | Picture src = src_picture (); |
|
|
185 | |
|
|
186 | XRenderPictureAttributes pa; |
318 | XRenderPictureAttributes pa; |
187 | pa.repeat = RepeatPad; |
319 | pa.repeat = RepeatPad; |
188 | 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); |
189 | |
322 | |
190 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
323 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
191 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
324 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
192 | XFreePixmap (dpy, tmp_pm); |
325 | XFreePixmap (dpy, tmp_pm); |
193 | |
326 | |
… | |
… | |
209 | |
342 | |
210 | size = rv * 2 + 1; |
343 | size = rv * 2 + 1; |
211 | get_gaussian_kernel (rv, size, kernel, params); |
344 | get_gaussian_kernel (rv, size, kernel, params); |
212 | ::swap (params[0], params[1]); |
345 | ::swap (params[0], params[1]); |
213 | |
346 | |
214 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
347 | XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); |
215 | XRenderComposite (dpy, |
348 | XRenderComposite (dpy, |
216 | PictOpSrc, |
349 | PictOpSrc, |
217 | tmp, |
350 | tmp, |
218 | None, |
351 | None, |
219 | dst, |
352 | dst, |
… | |
… | |
223 | w, h); |
356 | w, h); |
224 | } |
357 | } |
225 | |
358 | |
226 | free (kernel); |
359 | free (kernel); |
227 | free (params); |
360 | free (params); |
|
|
361 | |
228 | XRenderFreePicture (dpy, src); |
362 | XRenderFreePicture (dpy, src); |
229 | XRenderFreePicture (dpy, dst); |
363 | XRenderFreePicture (dpy, dst); |
230 | XRenderFreePicture (dpy, tmp); |
364 | XRenderFreePicture (dpy, tmp); |
231 | |
365 | |
232 | return img; |
366 | return img; |
233 | } |
367 | } |
234 | |
368 | |
235 | static Picture |
369 | static Picture |
236 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
370 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
237 | { |
371 | { |
238 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
372 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
239 | |
373 | |
240 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
374 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
241 | XRenderPictureAttributes pa; |
375 | XRenderPictureAttributes pa; |
242 | pa.repeat = True; |
376 | pa.repeat = RepeatNormal; |
|
|
377 | pa.component_alpha = component_alpha; |
243 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
378 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
244 | |
379 | |
245 | XFreePixmap (dpy, pixmap); |
380 | XFreePixmap (dpy, pixmap); |
246 | |
381 | |
247 | return mask; |
382 | return mask; |
248 | } |
383 | } |
249 | |
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 | |
250 | void |
404 | void |
251 | 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) |
252 | { |
406 | { |
|
|
407 | unshare (); |
|
|
408 | |
253 | Display *dpy = s->display->dpy; |
409 | Display *dpy = s->display->dpy; |
254 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
255 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
410 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
256 | |
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; |
257 | XRenderColor mask_c; |
416 | XRenderColor mask_c; |
258 | mask_c.red = r; |
417 | |
259 | 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)) |
260 | mask_c.blue = b; |
419 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
261 | 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 | { |
262 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
466 | XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
263 | |
|
|
264 | 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 | } |
265 | |
470 | |
|
|
471 | XRenderFreePicture (dpy, mul); |
|
|
472 | XRenderFreePicture (dpy, dst); |
266 | XRenderFreePicture (dpy, src); |
473 | XRenderFreePicture (dpy, src); |
267 | XRenderFreePicture (dpy, dst); |
|
|
268 | } |
|
|
269 | |
474 | |
270 | void |
475 | ::swap (img->ref, ref); |
271 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
476 | ::swap (img->pm , pm ); |
272 | { |
|
|
273 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
274 | return; |
|
|
275 | |
477 | |
276 | Display *dpy = s->display->dpy; |
478 | delete img; |
277 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
278 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
279 | |
|
|
280 | XRenderColor mask_c; |
|
|
281 | mask_c.red = r; |
|
|
282 | mask_c.green = g; |
|
|
283 | mask_c.blue = b; |
|
|
284 | mask_c.alpha = a; |
|
|
285 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
286 | |
|
|
287 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
288 | |
|
|
289 | XRenderFreePicture (dpy, src); |
|
|
290 | XRenderFreePicture (dpy, dst); |
|
|
291 | } |
|
|
292 | |
|
|
293 | bool |
|
|
294 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
|
|
295 | { |
|
|
296 | Display *dpy = s->display->dpy; |
|
|
297 | |
|
|
298 | if (s->visual->c_class != TrueColor) |
|
|
299 | return false; |
|
|
300 | |
|
|
301 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
|
|
302 | |
|
|
303 | red_mask = (uint32_t)format->direct.redMask << format->direct.red; |
|
|
304 | green_mask = (uint32_t)format->direct.greenMask << format->direct.green; |
|
|
305 | blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; |
|
|
306 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
|
|
307 | |
|
|
308 | int width_r = ecb_popcount32 (red_mask); |
|
|
309 | int width_g = ecb_popcount32 (green_mask); |
|
|
310 | int width_b = ecb_popcount32 (blue_mask); |
|
|
311 | int width_a = ecb_popcount32 (alpha_mask); |
|
|
312 | |
|
|
313 | if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) |
|
|
314 | return false; |
|
|
315 | |
|
|
316 | int sh_r = ecb_ctz32 (red_mask); |
|
|
317 | int sh_g = ecb_ctz32 (green_mask); |
|
|
318 | int sh_b = ecb_ctz32 (blue_mask); |
|
|
319 | int sh_a = ecb_ctz32 (alpha_mask); |
|
|
320 | |
|
|
321 | if (width > 32767 || height > 32767) |
|
|
322 | return false; |
|
|
323 | |
|
|
324 | XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, |
|
|
325 | width, height, 32, 0); |
|
|
326 | if (!ximage) |
|
|
327 | return false; |
|
|
328 | |
|
|
329 | if (height > INT_MAX / ximage->bytes_per_line |
|
|
330 | || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) |
|
|
331 | { |
|
|
332 | XDestroyImage (ximage); |
|
|
333 | return false; |
|
|
334 | } |
|
|
335 | |
|
|
336 | GC gc = XCreateGC (dpy, pm, 0, 0); |
|
|
337 | |
|
|
338 | ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
339 | |
|
|
340 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
|
|
341 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
|
|
342 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
|
|
343 | char *line = ximage->data; |
|
|
344 | |
|
|
345 | for (int y = 0; y < height; y++) |
|
|
346 | { |
|
|
347 | for (int x = 0; x < width; x++) |
|
|
348 | { |
|
|
349 | unsigned char *pixel = row + x * channels; |
|
|
350 | uint32_t value; |
|
|
351 | unsigned char r, g, b, a; |
|
|
352 | |
|
|
353 | if (channels == 4) |
|
|
354 | { |
|
|
355 | a = pixel[3]; |
|
|
356 | r = pixel[0] * a / 0xff; |
|
|
357 | g = pixel[1] * a / 0xff; |
|
|
358 | b = pixel[2] * a / 0xff; |
|
|
359 | } |
|
|
360 | else |
|
|
361 | { |
|
|
362 | a = 0xff; |
|
|
363 | r = pixel[0]; |
|
|
364 | g = pixel[1]; |
|
|
365 | b = pixel[2]; |
|
|
366 | } |
|
|
367 | |
|
|
368 | value = ((r >> (8 - width_r)) << sh_r) |
|
|
369 | | ((g >> (8 - width_g)) << sh_g) |
|
|
370 | | ((b >> (8 - width_b)) << sh_b) |
|
|
371 | | ((a >> (8 - width_a)) << sh_a); |
|
|
372 | |
|
|
373 | if (ximage->bits_per_pixel == 32) |
|
|
374 | ((uint32_t *)line)[x] = value; |
|
|
375 | else |
|
|
376 | XPutPixel (ximage, x, y, value); |
|
|
377 | } |
|
|
378 | |
|
|
379 | row += rowstride; |
|
|
380 | line += ximage->bytes_per_line; |
|
|
381 | } |
|
|
382 | |
|
|
383 | XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); |
|
|
384 | XDestroyImage (ximage); |
|
|
385 | XFreeGC (dpy, gc); |
|
|
386 | |
|
|
387 | return true; |
|
|
388 | } |
479 | } |
389 | |
480 | |
390 | rxvt_img * |
481 | rxvt_img * |
391 | rxvt_img::clone () |
482 | rxvt_img::clone () |
392 | { |
483 | { |
… | |
… | |
399 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
490 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
400 | return clone (); |
491 | return clone (); |
401 | |
492 | |
402 | Display *dpy = s->display->dpy; |
493 | Display *dpy = s->display->dpy; |
403 | |
494 | |
|
|
495 | // add an alpha channel if... |
404 | bool alpha = !format->direct.alphaMask |
496 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
405 | && (x || y) |
497 | && (x || y) // we need one because of non-zero offset |
406 | && repeat == RepeatNone; |
498 | && repeat == RepeatNone; // and we have no good pixels to fill with |
407 | |
499 | |
408 | rxvt_img *img = new rxvt_img (s, alpha ? XRenderFindStandardFormat (dpy, PictStandardARGB32) : 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); |
409 | img->alloc (); |
501 | img->alloc (); |
410 | |
502 | |
411 | Picture src = src_picture (); |
503 | Picture src = src_picture (); |
412 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
504 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
413 | |
505 | |
414 | if (alpha) |
506 | if (alpha) |
415 | { |
507 | { |
416 | XRenderColor rc = { 0, 0, 0, 0 }; |
508 | XRenderColor rc = { 0, 0, 0, 0 }; |
417 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
509 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
418 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
510 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
419 | } |
511 | } |
420 | else |
512 | else |
421 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
513 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
422 | |
514 | |
… | |
… | |
445 | } |
537 | } |
446 | |
538 | |
447 | return img; |
539 | return img; |
448 | } |
540 | } |
449 | |
541 | |
450 | rxvt_img * |
542 | static void |
451 | 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]) |
452 | { |
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 | |
453 | 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); |
454 | img->alloc (); |
602 | img->alloc (); |
455 | |
603 | |
456 | Display *dpy = s->display->dpy; |
604 | Display *dpy = s->display->dpy; |
457 | Picture src = src_picture (); |
605 | Picture src = src_picture (); |
458 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
606 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
459 | |
607 | |
460 | XTransform xfrm; |
608 | XTransform xfrm; |
461 | |
609 | |
462 | for (int i = 0; i < 3; ++i) |
610 | for (int i = 0; i < 3; ++i) |
463 | for (int j = 0; j < 3; ++j) |
611 | for (int j = 0; j < 3; ++j) |
464 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
612 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
465 | |
|
|
466 | xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO |
|
|
467 | xfrm.matrix [0][3] += XDoubleToFixed (y); |
|
|
468 | |
613 | |
469 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
614 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
470 | XRenderSetPictureTransform (dpy, src, &xfrm); |
615 | XRenderSetPictureTransform (dpy, src, &xfrm); |
471 | 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); |
472 | |
617 | |
473 | XRenderFreePicture (dpy, src); |
618 | XRenderFreePicture (dpy, src); |
474 | XRenderFreePicture (dpy, dst); |
619 | XRenderFreePicture (dpy, dst); |
475 | |
620 | |
476 | return img; |
621 | return img; |
… | |
… | |
480 | rxvt_img::scale (int new_width, int new_height) |
625 | rxvt_img::scale (int new_width, int new_height) |
481 | { |
626 | { |
482 | if (w == new_width && h == new_height) |
627 | if (w == new_width && h == new_height) |
483 | return clone (); |
628 | return clone (); |
484 | |
629 | |
485 | double matrix[9] = { |
630 | nv matrix[3][3] = { |
486 | w / (double)new_width, 0, 0, |
631 | { new_width / (nv)w, 0, 0 }, |
487 | 0, h / (double)new_height, 0, |
632 | { 0, new_height / (nv)h, 0 }, |
488 | 0, 0, 1 |
633 | { 0, 0, 1 } |
489 | }; |
634 | }; |
490 | |
635 | |
491 | int old_repeat_mode = repeat; |
636 | int old_repeat_mode = repeat; |
492 | 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 |
493 | |
638 | |
494 | rxvt_img *img = transform (new_width, new_height, matrix); |
639 | rxvt_img *img = transform (matrix); |
495 | |
640 | |
496 | repeat = old_repeat_mode; |
641 | repeat = old_repeat_mode; |
497 | img->repeat = repeat; |
642 | img->repeat = repeat; |
498 | |
643 | |
499 | return img; |
644 | return img; |
500 | } |
645 | } |
501 | |
646 | |
502 | rxvt_img * |
647 | rxvt_img * |
503 | 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) |
504 | { |
649 | { |
505 | double s = sin (phi); |
650 | nv s = sin (phi); |
506 | double c = cos (phi); |
651 | nv c = cos (phi); |
507 | |
652 | |
508 | double matrix[9] = { |
653 | nv matrix[3][3] = { |
509 | c, -s, -c * x + s * y + x, |
654 | { c, -s, cx - c * cx + s * cy }, |
510 | s, c, -s * x - c * y + y, |
655 | { s, c, cy - s * cx - c * cy }, |
511 | 0, 0, 1 |
656 | { 0, 0, 1 } |
|
|
657 | //{ c, -s, 0 }, |
|
|
658 | //{ s, c, 0 }, |
|
|
659 | //{ 0, 0, 1 } |
512 | }; |
660 | }; |
513 | |
661 | |
514 | return transform (new_width, new_height, matrix); |
662 | //move (-cx, -cy); |
515 | } |
663 | rxvt_img *img = transform (matrix); |
|
|
664 | //move ( cx, cy); |
|
|
665 | //img->move (cx, cy); |
516 | |
666 | |
|
|
667 | return img; |
|
|
668 | } |
|
|
669 | |
517 | rxvt_img * |
670 | rxvt_img * |
518 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
671 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
519 | { |
672 | { |
520 | if (new_format == format) |
673 | if (new_format == format) |
521 | return clone (); |
674 | return clone (); |
522 | |
675 | |
523 | rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h, repeat); |
676 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
524 | img->alloc (); |
677 | img->alloc (); |
525 | |
|
|
526 | printf ("convert %d to %d\n", format->direct.alphaMask, new_format->direct.alphaMask);//D |
|
|
527 | |
678 | |
528 | Display *dpy = s->display->dpy; |
679 | Display *dpy = s->display->dpy; |
529 | Picture src = src_picture (); |
680 | Picture src = src_picture (); |
530 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
681 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
531 | int op = PictOpSrc; |
682 | int op = PictOpSrc; |
532 | |
683 | |
533 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
684 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
534 | { |
685 | { |
535 | // does it have to be that complicated |
686 | // does it have to be that complicated |
536 | rgba c; |
|
|
537 | bg.get (c); |
|
|
538 | |
|
|
539 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
687 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
540 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
688 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
541 | |
689 | |
542 | op = PictOpOver; |
690 | op = PictOpOver; |
543 | } |
691 | } |
544 | |
692 | |
… | |
… | |
549 | |
697 | |
550 | return img; |
698 | return img; |
551 | } |
699 | } |
552 | |
700 | |
553 | rxvt_img * |
701 | rxvt_img * |
554 | rxvt_img::blend (rxvt_img *img, double factor) |
702 | rxvt_img::blend (rxvt_img *img, nv factor) |
555 | { |
703 | { |
556 | rxvt_img *img2 = clone (); |
704 | rxvt_img *img2 = clone (); |
557 | Display *dpy = s->display->dpy; |
705 | Display *dpy = s->display->dpy; |
558 | Picture src = img->src_picture (); |
706 | Picture src = img->src_picture (); |
559 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
707 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
560 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
708 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
561 | |
709 | |
562 | XRenderColor mask_c; |
710 | XRenderColor mask_c; |
563 | |
711 | |
564 | mask_c.alpha = float_to_component (factor); |
712 | mask_c.alpha = float_to_component (factor); |
565 | mask_c.red = |
713 | mask_c.red = |