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.7 by sf-exg, Sun Jun 3 20:34:25 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{
23 GError *err; 53 GError *err = 0;
24 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); 54 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
25 55
26 if (!pb) 56 if (!pb)
27 return 0; 57 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
28 58
29 rxvt_img *img = new rxvt_img ( 59 rxvt_img *img = new rxvt_img (
30 s, 60 s,
31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 61 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
32 gdk_pixbuf_get_width (pb), 62 gdk_pixbuf_get_width (pb),
33 gdk_pixbuf_get_height (pb) 63 gdk_pixbuf_get_height (pb)
34 ); 64 );
35 65
36 img->render (pb, 0, 0, img->w, img->h, 0, 0); 66 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
67
68 g_object_unref (pb);
37 69
38 return img; 70 return img;
39} 71}
40 72
41rxvt_img::~rxvt_img () 73rxvt_img::~rxvt_img ()
42{ 74{
43 if (!shared) 75 if (!shared)
44 XFreePixmap (s->display->dpy, pm); 76 XFreePixmap (s->display->dpy, pm);
77}
78
79void
80rxvt_img::unshare ()
81{
82 if (!shared)
83 return;
84
85 rxvt_img *img = clone ();
86
87 ::swap (pm , img->pm);
88 ::swap (shared, img->shared);
89
90 delete img;
45} 91}
46 92
47void 93void
48rxvt_img::fill (const rxvt_color &c) 94rxvt_img::fill (const rxvt_color &c)
49{ 95{
73 119
74 for (int i = 0; i < width; i++) 120 for (int i = 0; i < width; i++)
75 params[i+2] = XDoubleToFixed (kernel[i] / sum); 121 params[i+2] = XDoubleToFixed (kernel[i] / sum);
76} 122}
77 123
78void 124rxvt_img *
79rxvt_img::blur (int rh, int rv) 125rxvt_img::blur (int rh, int rv)
80{ 126{
81 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 127 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
82 return; 128 return clone ();
83 129
84 Display *dpy = s->display->dpy; 130 Display *dpy = s->display->dpy;
85 int size = max (rh, rv) * 2 + 1; 131 int size = max (rh, rv) * 2 + 1;
86 double *kernel = (double *)malloc (size * sizeof (double)); 132 double *kernel = (double *)malloc (size * sizeof (double));
87 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);
88 135
89 XRenderPictureAttributes pa; 136 XRenderPictureAttributes pa;
90 137
91 pa.repeat = RepeatPad; 138 pa.repeat = RepeatPad;
92 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
93 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); 142 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
94 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 143 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
95 XFreePixmap (dpy, tmp); 144 XFreePixmap (dpy, tmp_pm);
96 145
97 if (kernel && params) 146 if (kernel && params)
98 { 147 {
99 size = rh * 2 + 1; 148 size = rh * 2 + 1;
100 get_gaussian_kernel (rh, size, kernel, params); 149 get_gaussian_kernel (rh, size, kernel, params);
102 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 151 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
103 XRenderComposite (dpy, 152 XRenderComposite (dpy,
104 PictOpSrc, 153 PictOpSrc,
105 src, 154 src,
106 None, 155 None,
107 dst, 156 tmp,
108 0, 0, 157 0, 0,
109 0, 0, 158 0, 0,
110 0, 0, 159 0, 0,
111 w, h); 160 w, h);
112
113 ::swap (src, dst);
114 161
115 size = rv * 2 + 1; 162 size = rv * 2 + 1;
116 get_gaussian_kernel (rv, size, kernel, params); 163 get_gaussian_kernel (rv, size, kernel, params);
117 ::swap (params[0], params[1]); 164 ::swap (params[0], params[1]);
118 165
119 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 166 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
120 XRenderComposite (dpy, 167 XRenderComposite (dpy,
121 PictOpSrc, 168 PictOpSrc,
122 src, 169 tmp,
123 None, 170 None,
124 dst, 171 dst,
125 0, 0, 172 0, 0,
126 0, 0, 173 0, 0,
127 0, 0, 174 0, 0,
130 177
131 free (kernel); 178 free (kernel);
132 free (params); 179 free (params);
133 XRenderFreePicture (dpy, src); 180 XRenderFreePicture (dpy, src);
134 XRenderFreePicture (dpy, dst); 181 XRenderFreePicture (dpy, dst);
182 XRenderFreePicture (dpy, tmp);
183
184 return img;
135} 185}
136 186
137static Picture 187static Picture
138create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 188create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
139{ 189{
148 198
149 return mask; 199 return mask;
150} 200}
151 201
152void 202void
153rxvt_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)
154{ 204{
155 Display *dpy = s->display->dpy; 205 Display *dpy = s->display->dpy;
156 Picture src = create_xrender_mask (dpy, pm, True); 206 Picture src = create_xrender_mask (dpy, pm, True);
157 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 207 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
158 208
159 XRenderColor mask_c; 209 XRenderColor mask_c;
160 mask_c.red = float_to_component (r); 210 mask_c.red = r;
161 mask_c.green = float_to_component (g); 211 mask_c.green = g;
162 mask_c.blue = float_to_component (b); 212 mask_c.blue = b;
163 mask_c.alpha = float_to_component (a); 213 mask_c.alpha = a;
164 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 214 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
165 215
166 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);
167} 220}
168 221
169void 222void
170rxvt_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)
171{ 224{
172 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) 225 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
173 return; 226 return;
174 227
175 Display *dpy = s->display->dpy; 228 Display *dpy = s->display->dpy;
176 Picture src = create_xrender_mask (dpy, pm, True); 229 Picture src = create_xrender_mask (dpy, pm, True);
177 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 230 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
178 231
179 XRenderColor mask_c; 232 XRenderColor mask_c;
180 mask_c.red = float_to_component (r); 233 mask_c.red = r;
181 mask_c.green = float_to_component (g); 234 mask_c.green = g;
182 mask_c.blue = float_to_component (b); 235 mask_c.blue = b;
183 mask_c.alpha = float_to_component (a); 236 mask_c.alpha = a;
184 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 237 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
185 238
186 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);
187}
188 240
189void 241 XRenderFreePicture (dpy, src);
242 XRenderFreePicture (dpy, dst);
243}
244
245bool
190rxvt_img::render (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)
191{ 247{
192 //TODO 248 Display *dpy = s->display->dpy;
193}
194 249
250 if (s->visual->c_class != TrueColor)
251 return false;
252
253 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
254
255 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
256 green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
257 blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
258 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
259
260 int width_r = ecb_popcount32 (red_mask);
261 int width_g = ecb_popcount32 (green_mask);
262 int width_b = ecb_popcount32 (blue_mask);
263 int width_a = ecb_popcount32 (alpha_mask);
264
265 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
266 return false;
267
268 int sh_r = ecb_ctz32 (red_mask);
269 int sh_g = ecb_ctz32 (green_mask);
270 int sh_b = ecb_ctz32 (blue_mask);
271 int sh_a = ecb_ctz32 (alpha_mask);
272
273 if (width > 32767 || height > 32767)
274 return false;
275
276 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
277 width, height, 32, 0);
278 if (!ximage)
279 return false;
280
281 if (height > INT_MAX / ximage->bytes_per_line
282 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
283 {
284 XDestroyImage (ximage);
285 return false;
286 }
287
288 GC gc = XCreateGC (dpy, pm, 0, 0);
289
290 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
291
292 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
293 int channels = gdk_pixbuf_get_n_channels (pixbuf);
294 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
295 char *line = ximage->data;
296
297 for (int y = 0; y < height; y++)
298 {
299 for (int x = 0; x < width; x++)
300 {
301 unsigned char *pixel = row + x * channels;
302 uint32_t value;
303 unsigned char r, g, b, a;
304
305 if (channels == 4)
306 {
307 a = pixel[3];
308 r = pixel[0] * a / 0xff;
309 g = pixel[1] * a / 0xff;
310 b = pixel[2] * a / 0xff;
311 }
312 else
313 {
314 a = 0xff;
315 r = pixel[0];
316 g = pixel[1];
317 b = pixel[2];
318 }
319
320 value = ((r >> (8 - width_r)) << sh_r)
321 | ((g >> (8 - width_g)) << sh_g)
322 | ((b >> (8 - width_b)) << sh_b)
323 | ((a >> (8 - width_a)) << sh_a);
324
325 if (ximage->bits_per_pixel == 32)
326 ((uint32_t *)line)[x] = value;
327 else
328 XPutPixel (ximage, x, y, value);
329 }
330
331 row += rowstride;
332 line += ximage->bytes_per_line;
333 }
334
335 XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
336 XDestroyImage (ximage);
337 XFreeGC (dpy, gc);
338
339 return true;
340}
341
195rxvt_img * 342rxvt_img *
196rxvt_img::copy () 343rxvt_img::clone ()
197{ 344{
345 rxvt_img *img = new rxvt_img (s, format, w, h);
346
198 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 347 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
199 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth);
200 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);
201 XFreeGC (s->display->dpy, gc); 349 XFreeGC (s->display->dpy, gc);
202 return new rxvt_img (s, format, w, h, pm2);
203}
204 350
351 return img;
352}
353
205rxvt_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 *
206rxvt_img::transform (int new_width, int new_height, double matrix[16]) 374rxvt_img::transform (int new_width, int new_height, double matrix[9])
207{ 375{
208 //TODO 376 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
377
378 Display *dpy = s->display->dpy;
379 XRenderPictureAttributes pa;
380 pa.repeat = repeat;
381 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
382 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
383
384 XTransform xfrm;
385
386 for (int i = 0; i < 3; ++i)
387 for (int j = 0; j < 3; ++j)
388 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
389
390 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
391 XRenderSetPictureTransform (dpy, src, &xfrm);
392 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
393
394 XRenderFreePicture (dpy, src);
395 XRenderFreePicture (dpy, dst);
396
397 return img;
209} 398}
210 399
211rxvt_img * 400rxvt_img *
212rxvt_img::scale (int new_width, int new_height) 401rxvt_img::scale (int new_width, int new_height)
213{ 402{
214 // use transform 403 double matrix[9] = {
215 //TODO 404 w / (double)new_width, 0, 0,
216} 405 0, h / (double)new_height, 0,
406 0, 0, 1
407 };
217 408
409 return transform (new_width, new_height, matrix);
410}
411
218rxvt_img * 412rxvt_img *
413rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
414{
415 double s = sin (phi);
416 double c = cos (phi);
417
418 double matrix[9] = {
419 c, -s, -c * x + s * y + x,
420 s, c, -s * x - c * y + y,
421 0, 0, 1
422 };
423
424 return transform (new_width, new_height, matrix);
425}
426
427rxvt_img *
219rxvt_img::convert_to (XRenderPictFormat *new_format) 428rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
220{ 429{
430 rxvt_img *img = new rxvt_img (s, new_format, w, h);
431
221 Display *dpy = s->display->dpy; 432 Display *dpy = s->display->dpy;
222 Pixmap new_pm = XCreatePixmap (dpy, pm, w, h, new_format->depth);
223 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 433 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
224 Picture dst = XRenderCreatePicture (dpy, new_pm, new_format, 0, 0); 434 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
435 int op = PictOpSrc;
225 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
226 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);
227 450
228 XRenderFreePicture (dpy, src);
229 XRenderFreePicture (dpy, dst); 451 XRenderFreePicture (dpy, src);
452 XRenderFreePicture (dpy, dst);
230 453
231 rxvt_img *img = new rxvt_img ( 454 return img;
232 s, 455}
233 new_format,
234 w,
235 h,
236 new_pm
237 );
238 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
239 return img; 480 return img2;
240} 481}
241 482
242#endif 483#endif
243 484

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines