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