ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.32
Committed: Thu Jun 7 07:53:12 2012 UTC (11 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.31: +50 -20 lines
Log Message:
preliminary recounting

File Contents

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