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.48 by root, Thu Jun 7 19:24:02 2012 UTC vs.
Revision 1.78 by sf-exg, Mon Jun 11 12:08:30 2012 UTC

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), ref(img.ref) 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 ++ref->cnt; 16 ++ref->cnt;
17} 17}
18
19#if 0
20rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap)
21: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
22{
23}
24#endif
25 18
26rxvt_img * 19rxvt_img *
27rxvt_img::new_from_root (rxvt_screen *s) 20rxvt_img::new_from_root (rxvt_screen *s)
28{ 21{
29 Display *dpy = s->display->dpy; 22 Display *dpy = s->display->dpy;
56 img->ref->ours = false; 49 img->ref->ours = false;
57 50
58 return img; 51 return img;
59} 52}
60 53
54# if HAVE_PIXBUF
55
61rxvt_img * 56rxvt_img *
62rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) 57rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
63{ 58{
64 Display *dpy = s->display->dpy; 59 Display *dpy = s->display->dpy;
65 60
69 if (width > 32767 || height > 32767) // well, we *could* upload in chunks 64 if (width > 32767 || height > 32767) // well, we *could* upload in chunks
70 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); 65 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
71 66
72 // since we require rgb24/argb32 formats from xrender we assume 67 // since we require rgb24/argb32 formats from xrender we assume
73 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender 68 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender
74 int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; 69
70 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
75 71
76 XImage xi; 72 XImage xi;
77 73
78 xi.width = width; 74 xi.width = width;
79 xi.height = height; 75 xi.height = height;
80 xi.xoffset = 0; 76 xi.xoffset = 0;
81 xi.format = ZPixmap; 77 xi.format = ZPixmap;
82 xi.byte_order = MSBFirst; // maybe go for host byte order, because servers are usually local? 78 xi.byte_order = ImageByteOrder (dpy);
83 xi.bitmap_unit = 32; 79 xi.bitmap_unit = 0; //XY only, unused
84 xi.bitmap_bit_order = MSBFirst; 80 xi.bitmap_bit_order = 0; //XY only, unused
85 xi.bitmap_pad = BitmapPad (dpy); 81 xi.bitmap_pad = BitmapPad (dpy);
86 xi.depth = depth; 82 xi.depth = 32;
87 xi.bytes_per_line = 0; 83 xi.bytes_per_line = 0;
88 xi.bits_per_pixel = 32; 84 xi.bits_per_pixel = 32; //Z only
89 xi.red_mask = 0x00ff0000; 85 xi.red_mask = 0x00000000; //Z only, unused
90 xi.green_mask = 0x0000ff00; 86 xi.green_mask = 0x00000000; //Z only, unused
91 xi.blue_mask = 0x000000ff; 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;
92 91
93 if (!XInitImage (&xi)) 92 if (!XInitImage (&xi))
94 rxvt_fatal ("unable to initialise ximage, please report.\n"); 93 rxvt_fatal ("unable to initialise ximage, please report.\n");
95 94
96 if (height > INT_MAX / xi.bytes_per_line) 95 if (height > INT_MAX / xi.bytes_per_line)
97 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); 96 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
98 97
99 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 98 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
100 99
101 int rowstride = gdk_pixbuf_get_rowstride (pb); 100 int rowstride = gdk_pixbuf_get_rowstride (pb);
102 101 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
103 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
104 unsigned char *row = gdk_pixbuf_get_pixels (pb); 102 unsigned char *row = gdk_pixbuf_get_pixels (pb);
103
105 char *line = xi.data; 104 char *line = xi.data;
106 105
107 for (int y = 0; y < height; y++) 106 for (int y = 0; y < height; y++)
108 { 107 {
109 unsigned char r, g, b, a;
110 unsigned char *data = row; 108 unsigned char *src = row;
109 uint32_t *dst = (uint32_t *)line;
111 110
112 if (depth == 24) 111 if (!pb_has_alpha)
113 for (int x = 0; x < width; x++) 112 for (int x = 0; x < width; x++)
114 { 113 {
115 asm volatile("nop"); 114 uint8_t r = *src++;
116 r = *data++; 115 uint8_t g = *src++;
117 g = *data++; 116 uint8_t b = *src++;
118 b = *data++; 117
118 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
119
120 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
121 v = ecb_bswap32 (v);
122
119 *line++ = 0; 123 *dst++ = v;
120 *line++ = r;
121 *line++ = g;
122 *line++ = b;
123 asm volatile("nop");
124 } 124 }
125 else 125 else
126 for (int x = 0; x < width; x++) 126 for (int x = 0; x < width; x++)
127 { 127 {
128 uint32_t v = *(uint32_t *)data; data += 4; 128 uint32_t v = *(uint32_t *)src; src += 4;
129 v = ecb_big_endian () ? ecb_rotr32 (v, 8) : ecb_rotl32 (v, 8); 129
130 *(uint32_t *)line = x; line += 4; 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;
131 } 139 }
132 140
133 row += rowstride; 141 row += rowstride;
142 line += xi.bytes_per_line;
134 } 143 }
135 144
136 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); 145 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
137 img->alloc (); 146 img->alloc ();
138 147
139 GC gc = XCreateGC (dpy, img->pm, 0, 0); 148 GC gc = XCreateGC (dpy, img->pm, 0, 0);
140 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); 149 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
141 XFreeGC (dpy, gc); 150 XFreeGC (dpy, gc);
159 g_object_unref (pb); 168 g_object_unref (pb);
160 169
161 return img; 170 return img;
162} 171}
163 172
173# endif
174
164void 175void
165rxvt_img::destroy () 176rxvt_img::destroy ()
166{ 177{
167 if (--ref->cnt) 178 if (--ref->cnt)
168 return; 179 return;
201rxvt_img::unshare () 212rxvt_img::unshare ()
202{ 213{
203 if (ref->cnt == 1 && ref->ours) 214 if (ref->cnt == 1 && ref->ours)
204 return; 215 return;
205 216
206 //TODO: maybe should reify instead
207 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); 217 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
208 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 218 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
209 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); 219 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
210 XFreeGC (s->display->dpy, gc); 220 XFreeGC (s->display->dpy, gc);
211 221
216} 226}
217 227
218void 228void
219rxvt_img::fill (const rxvt_color &c) 229rxvt_img::fill (const rxvt_color &c)
220{ 230{
221 XGCValues gcv; 231 rgba cc;
222 gcv.foreground = c; 232 c.get (cc);
223 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 233 XRenderColor rc = { cc.r, cc.g, cc.b, cc.a };
224 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 234
225 XFreeGC (s->display->dpy, gc); 235 Display *dpy = s->display->dpy;
236 Picture src = src_picture ();
237 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
238 XRenderFreePicture (dpy, src);
226} 239}
227 240
228static void 241static void
229get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 242get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
230{ 243{
257 double *kernel = (double *)malloc (size * sizeof (double)); 270 double *kernel = (double *)malloc (size * sizeof (double));
258 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 271 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
259 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 272 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
260 img->alloc (); 273 img->alloc ();
261 274
262 Picture src = src_picture ();
263
264 XRenderPictureAttributes pa; 275 XRenderPictureAttributes pa;
265 pa.repeat = RepeatPad; 276 pa.repeat = RepeatPad;
266 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); 277 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
278 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
267 279
268 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 280 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
269 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 281 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
270 XFreePixmap (dpy, tmp_pm); 282 XFreePixmap (dpy, tmp_pm);
271 283
287 299
288 size = rv * 2 + 1; 300 size = rv * 2 + 1;
289 get_gaussian_kernel (rv, size, kernel, params); 301 get_gaussian_kernel (rv, size, kernel, params);
290 ::swap (params[0], params[1]); 302 ::swap (params[0], params[1]);
291 303
292 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 304 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
293 XRenderComposite (dpy, 305 XRenderComposite (dpy,
294 PictOpSrc, 306 PictOpSrc,
295 tmp, 307 tmp,
296 None, 308 None,
297 dst, 309 dst,
301 w, h); 313 w, h);
302 } 314 }
303 315
304 free (kernel); 316 free (kernel);
305 free (params); 317 free (params);
318
306 XRenderFreePicture (dpy, src); 319 XRenderFreePicture (dpy, src);
307 XRenderFreePicture (dpy, dst); 320 XRenderFreePicture (dpy, dst);
308 XRenderFreePicture (dpy, tmp); 321 XRenderFreePicture (dpy, tmp);
309 322
310 return img; 323 return img;
311} 324}
312 325
313static Picture 326static Picture
314create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 327create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
315{ 328{
316 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 329 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
317 330
318 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 331 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
319 XRenderPictureAttributes pa; 332 XRenderPictureAttributes pa;
320 pa.repeat = True; 333 pa.repeat = RepeatNormal;
334 pa.component_alpha = component_alpha;
321 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 335 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
322 336
323 XFreePixmap (dpy, pixmap); 337 XFreePixmap (dpy, pixmap);
324 338
325 return mask; 339 return mask;
326} 340}
327 341
342static void
343extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
344{
345 int32_t x = clamp (c, cl0, cl1);
346 c -= x;
347 xc = x;
348}
349
350static bool
351extract (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)
352{
353 extract (cl0, cl1, r, xr);
354 extract (cl0, cl1, g, xg);
355 extract (cl0, cl1, b, xb);
356 extract (cl0, cl1, a, xa);
357
358 return xr | xg | xb | xa;
359}
360
328void 361void
329rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 362rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
330{ 363{
364 unshare ();
365
331 Display *dpy = s->display->dpy; 366 Display *dpy = s->display->dpy;
332 Picture src = create_xrender_mask (dpy, pm, True);
333 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 367 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
334 368
369 // loop should not be needed for brightness, as only -1..1 makes sense
370 //while (r | g | b | a)
371 {
372 unsigned short xr, xg, xb, xa;
335 XRenderColor mask_c; 373 XRenderColor mask_c;
336 mask_c.red = r; 374
337 mask_c.green = g; 375 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
338 mask_c.blue = b; 376 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
339 mask_c.alpha = a; 377
378 if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
379 {
380 XRenderColor mask_w = { 65535, 65535, 65535, 65535 };
381 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
382 mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing
383 mask_c.green = -mask_c.green;
384 mask_c.blue = -mask_c.blue;
385 mask_c.alpha = -mask_c.alpha;
386 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
387 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
388 }
389 }
390
391 XRenderFreePicture (dpy, dst);
392}
393
394void
395rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a)
396{
397 if (r < 0 || g < 0 || b < 0 || a < 0)
398 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
399
400 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
401 img->alloc ();
402
403 {
404 rxvt_color empty;
405 empty.set (s, rgba (0, 0, 0, 0));
406 img->fill (empty);
407 }
408
409 // premultiply (yeah, these are not exact, sue me or fix it)
410 r = (r * (a >> 8)) >> 8;
411 g = (g * (a >> 8)) >> 8;
412 b = (b * (a >> 8)) >> 8;
413
414 Display *dpy = s->display->dpy;
415
416 Picture src = src_picture ();
417 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
418 Picture mul = create_xrender_mask (dpy, pm, True, True);
419
420 //TODO: this operator does not yet implement some useful contrast
421 while (r | g | b | a)
422 {
423 unsigned short xr, xg, xb, xa;
424 XRenderColor mask_c;
425
426 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
427 {
340 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 428 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
341
342 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 429 XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h);
430 }
431 }
343 432
433 XRenderFreePicture (dpy, mul);
434 XRenderFreePicture (dpy, dst);
344 XRenderFreePicture (dpy, src); 435 XRenderFreePicture (dpy, src);
345 XRenderFreePicture (dpy, dst);
346}
347 436
348void 437 ::swap (img->ref, ref);
349rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 438 ::swap (img->pm , pm );
350{
351 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
352 return;
353 439
354 Display *dpy = s->display->dpy; 440 delete img;
355 Picture src = create_xrender_mask (dpy, pm, True);
356 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
357
358 XRenderColor mask_c;
359 mask_c.red = r;
360 mask_c.green = g;
361 mask_c.blue = b;
362 mask_c.alpha = a;
363 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
364
365 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
366
367 XRenderFreePicture (dpy, src);
368 XRenderFreePicture (dpy, dst);
369} 441}
370 442
371rxvt_img * 443rxvt_img *
372rxvt_img::clone () 444rxvt_img::clone ()
373{ 445{
400 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 472 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
401 return clone (); 473 return clone ();
402 474
403 Display *dpy = s->display->dpy; 475 Display *dpy = s->display->dpy;
404 476
477 // add an alpha channel if...
405 bool alpha = !format->direct.alphaMask 478 bool alpha = !format->direct.alphaMask // pixmap has none yet
406 && (x || y) 479 && (x || y) // we need one because of non-zero offset
407 && repeat == RepeatNone; 480 && repeat == RepeatNone; // and we have no good pixels to fill with
408 481
409 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); 482 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
410 img->alloc (); 483 img->alloc ();
411 484
412 Picture src = src_picture (); 485 Picture src = src_picture ();
462 535
463 for (int i = 0; i < 3; ++i) 536 for (int i = 0; i < 3; ++i)
464 for (int j = 0; j < 3; ++j) 537 for (int j = 0; j < 3; ++j)
465 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 538 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
466 539
467#if 0
468 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO 540 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
469 xfrm.matrix [1][2] -= XDoubleToFixed (y); 541 xfrm.matrix [1][2] -= XDoubleToFixed (y);
470#endif
471 542
472 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 543 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
473 XRenderSetPictureTransform (dpy, src, &xfrm); 544 XRenderSetPictureTransform (dpy, src, &xfrm);
474 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 545 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
475 546
490 0, h / (double)new_height, 0, 561 0, h / (double)new_height, 0,
491 0, 0, 1 562 0, 0, 1
492 }; 563 };
493 564
494 int old_repeat_mode = repeat; 565 int old_repeat_mode = repeat;
495 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems 566 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
496 567
497 rxvt_img *img = transform (new_width, new_height, matrix); 568 rxvt_img *img = transform (new_width, new_height, matrix);
498 569
499 repeat = old_repeat_mode; 570 repeat = old_repeat_mode;
500 img->repeat = repeat; 571 img->repeat = repeat;
556{ 627{
557 rxvt_img *img2 = clone (); 628 rxvt_img *img2 = clone ();
558 Display *dpy = s->display->dpy; 629 Display *dpy = s->display->dpy;
559 Picture src = img->src_picture (); 630 Picture src = img->src_picture ();
560 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 631 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
561 Picture mask = create_xrender_mask (dpy, img->pm, False); 632 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
562 633
563 XRenderColor mask_c; 634 XRenderColor mask_c;
564 635
565 mask_c.alpha = float_to_component (factor); 636 mask_c.alpha = float_to_component (factor);
566 mask_c.red = 637 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines