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.21 by sf-exg, Tue Jun 5 14:59:44 2012 UTC vs.
Revision 1.30 by root, Wed Jun 6 22:01:44 2012 UTC

5#if HAVE_IMG 5#if HAVE_IMG
6 6
7#define float_to_component(d) ((d) * 65535.99) 7#define float_to_component(d) ((d) * 65535.99)
8 8
9rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) 9rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height)
10: s(screen), w(width), h(height), format(format), shared(false) 10: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false)
11{ 11{
12 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 12 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
13} 13}
14 14
15rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) 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) 16: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
17{ 17{
18} 18}
19 19
20rxvt_img * 20rxvt_img *
21rxvt_img::new_from_root (rxvt_screen *s) 21rxvt_img::new_from_root (rxvt_screen *s)
65 gdk_pixbuf_get_height (pb) 65 gdk_pixbuf_get_height (pb)
66 ); 66 );
67 67
68 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); 68 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
69 69
70 g_object_unref (pb);
71
70 return img; 72 return img;
71} 73}
72 74
73rxvt_img::~rxvt_img () 75rxvt_img::~rxvt_img ()
74{ 76{
119 121
120 for (int i = 0; i < width; i++) 122 for (int i = 0; i < width; i++)
121 params[i+2] = XDoubleToFixed (kernel[i] / sum); 123 params[i+2] = XDoubleToFixed (kernel[i] / sum);
122} 124}
123 125
124void 126rxvt_img *
125rxvt_img::blur (int rh, int rv) 127rxvt_img::blur (int rh, int rv)
126{ 128{
127 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 129 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
128 return; 130 return clone ();
129 131
130 Display *dpy = s->display->dpy; 132 Display *dpy = s->display->dpy;
131 int size = max (rh, rv) * 2 + 1; 133 int size = max (rh, rv) * 2 + 1;
132 double *kernel = (double *)malloc (size * sizeof (double)); 134 double *kernel = (double *)malloc (size * sizeof (double));
133 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 135 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
136 rxvt_img *img = new rxvt_img (s, format, w, h);
134 137
135 XRenderPictureAttributes pa; 138 XRenderPictureAttributes pa;
136 139
137 pa.repeat = RepeatPad; 140 pa.repeat = RepeatPad;
138 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); 141 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
142 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
143
139 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); 144 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
140 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 145 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
141 XFreePixmap (dpy, tmp); 146 XFreePixmap (dpy, tmp_pm);
142 147
143 if (kernel && params) 148 if (kernel && params)
144 { 149 {
145 size = rh * 2 + 1; 150 size = rh * 2 + 1;
146 get_gaussian_kernel (rh, size, kernel, params); 151 get_gaussian_kernel (rh, size, kernel, params);
148 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 153 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
149 XRenderComposite (dpy, 154 XRenderComposite (dpy,
150 PictOpSrc, 155 PictOpSrc,
151 src, 156 src,
152 None, 157 None,
153 dst, 158 tmp,
154 0, 0, 159 0, 0,
155 0, 0, 160 0, 0,
156 0, 0, 161 0, 0,
157 w, h); 162 w, h);
158
159 ::swap (src, dst);
160 163
161 size = rv * 2 + 1; 164 size = rv * 2 + 1;
162 get_gaussian_kernel (rv, size, kernel, params); 165 get_gaussian_kernel (rv, size, kernel, params);
163 ::swap (params[0], params[1]); 166 ::swap (params[0], params[1]);
164 167
165 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 168 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
166 XRenderComposite (dpy, 169 XRenderComposite (dpy,
167 PictOpSrc, 170 PictOpSrc,
168 src, 171 tmp,
169 None, 172 None,
170 dst, 173 dst,
171 0, 0, 174 0, 0,
172 0, 0, 175 0, 0,
173 0, 0, 176 0, 0,
176 179
177 free (kernel); 180 free (kernel);
178 free (params); 181 free (params);
179 XRenderFreePicture (dpy, src); 182 XRenderFreePicture (dpy, src);
180 XRenderFreePicture (dpy, dst); 183 XRenderFreePicture (dpy, dst);
184 XRenderFreePicture (dpy, tmp);
185
186 return img;
181} 187}
182 188
183static Picture 189static Picture
184create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 190create_xrender_mask (Display *dpy, Drawable drawable, Bool argb)
185{ 191{
208 mask_c.blue = float_to_component (b); 214 mask_c.blue = float_to_component (b);
209 mask_c.alpha = float_to_component (a); 215 mask_c.alpha = float_to_component (a);
210 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 216 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
211 217
212 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 218 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
219
220 XRenderFreePicture (dpy, src);
221 XRenderFreePicture (dpy, dst);
213} 222}
214 223
215void 224void
216rxvt_img::contrast (double r, double g, double b, double a) 225rxvt_img::contrast (double r, double g, double b, double a)
217{ 226{
228 mask_c.blue = float_to_component (b); 237 mask_c.blue = float_to_component (b);
229 mask_c.alpha = float_to_component (a); 238 mask_c.alpha = float_to_component (a);
230 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 239 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
231 240
232 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 241 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
242
243 XRenderFreePicture (dpy, src);
244 XRenderFreePicture (dpy, dst);
233} 245}
234 246
235bool 247bool
236rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 248rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
237{ 249{
281 293
282 int rowstride = gdk_pixbuf_get_rowstride (pixbuf); 294 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
283 int channels = gdk_pixbuf_get_n_channels (pixbuf); 295 int channels = gdk_pixbuf_get_n_channels (pixbuf);
284 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; 296 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
285 char *line = ximage->data; 297 char *line = ximage->data;
286
287 rgba c (0, 0, 0);
288
289 if (channels == 4 && alpha_mask == 0)
290 {
291 //pix_colors[Color_bg].get (c);
292 //TODO
293 c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D
294 c.r >>= 8;
295 c.g >>= 8;
296 c.b >>= 8;
297 }
298 298
299 for (int y = 0; y < height; y++) 299 for (int y = 0; y < height; y++)
300 { 300 {
301 for (int x = 0; x < width; x++) 301 for (int x = 0; x < width; x++)
302 { 302 {
305 unsigned char r, g, b, a; 305 unsigned char r, g, b, a;
306 306
307 if (channels == 4) 307 if (channels == 4)
308 { 308 {
309 a = pixel[3]; 309 a = pixel[3];
310 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; 310 r = pixel[0] * a / 0xff;
311 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; 311 g = pixel[1] * a / 0xff;
312 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; 312 b = pixel[2] * a / 0xff;
313 } 313 }
314 else 314 else
315 { 315 {
316 a = 0xff; 316 a = 0xff;
317 r = pixel[0]; 317 r = pixel[0];
352 352
353 return img; 353 return img;
354} 354}
355 355
356rxvt_img * 356rxvt_img *
357rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) 357rxvt_img::sub_rect (int x, int y, int width, int height)
358{ 358{
359 rxvt_img *img = new rxvt_img (s, format, width, height); 359 rxvt_img *img = new rxvt_img (s, format, width, height);
360 360
361 Display *dpy = s->display->dpy; 361 Display *dpy = s->display->dpy;
362 XRenderPictureAttributes pa; 362 XRenderPictureAttributes pa;
371 371
372 return img; 372 return img;
373} 373}
374 374
375rxvt_img * 375rxvt_img *
376rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) 376rxvt_img::transform (int new_width, int new_height, double matrix[9])
377{ 377{
378 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 378 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
379 379
380 Display *dpy = s->display->dpy; 380 Display *dpy = s->display->dpy;
381 XRenderPictureAttributes pa; 381 XRenderPictureAttributes pa;
410 410
411 return transform (new_width, new_height, matrix); 411 return transform (new_width, new_height, matrix);
412} 412}
413 413
414rxvt_img * 414rxvt_img *
415rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) 415rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
416{ 416{
417 double s = sin (phi); 417 double s = sin (phi);
418 double c = cos (phi); 418 double c = cos (phi);
419 419
420 double matrix[9] = { 420 double matrix[9] = {
421 c, -s, -c * x + s * y + x, 421 c, -s, -c * x + s * y + x,
422 s, c, -s * x - c * y + y, 422 s, c, -s * x - c * y + y,
423 0, 0, 1 423 0, 0, 1
424 }; 424 };
425 425
426 return transform (new_width, new_height, matrix, repeat); 426 return transform (new_width, new_height, matrix);
427} 427}
428 428
429rxvt_img * 429rxvt_img *
430rxvt_img::convert_to (XRenderPictFormat *new_format) 430rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
431{ 431{
432 rxvt_img *img = new rxvt_img (s, new_format, w, h); 432 rxvt_img *img = new rxvt_img (s, new_format, w, h);
433 433
434 Display *dpy = s->display->dpy; 434 Display *dpy = s->display->dpy;
435 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 435 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
436 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 436 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
437 int op = PictOpSrc;
437 438
439 if (format->direct.alphaMask && !new_format->direct.alphaMask)
440 {
441 // does it have to be that complicated
442 rgba c;
443 bg.get (c);
444
445 XRenderColor rc = { c.r, c.g, c.b, 0xffff };
446 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
447
448 op = PictOpOver;
449 }
450
438 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 451 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
439 452
440 XRenderFreePicture (dpy, src);
441 XRenderFreePicture (dpy, dst); 453 XRenderFreePicture (dpy, src);
454 XRenderFreePicture (dpy, dst);
442 455
456 return img;
457}
458
459rxvt_img *
460rxvt_img::blend (rxvt_img *img, double factor)
461{
462 rxvt_img *img2 = clone ();
463 Display *dpy = s->display->dpy;
464 Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
465 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
466 Picture mask = create_xrender_mask (dpy, img->pm, False);
467
468 XRenderColor mask_c;
469
470 mask_c.alpha = float_to_component (factor);
471 mask_c.red =
472 mask_c.green =
473 mask_c.blue = 0;
474 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
475
476 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h);
477
478 XRenderFreePicture (dpy, src);
479 XRenderFreePicture (dpy, dst);
480 XRenderFreePicture (dpy, mask);
481
443 return img; 482 return img2;
444} 483}
445 484
446#endif 485#endif
447 486

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines