ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.26
Committed: Wed Jun 6 18:13:03 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.25: +2 -0 lines
Log Message:
Fix leak.

File Contents

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