… | |
… | |
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 | |
235 | Display *dpy = s->display->dpy; |
358 | Display *dpy = s->display->dpy; |
236 | Picture src = create_xrender_mask (dpy, pm, True); |
359 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
360 | |
|
|
361 | // loop should not be needed for brightness, as only -1..1 makes sense |
|
|
362 | //while (r | g | b | a) |
|
|
363 | { |
|
|
364 | unsigned short xr, xg, xb, xa; |
|
|
365 | XRenderColor mask_c; |
|
|
366 | |
|
|
367 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
368 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
|
|
369 | |
|
|
370 | if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
371 | { |
|
|
372 | XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
|
|
373 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
374 | mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing |
|
|
375 | mask_c.green = -mask_c.green; |
|
|
376 | mask_c.blue = -mask_c.blue; |
|
|
377 | mask_c.alpha = -mask_c.alpha; |
|
|
378 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
|
|
379 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
380 | } |
|
|
381 | } |
|
|
382 | |
|
|
383 | |
|
|
384 | XRenderFreePicture (dpy, dst); |
|
|
385 | } |
|
|
386 | |
|
|
387 | void |
|
|
388 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
|
|
389 | { |
|
|
390 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
391 | { |
|
|
392 | rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); |
|
|
393 | return; |
|
|
394 | } |
|
|
395 | |
|
|
396 | unshare (); |
|
|
397 | |
|
|
398 | Display *dpy = s->display->dpy; |
237 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
399 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
238 | |
400 | |
239 | XRenderColor mask_c; |
401 | XRenderColor mask_c; |
240 | mask_c.red = r; |
402 | mask_c.red = r; |
241 | mask_c.green = g; |
403 | mask_c.green = g; |
242 | mask_c.blue = b; |
404 | mask_c.blue = b; |
243 | mask_c.alpha = a; |
405 | mask_c.alpha = a; |
244 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
406 | XRenderFillRectangle (dpy, PictOpMultiply, dst, &mask_c, 0, 0, w, h); |
245 | |
407 | |
|
|
408 | XRenderFreePicture (dpy, dst); |
|
|
409 | } |
|
|
410 | |
|
|
411 | rxvt_img * |
|
|
412 | rxvt_img::clone () |
|
|
413 | { |
|
|
414 | return new rxvt_img (*this); |
|
|
415 | } |
|
|
416 | |
|
|
417 | static XRenderPictFormat * |
|
|
418 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
419 | { |
|
|
420 | if (format->direct.alphaMask) |
|
|
421 | return format; // already has alpha |
|
|
422 | |
|
|
423 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
424 | if (format->type == PictTypeDirect) |
|
|
425 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
426 | if (f->direct.alphaMask |
|
|
427 | && f->type == PictTypeDirect |
|
|
428 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
429 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
430 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
431 | return f; |
|
|
432 | |
|
|
433 | // should be a very good fallback |
|
|
434 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
435 | } |
|
|
436 | |
|
|
437 | rxvt_img * |
|
|
438 | rxvt_img::reify () |
|
|
439 | { |
|
|
440 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
|
|
441 | return clone (); |
|
|
442 | |
|
|
443 | Display *dpy = s->display->dpy; |
|
|
444 | |
|
|
445 | // add an alpha channel if... |
|
|
446 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
|
|
447 | && (x || y) // we need one because of non-zero offset |
|
|
448 | && repeat == RepeatNone; // and we have no good pixels to fill with |
|
|
449 | |
|
|
450 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
|
|
451 | img->alloc (); |
|
|
452 | |
|
|
453 | Picture src = src_picture (); |
|
|
454 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
455 | |
|
|
456 | if (alpha) |
|
|
457 | { |
|
|
458 | XRenderColor rc = { 0, 0, 0, 0 }; |
|
|
459 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
|
|
460 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
|
|
461 | } |
|
|
462 | else |
246 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
463 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
247 | |
464 | |
248 | XRenderFreePicture (dpy, src); |
465 | XRenderFreePicture (dpy, src); |
249 | XRenderFreePicture (dpy, dst); |
466 | XRenderFreePicture (dpy, dst); |
250 | } |
|
|
251 | |
467 | |
252 | void |
|
|
253 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
|
|
254 | { |
|
|
255 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
256 | return; |
468 | return img; |
257 | |
|
|
258 | Display *dpy = s->display->dpy; |
|
|
259 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
260 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
261 | |
|
|
262 | XRenderColor mask_c; |
|
|
263 | mask_c.red = r; |
|
|
264 | mask_c.green = g; |
|
|
265 | mask_c.blue = b; |
|
|
266 | mask_c.alpha = a; |
|
|
267 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
268 | |
|
|
269 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
270 | |
|
|
271 | XRenderFreePicture (dpy, src); |
|
|
272 | XRenderFreePicture (dpy, dst); |
|
|
273 | } |
|
|
274 | |
|
|
275 | bool |
|
|
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 | } |
469 | } |
377 | |
470 | |
378 | rxvt_img * |
471 | rxvt_img * |
379 | rxvt_img::sub_rect (int x, int y, int width, int height) |
472 | rxvt_img::sub_rect (int x, int y, int width, int height) |
380 | { |
473 | { |
|
|
474 | rxvt_img *img = clone (); |
|
|
475 | |
|
|
476 | img->x += x; |
|
|
477 | img->y += y; |
|
|
478 | |
|
|
479 | if (w != width || h != height) |
|
|
480 | { |
|
|
481 | img->w = width; |
|
|
482 | img->h = height; |
|
|
483 | |
|
|
484 | rxvt_img *img2 = img->reify (); |
|
|
485 | delete img; |
|
|
486 | img = img2; |
|
|
487 | } |
|
|
488 | |
|
|
489 | return img; |
|
|
490 | } |
|
|
491 | |
|
|
492 | rxvt_img * |
|
|
493 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
|
|
494 | { |
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
495 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
382 | img->alloc (); |
496 | img->alloc (); |
383 | |
497 | |
384 | Display *dpy = s->display->dpy; |
498 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
499 | 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); |
500 | 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 | |
501 | |
410 | XTransform xfrm; |
502 | XTransform xfrm; |
411 | |
503 | |
412 | for (int i = 0; i < 3; ++i) |
504 | for (int i = 0; i < 3; ++i) |
413 | for (int j = 0; j < 3; ++j) |
505 | for (int j = 0; j < 3; ++j) |
414 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
506 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
415 | |
507 | |
|
|
508 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
|
|
509 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
510 | |
416 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
511 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
417 | XRenderSetPictureTransform (dpy, src, &xfrm); |
512 | XRenderSetPictureTransform (dpy, src, &xfrm); |
418 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
513 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
419 | |
514 | |
420 | XRenderFreePicture (dpy, src); |
515 | XRenderFreePicture (dpy, src); |
… | |
… | |
424 | } |
519 | } |
425 | |
520 | |
426 | rxvt_img * |
521 | rxvt_img * |
427 | rxvt_img::scale (int new_width, int new_height) |
522 | rxvt_img::scale (int new_width, int new_height) |
428 | { |
523 | { |
|
|
524 | if (w == new_width && h == new_height) |
|
|
525 | return clone (); |
|
|
526 | |
429 | double matrix[9] = { |
527 | double matrix[9] = { |
430 | w / (double)new_width, 0, 0, |
528 | w / (double)new_width, 0, 0, |
431 | 0, h / (double)new_height, 0, |
529 | 0, h / (double)new_height, 0, |
432 | 0, 0, 1 |
530 | 0, 0, 1 |
433 | }; |
531 | }; |
434 | |
532 | |
|
|
533 | int old_repeat_mode = repeat; |
|
|
534 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
|
|
535 | |
435 | return transform (new_width, new_height, matrix); |
536 | rxvt_img *img = transform (new_width, new_height, matrix); |
|
|
537 | |
|
|
538 | repeat = old_repeat_mode; |
|
|
539 | img->repeat = repeat; |
|
|
540 | |
|
|
541 | return img; |
436 | } |
542 | } |
437 | |
543 | |
438 | rxvt_img * |
544 | rxvt_img * |
439 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
545 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
440 | { |
546 | { |
… | |
… | |
449 | |
555 | |
450 | return transform (new_width, new_height, matrix); |
556 | return transform (new_width, new_height, matrix); |
451 | } |
557 | } |
452 | |
558 | |
453 | rxvt_img * |
559 | rxvt_img * |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
560 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
455 | { |
561 | { |
456 | if (new_format == format) |
562 | if (new_format == format) |
457 | return clone (); |
563 | return clone (); |
458 | |
564 | |
459 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
565 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
460 | img->alloc (); |
566 | img->alloc (); |
461 | |
567 | |
462 | Display *dpy = s->display->dpy; |
568 | Display *dpy = s->display->dpy; |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
569 | Picture src = src_picture (); |
464 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
570 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
465 | int op = PictOpSrc; |
571 | int op = PictOpSrc; |
466 | |
572 | |
467 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
573 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
468 | { |
574 | { |
… | |
… | |
487 | rxvt_img * |
593 | rxvt_img * |
488 | rxvt_img::blend (rxvt_img *img, double factor) |
594 | rxvt_img::blend (rxvt_img *img, double factor) |
489 | { |
595 | { |
490 | rxvt_img *img2 = clone (); |
596 | rxvt_img *img2 = clone (); |
491 | Display *dpy = s->display->dpy; |
597 | Display *dpy = s->display->dpy; |
492 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
598 | Picture src = img->src_picture (); |
493 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
599 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
494 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
600 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
495 | |
601 | |
496 | XRenderColor mask_c; |
602 | XRenderColor mask_c; |
497 | |
603 | |