|
|
1 | #include <math.h> |
1 | #include "../config.h" |
2 | #include "../config.h" |
2 | #include "rxvt.h" |
3 | #include "rxvt.h" |
3 | |
4 | |
4 | #if HAVE_IMG |
5 | #if HAVE_IMG |
|
|
6 | |
|
|
7 | #define float_to_component(d) ((d) * 65535.99) |
5 | |
8 | |
6 | 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) |
7 | : s(screen), w(width), h(height), format(format), shared(false) |
10 | : s(screen), w(width), h(height), format(format), shared(false) |
8 | { |
11 | { |
9 | 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); |
… | |
… | |
49 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
52 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
50 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
53 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
51 | XFreeGC (s->display->dpy, gc); |
54 | XFreeGC (s->display->dpy, gc); |
52 | } |
55 | } |
53 | |
56 | |
|
|
57 | static void |
|
|
58 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
|
|
59 | { |
|
|
60 | double sigma = radius / 2.0; |
|
|
61 | double scale = sqrt (2.0 * M_PI) * sigma; |
|
|
62 | double sum = 0.0; |
|
|
63 | |
|
|
64 | for (int i = 0; i < width; i++) |
|
|
65 | { |
|
|
66 | double x = i - width / 2; |
|
|
67 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
|
|
68 | sum += kernel[i]; |
|
|
69 | } |
|
|
70 | |
|
|
71 | params[0] = XDoubleToFixed (width); |
|
|
72 | params[1] = XDoubleToFixed (1); |
|
|
73 | |
|
|
74 | for (int i = 0; i < width; i++) |
|
|
75 | params[i+2] = XDoubleToFixed (kernel[i] / sum); |
|
|
76 | } |
|
|
77 | |
54 | void |
78 | void |
55 | rxvt_img::blur (int rh, int rv) |
79 | rxvt_img::blur (int rh, int rv) |
56 | { |
80 | { |
57 | //TODO |
81 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
|
|
82 | return; |
|
|
83 | |
|
|
84 | Display *dpy = s->display->dpy; |
|
|
85 | int size = max (rh, rv) * 2 + 1; |
|
|
86 | double *kernel = (double *)malloc (size * sizeof (double)); |
|
|
87 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
|
|
88 | |
|
|
89 | XRenderPictureAttributes pa; |
|
|
90 | |
|
|
91 | pa.repeat = RepeatPad; |
|
|
92 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
93 | Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); |
|
|
94 | Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); |
|
|
95 | XFreePixmap (dpy, tmp); |
|
|
96 | |
|
|
97 | if (kernel && params) |
|
|
98 | { |
|
|
99 | size = rh * 2 + 1; |
|
|
100 | get_gaussian_kernel (rh, size, kernel, params); |
|
|
101 | |
|
|
102 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
|
|
103 | XRenderComposite (dpy, |
|
|
104 | PictOpSrc, |
|
|
105 | src, |
|
|
106 | None, |
|
|
107 | dst, |
|
|
108 | 0, 0, |
|
|
109 | 0, 0, |
|
|
110 | 0, 0, |
|
|
111 | w, h); |
|
|
112 | |
|
|
113 | ::swap (src, dst); |
|
|
114 | |
|
|
115 | size = rv * 2 + 1; |
|
|
116 | get_gaussian_kernel (rv, size, kernel, params); |
|
|
117 | ::swap (params[0], params[1]); |
|
|
118 | |
|
|
119 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
|
|
120 | XRenderComposite (dpy, |
|
|
121 | PictOpSrc, |
|
|
122 | src, |
|
|
123 | None, |
|
|
124 | dst, |
|
|
125 | 0, 0, |
|
|
126 | 0, 0, |
|
|
127 | 0, 0, |
|
|
128 | w, h); |
|
|
129 | } |
|
|
130 | |
|
|
131 | free (kernel); |
|
|
132 | free (params); |
|
|
133 | XRenderFreePicture (dpy, src); |
|
|
134 | XRenderFreePicture (dpy, dst); |
|
|
135 | } |
|
|
136 | |
|
|
137 | static Picture |
|
|
138 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
|
|
139 | { |
|
|
140 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
|
|
141 | |
|
|
142 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
|
|
143 | XRenderPictureAttributes pa; |
|
|
144 | pa.repeat = True; |
|
|
145 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
|
|
146 | |
|
|
147 | XFreePixmap (dpy, pixmap); |
|
|
148 | |
|
|
149 | return mask; |
58 | } |
150 | } |
59 | |
151 | |
60 | void |
152 | void |
61 | rxvt_img::brightness (double r, double g, double b, double a) |
153 | rxvt_img::brightness (double r, double g, double b, double a) |
62 | { |
154 | { |
63 | //TODO |
155 | Display *dpy = s->display->dpy; |
|
|
156 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
157 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
158 | |
|
|
159 | XRenderColor mask_c; |
|
|
160 | mask_c.red = float_to_component (r); |
|
|
161 | mask_c.green = float_to_component (g); |
|
|
162 | mask_c.blue = float_to_component (b); |
|
|
163 | mask_c.alpha = float_to_component (a); |
|
|
164 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
165 | |
|
|
166 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
64 | } |
167 | } |
65 | |
168 | |
66 | void |
169 | void |
67 | rxvt_img::contrast (double r, double g, double b, double a) |
170 | rxvt_img::contrast (double r, double g, double b, double a) |
68 | { |
171 | { |
69 | //TODO |
172 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
173 | return; |
|
|
174 | |
|
|
175 | Display *dpy = s->display->dpy; |
|
|
176 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
177 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
178 | |
|
|
179 | XRenderColor mask_c; |
|
|
180 | mask_c.red = float_to_component (r); |
|
|
181 | mask_c.green = float_to_component (g); |
|
|
182 | mask_c.blue = float_to_component (b); |
|
|
183 | mask_c.alpha = float_to_component (a); |
|
|
184 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
185 | |
|
|
186 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
70 | } |
187 | } |
71 | |
188 | |
72 | void |
189 | void |
73 | rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
190 | rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
74 | { |
191 | { |