ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.22
Committed: Tue Jun 5 15:18:23 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +14 -10 lines
Log Message:
Make rxvt_img::blur non-inplace.

File Contents

# User Rev Content
1 sf-exg 1.6 #include <math.h>
2 root 1.1 #include "../config.h"
3     #include "rxvt.h"
4    
5     #if HAVE_IMG
6    
7 sf-exg 1.6 #define float_to_component(d) ((d) * 65535.99)
8    
9 root 1.3 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height)
10 root 1.4 : s(screen), w(width), h(height), format(format), shared(false)
11 root 1.1 {
12 root 1.3 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
13 root 1.1 }
14    
15 root 1.3 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap)
16 root 1.4 : s(screen), pm(pixmap), w(width), h(height), format(format), shared(false)
17 root 1.1 {
18     }
19    
20 root 1.4 rxvt_img *
21 sf-exg 1.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;
50     }
51    
52     rxvt_img *
53 root 1.4 rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
54     {
55 root 1.11 GError *err = 0;
56 root 1.4 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
57    
58     if (!pb)
59 root 1.11 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
60 root 1.4
61     rxvt_img *img = new rxvt_img (
62     s,
63     XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
64     gdk_pixbuf_get_width (pb),
65     gdk_pixbuf_get_height (pb)
66     );
67    
68 root 1.14 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
69 root 1.4
70     return img;
71     }
72    
73 root 1.1 rxvt_img::~rxvt_img ()
74     {
75 root 1.4 if (!shared)
76 root 1.1 XFreePixmap (s->display->dpy, pm);
77     }
78    
79     void
80 root 1.11 rxvt_img::unshare ()
81     {
82     if (!shared)
83     return;
84    
85     rxvt_img *img = clone ();
86    
87     ::swap (pm , img->pm);
88     ::swap (shared, img->shared);
89    
90     delete img;
91     }
92    
93     void
94 root 1.1 rxvt_img::fill (const rxvt_color &c)
95     {
96     XGCValues gcv;
97     gcv.foreground = c;
98     GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv);
99     XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h);
100 root 1.3 XFreeGC (s->display->dpy, gc);
101 root 1.1 }
102    
103 sf-exg 1.6 static void
104     get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
105     {
106     double sigma = radius / 2.0;
107     double scale = sqrt (2.0 * M_PI) * sigma;
108     double sum = 0.0;
109    
110     for (int i = 0; i < width; i++)
111     {
112     double x = i - width / 2;
113     kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
114     sum += kernel[i];
115     }
116    
117     params[0] = XDoubleToFixed (width);
118     params[1] = XDoubleToFixed (1);
119    
120     for (int i = 0; i < width; i++)
121     params[i+2] = XDoubleToFixed (kernel[i] / sum);
122     }
123    
124 sf-exg 1.22 rxvt_img *
125 root 1.3 rxvt_img::blur (int rh, int rv)
126     {
127 sf-exg 1.6 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
128 sf-exg 1.22 return clone ();
129 sf-exg 1.6
130     Display *dpy = s->display->dpy;
131     int size = max (rh, rv) * 2 + 1;
132     double *kernel = (double *)malloc (size * sizeof (double));
133     XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
134 sf-exg 1.22 rxvt_img *img = new rxvt_img (s, format, w, h);
135 sf-exg 1.6
136     XRenderPictureAttributes pa;
137    
138     pa.repeat = RepeatPad;
139 sf-exg 1.22 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
140     Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
141    
142     Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
143     Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
144     XFreePixmap (dpy, tmp_pm);
145 sf-exg 1.6
146     if (kernel && params)
147     {
148     size = rh * 2 + 1;
149     get_gaussian_kernel (rh, size, kernel, params);
150    
151     XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
152     XRenderComposite (dpy,
153     PictOpSrc,
154     src,
155     None,
156 sf-exg 1.22 tmp,
157 sf-exg 1.6 0, 0,
158     0, 0,
159     0, 0,
160     w, h);
161    
162     size = rv * 2 + 1;
163     get_gaussian_kernel (rv, size, kernel, params);
164     ::swap (params[0], params[1]);
165    
166     XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
167     XRenderComposite (dpy,
168     PictOpSrc,
169 sf-exg 1.22 tmp,
170 sf-exg 1.6 None,
171     dst,
172     0, 0,
173     0, 0,
174     0, 0,
175     w, h);
176     }
177    
178     free (kernel);
179     free (params);
180     XRenderFreePicture (dpy, src);
181     XRenderFreePicture (dpy, dst);
182 sf-exg 1.22 XRenderFreePicture (dpy, tmp);
183    
184     return img;
185 sf-exg 1.6 }
186    
187     static Picture
188     create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
189     {
190     Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
191    
192     XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
193     XRenderPictureAttributes pa;
194     pa.repeat = True;
195     Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
196    
197     XFreePixmap (dpy, pixmap);
198    
199     return mask;
200 root 1.3 }
201    
202     void
203     rxvt_img::brightness (double r, double g, double b, double a)
204     {
205 sf-exg 1.6 Display *dpy = s->display->dpy;
206     Picture src = create_xrender_mask (dpy, pm, True);
207     Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
208    
209     XRenderColor mask_c;
210     mask_c.red = float_to_component (r);
211     mask_c.green = float_to_component (g);
212     mask_c.blue = float_to_component (b);
213     mask_c.alpha = float_to_component (a);
214     XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
215    
216     XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
217 root 1.3 }
218    
219     void
220     rxvt_img::contrast (double r, double g, double b, double a)
221 root 1.1 {
222 sf-exg 1.6 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
223     return;
224    
225     Display *dpy = s->display->dpy;
226     Picture src = create_xrender_mask (dpy, pm, True);
227     Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
228    
229     XRenderColor mask_c;
230     mask_c.red = float_to_component (r);
231     mask_c.green = float_to_component (g);
232     mask_c.blue = float_to_component (b);
233     mask_c.alpha = float_to_component (a);
234     XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
235    
236     XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
237 root 1.1 }
238    
239 root 1.14 bool
240     rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
241 root 1.3 {
242 root 1.14 Display *dpy = s->display->dpy;
243    
244     if (s->visual->c_class != TrueColor)
245     return false;
246    
247     uint32_t red_mask, green_mask, blue_mask, alpha_mask;
248    
249 sf-exg 1.18 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
250     green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
251     blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
252     alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
253 root 1.14
254     int width_r = ecb_popcount32 (red_mask);
255     int width_g = ecb_popcount32 (green_mask);
256     int width_b = ecb_popcount32 (blue_mask);
257     int width_a = ecb_popcount32 (alpha_mask);
258    
259     if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
260     return false;
261    
262     int sh_r = ecb_ctz32 (red_mask);
263     int sh_g = ecb_ctz32 (green_mask);
264     int sh_b = ecb_ctz32 (blue_mask);
265     int sh_a = ecb_ctz32 (alpha_mask);
266    
267     if (width > 32767 || height > 32767)
268     return false;
269    
270 sf-exg 1.18 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
271 root 1.14 width, height, 32, 0);
272     if (!ximage)
273     return false;
274    
275     if (height > INT_MAX / ximage->bytes_per_line
276     || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
277     {
278     XDestroyImage (ximage);
279     return false;
280     }
281    
282     GC gc = XCreateGC (dpy, pm, 0, 0);
283    
284     ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
285    
286     int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
287     int channels = gdk_pixbuf_get_n_channels (pixbuf);
288     unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
289     char *line = ximage->data;
290    
291     rgba c (0, 0, 0);
292    
293     if (channels == 4 && alpha_mask == 0)
294     {
295     //pix_colors[Color_bg].get (c);
296     //TODO
297 root 1.15 c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D
298 root 1.14 c.r >>= 8;
299     c.g >>= 8;
300     c.b >>= 8;
301     }
302    
303     for (int y = 0; y < height; y++)
304     {
305     for (int x = 0; x < width; x++)
306     {
307     unsigned char *pixel = row + x * channels;
308     uint32_t value;
309     unsigned char r, g, b, a;
310    
311     if (channels == 4)
312     {
313     a = pixel[3];
314     r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
315     g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
316     b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
317     }
318     else
319     {
320     a = 0xff;
321     r = pixel[0];
322     g = pixel[1];
323     b = pixel[2];
324     }
325    
326     value = ((r >> (8 - width_r)) << sh_r)
327     | ((g >> (8 - width_g)) << sh_g)
328     | ((b >> (8 - width_b)) << sh_b)
329     | ((a >> (8 - width_a)) << sh_a);
330    
331     if (ximage->bits_per_pixel == 32)
332     ((uint32_t *)line)[x] = value;
333     else
334     XPutPixel (ximage, x, y, value);
335     }
336    
337     row += rowstride;
338     line += ximage->bytes_per_line;
339     }
340    
341     XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
342     XDestroyImage (ximage);
343     XFreeGC (dpy, gc);
344    
345     return true;
346 root 1.3 }
347    
348     rxvt_img *
349 root 1.11 rxvt_img::clone ()
350 root 1.3 {
351 root 1.19 rxvt_img *img = new rxvt_img (s, format, w, h);
352    
353 root 1.3 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
354 root 1.19 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0);
355 root 1.3 XFreeGC (s->display->dpy, gc);
356 root 1.20
357     return img;
358 root 1.3 }
359    
360     rxvt_img *
361 root 1.19 rxvt_img::sub_rect (int x, int y, int width, int height, int repeat)
362     {
363     rxvt_img *img = new rxvt_img (s, format, width, height);
364    
365     Display *dpy = s->display->dpy;
366     XRenderPictureAttributes pa;
367     pa.repeat = repeat;
368     Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
369     Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
370    
371     XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height);
372    
373     XRenderFreePicture (dpy, src);
374     XRenderFreePicture (dpy, dst);
375    
376     return img;
377     }
378    
379     rxvt_img *
380     rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat)
381 root 1.3 {
382 root 1.12 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
383    
384     Display *dpy = s->display->dpy;
385 root 1.13 XRenderPictureAttributes pa;
386     pa.repeat = repeat;
387     Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
388     Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
389 root 1.12
390     XTransform xfrm;
391    
392     for (int i = 0; i < 3; ++i)
393     for (int j = 0; j < 3; ++j)
394     xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
395    
396 root 1.17 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
397 root 1.12 XRenderSetPictureTransform (dpy, src, &xfrm);
398     XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
399    
400     XRenderFreePicture (dpy, src);
401     XRenderFreePicture (dpy, dst);
402    
403     return img;
404 root 1.3 }
405    
406     rxvt_img *
407     rxvt_img::scale (int new_width, int new_height)
408     {
409 root 1.11 double matrix[9] = {
410 root 1.16 w / (double)new_width, 0, 0,
411     0, h / (double)new_height, 0,
412 root 1.11 0, 0, 1
413     };
414    
415 root 1.19 return transform (new_width, new_height, matrix);
416 root 1.3 }
417    
418 sf-exg 1.7 rxvt_img *
419 root 1.19 rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat)
420 root 1.17 {
421     double s = sin (phi);
422     double c = cos (phi);
423    
424     double matrix[9] = {
425     c, -s, -c * x + s * y + x,
426     s, c, -s * x - c * y + y,
427     0, 0, 1
428     };
429    
430 root 1.19 return transform (new_width, new_height, matrix, repeat);
431 root 1.17 }
432    
433     rxvt_img *
434 sf-exg 1.7 rxvt_img::convert_to (XRenderPictFormat *new_format)
435     {
436 sf-exg 1.10 rxvt_img *img = new rxvt_img (s, new_format, w, h);
437 root 1.9
438 sf-exg 1.7 Display *dpy = s->display->dpy;
439 root 1.12 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
440 root 1.8 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
441 sf-exg 1.7
442     XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
443    
444     XRenderFreePicture (dpy, src);
445     XRenderFreePicture (dpy, dst);
446    
447     return img;
448     }
449 root 1.3
450 root 1.1 #endif
451