ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.75
Committed: Sun Jun 10 07:51:45 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.74: +5 -14 lines
Log Message:
Simplify.

File Contents

# Content
1 #include <math.h>
2 #include "../config.h"
3 #include "rxvt.h"
4
5 #if HAVE_IMG
6
7 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
8 : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), ref(0)
10 {
11 }
12
13 rxvt_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), ref(img.ref)
15 {
16 ++ref->cnt;
17 }
18
19 rxvt_img *
20 rxvt_img::new_from_root (rxvt_screen *s)
21 {
22 Display *dpy = s->display->dpy;
23 unsigned int root_pm_w, root_pm_h;
24 Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_XROOTPMAP_ID]);
25 if (root_pixmap == None)
26 root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_ESETROOT_PMAP_ID]);
27
28 if (root_pixmap == None)
29 return 0;
30
31 Window wdummy;
32 int idummy;
33 unsigned int udummy;
34
35 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy))
36 return 0;
37
38 rxvt_img *img = new rxvt_img (
39 s,
40 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
41 0,
42 0,
43 root_pm_w,
44 root_pm_h
45 );
46
47 img->pm = root_pixmap;
48 img->ref = new pixref (root_pm_w, root_pm_h);
49 img->ref->ours = false;
50
51 return img;
52 }
53
54 # if HAVE_PIXBUF
55 rxvt_img *
56 rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
57 {
58 Display *dpy = s->display->dpy;
59
60 int width = gdk_pixbuf_get_width (pb);
61 int height = gdk_pixbuf_get_height (pb);
62
63 if (width > 32767 || height > 32767) // well, we *could* upload in chunks
64 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
65
66 // since we require rgb24/argb32 formats from xrender we assume
67 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender
68 int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24;
69
70 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
71
72 XImage xi;
73
74 xi.width = width;
75 xi.height = height;
76 xi.xoffset = 0;
77 xi.format = ZPixmap;
78 xi.byte_order = ImageByteOrder (dpy);
79 xi.bitmap_unit = 0; //XY only, unused
80 xi.bitmap_bit_order = 0; //XY only, unused
81 xi.bitmap_pad = BitmapPad (dpy);
82 xi.depth = depth;
83 xi.bytes_per_line = 0;
84 xi.bits_per_pixel = 32; //Z only
85 xi.red_mask = 0x00000000; //Z only, unused
86 xi.green_mask = 0x00000000; //Z only, unused
87 xi.blue_mask = 0x00000000; //Z only, unused
88 xi.obdata = 0; // probably unused
89
90 bool byte_order_mismatch = byte_order != xi.byte_order;
91
92 if (!XInitImage (&xi))
93 rxvt_fatal ("unable to initialise ximage, please report.\n");
94
95 if (height > INT_MAX / xi.bytes_per_line)
96 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
97
98 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
99
100 int rowstride = gdk_pixbuf_get_rowstride (pb);
101
102 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
103 unsigned char *row = gdk_pixbuf_get_pixels (pb);
104 char *line = xi.data;
105
106 for (int y = 0; y < height; y++)
107 {
108 unsigned char *src = row;
109 uint32_t *dst = (uint32_t *)line;
110
111 if (depth == 24)
112 for (int x = 0; x < width; x++)
113 {
114 uint8_t r = *src++;
115 uint8_t g = *src++;
116 uint8_t b = *src++;
117
118 uint32_t v = (r << 16) | (g << 8) | b;
119
120 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
121 v = ecb_bswap32 (v);
122
123 *dst++ = v;
124 }
125 else
126 for (int x = 0; x < width; x++)
127 {
128 uint32_t v = *(uint32_t *)src; src += 4;
129
130 if (ecb_big_endian ())
131 v = ecb_bswap32 (v);
132
133 v = ecb_rotl32 (v, 8); // abgr to bgra
134
135 if (!byte_order_mismatch)
136 v = ecb_bswap32 (v);
137
138 *dst++ = v;
139 }
140
141 row += rowstride;
142 line += xi.bytes_per_line;
143 }
144
145 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height);
146 img->alloc ();
147
148 GC gc = XCreateGC (dpy, img->pm, 0, 0);
149 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
150 XFreeGC (dpy, gc);
151
152 free (xi.data);
153
154 return img;
155 }
156
157 rxvt_img *
158 rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
159 {
160 GError *err = 0;
161 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
162
163 if (!pb)
164 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
165
166 rxvt_img *img = new_from_pixbuf (s, pb);
167
168 g_object_unref (pb);
169
170 return img;
171 }
172 # endif
173
174 void
175 rxvt_img::destroy ()
176 {
177 if (--ref->cnt)
178 return;
179
180 if (pm && ref->ours)
181 XFreePixmap (s->display->dpy, pm);
182
183 delete ref;
184 }
185
186 rxvt_img::~rxvt_img ()
187 {
188 destroy ();
189 }
190
191 void
192 rxvt_img::alloc ()
193 {
194 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
195 ref = new pixref (w, h);
196 }
197
198 Picture
199 rxvt_img::src_picture ()
200 {
201 Display *dpy = s->display->dpy;
202
203 XRenderPictureAttributes pa;
204 pa.repeat = repeat;
205 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
206
207 return pic;
208 }
209
210 void
211 rxvt_img::unshare ()
212 {
213 if (ref->cnt == 1 && ref->ours)
214 return;
215
216 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
217 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
218 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
219 XFreeGC (s->display->dpy, gc);
220
221 destroy ();
222
223 pm = pm2;
224 ref = new pixref (ref->w, ref->h);
225 }
226
227 void
228 rxvt_img::fill (const rxvt_color &c)
229 {
230 XGCValues gcv;
231 gcv.foreground = c;
232 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv);
233 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h);
234 XFreeGC (s->display->dpy, gc);
235 }
236
237 static void
238 get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
239 {
240 double sigma = radius / 2.0;
241 double scale = sqrt (2.0 * M_PI) * sigma;
242 double sum = 0.0;
243
244 for (int i = 0; i < width; i++)
245 {
246 double x = i - width / 2;
247 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
248 sum += kernel[i];
249 }
250
251 params[0] = XDoubleToFixed (width);
252 params[1] = XDoubleToFixed (1);
253
254 for (int i = 0; i < width; i++)
255 params[i+2] = XDoubleToFixed (kernel[i] / sum);
256 }
257
258 rxvt_img *
259 rxvt_img::blur (int rh, int rv)
260 {
261 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
262 return clone ();
263
264 Display *dpy = s->display->dpy;
265 int size = max (rh, rv) * 2 + 1;
266 double *kernel = (double *)malloc (size * sizeof (double));
267 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
268 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
269 img->alloc ();
270
271 XRenderPictureAttributes pa;
272 pa.repeat = RepeatPad;
273 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
274 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
275
276 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
277 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
278 XFreePixmap (dpy, tmp_pm);
279
280 if (kernel && params)
281 {
282 size = rh * 2 + 1;
283 get_gaussian_kernel (rh, size, kernel, params);
284
285 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
286 XRenderComposite (dpy,
287 PictOpSrc,
288 src,
289 None,
290 tmp,
291 0, 0,
292 0, 0,
293 0, 0,
294 w, h);
295
296 size = rv * 2 + 1;
297 get_gaussian_kernel (rv, size, kernel, params);
298 ::swap (params[0], params[1]);
299
300 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
301 XRenderComposite (dpy,
302 PictOpSrc,
303 tmp,
304 None,
305 dst,
306 0, 0,
307 0, 0,
308 0, 0,
309 w, h);
310 }
311
312 free (kernel);
313 free (params);
314 XRenderFreePicture (dpy, src);
315 XRenderFreePicture (dpy, dst);
316 XRenderFreePicture (dpy, tmp);
317
318 return img;
319 }
320
321 static Picture
322 create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
323 {
324 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
325
326 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
327 XRenderPictureAttributes pa;
328 pa.repeat = RepeatNormal;
329 pa.component_alpha = component_alpha;
330 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
331
332 XFreePixmap (dpy, pixmap);
333
334 return mask;
335 }
336
337 static void
338 extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
339 {
340 int32_t x = clamp (c, cl0, cl1);
341 c -= x;
342 xc = x;
343 }
344
345 static bool
346 extract (int32_t cl0, int32_t cl1, int32_t &r, int32_t &g, int32_t &b, int32_t &a, unsigned short &xr, unsigned short &xg, unsigned short &xb, unsigned short &xa)
347 {
348 extract (cl0, cl1, r, xr);
349 extract (cl0, cl1, g, xg);
350 extract (cl0, cl1, b, xb);
351 extract (cl0, cl1, a, xa);
352
353 return xr | xg | xb | xa;
354 }
355
356 void
357 rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
358 {
359 unshare ();
360
361 Display *dpy = s->display->dpy;
362 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
363
364 // loop should not be needed for brightness, as only -1..1 makes sense
365 //while (r | g | b | a)
366 {
367 unsigned short xr, xg, xb, xa;
368 XRenderColor mask_c;
369
370 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
371 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
372
373 if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
374 {
375 XRenderColor mask_w = { 65535, 65535, 65535, 65535 };
376 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
377 mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing
378 mask_c.green = -mask_c.green;
379 mask_c.blue = -mask_c.blue;
380 mask_c.alpha = -mask_c.alpha;
381 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
382 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
383 }
384 }
385
386 XRenderFreePicture (dpy, dst);
387 }
388
389 void
390 rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a)
391 {
392 if (r < 0 || g < 0 || b < 0 || a < 0)
393 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
394
395 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
396 img->alloc ();
397
398 {
399 rxvt_color empty;
400 empty.set (s, rgba (0, 0, 0, 0));
401 img->fill (empty);
402 }
403
404 // premultiply (yeah, these are not exact, sue me or fix it)
405 r = (r * (a >> 8)) >> 8;
406 g = (g * (a >> 8)) >> 8;
407 b = (b * (a >> 8)) >> 8;
408
409 Display *dpy = s->display->dpy;
410
411 Picture src = src_picture ();
412 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
413 Picture mul = create_xrender_mask (dpy, pm, True, True);
414
415 //TODO: this operator does not yet implement some useful contrast
416 while (r | g | b | a)
417 {
418 unsigned short xr, xg, xb, xa;
419 XRenderColor mask_c;
420
421 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
422 {
423 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
424 XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h);
425 }
426 }
427
428 XRenderFreePicture (dpy, mul);
429 XRenderFreePicture (dpy, dst);
430 XRenderFreePicture (dpy, src);
431
432 ::swap (img->ref, ref);
433 ::swap (img->pm , pm );
434
435 delete img;
436 }
437
438 rxvt_img *
439 rxvt_img::clone ()
440 {
441 return new rxvt_img (*this);
442 }
443
444 static XRenderPictFormat *
445 find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
446 {
447 if (format->direct.alphaMask)
448 return format; // already has alpha
449
450 // try to find a suitable alpha format, one bit alpha is enough for our purposes
451 if (format->type == PictTypeDirect)
452 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
453 if (f->direct.alphaMask
454 && f->type == PictTypeDirect
455 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
456 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
457 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
458 return f;
459
460 // should be a very good fallback
461 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
462 }
463
464 rxvt_img *
465 rxvt_img::reify ()
466 {
467 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
468 return clone ();
469
470 Display *dpy = s->display->dpy;
471
472 // add an alpha channel if...
473 bool alpha = !format->direct.alphaMask // pixmap has none yet
474 && (x || y) // we need one because of non-zero offset
475 && repeat == RepeatNone; // and we have no good pixels to fill with
476
477 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
478 img->alloc ();
479
480 Picture src = src_picture ();
481 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
482
483 if (alpha)
484 {
485 XRenderColor rc = { 0, 0, 0, 0 };
486 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
487 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h);
488 }
489 else
490 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
491
492 XRenderFreePicture (dpy, src);
493 XRenderFreePicture (dpy, dst);
494
495 return img;
496 }
497
498 rxvt_img *
499 rxvt_img::sub_rect (int x, int y, int width, int height)
500 {
501 rxvt_img *img = clone ();
502
503 img->x += x;
504 img->y += y;
505
506 if (w != width || h != height)
507 {
508 img->w = width;
509 img->h = height;
510
511 rxvt_img *img2 = img->reify ();
512 delete img;
513 img = img2;
514 }
515
516 return img;
517 }
518
519 rxvt_img *
520 rxvt_img::transform (int new_width, int new_height, double matrix[9])
521 {
522 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat);
523 img->alloc ();
524
525 Display *dpy = s->display->dpy;
526 Picture src = src_picture ();
527 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
528
529 XTransform xfrm;
530
531 for (int i = 0; i < 3; ++i)
532 for (int j = 0; j < 3; ++j)
533 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
534
535 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
536 xfrm.matrix [1][2] -= XDoubleToFixed (y);
537
538 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
539 XRenderSetPictureTransform (dpy, src, &xfrm);
540 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
541
542 XRenderFreePicture (dpy, src);
543 XRenderFreePicture (dpy, dst);
544
545 return img;
546 }
547
548 rxvt_img *
549 rxvt_img::scale (int new_width, int new_height)
550 {
551 if (w == new_width && h == new_height)
552 return clone ();
553
554 double matrix[9] = {
555 w / (double)new_width, 0, 0,
556 0, h / (double)new_height, 0,
557 0, 0, 1
558 };
559
560 int old_repeat_mode = repeat;
561 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems
562
563 rxvt_img *img = transform (new_width, new_height, matrix);
564
565 repeat = old_repeat_mode;
566 img->repeat = repeat;
567
568 return img;
569 }
570
571 rxvt_img *
572 rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
573 {
574 double s = sin (phi);
575 double c = cos (phi);
576
577 double matrix[9] = {
578 c, -s, -c * x + s * y + x,
579 s, c, -s * x - c * y + y,
580 0, 0, 1
581 };
582
583 return transform (new_width, new_height, matrix);
584 }
585
586 rxvt_img *
587 rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg)
588 {
589 if (new_format == format)
590 return clone ();
591
592 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
593 img->alloc ();
594
595 Display *dpy = s->display->dpy;
596 Picture src = src_picture ();
597 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
598 int op = PictOpSrc;
599
600 if (format->direct.alphaMask && !new_format->direct.alphaMask)
601 {
602 // does it have to be that complicated
603 rgba c;
604 bg.get (c);
605
606 XRenderColor rc = { c.r, c.g, c.b, 0xffff };
607 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
608
609 op = PictOpOver;
610 }
611
612 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
613
614 XRenderFreePicture (dpy, src);
615 XRenderFreePicture (dpy, dst);
616
617 return img;
618 }
619
620 rxvt_img *
621 rxvt_img::blend (rxvt_img *img, double factor)
622 {
623 rxvt_img *img2 = clone ();
624 Display *dpy = s->display->dpy;
625 Picture src = img->src_picture ();
626 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
627 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
628
629 XRenderColor mask_c;
630
631 mask_c.alpha = float_to_component (factor);
632 mask_c.red =
633 mask_c.green =
634 mask_c.blue = 0;
635 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
636
637 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h);
638
639 XRenderFreePicture (dpy, src);
640 XRenderFreePicture (dpy, dst);
641 XRenderFreePicture (dpy, mask);
642
643 return img2;
644 }
645
646 #endif
647