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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines