… | |
… | |
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 | |
|
|
214 | //TODO: maybe should reify instead |
111 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
215 | 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); |
216 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
113 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
217 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
114 | XFreeGC (s->display->dpy, gc); |
218 | XFreeGC (s->display->dpy, gc); |
115 | |
219 | |
116 | destroy (); |
220 | destroy (); |
117 | |
221 | |
118 | pm = pm2; |
222 | pm = pm2; |
119 | refcnt = new int (1); |
223 | ref = new pixref (ref->w, ref->h); |
120 | } |
224 | } |
121 | |
225 | |
122 | void |
226 | void |
123 | rxvt_img::fill (const rxvt_color &c) |
227 | rxvt_img::fill (const rxvt_color &c) |
124 | { |
228 | { |
… | |
… | |
158 | |
262 | |
159 | Display *dpy = s->display->dpy; |
263 | Display *dpy = s->display->dpy; |
160 | int size = max (rh, rv) * 2 + 1; |
264 | int size = max (rh, rv) * 2 + 1; |
161 | double *kernel = (double *)malloc (size * sizeof (double)); |
265 | double *kernel = (double *)malloc (size * sizeof (double)); |
162 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
266 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
163 | rxvt_img *img = new rxvt_img (s, format, w, h); |
267 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
164 | img->alloc (); |
268 | img->alloc (); |
165 | |
269 | |
166 | XRenderPictureAttributes pa; |
270 | XRenderPictureAttributes pa; |
167 | |
|
|
168 | pa.repeat = RepeatPad; |
271 | pa.repeat = RepeatPad; |
169 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
272 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
170 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
273 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
171 | |
274 | |
172 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
275 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
173 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
276 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
174 | XFreePixmap (dpy, tmp_pm); |
277 | XFreePixmap (dpy, tmp_pm); |
175 | |
278 | |
… | |
… | |
191 | |
294 | |
192 | size = rv * 2 + 1; |
295 | size = rv * 2 + 1; |
193 | get_gaussian_kernel (rv, size, kernel, params); |
296 | get_gaussian_kernel (rv, size, kernel, params); |
194 | ::swap (params[0], params[1]); |
297 | ::swap (params[0], params[1]); |
195 | |
298 | |
196 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
299 | XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); |
197 | XRenderComposite (dpy, |
300 | XRenderComposite (dpy, |
198 | PictOpSrc, |
301 | PictOpSrc, |
199 | tmp, |
302 | tmp, |
200 | None, |
303 | None, |
201 | dst, |
304 | dst, |
… | |
… | |
270 | |
373 | |
271 | XRenderFreePicture (dpy, src); |
374 | XRenderFreePicture (dpy, src); |
272 | XRenderFreePicture (dpy, dst); |
375 | XRenderFreePicture (dpy, dst); |
273 | } |
376 | } |
274 | |
377 | |
275 | bool |
378 | rxvt_img * |
276 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
379 | rxvt_img::clone () |
277 | { |
380 | { |
278 | Display *dpy = s->display->dpy; |
381 | return new rxvt_img (*this); |
|
|
382 | } |
279 | |
383 | |
280 | if (s->visual->c_class != TrueColor) |
384 | static XRenderPictFormat * |
|
|
385 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
386 | { |
|
|
387 | if (format->direct.alphaMask) |
|
|
388 | return format; // already has alpha |
|
|
389 | |
|
|
390 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
391 | if (format->type == PictTypeDirect) |
|
|
392 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
393 | if (f->direct.alphaMask |
|
|
394 | && f->type == PictTypeDirect |
|
|
395 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
396 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
397 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
281 | return false; |
398 | return f; |
282 | |
399 | |
283 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
400 | // should be a very good fallback |
|
|
401 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
402 | } |
284 | |
403 | |
285 | red_mask = (uint32_t)format->direct.redMask << format->direct.red; |
404 | rxvt_img * |
286 | green_mask = (uint32_t)format->direct.greenMask << format->direct.green; |
405 | rxvt_img::reify () |
287 | blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; |
406 | { |
288 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
407 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
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; |
408 | return clone (); |
297 | |
409 | |
298 | int sh_r = ecb_ctz32 (red_mask); |
410 | Display *dpy = s->display->dpy; |
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 | |
411 | |
303 | if (width > 32767 || height > 32767) |
412 | bool alpha = !format->direct.alphaMask |
304 | return false; |
413 | && (x || y) |
|
|
414 | && repeat == RepeatNone; |
305 | |
415 | |
306 | XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, |
416 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
307 | width, height, 32, 0); |
417 | img->alloc (); |
308 | if (!ximage) |
|
|
309 | return false; |
|
|
310 | |
418 | |
311 | if (height > INT_MAX / ximage->bytes_per_line |
419 | Picture src = src_picture (); |
312 | || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) |
420 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
421 | |
|
|
422 | if (alpha) |
313 | { |
423 | { |
314 | XDestroyImage (ximage); |
424 | XRenderColor rc = { 0, 0, 0, 0 }; |
315 | return false; |
425 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
|
|
426 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
316 | } |
427 | } |
|
|
428 | else |
|
|
429 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
317 | |
430 | |
318 | GC gc = XCreateGC (dpy, pm, 0, 0); |
431 | XRenderFreePicture (dpy, src); |
|
|
432 | XRenderFreePicture (dpy, dst); |
319 | |
433 | |
320 | ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
434 | return img; |
|
|
435 | } |
321 | |
436 | |
322 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
437 | rxvt_img * |
323 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
438 | rxvt_img::sub_rect (int x, int y, int width, int height) |
324 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
439 | { |
325 | char *line = ximage->data; |
440 | rxvt_img *img = clone (); |
326 | |
441 | |
327 | for (int y = 0; y < height; y++) |
442 | img->x += x; |
|
|
443 | img->y += y; |
|
|
444 | |
|
|
445 | if (w != width || h != height) |
328 | { |
446 | { |
329 | for (int x = 0; x < width; x++) |
447 | img->w = width; |
330 | { |
448 | img->h = height; |
331 | unsigned char *pixel = row + x * channels; |
|
|
332 | uint32_t value; |
|
|
333 | unsigned char r, g, b, a; |
|
|
334 | |
449 | |
335 | if (channels == 4) |
450 | rxvt_img *img2 = img->reify (); |
336 | { |
451 | delete img; |
337 | a = pixel[3]; |
452 | img = img2; |
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 | } |
453 | } |
364 | |
454 | |
365 | XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); |
455 | return img; |
366 | XDestroyImage (ximage); |
|
|
367 | XFreeGC (dpy, gc); |
|
|
368 | |
|
|
369 | return true; |
|
|
370 | } |
456 | } |
371 | |
457 | |
372 | rxvt_img * |
458 | rxvt_img * |
373 | rxvt_img::clone () |
459 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
374 | { |
460 | { |
375 | return new rxvt_img (*this); |
|
|
376 | } |
|
|
377 | |
|
|
378 | rxvt_img * |
|
|
379 | rxvt_img::sub_rect (int x, int y, int width, int height) |
|
|
380 | { |
|
|
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
461 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
382 | img->alloc (); |
462 | img->alloc (); |
383 | |
463 | |
384 | Display *dpy = s->display->dpy; |
464 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
465 | 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); |
466 | 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 | |
467 | |
410 | XTransform xfrm; |
468 | XTransform xfrm; |
411 | |
469 | |
412 | for (int i = 0; i < 3; ++i) |
470 | for (int i = 0; i < 3; ++i) |
413 | for (int j = 0; j < 3; ++j) |
471 | for (int j = 0; j < 3; ++j) |
414 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
472 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
415 | |
473 | |
|
|
474 | #if 0 |
|
|
475 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
|
|
476 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
477 | #endif |
|
|
478 | |
416 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
479 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
417 | XRenderSetPictureTransform (dpy, src, &xfrm); |
480 | XRenderSetPictureTransform (dpy, src, &xfrm); |
418 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
481 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
419 | |
482 | |
420 | XRenderFreePicture (dpy, src); |
483 | XRenderFreePicture (dpy, src); |
… | |
… | |
424 | } |
487 | } |
425 | |
488 | |
426 | rxvt_img * |
489 | rxvt_img * |
427 | rxvt_img::scale (int new_width, int new_height) |
490 | rxvt_img::scale (int new_width, int new_height) |
428 | { |
491 | { |
|
|
492 | if (w == new_width && h == new_height) |
|
|
493 | return clone (); |
|
|
494 | |
429 | double matrix[9] = { |
495 | double matrix[9] = { |
430 | w / (double)new_width, 0, 0, |
496 | w / (double)new_width, 0, 0, |
431 | 0, h / (double)new_height, 0, |
497 | 0, h / (double)new_height, 0, |
432 | 0, 0, 1 |
498 | 0, 0, 1 |
433 | }; |
499 | }; |
434 | |
500 | |
|
|
501 | int old_repeat_mode = repeat; |
|
|
502 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
|
|
503 | |
435 | return transform (new_width, new_height, matrix); |
504 | rxvt_img *img = transform (new_width, new_height, matrix); |
|
|
505 | |
|
|
506 | repeat = old_repeat_mode; |
|
|
507 | img->repeat = repeat; |
|
|
508 | |
|
|
509 | return img; |
436 | } |
510 | } |
437 | |
511 | |
438 | rxvt_img * |
512 | rxvt_img * |
439 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
513 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
440 | { |
514 | { |
… | |
… | |
449 | |
523 | |
450 | return transform (new_width, new_height, matrix); |
524 | return transform (new_width, new_height, matrix); |
451 | } |
525 | } |
452 | |
526 | |
453 | rxvt_img * |
527 | rxvt_img * |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
528 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
455 | { |
529 | { |
456 | if (new_format == format) |
530 | if (new_format == format) |
457 | return clone (); |
531 | return clone (); |
458 | |
532 | |
459 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
533 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
460 | img->alloc (); |
534 | img->alloc (); |
461 | |
535 | |
462 | Display *dpy = s->display->dpy; |
536 | Display *dpy = s->display->dpy; |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
537 | Picture src = src_picture (); |
464 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
538 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
465 | int op = PictOpSrc; |
539 | int op = PictOpSrc; |
466 | |
540 | |
467 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
541 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
468 | { |
542 | { |
… | |
… | |
487 | rxvt_img * |
561 | rxvt_img * |
488 | rxvt_img::blend (rxvt_img *img, double factor) |
562 | rxvt_img::blend (rxvt_img *img, double factor) |
489 | { |
563 | { |
490 | rxvt_img *img2 = clone (); |
564 | rxvt_img *img2 = clone (); |
491 | Display *dpy = s->display->dpy; |
565 | Display *dpy = s->display->dpy; |
492 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
566 | Picture src = img->src_picture (); |
493 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
567 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
494 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
568 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
495 | |
569 | |
496 | XRenderColor mask_c; |
570 | XRenderColor mask_c; |
497 | |
571 | |