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.3 by root, Sun Jun 3 10:14:44 2012 UTC vs.
Revision 1.11 by root, Mon Jun 4 15:15:49 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) 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}
11 14
12rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) 15rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap)
13: s(screen), pm(pixmap), w(width), h(height), format(format) 16: s(screen), pm(pixmap), w(width), h(height), format(format), shared(false)
14{ 17{
18}
19
20rxvt_img *
21rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
22{
23 GError *err = 0;
24 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
25
26 if (!pb)
27 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
28
29 rxvt_img *img = new rxvt_img (
30 s,
31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
32 gdk_pixbuf_get_width (pb),
33 gdk_pixbuf_get_height (pb)
34 );
35
36 img->render (pb, 0, 0, img->w, img->h, 0, 0);
37
38 return img;
15} 39}
16 40
17rxvt_img::~rxvt_img () 41rxvt_img::~rxvt_img ()
18{ 42{
19 if (pm) 43 if (!shared)
20 XFreePixmap (s->display->dpy, pm); 44 XFreePixmap (s->display->dpy, pm);
45}
46
47void
48rxvt_img::unshare ()
49{
50 if (!shared)
51 return;
52
53 rxvt_img *img = clone ();
54
55 ::swap (pm , img->pm);
56 ::swap (shared, img->shared);
57
58 delete img;
21} 59}
22 60
23void 61void
24rxvt_img::fill (const rxvt_color &c) 62rxvt_img::fill (const rxvt_color &c)
25{ 63{
28 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 66 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv);
29 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 67 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h);
30 XFreeGC (s->display->dpy, gc); 68 XFreeGC (s->display->dpy, gc);
31} 69}
32 70
71static void
72get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
73{
74 double sigma = radius / 2.0;
75 double scale = sqrt (2.0 * M_PI) * sigma;
76 double sum = 0.0;
77
78 for (int i = 0; i < width; i++)
79 {
80 double x = i - width / 2;
81 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
82 sum += kernel[i];
83 }
84
85 params[0] = XDoubleToFixed (width);
86 params[1] = XDoubleToFixed (1);
87
88 for (int i = 0; i < width; i++)
89 params[i+2] = XDoubleToFixed (kernel[i] / sum);
90}
91
33void 92void
34rxvt_img::blur (int rh, int rv) 93rxvt_img::blur (int rh, int rv)
35{ 94{
95 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
96 return;
97
98 Display *dpy = s->display->dpy;
99 int size = max (rh, rv) * 2 + 1;
100 double *kernel = (double *)malloc (size * sizeof (double));
101 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
102
103 XRenderPictureAttributes pa;
104
105 pa.repeat = RepeatPad;
106 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
107 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth);
108 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
109 XFreePixmap (dpy, tmp);
110
111 if (kernel && params)
112 {
113 size = rh * 2 + 1;
114 get_gaussian_kernel (rh, size, kernel, params);
115
116 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
117 XRenderComposite (dpy,
118 PictOpSrc,
119 src,
120 None,
121 dst,
122 0, 0,
123 0, 0,
124 0, 0,
125 w, h);
126
127 ::swap (src, dst);
128
129 size = rv * 2 + 1;
130 get_gaussian_kernel (rv, size, kernel, params);
131 ::swap (params[0], params[1]);
132
133 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
134 XRenderComposite (dpy,
135 PictOpSrc,
136 src,
137 None,
138 dst,
139 0, 0,
140 0, 0,
141 0, 0,
142 w, h);
143 }
144
145 free (kernel);
146 free (params);
147 XRenderFreePicture (dpy, src);
148 XRenderFreePicture (dpy, dst);
149}
150
151static Picture
152create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
153{
154 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
155
156 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
157 XRenderPictureAttributes pa;
158 pa.repeat = True;
159 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
160
161 XFreePixmap (dpy, pixmap);
162
163 return mask;
164}
165
166void
167rxvt_img::brightness (double r, double g, double b, double a)
168{
169 Display *dpy = s->display->dpy;
170 Picture src = create_xrender_mask (dpy, pm, True);
171 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
172
173 XRenderColor mask_c;
174 mask_c.red = float_to_component (r);
175 mask_c.green = float_to_component (g);
176 mask_c.blue = float_to_component (b);
177 mask_c.alpha = float_to_component (a);
178 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
179
180 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
181}
182
183void
184rxvt_img::contrast (double r, double g, double b, double a)
185{
186 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
187 return;
188
189 Display *dpy = s->display->dpy;
190 Picture src = create_xrender_mask (dpy, pm, True);
191 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
192
193 XRenderColor mask_c;
194 mask_c.red = float_to_component (r);
195 mask_c.green = float_to_component (g);
196 mask_c.blue = float_to_component (b);
197 mask_c.alpha = float_to_component (a);
198 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
199
200 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
201}
202
203void
204rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
205{
36 //TODO 206 //TODO
37} 207}
38 208
39void
40rxvt_img::brightness (double r, double g, double b, double a)
41{
42 //TODO
43}
44
45void
46rxvt_img::contrast (double r, double g, double b, double a)
47{
48 //TODO
49}
50
51void
52rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
53{
54 //TODO
55}
56
57rxvt_img * 209rxvt_img *
58rxvt_img::copy () 210rxvt_img::clone ()
59{ 211{
60 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 212 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
61 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth); 213 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth);
62 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); 214 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0);
63 XFreeGC (s->display->dpy, gc); 215 XFreeGC (s->display->dpy, gc);
64 return new rxvt_img (s, format, w, h, pm2); 216 return new rxvt_img (s, format, w, h, pm2);
65} 217}
66 218
67rxvt_img * 219rxvt_img *
68rxvt_img::transform (int new_width, int new_height, double matrix[16]) 220rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9])
69{ 221{
70 //TODO 222 //TODO
71} 223}
72 224
73rxvt_img * 225rxvt_img *
74rxvt_img::scale (int new_width, int new_height) 226rxvt_img::scale (int new_width, int new_height)
75{ 227{
76 // use transform 228 double matrix[9] = {
77 //TODO 229 new_width / (double)w, 0, 0,
78} 230 0, new_height / (double)h, 0,
231 0, 0, 1
232 };
79 233
234 return transform (new_width, new_height, RepeatNormal, matrix);
235}
236
237rxvt_img *
238rxvt_img::convert_to (XRenderPictFormat *new_format)
239{
240 rxvt_img *img = new rxvt_img (s, new_format, w, h);
241
242 Display *dpy = s->display->dpy;
243 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
244 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
245
246 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
247
248 XRenderFreePicture (dpy, src);
249 XRenderFreePicture (dpy, dst);
250
251 return img;
252}
80 253
81#endif 254#endif
82 255

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines