… | |
… | |
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 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
8 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), |
8 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
9 | pm(0), refcnt(0) |
9 | pm(0), ref(0) |
10 | { |
10 | { |
11 | } |
11 | } |
12 | |
12 | |
13 | rxvt_img::rxvt_img (const rxvt_img &img) |
13 | rxvt_img::rxvt_img (const rxvt_img &img) |
14 | : s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), refcnt(img.refcnt) |
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) |
15 | { |
15 | { |
16 | if (refcnt) |
|
|
17 | ++*refcnt; |
16 | ++ref->cnt; |
18 | } |
17 | } |
19 | |
|
|
20 | #if 0 |
|
|
21 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
|
|
22 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
|
|
23 | { |
|
|
24 | } |
|
|
25 | #endif |
|
|
26 | |
18 | |
27 | rxvt_img * |
19 | rxvt_img * |
28 | rxvt_img::new_from_root (rxvt_screen *s) |
20 | rxvt_img::new_from_root (rxvt_screen *s) |
29 | { |
21 | { |
30 | Display *dpy = s->display->dpy; |
22 | Display *dpy = s->display->dpy; |
… | |
… | |
44 | return 0; |
36 | return 0; |
45 | |
37 | |
46 | rxvt_img *img = new rxvt_img ( |
38 | rxvt_img *img = new rxvt_img ( |
47 | s, |
39 | s, |
48 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
40 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
|
|
41 | 0, |
|
|
42 | 0, |
49 | root_pm_w, |
43 | root_pm_w, |
50 | root_pm_h |
44 | root_pm_h |
51 | ); |
45 | ); |
52 | |
46 | |
53 | img->pm = root_pixmap; |
47 | img->pm = root_pixmap; |
|
|
48 | img->ref = new pixref (root_pm_w, root_pm_h); |
|
|
49 | img->ref->ours = false; |
|
|
50 | |
|
|
51 | return img; |
|
|
52 | } |
|
|
53 | |
|
|
54 | rxvt_img * |
|
|
55 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
|
|
56 | { |
|
|
57 | Display *dpy = s->display->dpy; |
|
|
58 | |
|
|
59 | int width = gdk_pixbuf_get_width (pb); |
|
|
60 | int height = gdk_pixbuf_get_height (pb); |
|
|
61 | |
|
|
62 | 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"); |
|
|
64 | |
|
|
65 | // 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 |
|
|
67 | int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; |
|
|
68 | |
|
|
69 | int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
70 | |
|
|
71 | XImage xi; |
|
|
72 | |
|
|
73 | xi.width = width; |
|
|
74 | xi.height = height; |
|
|
75 | xi.xoffset = 0; |
|
|
76 | xi.format = ZPixmap; |
|
|
77 | xi.byte_order = ImageByteOrder (dpy); |
|
|
78 | xi.bitmap_unit = 0; //XY only, unused |
|
|
79 | xi.bitmap_bit_order = 0; //XY only, unused |
|
|
80 | xi.bitmap_pad = BitmapPad (dpy); |
|
|
81 | xi.depth = depth; |
|
|
82 | xi.bytes_per_line = 0; |
|
|
83 | xi.bits_per_pixel = 32; //Z only |
|
|
84 | xi.red_mask = 0x00000000; //Z only, unused |
|
|
85 | xi.green_mask = 0x00000000; //Z only, unused |
|
|
86 | xi.blue_mask = 0x00000000; //Z only, unused |
|
|
87 | xi.obdata = 0; // probably unused |
|
|
88 | |
|
|
89 | bool byte_order_mismatch = byte_order != xi.byte_order; |
|
|
90 | |
|
|
91 | if (!XInitImage (&xi)) |
|
|
92 | rxvt_fatal ("unable to initialise ximage, please report.\n"); |
|
|
93 | |
|
|
94 | if (height > INT_MAX / xi.bytes_per_line) |
|
|
95 | rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); |
|
|
96 | |
|
|
97 | xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); |
|
|
98 | |
|
|
99 | int rowstride = gdk_pixbuf_get_rowstride (pb); |
|
|
100 | |
|
|
101 | assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb)); |
|
|
102 | unsigned char *row = gdk_pixbuf_get_pixels (pb); |
|
|
103 | char *line = xi.data; |
|
|
104 | |
|
|
105 | for (int y = 0; y < height; y++) |
|
|
106 | { |
|
|
107 | unsigned char *src = row; |
|
|
108 | uint32_t *dst = (uint32_t *)line; |
|
|
109 | |
|
|
110 | if (depth == 24) |
|
|
111 | for (int x = 0; x < width; x++) |
|
|
112 | { |
|
|
113 | uint8_t r = *src++; |
|
|
114 | uint8_t g = *src++; |
|
|
115 | uint8_t b = *src++; |
|
|
116 | |
|
|
117 | uint32_t v = (r << 16) | (g << 8) | b; |
|
|
118 | |
|
|
119 | if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) |
|
|
120 | v = ecb_bswap32 (v); |
|
|
121 | |
|
|
122 | *dst++ = v; |
|
|
123 | } |
|
|
124 | else |
|
|
125 | for (int x = 0; x < width; x++) |
|
|
126 | { |
|
|
127 | uint32_t v = *(uint32_t *)src; src += 4; |
|
|
128 | |
|
|
129 | if (ecb_big_endian ()) |
|
|
130 | v = ecb_bswap32 (v); |
|
|
131 | |
|
|
132 | v = ecb_rotl32 (v, 8); // abgr to bgra |
|
|
133 | |
|
|
134 | if (!byte_order_mismatch) |
|
|
135 | v = ecb_bswap32 (v); |
|
|
136 | |
|
|
137 | *dst++ = v; |
|
|
138 | } |
|
|
139 | |
|
|
140 | row += rowstride; |
|
|
141 | line += xi.bytes_per_line; |
|
|
142 | } |
|
|
143 | |
|
|
144 | rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); |
|
|
145 | img->alloc (); |
|
|
146 | |
|
|
147 | GC gc = XCreateGC (dpy, img->pm, 0, 0); |
|
|
148 | XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); |
|
|
149 | XFreeGC (dpy, gc); |
|
|
150 | |
|
|
151 | free (xi.data); |
54 | |
152 | |
55 | return img; |
153 | return img; |
56 | } |
154 | } |
57 | |
155 | |
58 | rxvt_img * |
156 | rxvt_img * |
… | |
… | |
62 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
160 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
63 | |
161 | |
64 | if (!pb) |
162 | if (!pb) |
65 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
163 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
66 | |
164 | |
67 | rxvt_img *img = new rxvt_img ( |
165 | rxvt_img *img = new_from_pixbuf (s, pb); |
68 | s, |
|
|
69 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
|
|
70 | gdk_pixbuf_get_width (pb), |
|
|
71 | gdk_pixbuf_get_height (pb) |
|
|
72 | ); |
|
|
73 | img->alloc (); |
|
|
74 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
|
|
75 | |
166 | |
76 | g_object_unref (pb); |
167 | g_object_unref (pb); |
77 | |
168 | |
78 | return img; |
169 | return img; |
79 | } |
170 | } |
80 | |
171 | |
81 | void |
172 | void |
82 | rxvt_img::destroy () |
173 | rxvt_img::destroy () |
83 | { |
174 | { |
84 | if (!refcnt || --*refcnt) |
175 | if (--ref->cnt) |
85 | return; |
176 | return; |
86 | |
177 | |
87 | if (pm) |
178 | if (pm && ref->ours) |
88 | XFreePixmap (s->display->dpy, pm); |
179 | XFreePixmap (s->display->dpy, pm); |
89 | |
180 | |
90 | delete refcnt; |
181 | delete ref; |
91 | } |
182 | } |
92 | |
183 | |
93 | rxvt_img::~rxvt_img () |
184 | rxvt_img::~rxvt_img () |
94 | { |
185 | { |
95 | destroy (); |
186 | destroy (); |
… | |
… | |
97 | |
188 | |
98 | void |
189 | void |
99 | rxvt_img::alloc () |
190 | rxvt_img::alloc () |
100 | { |
191 | { |
101 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
192 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
102 | refcnt = new int (1); |
193 | ref = new pixref (w, h); |
|
|
194 | } |
|
|
195 | |
|
|
196 | Picture |
|
|
197 | rxvt_img::src_picture () |
|
|
198 | { |
|
|
199 | Display *dpy = s->display->dpy; |
|
|
200 | |
|
|
201 | XRenderPictureAttributes pa; |
|
|
202 | pa.repeat = repeat; |
|
|
203 | Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
204 | |
|
|
205 | return pic; |
103 | } |
206 | } |
104 | |
207 | |
105 | void |
208 | void |
106 | rxvt_img::unshare () |
209 | rxvt_img::unshare () |
107 | { |
210 | { |
108 | if (refcnt && *refcnt == 1) |
211 | if (ref->cnt == 1 && ref->ours) |
109 | return; |
212 | return; |
110 | |
213 | |
111 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
214 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
112 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
215 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
113 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
216 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
114 | XFreeGC (s->display->dpy, gc); |
217 | XFreeGC (s->display->dpy, gc); |
115 | |
218 | |
116 | destroy (); |
219 | destroy (); |
117 | |
220 | |
118 | pm = pm2; |
221 | pm = pm2; |
119 | refcnt = new int (1); |
222 | ref = new pixref (ref->w, ref->h); |
120 | } |
223 | } |
121 | |
224 | |
122 | void |
225 | void |
123 | rxvt_img::fill (const rxvt_color &c) |
226 | rxvt_img::fill (const rxvt_color &c) |
124 | { |
227 | { |
… | |
… | |
158 | |
261 | |
159 | Display *dpy = s->display->dpy; |
262 | Display *dpy = s->display->dpy; |
160 | int size = max (rh, rv) * 2 + 1; |
263 | int size = max (rh, rv) * 2 + 1; |
161 | double *kernel = (double *)malloc (size * sizeof (double)); |
264 | double *kernel = (double *)malloc (size * sizeof (double)); |
162 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
265 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
163 | rxvt_img *img = new rxvt_img (s, format, w, h); |
266 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
164 | img->alloc (); |
267 | img->alloc (); |
165 | |
268 | |
166 | XRenderPictureAttributes pa; |
269 | XRenderPictureAttributes pa; |
167 | |
|
|
168 | pa.repeat = RepeatPad; |
270 | pa.repeat = RepeatPad; |
169 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
271 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
170 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
272 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
171 | |
273 | |
172 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
274 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
173 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
275 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
174 | XFreePixmap (dpy, tmp_pm); |
276 | XFreePixmap (dpy, tmp_pm); |
175 | |
277 | |
… | |
… | |
191 | |
293 | |
192 | size = rv * 2 + 1; |
294 | size = rv * 2 + 1; |
193 | get_gaussian_kernel (rv, size, kernel, params); |
295 | get_gaussian_kernel (rv, size, kernel, params); |
194 | ::swap (params[0], params[1]); |
296 | ::swap (params[0], params[1]); |
195 | |
297 | |
196 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
298 | XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); |
197 | XRenderComposite (dpy, |
299 | XRenderComposite (dpy, |
198 | PictOpSrc, |
300 | PictOpSrc, |
199 | tmp, |
301 | tmp, |
200 | None, |
302 | None, |
201 | dst, |
303 | dst, |
… | |
… | |
227 | XFreePixmap (dpy, pixmap); |
329 | XFreePixmap (dpy, pixmap); |
228 | |
330 | |
229 | return mask; |
331 | return mask; |
230 | } |
332 | } |
231 | |
333 | |
|
|
334 | static void |
|
|
335 | extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) |
|
|
336 | { |
|
|
337 | int32_t x = clamp (c, cl0, cl1); |
|
|
338 | c -= x; |
|
|
339 | xc = x; |
|
|
340 | } |
|
|
341 | |
|
|
342 | static bool |
|
|
343 | 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) |
|
|
344 | { |
|
|
345 | extract (cl0, cl1, r, xr); |
|
|
346 | extract (cl0, cl1, g, xg); |
|
|
347 | extract (cl0, cl1, b, xb); |
|
|
348 | extract (cl0, cl1, a, xa); |
|
|
349 | |
|
|
350 | return xr | xg | xb | xa; |
|
|
351 | } |
|
|
352 | |
232 | void |
353 | void |
233 | rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
354 | rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) |
234 | { |
355 | { |
|
|
356 | unshare (); |
|
|
357 | |
|
|
358 | Display *dpy = s->display->dpy; |
|
|
359 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
360 | |
|
|
361 | while (r | g | b | a) |
|
|
362 | { |
|
|
363 | unsigned short xr, xg, xb, xa; |
|
|
364 | XRenderColor mask_c; |
|
|
365 | |
|
|
366 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
367 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
|
|
368 | |
|
|
369 | if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
370 | { |
|
|
371 | XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
|
|
372 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
373 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
|
|
374 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
375 | } |
|
|
376 | } |
|
|
377 | |
|
|
378 | |
|
|
379 | XRenderFreePicture (dpy, dst); |
|
|
380 | } |
|
|
381 | |
|
|
382 | void |
|
|
383 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
|
|
384 | { |
|
|
385 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
386 | { |
|
|
387 | rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); |
|
|
388 | return; |
|
|
389 | } |
|
|
390 | |
|
|
391 | unshare (); |
|
|
392 | |
235 | Display *dpy = s->display->dpy; |
393 | Display *dpy = s->display->dpy; |
236 | Picture src = create_xrender_mask (dpy, pm, True); |
394 | Picture src = create_xrender_mask (dpy, pm, True); |
237 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
395 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
238 | |
396 | |
239 | XRenderColor mask_c; |
397 | XRenderColor mask_c; |
… | |
… | |
241 | mask_c.green = g; |
399 | mask_c.green = g; |
242 | mask_c.blue = b; |
400 | mask_c.blue = b; |
243 | mask_c.alpha = a; |
401 | mask_c.alpha = a; |
244 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
402 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
245 | |
403 | |
246 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
404 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
247 | |
405 | |
248 | XRenderFreePicture (dpy, src); |
406 | XRenderFreePicture (dpy, src); |
249 | XRenderFreePicture (dpy, dst); |
407 | XRenderFreePicture (dpy, dst); |
250 | } |
408 | } |
251 | |
409 | |
252 | void |
410 | rxvt_img * |
253 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
411 | rxvt_img::clone () |
254 | { |
412 | { |
255 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
413 | return new rxvt_img (*this); |
|
|
414 | } |
|
|
415 | |
|
|
416 | static XRenderPictFormat * |
|
|
417 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
418 | { |
|
|
419 | if (format->direct.alphaMask) |
|
|
420 | return format; // already has alpha |
|
|
421 | |
|
|
422 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
423 | if (format->type == PictTypeDirect) |
|
|
424 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
425 | if (f->direct.alphaMask |
|
|
426 | && f->type == PictTypeDirect |
|
|
427 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
428 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
429 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
256 | return; |
430 | return f; |
257 | |
431 | |
|
|
432 | // should be a very good fallback |
|
|
433 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
434 | } |
|
|
435 | |
|
|
436 | rxvt_img * |
|
|
437 | rxvt_img::reify () |
|
|
438 | { |
|
|
439 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
|
|
440 | return clone (); |
|
|
441 | |
258 | Display *dpy = s->display->dpy; |
442 | Display *dpy = s->display->dpy; |
259 | Picture src = create_xrender_mask (dpy, pm, True); |
443 | |
|
|
444 | // add an alpha channel if... |
|
|
445 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
|
|
446 | && (x || y) // we need one because of non-zero offset |
|
|
447 | && repeat == RepeatNone; // and we have no good pixels to fill with |
|
|
448 | |
|
|
449 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
|
|
450 | img->alloc (); |
|
|
451 | |
|
|
452 | Picture src = src_picture (); |
260 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
453 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
261 | |
454 | |
262 | XRenderColor mask_c; |
455 | if (alpha) |
263 | mask_c.red = r; |
456 | { |
264 | mask_c.green = g; |
457 | XRenderColor rc = { 0, 0, 0, 0 }; |
265 | mask_c.blue = b; |
458 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
266 | mask_c.alpha = a; |
459 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
267 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
460 | } |
268 | |
461 | else |
269 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
462 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
270 | |
463 | |
271 | XRenderFreePicture (dpy, src); |
464 | XRenderFreePicture (dpy, src); |
272 | XRenderFreePicture (dpy, dst); |
465 | XRenderFreePicture (dpy, dst); |
273 | } |
|
|
274 | |
466 | |
275 | bool |
467 | return img; |
276 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
|
|
277 | { |
|
|
278 | Display *dpy = s->display->dpy; |
|
|
279 | |
|
|
280 | if (s->visual->c_class != TrueColor) |
|
|
281 | return false; |
|
|
282 | |
|
|
283 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
|
|
284 | |
|
|
285 | red_mask = (uint32_t)format->direct.redMask << format->direct.red; |
|
|
286 | green_mask = (uint32_t)format->direct.greenMask << format->direct.green; |
|
|
287 | blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; |
|
|
288 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
|
|
289 | |
|
|
290 | int width_r = ecb_popcount32 (red_mask); |
|
|
291 | int width_g = ecb_popcount32 (green_mask); |
|
|
292 | int width_b = ecb_popcount32 (blue_mask); |
|
|
293 | int width_a = ecb_popcount32 (alpha_mask); |
|
|
294 | |
|
|
295 | if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) |
|
|
296 | return false; |
|
|
297 | |
|
|
298 | int sh_r = ecb_ctz32 (red_mask); |
|
|
299 | int sh_g = ecb_ctz32 (green_mask); |
|
|
300 | int sh_b = ecb_ctz32 (blue_mask); |
|
|
301 | int sh_a = ecb_ctz32 (alpha_mask); |
|
|
302 | |
|
|
303 | if (width > 32767 || height > 32767) |
|
|
304 | return false; |
|
|
305 | |
|
|
306 | XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, |
|
|
307 | width, height, 32, 0); |
|
|
308 | if (!ximage) |
|
|
309 | return false; |
|
|
310 | |
|
|
311 | if (height > INT_MAX / ximage->bytes_per_line |
|
|
312 | || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) |
|
|
313 | { |
|
|
314 | XDestroyImage (ximage); |
|
|
315 | return false; |
|
|
316 | } |
|
|
317 | |
|
|
318 | GC gc = XCreateGC (dpy, pm, 0, 0); |
|
|
319 | |
|
|
320 | ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
321 | |
|
|
322 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
|
|
323 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
|
|
324 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
|
|
325 | char *line = ximage->data; |
|
|
326 | |
|
|
327 | for (int y = 0; y < height; y++) |
|
|
328 | { |
|
|
329 | for (int x = 0; x < width; x++) |
|
|
330 | { |
|
|
331 | unsigned char *pixel = row + x * channels; |
|
|
332 | uint32_t value; |
|
|
333 | unsigned char r, g, b, a; |
|
|
334 | |
|
|
335 | if (channels == 4) |
|
|
336 | { |
|
|
337 | a = pixel[3]; |
|
|
338 | r = pixel[0] * a / 0xff; |
|
|
339 | g = pixel[1] * a / 0xff; |
|
|
340 | b = pixel[2] * a / 0xff; |
|
|
341 | } |
|
|
342 | else |
|
|
343 | { |
|
|
344 | a = 0xff; |
|
|
345 | r = pixel[0]; |
|
|
346 | g = pixel[1]; |
|
|
347 | b = pixel[2]; |
|
|
348 | } |
|
|
349 | |
|
|
350 | value = ((r >> (8 - width_r)) << sh_r) |
|
|
351 | | ((g >> (8 - width_g)) << sh_g) |
|
|
352 | | ((b >> (8 - width_b)) << sh_b) |
|
|
353 | | ((a >> (8 - width_a)) << sh_a); |
|
|
354 | |
|
|
355 | if (ximage->bits_per_pixel == 32) |
|
|
356 | ((uint32_t *)line)[x] = value; |
|
|
357 | else |
|
|
358 | XPutPixel (ximage, x, y, value); |
|
|
359 | } |
|
|
360 | |
|
|
361 | row += rowstride; |
|
|
362 | line += ximage->bytes_per_line; |
|
|
363 | } |
|
|
364 | |
|
|
365 | XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); |
|
|
366 | XDestroyImage (ximage); |
|
|
367 | XFreeGC (dpy, gc); |
|
|
368 | |
|
|
369 | return true; |
|
|
370 | } |
|
|
371 | |
|
|
372 | rxvt_img * |
|
|
373 | rxvt_img::clone () |
|
|
374 | { |
|
|
375 | return new rxvt_img (*this); |
|
|
376 | } |
468 | } |
377 | |
469 | |
378 | rxvt_img * |
470 | rxvt_img * |
379 | rxvt_img::sub_rect (int x, int y, int width, int height) |
471 | rxvt_img::sub_rect (int x, int y, int width, int height) |
380 | { |
472 | { |
|
|
473 | rxvt_img *img = clone (); |
|
|
474 | |
|
|
475 | img->x += x; |
|
|
476 | img->y += y; |
|
|
477 | |
|
|
478 | if (w != width || h != height) |
|
|
479 | { |
|
|
480 | img->w = width; |
|
|
481 | img->h = height; |
|
|
482 | |
|
|
483 | rxvt_img *img2 = img->reify (); |
|
|
484 | delete img; |
|
|
485 | img = img2; |
|
|
486 | } |
|
|
487 | |
|
|
488 | return img; |
|
|
489 | } |
|
|
490 | |
|
|
491 | rxvt_img * |
|
|
492 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
|
|
493 | { |
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
494 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
382 | img->alloc (); |
495 | img->alloc (); |
383 | |
496 | |
384 | Display *dpy = s->display->dpy; |
497 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
498 | Picture src = src_picture (); |
386 | pa.repeat = repeat; |
|
|
387 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
388 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
499 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
389 | |
|
|
390 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
|
|
391 | |
|
|
392 | XRenderFreePicture (dpy, src); |
|
|
393 | XRenderFreePicture (dpy, dst); |
|
|
394 | |
|
|
395 | return img; |
|
|
396 | } |
|
|
397 | |
|
|
398 | rxvt_img * |
|
|
399 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
|
|
400 | { |
|
|
401 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
|
|
402 | img->alloc (); |
|
|
403 | |
|
|
404 | Display *dpy = s->display->dpy; |
|
|
405 | XRenderPictureAttributes pa; |
|
|
406 | pa.repeat = repeat; |
|
|
407 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
408 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
409 | |
500 | |
410 | XTransform xfrm; |
501 | XTransform xfrm; |
411 | |
502 | |
412 | for (int i = 0; i < 3; ++i) |
503 | for (int i = 0; i < 3; ++i) |
413 | for (int j = 0; j < 3; ++j) |
504 | for (int j = 0; j < 3; ++j) |
414 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
505 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
415 | |
506 | |
|
|
507 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
|
|
508 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
509 | |
416 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
510 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
417 | XRenderSetPictureTransform (dpy, src, &xfrm); |
511 | XRenderSetPictureTransform (dpy, src, &xfrm); |
418 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
512 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
419 | |
513 | |
420 | XRenderFreePicture (dpy, src); |
514 | XRenderFreePicture (dpy, src); |
… | |
… | |
424 | } |
518 | } |
425 | |
519 | |
426 | rxvt_img * |
520 | rxvt_img * |
427 | rxvt_img::scale (int new_width, int new_height) |
521 | rxvt_img::scale (int new_width, int new_height) |
428 | { |
522 | { |
|
|
523 | if (w == new_width && h == new_height) |
|
|
524 | return clone (); |
|
|
525 | |
429 | double matrix[9] = { |
526 | double matrix[9] = { |
430 | w / (double)new_width, 0, 0, |
527 | w / (double)new_width, 0, 0, |
431 | 0, h / (double)new_height, 0, |
528 | 0, h / (double)new_height, 0, |
432 | 0, 0, 1 |
529 | 0, 0, 1 |
433 | }; |
530 | }; |
434 | |
531 | |
|
|
532 | int old_repeat_mode = repeat; |
|
|
533 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
|
|
534 | |
435 | return transform (new_width, new_height, matrix); |
535 | rxvt_img *img = transform (new_width, new_height, matrix); |
|
|
536 | |
|
|
537 | repeat = old_repeat_mode; |
|
|
538 | img->repeat = repeat; |
|
|
539 | |
|
|
540 | return img; |
436 | } |
541 | } |
437 | |
542 | |
438 | rxvt_img * |
543 | rxvt_img * |
439 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
544 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
440 | { |
545 | { |
… | |
… | |
449 | |
554 | |
450 | return transform (new_width, new_height, matrix); |
555 | return transform (new_width, new_height, matrix); |
451 | } |
556 | } |
452 | |
557 | |
453 | rxvt_img * |
558 | rxvt_img * |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
559 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
455 | { |
560 | { |
456 | if (new_format == format) |
561 | if (new_format == format) |
457 | return clone (); |
562 | return clone (); |
458 | |
563 | |
459 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
564 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
460 | img->alloc (); |
565 | img->alloc (); |
461 | |
566 | |
462 | Display *dpy = s->display->dpy; |
567 | Display *dpy = s->display->dpy; |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
568 | Picture src = src_picture (); |
464 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
569 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
465 | int op = PictOpSrc; |
570 | int op = PictOpSrc; |
466 | |
571 | |
467 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
572 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
468 | { |
573 | { |
… | |
… | |
487 | rxvt_img * |
592 | rxvt_img * |
488 | rxvt_img::blend (rxvt_img *img, double factor) |
593 | rxvt_img::blend (rxvt_img *img, double factor) |
489 | { |
594 | { |
490 | rxvt_img *img2 = clone (); |
595 | rxvt_img *img2 = clone (); |
491 | Display *dpy = s->display->dpy; |
596 | Display *dpy = s->display->dpy; |
492 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
597 | Picture src = img->src_picture (); |
493 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
598 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
494 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
599 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
495 | |
600 | |
496 | XRenderColor mask_c; |
601 | XRenderColor mask_c; |
497 | |
602 | |