… | |
… | |
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), |
|
|
9 | pm(0), refcnt(0) |
11 | { |
10 | { |
12 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
13 | } |
11 | } |
14 | |
12 | |
|
|
13 | rxvt_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), refcnt(img.refcnt) |
|
|
15 | { |
|
|
16 | if (refcnt) |
|
|
17 | ++*refcnt; |
|
|
18 | } |
|
|
19 | |
|
|
20 | #if 0 |
15 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
21 | 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) |
22 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) |
17 | { |
23 | { |
18 | } |
24 | } |
|
|
25 | #endif |
19 | |
26 | |
20 | rxvt_img * |
27 | rxvt_img * |
21 | rxvt_img::new_from_root (rxvt_screen *s) |
28 | rxvt_img::new_from_root (rxvt_screen *s) |
22 | { |
29 | { |
23 | Display *dpy = s->display->dpy; |
30 | Display *dpy = s->display->dpy; |
… | |
… | |
38 | |
45 | |
39 | rxvt_img *img = new rxvt_img ( |
46 | rxvt_img *img = new rxvt_img ( |
40 | s, |
47 | s, |
41 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
48 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
42 | root_pm_w, |
49 | root_pm_w, |
43 | root_pm_h, |
50 | root_pm_h |
44 | root_pixmap |
|
|
45 | ); |
51 | ); |
46 | |
52 | |
47 | img->shared = true; |
53 | img->pm = root_pixmap; |
48 | |
54 | |
49 | return img; |
55 | return img; |
50 | } |
56 | } |
51 | |
57 | |
52 | rxvt_img * |
58 | rxvt_img * |
… | |
… | |
62 | s, |
68 | s, |
63 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
69 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
64 | gdk_pixbuf_get_width (pb), |
70 | gdk_pixbuf_get_width (pb), |
65 | gdk_pixbuf_get_height (pb) |
71 | gdk_pixbuf_get_height (pb) |
66 | ); |
72 | ); |
67 | |
73 | img->alloc (); |
68 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
74 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
69 | |
75 | |
|
|
76 | g_object_unref (pb); |
|
|
77 | |
70 | return img; |
78 | return img; |
71 | } |
79 | } |
72 | |
80 | |
|
|
81 | void |
|
|
82 | rxvt_img::destroy () |
|
|
83 | { |
|
|
84 | if (!refcnt || --*refcnt) |
|
|
85 | return; |
|
|
86 | |
|
|
87 | if (pm) |
|
|
88 | XFreePixmap (s->display->dpy, pm); |
|
|
89 | |
|
|
90 | delete refcnt; |
|
|
91 | } |
|
|
92 | |
73 | rxvt_img::~rxvt_img () |
93 | rxvt_img::~rxvt_img () |
74 | { |
94 | { |
75 | if (!shared) |
95 | destroy (); |
76 | XFreePixmap (s->display->dpy, pm); |
96 | } |
|
|
97 | |
|
|
98 | void |
|
|
99 | rxvt_img::alloc () |
|
|
100 | { |
|
|
101 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
102 | refcnt = new int (1); |
77 | } |
103 | } |
78 | |
104 | |
79 | void |
105 | void |
80 | rxvt_img::unshare () |
106 | rxvt_img::unshare () |
81 | { |
107 | { |
82 | if (!shared) |
108 | if (refcnt && *refcnt == 1) |
83 | return; |
109 | return; |
84 | |
110 | |
85 | rxvt_img *img = clone (); |
111 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
112 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
113 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
|
|
114 | XFreeGC (s->display->dpy, gc); |
86 | |
115 | |
87 | ::swap (pm , img->pm); |
116 | destroy (); |
88 | ::swap (shared, img->shared); |
|
|
89 | |
117 | |
90 | delete img; |
118 | pm = pm2; |
|
|
119 | refcnt = new int (1); |
91 | } |
120 | } |
92 | |
121 | |
93 | void |
122 | void |
94 | rxvt_img::fill (const rxvt_color &c) |
123 | rxvt_img::fill (const rxvt_color &c) |
95 | { |
124 | { |
… | |
… | |
130 | Display *dpy = s->display->dpy; |
159 | Display *dpy = s->display->dpy; |
131 | int size = max (rh, rv) * 2 + 1; |
160 | int size = max (rh, rv) * 2 + 1; |
132 | double *kernel = (double *)malloc (size * sizeof (double)); |
161 | double *kernel = (double *)malloc (size * sizeof (double)); |
133 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
162 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
134 | rxvt_img *img = new rxvt_img (s, format, w, h); |
163 | rxvt_img *img = new rxvt_img (s, format, w, h); |
|
|
164 | img->alloc (); |
135 | |
165 | |
136 | XRenderPictureAttributes pa; |
166 | XRenderPictureAttributes pa; |
137 | |
167 | |
138 | pa.repeat = RepeatPad; |
168 | pa.repeat = RepeatPad; |
139 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
169 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
… | |
… | |
198 | |
228 | |
199 | return mask; |
229 | return mask; |
200 | } |
230 | } |
201 | |
231 | |
202 | void |
232 | void |
203 | rxvt_img::brightness (double r, double g, double b, double a) |
233 | rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
204 | { |
234 | { |
205 | Display *dpy = s->display->dpy; |
235 | Display *dpy = s->display->dpy; |
206 | Picture src = create_xrender_mask (dpy, pm, True); |
236 | Picture src = create_xrender_mask (dpy, pm, True); |
207 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
237 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
208 | |
238 | |
209 | XRenderColor mask_c; |
239 | XRenderColor mask_c; |
210 | mask_c.red = float_to_component (r); |
240 | mask_c.red = r; |
211 | mask_c.green = float_to_component (g); |
241 | mask_c.green = g; |
212 | mask_c.blue = float_to_component (b); |
242 | mask_c.blue = b; |
213 | mask_c.alpha = float_to_component (a); |
243 | mask_c.alpha = a; |
214 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
244 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
215 | |
245 | |
216 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
246 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
217 | |
247 | |
218 | XRenderFreePicture (dpy, src); |
248 | XRenderFreePicture (dpy, src); |
219 | XRenderFreePicture (dpy, dst); |
249 | XRenderFreePicture (dpy, dst); |
220 | } |
250 | } |
221 | |
251 | |
222 | void |
252 | void |
223 | rxvt_img::contrast (double r, double g, double b, double a) |
253 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
224 | { |
254 | { |
225 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
255 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
226 | return; |
256 | return; |
227 | |
257 | |
228 | Display *dpy = s->display->dpy; |
258 | Display *dpy = s->display->dpy; |
229 | Picture src = create_xrender_mask (dpy, pm, True); |
259 | Picture src = create_xrender_mask (dpy, pm, True); |
230 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
260 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
231 | |
261 | |
232 | XRenderColor mask_c; |
262 | XRenderColor mask_c; |
233 | mask_c.red = float_to_component (r); |
263 | mask_c.red = r; |
234 | mask_c.green = float_to_component (g); |
264 | mask_c.green = g; |
235 | mask_c.blue = float_to_component (b); |
265 | mask_c.blue = b; |
236 | mask_c.alpha = float_to_component (a); |
266 | mask_c.alpha = a; |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
267 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
238 | |
268 | |
239 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
269 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
240 | |
270 | |
241 | XRenderFreePicture (dpy, src); |
271 | XRenderFreePicture (dpy, src); |
… | |
… | |
291 | |
321 | |
292 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
322 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
293 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
323 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
294 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
324 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
295 | char *line = ximage->data; |
325 | char *line = ximage->data; |
296 | |
|
|
297 | rgba c (0, 0, 0); |
|
|
298 | |
|
|
299 | if (channels == 4 && alpha_mask == 0) |
|
|
300 | { |
|
|
301 | //pix_colors[Color_bg].get (c); |
|
|
302 | //TODO |
|
|
303 | c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D |
|
|
304 | c.r >>= 8; |
|
|
305 | c.g >>= 8; |
|
|
306 | c.b >>= 8; |
|
|
307 | } |
|
|
308 | |
326 | |
309 | for (int y = 0; y < height; y++) |
327 | for (int y = 0; y < height; y++) |
310 | { |
328 | { |
311 | for (int x = 0; x < width; x++) |
329 | for (int x = 0; x < width; x++) |
312 | { |
330 | { |
… | |
… | |
315 | unsigned char r, g, b, a; |
333 | unsigned char r, g, b, a; |
316 | |
334 | |
317 | if (channels == 4) |
335 | if (channels == 4) |
318 | { |
336 | { |
319 | a = pixel[3]; |
337 | a = pixel[3]; |
320 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
338 | r = pixel[0] * a / 0xff; |
321 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
339 | g = pixel[1] * a / 0xff; |
322 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
340 | b = pixel[2] * a / 0xff; |
323 | } |
341 | } |
324 | else |
342 | else |
325 | { |
343 | { |
326 | a = 0xff; |
344 | a = 0xff; |
327 | r = pixel[0]; |
345 | r = pixel[0]; |
… | |
… | |
352 | } |
370 | } |
353 | |
371 | |
354 | rxvt_img * |
372 | rxvt_img * |
355 | rxvt_img::clone () |
373 | rxvt_img::clone () |
356 | { |
374 | { |
357 | rxvt_img *img = new rxvt_img (s, format, w, h); |
375 | return new rxvt_img (*this); |
358 | |
|
|
359 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
360 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
|
|
361 | XFreeGC (s->display->dpy, gc); |
|
|
362 | |
|
|
363 | return img; |
|
|
364 | } |
376 | } |
365 | |
377 | |
366 | rxvt_img * |
378 | rxvt_img * |
367 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
379 | rxvt_img::sub_rect (int x, int y, int width, int height) |
368 | { |
380 | { |
369 | rxvt_img *img = new rxvt_img (s, format, width, height); |
381 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
382 | img->alloc (); |
370 | |
383 | |
371 | Display *dpy = s->display->dpy; |
384 | Display *dpy = s->display->dpy; |
372 | XRenderPictureAttributes pa; |
385 | XRenderPictureAttributes pa; |
373 | pa.repeat = repeat; |
386 | pa.repeat = repeat; |
374 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
387 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
381 | |
394 | |
382 | return img; |
395 | return img; |
383 | } |
396 | } |
384 | |
397 | |
385 | rxvt_img * |
398 | rxvt_img * |
386 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
399 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
387 | { |
400 | { |
388 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
401 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
|
|
402 | img->alloc (); |
389 | |
403 | |
390 | Display *dpy = s->display->dpy; |
404 | Display *dpy = s->display->dpy; |
391 | XRenderPictureAttributes pa; |
405 | XRenderPictureAttributes pa; |
392 | pa.repeat = repeat; |
406 | pa.repeat = repeat; |
393 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
407 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
… | |
… | |
420 | |
434 | |
421 | return transform (new_width, new_height, matrix); |
435 | return transform (new_width, new_height, matrix); |
422 | } |
436 | } |
423 | |
437 | |
424 | rxvt_img * |
438 | rxvt_img * |
425 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
439 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
426 | { |
440 | { |
427 | double s = sin (phi); |
441 | double s = sin (phi); |
428 | double c = cos (phi); |
442 | double c = cos (phi); |
429 | |
443 | |
430 | double matrix[9] = { |
444 | double matrix[9] = { |
431 | c, -s, -c * x + s * y + x, |
445 | c, -s, -c * x + s * y + x, |
432 | s, c, -s * x - c * y + y, |
446 | s, c, -s * x - c * y + y, |
433 | 0, 0, 1 |
447 | 0, 0, 1 |
434 | }; |
448 | }; |
435 | |
449 | |
436 | return transform (new_width, new_height, matrix, repeat); |
450 | return transform (new_width, new_height, matrix); |
437 | } |
451 | } |
438 | |
452 | |
439 | rxvt_img * |
453 | rxvt_img * |
440 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
454 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
441 | { |
455 | { |
|
|
456 | if (new_format == format) |
|
|
457 | return clone (); |
|
|
458 | |
442 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
459 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
|
|
460 | img->alloc (); |
443 | |
461 | |
444 | Display *dpy = s->display->dpy; |
462 | Display *dpy = s->display->dpy; |
445 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
463 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
446 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
464 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
465 | int op = PictOpSrc; |
447 | |
466 | |
|
|
467 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
|
|
468 | { |
|
|
469 | // does it have to be that complicated |
|
|
470 | rgba c; |
|
|
471 | bg.get (c); |
|
|
472 | |
|
|
473 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
|
|
474 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
|
|
475 | |
|
|
476 | op = PictOpOver; |
|
|
477 | } |
|
|
478 | |
448 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
479 | XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
449 | |
480 | |
450 | XRenderFreePicture (dpy, src); |
481 | XRenderFreePicture (dpy, src); |
451 | XRenderFreePicture (dpy, dst); |
482 | XRenderFreePicture (dpy, dst); |
452 | |
483 | |
453 | return img; |
484 | return img; |