|
|
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 |
5 | |
6 | |
|
|
7 | #define float_to_component(d) ((d) * 65535.99) |
|
|
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); |
10 | } |
13 | } |
… | |
… | |
15 | } |
18 | } |
16 | |
19 | |
17 | rxvt_img * |
20 | rxvt_img * |
18 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
21 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
19 | { |
22 | { |
20 | GError *err; |
23 | GError *err = 0; |
21 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
24 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
22 | |
25 | |
23 | if (!pb) |
26 | if (!pb) |
24 | return 0; |
27 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
25 | |
|
|
26 | int w = gdk_pixbuf_get_width (pb); |
|
|
27 | int h = gdk_pixbuf_get_height (pb); |
|
|
28 | |
28 | |
29 | rxvt_img *img = new rxvt_img ( |
29 | rxvt_img *img = new rxvt_img ( |
30 | s, |
30 | s, |
31 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
31 | XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), |
32 | gdk_pixbuf_get_width (pb), |
32 | gdk_pixbuf_get_width (pb), |
33 | gdk_pixbuf_get_height (pb) |
33 | gdk_pixbuf_get_height (pb) |
34 | ); |
34 | ); |
35 | |
35 | |
36 | img->render (pb, 0, 0, w, h, 0, 0); |
36 | img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); |
37 | |
37 | |
38 | return img; |
38 | return img; |
39 | } |
39 | } |
40 | |
40 | |
41 | rxvt_img::~rxvt_img () |
41 | rxvt_img::~rxvt_img () |
42 | { |
42 | { |
43 | if (!shared) |
43 | if (!shared) |
44 | XFreePixmap (s->display->dpy, pm); |
44 | XFreePixmap (s->display->dpy, pm); |
|
|
45 | } |
|
|
46 | |
|
|
47 | void |
|
|
48 | rxvt_img::unshare () |
|
|
49 | { |
|
|
50 | if (!shared) |
|
|
51 | return; |
|
|
52 | |
|
|
53 | rxvt_img *img = clone (); |
|
|
54 | |
|
|
55 | ::swap (pm , img->pm); |
|
|
56 | ::swap (shared, img->shared); |
|
|
57 | |
|
|
58 | delete img; |
45 | } |
59 | } |
46 | |
60 | |
47 | void |
61 | void |
48 | rxvt_img::fill (const rxvt_color &c) |
62 | rxvt_img::fill (const rxvt_color &c) |
49 | { |
63 | { |
… | |
… | |
52 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
66 | GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); |
53 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
67 | XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); |
54 | XFreeGC (s->display->dpy, gc); |
68 | XFreeGC (s->display->dpy, gc); |
55 | } |
69 | } |
56 | |
70 | |
|
|
71 | static void |
|
|
72 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
|
|
73 | { |
|
|
74 | double sigma = radius / 2.0; |
|
|
75 | double scale = sqrt (2.0 * M_PI) * sigma; |
|
|
76 | double sum = 0.0; |
|
|
77 | |
|
|
78 | for (int i = 0; i < width; i++) |
|
|
79 | { |
|
|
80 | double x = i - width / 2; |
|
|
81 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
|
|
82 | sum += kernel[i]; |
|
|
83 | } |
|
|
84 | |
|
|
85 | params[0] = XDoubleToFixed (width); |
|
|
86 | params[1] = XDoubleToFixed (1); |
|
|
87 | |
|
|
88 | for (int i = 0; i < width; i++) |
|
|
89 | params[i+2] = XDoubleToFixed (kernel[i] / sum); |
|
|
90 | } |
|
|
91 | |
57 | void |
92 | void |
58 | rxvt_img::blur (int rh, int rv) |
93 | rxvt_img::blur (int rh, int rv) |
59 | { |
94 | { |
60 | //TODO |
95 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
|
|
96 | return; |
|
|
97 | |
|
|
98 | Display *dpy = s->display->dpy; |
|
|
99 | int size = max (rh, rv) * 2 + 1; |
|
|
100 | double *kernel = (double *)malloc (size * sizeof (double)); |
|
|
101 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
|
|
102 | |
|
|
103 | XRenderPictureAttributes pa; |
|
|
104 | |
|
|
105 | pa.repeat = RepeatPad; |
|
|
106 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
|
|
107 | Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); |
|
|
108 | Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); |
|
|
109 | XFreePixmap (dpy, tmp); |
|
|
110 | |
|
|
111 | if (kernel && params) |
|
|
112 | { |
|
|
113 | size = rh * 2 + 1; |
|
|
114 | get_gaussian_kernel (rh, size, kernel, params); |
|
|
115 | |
|
|
116 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
|
|
117 | XRenderComposite (dpy, |
|
|
118 | PictOpSrc, |
|
|
119 | src, |
|
|
120 | None, |
|
|
121 | dst, |
|
|
122 | 0, 0, |
|
|
123 | 0, 0, |
|
|
124 | 0, 0, |
|
|
125 | w, h); |
|
|
126 | |
|
|
127 | ::swap (src, dst); |
|
|
128 | |
|
|
129 | size = rv * 2 + 1; |
|
|
130 | get_gaussian_kernel (rv, size, kernel, params); |
|
|
131 | ::swap (params[0], params[1]); |
|
|
132 | |
|
|
133 | XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); |
|
|
134 | XRenderComposite (dpy, |
|
|
135 | PictOpSrc, |
|
|
136 | src, |
|
|
137 | None, |
|
|
138 | dst, |
|
|
139 | 0, 0, |
|
|
140 | 0, 0, |
|
|
141 | 0, 0, |
|
|
142 | w, h); |
|
|
143 | } |
|
|
144 | |
|
|
145 | free (kernel); |
|
|
146 | free (params); |
|
|
147 | XRenderFreePicture (dpy, src); |
|
|
148 | XRenderFreePicture (dpy, dst); |
|
|
149 | } |
|
|
150 | |
|
|
151 | static Picture |
|
|
152 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
|
|
153 | { |
|
|
154 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
|
|
155 | |
|
|
156 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
|
|
157 | XRenderPictureAttributes pa; |
|
|
158 | pa.repeat = True; |
|
|
159 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
|
|
160 | |
|
|
161 | XFreePixmap (dpy, pixmap); |
|
|
162 | |
|
|
163 | return mask; |
61 | } |
164 | } |
62 | |
165 | |
63 | void |
166 | void |
64 | rxvt_img::brightness (double r, double g, double b, double a) |
167 | rxvt_img::brightness (double r, double g, double b, double a) |
65 | { |
168 | { |
66 | //TODO |
169 | Display *dpy = s->display->dpy; |
|
|
170 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
171 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
172 | |
|
|
173 | XRenderColor mask_c; |
|
|
174 | mask_c.red = float_to_component (r); |
|
|
175 | mask_c.green = float_to_component (g); |
|
|
176 | mask_c.blue = float_to_component (b); |
|
|
177 | mask_c.alpha = float_to_component (a); |
|
|
178 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
179 | |
|
|
180 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
67 | } |
181 | } |
68 | |
182 | |
69 | void |
183 | void |
70 | rxvt_img::contrast (double r, double g, double b, double a) |
184 | rxvt_img::contrast (double r, double g, double b, double a) |
71 | { |
185 | { |
72 | //TODO |
186 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
73 | } |
187 | return; |
74 | |
188 | |
75 | void |
189 | Display *dpy = s->display->dpy; |
|
|
190 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
191 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
192 | |
|
|
193 | XRenderColor mask_c; |
|
|
194 | mask_c.red = float_to_component (r); |
|
|
195 | mask_c.green = float_to_component (g); |
|
|
196 | mask_c.blue = float_to_component (b); |
|
|
197 | mask_c.alpha = float_to_component (a); |
|
|
198 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
199 | |
|
|
200 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
201 | } |
|
|
202 | |
|
|
203 | bool |
76 | rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
204 | rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) |
77 | { |
205 | { |
|
|
206 | Display *dpy = s->display->dpy; |
|
|
207 | |
|
|
208 | if (s->visual->c_class != TrueColor) |
|
|
209 | return false; |
|
|
210 | |
|
|
211 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
|
|
212 | |
|
|
213 | red_mask = (uint32_t)format->direct.redMask << format->direct.red; |
|
|
214 | green_mask = (uint32_t)format->direct.greenMask << format->direct.green; |
|
|
215 | blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; |
|
|
216 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
|
|
217 | |
|
|
218 | int width_r = ecb_popcount32 (red_mask); |
|
|
219 | int width_g = ecb_popcount32 (green_mask); |
|
|
220 | int width_b = ecb_popcount32 (blue_mask); |
|
|
221 | int width_a = ecb_popcount32 (alpha_mask); |
|
|
222 | |
|
|
223 | if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) |
|
|
224 | return false; |
|
|
225 | |
|
|
226 | int sh_r = ecb_ctz32 (red_mask); |
|
|
227 | int sh_g = ecb_ctz32 (green_mask); |
|
|
228 | int sh_b = ecb_ctz32 (blue_mask); |
|
|
229 | int sh_a = ecb_ctz32 (alpha_mask); |
|
|
230 | |
|
|
231 | if (width > 32767 || height > 32767) |
|
|
232 | return false; |
|
|
233 | |
|
|
234 | XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, |
|
|
235 | width, height, 32, 0); |
|
|
236 | if (!ximage) |
|
|
237 | return false; |
|
|
238 | |
|
|
239 | if (height > INT_MAX / ximage->bytes_per_line |
|
|
240 | || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) |
|
|
241 | { |
|
|
242 | XDestroyImage (ximage); |
|
|
243 | return false; |
|
|
244 | } |
|
|
245 | |
|
|
246 | GC gc = XCreateGC (dpy, pm, 0, 0); |
|
|
247 | |
|
|
248 | ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
249 | |
|
|
250 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
|
|
251 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
|
|
252 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
|
|
253 | char *line = ximage->data; |
|
|
254 | |
|
|
255 | rgba c (0, 0, 0); |
|
|
256 | |
|
|
257 | if (channels == 4 && alpha_mask == 0) |
|
|
258 | { |
|
|
259 | //pix_colors[Color_bg].get (c); |
78 | //TODO |
260 | //TODO |
79 | } |
261 | c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D |
|
|
262 | c.r >>= 8; |
|
|
263 | c.g >>= 8; |
|
|
264 | c.b >>= 8; |
|
|
265 | } |
80 | |
266 | |
|
|
267 | for (int y = 0; y < height; y++) |
|
|
268 | { |
|
|
269 | for (int x = 0; x < width; x++) |
|
|
270 | { |
|
|
271 | unsigned char *pixel = row + x * channels; |
|
|
272 | uint32_t value; |
|
|
273 | unsigned char r, g, b, a; |
|
|
274 | |
|
|
275 | if (channels == 4) |
|
|
276 | { |
|
|
277 | a = pixel[3]; |
|
|
278 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
|
|
279 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
|
|
280 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
|
|
281 | } |
|
|
282 | else |
|
|
283 | { |
|
|
284 | a = 0xff; |
|
|
285 | r = pixel[0]; |
|
|
286 | g = pixel[1]; |
|
|
287 | b = pixel[2]; |
|
|
288 | } |
|
|
289 | |
|
|
290 | value = ((r >> (8 - width_r)) << sh_r) |
|
|
291 | | ((g >> (8 - width_g)) << sh_g) |
|
|
292 | | ((b >> (8 - width_b)) << sh_b) |
|
|
293 | | ((a >> (8 - width_a)) << sh_a); |
|
|
294 | |
|
|
295 | if (ximage->bits_per_pixel == 32) |
|
|
296 | ((uint32_t *)line)[x] = value; |
|
|
297 | else |
|
|
298 | XPutPixel (ximage, x, y, value); |
|
|
299 | } |
|
|
300 | |
|
|
301 | row += rowstride; |
|
|
302 | line += ximage->bytes_per_line; |
|
|
303 | } |
|
|
304 | |
|
|
305 | XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); |
|
|
306 | XDestroyImage (ximage); |
|
|
307 | XFreeGC (dpy, gc); |
|
|
308 | |
|
|
309 | return true; |
|
|
310 | } |
|
|
311 | |
81 | rxvt_img * |
312 | rxvt_img * |
82 | rxvt_img::copy () |
313 | rxvt_img::clone () |
83 | { |
314 | { |
|
|
315 | rxvt_img *img = new rxvt_img (s, format, w, h); |
|
|
316 | |
84 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
317 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
85 | Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth); |
|
|
86 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
318 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
87 | XFreeGC (s->display->dpy, gc); |
319 | XFreeGC (s->display->dpy, gc); |
88 | return new rxvt_img (s, format, w, h, pm2); |
|
|
89 | } |
|
|
90 | |
320 | |
|
|
321 | return img; |
|
|
322 | } |
|
|
323 | |
91 | rxvt_img * |
324 | rxvt_img * |
|
|
325 | rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) |
|
|
326 | { |
|
|
327 | rxvt_img *img = new rxvt_img (s, format, width, height); |
|
|
328 | |
|
|
329 | Display *dpy = s->display->dpy; |
|
|
330 | XRenderPictureAttributes pa; |
|
|
331 | pa.repeat = repeat; |
|
|
332 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
333 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
334 | |
|
|
335 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
|
|
336 | |
|
|
337 | XRenderFreePicture (dpy, src); |
|
|
338 | XRenderFreePicture (dpy, dst); |
|
|
339 | |
|
|
340 | return img; |
|
|
341 | } |
|
|
342 | |
|
|
343 | rxvt_img * |
92 | rxvt_img::transform (int new_width, int new_height, double matrix[16]) |
344 | rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) |
93 | { |
345 | { |
94 | //TODO |
346 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
|
|
347 | |
|
|
348 | Display *dpy = s->display->dpy; |
|
|
349 | XRenderPictureAttributes pa; |
|
|
350 | pa.repeat = repeat; |
|
|
351 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
352 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
353 | |
|
|
354 | XTransform xfrm; |
|
|
355 | |
|
|
356 | for (int i = 0; i < 3; ++i) |
|
|
357 | for (int j = 0; j < 3; ++j) |
|
|
358 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
|
|
359 | |
|
|
360 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
|
|
361 | XRenderSetPictureTransform (dpy, src, &xfrm); |
|
|
362 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
|
|
363 | |
|
|
364 | XRenderFreePicture (dpy, src); |
|
|
365 | XRenderFreePicture (dpy, dst); |
|
|
366 | |
|
|
367 | return img; |
95 | } |
368 | } |
96 | |
369 | |
97 | rxvt_img * |
370 | rxvt_img * |
98 | rxvt_img::scale (int new_width, int new_height) |
371 | rxvt_img::scale (int new_width, int new_height) |
99 | { |
372 | { |
100 | // use transform |
373 | double matrix[9] = { |
101 | //TODO |
374 | w / (double)new_width, 0, 0, |
102 | } |
375 | 0, h / (double)new_height, 0, |
|
|
376 | 0, 0, 1 |
|
|
377 | }; |
103 | |
378 | |
|
|
379 | return transform (new_width, new_height, matrix); |
|
|
380 | } |
|
|
381 | |
|
|
382 | rxvt_img * |
|
|
383 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) |
|
|
384 | { |
|
|
385 | double s = sin (phi); |
|
|
386 | double c = cos (phi); |
|
|
387 | |
|
|
388 | double matrix[9] = { |
|
|
389 | c, -s, -c * x + s * y + x, |
|
|
390 | s, c, -s * x - c * y + y, |
|
|
391 | 0, 0, 1 |
|
|
392 | }; |
|
|
393 | |
|
|
394 | return transform (new_width, new_height, matrix, repeat); |
|
|
395 | } |
|
|
396 | |
|
|
397 | rxvt_img * |
|
|
398 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
|
|
399 | { |
|
|
400 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
|
|
401 | |
|
|
402 | Display *dpy = s->display->dpy; |
|
|
403 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
404 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
405 | |
|
|
406 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
407 | |
|
|
408 | XRenderFreePicture (dpy, src); |
|
|
409 | XRenderFreePicture (dpy, dst); |
|
|
410 | |
|
|
411 | return img; |
|
|
412 | } |
104 | |
413 | |
105 | #endif |
414 | #endif |
106 | |
415 | |