main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * MODULE: g.cairocomp
  3. * AUTHOR(S): Glynn Clements
  4. * PURPOSE: g.cairocomp isn't meant for end users. It's an internal tool for use by
  5. * a wx GUI.
  6. * g.cairocomp composites a series of X pixmaps.
  7. * COPYRIGHT: (C) 2008 by Glynn Clements and the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <string.h>
  18. #include <cairo.h>
  19. #include <cairo-xlib.h>
  20. #include <cairo-xlib-xrender.h>
  21. #include <X11/X.h>
  22. #include <X11/Xlib.h>
  23. #include <X11/Xutil.h>
  24. #include <grass/gis.h>
  25. #include <grass/colors.h>
  26. #include <grass/glocale.h>
  27. static int width, height;
  28. static XID output;
  29. static Display *dpy;
  30. static cairo_surface_t *surface;
  31. static cairo_t *cairo;
  32. static Screen *screen;
  33. static XRenderPictFormat *format;
  34. static int depth;
  35. static XID read_xid(const char *filename)
  36. {
  37. FILE *fp;
  38. char buf[64];
  39. long xid;
  40. fp = fopen(filename, "r");
  41. if (!fp)
  42. G_fatal_error(_("Unable to open input file <%s>"), filename);
  43. if (!fgets(buf, sizeof(buf), fp))
  44. G_fatal_error(_("Unable to read input file <%s>"), filename);
  45. if (sscanf(buf, "%li", &xid) != 1)
  46. G_fatal_error(_("Unable to parse input file <%s>"), filename);
  47. fclose(fp);
  48. return (XID) xid;
  49. }
  50. static void write_xid(const char *filename, XID xid)
  51. {
  52. FILE *fp;
  53. char buf[64];
  54. fp = fopen(filename, "w");
  55. if (!fp)
  56. G_fatal_error(_("Unable to open output file <%s>"), filename);
  57. sprintf(buf, "0x%08lx\n", (unsigned long) xid);
  58. if (fputs(buf, fp) < 0)
  59. G_fatal_error(_("Unable to write output file <%s>"), filename);
  60. fclose(fp);
  61. }
  62. static void init_xlib(const char *scr, const char *vis)
  63. {
  64. XVisualInfo templ;
  65. XVisualInfo *vinfo;
  66. int count;
  67. Visual *visual;
  68. Pixmap pix;
  69. int scrn;
  70. long visid;
  71. cairo_surface_t *s1, *s2;
  72. dpy = XOpenDisplay(NULL);
  73. if (!dpy)
  74. G_fatal_error(_("Unable to open display"));
  75. if (scr)
  76. scrn = atoi(scr);
  77. else {
  78. const char *p = getenv("GRASS_RENDER_CAIRO_SCREEN");
  79. if (!p || sscanf(p, "%i", &scrn) != 1)
  80. scrn = DefaultScreen(dpy);
  81. }
  82. screen = ScreenOfDisplay(dpy, scrn);
  83. if (vis)
  84. visid = strtoul(vis, NULL, 0);
  85. else {
  86. const char *p = getenv("GRASS_RENDER_CAIRO_VISUAL");
  87. if (!p || sscanf(p, "%li", &visid) != 1)
  88. visid = DefaultVisual(dpy, scrn)->visualid;
  89. }
  90. templ.visualid = visid;
  91. templ.screen = scrn;
  92. vinfo = XGetVisualInfo(dpy, VisualIDMask|VisualScreenMask, &templ, &count);
  93. if (!vinfo || !count)
  94. G_fatal_error(_("Unable to obtain visual"));
  95. visual = vinfo[0].visual;
  96. pix = XCreatePixmap(dpy, RootWindow(dpy, scrn), 1, 1, vinfo[0].depth);
  97. s1 = cairo_xlib_surface_create(dpy, pix, visual, 1, 1);
  98. s2 = cairo_surface_create_similar(s1, CAIRO_CONTENT_COLOR_ALPHA, 1, 1);
  99. format = cairo_xlib_surface_get_xrender_format(s2);
  100. depth = cairo_xlib_surface_get_depth(s2);
  101. cairo_surface_destroy(s2);
  102. cairo_surface_destroy(s1);
  103. XFreePixmap(dpy, pix);
  104. output = XCreatePixmap(dpy, RootWindow(dpy, scrn), width, height, depth);
  105. surface = cairo_xlib_surface_create_with_xrender_format(dpy, output, screen, format, width, height);
  106. if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
  107. G_fatal_error(_("Failed to initialize output surface"));
  108. cairo = cairo_create(surface);
  109. }
  110. static void fini_xlib(void)
  111. {
  112. cairo_surface_destroy(surface);
  113. XFlush(dpy);
  114. XSetCloseDownMode(dpy, RetainPermanent);
  115. XCloseDisplay(dpy);
  116. }
  117. static void erase(const char *color)
  118. {
  119. int r, g, b, a;
  120. double fr, fg, fb, fa;
  121. a = 255;
  122. if (sscanf(color, "%d:%d:%d:%d", &r, &g, &b, &a) != 4 ||
  123. G_str_to_color(color, &r, &g, &b) != 1)
  124. G_fatal_error(_("Invalid color: %s"), color);
  125. fr = r / 255.0;
  126. fg = g / 255.0;
  127. fb = b / 255.0;
  128. fa = a / 255.0;
  129. cairo_save(cairo);
  130. cairo_set_source_rgba(cairo, fr, fg, fb, fa);
  131. cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
  132. cairo_paint(cairo);
  133. cairo_restore(cairo);
  134. }
  135. static void overlay(XID src, float alpha)
  136. {
  137. cairo_surface_t *src_surf;
  138. src_surf = cairo_xlib_surface_create_with_xrender_format(
  139. dpy, src, screen, format, width, height);
  140. if (cairo_surface_status(src_surf) != CAIRO_STATUS_SUCCESS)
  141. G_fatal_error(_("Failed to initialize input surface"));
  142. cairo_save(cairo);
  143. cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
  144. cairo_set_source_surface(cairo, src_surf, 0, 0);
  145. cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_NEAREST);
  146. cairo_paint_with_alpha(cairo, alpha);
  147. cairo_restore(cairo);
  148. cairo_surface_destroy(src_surf);
  149. }
  150. int main(int argc, char *argv[])
  151. {
  152. struct GModule *module;
  153. struct
  154. {
  155. struct Option *in, *out, *visual, *screen, *alpha, *width, *height, *bg;
  156. } opt;
  157. struct Flag *flag_d;
  158. int i;
  159. G_gisinit(argv[0]);
  160. module = G_define_module();
  161. G_add_keyword(_("general"));
  162. G_add_keyword(_("display"));
  163. module->description = _("Overlays multiple X Pixmaps.");
  164. opt.in = G_define_standard_option(G_OPT_F_INPUT);
  165. opt.in->required = YES;
  166. opt.in->multiple = YES;
  167. opt.in->description = _("Name of input file(s)");
  168. opt.out = G_define_standard_option(G_OPT_F_OUTPUT);
  169. opt.out->required = YES;
  170. opt.visual = G_define_option();
  171. opt.visual->key = "visual";
  172. opt.visual->type = TYPE_INTEGER;
  173. opt.visual->required = NO;
  174. opt.visual->description = _("Output Visual XID");
  175. opt.screen = G_define_option();
  176. opt.screen->key = "screen";
  177. opt.screen->type = TYPE_INTEGER;
  178. opt.screen->required = NO;
  179. opt.screen->description = _("Output screen");
  180. opt.alpha = G_define_option();
  181. opt.alpha->key = "opacity";
  182. opt.alpha->type = TYPE_DOUBLE;
  183. opt.alpha->multiple = YES;
  184. opt.alpha->description = _("Layer opacities");
  185. opt.width = G_define_option();
  186. opt.width->key = "width";
  187. opt.width->type = TYPE_INTEGER;
  188. opt.width->required = YES;
  189. opt.width->description = _("Image width");
  190. opt.height = G_define_option();
  191. opt.height->key = "height";
  192. opt.height->type = TYPE_INTEGER;
  193. opt.height->required = YES;
  194. opt.height->description = _("Image height");
  195. opt.bg = G_define_standard_option(G_OPT_C);
  196. opt.bg->key = "bgcolor";
  197. opt.bg->label = _("Background color (R:G:B:A)");
  198. opt.bg->answer = NULL;
  199. flag_d = G_define_flag();
  200. flag_d->key = 'd';
  201. flag_d->description = _("Do not composite; just delete input Pixmaps");
  202. if (G_parser(argc, argv))
  203. exit(EXIT_FAILURE);
  204. width = atoi(opt.width->answer);
  205. height = atoi(opt.height->answer);
  206. if (flag_d->answer) {
  207. dpy = XOpenDisplay(NULL);
  208. if (!dpy)
  209. G_fatal_error(_("Unable to open display"));
  210. for (i = 0; opt.in->answers[i]; i++) {
  211. unsigned long xid = read_xid(opt.in->answers[i]);
  212. XKillClient(dpy, xid);
  213. }
  214. XFlush(dpy);
  215. XCloseDisplay(dpy);
  216. return 0;
  217. }
  218. init_xlib(opt.screen->answer, opt.visual->answer);
  219. if (opt.bg->answer)
  220. erase(opt.bg->answer);
  221. for (i = 0; opt.in->answers[i]; i++) {
  222. XID input = read_xid(opt.in->answers[i]);
  223. double alpha;
  224. if (opt.alpha->answer && !opt.alpha->answers[i])
  225. G_fatal_error(
  226. _("input= and opacity= must have the same number of values"));
  227. alpha = opt.alpha->answer ? atof(opt.alpha->answers[i]) : 1.0;
  228. overlay(input, alpha);
  229. }
  230. write_xid(opt.out->answer, output);
  231. fini_xlib();
  232. return 0;
  233. }