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.74 by root, Sat Jun 9 19:20:04 2012 UTC vs.
Revision 1.82 by root, Thu Jun 14 17:06:57 2012 UTC

50 50
51 return img; 51 return img;
52} 52}
53 53
54# if HAVE_PIXBUF 54# if HAVE_PIXBUF
55
55rxvt_img * 56rxvt_img *
56rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) 57rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
57{ 58{
58 Display *dpy = s->display->dpy; 59 Display *dpy = s->display->dpy;
59 60
63 if (width > 32767 || height > 32767) // well, we *could* upload in chunks 64 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 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
65 66
66 // since we require rgb24/argb32 formats from xrender we assume 67 // 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 // 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 69
70 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 70 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
71 71
72 XImage xi; 72 XImage xi;
73 73
77 xi.format = ZPixmap; 77 xi.format = ZPixmap;
78 xi.byte_order = ImageByteOrder (dpy); 78 xi.byte_order = ImageByteOrder (dpy);
79 xi.bitmap_unit = 0; //XY only, unused 79 xi.bitmap_unit = 0; //XY only, unused
80 xi.bitmap_bit_order = 0; //XY only, unused 80 xi.bitmap_bit_order = 0; //XY only, unused
81 xi.bitmap_pad = BitmapPad (dpy); 81 xi.bitmap_pad = BitmapPad (dpy);
82 xi.depth = depth; 82 xi.depth = 32;
83 xi.bytes_per_line = 0; 83 xi.bytes_per_line = 0;
84 xi.bits_per_pixel = 32; //Z only 84 xi.bits_per_pixel = 32; //Z only
85 xi.red_mask = 0x00000000; //Z only, unused 85 xi.red_mask = 0x00000000; //Z only, unused
86 xi.green_mask = 0x00000000; //Z only, unused 86 xi.green_mask = 0x00000000; //Z only, unused
87 xi.blue_mask = 0x00000000; //Z only, unused 87 xi.blue_mask = 0x00000000; //Z only, unused
96 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");
97 97
98 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 98 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
99 99
100 int rowstride = gdk_pixbuf_get_rowstride (pb); 100 int rowstride = gdk_pixbuf_get_rowstride (pb);
101 101 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
102 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
103 unsigned char *row = gdk_pixbuf_get_pixels (pb); 102 unsigned char *row = gdk_pixbuf_get_pixels (pb);
103
104 char *line = xi.data; 104 char *line = xi.data;
105 105
106 for (int y = 0; y < height; y++) 106 for (int y = 0; y < height; y++)
107 { 107 {
108 unsigned char *src = row; 108 unsigned char *src = row;
109 uint32_t *dst = (uint32_t *)line; 109 uint32_t *dst = (uint32_t *)line;
110 110
111 if (depth == 24) 111 if (!pb_has_alpha)
112 for (int x = 0; x < width; x++) 112 for (int x = 0; x < width; x++)
113 { 113 {
114 uint8_t r = *src++; 114 uint8_t r = *src++;
115 uint8_t g = *src++; 115 uint8_t g = *src++;
116 uint8_t b = *src++; 116 uint8_t b = *src++;
117 117
118 uint32_t v = (r << 16) | (g << 8) | b; 118 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
119 119
120 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) 120 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
121 v = ecb_bswap32 (v); 121 v = ecb_bswap32 (v);
122 122
123 *dst++ = v; 123 *dst++ = v;
140 140
141 row += rowstride; 141 row += rowstride;
142 line += xi.bytes_per_line; 142 line += xi.bytes_per_line;
143 } 143 }
144 144
145 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);
146 img->alloc (); 146 img->alloc ();
147 147
148 GC gc = XCreateGC (dpy, img->pm, 0, 0); 148 GC gc = XCreateGC (dpy, img->pm, 0, 0);
149 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);
150 XFreeGC (dpy, gc); 150 XFreeGC (dpy, gc);
167 167
168 g_object_unref (pb); 168 g_object_unref (pb);
169 169
170 return img; 170 return img;
171} 171}
172
172# endif 173# endif
173 174
174void 175void
175rxvt_img::destroy () 176rxvt_img::destroy ()
176{ 177{
223 pm = pm2; 224 pm = pm2;
224 ref = new pixref (ref->w, ref->h); 225 ref = new pixref (ref->w, ref->h);
225} 226}
226 227
227void 228void
228rxvt_img::fill (const rxvt_color &c) 229rxvt_img::fill (const rgba &c)
229{ 230{
230 XGCValues gcv; 231 XRenderColor rc = { c.r, c.g, c.b, c.a };
231 gcv.foreground = c; 232
232 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 233 Display *dpy = s->display->dpy;
233 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 234 Picture src = src_picture ();
234 XFreeGC (s->display->dpy, gc); 235 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
236 XRenderFreePicture (dpy, src);
235} 237}
236 238
237static void 239static void
238get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 240get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
239{ 241{
309 w, h); 311 w, h);
310 } 312 }
311 313
312 free (kernel); 314 free (kernel);
313 free (params); 315 free (params);
316
314 XRenderFreePicture (dpy, src); 317 XRenderFreePicture (dpy, src);
315 XRenderFreePicture (dpy, dst); 318 XRenderFreePicture (dpy, dst);
316 XRenderFreePicture (dpy, tmp); 319 XRenderFreePicture (dpy, tmp);
317 320
318 return img; 321 return img;
319} 322}
320 323
321static Picture 324static Picture
322create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 325create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
323{ 326{
324 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 327 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
325 328
326 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 329 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
327 XRenderPictureAttributes pa; 330 XRenderPictureAttributes pa;
328 pa.repeat = RepeatNormal; 331 pa.repeat = RepeatNormal;
332 pa.component_alpha = component_alpha;
329 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 333 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
330 334
331 XFreePixmap (dpy, pixmap); 335 XFreePixmap (dpy, pixmap);
332 336
333 return mask; 337 return mask;
334} 338}
391 if (r < 0 || g < 0 || b < 0 || a < 0) 395 if (r < 0 || g < 0 || b < 0 || a < 0)
392 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 396 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
393 397
394 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 398 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
395 img->alloc (); 399 img->alloc ();
396 400 img->fill (rgba (0, 0, 0, 0));
397 {
398 rxvt_color empty;
399 empty.set (s, rgba (0, 0, 0, 0));
400 img->fill (empty);
401 }
402 401
403 // premultiply (yeah, these are not exact, sue me or fix it) 402 // premultiply (yeah, these are not exact, sue me or fix it)
404 r = (r * (a >> 8)) >> 8; 403 r = (r * (a >> 8)) >> 8;
405 g = (g * (a >> 8)) >> 8; 404 g = (g * (a >> 8)) >> 8;
406 b = (b * (a >> 8)) >> 8; 405 b = (b * (a >> 8)) >> 8;
407 406
408 Display *dpy = s->display->dpy; 407 Display *dpy = s->display->dpy;
409 408
410 Picture src = src_picture (); 409 Picture src = src_picture ();
411 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); 410 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
412 Picture mul = create_xrender_mask (dpy, pm, True); 411 Picture mul = create_xrender_mask (dpy, pm, True, True);
413
414 XRenderPictureAttributes pa;
415 pa.component_alpha = 1;
416 XRenderChangePicture (dpy, mul, CPComponentAlpha, &pa);
417 412
418 //TODO: this operator does not yet implement some useful contrast 413 //TODO: this operator does not yet implement some useful contrast
419 while (r | g | b | a) 414 while (r | g | b | a)
420 { 415 {
421 unsigned short xr, xg, xb, xa; 416 unsigned short xr, xg, xb, xa;
517 } 512 }
518 513
519 return img; 514 return img;
520} 515}
521 516
522rxvt_img * 517static void
523rxvt_img::transform (int new_width, int new_height, double matrix[9]) 518mat_invert (double mat[3][3], double (&inv)[3][3])
524{ 519{
520 double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
521 double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
522 double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
523
524 double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
525
526 inv [0][0] = invdet * s0;
527 inv [0][1] = invdet * s1;
528 inv [0][2] = invdet * s2;
529
530 inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
531 inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
532 inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
533
534 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
535 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
536 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
537}
538
539static double
540mat_apply (double mat[3][3], int i, double x, double y)
541{
542 double v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
543 double w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
544
545 return v * (1. / w);
546}
547
548rxvt_img *
549rxvt_img::transform (double matrix[3][3])
550{
551 // find new offset
552 int ox = mat_apply (matrix, 0, x, y);
553 int oy = mat_apply (matrix, 1, x, y);
554
555 // calculate new pixel bounding box coordinates
556 double d [2], rmin[2], rmax[2];
557
558 for (int i = 0; i < 2; ++i)
559 {
560 double v;
561 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
562 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
563 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v);
564 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v);
565 }
566
567 int dx = floor (rmin [0]);
568 int dy = floor (rmin [1]);
569
570 int new_width = ceil (rmax [0] - dx);
571 int new_height = ceil (rmax [1] - dy);
572
573 double inv[3][3];
574 mat_invert (matrix, inv);
575
525 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); 576 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
526 img->alloc (); 577 img->alloc ();
527 578
528 Display *dpy = s->display->dpy; 579 Display *dpy = s->display->dpy;
529 Picture src = src_picture (); 580 Picture src = src_picture ();
530 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 581 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
531 582
532 XTransform xfrm; 583 XTransform xfrm;
533 584
534 for (int i = 0; i < 3; ++i) 585 for (int i = 0; i < 3; ++i)
535 for (int j = 0; j < 3; ++j) 586 for (int j = 0; j < 3; ++j)
536 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 587 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
537
538 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
539 xfrm.matrix [1][2] -= XDoubleToFixed (y);
540 588
541 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 589 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
542 XRenderSetPictureTransform (dpy, src, &xfrm); 590 XRenderSetPictureTransform (dpy, src, &xfrm);
543 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 591 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
544 592
545 XRenderFreePicture (dpy, src); 593 XRenderFreePicture (dpy, src);
546 XRenderFreePicture (dpy, dst); 594 XRenderFreePicture (dpy, dst);
547 595
548 return img; 596 return img;
552rxvt_img::scale (int new_width, int new_height) 600rxvt_img::scale (int new_width, int new_height)
553{ 601{
554 if (w == new_width && h == new_height) 602 if (w == new_width && h == new_height)
555 return clone (); 603 return clone ();
556 604
557 double matrix[9] = { 605 double matrix[3][3] = {
558 w / (double)new_width, 0, 0, 606 { new_width / (double)w, 0, 0 },
559 0, h / (double)new_height, 0, 607 { 0, new_height / (double)h, 0 },
560 0, 0, 1 608 { 0, 0, 1 }
561 }; 609 };
562 610
563 int old_repeat_mode = repeat; 611 int old_repeat_mode = repeat;
564 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems 612 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
565 613
566 rxvt_img *img = transform (new_width, new_height, matrix); 614 rxvt_img *img = transform (matrix);
567 615
568 repeat = old_repeat_mode; 616 repeat = old_repeat_mode;
569 img->repeat = repeat; 617 img->repeat = repeat;
570 618
571 return img; 619 return img;
572} 620}
573 621
574rxvt_img * 622rxvt_img *
575rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 623rxvt_img::rotate (int cx, int cy, double phi)
576{ 624{
577 double s = sin (phi); 625 double s = sin (phi);
578 double c = cos (phi); 626 double c = cos (phi);
579 627
580 double matrix[9] = { 628 double matrix[3][3] = {
581 c, -s, -c * x + s * y + x, 629 { c, -s, cx - c * cx + s * cy },
582 s, c, -s * x - c * y + y, 630 { s, c, cy - s * cx - c * cy },
583 0, 0, 1 631 { 0, 0, 1 }
632 //{ c, -s, 0 },
633 //{ s, c, 0 },
634 //{ 0, 0, 1 }
584 }; 635 };
585 636
586 return transform (new_width, new_height, matrix); 637 //move (-cx, -cy);
587} 638 rxvt_img *img = transform (matrix);
639 //move ( cx, cy);
640 //img->move (cx, cy);
588 641
642 return img;
643}
644
589rxvt_img * 645rxvt_img *
590rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 646rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
591{ 647{
592 if (new_format == format) 648 if (new_format == format)
593 return clone (); 649 return clone ();
594 650
595 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 651 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
601 int op = PictOpSrc; 657 int op = PictOpSrc;
602 658
603 if (format->direct.alphaMask && !new_format->direct.alphaMask) 659 if (format->direct.alphaMask && !new_format->direct.alphaMask)
604 { 660 {
605 // does it have to be that complicated 661 // does it have to be that complicated
606 rgba c;
607 bg.get (c);
608
609 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 662 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
610 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 663 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
611 664
612 op = PictOpOver; 665 op = PictOpOver;
613 } 666 }
614 667
625{ 678{
626 rxvt_img *img2 = clone (); 679 rxvt_img *img2 = clone ();
627 Display *dpy = s->display->dpy; 680 Display *dpy = s->display->dpy;
628 Picture src = img->src_picture (); 681 Picture src = img->src_picture ();
629 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 682 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
630 683 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
631 Pixmap pixmap = XCreatePixmap (dpy, img->pm, 1, 1, 8);
632 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, PictStandardA8);
633 XRenderPictureAttributes pa;
634 pa.repeat = True;
635 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
636 XFreePixmap (dpy, pixmap);
637 684
638 XRenderColor mask_c; 685 XRenderColor mask_c;
639 686
640 mask_c.alpha = float_to_component (factor); 687 mask_c.alpha = float_to_component (factor);
641 mask_c.red = 688 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines