… | |
… | |
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 | |
9 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
9 | rxvt_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 | |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
15 | rxvt_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 | |
20 | rxvt_img * |
20 | rxvt_img * |
21 | rxvt_img::new_from_root (rxvt_screen *s) |
21 | rxvt_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 | |
73 | rxvt_img::~rxvt_img () |
75 | rxvt_img::~rxvt_img () |
74 | { |
76 | { |
… | |
… | |
212 | mask_c.blue = float_to_component (b); |
214 | mask_c.blue = float_to_component (b); |
213 | mask_c.alpha = float_to_component (a); |
215 | mask_c.alpha = float_to_component (a); |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
216 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
215 | |
217 | |
216 | 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); |
217 | } |
222 | } |
218 | |
223 | |
219 | void |
224 | void |
220 | rxvt_img::contrast (double r, double g, double b, double a) |
225 | rxvt_img::contrast (double r, double g, double b, double a) |
221 | { |
226 | { |
… | |
… | |
232 | mask_c.blue = float_to_component (b); |
237 | mask_c.blue = float_to_component (b); |
233 | mask_c.alpha = float_to_component (a); |
238 | mask_c.alpha = float_to_component (a); |
234 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
239 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
235 | |
240 | |
236 | 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); |
237 | } |
245 | } |
238 | |
246 | |
239 | bool |
247 | bool |
240 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
248 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
241 | { |
249 | { |
… | |
… | |
285 | |
293 | |
286 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
294 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
287 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
295 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
288 | 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; |
289 | char *line = ximage->data; |
297 | char *line = ximage->data; |
290 | |
|
|
291 | rgba c (0, 0, 0); |
|
|
292 | |
|
|
293 | if (channels == 4 && alpha_mask == 0) |
|
|
294 | { |
|
|
295 | //pix_colors[Color_bg].get (c); |
|
|
296 | //TODO |
|
|
297 | c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D |
|
|
298 | c.r >>= 8; |
|
|
299 | c.g >>= 8; |
|
|
300 | c.b >>= 8; |
|
|
301 | } |
|
|
302 | |
298 | |
303 | for (int y = 0; y < height; y++) |
299 | for (int y = 0; y < height; y++) |
304 | { |
300 | { |
305 | for (int x = 0; x < width; x++) |
301 | for (int x = 0; x < width; x++) |
306 | { |
302 | { |
… | |
… | |
309 | unsigned char r, g, b, a; |
305 | unsigned char r, g, b, a; |
310 | |
306 | |
311 | if (channels == 4) |
307 | if (channels == 4) |
312 | { |
308 | { |
313 | a = pixel[3]; |
309 | a = pixel[3]; |
314 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
310 | r = pixel[0] * a / 0xff; |
315 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
311 | g = pixel[1] * a / 0xff; |
316 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
312 | b = pixel[2] * a / 0xff; |
317 | } |
313 | } |
318 | else |
314 | else |
319 | { |
315 | { |
320 | a = 0xff; |
316 | a = 0xff; |
321 | r = pixel[0]; |
317 | r = pixel[0]; |
… | |
… | |
356 | |
352 | |
357 | return img; |
353 | return img; |
358 | } |
354 | } |
359 | |
355 | |
360 | rxvt_img * |
356 | rxvt_img * |
361 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
357 | rxvt_img::sub_rect (int x, int y, int width, int height) |
362 | { |
358 | { |
363 | rxvt_img *img = new rxvt_img (s, format, width, height); |
359 | rxvt_img *img = new rxvt_img (s, format, width, height); |
364 | |
360 | |
365 | Display *dpy = s->display->dpy; |
361 | Display *dpy = s->display->dpy; |
366 | XRenderPictureAttributes pa; |
362 | XRenderPictureAttributes pa; |
… | |
… | |
375 | |
371 | |
376 | return img; |
372 | return img; |
377 | } |
373 | } |
378 | |
374 | |
379 | rxvt_img * |
375 | rxvt_img * |
380 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
376 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
381 | { |
377 | { |
382 | 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); |
383 | |
379 | |
384 | Display *dpy = s->display->dpy; |
380 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
381 | XRenderPictureAttributes pa; |
… | |
… | |
414 | |
410 | |
415 | return transform (new_width, new_height, matrix); |
411 | return transform (new_width, new_height, matrix); |
416 | } |
412 | } |
417 | |
413 | |
418 | rxvt_img * |
414 | rxvt_img * |
419 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
415 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
420 | { |
416 | { |
421 | double s = sin (phi); |
417 | double s = sin (phi); |
422 | double c = cos (phi); |
418 | double c = cos (phi); |
423 | |
419 | |
424 | double matrix[9] = { |
420 | double matrix[9] = { |
425 | c, -s, -c * x + s * y + x, |
421 | c, -s, -c * x + s * y + x, |
426 | s, c, -s * x - c * y + y, |
422 | s, c, -s * x - c * y + y, |
427 | 0, 0, 1 |
423 | 0, 0, 1 |
428 | }; |
424 | }; |
429 | |
425 | |
430 | return transform (new_width, new_height, matrix, repeat); |
426 | return transform (new_width, new_height, matrix); |
431 | } |
427 | } |
432 | |
428 | |
433 | rxvt_img * |
429 | rxvt_img * |
434 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
430 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
435 | { |
431 | { |
436 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
432 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
437 | |
433 | |
438 | Display *dpy = s->display->dpy; |
434 | Display *dpy = s->display->dpy; |
439 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
435 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
440 | 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; |
441 | |
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 | |
442 | 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); |
443 | |
452 | |
444 | XRenderFreePicture (dpy, src); |
|
|
445 | XRenderFreePicture (dpy, dst); |
453 | XRenderFreePicture (dpy, src); |
|
|
454 | XRenderFreePicture (dpy, dst); |
446 | |
455 | |
|
|
456 | return img; |
|
|
457 | } |
|
|
458 | |
|
|
459 | rxvt_img * |
|
|
460 | rxvt_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 | |
447 | return img; |
482 | return img2; |
448 | } |
483 | } |
449 | |
484 | |
450 | #endif |
485 | #endif |
451 | |
486 | |