064959a0efd73cf17f6063bf9945474e39de205c
[synfig.git] / synfig-core / src / tool / main.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file tool/main.cpp
3 **      \brief SYNFIG Tool
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <iostream>
34 #include <ETL/stringf>
35 #include <list>
36 #include <ETL/clock>
37 #include <algorithm>
38 #include <cstring>
39
40 #include <synfig/loadcanvas.h>
41 #include <synfig/savecanvas.h>
42 #include <synfig/target_scanline.h>
43 #include <synfig/module.h>
44 #include <synfig/importer.h>
45 #include <synfig/layer.h>
46 #include <synfig/canvas.h>
47 #include <synfig/target.h>
48 #include <synfig/time.h>
49 #include <synfig/string.h>
50 #include <synfig/paramdesc.h>
51 #include <synfig/main.h>
52 #include <synfig/guid.h>
53 #include <autorevision.h>
54 #include "definitions.h"
55 #include "progress.h"
56 #include "renderprogress.h"
57 #include "job.h"
58 #endif
59
60 using namespace std;
61 using namespace etl;
62 using namespace synfig;
63
64 /* === G L O B A L S ======================================================= */
65
66 const char *progname;
67 int verbosity=0;
68 bool be_quiet=false;
69 bool print_benchmarks=false;
70
71 /* === T Y P E D E F S ===================================================== */
72
73 typedef list<String> arg_list_t;
74 typedef list<Job> job_list_t;
75
76 /* === M E T H O D S ======================================================= */
77
78 void guid_test()
79 {
80         cout<<"GUID Test"<<endl;
81         for(int i=20;i;i--)
82                 cout<<synfig::GUID().get_string()<<' '<<synfig::GUID().get_string()<<endl;
83 }
84
85 void signal_test_func()
86 {
87         cout<<"**SIGNAL CALLED**"<<endl;
88 }
89
90 void signal_test()
91 {
92         sigc::signal<void> sig;
93         sigc::connection conn;
94         cout<<"Signal Test"<<endl;
95         conn=sig.connect(sigc::ptr_fun(signal_test_func));
96         cout<<"Next line should exclaim signal called."<<endl;
97         sig();
98         conn.disconnect();
99         cout<<"Next line should NOT exclaim signal called."<<endl;
100         sig();
101         cout<<"done."<<endl;
102 }
103
104 /* === P R O C E D U R E S ================================================= */
105
106 void display_help_option(const char *flag, const char *arg, string description)
107 {
108         const char spaces[]="                      ";
109         if(arg)
110                 cerr << strprintf(" %s %s %s", flag, arg, spaces+strlen(arg)+strlen(flag)+1)+description << endl;
111         else
112                 cerr << strprintf(" %s %s", flag, spaces+strlen(flag))+description << endl;
113 }
114
115 void display_help(bool full)
116 {
117         cerr << endl << _("syntax: ") << progname << " [DEFAULT OPTIONS] ([SIF FILE] [SPECIFIC OPTIONS])..." << endl << endl;
118
119         if(full)
120         {
121                 display_help_option("-t", "<output type>", _("Specify output target (Default:unknown)"));
122                 display_help_option("-w", "<pixel width>", _("Set the image width (Use zero for file default)"));
123                 display_help_option("-h", "<pixel height>", _("Set the image height (Use zero for file default)"));
124                 display_help_option("-s", "<image dist>", _("Set the diagonal size of image window (Span)"));
125                 display_help_option("-a", "<1...30>", _("Set antialias amount for parametric renderer."));
126                 display_help_option("-Q", "<0...10>", strprintf(_("Specify image quality for accelerated renderer (default=%d)"), DEFAULT_QUALITY).c_str());
127                 display_help_option("-g", "<amount>", _("Gamma (default=2.2)"));
128                 display_help_option("-v", NULL, _("Verbose Output (add more for more verbosity)"));
129                 display_help_option("-q", NULL, _("Quiet mode (No progress/time-remaining display)"));
130                 display_help_option("-c", "<canvas id>", _("Render the canvas with the given id instead of the root."));
131                 display_help_option("-o", "<output file>", _("Specify output filename"));
132                 display_help_option("-T", "<# of threads>", _("Enable multithreaded renderer using specified # of threads"));
133                 display_help_option("-b", NULL, _("Print Benchmarks"));
134                 display_help_option("--fps", "<framerate>", _("Set the frame rate"));
135                 display_help_option("--time", "<time>", _("Render a single frame at <seconds>"));
136                 display_help_option("--begin-time", "<time>", _("Set the starting time"));
137                 display_help_option("--start-time", "<time>", _("Set the starting time"));
138                 display_help_option("--end-time", "<time>", _("Set the ending time"));
139                 display_help_option("--dpi", "<res>", _("Set the physical resolution (dots-per-inch)"));
140                 display_help_option("--dpi-x", "<res>", _("Set the physical X resolution (dots-per-inch)"));
141                 display_help_option("--dpi-y", "<res>", _("Set the physical Y resolution (dots-per-inch)"));
142
143                 display_help_option("--list-canvases", NULL, _("List the exported canvases in the composition"));
144                 display_help_option("--canvas-info", "<fields>", _("Print out specified details of the root canvas"));
145                 display_help_option("--append", "<filename>", _("Append layers in <filename> to composition"));
146
147                 display_help_option("--layer-info", "<layer>", _("Print out layer's description, parameter info, etc."));
148                 display_help_option("--layers", NULL, _("Print out the list of available layers"));
149                 display_help_option("--targets", NULL, _("Print out the list of available targets"));
150                 display_help_option("--importers", NULL, _("Print out the list of available importers"));
151                 display_help_option("--valuenodes", NULL, _("Print out the list of available ValueNodes"));
152                 display_help_option("--modules", NULL, _("Print out the list of loaded modules"));
153                 display_help_option("--version", NULL, _("Print out version information"));
154                 display_help_option("--info", NULL, _("Print out misc build information"));
155                 display_help_option("--license", NULL, _("Print out license information"));
156
157 #ifdef _DEBUG
158                 display_help_option("--guid-test", NULL, _("Test GUID generation"));
159                 display_help_option("--signal-test", NULL, _("Test signal implementation"));
160 #endif
161         }
162         else
163                 display_help_option("--help", NULL, _("Print out usage and syntax info"));
164
165         cerr << endl;
166 }
167
168 int process_global_flags(arg_list_t &arg_list)
169 {
170         arg_list_t::iterator iter, next;
171
172         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
173         {
174                 if(*iter == "--")
175                         return SYNFIGTOOL_OK;
176
177                 if(*iter == "--signal-test")
178                 {
179                         signal_test();
180                         return SYNFIGTOOL_HELP;
181                 }
182
183                 if(*iter == "--guid-test")
184                 {
185                         guid_test();
186                         return SYNFIGTOOL_HELP;
187                 }
188
189                 if(*iter == "--help")
190                 {
191                         display_help(true);
192                         return SYNFIGTOOL_HELP;
193                 }
194
195                 if(*iter == "--info")
196                 {
197                         cout<<PACKAGE"-"VERSION<<endl;
198                         #ifdef DEVEL_VERSION
199                                 cout<<endl<<DEVEL_VERSION<<endl<<endl;
200                         #endif
201                         cout<<"Compiled on "__DATE__ /* " at "__TIME__ */;
202 #ifdef __GNUC__
203                         cout<<" with GCC "<<__VERSION__;
204 #endif
205 #ifdef _MSC_VER
206                         cout<<" with Microsoft Visual C++ "<<(_MSC_VER>>8)<<'.'<<(_MSC_VER&255);
207 #endif
208 #ifdef __TCPLUSPLUS__
209                         cout<<" with Borland Turbo C++ "<<(__TCPLUSPLUS__>>8)<<'.'<<((__TCPLUSPLUS__&255)>>4)<<'.'<<(__TCPLUSPLUS__&15);
210 #endif
211                         cout<<endl<<SYNFIG_COPYRIGHT<<endl;
212                         cout<<endl;
213                         return SYNFIGTOOL_HELP;
214                 }
215
216                 if(*iter == "--layers")
217                 {
218                         Progress p(PACKAGE);
219                         synfig::Main synfig_main(dirname(progname),&p);
220                         synfig::Layer::Book::iterator iter=synfig::Layer::book().begin();
221                         for(;iter!=synfig::Layer::book().end();iter++)
222                                 if (iter->second.category != CATEGORY_DO_NOT_USE)
223                                         cout<<iter->first<<endl;
224
225                         return SYNFIGTOOL_HELP;
226                 }
227
228                 if(*iter == "--layer-info")
229                 {
230                         Progress p(PACKAGE);
231                         synfig::Main synfig_main(dirname(progname),&p);
232                         iter=next++;
233                         if (iter==arg_list.end())
234                         {
235                                 error("The `%s' flag requires a value.  Use --help for a list of options.", "--layer-info");
236                                 return SYNFIGTOOL_MISSINGARGUMENT;
237                         }
238                         Layer::Handle layer=synfig::Layer::create(*iter);
239                         cout<<"Layer Name: "<<layer->get_name()<<endl;
240                         cout<<"Localized Layer Name: "<<layer->get_local_name()<<endl;
241                         cout<<"Version: "<<layer->get_version()<<endl;
242                         Layer::Vocab vocab=layer->get_param_vocab();
243                         for(;!vocab.empty();vocab.pop_front())
244                         {
245                                 cout<<"param - "<<vocab.front().get_name();
246                                 if(!vocab.front().get_critical())
247                                         cout<<" (not critical)";
248                                 cout<<endl<<"\tLocalized Name: "<<vocab.front().get_local_name()<<endl;
249                                 if(!vocab.front().get_description().empty())
250                                         cout<<"\tDescription: "<<vocab.front().get_description()<<endl;
251                                 if(!vocab.front().get_hint().empty())
252                                         cout<<"\tHint: "<<vocab.front().get_hint()<<endl;
253                         }
254
255                         return SYNFIGTOOL_HELP;
256                 }
257
258                 if(*iter == "--modules")
259                 {
260                         Progress p(PACKAGE);
261                         synfig::Main synfig_main(dirname(progname),&p);
262                         synfig::Module::Book::iterator iter=synfig::Module::book().begin();
263                         for(;iter!=synfig::Module::book().end();iter++)
264                                 cout<<iter->first<<endl;
265                         return SYNFIGTOOL_HELP;
266                 }
267
268                 if(*iter == "--targets")
269                 {
270                         Progress p(PACKAGE);
271                         synfig::Main synfig_main(dirname(progname),&p);
272                         synfig::Target::Book::iterator iter=synfig::Target::book().begin();
273                         for(;iter!=synfig::Target::book().end();iter++)
274                                 cout<<iter->first<<endl;
275                         return SYNFIGTOOL_HELP;
276                 }
277
278                 if(*iter == "--valuenodes")
279                 {
280                         Progress p(PACKAGE);
281                         synfig::Main synfig_main(dirname(progname),&p);
282                         synfig::LinkableValueNode::Book::iterator iter=synfig::LinkableValueNode::book().begin();
283                         for(;iter!=synfig::LinkableValueNode::book().end();iter++)
284                                 cout<<iter->first<<endl;
285                         return SYNFIGTOOL_HELP;
286                 }
287
288                 if(*iter == "--importers")
289                 {
290                         Progress p(PACKAGE);
291                         synfig::Main synfig_main(dirname(progname),&p);
292                         synfig::Importer::Book::iterator iter=synfig::Importer::book().begin();
293                         for(;iter!=synfig::Importer::book().end();iter++)
294                                 cout<<iter->first<<endl;
295                         return SYNFIGTOOL_HELP;
296                 }
297
298                 if(*iter == "--version")
299                 {
300                         cerr<<PACKAGE<<" "<<VERSION<<endl;
301                         arg_list.erase(iter);
302                         return SYNFIGTOOL_HELP;
303                 }
304
305                 if(*iter == "--license")
306                 {
307                         cerr<<PACKAGE<<" "<<VERSION<<endl;
308                         cout<<SYNFIG_COPYRIGHT<<endl<<endl;
309                         cerr<<"\
310 **      This package is free software; you can redistribute it and/or\n\
311 **      modify it under the terms of the GNU General Public License as\n\
312 **      published by the Free Software Foundation; either version 2 of\n\
313 **      the License, or (at your option) any later version.\n\
314 **\n\
315 **      " << endl << endl;
316                         arg_list.erase(iter);
317                         return SYNFIGTOOL_HELP;
318                 }
319
320                 if(*iter == "-v")
321                 {
322                         verbosity++;
323                         arg_list.erase(iter);
324                         continue;
325                 }
326
327                 if(*iter == "-q")
328                 {
329                         be_quiet=true;
330                         arg_list.erase(iter);
331                         continue;
332                 }
333                 if(*iter == "-b")
334                 {
335                         print_benchmarks=true;
336                         arg_list.erase(iter);
337                         continue;
338                 }
339         }
340
341         return SYNFIGTOOL_OK;
342 }
343
344 /* true if the given flag takes an extra parameter */
345 bool flag_requires_value(String flag)
346 {
347         return (flag=="-a"                      || flag=="-c"                   || flag=="-g"                   || flag=="-h"                   || flag=="-o"                   ||
348                         flag=="-Q"                      || flag=="-s"                   || flag=="-t"                   || flag=="-T"                   || flag=="-w"                   ||
349                         flag=="--append"        || flag=="--begin-time" || flag=="--canvas-info"|| flag=="--dpi"                || flag=="--dpi-x"              ||
350                         flag=="--dpi-y"         || flag=="--end-time"   || flag=="--fps"                || flag=="--layer-info" || flag=="--start-time" ||
351                         flag=="--time"          );
352 }
353
354 int extract_arg_cluster(arg_list_t &arg_list,arg_list_t &cluster)
355 {
356         arg_list_t::iterator iter, next;
357
358         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
359         {
360                 if(*iter->begin() != '-')
361                 {
362                         //cerr<<*iter->begin()<<"-----------"<<endl;
363                         return SYNFIGTOOL_OK;
364                 }
365
366                 if (flag_requires_value(*iter))
367                 {
368                         cluster.push_back(*iter);
369                         arg_list.erase(iter);
370                         iter=next++;
371                         if (iter==arg_list.end())
372                         {
373                                 error("The `%s' flag requires a value.  Use --help for a list of options.", cluster.back().c_str());
374                                 return SYNFIGTOOL_MISSINGARGUMENT;
375                         }
376                 }
377
378                 cluster.push_back(*iter);
379                 arg_list.erase(iter);
380         }
381
382         return SYNFIGTOOL_OK;
383 }
384
385 int extract_RendDesc(arg_list_t &arg_list,RendDesc &desc)
386 {
387         arg_list_t::iterator iter, next;
388         int w=0,h=0;
389         float span=0;
390         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
391         {
392                 if(*iter=="-w")
393                 {
394                         arg_list.erase(iter);
395                         iter=next++;
396                         w=atoi(iter->c_str());
397                         arg_list.erase(iter);
398                 }
399                 else if(*iter=="-h")
400                 {
401                         arg_list.erase(iter);
402                         iter=next++;
403                         h=atoi(iter->c_str());
404                         arg_list.erase(iter);
405                 }
406                 else if(*iter=="-a")
407                 {
408             int a;
409                         arg_list.erase(iter);
410                         iter=next++;
411                         a=atoi(iter->c_str());
412                         desc.set_antialias(a);
413                         VERBOSE_OUT(1)<<strprintf(_("Antialiasing set to %d, (%d samples per pixel)"),a,a*a)<<endl;
414                         arg_list.erase(iter);
415                 }
416                 else if(*iter=="-s")
417                 {
418                         arg_list.erase(iter);
419                         iter=next++;
420                         span=atof(iter->c_str());
421                         VERBOSE_OUT(1)<<strprintf(_("Span set to %d units"),span)<<endl;
422                         arg_list.erase(iter);
423                 }
424                 else if(*iter=="--fps")
425                 {
426                         arg_list.erase(iter);
427                         iter=next++;
428                         float fps=atof(iter->c_str());
429                         desc.set_frame_rate(fps);
430                         arg_list.erase(iter);
431                         VERBOSE_OUT(1)<<strprintf(_("Frame rate set to %d frames per second"),fps)<<endl;
432                 }
433                 else if(*iter=="--dpi")
434                 {
435                         arg_list.erase(iter);
436                         iter=next++;
437                         float dpi=atof(iter->c_str());
438                         float dots_per_meter=dpi*39.3700787402;
439                         desc.set_x_res(dots_per_meter).set_y_res(dots_per_meter);
440                         arg_list.erase(iter);
441                         VERBOSE_OUT(1)<<strprintf(_("Physical resolution set to %f dpi"),dpi)<<endl;
442                 }
443                 else if(*iter=="--dpi-x")
444                 {
445                         arg_list.erase(iter);
446                         iter=next++;
447                         float dpi=atof(iter->c_str());
448                         float dots_per_meter=dpi*39.3700787402;
449                         desc.set_x_res(dots_per_meter);
450                         arg_list.erase(iter);
451                         VERBOSE_OUT(1)<<strprintf(_("Physical X resolution set to %f dpi"),dpi)<<endl;
452                 }
453                 else if(*iter=="--dpi-y")
454                 {
455                         arg_list.erase(iter);
456                         iter=next++;
457                         float dpi=atof(iter->c_str());
458                         float dots_per_meter=dpi*39.3700787402;
459                         desc.set_y_res(dots_per_meter);
460                         arg_list.erase(iter);
461                         VERBOSE_OUT(1)<<strprintf(_("Physical Y resolution set to %f dpi"),dpi)<<endl;
462                 }
463                 else if(*iter=="--start-time" || *iter=="--begin-time")
464                 {
465                         arg_list.erase(iter);
466                         iter=next++;
467                         desc.set_time_start(Time(*iter,desc.get_frame_rate()));
468                         arg_list.erase(iter);
469                 }
470                 else if(*iter=="--end-time")
471                 {
472                         arg_list.erase(iter);
473                         iter=next++;
474                         desc.set_time_end(Time(*iter,desc.get_frame_rate()));
475                         arg_list.erase(iter);
476                 }
477                 else if(*iter=="--time")
478                 {
479                         arg_list.erase(iter);
480                         iter=next++;
481                         desc.set_time(Time(*iter,desc.get_frame_rate()));
482                         VERBOSE_OUT(1)<<_("Rendering frame at ")<<desc.get_time_start().get_string(desc.get_frame_rate())<<endl;
483                         arg_list.erase(iter);
484                 }
485                 else if(*iter=="-g")
486                 {
487                         synfig::warning("Gamma argument is currently ignored");
488                         arg_list.erase(iter);
489                         iter=next++;
490                         //desc.set_gamma(Gamma(atof(iter->c_str())));
491                         arg_list.erase(iter);
492                 }
493                 else if (flag_requires_value(*iter))
494                         iter=next++;
495         }
496         if (w||h)
497         {
498                 if (!w)
499                         w = desc.get_w() * h / desc.get_h();
500                 else if (!h)
501                         h = desc.get_h() * w / desc.get_w();
502
503                 desc.set_wh(w,h);
504                 VERBOSE_OUT(1)<<strprintf(_("Resolution set to %dx%d"),w,h)<<endl;
505         }
506         if(span)
507                 desc.set_span(span);
508         return SYNFIGTOOL_OK;
509 }
510
511 int extract_quality(arg_list_t &arg_list,int &quality)
512 {
513         arg_list_t::iterator iter, next;
514         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
515         {
516                 if(*iter=="-Q")
517                 {
518                         arg_list.erase(iter);
519                         iter=next++;
520                         quality=atoi(iter->c_str());
521                         VERBOSE_OUT(1)<<strprintf(_("Quality set to %d"),quality)<<endl;
522                         arg_list.erase(iter);
523                 }
524                 else if (flag_requires_value(*iter))
525                         iter=next++;
526         }
527
528         return SYNFIGTOOL_OK;
529 }
530
531 int extract_threads(arg_list_t &arg_list,int &threads)
532 {
533         arg_list_t::iterator iter, next;
534         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
535         {
536                 if(*iter=="-T")
537                 {
538                         arg_list.erase(iter);
539                         iter=next++;
540                         threads=atoi(iter->c_str());
541                         VERBOSE_OUT(1)<<strprintf(_("Threads set to %d"),threads)<<endl;
542                         arg_list.erase(iter);
543                 }
544                 else if (flag_requires_value(*iter))
545                         iter=next++;
546         }
547
548         return SYNFIGTOOL_OK;
549 }
550
551 int extract_target(arg_list_t &arg_list,string &type)
552 {
553         arg_list_t::iterator iter, next;
554         type.clear();
555
556         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
557         {
558                 if(*iter=="-t")
559                 {
560                         arg_list.erase(iter);
561                         iter=next++;
562                         type=*iter;
563                         arg_list.erase(iter);
564                 }
565                 else if (flag_requires_value(*iter))
566                         iter=next++;
567         }
568
569         return SYNFIGTOOL_OK;
570 }
571
572 int extract_append(arg_list_t &arg_list,string &filename)
573 {
574         arg_list_t::iterator iter, next;
575         filename.clear();
576
577         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
578         {
579                 if(*iter=="--append")
580                 {
581                         arg_list.erase(iter);
582                         iter=next++;
583                         filename=*iter;
584                         arg_list.erase(iter);
585                 }
586                 else if (flag_requires_value(*iter))
587                         iter=next++;
588         }
589
590         return SYNFIGTOOL_OK;
591 }
592
593 int extract_outfile(arg_list_t &arg_list,string &outfile)
594 {
595         arg_list_t::iterator iter, next;
596         int ret=SYNFIGTOOL_FILENOTFOUND;
597         outfile.clear();
598
599         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
600         {
601                 if(*iter=="-o")
602                 {
603                         arg_list.erase(iter);
604                         iter=next++;
605                         outfile=*iter;
606                         arg_list.erase(iter);
607                         ret=SYNFIGTOOL_OK;
608                 }
609                 else if (flag_requires_value(*iter))
610                         iter=next++;
611         }
612
613         return ret;
614 }
615
616 int extract_canvasid(arg_list_t &arg_list,string &canvasid)
617 {
618         arg_list_t::iterator iter, next;
619         //canvasid.clear();
620
621         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
622         {
623                 if(*iter=="-c")
624                 {
625                         arg_list.erase(iter);
626                         iter=next++;
627                         canvasid=*iter;
628                         arg_list.erase(iter);
629                 }
630                 else if (flag_requires_value(*iter))
631                         iter=next++;
632         }
633
634         return SYNFIGTOOL_OK;
635 }
636
637 int extract_list_canvases(arg_list_t &arg_list,bool &list_canvases)
638 {
639         arg_list_t::iterator iter, next;
640
641         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
642                 if(*iter=="--list-canvases")
643                 {
644                         list_canvases = true;
645                         arg_list.erase(iter);
646                 }
647
648         return SYNFIGTOOL_OK;
649 }
650
651 void extract_canvas_info(arg_list_t &arg_list, Job &job)
652 {
653         arg_list_t::iterator iter, next;
654
655         for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
656                 if(*iter=="--canvas-info")
657                 {
658                         job.canvas_info = true;
659                         arg_list.erase(iter);
660                         iter=next++;
661                         String values(*iter), value;
662                         arg_list.erase(iter);
663
664                         std::string::size_type pos;
665                         while (!values.empty())
666                         {
667                                 pos = values.find_first_of(',');
668                                 if (pos == std::string::npos)
669                                 {
670                                         value = values;
671                                         values = "";
672                                 }
673                                 else
674                                 {
675                                         value = values.substr(0, pos);
676                                         values = values.substr(pos+1);
677                                 }
678                                 if (value == "all")
679                                 {
680                                         job.canvas_info_all = true;
681                                         return;
682                                 }
683
684                                 if (value == "time_start")                      job.canvas_info_time_start              = true;
685                                 else if (value == "time_end")           job.canvas_info_time_end                = true;
686                                 else if (value == "frame_rate")         job.canvas_info_frame_rate              = true;
687                                 else if (value == "frame_start")        job.canvas_info_frame_start             = true;
688                                 else if (value == "frame_end")          job.canvas_info_frame_end               = true;
689                                 else if (value == "w")                          job.canvas_info_w                               = true;
690                                 else if (value == "h")                          job.canvas_info_h                               = true;
691                                 else if (value == "image_aspect")       job.canvas_info_image_aspect    = true;
692                                 else if (value == "pw")                         job.canvas_info_pw                              = true;
693                                 else if (value == "ph")                         job.canvas_info_ph                              = true;
694                                 else if (value == "pixel_aspect")       job.canvas_info_pixel_aspect    = true;
695                                 else if (value == "tl")                         job.canvas_info_tl                              = true;
696                                 else if (value == "br")                         job.canvas_info_br                              = true;
697                                 else if (value == "physical_w")         job.canvas_info_physical_w              = true;
698                                 else if (value == "physical_h")         job.canvas_info_physical_h              = true;
699                                 else if (value == "x_res")                      job.canvas_info_x_res                   = true;
700                                 else if (value == "y_res")                      job.canvas_info_y_res                   = true;
701                                 else if (value == "span")                       job.canvas_info_span                    = true;
702                                 else if (value == "interlaced")         job.canvas_info_interlaced              = true;
703                                 else if (value == "antialias")          job.canvas_info_antialias               = true;
704                                 else if (value == "clamp")                      job.canvas_info_clamp                   = true;
705                                 else if (value == "flags")                      job.canvas_info_flags                   = true;
706                                 else if (value == "focus")                      job.canvas_info_focus                   = true;
707                                 else if (value == "bg_color")           job.canvas_info_bg_color                = true;
708                                 else if (value == "metadata")           job.canvas_info_metadata                = true;
709                                 else
710                                 {
711                                         cerr<<_("Unrecognised canvas variable: ") << "'" << value << "'" << endl;
712                                         cerr<<_("Recognized variables are:") << endl <<
713                                                 "  all, time_start, time_end, frame_rate, frame_start, frame_end, w, h," << endl <<
714                                                 "  image_aspect, pw, ph, pixel_aspect, tl, br, physical_w, physical_h," << endl <<
715                                                 "  x_res, y_res, span, interlaced, antialias, clamp, flags," << endl <<
716                                                 "  focus, bg_color, metadata" << endl;
717                                 }
718
719                                 if (pos == std::string::npos)
720                                         break;
721                         };
722                 }
723 }
724
725 void list_child_canvases(string prefix, Canvas::Handle canvas)
726 {
727         Canvas::Children children(canvas->children());
728         for (Canvas::Children::iterator iter = children.begin(); iter != children.end(); iter++)
729         {
730                 cout << prefix << ":" << (*iter)->get_id() << endl;
731                 list_child_canvases(prefix + ":" + (*iter)->get_id(), *iter);
732         }
733 }
734
735 void list_canvas_info(Job job)
736 {
737         Canvas::Handle canvas(job.canvas);
738         const RendDesc &rend_desc(canvas->rend_desc());
739
740         if (job.canvas_info_all || job.canvas_info_time_start)
741         {
742                 cout << endl << "# " << _("Start Time") << endl;
743                 cout << "time_start"    << "=" << rend_desc.get_time_start().get_string().c_str() << endl;
744         }
745
746         if (job.canvas_info_all || job.canvas_info_time_end)
747         {
748                 cout << endl << "# " << _("End Time") << endl;
749                 cout << "time_end"              << "=" << rend_desc.get_time_end().get_string().c_str() << endl;
750         }
751
752         if (job.canvas_info_all || job.canvas_info_frame_rate)
753         {
754                 cout << endl << "# " << _("Frame Rate") << endl;
755                 cout << "frame_rate"    << "=" << rend_desc.get_frame_rate() << endl;
756         }
757
758         if (job.canvas_info_all || job.canvas_info_frame_start)
759         {
760                 cout << endl << "# " << _("Start Frame") << endl;
761                 cout << "frame_start"   << "=" << rend_desc.get_frame_start() << endl;
762         }
763
764         if (job.canvas_info_all || job.canvas_info_frame_end)
765         {
766                 cout << endl << "# " << _("End Frame") << endl;
767                 cout << "frame_end"             << "=" << rend_desc.get_frame_end() << endl;
768         }
769
770         if (job.canvas_info_all)
771                 cout << endl;
772
773         if (job.canvas_info_all || job.canvas_info_w)
774         {
775                 cout << endl << "# " << _("Width") << endl;
776                 cout << "w"                             << "=" << rend_desc.get_w() << endl;
777         }
778
779         if (job.canvas_info_all || job.canvas_info_h)
780         {
781                 cout << endl << "# " << _("Height") << endl;
782                 cout << "h"                             << "=" << rend_desc.get_h() << endl;
783         }
784
785         if (job.canvas_info_all || job.canvas_info_image_aspect)
786         {
787                 cout << endl << "# " << _("Image Aspect Ratio") << endl;
788                 cout << "image_aspect"  << "=" << rend_desc.get_image_aspect() << endl;
789         }
790
791         if (job.canvas_info_all)
792                 cout << endl;
793
794         if (job.canvas_info_all || job.canvas_info_pw)
795         {
796                 cout << endl << "# " << _("Pixel Width") << endl;
797                 cout << "pw"                    << "=" << rend_desc.get_pw() << endl;
798         }
799
800         if (job.canvas_info_all || job.canvas_info_ph)
801         {
802                 cout << endl << "# " << _("Pixel Height") << endl;
803                 cout << "ph"                    << "=" << rend_desc.get_ph() << endl;
804         }
805
806         if (job.canvas_info_all || job.canvas_info_pixel_aspect)
807         {
808                 cout << endl << "# " << _("Pixel Aspect Ratio") << endl;
809                 cout << "pixel_aspect"  << "=" << rend_desc.get_pixel_aspect() << endl;
810         }
811
812         if (job.canvas_info_all)
813                 cout << endl;
814
815         if (job.canvas_info_all || job.canvas_info_tl)
816         {
817                 cout << endl << "# " << _("Top Left") << endl;
818                 cout << "tl"                    << "=" << rend_desc.get_tl()[0]
819                          << " " << rend_desc.get_tl()[1] << endl;
820         }
821
822         if (job.canvas_info_all || job.canvas_info_br)
823         {
824                 cout << endl << "# " << _("Bottom Right") << endl;
825                 cout << "br"                    << "=" << rend_desc.get_br()[0]
826                          << " " << rend_desc.get_br()[1] << endl;
827         }
828
829         if (job.canvas_info_all || job.canvas_info_physical_w)
830         {
831                 cout << endl << "# " << _("Physical Width") << endl;
832                 cout << "physical_w"    << "=" << rend_desc.get_physical_w() << endl;
833         }
834
835         if (job.canvas_info_all || job.canvas_info_physical_h)
836         {
837                 cout << endl << "# " << _("Physical Height") << endl;
838                 cout << "physical_h"    << "=" << rend_desc.get_physical_h() << endl;
839         }
840
841         if (job.canvas_info_all || job.canvas_info_x_res)
842         {
843                 cout << endl << "# " << _("X Resolution") << endl;
844                 cout << "x_res"                 << "=" << rend_desc.get_x_res() << endl;
845         }
846
847         if (job.canvas_info_all || job.canvas_info_y_res)
848         {
849                 cout << endl << "# " << _("Y Resolution") << endl;
850                 cout << "y_res"                 << "=" << rend_desc.get_y_res() << endl;
851         }
852
853         if (job.canvas_info_all || job.canvas_info_span)
854         {
855                 cout << endl << "# " << _("Diagonal Image Span") << endl;
856                 cout << "span"                  << "=" << rend_desc.get_span() << endl;
857         }
858
859         if (job.canvas_info_all)
860                 cout << endl;
861
862         if (job.canvas_info_all || job.canvas_info_interlaced)
863         {
864                 cout << endl << "# " << _("Interlaced") << endl;
865                 cout << "interlaced"    << "=" << rend_desc.get_interlaced() << endl;
866         }
867
868         if (job.canvas_info_all || job.canvas_info_antialias)
869         {
870                 cout << endl << "# " << _("Antialias") << endl;
871                 cout << "antialias"             << "=" << rend_desc.get_antialias() << endl;
872         }
873
874         if (job.canvas_info_all || job.canvas_info_clamp)
875         {
876                 cout << endl << "# " << _("Clamp") << endl;
877                 cout << "clamp"                 << "=" << rend_desc.get_clamp() << endl;
878         }
879
880         if (job.canvas_info_all || job.canvas_info_flags)
881         {
882                 cout << endl << "# " << _("Flags") << endl;
883                 cout << "flags"                 << "=" << rend_desc.get_flags() << endl;
884         }
885
886         if (job.canvas_info_all || job.canvas_info_focus)
887         {
888                 cout << endl << "# " << _("Focus") << endl;
889                 cout << "focus"                 << "=" << rend_desc.get_focus()[0]
890                          << " " << rend_desc.get_focus()[1] << endl;
891         }
892
893         if (job.canvas_info_all || job.canvas_info_bg_color)
894         {
895                 cout << endl << "# " << _("Background Color") << endl;
896                 cout << "bg_color"              << "=" << rend_desc.get_bg_color().get_string().c_str() << endl;
897         }
898
899         if (job.canvas_info_all)
900                 cout << endl;
901
902         if (job.canvas_info_all || job.canvas_info_metadata)
903         {
904                 std::list<String> keys(canvas->get_meta_data_keys());
905                 cout << endl << "# " << _("Metadata") << endl;
906                 for (std::list<String>::iterator iter = keys.begin(); iter != keys.end(); iter++)
907                         cout << *iter << "=" << canvas->get_meta_data(*iter) << endl;
908         }
909 }
910
911 /* === M E T H O D S ======================================================= */
912
913 /* === E N T R Y P O I N T ================================================= */
914
915 int main(int argc, char *argv[])
916 {
917         int i;
918         arg_list_t arg_list;
919         job_list_t job_list;
920
921         setlocale(LC_ALL, "");
922
923 #ifdef ENABLE_NLS
924         bindtextdomain("synfig", LOCALEDIR);
925         bind_textdomain_codeset("synfig", "UTF-8");
926         textdomain("synfig");
927 #endif
928
929         progname=argv[0];
930         Progress p(argv[0]);
931
932         if(!SYNFIG_CHECK_VERSION())
933         {
934                 cerr<<_("FATAL: Synfig Version Mismatch")<<endl;
935                 return SYNFIGTOOL_BADVERSION;
936         }
937
938         if(argc==1)
939         {
940                 display_help(false);
941                 return SYNFIGTOOL_BLANK;
942         }
943
944         for(i=1;i<argc;i++)
945                 arg_list.push_back(argv[i]);
946
947         if((i=process_global_flags(arg_list)))
948                 return i;
949
950         VERBOSE_OUT(1)<<_("verbosity set to ")<<verbosity<<endl;
951         synfig::Main synfig_main(dirname(progname),&p);
952
953         {
954                 arg_list_t defaults, imageargs;
955                 int ret;
956
957                 // Grab the defaults before the first file
958                 if ((ret = extract_arg_cluster(arg_list,defaults)) != SYNFIGTOOL_OK)
959                   return ret;
960
961                 while(arg_list.size())
962                 {
963                         string target_name;
964                         job_list.push_front(Job());
965                         int threads=0;
966
967                         imageargs=defaults;
968                         job_list.front().filename=arg_list.front();
969                         arg_list.pop_front();
970
971                         if ((ret = extract_arg_cluster(arg_list,imageargs)) != SYNFIGTOOL_OK)
972                           return ret;
973
974                         // Open the composition
975                         String errors, warnings;
976                         try
977                         {
978                                 job_list.front().root=open_canvas(job_list.front().filename, errors, warnings);
979                         }
980                         catch(runtime_error x)
981                         {
982                                 job_list.front().root = 0;
983                         }
984
985                         if(!job_list.front().root)
986                         {
987                                 cerr<<_("Unable to load '")<<job_list.front().filename<<"'."<<endl;
988                                 cerr<<_("Throwing out job...")<<endl;
989                                 job_list.pop_front();
990                                 continue;
991                         }
992
993                         bool list_canvases = false;
994                         extract_list_canvases(imageargs, list_canvases);
995                         job_list.front().list_canvases = list_canvases;
996
997                         extract_canvas_info(imageargs, job_list.front());
998
999                         job_list.front().root->set_time(0);
1000
1001                         string canvasid;
1002                         extract_canvasid(imageargs,canvasid);
1003                         if(!canvasid.empty())
1004                         {
1005                                 try
1006                                 {
1007                                         String warnings;
1008                                         job_list.front().canvas=job_list.front().root->find_canvas(canvasid, warnings);
1009                                 }
1010                                 catch(Exception::IDNotFound)
1011                                 {
1012                                         cerr<<_("Unable to find canvas with ID \"")<<canvasid<<_("\" in ")<<job_list.front().filename<<"."<<endl;
1013                                         cerr<<_("Throwing out job...")<<endl;
1014                                         job_list.pop_front();
1015                                         continue;
1016
1017                                 }
1018                                 catch(Exception::BadLinkName)
1019                                 {
1020                                         cerr<<_("Invalid canvas name \"")<<canvasid<<_("\" in ")<<job_list.front().filename<<"."<<endl;
1021                                         cerr<<_("Throwing out job...")<<endl;
1022                                         job_list.pop_front();
1023                                         continue;
1024                                 }
1025                         }
1026                         else
1027                                 job_list.front().canvas=job_list.front().root;
1028
1029                         extract_RendDesc(imageargs,job_list.front().canvas->rend_desc());
1030                         extract_target(imageargs,target_name);
1031                         extract_threads(imageargs,threads);
1032                         job_list.front().quality=DEFAULT_QUALITY;
1033                         extract_quality(imageargs,job_list.front().quality);
1034                         VERBOSE_OUT(2)<<_("Quality set to ")<<job_list.front().quality<<endl;
1035                         job_list.front().desc=job_list.front().canvas->rend_desc();
1036                         extract_outfile(imageargs,job_list.front().outfilename);
1037
1038                         // Extract composite
1039                         do{
1040                                 string composite_file;
1041                                 extract_append(imageargs,composite_file);
1042                                 if(!composite_file.empty())
1043                                 {
1044                                         String errors, warnings;
1045                                         Canvas::Handle composite(open_canvas(composite_file, errors, warnings));
1046                                         if(!composite)
1047                                         {
1048                                                 cerr<<_("Unable to append '")<<composite_file<<"'."<<endl;
1049                                                 break;
1050                                         }
1051                                         Canvas::reverse_iterator iter;
1052                                         for(iter=composite->rbegin();iter!=composite->rend();++iter)
1053                                         {
1054                                                 Layer::Handle layer(*iter);
1055                                                 if(layer->active())
1056                                                         job_list.front().canvas->push_front(layer->clone());
1057                                         }
1058                                         VERBOSE_OUT(2)<<_("Appended contents of ")<<composite_file<<endl;
1059                                 }
1060                         } while(false);
1061
1062                         VERBOSE_OUT(4)<<_("Attempting to determine target/outfile...")<<endl;
1063
1064                         // If the target type is not yet defined,
1065                         // try to figure it out from the outfile.
1066                         if(target_name.empty() && !job_list.front().outfilename.empty())
1067                         {
1068                                 VERBOSE_OUT(3)<<_("Target name undefined, attempting to figure it out")<<endl;
1069                                 string ext = filename_extension(job_list.front().outfilename);
1070                                 if (ext.length()) ext = ext.substr(1);
1071                                 if(Target::ext_book().count(ext))
1072                                 {
1073                                         target_name=Target::ext_book()[ext];
1074                                         info("target name not specified - using %s", target_name.c_str());
1075                                 }
1076                                 else
1077                                 {
1078                                         string lower_ext(ext);
1079
1080                                         for(unsigned int i=0;i<ext.length();i++)
1081                                                 lower_ext[i] = tolower(ext[i]);
1082
1083                                         if(Target::ext_book().count(lower_ext))
1084                                         {
1085                                                 target_name=Target::ext_book()[lower_ext];
1086                                                 info("target name not specified - using %s", target_name.c_str());
1087                                         }
1088                                         else
1089                                                 target_name=ext;
1090                                 }
1091                         }
1092
1093                         // If the target type is STILL not yet defined, then
1094                         // set it to a some sort of default
1095                         if(target_name.empty())
1096                         {
1097                                 VERBOSE_OUT(2)<<_("Defaulting to PNG target...")<<endl;
1098                                 target_name="png";
1099                         }
1100
1101                         // If no output filename was provided, then
1102                         // create a output filename based on the
1103                         // given input filename. (ie: change the extension)
1104                         if(job_list.front().outfilename.empty())
1105                         {
1106                                 job_list.front().outfilename = filename_sans_extension(job_list.front().filename) + '.';
1107                                 if(Target::book().count(target_name))
1108                                         job_list.front().outfilename+=Target::book()[target_name].filename;
1109                                 else
1110                                         job_list.front().outfilename+=target_name;
1111                         }
1112
1113                         VERBOSE_OUT(4)<<"target_name="<<target_name<<endl;
1114                         VERBOSE_OUT(4)<<"outfile_name="<<job_list.front().outfilename<<endl;
1115
1116                         VERBOSE_OUT(4)<<_("Creating the target...")<<endl;
1117                         job_list.front().target=synfig::Target::create(target_name,job_list.front().outfilename);
1118
1119                         if(target_name=="sif")
1120                                 job_list.front().sifout=true;
1121                         else
1122                         {
1123                                 if(!job_list.front().target)
1124                                 {
1125                                         cerr<<_("Unknown target for ")<<job_list.front().filename<<": "<<target_name<<endl;
1126                                         cerr<<_("Throwing out job...")<<endl;
1127                                         job_list.pop_front();
1128                                         continue;
1129                                 }
1130                                 job_list.front().sifout=false;
1131                         }
1132
1133                         // Set the Canvas on the Target
1134                         if(job_list.front().target)
1135                         {
1136                                 VERBOSE_OUT(4)<<_("Setting the canvas on the target...")<<endl;
1137                                 job_list.front().target->set_canvas(job_list.front().canvas);
1138                                 VERBOSE_OUT(4)<<_("Setting the quality of the target...")<<endl;
1139                                 job_list.front().target->set_quality(job_list.front().quality);
1140                         }
1141
1142                         // Set the threads for the target
1143                         if(job_list.front().target && Target_Scanline::Handle::cast_dynamic(job_list.front().target))
1144                                 Target_Scanline::Handle::cast_dynamic(job_list.front().target)->set_threads(threads);
1145
1146                         if(imageargs.size())
1147                         {
1148                                 cerr<<_("Unidentified arguments for ")<<job_list.front().filename<<": ";
1149                                 for(;imageargs.size();imageargs.pop_front())
1150                                         cerr<<' '<<imageargs.front();
1151                                 cerr<<endl;
1152                                 cerr<<_("Throwing out job...")<<endl;
1153                                 job_list.pop_front();
1154                                 continue;
1155                         }
1156                 }
1157         }
1158
1159         if(arg_list.size())
1160         {
1161                 cerr<<_("Unidentified arguments:");
1162                 for(;arg_list.size();arg_list.pop_front())
1163                         cerr<<' '<<arg_list.front();
1164                 cerr<<endl;
1165                 return SYNFIGTOOL_UNKNOWNARGUMENT;
1166         }
1167
1168         if(!job_list.size())
1169         {
1170                 cerr<<_("Nothing to do!")<<endl;
1171                 return SYNFIGTOOL_BORED;
1172         }
1173
1174         for(;job_list.size();job_list.pop_front())
1175         {
1176                 VERBOSE_OUT(3)<<job_list.front().filename<<" -- "<<endl<<'\t'<<
1177                 strprintf("w:%d, h:%d, a:%d, pxaspect:%f, imaspect:%f, span:%f",
1178                         job_list.front().desc.get_w(),
1179                         job_list.front().desc.get_h(),
1180                         job_list.front().desc.get_antialias(),
1181                         job_list.front().desc.get_pixel_aspect(),
1182                         job_list.front().desc.get_image_aspect(),
1183                         job_list.front().desc.get_span()
1184                         )<<endl<<'\t'<<
1185                 strprintf("tl:[%f,%f], br:[%f,%f], focus:[%f,%f]",
1186                         job_list.front().desc.get_tl()[0],job_list.front().desc.get_tl()[1],
1187                         job_list.front().desc.get_br()[0],job_list.front().desc.get_br()[1],
1188                         job_list.front().desc.get_focus()[0],job_list.front().desc.get_focus()[1]
1189                         )<<endl;
1190
1191                 RenderProgress p;
1192                 p.task(job_list.front().filename+" ==> "+job_list.front().outfilename);
1193                 if(job_list.front().sifout)
1194                 {
1195                         if(!save_canvas(job_list.front().outfilename,job_list.front().canvas))
1196                         {
1197                                 cerr<<"Render Failure."<<endl;
1198                                 return SYNFIGTOOL_RENDERFAILURE;
1199                         }
1200                 }
1201                 else if (job_list.front().list_canvases)
1202                 {
1203                         list_child_canvases(job_list.front().filename + "#", job_list.front().canvas);
1204                         cerr << endl;
1205                 }
1206                 else if (job_list.front().canvas_info)
1207                 {
1208                         list_canvas_info(job_list.front());
1209                         cerr << endl;
1210                 }
1211                 else
1212                 {
1213                         VERBOSE_OUT(1)<<_("Rendering...")<<endl;
1214                         etl::clock timer;
1215                         timer.reset();
1216                         // Call the render member of the target
1217                         if(!job_list.front().target->render(&p))
1218                         {
1219                                 cerr<<"Render Failure."<<endl;
1220                                 return SYNFIGTOOL_RENDERFAILURE;
1221                         }
1222                         if(print_benchmarks)
1223                                 cout<<job_list.front().filename+": Rendered in "<<timer()<<" seconds."<<endl;
1224                 }
1225         }
1226
1227         job_list.clear();
1228
1229         VERBOSE_OUT(1)<<_("Done.")<<endl;
1230
1231         return SYNFIGTOOL_OK;
1232 }