… | |
… | |
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 | |
7 | #define float_to_component(d) ((d) * 65535.99) |
|
|
8 | |
|
|
9 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) |
10 | : s(screen), w(width), h(height), format(format), shared(false) |
8 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false) |
11 | { |
9 | { |
12 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
10 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
13 | } |
11 | } |
14 | |
12 | |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
13 | 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) |
14 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
17 | { |
15 | { |
18 | } |
16 | } |
19 | |
17 | |
20 | rxvt_img * |
18 | rxvt_img * |
21 | rxvt_img::new_from_root (rxvt_screen *s) |
19 | rxvt_img::new_from_root (rxvt_screen *s) |
… | |
… | |
65 | gdk_pixbuf_get_height (pb) |
63 | gdk_pixbuf_get_height (pb) |
66 | ); |
64 | ); |
67 | |
65 | |
68 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
66 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
69 | |
67 | |
|
|
68 | g_object_unref (pb); |
|
|
69 | |
70 | return img; |
70 | return img; |
71 | } |
71 | } |
72 | |
72 | |
73 | rxvt_img::~rxvt_img () |
73 | rxvt_img::~rxvt_img () |
74 | { |
74 | { |
… | |
… | |
198 | |
198 | |
199 | return mask; |
199 | return mask; |
200 | } |
200 | } |
201 | |
201 | |
202 | void |
202 | void |
203 | rxvt_img::brightness (double r, double g, double b, double a) |
203 | rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
204 | { |
204 | { |
205 | Display *dpy = s->display->dpy; |
205 | Display *dpy = s->display->dpy; |
206 | Picture src = create_xrender_mask (dpy, pm, True); |
206 | Picture src = create_xrender_mask (dpy, pm, True); |
207 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
207 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
208 | |
208 | |
209 | XRenderColor mask_c; |
209 | XRenderColor mask_c; |
210 | mask_c.red = float_to_component (r); |
210 | mask_c.red = r; |
211 | mask_c.green = float_to_component (g); |
211 | mask_c.green = g; |
212 | mask_c.blue = float_to_component (b); |
212 | mask_c.blue = b; |
213 | mask_c.alpha = float_to_component (a); |
213 | mask_c.alpha = a; |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
215 | |
215 | |
216 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
216 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
217 | |
|
|
218 | XRenderFreePicture (dpy, src); |
|
|
219 | XRenderFreePicture (dpy, dst); |
217 | } |
220 | } |
218 | |
221 | |
219 | void |
222 | void |
220 | rxvt_img::contrast (double r, double g, double b, double a) |
223 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
221 | { |
224 | { |
222 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
225 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
223 | return; |
226 | return; |
224 | |
227 | |
225 | Display *dpy = s->display->dpy; |
228 | Display *dpy = s->display->dpy; |
226 | Picture src = create_xrender_mask (dpy, pm, True); |
229 | Picture src = create_xrender_mask (dpy, pm, True); |
227 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
230 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
228 | |
231 | |
229 | XRenderColor mask_c; |
232 | XRenderColor mask_c; |
230 | mask_c.red = float_to_component (r); |
233 | mask_c.red = r; |
231 | mask_c.green = float_to_component (g); |
234 | mask_c.green = g; |
232 | mask_c.blue = float_to_component (b); |
235 | mask_c.blue = b; |
233 | mask_c.alpha = float_to_component (a); |
236 | mask_c.alpha = a; |
234 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
235 | |
238 | |
236 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
239 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
240 | |
|
|
241 | XRenderFreePicture (dpy, src); |
|
|
242 | XRenderFreePicture (dpy, dst); |
237 | } |
243 | } |
238 | |
244 | |
239 | bool |
245 | bool |
240 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
246 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
241 | { |
247 | { |
… | |
… | |
285 | |
291 | |
286 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
292 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
287 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
293 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
288 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
294 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
289 | char *line = ximage->data; |
295 | 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 | |
296 | |
303 | for (int y = 0; y < height; y++) |
297 | for (int y = 0; y < height; y++) |
304 | { |
298 | { |
305 | for (int x = 0; x < width; x++) |
299 | for (int x = 0; x < width; x++) |
306 | { |
300 | { |
… | |
… | |
309 | unsigned char r, g, b, a; |
303 | unsigned char r, g, b, a; |
310 | |
304 | |
311 | if (channels == 4) |
305 | if (channels == 4) |
312 | { |
306 | { |
313 | a = pixel[3]; |
307 | a = pixel[3]; |
314 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
308 | r = pixel[0] * a / 0xff; |
315 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
309 | g = pixel[1] * a / 0xff; |
316 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
310 | b = pixel[2] * a / 0xff; |
317 | } |
311 | } |
318 | else |
312 | else |
319 | { |
313 | { |
320 | a = 0xff; |
314 | a = 0xff; |
321 | r = pixel[0]; |
315 | r = pixel[0]; |
… | |
… | |
356 | |
350 | |
357 | return img; |
351 | return img; |
358 | } |
352 | } |
359 | |
353 | |
360 | rxvt_img * |
354 | rxvt_img * |
361 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
355 | rxvt_img::sub_rect (int x, int y, int width, int height) |
362 | { |
356 | { |
363 | rxvt_img *img = new rxvt_img (s, format, width, height); |
357 | rxvt_img *img = new rxvt_img (s, format, width, height); |
364 | |
358 | |
365 | Display *dpy = s->display->dpy; |
359 | Display *dpy = s->display->dpy; |
366 | XRenderPictureAttributes pa; |
360 | XRenderPictureAttributes pa; |
… | |
… | |
375 | |
369 | |
376 | return img; |
370 | return img; |
377 | } |
371 | } |
378 | |
372 | |
379 | rxvt_img * |
373 | rxvt_img * |
380 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
374 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
381 | { |
375 | { |
382 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
376 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
383 | |
377 | |
384 | Display *dpy = s->display->dpy; |
378 | Display *dpy = s->display->dpy; |
385 | XRenderPictureAttributes pa; |
379 | XRenderPictureAttributes pa; |
… | |
… | |
414 | |
408 | |
415 | return transform (new_width, new_height, matrix); |
409 | return transform (new_width, new_height, matrix); |
416 | } |
410 | } |
417 | |
411 | |
418 | rxvt_img * |
412 | rxvt_img * |
419 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
413 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
420 | { |
414 | { |
421 | double s = sin (phi); |
415 | double s = sin (phi); |
422 | double c = cos (phi); |
416 | double c = cos (phi); |
423 | |
417 | |
424 | double matrix[9] = { |
418 | double matrix[9] = { |
425 | c, -s, -c * x + s * y + x, |
419 | c, -s, -c * x + s * y + x, |
426 | s, c, -s * x - c * y + y, |
420 | s, c, -s * x - c * y + y, |
427 | 0, 0, 1 |
421 | 0, 0, 1 |
428 | }; |
422 | }; |
429 | |
423 | |
430 | return transform (new_width, new_height, matrix, repeat); |
424 | return transform (new_width, new_height, matrix); |
431 | } |
425 | } |
432 | |
426 | |
433 | rxvt_img * |
427 | rxvt_img * |
434 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
428 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
435 | { |
429 | { |
436 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
430 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
437 | |
431 | |
438 | Display *dpy = s->display->dpy; |
432 | Display *dpy = s->display->dpy; |
439 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
433 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
440 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
434 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
435 | int op = PictOpSrc; |
441 | |
436 | |
|
|
437 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
|
|
438 | { |
|
|
439 | // does it have to be that complicated |
|
|
440 | rgba c; |
|
|
441 | bg.get (c); |
|
|
442 | |
|
|
443 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
|
|
444 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
|
|
445 | |
|
|
446 | op = PictOpOver; |
|
|
447 | } |
|
|
448 | |
442 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
449 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
443 | |
450 | |
444 | XRenderFreePicture (dpy, src); |
|
|
445 | XRenderFreePicture (dpy, dst); |
451 | XRenderFreePicture (dpy, src); |
|
|
452 | XRenderFreePicture (dpy, dst); |
446 | |
453 | |
|
|
454 | return img; |
|
|
455 | } |
|
|
456 | |
|
|
457 | rxvt_img * |
|
|
458 | rxvt_img::blend (rxvt_img *img, double factor) |
|
|
459 | { |
|
|
460 | rxvt_img *img2 = clone (); |
|
|
461 | Display *dpy = s->display->dpy; |
|
|
462 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
463 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
|
|
464 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
|
|
465 | |
|
|
466 | XRenderColor mask_c; |
|
|
467 | |
|
|
468 | mask_c.alpha = float_to_component (factor); |
|
|
469 | mask_c.red = |
|
|
470 | mask_c.green = |
|
|
471 | mask_c.blue = 0; |
|
|
472 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
473 | |
|
|
474 | XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
475 | |
|
|
476 | XRenderFreePicture (dpy, src); |
|
|
477 | XRenderFreePicture (dpy, dst); |
|
|
478 | XRenderFreePicture (dpy, mask); |
|
|
479 | |
447 | return img; |
480 | return img2; |
448 | } |
481 | } |
449 | |
482 | |
450 | #endif |
483 | #endif |
451 | |
484 | |