ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.43
Committed: Thu Jun 7 13:02:50 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.42: +3 -0 lines
Log Message:
Make rxvt_img::scale return a cloned image if no scaling is needed.

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.41 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
8     : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
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     return pic;
120 root 1.1 }
121    
122     void
123 root 1.11 rxvt_img::unshare ()
124     {
125 root 1.34 if (ref->cnt == 1 && ref->ours)
126 root 1.11 return;
127    
128 root 1.34 //TODO: maybe should reify instead
129     Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
130 root 1.32 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
131 root 1.34 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
132 root 1.32 XFreeGC (s->display->dpy, gc);
133 root 1.11
134 root 1.32 destroy ();
135 root 1.11
136 root 1.32 pm = pm2;
137 root 1.34 ref = new pixref (ref->w, ref->h);
138 root 1.11 }
139    
140     void
141 root 1.1 rxvt_img::fill (const rxvt_color &c)
142     {
143     XGCValues gcv;
144     gcv.foreground = c;
145     GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv);
146     XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h);
147 root 1.3 XFreeGC (s->display->dpy, gc);
148 root 1.1 }
149    
150 sf-exg 1.6 static void
151     get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
152     {
153     double sigma = radius / 2.0;
154     double scale = sqrt (2.0 * M_PI) * sigma;
155     double sum = 0.0;
156    
157     for (int i = 0; i < width; i++)
158     {
159     double x = i - width / 2;
160     kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
161     sum += kernel[i];
162     }
163    
164     params[0] = XDoubleToFixed (width);
165     params[1] = XDoubleToFixed (1);
166    
167     for (int i = 0; i < width; i++)
168     params[i+2] = XDoubleToFixed (kernel[i] / sum);
169     }
170    
171 sf-exg 1.22 rxvt_img *
172 root 1.3 rxvt_img::blur (int rh, int rv)
173     {
174 sf-exg 1.6 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
175 sf-exg 1.22 return clone ();
176 sf-exg 1.6
177     Display *dpy = s->display->dpy;
178     int size = max (rh, rv) * 2 + 1;
179     double *kernel = (double *)malloc (size * sizeof (double));
180     XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
181 root 1.41 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
182 root 1.32 img->alloc ();
183 sf-exg 1.6
184 root 1.34 Picture src = src_picture ();
185    
186 sf-exg 1.6 XRenderPictureAttributes pa;
187     pa.repeat = RepeatPad;
188 sf-exg 1.22 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
189    
190     Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
191     Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
192     XFreePixmap (dpy, tmp_pm);
193 sf-exg 1.6
194     if (kernel && params)
195     {
196     size = rh * 2 + 1;
197     get_gaussian_kernel (rh, size, kernel, params);
198    
199     XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
200     XRenderComposite (dpy,
201     PictOpSrc,
202     src,
203     None,
204 sf-exg 1.22 tmp,
205 sf-exg 1.6 0, 0,
206     0, 0,
207     0, 0,
208     w, h);
209    
210     size = rv * 2 + 1;
211     get_gaussian_kernel (rv, size, kernel, params);
212     ::swap (params[0], params[1]);
213    
214     XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
215     XRenderComposite (dpy,
216     PictOpSrc,
217 sf-exg 1.22 tmp,
218 sf-exg 1.6 None,
219     dst,
220     0, 0,
221     0, 0,
222     0, 0,
223     w, h);
224     }
225    
226     free (kernel);
227     free (params);
228     XRenderFreePicture (dpy, src);
229     XRenderFreePicture (dpy, dst);
230 sf-exg 1.22 XRenderFreePicture (dpy, tmp);
231    
232     return img;
233 sf-exg 1.6 }
234    
235     static Picture
236     create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
237     {
238     Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
239    
240     XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
241     XRenderPictureAttributes pa;
242     pa.repeat = True;
243     Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
244    
245     XFreePixmap (dpy, pixmap);
246    
247     return mask;
248 root 1.3 }
249    
250     void
251 sf-exg 1.31 rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
252 root 1.3 {
253 sf-exg 1.6 Display *dpy = s->display->dpy;
254     Picture src = create_xrender_mask (dpy, pm, True);
255     Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
256    
257     XRenderColor mask_c;
258 sf-exg 1.31 mask_c.red = r;
259     mask_c.green = g;
260     mask_c.blue = b;
261     mask_c.alpha = a;
262 sf-exg 1.6 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
263    
264     XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
265 sf-exg 1.23
266     XRenderFreePicture (dpy, src);
267     XRenderFreePicture (dpy, dst);
268 root 1.3 }
269    
270     void
271 sf-exg 1.31 rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
272 root 1.1 {
273 sf-exg 1.6 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
274     return;
275    
276     Display *dpy = s->display->dpy;
277     Picture src = create_xrender_mask (dpy, pm, True);
278     Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
279    
280     XRenderColor mask_c;
281 sf-exg 1.31 mask_c.red = r;
282     mask_c.green = g;
283     mask_c.blue = b;
284     mask_c.alpha = a;
285 sf-exg 1.6 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
286    
287     XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
288 sf-exg 1.23
289     XRenderFreePicture (dpy, src);
290     XRenderFreePicture (dpy, dst);
291 root 1.1 }
292    
293 root 1.14 bool
294     rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
295 root 1.3 {
296 root 1.14 Display *dpy = s->display->dpy;
297    
298     if (s->visual->c_class != TrueColor)
299     return false;
300    
301     uint32_t red_mask, green_mask, blue_mask, alpha_mask;
302    
303 sf-exg 1.18 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
304     green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
305     blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
306     alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
307 root 1.14
308     int width_r = ecb_popcount32 (red_mask);
309     int width_g = ecb_popcount32 (green_mask);
310     int width_b = ecb_popcount32 (blue_mask);
311     int width_a = ecb_popcount32 (alpha_mask);
312    
313     if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
314     return false;
315    
316     int sh_r = ecb_ctz32 (red_mask);
317     int sh_g = ecb_ctz32 (green_mask);
318     int sh_b = ecb_ctz32 (blue_mask);
319     int sh_a = ecb_ctz32 (alpha_mask);
320    
321     if (width > 32767 || height > 32767)
322     return false;
323    
324 sf-exg 1.18 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
325 root 1.14 width, height, 32, 0);
326     if (!ximage)
327     return false;
328    
329     if (height > INT_MAX / ximage->bytes_per_line
330     || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
331     {
332     XDestroyImage (ximage);
333     return false;
334     }
335    
336     GC gc = XCreateGC (dpy, pm, 0, 0);
337    
338     ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
339    
340     int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
341     int channels = gdk_pixbuf_get_n_channels (pixbuf);
342     unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
343     char *line = ximage->data;
344    
345     for (int y = 0; y < height; y++)
346     {
347     for (int x = 0; x < width; x++)
348     {
349     unsigned char *pixel = row + x * channels;
350     uint32_t value;
351     unsigned char r, g, b, a;
352    
353     if (channels == 4)
354     {
355     a = pixel[3];
356 sf-exg 1.25 r = pixel[0] * a / 0xff;
357     g = pixel[1] * a / 0xff;
358     b = pixel[2] * a / 0xff;
359 root 1.14 }
360     else
361     {
362     a = 0xff;
363     r = pixel[0];
364     g = pixel[1];
365     b = pixel[2];
366     }
367    
368     value = ((r >> (8 - width_r)) << sh_r)
369     | ((g >> (8 - width_g)) << sh_g)
370     | ((b >> (8 - width_b)) << sh_b)
371     | ((a >> (8 - width_a)) << sh_a);
372    
373     if (ximage->bits_per_pixel == 32)
374     ((uint32_t *)line)[x] = value;
375     else
376     XPutPixel (ximage, x, y, value);
377     }
378    
379     row += rowstride;
380     line += ximage->bytes_per_line;
381     }
382    
383     XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
384     XDestroyImage (ximage);
385     XFreeGC (dpy, gc);
386    
387     return true;
388 root 1.3 }
389    
390     rxvt_img *
391 root 1.11 rxvt_img::clone ()
392 root 1.3 {
393 root 1.32 return new rxvt_img (*this);
394 root 1.3 }
395    
396     rxvt_img *
397 root 1.33 rxvt_img::reify ()
398 root 1.19 {
399 root 1.35 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
400     return clone ();
401    
402 root 1.40 Display *dpy = s->display->dpy;
403    
404     bool alpha = !format->direct.alphaMask
405     && (x || y)
406     && repeat == RepeatNone;
407    
408 root 1.41 rxvt_img *img = new rxvt_img (s, alpha ? XRenderFindStandardFormat (dpy, PictStandardARGB32) : format, 0, 0, w, h, repeat);
409 root 1.32 img->alloc ();
410 root 1.19
411 root 1.34 Picture src = src_picture ();
412     Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
413 root 1.33
414 root 1.42 if (alpha)
415     {
416     XRenderColor rc = { 0, 0, 0, 0 };
417     XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
418     XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h);
419     }
420     else
421     XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
422    
423 root 1.33 XRenderFreePicture (dpy, src);
424     XRenderFreePicture (dpy, dst);
425 root 1.19
426 root 1.33 return img;
427     }
428 root 1.19
429 root 1.33 rxvt_img *
430     rxvt_img::sub_rect (int x, int y, int width, int height)
431     {
432     rxvt_img *img = clone ();
433    
434     img->x += x;
435     img->y += y;
436 root 1.19
437 root 1.37 if (w != width || h != height)
438     {
439     img->w = width;
440     img->h = height;
441    
442 root 1.39 rxvt_img *img2 = img->reify ();
443     delete img;
444     img = img2;
445 root 1.37 }
446 root 1.36
447 root 1.19 return img;
448     }
449    
450     rxvt_img *
451 root 1.30 rxvt_img::transform (int new_width, int new_height, double matrix[9])
452 root 1.3 {
453 root 1.41 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat);
454 root 1.32 img->alloc ();
455 root 1.12
456     Display *dpy = s->display->dpy;
457 root 1.34 Picture src = src_picture ();
458     Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
459 root 1.12
460     XTransform xfrm;
461    
462     for (int i = 0; i < 3; ++i)
463     for (int j = 0; j < 3; ++j)
464     xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
465    
466 root 1.33 xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO
467     xfrm.matrix [0][3] += XDoubleToFixed (y);
468    
469 root 1.17 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
470 root 1.12 XRenderSetPictureTransform (dpy, src, &xfrm);
471     XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
472    
473     XRenderFreePicture (dpy, src);
474     XRenderFreePicture (dpy, dst);
475    
476     return img;
477 root 1.3 }
478    
479     rxvt_img *
480     rxvt_img::scale (int new_width, int new_height)
481     {
482 sf-exg 1.43 if (w == new_width && h == new_height)
483     return clone ();
484    
485 root 1.11 double matrix[9] = {
486 root 1.16 w / (double)new_width, 0, 0,
487     0, h / (double)new_height, 0,
488 root 1.11 0, 0, 1
489     };
490    
491 root 1.42 int old_repeat_mode = repeat;
492     repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems
493    
494     rxvt_img *img = transform (new_width, new_height, matrix);
495    
496     repeat = old_repeat_mode;
497     img->repeat = repeat;
498    
499     return img;
500 root 1.3 }
501    
502 sf-exg 1.7 rxvt_img *
503 root 1.30 rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
504 root 1.17 {
505     double s = sin (phi);
506     double c = cos (phi);
507    
508     double matrix[9] = {
509     c, -s, -c * x + s * y + x,
510     s, c, -s * x - c * y + y,
511     0, 0, 1
512     };
513    
514 root 1.30 return transform (new_width, new_height, matrix);
515 root 1.17 }
516    
517     rxvt_img *
518 root 1.27 rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
519 sf-exg 1.7 {
520 root 1.32 if (new_format == format)
521     return clone ();
522    
523 root 1.41 rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h, repeat);
524 root 1.32 img->alloc ();
525 root 1.9
526 root 1.42 printf ("convert %d to %d\n", format->direct.alphaMask, new_format->direct.alphaMask);//D
527    
528 sf-exg 1.7 Display *dpy = s->display->dpy;
529 root 1.34 Picture src = src_picture ();
530 root 1.8 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
531 root 1.27 int op = PictOpSrc;
532 sf-exg 1.7
533 root 1.29 if (format->direct.alphaMask && !new_format->direct.alphaMask)
534 root 1.27 {
535     // does it have to be that complicated
536     rgba c;
537     bg.get (c);
538    
539     XRenderColor rc = { c.r, c.g, c.b, 0xffff };
540 root 1.28 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
541 root 1.27
542     op = PictOpOver;
543     }
544    
545     XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
546 sf-exg 1.7
547     XRenderFreePicture (dpy, src);
548     XRenderFreePicture (dpy, dst);
549    
550     return img;
551     }
552 root 1.3
553 sf-exg 1.24 rxvt_img *
554     rxvt_img::blend (rxvt_img *img, double factor)
555     {
556     rxvt_img *img2 = clone ();
557     Display *dpy = s->display->dpy;
558 sf-exg 1.38 Picture src = img->src_picture ();
559 sf-exg 1.24 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
560     Picture mask = create_xrender_mask (dpy, img->pm, False);
561    
562     XRenderColor mask_c;
563    
564     mask_c.alpha = float_to_component (factor);
565     mask_c.red =
566     mask_c.green =
567     mask_c.blue = 0;
568     XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
569    
570     XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h);
571    
572     XRenderFreePicture (dpy, src);
573     XRenderFreePicture (dpy, dst);
574     XRenderFreePicture (dpy, mask);
575    
576     return img2;
577     }
578    
579 root 1.1 #endif
580