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.13 by root, Mon Jun 4 15:30:41 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 (pb, 0, 0, img->w, img->h, 0, 0); 74 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
75
76 g_object_unref (pb);
37 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);
201}
202 270
203void 271 XRenderFreePicture (dpy, src);
272 XRenderFreePicture (dpy, dst);
273}
274
275bool
204rxvt_img::render (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{
206 //TODO 278 Display *dpy = s->display->dpy;
279
280 if (s->visual->c_class != TrueColor)
281 return false;
282
283 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
284
285 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
286 green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
287 blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
288 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
289
290 int width_r = ecb_popcount32 (red_mask);
291 int width_g = ecb_popcount32 (green_mask);
292 int width_b = ecb_popcount32 (blue_mask);
293 int width_a = ecb_popcount32 (alpha_mask);
294
295 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
296 return false;
297
298 int sh_r = ecb_ctz32 (red_mask);
299 int sh_g = ecb_ctz32 (green_mask);
300 int sh_b = ecb_ctz32 (blue_mask);
301 int sh_a = ecb_ctz32 (alpha_mask);
302
303 if (width > 32767 || height > 32767)
304 return false;
305
306 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
307 width, height, 32, 0);
308 if (!ximage)
309 return false;
310
311 if (height > INT_MAX / ximage->bytes_per_line
312 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
313 {
314 XDestroyImage (ximage);
315 return false;
316 }
317
318 GC gc = XCreateGC (dpy, pm, 0, 0);
319
320 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
321
322 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
323 int channels = gdk_pixbuf_get_n_channels (pixbuf);
324 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
325 char *line = ximage->data;
326
327 for (int y = 0; y < height; y++)
328 {
329 for (int x = 0; x < width; x++)
330 {
331 unsigned char *pixel = row + x * channels;
332 uint32_t value;
333 unsigned char r, g, b, a;
334
335 if (channels == 4)
336 {
337 a = pixel[3];
338 r = pixel[0] * a / 0xff;
339 g = pixel[1] * a / 0xff;
340 b = pixel[2] * a / 0xff;
341 }
342 else
343 {
344 a = 0xff;
345 r = pixel[0];
346 g = pixel[1];
347 b = pixel[2];
348 }
349
350 value = ((r >> (8 - width_r)) << sh_r)
351 | ((g >> (8 - width_g)) << sh_g)
352 | ((b >> (8 - width_b)) << sh_b)
353 | ((a >> (8 - width_a)) << sh_a);
354
355 if (ximage->bits_per_pixel == 32)
356 ((uint32_t *)line)[x] = value;
357 else
358 XPutPixel (ximage, x, y, value);
359 }
360
361 row += rowstride;
362 line += ximage->bytes_per_line;
363 }
364
365 XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
366 XDestroyImage (ximage);
367 XFreeGC (dpy, gc);
368
369 return true;
207} 370}
208 371
209rxvt_img * 372rxvt_img *
210rxvt_img::clone () 373rxvt_img::clone ()
211{ 374{
212 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 375 return new rxvt_img (*this);
213 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth);
214 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0);
215 XFreeGC (s->display->dpy, gc);
216 return new rxvt_img (s, format, w, h, pm2);
217} 376}
218 377
219rxvt_img * 378rxvt_img *
220rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) 379rxvt_img::sub_rect (int x, int y, int width, int height)
221{ 380{
222 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 381 rxvt_img *img = new rxvt_img (s, format, width, height);
382 img->alloc ();
223 383
224 Display *dpy = s->display->dpy; 384 Display *dpy = s->display->dpy;
225 XRenderPictureAttributes pa; 385 XRenderPictureAttributes pa;
226 pa.repeat = repeat; 386 pa.repeat = repeat;
227 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 387 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
228 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 388 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
229 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 *
399rxvt_img::transform (int new_width, int new_height, double matrix[9])
400{
401 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
402 img->alloc ();
403
404 Display *dpy = s->display->dpy;
405 XRenderPictureAttributes pa;
406 pa.repeat = repeat;
407 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
408 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
409
230 XTransform xfrm; 410 XTransform xfrm;
231 411
232 for (int i = 0; i < 3; ++i) 412 for (int i = 0; i < 3; ++i)
233 for (int j = 0; j < 3; ++j) 413 for (int j = 0; j < 3; ++j)
234 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 414 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
235 415
416 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
236 XRenderSetPictureTransform (dpy, src, &xfrm); 417 XRenderSetPictureTransform (dpy, src, &xfrm);
237 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 418 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
238 419
239 XRenderFreePicture (dpy, src); 420 XRenderFreePicture (dpy, src);
240 XRenderFreePicture (dpy, dst); 421 XRenderFreePicture (dpy, dst);
244 425
245rxvt_img * 426rxvt_img *
246rxvt_img::scale (int new_width, int new_height) 427rxvt_img::scale (int new_width, int new_height)
247{ 428{
248 double matrix[9] = { 429 double matrix[9] = {
249 new_width / (double)w, 0, 0, 430 w / (double)new_width, 0, 0,
250 0, new_height / (double)h, 0, 431 0, h / (double)new_height, 0,
251 0, 0, 1 432 0, 0, 1
252 }; 433 };
253 434
254 return transform (new_width, new_height, RepeatNormal, matrix); 435 return transform (new_width, new_height, matrix);
255} 436}
256 437
257rxvt_img * 438rxvt_img *
439rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
440{
441 double s = sin (phi);
442 double c = cos (phi);
443
444 double matrix[9] = {
445 c, -s, -c * x + s * y + x,
446 s, c, -s * x - c * y + y,
447 0, 0, 1
448 };
449
450 return transform (new_width, new_height, matrix);
451}
452
453rxvt_img *
258rxvt_img::convert_to (XRenderPictFormat *new_format) 454rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
259{ 455{
456 if (new_format == format)
457 return clone ();
458
260 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 ();
261 461
262 Display *dpy = s->display->dpy; 462 Display *dpy = s->display->dpy;
263 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 463 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
264 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;
265 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
266 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);
267 480
268 XRenderFreePicture (dpy, src); 481 XRenderFreePicture (dpy, src);
269 XRenderFreePicture (dpy, dst); 482 XRenderFreePicture (dpy, dst);
270 483
271 return img; 484 return img;
272} 485}
273 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
274#endif 513#endif
275 514

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines