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.18 by sf-exg, Tue Jun 5 12:30:37 2012 UTC vs.
Revision 1.32 by root, Thu Jun 7 07:53:12 2012 UTC

2#include "../config.h" 2#include "../config.h"
3#include "rxvt.h" 3#include "rxvt.h"
4 4
5#if HAVE_IMG 5#if HAVE_IMG
6 6
7#define float_to_component(d) ((d) * 65535.99)
8
9rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) 7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height)
10: s(screen), w(width), h(height), format(format), shared(false) 8: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal),
9 pm(0), refcnt(0)
11{ 10{
12 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
13} 11}
14 12
13rxvt_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
15rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) 21rxvt_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) 22: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
17{ 23{
24}
25#endif
26
27rxvt_img *
28rxvt_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;
18} 56}
19 57
20rxvt_img * 58rxvt_img *
21rxvt_img::new_from_file (rxvt_screen *s, const char *filename) 59rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
22{ 60{
30 s, 68 s,
31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 69 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
32 gdk_pixbuf_get_width (pb), 70 gdk_pixbuf_get_width (pb),
33 gdk_pixbuf_get_height (pb) 71 gdk_pixbuf_get_height (pb)
34 ); 72 );
35 73 img->alloc ();
36 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); 74 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
37 75
76 g_object_unref (pb);
77
38 return img; 78 return img;
39} 79}
40 80
81void
82rxvt_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
41rxvt_img::~rxvt_img () 93rxvt_img::~rxvt_img ()
42{ 94{
43 if (!shared) 95 destroy ();
44 XFreePixmap (s->display->dpy, pm); 96}
97
98void
99rxvt_img::alloc ()
100{
101 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
102 refcnt = new int (1);
45} 103}
46 104
47void 105void
48rxvt_img::unshare () 106rxvt_img::unshare ()
49{ 107{
50 if (!shared) 108 if (refcnt && *refcnt == 1)
51 return; 109 return;
52 110
53 rxvt_img *img = clone (); 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);
54 115
55 ::swap (pm , img->pm); 116 destroy ();
56 ::swap (shared, img->shared);
57 117
58 delete img; 118 pm = pm2;
119 refcnt = new int (1);
59} 120}
60 121
61void 122void
62rxvt_img::fill (const rxvt_color &c) 123rxvt_img::fill (const rxvt_color &c)
63{ 124{
87 148
88 for (int i = 0; i < width; i++) 149 for (int i = 0; i < width; i++)
89 params[i+2] = XDoubleToFixed (kernel[i] / sum); 150 params[i+2] = XDoubleToFixed (kernel[i] / sum);
90} 151}
91 152
92void 153rxvt_img *
93rxvt_img::blur (int rh, int rv) 154rxvt_img::blur (int rh, int rv)
94{ 155{
95 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 156 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
96 return; 157 return clone ();
97 158
98 Display *dpy = s->display->dpy; 159 Display *dpy = s->display->dpy;
99 int size = max (rh, rv) * 2 + 1; 160 int size = max (rh, rv) * 2 + 1;
100 double *kernel = (double *)malloc (size * sizeof (double)); 161 double *kernel = (double *)malloc (size * sizeof (double));
101 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 162 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
163 rxvt_img *img = new rxvt_img (s, format, w, h);
164 img->alloc ();
102 165
103 XRenderPictureAttributes pa; 166 XRenderPictureAttributes pa;
104 167
105 pa.repeat = RepeatPad; 168 pa.repeat = RepeatPad;
106 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); 169 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
170 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
171
107 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); 172 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
108 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 173 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
109 XFreePixmap (dpy, tmp); 174 XFreePixmap (dpy, tmp_pm);
110 175
111 if (kernel && params) 176 if (kernel && params)
112 { 177 {
113 size = rh * 2 + 1; 178 size = rh * 2 + 1;
114 get_gaussian_kernel (rh, size, kernel, params); 179 get_gaussian_kernel (rh, size, kernel, params);
116 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 181 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
117 XRenderComposite (dpy, 182 XRenderComposite (dpy,
118 PictOpSrc, 183 PictOpSrc,
119 src, 184 src,
120 None, 185 None,
121 dst, 186 tmp,
122 0, 0, 187 0, 0,
123 0, 0, 188 0, 0,
124 0, 0, 189 0, 0,
125 w, h); 190 w, h);
126 191
127 ::swap (src, dst);
128
129 size = rv * 2 + 1; 192 size = rv * 2 + 1;
130 get_gaussian_kernel (rv, size, kernel, params); 193 get_gaussian_kernel (rv, size, kernel, params);
131 ::swap (params[0], params[1]); 194 ::swap (params[0], params[1]);
132 195
133 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 196 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
134 XRenderComposite (dpy, 197 XRenderComposite (dpy,
135 PictOpSrc, 198 PictOpSrc,
136 src, 199 tmp,
137 None, 200 None,
138 dst, 201 dst,
139 0, 0, 202 0, 0,
140 0, 0, 203 0, 0,
141 0, 0, 204 0, 0,
144 207
145 free (kernel); 208 free (kernel);
146 free (params); 209 free (params);
147 XRenderFreePicture (dpy, src); 210 XRenderFreePicture (dpy, src);
148 XRenderFreePicture (dpy, dst); 211 XRenderFreePicture (dpy, dst);
212 XRenderFreePicture (dpy, tmp);
213
214 return img;
149} 215}
150 216
151static Picture 217static Picture
152create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 218create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
153{ 219{
162 228
163 return mask; 229 return mask;
164} 230}
165 231
166void 232void
167rxvt_img::brightness (double r, double g, double b, double a) 233rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
168{ 234{
169 Display *dpy = s->display->dpy; 235 Display *dpy = s->display->dpy;
170 Picture src = create_xrender_mask (dpy, pm, True); 236 Picture src = create_xrender_mask (dpy, pm, True);
171 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 237 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
172 238
173 XRenderColor mask_c; 239 XRenderColor mask_c;
174 mask_c.red = float_to_component (r); 240 mask_c.red = r;
175 mask_c.green = float_to_component (g); 241 mask_c.green = g;
176 mask_c.blue = float_to_component (b); 242 mask_c.blue = b;
177 mask_c.alpha = float_to_component (a); 243 mask_c.alpha = a;
178 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 244 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
179 245
180 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 246 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
247
248 XRenderFreePicture (dpy, src);
249 XRenderFreePicture (dpy, dst);
181} 250}
182 251
183void 252void
184rxvt_img::contrast (double r, double g, double b, double a) 253rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
185{ 254{
186 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) 255 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
187 return; 256 return;
188 257
189 Display *dpy = s->display->dpy; 258 Display *dpy = s->display->dpy;
190 Picture src = create_xrender_mask (dpy, pm, True); 259 Picture src = create_xrender_mask (dpy, pm, True);
191 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 260 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
192 261
193 XRenderColor mask_c; 262 XRenderColor mask_c;
194 mask_c.red = float_to_component (r); 263 mask_c.red = r;
195 mask_c.green = float_to_component (g); 264 mask_c.green = g;
196 mask_c.blue = float_to_component (b); 265 mask_c.blue = b;
197 mask_c.alpha = float_to_component (a); 266 mask_c.alpha = a;
198 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 267 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
199 268
200 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 269 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
270
271 XRenderFreePicture (dpy, src);
272 XRenderFreePicture (dpy, dst);
201} 273}
202 274
203bool 275bool
204rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 276rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
205{ 277{
249 321
250 int rowstride = gdk_pixbuf_get_rowstride (pixbuf); 322 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
251 int channels = gdk_pixbuf_get_n_channels (pixbuf); 323 int channels = gdk_pixbuf_get_n_channels (pixbuf);
252 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; 324 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
253 char *line = ximage->data; 325 char *line = ximage->data;
254
255 rgba c (0, 0, 0);
256
257 if (channels == 4 && alpha_mask == 0)
258 {
259 //pix_colors[Color_bg].get (c);
260 //TODO
261 c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D
262 c.r >>= 8;
263 c.g >>= 8;
264 c.b >>= 8;
265 }
266 326
267 for (int y = 0; y < height; y++) 327 for (int y = 0; y < height; y++)
268 { 328 {
269 for (int x = 0; x < width; x++) 329 for (int x = 0; x < width; x++)
270 { 330 {
273 unsigned char r, g, b, a; 333 unsigned char r, g, b, a;
274 334
275 if (channels == 4) 335 if (channels == 4)
276 { 336 {
277 a = pixel[3]; 337 a = pixel[3];
278 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; 338 r = pixel[0] * a / 0xff;
279 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; 339 g = pixel[1] * a / 0xff;
280 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; 340 b = pixel[2] * a / 0xff;
281 } 341 }
282 else 342 else
283 { 343 {
284 a = 0xff; 344 a = 0xff;
285 r = pixel[0]; 345 r = pixel[0];
310} 370}
311 371
312rxvt_img * 372rxvt_img *
313rxvt_img::clone () 373rxvt_img::clone ()
314{ 374{
315 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 375 return new rxvt_img (*this);
316 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth);
317 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0);
318 XFreeGC (s->display->dpy, gc);
319 return new rxvt_img (s, format, w, h, pm2);
320} 376}
321 377
322rxvt_img * 378rxvt_img *
379rxvt_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
398rxvt_img *
323rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) 399rxvt_img::transform (int new_width, int new_height, double matrix[9])
324{ 400{
325 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 401 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
402 img->alloc ();
326 403
327 Display *dpy = s->display->dpy; 404 Display *dpy = s->display->dpy;
328 XRenderPictureAttributes pa; 405 XRenderPictureAttributes pa;
329 pa.repeat = repeat; 406 pa.repeat = repeat;
330 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 407 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
353 w / (double)new_width, 0, 0, 430 w / (double)new_width, 0, 0,
354 0, h / (double)new_height, 0, 431 0, h / (double)new_height, 0,
355 0, 0, 1 432 0, 0, 1
356 }; 433 };
357 434
358 return transform (new_width, new_height, RepeatNormal, matrix); 435 return transform (new_width, new_height, matrix);
359} 436}
360 437
361rxvt_img * 438rxvt_img *
362rxvt_img::rotate (int new_width, int new_height, int repeat, int x, int y, double phi) 439rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
363{ 440{
364 double s = sin (phi); 441 double s = sin (phi);
365 double c = cos (phi); 442 double c = cos (phi);
366 443
367 double matrix[9] = { 444 double matrix[9] = {
368 c, -s, -c * x + s * y + x, 445 c, -s, -c * x + s * y + x,
369 s, c, -s * x - c * y + y, 446 s, c, -s * x - c * y + y,
370 0, 0, 1 447 0, 0, 1
371 }; 448 };
372 449
373 return transform (new_width, new_height, repeat, matrix); 450 return transform (new_width, new_height, matrix);
374} 451}
375 452
376rxvt_img * 453rxvt_img *
377rxvt_img::convert_to (XRenderPictFormat *new_format) 454rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
378{ 455{
456 if (new_format == format)
457 return clone ();
458
379 rxvt_img *img = new rxvt_img (s, new_format, w, h); 459 rxvt_img *img = new rxvt_img (s, new_format, w, h);
460 img->alloc ();
380 461
381 Display *dpy = s->display->dpy; 462 Display *dpy = s->display->dpy;
382 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 463 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
383 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 464 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
465 int op = PictOpSrc;
384 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
385 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 479 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
386 480
387 XRenderFreePicture (dpy, src); 481 XRenderFreePicture (dpy, src);
388 XRenderFreePicture (dpy, dst); 482 XRenderFreePicture (dpy, dst);
389 483
390 return img; 484 return img;
391} 485}
392 486
487rxvt_img *
488rxvt_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
393#endif 513#endif
394 514

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines