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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines