Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
batch_render.cpp
1 
2 /***************************************************************************
3  * batch_render.cpp - Render a directory of dot graphs
4  *
5  * Created: Sat Mar 21 17:16:01 2009
6  * Copyright 2008-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "gvplugin_skillgui_cairo.h"
24 
25 #include <utils/system/argparser.h>
26 #include <cstdlib>
27 #include <cstring>
28 #include <cstdio>
29 #include <cmath>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 
37 using namespace fawkes;
38 
39 
40 /** DOT graph batch renderer. */
43 {
44  public:
45  /** Constructor.
46  * @param argc number of arguments
47  * @param argv arguments
48  */
49  SkillGuiBatchRenderer(int argc, char **argv)
50  : argp(argc, argv, "hi:o:f:wps:")
51  {
52  if (! (argp.has_arg("i") && argp.has_arg("o") && argp.has_arg("f"))
53  || argp.has_arg("h")) {
54  usage();
55  exit(-1);
56  }
57 
58  format = argp.arg("f");
59  write_to_png = false;
60  bbw = bbh = 0;
61  white_bg = argp.has_arg("w");
62  postproc_required = false;
63  do_postproc = argp.has_arg("p");
64  maxwidth = maxheight = 0;
65  scale = 1.0;
66 
67  if ( (format != "pdf") && (format != "svg") && (format != "png") ) {
68  printf("Unknown format '%s'\n\n", format.c_str());
69  usage();
70  exit(-2);
71  }
72 
73  if ( do_postproc && (format != "png") ) {
74  printf("Post-processing only available for PNG output format.\n");
75  exit(-7);
76  }
77 
78  if (argp.has_arg("s")) {
79  char *endptr;
80  scale = strtod(argp.arg("s"), &endptr);
81  if ( *endptr != 0 ) {
82  printf("Invalid scale value '%s', could not convert to number (failed at '%s').\n",
83  argp.arg("s"), endptr);
84  exit(-8);
85  }
86  }
87 
88  indir = argp.arg("i");
89  outdir = argp.arg("o");
90 
91  struct stat statbuf_in, statbuf_out;
92  if (stat(indir.c_str(), &statbuf_in) != 0) {
93  perror("Unable to stat input directory");
94  exit(-3);
95  }
96  if (stat(outdir.c_str(), &statbuf_out) != 0) {
97  perror("Unable to stat output directory");
98  exit(-4);
99  }
100  if (! S_ISDIR(statbuf_in.st_mode) || ! S_ISDIR(statbuf_out.st_mode)) {
101  printf("Input or output directory is not a directory.\n\n");
102  exit(-5);
103  }
104 
105  char outdir_real[PATH_MAX];
106  if (realpath(outdir.c_str(), outdir_real)) {
107  outdir = outdir_real;
108  }
109 
110  directory = opendir(indir.c_str());
111  if (! directory) {
112  printf("Could not open input directory\n");
113  exit(-6);
114  }
115 
116  gvc = gvContext();
117  gvplugin_skillgui_cairo_setup(gvc, this);
118  }
119 
120  /** Destructor. */
122  {
123  gvFreeContext(gvc);
124  closedir(directory);
125  }
126 
127  /** Show usage instructions. */
128  void usage()
129  {
130  printf("\nUsage: %s -i <dir> -o <dir> -f <format> [-w] [-s scale]\n"
131  " -i dir Input directory containing dot graphs\n"
132  " -o dir Output directory for generated graphs\n"
133  " -f format Output format, one of pdf, svg, or png\n"
134  " -w White background\n"
135  " -p Postprocess frames to same size (PNG only)\n"
136  " -s scale Scale factor to apply during rendering\n"
137  "\n",
138  argp.program_name());
139  }
140 
141  virtual Cairo::RefPtr<Cairo::Context> get_cairo()
142  {
143  if (! cairo) {
144  this->bbw = bbw;
145  this->bbh = bbh;
146  if (format == "pdf") {
147  surface = Cairo::PdfSurface::create(outfile, bbw * scale, bbh * scale);
148  printf("Creating PDF context of size %f x %f\n", bbw * scale, bbh * scale);
149  } else if (format == "svg") {
150  surface = Cairo::SvgSurface::create(outfile, bbw * scale, bbh * scale);
151  } else if (format == "png") {
152  surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32,
153  (int)ceilf(bbw * scale),
154  (int)ceilf(bbh * scale));
155  write_to_png = true;
156  }
157  cairo = Cairo::Context::create(surface);
158  if (white_bg) {
159  cairo->set_source_rgb(1, 1, 1);
160  cairo->paint();
161  }
162  }
163  return cairo;
164  }
165 
166  virtual bool scale_override() { return true; }
167 
168  virtual void get_dimensions(double &width, double &height)
169  {
170  width = bbw * scale;
171  height = bbh * scale;
172  }
173 
174  virtual double get_scale() { return scale; }
175  virtual void set_scale(double scale) {};
176  virtual void set_translation(double tx, double ty) {};
177 
178  virtual void get_translation(double &tx, double &ty)
179  {
180  // no padding
181  tx = pad_x * scale;
182  ty = (bbh - pad_y) * scale;
183  }
184 
185  virtual void set_bb(double bbw, double bbh)
186  {
187  this->bbw = bbw;
188  this->bbh = bbh;
189 
190  if ( bbw * scale > maxwidth ) {
191  postproc_required = (maxwidth != 0);
192  maxwidth = bbw * scale;
193  }
194  if ( bbh * scale > maxheight * scale ) {
195  postproc_required = (maxheight != 0);
196  maxheight = bbh * scale;
197  }
198  }
199 
200  virtual void set_pad(double pad_x, double pad_y)
201  {
202  this->pad_x = pad_x;
203  this->pad_y = pad_y;
204  }
205 
206 
207  virtual void get_pad(double &pad_x, double &pad_y)
208  {
209  pad_x = 0;
210  pad_y = 0;
211  }
212 
213  /** Render graph. */
214  void render()
215  {
216 
217  FILE *f = fopen(infile.c_str(), "r");
218  Agraph_t *g = agread(f, NULL);
219  if (g) {
220  gvLayout(gvc, g, (char *)"dot");
221  gvRender(gvc, g, (char *)"skillguicairo", NULL);
222  gvFreeLayout(gvc, g);
223  agclose(g);
224  }
225  fclose(f);
226 
227  if (write_to_png) {
228  surface->write_to_png(outfile);
229  }
230 
231  cairo.clear();
232  surface.clear();
233  }
234 
235  /** Run the renderer. */
236  void run()
237  {
238  struct dirent *d;
239 
240  while ((d = readdir(directory)) != NULL) {
241  if (fnmatch("*.dot", d->d_name, FNM_PATHNAME | FNM_PERIOD) == 0) {
242  char infile_real[PATH_MAX];
243  infile = indir + "/" + d->d_name;
244  if (realpath(infile.c_str(), infile_real)) {
245  infile = infile_real;
246  }
247  char *basefile = strdup(infile.c_str());
248  std::string basen = basename(basefile);
249  free(basefile);
250  outfile = outdir + "/" + basen.substr(0, basen.length() - 3) + format;
251  printf("Converting %s to %s\n", infile.c_str(), outfile.c_str());
252  render();
253  } else {
254  printf("%s does not match pattern\n", d->d_name);
255  }
256  }
257 
258  if (do_postproc && postproc_required) {
259  postprocess();
260  }
261  }
262 
263  /** Write function for Cairo.
264  * @param closure contains the file handle
265  * @param data data to write
266  * @param length length of data
267  * @return Cairo status
268  */
269  static cairo_status_t write_func(void *closure,
270  const unsigned char *data, unsigned int length)
271  {
272  FILE *f = (FILE *)closure;
273  if (fwrite(data, length, 1, f)) {
274  return CAIRO_STATUS_SUCCESS;
275  } else {
276  return CAIRO_STATUS_WRITE_ERROR;
277  }
278  }
279 
280  /** Post-process files. Only valid for PNGs. */
281  void postprocess()
282  {
283  printf("Post-processing PNG files, resizing to %fx%f\n", maxwidth, maxheight);
284  struct dirent *d;
285  DIR *output_dir = opendir(outdir.c_str());
286  while ((d = readdir(output_dir)) != NULL) {
287  if (fnmatch("*.png", d->d_name, FNM_PATHNAME | FNM_PERIOD) == 0) {
288  infile = outdir + "/" + d->d_name;
289  Cairo::RefPtr<Cairo::ImageSurface> imgs = Cairo::ImageSurface::create_from_png(infile);
290  if ( (imgs->get_height() != maxheight) || (imgs->get_width() != maxwidth)) {
291  // need to re-create
292  char *tmpout = strdup((outdir + "/tmpXXXXXX").c_str());
293  FILE *f = fdopen(mkstemp(tmpout), "w");
294  outfile = tmpout;
295  free(tmpout);
296 
297  Cairo::RefPtr<Cairo::ImageSurface> outs = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32,
298  (int)ceilf(maxwidth),
299  (int)ceilf(maxheight));
300  double tx = (maxwidth - imgs->get_width()) / 2.0;
301  double ty = (maxheight - imgs->get_height()) / 2.0;
302  printf("Re-creating %s for post-processing, "
303  "resizing from %ix%i, tx=%f, ty=%f\n", infile.c_str(),
304  imgs->get_width(), imgs->get_height(), tx, ty);
305  Cairo::RefPtr<Cairo::Context> cc = Cairo::Context::create(outs);
306  if (white_bg) {
307  cc->set_source_rgb(1, 1, 1);
308  cc->paint();
309  }
310  cc->set_source(imgs, tx, ty);
311  cc->paint();
312  outs->write_to_png(&SkillGuiBatchRenderer::write_func, f);
313  imgs.clear();
314  cc.clear();
315  outs.clear();
316  fclose(f);
317  rename(outfile.c_str(), infile.c_str());
318  }
319  }
320  }
321  closedir(output_dir);
322  }
323 
324  private:
325  GVC_t *gvc;
326  ArgumentParser argp;
327  std::string format;
328  Cairo::RefPtr<Cairo::Surface> surface;
329  Cairo::RefPtr<Cairo::Context> cairo;
330  bool write_to_png;
331  bool white_bg;
332  double bbw, bbh;
333  double pad_x, pad_y;
334  std::string infile;
335  std::string outfile;
336  std::string indir;
337  std::string outdir;
338  DIR *directory;
339  double maxwidth, maxheight;
340  bool postproc_required;
341  bool do_postproc;
342  double scale;
343 };
344 
345 /** This is the main program of the Skill GUI.
346  */
347 int
348 main(int argc, char **argv)
349 {
350  SkillGuiBatchRenderer renderer(argc, argv);
351  renderer.run();
352  return 0;
353 }