ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.4 by root, Sun Jun 3 18:14:13 2012 UTC vs.
Revision 1.6 by sf-exg, Sun Jun 3 18:49:55 2012 UTC

1#include <math.h>
1#include "../config.h" 2#include "../config.h"
2#include "rxvt.h" 3#include "rxvt.h"
3 4
4#if HAVE_IMG 5#if HAVE_IMG
5 6
7#define float_to_component(d) ((d) * 65535.99)
8
6rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) 9rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height)
7: s(screen), w(width), h(height), format(format), shared(false) 10: s(screen), w(width), h(height), format(format), shared(false)
8{ 11{
9 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 12 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
10} 13}
20 GError *err; 23 GError *err;
21 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); 24 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
22 25
23 if (!pb) 26 if (!pb)
24 return 0; 27 return 0;
25
26 int w = gdk_pixbuf_get_width (pb);
27 int h = gdk_pixbuf_get_height (pb);
28 28
29 rxvt_img *img = new rxvt_img ( 29 rxvt_img *img = new rxvt_img (
30 s, 30 s,
31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
32 gdk_pixbuf_get_width (pb), 32 gdk_pixbuf_get_width (pb),
33 gdk_pixbuf_get_height (pb) 33 gdk_pixbuf_get_height (pb)
34 ); 34 );
35 35
36 img->render (pb, 0, 0, w, h, 0, 0); 36 img->render (pb, 0, 0, img->w, img->h, 0, 0);
37 37
38 return img; 38 return img;
39} 39}
40 40
41rxvt_img::~rxvt_img () 41rxvt_img::~rxvt_img ()
52 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 52 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv);
53 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 53 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h);
54 XFreeGC (s->display->dpy, gc); 54 XFreeGC (s->display->dpy, gc);
55} 55}
56 56
57static void
58get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
59{
60 double sigma = radius / 2.0;
61 double scale = sqrt (2.0 * M_PI) * sigma;
62 double sum = 0.0;
63
64 for (int i = 0; i < width; i++)
65 {
66 double x = i - width / 2;
67 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
68 sum += kernel[i];
69 }
70
71 params[0] = XDoubleToFixed (width);
72 params[1] = XDoubleToFixed (1);
73
74 for (int i = 0; i < width; i++)
75 params[i+2] = XDoubleToFixed (kernel[i] / sum);
76}
77
57void 78void
58rxvt_img::blur (int rh, int rv) 79rxvt_img::blur (int rh, int rv)
59{ 80{
60 //TODO 81 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
82 return;
83
84 Display *dpy = s->display->dpy;
85 int size = max (rh, rv) * 2 + 1;
86 double *kernel = (double *)malloc (size * sizeof (double));
87 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
88
89 XRenderPictureAttributes pa;
90
91 pa.repeat = RepeatPad;
92 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
93 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth);
94 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
95 XFreePixmap (dpy, tmp);
96
97 if (kernel && params)
98 {
99 size = rh * 2 + 1;
100 get_gaussian_kernel (rh, size, kernel, params);
101
102 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
103 XRenderComposite (dpy,
104 PictOpSrc,
105 src,
106 None,
107 dst,
108 0, 0,
109 0, 0,
110 0, 0,
111 w, h);
112
113 ::swap (src, dst);
114
115 size = rv * 2 + 1;
116 get_gaussian_kernel (rv, size, kernel, params);
117 ::swap (params[0], params[1]);
118
119 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
120 XRenderComposite (dpy,
121 PictOpSrc,
122 src,
123 None,
124 dst,
125 0, 0,
126 0, 0,
127 0, 0,
128 w, h);
129 }
130
131 free (kernel);
132 free (params);
133 XRenderFreePicture (dpy, src);
134 XRenderFreePicture (dpy, dst);
135}
136
137static Picture
138create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
139{
140 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
141
142 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
143 XRenderPictureAttributes pa;
144 pa.repeat = True;
145 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
146
147 XFreePixmap (dpy, pixmap);
148
149 return mask;
61} 150}
62 151
63void 152void
64rxvt_img::brightness (double r, double g, double b, double a) 153rxvt_img::brightness (double r, double g, double b, double a)
65{ 154{
66 //TODO 155 Display *dpy = s->display->dpy;
156 Picture src = create_xrender_mask (dpy, pm, True);
157 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
158
159 XRenderColor mask_c;
160 mask_c.red = float_to_component (r);
161 mask_c.green = float_to_component (g);
162 mask_c.blue = float_to_component (b);
163 mask_c.alpha = float_to_component (a);
164 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
165
166 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
67} 167}
68 168
69void 169void
70rxvt_img::contrast (double r, double g, double b, double a) 170rxvt_img::contrast (double r, double g, double b, double a)
71{ 171{
72 //TODO 172 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
173 return;
174
175 Display *dpy = s->display->dpy;
176 Picture src = create_xrender_mask (dpy, pm, True);
177 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
178
179 XRenderColor mask_c;
180 mask_c.red = float_to_component (r);
181 mask_c.green = float_to_component (g);
182 mask_c.blue = float_to_component (b);
183 mask_c.alpha = float_to_component (a);
184 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
185
186 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
73} 187}
74 188
75void 189void
76rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 190rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
77{ 191{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines