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.31 by sf-exg, Thu Jun 7 06:08:09 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), shared(false)
11{ 9{
12 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 10 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
13} 11}
14 12
15rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) 13rxvt_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) 14: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
17{ 15{
16}
17
18rxvt_img *
19rxvt_img::new_from_root (rxvt_screen *s)
20{
21 Display *dpy = s->display->dpy;
22 unsigned int root_pm_w, root_pm_h;
23 Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_XROOTPMAP_ID]);
24 if (root_pixmap == None)
25 root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_ESETROOT_PMAP_ID]);
26
27 if (root_pixmap == None)
28 return 0;
29
30 Window wdummy;
31 int idummy;
32 unsigned int udummy;
33
34 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy))
35 return 0;
36
37 rxvt_img *img = new rxvt_img (
38 s,
39 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
40 root_pm_w,
41 root_pm_h,
42 root_pixmap
43 );
44
45 img->shared = true;
46
47 return img;
18} 48}
19 49
20rxvt_img * 50rxvt_img *
21rxvt_img::new_from_file (rxvt_screen *s, const char *filename) 51rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
22{ 52{
33 gdk_pixbuf_get_height (pb) 63 gdk_pixbuf_get_height (pb)
34 ); 64 );
35 65
36 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); 66 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
37 67
68 g_object_unref (pb);
69
38 return img; 70 return img;
39} 71}
40 72
41rxvt_img::~rxvt_img () 73rxvt_img::~rxvt_img ()
42{ 74{
87 119
88 for (int i = 0; i < width; i++) 120 for (int i = 0; i < width; i++)
89 params[i+2] = XDoubleToFixed (kernel[i] / sum); 121 params[i+2] = XDoubleToFixed (kernel[i] / sum);
90} 122}
91 123
92void 124rxvt_img *
93rxvt_img::blur (int rh, int rv) 125rxvt_img::blur (int rh, int rv)
94{ 126{
95 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 127 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
96 return; 128 return clone ();
97 129
98 Display *dpy = s->display->dpy; 130 Display *dpy = s->display->dpy;
99 int size = max (rh, rv) * 2 + 1; 131 int size = max (rh, rv) * 2 + 1;
100 double *kernel = (double *)malloc (size * sizeof (double)); 132 double *kernel = (double *)malloc (size * sizeof (double));
101 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 133 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
134 rxvt_img *img = new rxvt_img (s, format, w, h);
102 135
103 XRenderPictureAttributes pa; 136 XRenderPictureAttributes pa;
104 137
105 pa.repeat = RepeatPad; 138 pa.repeat = RepeatPad;
106 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); 139 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
140 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
141
107 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); 142 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
108 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 143 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
109 XFreePixmap (dpy, tmp); 144 XFreePixmap (dpy, tmp_pm);
110 145
111 if (kernel && params) 146 if (kernel && params)
112 { 147 {
113 size = rh * 2 + 1; 148 size = rh * 2 + 1;
114 get_gaussian_kernel (rh, size, kernel, params); 149 get_gaussian_kernel (rh, size, kernel, params);
116 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 151 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
117 XRenderComposite (dpy, 152 XRenderComposite (dpy,
118 PictOpSrc, 153 PictOpSrc,
119 src, 154 src,
120 None, 155 None,
121 dst, 156 tmp,
122 0, 0, 157 0, 0,
123 0, 0, 158 0, 0,
124 0, 0, 159 0, 0,
125 w, h); 160 w, h);
126
127 ::swap (src, dst);
128 161
129 size = rv * 2 + 1; 162 size = rv * 2 + 1;
130 get_gaussian_kernel (rv, size, kernel, params); 163 get_gaussian_kernel (rv, size, kernel, params);
131 ::swap (params[0], params[1]); 164 ::swap (params[0], params[1]);
132 165
133 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 166 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
134 XRenderComposite (dpy, 167 XRenderComposite (dpy,
135 PictOpSrc, 168 PictOpSrc,
136 src, 169 tmp,
137 None, 170 None,
138 dst, 171 dst,
139 0, 0, 172 0, 0,
140 0, 0, 173 0, 0,
141 0, 0, 174 0, 0,
144 177
145 free (kernel); 178 free (kernel);
146 free (params); 179 free (params);
147 XRenderFreePicture (dpy, src); 180 XRenderFreePicture (dpy, src);
148 XRenderFreePicture (dpy, dst); 181 XRenderFreePicture (dpy, dst);
182 XRenderFreePicture (dpy, tmp);
183
184 return img;
149} 185}
150 186
151static Picture 187static Picture
152create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 188create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
153{ 189{
162 198
163 return mask; 199 return mask;
164} 200}
165 201
166void 202void
167rxvt_img::brightness (double r, double g, double b, double a) 203rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
168{ 204{
169 Display *dpy = s->display->dpy; 205 Display *dpy = s->display->dpy;
170 Picture src = create_xrender_mask (dpy, pm, True); 206 Picture src = create_xrender_mask (dpy, pm, True);
171 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 207 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
172 208
173 XRenderColor mask_c; 209 XRenderColor mask_c;
174 mask_c.red = float_to_component (r); 210 mask_c.red = r;
175 mask_c.green = float_to_component (g); 211 mask_c.green = g;
176 mask_c.blue = float_to_component (b); 212 mask_c.blue = b;
177 mask_c.alpha = float_to_component (a); 213 mask_c.alpha = a;
178 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 214 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
179 215
180 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 216 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
217
218 XRenderFreePicture (dpy, src);
219 XRenderFreePicture (dpy, dst);
181} 220}
182 221
183void 222void
184rxvt_img::contrast (double r, double g, double b, double a) 223rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
185{ 224{
186 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) 225 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
187 return; 226 return;
188 227
189 Display *dpy = s->display->dpy; 228 Display *dpy = s->display->dpy;
190 Picture src = create_xrender_mask (dpy, pm, True); 229 Picture src = create_xrender_mask (dpy, pm, True);
191 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 230 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
192 231
193 XRenderColor mask_c; 232 XRenderColor mask_c;
194 mask_c.red = float_to_component (r); 233 mask_c.red = r;
195 mask_c.green = float_to_component (g); 234 mask_c.green = g;
196 mask_c.blue = float_to_component (b); 235 mask_c.blue = b;
197 mask_c.alpha = float_to_component (a); 236 mask_c.alpha = a;
198 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 237 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
199 238
200 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 239 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
240
241 XRenderFreePicture (dpy, src);
242 XRenderFreePicture (dpy, dst);
201} 243}
202 244
203bool 245bool
204rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 246rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
205{ 247{
249 291
250 int rowstride = gdk_pixbuf_get_rowstride (pixbuf); 292 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
251 int channels = gdk_pixbuf_get_n_channels (pixbuf); 293 int channels = gdk_pixbuf_get_n_channels (pixbuf);
252 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; 294 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
253 char *line = ximage->data; 295 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 296
267 for (int y = 0; y < height; y++) 297 for (int y = 0; y < height; y++)
268 { 298 {
269 for (int x = 0; x < width; x++) 299 for (int x = 0; x < width; x++)
270 { 300 {
273 unsigned char r, g, b, a; 303 unsigned char r, g, b, a;
274 304
275 if (channels == 4) 305 if (channels == 4)
276 { 306 {
277 a = pixel[3]; 307 a = pixel[3];
278 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; 308 r = pixel[0] * a / 0xff;
279 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; 309 g = pixel[1] * a / 0xff;
280 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; 310 b = pixel[2] * a / 0xff;
281 } 311 }
282 else 312 else
283 { 313 {
284 a = 0xff; 314 a = 0xff;
285 r = pixel[0]; 315 r = pixel[0];
310} 340}
311 341
312rxvt_img * 342rxvt_img *
313rxvt_img::clone () 343rxvt_img::clone ()
314{ 344{
345 rxvt_img *img = new rxvt_img (s, format, w, h);
346
315 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 347 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
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); 348 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0);
318 XFreeGC (s->display->dpy, gc); 349 XFreeGC (s->display->dpy, gc);
319 return new rxvt_img (s, format, w, h, pm2);
320}
321 350
351 return img;
352}
353
322rxvt_img * 354rxvt_img *
355rxvt_img::sub_rect (int x, int y, int width, int height)
356{
357 rxvt_img *img = new rxvt_img (s, format, width, height);
358
359 Display *dpy = s->display->dpy;
360 XRenderPictureAttributes pa;
361 pa.repeat = repeat;
362 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
363 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
364
365 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height);
366
367 XRenderFreePicture (dpy, src);
368 XRenderFreePicture (dpy, dst);
369
370 return img;
371}
372
373rxvt_img *
323rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) 374rxvt_img::transform (int new_width, int new_height, double matrix[9])
324{ 375{
325 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 376 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
326 377
327 Display *dpy = s->display->dpy; 378 Display *dpy = s->display->dpy;
328 XRenderPictureAttributes pa; 379 XRenderPictureAttributes pa;
353 w / (double)new_width, 0, 0, 404 w / (double)new_width, 0, 0,
354 0, h / (double)new_height, 0, 405 0, h / (double)new_height, 0,
355 0, 0, 1 406 0, 0, 1
356 }; 407 };
357 408
358 return transform (new_width, new_height, RepeatNormal, matrix); 409 return transform (new_width, new_height, matrix);
359} 410}
360 411
361rxvt_img * 412rxvt_img *
362rxvt_img::rotate (int new_width, int new_height, int repeat, int x, int y, double phi) 413rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
363{ 414{
364 double s = sin (phi); 415 double s = sin (phi);
365 double c = cos (phi); 416 double c = cos (phi);
366 417
367 double matrix[9] = { 418 double matrix[9] = {
368 c, -s, -c * x + s * y + x, 419 c, -s, -c * x + s * y + x,
369 s, c, -s * x - c * y + y, 420 s, c, -s * x - c * y + y,
370 0, 0, 1 421 0, 0, 1
371 }; 422 };
372 423
373 return transform (new_width, new_height, repeat, matrix); 424 return transform (new_width, new_height, matrix);
374} 425}
375 426
376rxvt_img * 427rxvt_img *
377rxvt_img::convert_to (XRenderPictFormat *new_format) 428rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
378{ 429{
379 rxvt_img *img = new rxvt_img (s, new_format, w, h); 430 rxvt_img *img = new rxvt_img (s, new_format, w, h);
380 431
381 Display *dpy = s->display->dpy; 432 Display *dpy = s->display->dpy;
382 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 433 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
383 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 434 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
435 int op = PictOpSrc;
384 436
437 if (format->direct.alphaMask && !new_format->direct.alphaMask)
438 {
439 // does it have to be that complicated
440 rgba c;
441 bg.get (c);
442
443 XRenderColor rc = { c.r, c.g, c.b, 0xffff };
444 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
445
446 op = PictOpOver;
447 }
448
385 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 449 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
386 450
387 XRenderFreePicture (dpy, src);
388 XRenderFreePicture (dpy, dst); 451 XRenderFreePicture (dpy, src);
452 XRenderFreePicture (dpy, dst);
389 453
454 return img;
455}
456
457rxvt_img *
458rxvt_img::blend (rxvt_img *img, double factor)
459{
460 rxvt_img *img2 = clone ();
461 Display *dpy = s->display->dpy;
462 Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
463 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
464 Picture mask = create_xrender_mask (dpy, img->pm, False);
465
466 XRenderColor mask_c;
467
468 mask_c.alpha = float_to_component (factor);
469 mask_c.red =
470 mask_c.green =
471 mask_c.blue = 0;
472 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
473
474 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h);
475
476 XRenderFreePicture (dpy, src);
477 XRenderFreePicture (dpy, dst);
478 XRenderFreePicture (dpy, mask);
479
390 return img; 480 return img2;
391} 481}
392 482
393#endif 483#endif
394 484

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines