… | |
… | |
13 | } |
13 | } |
14 | |
14 | |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
16 | : s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) |
16 | : s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) |
17 | { |
17 | { |
|
|
18 | } |
|
|
19 | |
|
|
20 | rxvt_img * |
|
|
21 | rxvt_img::new_from_root (rxvt_screen *s) |
|
|
22 | { |
|
|
23 | Display *dpy = s->display->dpy; |
|
|
24 | unsigned int root_pm_w, root_pm_h; |
|
|
25 | Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_XROOTPMAP_ID]); |
|
|
26 | if (root_pixmap == None) |
|
|
27 | root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_ESETROOT_PMAP_ID]); |
|
|
28 | |
|
|
29 | if (root_pixmap == None) |
|
|
30 | return 0; |
|
|
31 | |
|
|
32 | Window wdummy; |
|
|
33 | int idummy; |
|
|
34 | unsigned int udummy; |
|
|
35 | |
|
|
36 | if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy)) |
|
|
37 | return 0; |
|
|
38 | |
|
|
39 | rxvt_img *img = new rxvt_img ( |
|
|
40 | s, |
|
|
41 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
|
|
42 | root_pm_w, |
|
|
43 | root_pm_h, |
|
|
44 | root_pixmap |
|
|
45 | ); |
|
|
46 | |
|
|
47 | img->shared = true; |
|
|
48 | |
|
|
49 | return img; |
18 | } |
50 | } |
19 | |
51 | |
20 | rxvt_img * |
52 | rxvt_img * |
21 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
53 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
22 | { |
54 | { |
… | |
… | |
87 | |
119 | |
88 | for (int i = 0; i < width; i++) |
120 | for (int i = 0; i < width; i++) |
89 | params[i+2] = XDoubleToFixed (kernel[i] / sum); |
121 | params[i+2] = XDoubleToFixed (kernel[i] / sum); |
90 | } |
122 | } |
91 | |
123 | |
92 | void |
124 | rxvt_img * |
93 | rxvt_img::blur (int rh, int rv) |
125 | rxvt_img::blur (int rh, int rv) |
94 | { |
126 | { |
95 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
127 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
96 | return; |
128 | return clone (); |
97 | |
129 | |
98 | Display *dpy = s->display->dpy; |
130 | Display *dpy = s->display->dpy; |
99 | int size = max (rh, rv) * 2 + 1; |
131 | int size = max (rh, rv) * 2 + 1; |
100 | double *kernel = (double *)malloc (size * sizeof (double)); |
132 | double *kernel = (double *)malloc (size * sizeof (double)); |
101 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
133 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
|
|
134 | rxvt_img *img = new rxvt_img (s, format, w, h); |
102 | |
135 | |
103 | XRenderPictureAttributes pa; |
136 | XRenderPictureAttributes pa; |
104 | |
137 | |
105 | pa.repeat = RepeatPad; |
138 | pa.repeat = RepeatPad; |
106 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
139 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
|
|
140 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
|
|
141 | |
107 | Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); |
142 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
108 | Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); |
143 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
109 | XFreePixmap (dpy, tmp); |
144 | XFreePixmap (dpy, tmp_pm); |
110 | |
145 | |
111 | if (kernel && params) |
146 | if (kernel && params) |
112 | { |
147 | { |
113 | size = rh * 2 + 1; |
148 | size = rh * 2 + 1; |
114 | get_gaussian_kernel (rh, size, kernel, params); |
149 | get_gaussian_kernel (rh, size, kernel, params); |
… | |
… | |
116 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
151 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
117 | XRenderComposite (dpy, |
152 | XRenderComposite (dpy, |
118 | PictOpSrc, |
153 | PictOpSrc, |
119 | src, |
154 | src, |
120 | None, |
155 | None, |
121 | dst, |
156 | tmp, |
122 | 0, 0, |
157 | 0, 0, |
123 | 0, 0, |
158 | 0, 0, |
124 | 0, 0, |
159 | 0, 0, |
125 | w, h); |
160 | w, h); |
126 | |
|
|
127 | ::swap (src, dst); |
|
|
128 | |
161 | |
129 | size = rv * 2 + 1; |
162 | size = rv * 2 + 1; |
130 | get_gaussian_kernel (rv, size, kernel, params); |
163 | get_gaussian_kernel (rv, size, kernel, params); |
131 | ::swap (params[0], params[1]); |
164 | ::swap (params[0], params[1]); |
132 | |
165 | |
133 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
166 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
134 | XRenderComposite (dpy, |
167 | XRenderComposite (dpy, |
135 | PictOpSrc, |
168 | PictOpSrc, |
136 | src, |
169 | tmp, |
137 | None, |
170 | None, |
138 | dst, |
171 | dst, |
139 | 0, 0, |
172 | 0, 0, |
140 | 0, 0, |
173 | 0, 0, |
141 | 0, 0, |
174 | 0, 0, |
… | |
… | |
144 | |
177 | |
145 | free (kernel); |
178 | free (kernel); |
146 | free (params); |
179 | free (params); |
147 | XRenderFreePicture (dpy, src); |
180 | XRenderFreePicture (dpy, src); |
148 | XRenderFreePicture (dpy, dst); |
181 | XRenderFreePicture (dpy, dst); |
|
|
182 | XRenderFreePicture (dpy, tmp); |
|
|
183 | |
|
|
184 | return img; |
149 | } |
185 | } |
150 | |
186 | |
151 | static Picture |
187 | static Picture |
152 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
188 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
153 | { |
189 | { |
… | |
… | |
176 | mask_c.blue = float_to_component (b); |
212 | mask_c.blue = float_to_component (b); |
177 | mask_c.alpha = float_to_component (a); |
213 | mask_c.alpha = float_to_component (a); |
178 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
179 | |
215 | |
180 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
216 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
217 | |
|
|
218 | XRenderFreePicture (dpy, src); |
|
|
219 | XRenderFreePicture (dpy, dst); |
181 | } |
220 | } |
182 | |
221 | |
183 | void |
222 | void |
184 | rxvt_img::contrast (double r, double g, double b, double a) |
223 | rxvt_img::contrast (double r, double g, double b, double a) |
185 | { |
224 | { |
… | |
… | |
196 | mask_c.blue = float_to_component (b); |
235 | mask_c.blue = float_to_component (b); |
197 | mask_c.alpha = float_to_component (a); |
236 | mask_c.alpha = float_to_component (a); |
198 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
199 | |
238 | |
200 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
239 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
240 | |
|
|
241 | XRenderFreePicture (dpy, src); |
|
|
242 | XRenderFreePicture (dpy, dst); |
201 | } |
243 | } |
202 | |
244 | |
203 | bool |
245 | bool |
204 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
246 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
205 | { |
247 | { |
206 | bool argb = format->id == PictStandardARGB32; |
|
|
207 | |
|
|
208 | Display *dpy = s->display->dpy; |
248 | Display *dpy = s->display->dpy; |
209 | |
249 | |
210 | if (s->visual->c_class != TrueColor) |
250 | if (s->visual->c_class != TrueColor) |
211 | return false; |
251 | return false; |
212 | |
252 | |
213 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
253 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
214 | |
254 | |
215 | if (argb) |
255 | red_mask = (uint32_t)format->direct.redMask << format->direct.red; |
216 | { |
256 | green_mask = (uint32_t)format->direct.greenMask << format->direct.green; |
217 | red_mask = 0xff << 16; |
257 | blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; |
218 | green_mask = 0xff << 8; |
|
|
219 | blue_mask = 0xff; |
|
|
220 | alpha_mask = 0xff << 24; |
|
|
221 | } |
|
|
222 | else |
|
|
223 | { |
|
|
224 | red_mask = s->visual->red_mask; |
|
|
225 | green_mask = s->visual->green_mask; |
|
|
226 | blue_mask = s->visual->blue_mask; |
|
|
227 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
258 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
228 | } |
|
|
229 | |
259 | |
230 | int width_r = ecb_popcount32 (red_mask); |
260 | int width_r = ecb_popcount32 (red_mask); |
231 | int width_g = ecb_popcount32 (green_mask); |
261 | int width_g = ecb_popcount32 (green_mask); |
232 | int width_b = ecb_popcount32 (blue_mask); |
262 | int width_b = ecb_popcount32 (blue_mask); |
233 | int width_a = ecb_popcount32 (alpha_mask); |
263 | int width_a = ecb_popcount32 (alpha_mask); |
… | |
… | |
241 | int sh_a = ecb_ctz32 (alpha_mask); |
271 | int sh_a = ecb_ctz32 (alpha_mask); |
242 | |
272 | |
243 | if (width > 32767 || height > 32767) |
273 | if (width > 32767 || height > 32767) |
244 | return false; |
274 | return false; |
245 | |
275 | |
246 | XImage *ximage = XCreateImage (dpy, s->visual, argb ? 32 : format->depth, ZPixmap, 0, 0, |
276 | XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, |
247 | width, height, 32, 0); |
277 | width, height, 32, 0); |
248 | if (!ximage) |
278 | if (!ximage) |
249 | return false; |
279 | return false; |
250 | |
280 | |
251 | if (height > INT_MAX / ximage->bytes_per_line |
281 | if (height > INT_MAX / ximage->bytes_per_line |
… | |
… | |
322 | } |
352 | } |
323 | |
353 | |
324 | rxvt_img * |
354 | rxvt_img * |
325 | rxvt_img::clone () |
355 | rxvt_img::clone () |
326 | { |
356 | { |
|
|
357 | rxvt_img *img = new rxvt_img (s, format, w, h); |
|
|
358 | |
327 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
359 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
328 | Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth); |
|
|
329 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
360 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
330 | XFreeGC (s->display->dpy, gc); |
361 | XFreeGC (s->display->dpy, gc); |
331 | return new rxvt_img (s, format, w, h, pm2); |
|
|
332 | } |
|
|
333 | |
362 | |
|
|
363 | return img; |
|
|
364 | } |
|
|
365 | |
334 | rxvt_img * |
366 | rxvt_img * |
|
|
367 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
|
|
368 | { |
|
|
369 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
370 | |
|
|
371 | Display *dpy = s->display->dpy; |
|
|
372 | XRenderPictureAttributes pa; |
|
|
373 | pa.repeat = repeat; |
|
|
374 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
375 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
376 | |
|
|
377 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
|
|
378 | |
|
|
379 | XRenderFreePicture (dpy, src); |
|
|
380 | XRenderFreePicture (dpy, dst); |
|
|
381 | |
|
|
382 | return img; |
|
|
383 | } |
|
|
384 | |
|
|
385 | rxvt_img * |
335 | rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) |
386 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
336 | { |
387 | { |
337 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
388 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
338 | |
389 | |
339 | Display *dpy = s->display->dpy; |
390 | Display *dpy = s->display->dpy; |
340 | XRenderPictureAttributes pa; |
391 | XRenderPictureAttributes pa; |
… | |
… | |
365 | w / (double)new_width, 0, 0, |
416 | w / (double)new_width, 0, 0, |
366 | 0, h / (double)new_height, 0, |
417 | 0, h / (double)new_height, 0, |
367 | 0, 0, 1 |
418 | 0, 0, 1 |
368 | }; |
419 | }; |
369 | |
420 | |
370 | return transform (new_width, new_height, RepeatNormal, matrix); |
421 | return transform (new_width, new_height, matrix); |
371 | } |
422 | } |
372 | |
423 | |
373 | rxvt_img * |
424 | rxvt_img * |
374 | rxvt_img::rotate (int new_width, int new_height, int repeat, int x, int y, double phi) |
425 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
375 | { |
426 | { |
376 | double s = sin (phi); |
427 | double s = sin (phi); |
377 | double c = cos (phi); |
428 | double c = cos (phi); |
378 | |
429 | |
379 | double matrix[9] = { |
430 | double matrix[9] = { |
380 | c, -s, -c * x + s * y + x, |
431 | c, -s, -c * x + s * y + x, |
381 | s, c, -s * x - c * y + y, |
432 | s, c, -s * x - c * y + y, |
382 | 0, 0, 1 |
433 | 0, 0, 1 |
383 | }; |
434 | }; |
384 | |
435 | |
385 | return transform (new_width, new_height, repeat, matrix); |
436 | return transform (new_width, new_height, matrix, repeat); |
386 | } |
437 | } |
387 | |
438 | |
388 | rxvt_img * |
439 | rxvt_img * |
389 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
440 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
390 | { |
441 | { |