ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.6 by sf-exg, Sun Jun 3 18:49:55 2012 UTC vs.
Revision 1.21 by sf-exg, Tue Jun 5 14:59:44 2012 UTC

16: s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) 16: s(screen), pm(pixmap), w(width), h(height), format(format), shared(false)
17{ 17{
18} 18}
19 19
20rxvt_img * 20rxvt_img *
21rxvt_img::new_from_root (rxvt_screen *s)
22{
23 Display *dpy = s->display->dpy;
24 unsigned int root_pm_w, root_pm_h;
25 Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_XROOTPMAP_ID]);
26 if (root_pixmap == None)
27 root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_ESETROOT_PMAP_ID]);
28
29 if (root_pixmap == None)
30 return 0;
31
32 Window wdummy;
33 int idummy;
34 unsigned int udummy;
35
36 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy))
37 return 0;
38
39 rxvt_img *img = new rxvt_img (
40 s,
41 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
42 root_pm_w,
43 root_pm_h,
44 root_pixmap
45 );
46
47 img->shared = true;
48
49 return img;
50}
51
52rxvt_img *
21rxvt_img::new_from_file (rxvt_screen *s, const char *filename) 53rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
22{ 54{
23 GError *err; 55 GError *err = 0;
24 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); 56 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
25 57
26 if (!pb) 58 if (!pb)
27 return 0; 59 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
28 60
29 rxvt_img *img = new rxvt_img ( 61 rxvt_img *img = new rxvt_img (
30 s, 62 s,
31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 63 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
32 gdk_pixbuf_get_width (pb), 64 gdk_pixbuf_get_width (pb),
33 gdk_pixbuf_get_height (pb) 65 gdk_pixbuf_get_height (pb)
34 ); 66 );
35 67
36 img->render (pb, 0, 0, img->w, img->h, 0, 0); 68 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
37 69
38 return img; 70 return img;
39} 71}
40 72
41rxvt_img::~rxvt_img () 73rxvt_img::~rxvt_img ()
42{ 74{
43 if (!shared) 75 if (!shared)
44 XFreePixmap (s->display->dpy, pm); 76 XFreePixmap (s->display->dpy, pm);
77}
78
79void
80rxvt_img::unshare ()
81{
82 if (!shared)
83 return;
84
85 rxvt_img *img = clone ();
86
87 ::swap (pm , img->pm);
88 ::swap (shared, img->shared);
89
90 delete img;
45} 91}
46 92
47void 93void
48rxvt_img::fill (const rxvt_color &c) 94rxvt_img::fill (const rxvt_color &c)
49{ 95{
87 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 133 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
88 134
89 XRenderPictureAttributes pa; 135 XRenderPictureAttributes pa;
90 136
91 pa.repeat = RepeatPad; 137 pa.repeat = RepeatPad;
92 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 138 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
93 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); 139 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth);
94 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 140 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
95 XFreePixmap (dpy, tmp); 141 XFreePixmap (dpy, tmp);
96 142
97 if (kernel && params) 143 if (kernel && params)
184 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 230 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
185 231
186 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 232 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
187} 233}
188 234
189void 235bool
190rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 236rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
191{ 237{
238 Display *dpy = s->display->dpy;
239
240 if (s->visual->c_class != TrueColor)
241 return false;
242
243 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
244
245 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
246 green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
247 blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
248 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
249
250 int width_r = ecb_popcount32 (red_mask);
251 int width_g = ecb_popcount32 (green_mask);
252 int width_b = ecb_popcount32 (blue_mask);
253 int width_a = ecb_popcount32 (alpha_mask);
254
255 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
256 return false;
257
258 int sh_r = ecb_ctz32 (red_mask);
259 int sh_g = ecb_ctz32 (green_mask);
260 int sh_b = ecb_ctz32 (blue_mask);
261 int sh_a = ecb_ctz32 (alpha_mask);
262
263 if (width > 32767 || height > 32767)
264 return false;
265
266 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
267 width, height, 32, 0);
268 if (!ximage)
269 return false;
270
271 if (height > INT_MAX / ximage->bytes_per_line
272 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
273 {
274 XDestroyImage (ximage);
275 return false;
276 }
277
278 GC gc = XCreateGC (dpy, pm, 0, 0);
279
280 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
281
282 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
283 int channels = gdk_pixbuf_get_n_channels (pixbuf);
284 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
285 char *line = ximage->data;
286
287 rgba c (0, 0, 0);
288
289 if (channels == 4 && alpha_mask == 0)
290 {
291 //pix_colors[Color_bg].get (c);
192 //TODO 292 //TODO
193} 293 c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D
294 c.r >>= 8;
295 c.g >>= 8;
296 c.b >>= 8;
297 }
194 298
299 for (int y = 0; y < height; y++)
300 {
301 for (int x = 0; x < width; x++)
302 {
303 unsigned char *pixel = row + x * channels;
304 uint32_t value;
305 unsigned char r, g, b, a;
306
307 if (channels == 4)
308 {
309 a = pixel[3];
310 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
311 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
312 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
313 }
314 else
315 {
316 a = 0xff;
317 r = pixel[0];
318 g = pixel[1];
319 b = pixel[2];
320 }
321
322 value = ((r >> (8 - width_r)) << sh_r)
323 | ((g >> (8 - width_g)) << sh_g)
324 | ((b >> (8 - width_b)) << sh_b)
325 | ((a >> (8 - width_a)) << sh_a);
326
327 if (ximage->bits_per_pixel == 32)
328 ((uint32_t *)line)[x] = value;
329 else
330 XPutPixel (ximage, x, y, value);
331 }
332
333 row += rowstride;
334 line += ximage->bytes_per_line;
335 }
336
337 XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
338 XDestroyImage (ximage);
339 XFreeGC (dpy, gc);
340
341 return true;
342}
343
195rxvt_img * 344rxvt_img *
196rxvt_img::copy () 345rxvt_img::clone ()
197{ 346{
347 rxvt_img *img = new rxvt_img (s, format, w, h);
348
198 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 349 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
199 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); 350 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0);
201 XFreeGC (s->display->dpy, gc); 351 XFreeGC (s->display->dpy, gc);
202 return new rxvt_img (s, format, w, h, pm2);
203}
204 352
353 return img;
354}
355
205rxvt_img * 356rxvt_img *
357rxvt_img::sub_rect (int x, int y, int width, int height, int repeat)
358{
359 rxvt_img *img = new rxvt_img (s, format, width, height);
360
361 Display *dpy = s->display->dpy;
362 XRenderPictureAttributes pa;
363 pa.repeat = repeat;
364 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
365 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
366
367 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height);
368
369 XRenderFreePicture (dpy, src);
370 XRenderFreePicture (dpy, dst);
371
372 return img;
373}
374
375rxvt_img *
206rxvt_img::transform (int new_width, int new_height, double matrix[16]) 376rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat)
207{ 377{
208 //TODO 378 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
379
380 Display *dpy = s->display->dpy;
381 XRenderPictureAttributes pa;
382 pa.repeat = repeat;
383 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
384 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
385
386 XTransform xfrm;
387
388 for (int i = 0; i < 3; ++i)
389 for (int j = 0; j < 3; ++j)
390 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
391
392 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
393 XRenderSetPictureTransform (dpy, src, &xfrm);
394 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
395
396 XRenderFreePicture (dpy, src);
397 XRenderFreePicture (dpy, dst);
398
399 return img;
209} 400}
210 401
211rxvt_img * 402rxvt_img *
212rxvt_img::scale (int new_width, int new_height) 403rxvt_img::scale (int new_width, int new_height)
213{ 404{
214 // use transform 405 double matrix[9] = {
215 //TODO 406 w / (double)new_width, 0, 0,
216} 407 0, h / (double)new_height, 0,
408 0, 0, 1
409 };
217 410
411 return transform (new_width, new_height, matrix);
412}
413
414rxvt_img *
415rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat)
416{
417 double s = sin (phi);
418 double c = cos (phi);
419
420 double matrix[9] = {
421 c, -s, -c * x + s * y + x,
422 s, c, -s * x - c * y + y,
423 0, 0, 1
424 };
425
426 return transform (new_width, new_height, matrix, repeat);
427}
428
429rxvt_img *
430rxvt_img::convert_to (XRenderPictFormat *new_format)
431{
432 rxvt_img *img = new rxvt_img (s, new_format, w, h);
433
434 Display *dpy = s->display->dpy;
435 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
436 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
437
438 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
439
440 XRenderFreePicture (dpy, src);
441 XRenderFreePicture (dpy, dst);
442
443 return img;
444}
218 445
219#endif 446#endif
220 447

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines