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.5 by root, Sun Jun 3 18:33:46 2012 UTC vs.
Revision 1.25 by sf-exg, Tue Jun 5 22:00:15 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}
13: 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)
14{ 17{
15} 18}
16 19
17rxvt_img * 20rxvt_img *
21rxvt_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
52rxvt_img *
18rxvt_img::new_from_file (rxvt_screen *s, const char *filename) 53rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
19{ 54{
20 GError *err; 55 GError *err = 0;
21 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); 56 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
22 57
23 if (!pb) 58 if (!pb)
24 return 0; 59 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
25 60
26 rxvt_img *img = new rxvt_img ( 61 rxvt_img *img = new rxvt_img (
27 s, 62 s,
28 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 63 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
29 gdk_pixbuf_get_width (pb), 64 gdk_pixbuf_get_width (pb),
30 gdk_pixbuf_get_height (pb) 65 gdk_pixbuf_get_height (pb)
31 ); 66 );
32 67
33 img->render (pb, 0, 0, img->w, img->h, 0, 0); 68 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
34 69
35 return img; 70 return img;
36} 71}
37 72
38rxvt_img::~rxvt_img () 73rxvt_img::~rxvt_img ()
39{ 74{
40 if (!shared) 75 if (!shared)
41 XFreePixmap (s->display->dpy, pm); 76 XFreePixmap (s->display->dpy, pm);
77}
78
79void
80rxvt_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;
42} 91}
43 92
44void 93void
45rxvt_img::fill (const rxvt_color &c) 94rxvt_img::fill (const rxvt_color &c)
46{ 95{
49 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 98 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv);
50 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 99 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h);
51 XFreeGC (s->display->dpy, gc); 100 XFreeGC (s->display->dpy, gc);
52} 101}
53 102
54void 103static void
104get_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
124rxvt_img *
55rxvt_img::blur (int rh, int rv) 125rxvt_img::blur (int rh, int rv)
56{ 126{
57 //TODO 127 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
128 return clone ();
129
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 rxvt_img *img = new rxvt_img (s, format, w, h);
135
136 XRenderPictureAttributes pa;
137
138 pa.repeat = RepeatPad;
139 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
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 tmp,
157 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 tmp,
170 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 XRenderFreePicture (dpy, tmp);
183
184 return img;
185}
186
187static Picture
188create_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;
58} 200}
59 201
60void 202void
61rxvt_img::brightness (double r, double g, double b, double a) 203rxvt_img::brightness (double r, double g, double b, double a)
62{ 204{
63 //TODO 205 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
218 XRenderFreePicture (dpy, src);
219 XRenderFreePicture (dpy, dst);
64} 220}
65 221
66void 222void
67rxvt_img::contrast (double r, double g, double b, double a) 223rxvt_img::contrast (double r, double g, double b, double a)
68{ 224{
69 //TODO 225 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
70} 226 return;
71 227
72void 228 Display *dpy = s->display->dpy;
229 Picture src = create_xrender_mask (dpy, pm, True);
230 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
231
232 XRenderColor mask_c;
233 mask_c.red = float_to_component (r);
234 mask_c.green = float_to_component (g);
235 mask_c.blue = float_to_component (b);
236 mask_c.alpha = float_to_component (a);
237 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
238
239 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
240
241 XRenderFreePicture (dpy, src);
242 XRenderFreePicture (dpy, dst);
243}
244
245bool
73rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 246rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
74{ 247{
75 //TODO 248 Display *dpy = s->display->dpy;
76}
77 249
250 if (s->visual->c_class != TrueColor)
251 return false;
252
253 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
254
255 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
256 green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
257 blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
258 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
259
260 int width_r = ecb_popcount32 (red_mask);
261 int width_g = ecb_popcount32 (green_mask);
262 int width_b = ecb_popcount32 (blue_mask);
263 int width_a = ecb_popcount32 (alpha_mask);
264
265 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
266 return false;
267
268 int sh_r = ecb_ctz32 (red_mask);
269 int sh_g = ecb_ctz32 (green_mask);
270 int sh_b = ecb_ctz32 (blue_mask);
271 int sh_a = ecb_ctz32 (alpha_mask);
272
273 if (width > 32767 || height > 32767)
274 return false;
275
276 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
277 width, height, 32, 0);
278 if (!ximage)
279 return false;
280
281 if (height > INT_MAX / ximage->bytes_per_line
282 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
283 {
284 XDestroyImage (ximage);
285 return false;
286 }
287
288 GC gc = XCreateGC (dpy, pm, 0, 0);
289
290 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
291
292 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
293 int channels = gdk_pixbuf_get_n_channels (pixbuf);
294 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
295 char *line = ximage->data;
296
297 for (int y = 0; y < height; y++)
298 {
299 for (int x = 0; x < width; x++)
300 {
301 unsigned char *pixel = row + x * channels;
302 uint32_t value;
303 unsigned char r, g, b, a;
304
305 if (channels == 4)
306 {
307 a = pixel[3];
308 r = pixel[0] * a / 0xff;
309 g = pixel[1] * a / 0xff;
310 b = pixel[2] * a / 0xff;
311 }
312 else
313 {
314 a = 0xff;
315 r = pixel[0];
316 g = pixel[1];
317 b = pixel[2];
318 }
319
320 value = ((r >> (8 - width_r)) << sh_r)
321 | ((g >> (8 - width_g)) << sh_g)
322 | ((b >> (8 - width_b)) << sh_b)
323 | ((a >> (8 - width_a)) << sh_a);
324
325 if (ximage->bits_per_pixel == 32)
326 ((uint32_t *)line)[x] = value;
327 else
328 XPutPixel (ximage, x, y, value);
329 }
330
331 row += rowstride;
332 line += ximage->bytes_per_line;
333 }
334
335 XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
336 XDestroyImage (ximage);
337 XFreeGC (dpy, gc);
338
339 return true;
340}
341
78rxvt_img * 342rxvt_img *
79rxvt_img::copy () 343rxvt_img::clone ()
80{ 344{
345 rxvt_img *img = new rxvt_img (s, format, w, h);
346
81 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 347 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
82 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth);
83 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); 348 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0);
84 XFreeGC (s->display->dpy, gc); 349 XFreeGC (s->display->dpy, gc);
85 return new rxvt_img (s, format, w, h, pm2);
86}
87 350
351 return img;
352}
353
88rxvt_img * 354rxvt_img *
355rxvt_img::sub_rect (int x, int y, int width, int height, int repeat)
356{
357 rxvt_img *img = new rxvt_img (s, format, width, height);
358
359 Display *dpy = s->display->dpy;
360 XRenderPictureAttributes pa;
361 pa.repeat = repeat;
362 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
363 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
364
365 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height);
366
367 XRenderFreePicture (dpy, src);
368 XRenderFreePicture (dpy, dst);
369
370 return img;
371}
372
373rxvt_img *
89rxvt_img::transform (int new_width, int new_height, double matrix[16]) 374rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat)
90{ 375{
91 //TODO 376 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
377
378 Display *dpy = s->display->dpy;
379 XRenderPictureAttributes pa;
380 pa.repeat = repeat;
381 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
382 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
383
384 XTransform xfrm;
385
386 for (int i = 0; i < 3; ++i)
387 for (int j = 0; j < 3; ++j)
388 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
389
390 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
391 XRenderSetPictureTransform (dpy, src, &xfrm);
392 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
393
394 XRenderFreePicture (dpy, src);
395 XRenderFreePicture (dpy, dst);
396
397 return img;
92} 398}
93 399
94rxvt_img * 400rxvt_img *
95rxvt_img::scale (int new_width, int new_height) 401rxvt_img::scale (int new_width, int new_height)
96{ 402{
97 // use transform 403 double matrix[9] = {
98 //TODO 404 w / (double)new_width, 0, 0,
99} 405 0, h / (double)new_height, 0,
406 0, 0, 1
407 };
100 408
409 return transform (new_width, new_height, matrix);
410}
411
412rxvt_img *
413rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat)
414{
415 double s = sin (phi);
416 double c = cos (phi);
417
418 double matrix[9] = {
419 c, -s, -c * x + s * y + x,
420 s, c, -s * x - c * y + y,
421 0, 0, 1
422 };
423
424 return transform (new_width, new_height, matrix, repeat);
425}
426
427rxvt_img *
428rxvt_img::convert_to (XRenderPictFormat *new_format)
429{
430 rxvt_img *img = new rxvt_img (s, new_format, w, h);
431
432 Display *dpy = s->display->dpy;
433 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
434 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
435
436 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
437
438 XRenderFreePicture (dpy, src);
439 XRenderFreePicture (dpy, dst);
440
441 return img;
442}
443
444rxvt_img *
445rxvt_img::blend (rxvt_img *img, double factor)
446{
447 rxvt_img *img2 = clone ();
448 Display *dpy = s->display->dpy;
449 Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
450 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
451 Picture mask = create_xrender_mask (dpy, img->pm, False);
452
453 XRenderColor mask_c;
454
455 mask_c.alpha = float_to_component (factor);
456 mask_c.red =
457 mask_c.green =
458 mask_c.blue = 0;
459 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
460
461 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h);
462
463 XRenderFreePicture (dpy, src);
464 XRenderFreePicture (dpy, dst);
465 XRenderFreePicture (dpy, mask);
466
467 return img2;
468}
101 469
102#endif 470#endif
103 471

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines