1 /* === S Y N F I G ========================================================= */
2 /*! \file tool/main.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
34 #include <ETL/stringf>
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>
57 using namespace synfig;
59 /* === M A C R O S ========================================================= */
63 #define _(x) gettext(x)
72 SYNFIGTOOL_FILENOTFOUND = 1,
75 SYNFIGTOOL_UNKNOWNARGUMENT = 4,
76 SYNFIGTOOL_UNKNOWNERROR = 5,
77 SYNFIGTOOL_INVALIDTARGET = 6,
78 SYNFIGTOOL_RENDERFAILURE = 7,
80 SYNFIGTOOL_BADVERSION = 9,
81 SYNFIGTOOL_MISSINGARGUMENT =10
85 #define VERSION "unknown"
86 #define PACKAGE "synfig-tool"
89 #ifdef DEFAULT_QUALITY
90 #undef DEFAULT_QUALITY
93 #define DEFAULT_QUALITY 2
94 #define VERBOSE_OUT(x) if(verbosity>=(x))std::cerr
96 /* === G L O B A L S ======================================================= */
101 bool print_benchmarks=false;
103 /* === M E T H O D S ======================================================= */
105 class Progress : public synfig::ProgressCallback
111 Progress(const char *name):program(name) { }
114 task(const String &task)
116 VERBOSE_OUT(1)<<program<<": "<<task<<std::endl;
121 error(const String &task)
123 std::cerr<<program<<": "<<_("error")<<": "<<task<<std::endl;
128 warning(const String &task)
130 std::cerr<<program<<": "<<_("warning")<<": "<<task<<std::endl;
135 amount_complete(int /*current*/, int /*total*/)
141 class RenderProgress : public synfig::ProgressCallback
146 int clk_scanline; // The scanline at which the clock was reset
152 RenderProgress():clk_scanline(0), last_time(0) { }
155 task(const String &thetask)
162 error(const String &task)
164 std::cout<<_("error")<<": "<<task<<std::endl;
169 warning(const String &task)
171 std::cout<<_("warning")<<": "<<task<<std::endl;
176 amount_complete(int scanline, int h)
178 if(be_quiet)return true;
181 const float time(clk()*(float)(h-scanline)/(float)(scanline-clk_scanline));
182 const float delta(time-last_time);
184 int weeks=0,days=0,hours=0,minutes=0,seconds=0;
196 //cerr<<"reset"<<endl;
198 clk_scanline=scanline;
204 clk_scanline=scanline;
208 minutes++,seconds-=60;
216 cerr<<taskname<<": "<<_("Line")<<" "<<scanline<<_(" of ")<<h<<" -- ";
217 //cerr<<time/(h-clk_scanline)<<" ";
219 if(delta>=-time/(h-clk_scanline) )
222 if(delta>=0 && clk()>4.0 && scanline>clk_scanline+200)
224 //cerr<<"reset"<<endl;
226 clk_scanline=scanline;
243 cerr<<taskname<<": "<<_("DONE")<<" "<<endl;;
256 Canvas::Handle canvas;
257 Target::Handle target;
263 bool canvas_info, canvas_info_all, canvas_info_time_start, canvas_info_time_end, canvas_info_frame_rate,
264 canvas_info_frame_start, canvas_info_frame_end, canvas_info_w, canvas_info_h, canvas_info_image_aspect,
265 canvas_info_pw, canvas_info_ph, canvas_info_pixel_aspect, canvas_info_tl, canvas_info_br,
266 canvas_info_physical_w, canvas_info_physical_h, canvas_info_x_res, canvas_info_y_res, canvas_info_span,
267 canvas_info_interlaced, canvas_info_antialias, canvas_info_clamp, canvas_info_flags, canvas_info_focus,
268 canvas_info_bg_color, canvas_info_metadata;
272 canvas_info = canvas_info_all = canvas_info_time_start = canvas_info_time_end = canvas_info_frame_rate = canvas_info_frame_start = canvas_info_frame_end = canvas_info_w = canvas_info_h = canvas_info_image_aspect = canvas_info_pw = canvas_info_ph = canvas_info_pixel_aspect = canvas_info_tl = canvas_info_br = canvas_info_physical_w = canvas_info_physical_h = canvas_info_x_res = canvas_info_y_res = canvas_info_span = canvas_info_interlaced = canvas_info_antialias = canvas_info_clamp = canvas_info_flags = canvas_info_focus = canvas_info_bg_color = canvas_info_metadata = false;
276 typedef list<String> arg_list_t;
277 typedef list<Job> job_list_t;
281 cout<<"GUID Test"<<endl;
283 cout<<synfig::GUID().get_string()<<' '<<synfig::GUID().get_string()<<endl;
286 void signal_test_func()
288 cout<<"**SIGNAL CALLED**"<<endl;
293 sigc::signal<void> sig;
294 sigc::connection conn;
295 cout<<"Signal Test"<<endl;
296 conn=sig.connect(sigc::ptr_fun(signal_test_func));
297 cout<<"Next line should exclaim signal called."<<endl;
300 cout<<"Next line should NOT exclaim signal called."<<endl;
305 /* === P R O C E D U R E S ================================================= */
307 void display_help_option(const char *flag, const char *arg, string description)
309 const char spaces[]=" ";
311 cerr << strprintf(" %s %s %s", flag, arg, spaces+strlen(arg)+strlen(flag)+1)+description << endl;
313 cerr << strprintf(" %s %s", flag, spaces+strlen(flag))+description << endl;
316 void display_help(bool full)
318 cerr << endl << _("syntax: ") << progname << " [DEFAULT OPTIONS] ([SIF FILE] [SPECIFIC OPTIONS])..." << endl << endl;
322 display_help_option("-t", "<output type>", _("Specify output target (Default:unknown)"));
323 display_help_option("-w", "<pixel width>", _("Set the image width (Use zero for file default)"));
324 display_help_option("-h", "<pixel height>", _("Set the image height (Use zero for file default)"));
325 display_help_option("-s", "<image dist>", _("Set the diagonal size of image window (Span)"));
326 display_help_option("-a", "<1...30>", _("Set antialias amount for parametric renderer."));
327 display_help_option("-Q", "<0...10>", strprintf(_("Specify image quality for accelerated renderer (default=%d)"), DEFAULT_QUALITY).c_str());
328 display_help_option("-g", "<amount>", _("Gamma (default=2.2)"));
329 display_help_option("-v", NULL, _("Verbose Output (add more for more verbosity)"));
330 display_help_option("-q", NULL, _("Quiet mode (No progress/time-remaining display)"));
331 display_help_option("-c", "<canvas id>", _("Render the canvas with the given id instead of the root."));
332 display_help_option("-o", "<output file>", _("Specify output filename"));
333 display_help_option("-T", "<# of threads>", _("Enable multithreaded renderer using specified # of threads"));
334 display_help_option("-b", NULL, _("Print Benchmarks"));
335 display_help_option("--fps", "<framerate>", _("Set the frame rate"));
336 display_help_option("--time", "<time>", _("Render a single frame at <seconds>"));
337 display_help_option("--begin-time", "<time>", _("Set the starting time"));
338 display_help_option("--start-time", "<time>", _("Set the starting time"));
339 display_help_option("--end-time", "<time>", _("Set the ending time"));
340 display_help_option("--dpi", "<res>", _("Set the physical resolution (dots-per-inch)"));
341 display_help_option("--dpi-x", "<res>", _("Set the physical X resolution (dots-per-inch)"));
342 display_help_option("--dpi-y", "<res>", _("Set the physical Y resolution (dots-per-inch)"));
344 display_help_option("--list-canvases", NULL, _("List the exported canvases in the composition"));
345 display_help_option("--canvas-info", "<fields>", _("Print out specified details of the root canvas"));
346 display_help_option("--append", "<filename>", _("Append layers in <filename> to composition"));
348 display_help_option("--layer-info", "<layer>", _("Print out layer's description, parameter info, etc."));
349 display_help_option("--layers", NULL, _("Print out the list of available layers"));
350 display_help_option("--targets", NULL, _("Print out the list of available targets"));
351 display_help_option("--importers", NULL, _("Print out the list of available importers"));
352 display_help_option("--valuenodes", NULL, _("Print out the list of available ValueNodes"));
353 display_help_option("--modules", NULL, _("Print out the list of loaded modules"));
354 display_help_option("--version", NULL, _("Print out version information"));
355 display_help_option("--info", NULL, _("Print out misc build information"));
356 display_help_option("--license", NULL, _("Print out license information"));
359 display_help_option("--guid-test", NULL, _("Test GUID generation"));
360 display_help_option("--signal-test", NULL, _("Test signal implementation"));
364 display_help_option("--help", NULL, _("Print out usage and syntax info"));
369 int process_global_flags(arg_list_t &arg_list)
371 arg_list_t::iterator iter, next;
373 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
376 return SYNFIGTOOL_OK;
378 if(*iter == "--signal-test")
381 return SYNFIGTOOL_HELP;
384 if(*iter == "--guid-test")
387 return SYNFIGTOOL_HELP;
390 if(*iter == "--help")
393 return SYNFIGTOOL_HELP;
396 if(*iter == "--info")
398 cout<<PACKAGE"-"VERSION<<endl;
399 cout<<"Compiled on "__DATE__ /* " at "__TIME__ */;
401 cout<<" with GCC "<<__VERSION__;
404 cout<<" with Microsoft Visual C++ "<<(_MSC_VER>>8)<<'.'<<(_MSC_VER&255);
406 #ifdef __TCPLUSPLUS__
407 cout<<" with Borland Turbo C++ "<<(__TCPLUSPLUS__>>8)<<'.'<<((__TCPLUSPLUS__&255)>>4)<<'.'<<(__TCPLUSPLUS__&15);
410 cout<<endl<<SYNFIG_COPYRIGHT<<endl;
412 return SYNFIGTOOL_HELP;
415 if(*iter == "--layers")
418 synfig::Main synfig_main(dirname(progname),&p);
419 synfig::Layer::Book::iterator iter=synfig::Layer::book().begin();
420 for(;iter!=synfig::Layer::book().end();iter++)
421 if (iter->second.category != CATEGORY_DO_NOT_USE)
422 cout<<iter->first<<endl;
424 return SYNFIGTOOL_HELP;
427 if(*iter == "--layer-info")
430 synfig::Main synfig_main(dirname(progname),&p);
432 if (iter==arg_list.end())
434 error("The `%s' flag requires a value. Use --help for a list of options.", "--layer-info");
435 return SYNFIGTOOL_MISSINGARGUMENT;
437 Layer::Handle layer=synfig::Layer::create(*iter);
438 cout<<"Layer Name: "<<layer->get_name()<<endl;
439 cout<<"Localized Layer Name: "<<layer->get_local_name()<<endl;
440 cout<<"Version: "<<layer->get_version()<<endl;
441 Layer::Vocab vocab=layer->get_param_vocab();
442 for(;!vocab.empty();vocab.pop_front())
444 cout<<"param - "<<vocab.front().get_name();
445 if(!vocab.front().get_critical())
446 cout<<" (not critical)";
447 cout<<endl<<"\tLocalized Name: "<<vocab.front().get_local_name()<<endl;
448 if(!vocab.front().get_description().empty())
449 cout<<"\tDescription: "<<vocab.front().get_description()<<endl;
450 if(!vocab.front().get_hint().empty())
451 cout<<"\tHint: "<<vocab.front().get_hint()<<endl;
454 return SYNFIGTOOL_HELP;
457 if(*iter == "--modules")
460 synfig::Main synfig_main(dirname(progname),&p);
461 synfig::Module::Book::iterator iter=synfig::Module::book().begin();
462 for(;iter!=synfig::Module::book().end();iter++)
463 cout<<iter->first<<endl;
464 return SYNFIGTOOL_HELP;
467 if(*iter == "--targets")
470 synfig::Main synfig_main(dirname(progname),&p);
471 synfig::Target::Book::iterator iter=synfig::Target::book().begin();
472 for(;iter!=synfig::Target::book().end();iter++)
473 cout<<iter->first<<endl;
474 return SYNFIGTOOL_HELP;
477 if(*iter == "--valuenodes")
480 synfig::Main synfig_main(dirname(progname),&p);
481 synfig::LinkableValueNode::Book::iterator iter=synfig::LinkableValueNode::book().begin();
482 for(;iter!=synfig::LinkableValueNode::book().end();iter++)
483 cout<<iter->first<<endl;
484 return SYNFIGTOOL_HELP;
487 if(*iter == "--importers")
490 synfig::Main synfig_main(dirname(progname),&p);
491 synfig::Importer::Book::iterator iter=synfig::Importer::book().begin();
492 for(;iter!=synfig::Importer::book().end();iter++)
493 cout<<iter->first<<endl;
494 return SYNFIGTOOL_HELP;
497 if(*iter == "--version")
499 cerr<<PACKAGE<<" "<<VERSION<<endl;
500 arg_list.erase(iter);
501 return SYNFIGTOOL_HELP;
504 if(*iter == "--license")
506 cerr<<PACKAGE<<" "<<VERSION<<endl;
507 cout<<SYNFIG_COPYRIGHT<<endl<<endl;
509 ** This package is free software; you can redistribute it and/or\n\
510 ** modify it under the terms of the GNU General Public License as\n\
511 ** published by the Free Software Foundation; either version 2 of\n\
512 ** the License, or (at your option) any later version.\n\
514 ** " << endl << endl;
515 arg_list.erase(iter);
516 return SYNFIGTOOL_HELP;
522 arg_list.erase(iter);
529 arg_list.erase(iter);
534 print_benchmarks=true;
535 arg_list.erase(iter);
540 return SYNFIGTOOL_OK;
543 /* true if the given flag takes an extra parameter */
544 bool flag_requires_value(String flag)
546 return (flag=="-a" || flag=="-c" || flag=="-g" || flag=="-h" || flag=="-o" ||
547 flag=="-Q" || flag=="-s" || flag=="-t" || flag=="-T" || flag=="-w" ||
548 flag=="--append" || flag=="--begin-time" || flag=="--canvas-info"|| flag=="--dpi" || flag=="--dpi-x" ||
549 flag=="--dpi-y" || flag=="--end-time" || flag=="--fps" || flag=="--layer-info" || flag=="--start-time" ||
553 int extract_arg_cluster(arg_list_t &arg_list,arg_list_t &cluster)
555 arg_list_t::iterator iter, next;
557 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
559 if(*iter->begin() != '-')
561 //cerr<<*iter->begin()<<"-----------"<<endl;
562 return SYNFIGTOOL_OK;
565 if (flag_requires_value(*iter))
567 cluster.push_back(*iter);
568 arg_list.erase(iter);
570 if (iter==arg_list.end())
572 error("The `%s' flag requires a value. Use --help for a list of options.", cluster.back().c_str());
573 return SYNFIGTOOL_MISSINGARGUMENT;
577 cluster.push_back(*iter);
578 arg_list.erase(iter);
581 return SYNFIGTOOL_OK;
584 int extract_RendDesc(arg_list_t &arg_list,RendDesc &desc)
586 arg_list_t::iterator iter, next;
589 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
593 arg_list.erase(iter);
595 w=atoi(iter->c_str());
596 arg_list.erase(iter);
600 arg_list.erase(iter);
602 h=atoi(iter->c_str());
603 arg_list.erase(iter);
608 arg_list.erase(iter);
610 a=atoi(iter->c_str());
611 desc.set_antialias(a);
612 VERBOSE_OUT(1)<<strprintf(_("Antialiasing set to %d, (%d samples per pixel)"),a,a*a)<<endl;
613 arg_list.erase(iter);
617 arg_list.erase(iter);
619 span=atof(iter->c_str());
620 VERBOSE_OUT(1)<<strprintf(_("Span set to %d units"),span)<<endl;
621 arg_list.erase(iter);
623 else if(*iter=="--fps")
625 arg_list.erase(iter);
627 float fps=atof(iter->c_str());
628 desc.set_frame_rate(fps);
629 arg_list.erase(iter);
630 VERBOSE_OUT(1)<<strprintf(_("Frame rate set to %d frames per second"),fps)<<endl;
632 else if(*iter=="--dpi")
634 arg_list.erase(iter);
636 float dpi=atof(iter->c_str());
637 float dots_per_meter=dpi*39.3700787402;
638 desc.set_x_res(dots_per_meter).set_y_res(dots_per_meter);
639 arg_list.erase(iter);
640 VERBOSE_OUT(1)<<strprintf(_("Physical resolution set to %f dpi"),dpi)<<endl;
642 else if(*iter=="--dpi-x")
644 arg_list.erase(iter);
646 float dpi=atof(iter->c_str());
647 float dots_per_meter=dpi*39.3700787402;
648 desc.set_x_res(dots_per_meter);
649 arg_list.erase(iter);
650 VERBOSE_OUT(1)<<strprintf(_("Physical X resolution set to %f dpi"),dpi)<<endl;
652 else if(*iter=="--dpi-y")
654 arg_list.erase(iter);
656 float dpi=atof(iter->c_str());
657 float dots_per_meter=dpi*39.3700787402;
658 desc.set_y_res(dots_per_meter);
659 arg_list.erase(iter);
660 VERBOSE_OUT(1)<<strprintf(_("Physical Y resolution set to %f dpi"),dpi)<<endl;
662 else if(*iter=="--start-time" || *iter=="--begin-time")
664 arg_list.erase(iter);
666 desc.set_time_start(Time(*iter,desc.get_frame_rate()));
667 arg_list.erase(iter);
669 else if(*iter=="--end-time")
671 arg_list.erase(iter);
673 desc.set_time_end(Time(*iter,desc.get_frame_rate()));
674 arg_list.erase(iter);
676 else if(*iter=="--time")
678 arg_list.erase(iter);
680 desc.set_time(Time(*iter,desc.get_frame_rate()));
681 VERBOSE_OUT(1)<<_("Rendering frame at ")<<desc.get_time_start().get_string(desc.get_frame_rate())<<endl;
682 arg_list.erase(iter);
686 synfig::warning("Gamma argument is currently ignored");
687 arg_list.erase(iter);
689 //desc.set_gamma(Gamma(atof(iter->c_str())));
690 arg_list.erase(iter);
692 else if (flag_requires_value(*iter))
698 w = desc.get_w() * h / desc.get_h();
700 h = desc.get_h() * w / desc.get_w();
703 VERBOSE_OUT(1)<<strprintf(_("Resolution set to %dx%d"),w,h)<<endl;
707 return SYNFIGTOOL_OK;
710 int extract_quality(arg_list_t &arg_list,int &quality)
712 arg_list_t::iterator iter, next;
713 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
717 arg_list.erase(iter);
719 quality=atoi(iter->c_str());
720 VERBOSE_OUT(1)<<strprintf(_("Quality set to %d"),quality)<<endl;
721 arg_list.erase(iter);
723 else if (flag_requires_value(*iter))
727 return SYNFIGTOOL_OK;
730 int extract_threads(arg_list_t &arg_list,int &threads)
732 arg_list_t::iterator iter, next;
733 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
737 arg_list.erase(iter);
739 threads=atoi(iter->c_str());
740 VERBOSE_OUT(1)<<strprintf(_("Threads set to %d"),threads)<<endl;
741 arg_list.erase(iter);
743 else if (flag_requires_value(*iter))
747 return SYNFIGTOOL_OK;
750 int extract_target(arg_list_t &arg_list,string &type)
752 arg_list_t::iterator iter, next;
755 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
759 arg_list.erase(iter);
762 arg_list.erase(iter);
764 else if (flag_requires_value(*iter))
768 return SYNFIGTOOL_OK;
771 int extract_append(arg_list_t &arg_list,string &filename)
773 arg_list_t::iterator iter, next;
776 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
778 if(*iter=="--append")
780 arg_list.erase(iter);
783 arg_list.erase(iter);
785 else if (flag_requires_value(*iter))
789 return SYNFIGTOOL_OK;
792 int extract_outfile(arg_list_t &arg_list,string &outfile)
794 arg_list_t::iterator iter, next;
795 int ret=SYNFIGTOOL_FILENOTFOUND;
798 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
802 arg_list.erase(iter);
805 arg_list.erase(iter);
808 else if (flag_requires_value(*iter))
815 int extract_canvasid(arg_list_t &arg_list,string &canvasid)
817 arg_list_t::iterator iter, next;
820 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
824 arg_list.erase(iter);
827 arg_list.erase(iter);
829 else if (flag_requires_value(*iter))
833 return SYNFIGTOOL_OK;
836 int extract_list_canvases(arg_list_t &arg_list,bool &list_canvases)
838 arg_list_t::iterator iter, next;
840 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
841 if(*iter=="--list-canvases")
843 list_canvases = true;
844 arg_list.erase(iter);
847 return SYNFIGTOOL_OK;
850 void extract_canvas_info(arg_list_t &arg_list, Job &job)
852 arg_list_t::iterator iter, next;
854 for(next=arg_list.begin(),iter=next++;iter!=arg_list.end();iter=next++)
855 if(*iter=="--canvas-info")
857 job.canvas_info = true;
858 arg_list.erase(iter);
860 String values(*iter), value;
861 arg_list.erase(iter);
863 std::string::size_type pos;
864 while (!values.empty())
866 pos = values.find_first_of(',');
867 if (pos == std::string::npos)
874 value = values.substr(0, pos);
875 values = values.substr(pos+1);
879 job.canvas_info_all = true;
883 if (value == "time_start") job.canvas_info_time_start = true;
884 else if (value == "time_end") job.canvas_info_time_end = true;
885 else if (value == "frame_rate") job.canvas_info_frame_rate = true;
886 else if (value == "frame_start") job.canvas_info_frame_start = true;
887 else if (value == "frame_end") job.canvas_info_frame_end = true;
888 else if (value == "w") job.canvas_info_w = true;
889 else if (value == "h") job.canvas_info_h = true;
890 else if (value == "image_aspect") job.canvas_info_image_aspect = true;
891 else if (value == "pw") job.canvas_info_pw = true;
892 else if (value == "ph") job.canvas_info_ph = true;
893 else if (value == "pixel_aspect") job.canvas_info_pixel_aspect = true;
894 else if (value == "tl") job.canvas_info_tl = true;
895 else if (value == "br") job.canvas_info_br = true;
896 else if (value == "physical_w") job.canvas_info_physical_w = true;
897 else if (value == "physical_h") job.canvas_info_physical_h = true;
898 else if (value == "x_res") job.canvas_info_x_res = true;
899 else if (value == "y_res") job.canvas_info_y_res = true;
900 else if (value == "span") job.canvas_info_span = true;
901 else if (value == "interlaced") job.canvas_info_interlaced = true;
902 else if (value == "antialias") job.canvas_info_antialias = true;
903 else if (value == "clamp") job.canvas_info_clamp = true;
904 else if (value == "flags") job.canvas_info_flags = true;
905 else if (value == "focus") job.canvas_info_focus = true;
906 else if (value == "bg_color") job.canvas_info_bg_color = true;
907 else if (value == "metadata") job.canvas_info_metadata = true;
910 cerr<<_("Unrecognised canvas variable: ") << "'" << value << "'" << endl;
911 cerr<<_("Recognized variables are:") << endl <<
912 " all, time_start, time_end, frame_rate, frame_start, frame_end, w, h," << endl <<
913 " image_aspect, pw, ph, pixel_aspect, tl, br, physical_w, physical_h," << endl <<
914 " x_res, y_res, span, interlaced, antialias, clamp, flags," << endl <<
915 " focus, bg_color, metadata" << endl;
918 if (pos == std::string::npos)
924 void list_child_canvases(string prefix, Canvas::Handle canvas)
926 Canvas::Children children(canvas->children());
927 for (Canvas::Children::iterator iter = children.begin(); iter != children.end(); iter++)
929 cout << prefix << ":" << (*iter)->get_id() << endl;
930 list_child_canvases(prefix + ":" + (*iter)->get_id(), *iter);
934 void list_canvas_info(Job job)
936 Canvas::Handle canvas(job.canvas);
937 const RendDesc &rend_desc(canvas->rend_desc());
939 if (job.canvas_info_all || job.canvas_info_time_start)
941 cout << endl << "# " << _("Start Time") << endl;
942 cout << "time_start" << "=" << rend_desc.get_time_start().get_string().c_str() << endl;
945 if (job.canvas_info_all || job.canvas_info_time_end)
947 cout << endl << "# " << _("End Time") << endl;
948 cout << "time_end" << "=" << rend_desc.get_time_end().get_string().c_str() << endl;
951 if (job.canvas_info_all || job.canvas_info_frame_rate)
953 cout << endl << "# " << _("Frame Rate") << endl;
954 cout << "frame_rate" << "=" << rend_desc.get_frame_rate() << endl;
957 if (job.canvas_info_all || job.canvas_info_frame_start)
959 cout << endl << "# " << _("Start Frame") << endl;
960 cout << "frame_start" << "=" << rend_desc.get_frame_start() << endl;
963 if (job.canvas_info_all || job.canvas_info_frame_end)
965 cout << endl << "# " << _("End Frame") << endl;
966 cout << "frame_end" << "=" << rend_desc.get_frame_end() << endl;
969 if (job.canvas_info_all)
972 if (job.canvas_info_all || job.canvas_info_w)
974 cout << endl << "# " << _("Width") << endl;
975 cout << "w" << "=" << rend_desc.get_w() << endl;
978 if (job.canvas_info_all || job.canvas_info_h)
980 cout << endl << "# " << _("Height") << endl;
981 cout << "h" << "=" << rend_desc.get_h() << endl;
984 if (job.canvas_info_all || job.canvas_info_image_aspect)
986 cout << endl << "# " << _("Image Aspect Ratio") << endl;
987 cout << "image_aspect" << "=" << rend_desc.get_image_aspect() << endl;
990 if (job.canvas_info_all)
993 if (job.canvas_info_all || job.canvas_info_pw)
995 cout << endl << "# " << _("Pixel Width") << endl;
996 cout << "pw" << "=" << rend_desc.get_pw() << endl;
999 if (job.canvas_info_all || job.canvas_info_ph)
1001 cout << endl << "# " << _("Pixel Height") << endl;
1002 cout << "ph" << "=" << rend_desc.get_ph() << endl;
1005 if (job.canvas_info_all || job.canvas_info_pixel_aspect)
1007 cout << endl << "# " << _("Pixel Aspect Ratio") << endl;
1008 cout << "pixel_aspect" << "=" << rend_desc.get_pixel_aspect() << endl;
1011 if (job.canvas_info_all)
1014 if (job.canvas_info_all || job.canvas_info_tl)
1016 cout << endl << "# " << _("Top Left") << endl;
1017 cout << "tl" << "=" << rend_desc.get_tl()[0]
1018 << " " << rend_desc.get_tl()[1] << endl;
1021 if (job.canvas_info_all || job.canvas_info_br)
1023 cout << endl << "# " << _("Bottom Right") << endl;
1024 cout << "br" << "=" << rend_desc.get_br()[0]
1025 << " " << rend_desc.get_br()[1] << endl;
1028 if (job.canvas_info_all || job.canvas_info_physical_w)
1030 cout << endl << "# " << _("Physical Width") << endl;
1031 cout << "physical_w" << "=" << rend_desc.get_physical_w() << endl;
1034 if (job.canvas_info_all || job.canvas_info_physical_h)
1036 cout << endl << "# " << _("Physical Height") << endl;
1037 cout << "physical_h" << "=" << rend_desc.get_physical_h() << endl;
1040 if (job.canvas_info_all || job.canvas_info_x_res)
1042 cout << endl << "# " << _("X Resolution") << endl;
1043 cout << "x_res" << "=" << rend_desc.get_x_res() << endl;
1046 if (job.canvas_info_all || job.canvas_info_y_res)
1048 cout << endl << "# " << _("Y Resolution") << endl;
1049 cout << "y_res" << "=" << rend_desc.get_y_res() << endl;
1052 if (job.canvas_info_all || job.canvas_info_span)
1054 cout << endl << "# " << _("Diagonal Image Span") << endl;
1055 cout << "span" << "=" << rend_desc.get_span() << endl;
1058 if (job.canvas_info_all)
1061 if (job.canvas_info_all || job.canvas_info_interlaced)
1063 cout << endl << "# " << _("Interlaced") << endl;
1064 cout << "interlaced" << "=" << rend_desc.get_interlaced() << endl;
1067 if (job.canvas_info_all || job.canvas_info_antialias)
1069 cout << endl << "# " << _("Antialias") << endl;
1070 cout << "antialias" << "=" << rend_desc.get_antialias() << endl;
1073 if (job.canvas_info_all || job.canvas_info_clamp)
1075 cout << endl << "# " << _("Clamp") << endl;
1076 cout << "clamp" << "=" << rend_desc.get_clamp() << endl;
1079 if (job.canvas_info_all || job.canvas_info_flags)
1081 cout << endl << "# " << _("Flags") << endl;
1082 cout << "flags" << "=" << rend_desc.get_flags() << endl;
1085 if (job.canvas_info_all || job.canvas_info_focus)
1087 cout << endl << "# " << _("Focus") << endl;
1088 cout << "focus" << "=" << rend_desc.get_focus()[0]
1089 << " " << rend_desc.get_focus()[1] << endl;
1092 if (job.canvas_info_all || job.canvas_info_bg_color)
1094 cout << endl << "# " << _("Background Color") << endl;
1095 cout << "bg_color" << "=" << rend_desc.get_bg_color().get_string().c_str() << endl;
1098 if (job.canvas_info_all)
1101 if (job.canvas_info_all || job.canvas_info_metadata)
1103 std::list<String> keys(canvas->get_meta_data_keys());
1104 cout << endl << "# " << _("Metadata") << endl;
1105 for (std::list<String>::iterator iter = keys.begin(); iter != keys.end(); iter++)
1106 cout << *iter << "=" << canvas->get_meta_data(*iter) << endl;
1110 /* === M E T H O D S ======================================================= */
1112 /* === E N T R Y P O I N T ================================================= */
1114 int main(int argc, char *argv[])
1117 arg_list_t arg_list;
1118 job_list_t job_list;
1120 setlocale(LC_ALL, "");
1123 bindtextdomain("synfig", LOCALEDIR);
1124 bind_textdomain_codeset("synfig", "UTF-8");
1125 textdomain("synfig");
1129 Progress p(argv[0]);
1131 if(!SYNFIG_CHECK_VERSION())
1133 cerr<<_("FATAL: Synfig Version Mismatch")<<endl;
1134 return SYNFIGTOOL_BADVERSION;
1139 display_help(false);
1140 return SYNFIGTOOL_BLANK;
1144 arg_list.push_back(argv[i]);
1146 if((i=process_global_flags(arg_list)))
1149 VERBOSE_OUT(1)<<_("verbosity set to ")<<verbosity<<endl;
1150 synfig::Main synfig_main(dirname(progname),&p);
1153 arg_list_t defaults, imageargs;
1156 // Grab the defaults before the first file
1157 if ((ret = extract_arg_cluster(arg_list,defaults)) != SYNFIGTOOL_OK)
1160 while(arg_list.size())
1163 job_list.push_front(Job());
1167 job_list.front().filename=arg_list.front();
1168 arg_list.pop_front();
1170 if ((ret = extract_arg_cluster(arg_list,imageargs)) != SYNFIGTOOL_OK)
1173 // Open the composition
1174 String errors, warnings;
1177 job_list.front().root=open_canvas(job_list.front().filename, errors, warnings);
1179 catch(runtime_error x)
1181 job_list.front().root = 0;
1184 if(!job_list.front().root)
1186 cerr<<_("Unable to load '")<<job_list.front().filename<<"'."<<endl;
1187 cerr<<_("Throwing out job...")<<endl;
1188 job_list.pop_front();
1192 bool list_canvases = false;
1193 extract_list_canvases(imageargs, list_canvases);
1194 job_list.front().list_canvases = list_canvases;
1196 extract_canvas_info(imageargs, job_list.front());
1198 job_list.front().root->set_time(0);
1201 extract_canvasid(imageargs,canvasid);
1202 if(!canvasid.empty())
1207 job_list.front().canvas=job_list.front().root->find_canvas(canvasid, warnings);
1209 catch(Exception::IDNotFound)
1211 cerr<<_("Unable to find canvas with ID \"")<<canvasid<<_("\" in ")<<job_list.front().filename<<"."<<endl;
1212 cerr<<_("Throwing out job...")<<endl;
1213 job_list.pop_front();
1217 catch(Exception::BadLinkName)
1219 cerr<<_("Invalid canvas name \"")<<canvasid<<_("\" in ")<<job_list.front().filename<<"."<<endl;
1220 cerr<<_("Throwing out job...")<<endl;
1221 job_list.pop_front();
1226 job_list.front().canvas=job_list.front().root;
1228 extract_RendDesc(imageargs,job_list.front().canvas->rend_desc());
1229 extract_target(imageargs,target_name);
1230 extract_threads(imageargs,threads);
1231 job_list.front().quality=DEFAULT_QUALITY;
1232 extract_quality(imageargs,job_list.front().quality);
1233 VERBOSE_OUT(2)<<_("Quality set to ")<<job_list.front().quality<<endl;
1234 job_list.front().desc=job_list.front().canvas->rend_desc();
1235 extract_outfile(imageargs,job_list.front().outfilename);
1237 // Extract composite
1239 string composite_file;
1240 extract_append(imageargs,composite_file);
1241 if(!composite_file.empty())
1243 String errors, warnings;
1244 Canvas::Handle composite(open_canvas(composite_file, errors, warnings));
1247 cerr<<_("Unable to append '")<<composite_file<<"'."<<endl;
1250 Canvas::reverse_iterator iter;
1251 for(iter=composite->rbegin();iter!=composite->rend();++iter)
1253 Layer::Handle layer(*iter);
1255 job_list.front().canvas->push_front(layer->clone());
1257 VERBOSE_OUT(2)<<_("Appended contents of ")<<composite_file<<endl;
1261 VERBOSE_OUT(4)<<_("Attempting to determine target/outfile...")<<endl;
1263 // If the target type is not yet defined,
1264 // try to figure it out from the outfile.
1265 if(target_name.empty() && !job_list.front().outfilename.empty())
1267 VERBOSE_OUT(3)<<_("Target name undefined, attempting to figure it out")<<endl;
1268 string ext = filename_extension(job_list.front().outfilename);
1269 if (ext.length()) ext = ext.substr(1);
1270 if(Target::ext_book().count(ext))
1272 target_name=Target::ext_book()[ext];
1273 info("target name not specified - using %s", target_name.c_str());
1277 string lower_ext(ext);
1279 for(unsigned int i=0;i<ext.length();i++)
1280 lower_ext[i] = tolower(ext[i]);
1282 if(Target::ext_book().count(lower_ext))
1284 target_name=Target::ext_book()[lower_ext];
1285 info("target name not specified - using %s", target_name.c_str());
1292 // If the target type is STILL not yet defined, then
1293 // set it to a some sort of default
1294 if(target_name.empty())
1296 VERBOSE_OUT(2)<<_("Defaulting to PNG target...")<<endl;
1300 // If no output filename was provided, then
1301 // create a output filename based on the
1302 // given input filename. (ie: change the extension)
1303 if(job_list.front().outfilename.empty())
1305 job_list.front().outfilename = filename_sans_extension(job_list.front().filename) + '.';
1306 if(Target::book().count(target_name))
1307 job_list.front().outfilename+=Target::book()[target_name].second;
1309 job_list.front().outfilename+=target_name;
1312 VERBOSE_OUT(4)<<"target_name="<<target_name<<endl;
1313 VERBOSE_OUT(4)<<"outfile_name="<<job_list.front().outfilename<<endl;
1315 VERBOSE_OUT(4)<<_("Creating the target...")<<endl;
1316 job_list.front().target=synfig::Target::create(target_name,job_list.front().outfilename);
1318 if(target_name=="sif")
1319 job_list.front().sifout=true;
1322 if(!job_list.front().target)
1324 cerr<<_("Unknown target for ")<<job_list.front().filename<<": "<<target_name<<endl;
1325 cerr<<_("Throwing out job...")<<endl;
1326 job_list.pop_front();
1329 job_list.front().sifout=false;
1332 // Set the Canvas on the Target
1333 if(job_list.front().target)
1335 VERBOSE_OUT(4)<<_("Setting the canvas on the target...")<<endl;
1336 job_list.front().target->set_canvas(job_list.front().canvas);
1337 VERBOSE_OUT(4)<<_("Setting the quality of the target...")<<endl;
1338 job_list.front().target->set_quality(job_list.front().quality);
1341 // Set the threads for the target
1342 if(job_list.front().target && Target_Scanline::Handle::cast_dynamic(job_list.front().target))
1343 Target_Scanline::Handle::cast_dynamic(job_list.front().target)->set_threads(threads);
1345 if(imageargs.size())
1347 cerr<<_("Unidentified arguments for ")<<job_list.front().filename<<": ";
1348 for(;imageargs.size();imageargs.pop_front())
1349 cerr<<' '<<imageargs.front();
1351 cerr<<_("Throwing out job...")<<endl;
1352 job_list.pop_front();
1360 cerr<<_("Unidentified arguments:");
1361 for(;arg_list.size();arg_list.pop_front())
1362 cerr<<' '<<arg_list.front();
1364 return SYNFIGTOOL_UNKNOWNARGUMENT;
1367 if(!job_list.size())
1369 cerr<<_("Nothing to do!")<<endl;
1370 return SYNFIGTOOL_BORED;
1373 for(;job_list.size();job_list.pop_front())
1375 VERBOSE_OUT(3)<<job_list.front().filename<<" -- "<<endl<<'\t'<<
1376 strprintf("w:%d, h:%d, a:%d, pxaspect:%f, imaspect:%f, span:%f",
1377 job_list.front().desc.get_w(),
1378 job_list.front().desc.get_h(),
1379 job_list.front().desc.get_antialias(),
1380 job_list.front().desc.get_pixel_aspect(),
1381 job_list.front().desc.get_image_aspect(),
1382 job_list.front().desc.get_span()
1384 strprintf("tl:[%f,%f], br:[%f,%f], focus:[%f,%f]",
1385 job_list.front().desc.get_tl()[0],job_list.front().desc.get_tl()[1],
1386 job_list.front().desc.get_br()[0],job_list.front().desc.get_br()[1],
1387 job_list.front().desc.get_focus()[0],job_list.front().desc.get_focus()[1]
1391 p.task(job_list.front().filename+" ==> "+job_list.front().outfilename);
1392 if(job_list.front().sifout)
1394 if(!save_canvas(job_list.front().outfilename,job_list.front().canvas))
1396 cerr<<"Render Failure."<<endl;
1397 return SYNFIGTOOL_RENDERFAILURE;
1400 else if (job_list.front().list_canvases)
1402 list_child_canvases(job_list.front().filename + "#", job_list.front().canvas);
1405 else if (job_list.front().canvas_info)
1407 list_canvas_info(job_list.front());
1412 VERBOSE_OUT(1)<<_("Rendering...")<<endl;
1415 // Call the render member of the target
1416 if(!job_list.front().target->render(&p))
1418 cerr<<"Render Failure."<<endl;
1419 return SYNFIGTOOL_RENDERFAILURE;
1421 if(print_benchmarks)
1422 cout<<job_list.front().filename+": Rendered in "<<timer()<<" seconds."<<endl;
1428 VERBOSE_OUT(1)<<_("Done.")<<endl;
1430 return SYNFIGTOOL_OK;