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.25 by sf-exg, Tue Jun 5 22:00:15 2012 UTC vs.
Revision 1.33 by root, Thu Jun 7 08:12:55 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 x, int y, int width, int height)
10: s(screen), w(width), h(height), format(format), shared(false) 8: s(screen), x(x), y(y), 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{
18} 24}
25#endif
19 26
20rxvt_img * 27rxvt_img *
21rxvt_img::new_from_root (rxvt_screen *s) 28rxvt_img::new_from_root (rxvt_screen *s)
22{ 29{
23 Display *dpy = s->display->dpy; 30 Display *dpy = s->display->dpy;
37 return 0; 44 return 0;
38 45
39 rxvt_img *img = new rxvt_img ( 46 rxvt_img *img = new rxvt_img (
40 s, 47 s,
41 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), 48 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
49 0,
50 0,
42 root_pm_w, 51 root_pm_w,
43 root_pm_h, 52 root_pm_h
44 root_pixmap
45 ); 53 );
46 54
47 img->shared = true; 55 img->pm = root_pixmap;
48 56
49 return img; 57 return img;
50} 58}
51 59
52rxvt_img * 60rxvt_img *
59 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); 67 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
60 68
61 rxvt_img *img = new rxvt_img ( 69 rxvt_img *img = new rxvt_img (
62 s, 70 s,
63 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 71 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
72 0,
73 0,
64 gdk_pixbuf_get_width (pb), 74 gdk_pixbuf_get_width (pb),
65 gdk_pixbuf_get_height (pb) 75 gdk_pixbuf_get_height (pb)
66 ); 76 );
67 77 img->alloc ();
68 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); 78 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
69 79
80 g_object_unref (pb);
81
82 return img;
83}
84
85void
86rxvt_img::destroy ()
87{
88 if (!refcnt || --*refcnt)
70 return img; 89 return;
90
91 if (pm)
92 XFreePixmap (s->display->dpy, pm);
93
94 delete refcnt;
71} 95}
72 96
73rxvt_img::~rxvt_img () 97rxvt_img::~rxvt_img ()
74{ 98{
75 if (!shared) 99 destroy ();
76 XFreePixmap (s->display->dpy, pm); 100}
101
102void
103rxvt_img::alloc ()
104{
105 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
106 refcnt = new int (1);
77} 107}
78 108
79void 109void
80rxvt_img::unshare () 110rxvt_img::unshare ()
81{ 111{
82 if (!shared) 112 if (refcnt && *refcnt == 1)
83 return; 113 return;
84 114
85 rxvt_img *img = clone (); 115 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
116 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
117 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0);
118 XFreeGC (s->display->dpy, gc);
86 119
87 ::swap (pm , img->pm); 120 destroy ();
88 ::swap (shared, img->shared);
89 121
90 delete img; 122 pm = pm2;
123 refcnt = new int (1);
91} 124}
92 125
93void 126void
94rxvt_img::fill (const rxvt_color &c) 127rxvt_img::fill (const rxvt_color &c)
95{ 128{
129 162
130 Display *dpy = s->display->dpy; 163 Display *dpy = s->display->dpy;
131 int size = max (rh, rv) * 2 + 1; 164 int size = max (rh, rv) * 2 + 1;
132 double *kernel = (double *)malloc (size * sizeof (double)); 165 double *kernel = (double *)malloc (size * sizeof (double));
133 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 166 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
134 rxvt_img *img = new rxvt_img (s, format, w, h); 167 rxvt_img *img = new rxvt_img (s, format, x, y, w, h);
168 img->alloc ();
135 169
136 XRenderPictureAttributes pa; 170 XRenderPictureAttributes pa;
137 171
138 pa.repeat = RepeatPad; 172 pa.repeat = RepeatPad;
139 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); 173 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
198 232
199 return mask; 233 return mask;
200} 234}
201 235
202void 236void
203rxvt_img::brightness (double r, double g, double b, double a) 237rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
204{ 238{
205 Display *dpy = s->display->dpy; 239 Display *dpy = s->display->dpy;
206 Picture src = create_xrender_mask (dpy, pm, True); 240 Picture src = create_xrender_mask (dpy, pm, True);
207 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 241 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
208 242
209 XRenderColor mask_c; 243 XRenderColor mask_c;
210 mask_c.red = float_to_component (r); 244 mask_c.red = r;
211 mask_c.green = float_to_component (g); 245 mask_c.green = g;
212 mask_c.blue = float_to_component (b); 246 mask_c.blue = b;
213 mask_c.alpha = float_to_component (a); 247 mask_c.alpha = a;
214 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 248 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
215 249
216 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 250 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
217 251
218 XRenderFreePicture (dpy, src); 252 XRenderFreePicture (dpy, src);
219 XRenderFreePicture (dpy, dst); 253 XRenderFreePicture (dpy, dst);
220} 254}
221 255
222void 256void
223rxvt_img::contrast (double r, double g, double b, double a) 257rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
224{ 258{
225 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) 259 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
226 return; 260 return;
227 261
228 Display *dpy = s->display->dpy; 262 Display *dpy = s->display->dpy;
229 Picture src = create_xrender_mask (dpy, pm, True); 263 Picture src = create_xrender_mask (dpy, pm, True);
230 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 264 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
231 265
232 XRenderColor mask_c; 266 XRenderColor mask_c;
233 mask_c.red = float_to_component (r); 267 mask_c.red = r;
234 mask_c.green = float_to_component (g); 268 mask_c.green = g;
235 mask_c.blue = float_to_component (b); 269 mask_c.blue = b;
236 mask_c.alpha = float_to_component (a); 270 mask_c.alpha = a;
237 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 271 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
238 272
239 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 273 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
240 274
241 XRenderFreePicture (dpy, src); 275 XRenderFreePicture (dpy, src);
340} 374}
341 375
342rxvt_img * 376rxvt_img *
343rxvt_img::clone () 377rxvt_img::clone ()
344{ 378{
379 return new rxvt_img (*this);
380}
381
382rxvt_img *
383rxvt_img::reify ()
384{
345 rxvt_img *img = new rxvt_img (s, format, w, h); 385 rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h);
386 img->alloc ();
346 387
347 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 388 // todo, if x==0 and y==0 and w==real width we could clone
348 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); 389 // but that involves an rtt to find pixmap width.
349 XFreeGC (s->display->dpy, gc);
350
351 return img;
352}
353
354rxvt_img *
355rxvt_img::sub_rect (int x, int y, int width, int height, int repeat)
356{
357 rxvt_img *img = new rxvt_img (s, format, width, height);
358 390
359 Display *dpy = s->display->dpy; 391 Display *dpy = s->display->dpy;
360 XRenderPictureAttributes pa; 392 XRenderPictureAttributes pa;
361 pa.repeat = repeat; 393 pa.repeat = repeat;
362 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 394 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
363 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 395 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
364 396
365 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); 397 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
366 398
367 XRenderFreePicture (dpy, src);
368 XRenderFreePicture (dpy, dst); 399 XRenderFreePicture (dpy, src);
400 XRenderFreePicture (dpy, dst);
369 401
370 return img; 402 return img;
371} 403}
372 404
373rxvt_img * 405rxvt_img *
406rxvt_img::sub_rect (int x, int y, int width, int height)
407{
408 rxvt_img *img = clone ();
409
410 img->x += x;
411 img->y += y;
412 img->w = width;
413 img->h = height;
414
415 return img;
416}
417
418rxvt_img *
374rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) 419rxvt_img::transform (int new_width, int new_height, double matrix[9])
375{ 420{
376 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 421 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height);
422 img->alloc ();
377 423
378 Display *dpy = s->display->dpy; 424 Display *dpy = s->display->dpy;
379 XRenderPictureAttributes pa; 425 XRenderPictureAttributes pa;
380 pa.repeat = repeat; 426 pa.repeat = repeat;
381 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 427 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
384 XTransform xfrm; 430 XTransform xfrm;
385 431
386 for (int i = 0; i < 3; ++i) 432 for (int i = 0; i < 3; ++i)
387 for (int j = 0; j < 3; ++j) 433 for (int j = 0; j < 3; ++j)
388 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 434 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
435
436 xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO
437 xfrm.matrix [0][3] += XDoubleToFixed (y);
389 438
390 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 439 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
391 XRenderSetPictureTransform (dpy, src, &xfrm); 440 XRenderSetPictureTransform (dpy, src, &xfrm);
392 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 441 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
393 442
408 457
409 return transform (new_width, new_height, matrix); 458 return transform (new_width, new_height, matrix);
410} 459}
411 460
412rxvt_img * 461rxvt_img *
413rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) 462rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
414{ 463{
415 double s = sin (phi); 464 double s = sin (phi);
416 double c = cos (phi); 465 double c = cos (phi);
417 466
418 double matrix[9] = { 467 double matrix[9] = {
419 c, -s, -c * x + s * y + x, 468 c, -s, -c * x + s * y + x,
420 s, c, -s * x - c * y + y, 469 s, c, -s * x - c * y + y,
421 0, 0, 1 470 0, 0, 1
422 }; 471 };
423 472
424 return transform (new_width, new_height, matrix, repeat); 473 return transform (new_width, new_height, matrix);
425} 474}
426 475
427rxvt_img * 476rxvt_img *
428rxvt_img::convert_to (XRenderPictFormat *new_format) 477rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
429{ 478{
479 if (new_format == format)
480 return clone ();
481
430 rxvt_img *img = new rxvt_img (s, new_format, w, h); 482 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h);
483 img->alloc ();
431 484
432 Display *dpy = s->display->dpy; 485 Display *dpy = s->display->dpy;
433 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 486 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
434 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 487 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
488 int op = PictOpSrc;
435 489
490 if (format->direct.alphaMask && !new_format->direct.alphaMask)
491 {
492 // does it have to be that complicated
493 rgba c;
494 bg.get (c);
495
496 XRenderColor rc = { c.r, c.g, c.b, 0xffff };
497 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
498
499 op = PictOpOver;
500 }
501
436 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 502 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
437 503
438 XRenderFreePicture (dpy, src); 504 XRenderFreePicture (dpy, src);
439 XRenderFreePicture (dpy, dst); 505 XRenderFreePicture (dpy, dst);
440 506
441 return img; 507 return img;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines