ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.35
Committed: Thu Jun 7 08:36:53 2012 UTC (11 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.34: +3 -3 lines
Log Message:
not quite

File Contents

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