main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/glocale.h>
  26. static int width, height;
  27. static XID output;
  28. static Display *dpy;
  29. static cairo_surface_t *surface;
  30. static cairo_t *cairo;
  31. static Screen *screen;
  32. static XRenderPictFormat *format;
  33. static int depth;
  34. static XID read_xid(const char *filename)
  35. {
  36. FILE *fp;
  37. char buf[64];
  38. long xid;
  39. fp = fopen(filename, "r");
  40. if (!fp)
  41. G_fatal_error(_("Unable to open input file <%s>"), filename);
  42. if (!fgets(buf, sizeof(buf), fp))
  43. G_fatal_error(_("Unable to read input file <%s>"), filename);
  44. if (sscanf(buf, "%li", &xid) != 1)
  45. G_fatal_error(_("Unable to parse input file <%s>"), filename);
  46. fclose(fp);
  47. return (XID) xid;
  48. }
  49. static void write_xid(const char *filename, XID xid)
  50. {
  51. FILE *fp;
  52. char buf[64];
  53. fp = fopen(filename, "w");
  54. if (!fp)
  55. G_fatal_error(_("Unable to open output file <%s>"), filename);
  56. sprintf(buf, "0x%08lx\n", (unsigned long) xid);
  57. if (fputs(buf, fp) < 0)
  58. G_fatal_error(_("Unable to write output file <%s>"), filename);
  59. fclose(fp);
  60. }
  61. static void init_xlib(const char *scr, const char *vis)
  62. {
  63. XVisualInfo templ;
  64. XVisualInfo *vinfo;
  65. int count;
  66. Visual *visual;
  67. Pixmap pix;
  68. int scrn;
  69. long visid;
  70. cairo_surface_t *s1, *s2;
  71. dpy = XOpenDisplay(NULL);
  72. if (!dpy)
  73. G_fatal_error(_("Unable to open display"));
  74. if (scr)
  75. scrn = atoi(scr);
  76. else {
  77. const char *p = getenv("GRASS_CAIRO_SCREEN");
  78. if (!p || sscanf(p, "%i", &scrn) != 1)
  79. scrn = DefaultScreen(dpy);
  80. }
  81. screen = ScreenOfDisplay(dpy, scrn);
  82. if (vis)
  83. visid = strtoul(vis, NULL, 0);
  84. else {
  85. const char *p = getenv("GRASS_CAIRO_VISUAL");
  86. if (!p || sscanf(p, "%li", &visid) != 1)
  87. visid = DefaultVisual(dpy, scrn)->visualid;
  88. }
  89. templ.visualid = visid;
  90. templ.screen = scrn;
  91. vinfo = XGetVisualInfo(dpy, VisualIDMask|VisualScreenMask, &templ, &count);
  92. if (!vinfo || !count)
  93. G_fatal_error(_("Unable to obtain visual"));
  94. visual = vinfo[0].visual;
  95. pix = XCreatePixmap(dpy, RootWindow(dpy, scrn), 1, 1, vinfo[0].depth);
  96. s1 = cairo_xlib_surface_create(dpy, pix, visual, 1, 1);
  97. s2 = cairo_surface_create_similar(s1, CAIRO_CONTENT_COLOR_ALPHA, 1, 1);
  98. format = cairo_xlib_surface_get_xrender_format(s2);
  99. depth = cairo_xlib_surface_get_depth(s2);
  100. cairo_surface_destroy(s2);
  101. cairo_surface_destroy(s1);
  102. XFreePixmap(dpy, pix);
  103. output = XCreatePixmap(dpy, RootWindow(dpy, scrn), width, height, depth);
  104. surface = cairo_xlib_surface_create_with_xrender_format(dpy, output, screen, format, width, height);
  105. if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
  106. G_fatal_error(_("Failed to initialize output surface"));
  107. cairo = cairo_create(surface);
  108. }
  109. static void fini_xlib(void)
  110. {
  111. cairo_surface_destroy(surface);
  112. XFlush(dpy);
  113. XSetCloseDownMode(dpy, RetainPermanent);
  114. XCloseDisplay(dpy);
  115. }
  116. static void erase(const char *color)
  117. {
  118. int r, g, b, a;
  119. double fr, fg, fb, fa;
  120. if (sscanf(color, "%d:%d:%d:%d", &r, &g, &b, &a) != 4)
  121. G_fatal_error(_("Invalid color: %s"), color);
  122. fr = r / 255.0;
  123. fg = g / 255.0;
  124. fb = b / 255.0;
  125. fa = a / 255.0;
  126. cairo_save(cairo);
  127. cairo_set_source_rgba(cairo, fr, fg, fb, fa);
  128. cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
  129. cairo_paint(cairo);
  130. cairo_restore(cairo);
  131. }
  132. static void overlay(XID src, float alpha)
  133. {
  134. cairo_surface_t *src_surf;
  135. src_surf = cairo_xlib_surface_create_with_xrender_format(
  136. dpy, src, screen, format, width, height);
  137. if (cairo_surface_status(src_surf) != CAIRO_STATUS_SUCCESS)
  138. G_fatal_error(_("Failed to initialize input surface"));
  139. cairo_save(cairo);
  140. cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
  141. cairo_set_source_surface(cairo, src_surf, 0, 0);
  142. cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_NEAREST);
  143. cairo_paint_with_alpha(cairo, alpha);
  144. cairo_restore(cairo);
  145. cairo_surface_destroy(src_surf);
  146. }
  147. int main(int argc, char *argv[])
  148. {
  149. struct GModule *module;
  150. struct
  151. {
  152. struct Option *in, *out, *visual, *screen, *alpha, *width, *height, *bg;
  153. } opt;
  154. struct Flag *flag_d;
  155. int i;
  156. G_gisinit(argv[0]);
  157. module = G_define_module();
  158. G_add_keyword(_("general"));
  159. G_add_keyword(_("gui"));
  160. module->description = _("Overlays multiple X Pixmaps");
  161. opt.in = G_define_option();
  162. opt.in->key = "input";
  163. opt.in->type = TYPE_STRING;
  164. opt.in->required = YES;
  165. opt.in->multiple = YES;
  166. opt.in->description = _("Names of input files");
  167. opt.in->gisprompt = "old_file,file,input";
  168. opt.out = G_define_option();
  169. opt.out->key = "output";
  170. opt.out->type = TYPE_STRING;
  171. opt.out->required = YES;
  172. opt.out->description = _("Name of output file");
  173. opt.out->gisprompt = "new_file,file,output";
  174. opt.visual = G_define_option();
  175. opt.visual->key = "visual";
  176. opt.visual->type = TYPE_INTEGER;
  177. opt.visual->required = NO;
  178. opt.visual->description = _("Output Visual XID");
  179. opt.screen = G_define_option();
  180. opt.screen->key = "screen";
  181. opt.screen->type = TYPE_INTEGER;
  182. opt.screen->required = NO;
  183. opt.screen->description = _("Output screen");
  184. opt.alpha = G_define_option();
  185. opt.alpha->key = "opacity";
  186. opt.alpha->type = TYPE_DOUBLE;
  187. opt.alpha->multiple = YES;
  188. opt.alpha->description = _("Layer opacities");
  189. opt.width = G_define_option();
  190. opt.width->key = "width";
  191. opt.width->type = TYPE_INTEGER;
  192. opt.width->required = YES;
  193. opt.width->description = _("Image width");
  194. opt.height = G_define_option();
  195. opt.height->key = "height";
  196. opt.height->type = TYPE_INTEGER;
  197. opt.height->required = YES;
  198. opt.height->description = _("Image height");
  199. opt.bg = G_define_option();
  200. opt.bg->key = "background";
  201. opt.bg->type = TYPE_STRING;
  202. opt.bg->description = _("Background color");
  203. flag_d = G_define_flag();
  204. flag_d->key = 'd';
  205. flag_d->description = _("Don't composite; just delete input Pixmaps");
  206. if (G_parser(argc, argv))
  207. exit(EXIT_FAILURE);
  208. width = atoi(opt.width->answer);
  209. height = atoi(opt.height->answer);
  210. if (flag_d->answer) {
  211. dpy = XOpenDisplay(NULL);
  212. if (!dpy)
  213. G_fatal_error(_("Unable to open display"));
  214. for (i = 0; opt.in->answers[i]; i++) {
  215. unsigned long xid = read_xid(opt.in->answers[i]);
  216. XKillClient(dpy, xid);
  217. }
  218. XFlush(dpy);
  219. XCloseDisplay(dpy);
  220. return 0;
  221. }
  222. init_xlib(opt.screen->answer, opt.visual->answer);
  223. if (opt.bg->answer)
  224. erase(opt.bg->answer);
  225. for (i = 0; opt.in->answers[i]; i++) {
  226. XID input = read_xid(opt.in->answers[i]);
  227. double alpha;
  228. if (opt.alpha->answer && !opt.alpha->answers[i])
  229. G_fatal_error(
  230. _("input= and opacity= must have the same number of values"));
  231. alpha = opt.alpha->answer ? atof(opt.alpha->answers[i]) : 1.0;
  232. overlay(input, alpha);
  233. }
  234. write_xid(opt.out->answer, output);
  235. fini_xlib();
  236. return 0;
  237. }