… | |
… | |
18 | } |
18 | } |
19 | |
19 | |
20 | rxvt_img * |
20 | rxvt_img * |
21 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
21 | rxvt_img::new_from_file (rxvt_screen *s, const char *filename) |
22 | { |
22 | { |
23 | GError *err; |
23 | GError *err = 0; |
24 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
24 | GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); |
25 | |
25 | |
26 | if (!pb) |
26 | if (!pb) |
27 | return 0; |
27 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
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, img->w, img->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 | { |
… | |
… | |
87 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
101 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
88 | |
102 | |
89 | XRenderPictureAttributes pa; |
103 | XRenderPictureAttributes pa; |
90 | |
104 | |
91 | pa.repeat = RepeatPad; |
105 | pa.repeat = RepeatPad; |
92 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
106 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
93 | Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); |
107 | Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); |
94 | Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); |
108 | Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); |
95 | XFreePixmap (dpy, tmp); |
109 | XFreePixmap (dpy, tmp); |
96 | |
110 | |
97 | if (kernel && params) |
111 | if (kernel && params) |
… | |
… | |
184 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
198 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
185 | |
199 | |
186 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
200 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
187 | } |
201 | } |
188 | |
202 | |
189 | void |
203 | bool |
190 | 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) |
191 | { |
205 | { |
|
|
206 | bool argb = format->id == PictStandardARGB32; |
|
|
207 | |
|
|
208 | Display *dpy = s->display->dpy; |
|
|
209 | |
|
|
210 | if (s->visual->c_class != TrueColor) |
|
|
211 | return false; |
|
|
212 | |
|
|
213 | uint32_t red_mask, green_mask, blue_mask, alpha_mask; |
|
|
214 | |
|
|
215 | if (argb) |
|
|
216 | { |
|
|
217 | red_mask = 0xff << 16; |
|
|
218 | green_mask = 0xff << 8; |
|
|
219 | blue_mask = 0xff; |
|
|
220 | alpha_mask = 0xff << 24; |
|
|
221 | } |
|
|
222 | else |
|
|
223 | { |
|
|
224 | red_mask = s->visual->red_mask; |
|
|
225 | green_mask = s->visual->green_mask; |
|
|
226 | blue_mask = s->visual->blue_mask; |
|
|
227 | alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; |
|
|
228 | } |
|
|
229 | |
|
|
230 | int width_r = ecb_popcount32 (red_mask); |
|
|
231 | int width_g = ecb_popcount32 (green_mask); |
|
|
232 | int width_b = ecb_popcount32 (blue_mask); |
|
|
233 | int width_a = ecb_popcount32 (alpha_mask); |
|
|
234 | |
|
|
235 | if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) |
|
|
236 | return false; |
|
|
237 | |
|
|
238 | int sh_r = ecb_ctz32 (red_mask); |
|
|
239 | int sh_g = ecb_ctz32 (green_mask); |
|
|
240 | int sh_b = ecb_ctz32 (blue_mask); |
|
|
241 | int sh_a = ecb_ctz32 (alpha_mask); |
|
|
242 | |
|
|
243 | if (width > 32767 || height > 32767) |
|
|
244 | return false; |
|
|
245 | |
|
|
246 | XImage *ximage = XCreateImage (dpy, s->visual, argb ? 32 : format->depth, ZPixmap, 0, 0, |
|
|
247 | width, height, 32, 0); |
|
|
248 | if (!ximage) |
|
|
249 | return false; |
|
|
250 | |
|
|
251 | if (height > INT_MAX / ximage->bytes_per_line |
|
|
252 | || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) |
|
|
253 | { |
|
|
254 | XDestroyImage (ximage); |
|
|
255 | return false; |
|
|
256 | } |
|
|
257 | |
|
|
258 | GC gc = XCreateGC (dpy, pm, 0, 0); |
|
|
259 | |
|
|
260 | ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; |
|
|
261 | |
|
|
262 | int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
|
|
263 | int channels = gdk_pixbuf_get_n_channels (pixbuf); |
|
|
264 | unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; |
|
|
265 | char *line = ximage->data; |
|
|
266 | |
|
|
267 | rgba c (0, 0, 0); |
|
|
268 | |
|
|
269 | if (channels == 4 && alpha_mask == 0) |
|
|
270 | { |
|
|
271 | //pix_colors[Color_bg].get (c); |
192 | //TODO |
272 | //TODO |
193 | } |
273 | c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D |
|
|
274 | c.r >>= 8; |
|
|
275 | c.g >>= 8; |
|
|
276 | c.b >>= 8; |
|
|
277 | } |
194 | |
278 | |
|
|
279 | for (int y = 0; y < height; y++) |
|
|
280 | { |
|
|
281 | for (int x = 0; x < width; x++) |
|
|
282 | { |
|
|
283 | unsigned char *pixel = row + x * channels; |
|
|
284 | uint32_t value; |
|
|
285 | unsigned char r, g, b, a; |
|
|
286 | |
|
|
287 | if (channels == 4) |
|
|
288 | { |
|
|
289 | a = pixel[3]; |
|
|
290 | r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; |
|
|
291 | g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; |
|
|
292 | b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; |
|
|
293 | } |
|
|
294 | else |
|
|
295 | { |
|
|
296 | a = 0xff; |
|
|
297 | r = pixel[0]; |
|
|
298 | g = pixel[1]; |
|
|
299 | b = pixel[2]; |
|
|
300 | } |
|
|
301 | |
|
|
302 | value = ((r >> (8 - width_r)) << sh_r) |
|
|
303 | | ((g >> (8 - width_g)) << sh_g) |
|
|
304 | | ((b >> (8 - width_b)) << sh_b) |
|
|
305 | | ((a >> (8 - width_a)) << sh_a); |
|
|
306 | |
|
|
307 | if (ximage->bits_per_pixel == 32) |
|
|
308 | ((uint32_t *)line)[x] = value; |
|
|
309 | else |
|
|
310 | XPutPixel (ximage, x, y, value); |
|
|
311 | } |
|
|
312 | |
|
|
313 | row += rowstride; |
|
|
314 | line += ximage->bytes_per_line; |
|
|
315 | } |
|
|
316 | |
|
|
317 | XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); |
|
|
318 | XDestroyImage (ximage); |
|
|
319 | XFreeGC (dpy, gc); |
|
|
320 | |
|
|
321 | return true; |
|
|
322 | } |
|
|
323 | |
195 | rxvt_img * |
324 | rxvt_img * |
196 | rxvt_img::copy () |
325 | rxvt_img::clone () |
197 | { |
326 | { |
198 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
327 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
199 | Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth); |
328 | Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth); |
200 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
329 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); |
201 | XFreeGC (s->display->dpy, gc); |
330 | XFreeGC (s->display->dpy, gc); |
202 | return new rxvt_img (s, format, w, h, pm2); |
331 | return new rxvt_img (s, format, w, h, pm2); |
203 | } |
332 | } |
204 | |
333 | |
205 | rxvt_img * |
334 | rxvt_img * |
206 | rxvt_img::transform (int new_width, int new_height, double matrix[16]) |
335 | rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) |
207 | { |
336 | { |
208 | //TODO |
337 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
209 | } |
|
|
210 | |
338 | |
211 | rxvt_img * |
|
|
212 | rxvt_img::scale (int new_width, int new_height) |
|
|
213 | { |
|
|
214 | // use transform |
|
|
215 | //TODO |
|
|
216 | } |
|
|
217 | |
|
|
218 | rxvt_img * |
|
|
219 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
|
|
220 | { |
|
|
221 | Display *dpy = s->display->dpy; |
339 | Display *dpy = s->display->dpy; |
222 | Pixmap new_pm = XCreatePixmap (dpy, pm, w, h, new_format->depth); |
340 | XRenderPictureAttributes pa; |
|
|
341 | pa.repeat = repeat; |
223 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
342 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
224 | Picture dst = XRenderCreatePicture (dpy, new_pm, new_format, 0, 0); |
343 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
225 | |
344 | |
|
|
345 | XTransform xfrm; |
|
|
346 | |
|
|
347 | for (int i = 0; i < 3; ++i) |
|
|
348 | for (int j = 0; j < 3; ++j) |
|
|
349 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
|
|
350 | |
|
|
351 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
|
|
352 | XRenderSetPictureTransform (dpy, src, &xfrm); |
226 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
353 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
227 | |
354 | |
228 | XRenderFreePicture (dpy, src); |
355 | XRenderFreePicture (dpy, src); |
229 | XRenderFreePicture (dpy, dst); |
356 | XRenderFreePicture (dpy, dst); |
230 | |
357 | |
231 | rxvt_img *img = new rxvt_img ( |
|
|
232 | s, |
|
|
233 | new_format, |
|
|
234 | w, |
|
|
235 | h, |
|
|
236 | new_pm |
|
|
237 | ); |
|
|
238 | |
|
|
239 | return img; |
358 | return img; |
240 | } |
359 | } |
241 | |
360 | |
|
|
361 | rxvt_img * |
|
|
362 | rxvt_img::scale (int new_width, int new_height) |
|
|
363 | { |
|
|
364 | double matrix[9] = { |
|
|
365 | w / (double)new_width, 0, 0, |
|
|
366 | 0, h / (double)new_height, 0, |
|
|
367 | 0, 0, 1 |
|
|
368 | }; |
|
|
369 | |
|
|
370 | return transform (new_width, new_height, RepeatNormal, matrix); |
|
|
371 | } |
|
|
372 | |
|
|
373 | rxvt_img * |
|
|
374 | rxvt_img::rotate (int new_width, int new_height, int repeat, int x, int y, double phi) |
|
|
375 | { |
|
|
376 | double s = sin (phi); |
|
|
377 | double c = cos (phi); |
|
|
378 | |
|
|
379 | double matrix[9] = { |
|
|
380 | c, -s, -c * x + s * y + x, |
|
|
381 | s, c, -s * x - c * y + y, |
|
|
382 | 0, 0, 1 |
|
|
383 | }; |
|
|
384 | |
|
|
385 | return transform (new_width, new_height, repeat, matrix); |
|
|
386 | } |
|
|
387 | |
|
|
388 | rxvt_img * |
|
|
389 | rxvt_img::convert_to (XRenderPictFormat *new_format) |
|
|
390 | { |
|
|
391 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
|
|
392 | |
|
|
393 | Display *dpy = s->display->dpy; |
|
|
394 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
395 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
|
|
396 | |
|
|
397 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
398 | |
|
|
399 | XRenderFreePicture (dpy, src); |
|
|
400 | XRenderFreePicture (dpy, dst); |
|
|
401 | |
|
|
402 | return img; |
|
|
403 | } |
|
|
404 | |
242 | #endif |
405 | #endif |
243 | |
406 | |