Blame synfig-core/src/tool/joblistprocessor.cpp

Diego Barrios Romero 53159b
/* === S Y N F I G ========================================================= */
Diego Barrios Romero 53159b
/*!	\file tool/joblistprocessor.cpp
Diego Barrios Romero 53159b
**	\brief Synfig Tool Rendering Job List Processor Class
Diego Barrios Romero 53159b
**
Diego Barrios Romero 53159b
**	$Id$
Diego Barrios Romero 53159b
**
Diego Barrios Romero 53159b
**	\legal
Diego Barrios Romero 53159b
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Diego Barrios Romero 53159b
**	Copyright (c) 2007, 2008 Chris Moore
Diego Barrios Romero 088628
**	Copyright (c) 2009-2015 Diego Barrios Romero
Diego Barrios Romero 53159b
**
Diego Barrios Romero 53159b
**	This package is free software; you can redistribute it and/or
Diego Barrios Romero 53159b
**	modify it under the terms of the GNU General Public License as
Diego Barrios Romero 53159b
**	published by the Free Software Foundation; either version 2 of
Diego Barrios Romero 53159b
**	the License, or (at your option) any later version.
Diego Barrios Romero 53159b
**
Diego Barrios Romero 53159b
**	This package is distributed in the hope that it will be useful,
Diego Barrios Romero 53159b
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Diego Barrios Romero 53159b
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Diego Barrios Romero 53159b
**	General Public License for more details.
Diego Barrios Romero 53159b
**	\endlegal
Diego Barrios Romero 53159b
*/
Diego Barrios Romero 53159b
/* ========================================================================= */
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
#ifdef USING_PCH
Diego Barrios Romero 53159b
#	include "pch.h"
Diego Barrios Romero 53159b
#else
Diego Barrios Romero 53159b
#ifdef HAVE_CONFIG_H
Diego Barrios Romero 53159b
#	include <config.h></config.h>
Diego Barrios Romero 53159b
#endif
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
#include <iostream></iostream>
Diego Barrios Romero 53159b
#include <list></list>
Diego Barrios Romero 53159b
#include <algorithm></algorithm>
Diego Barrios Romero 53159b
#include <errno.h></errno.h>
Diego Barrios Romero 53159b
#include <cstring></cstring>
Diego Barrios Romero 53159b
cc8279
//#include <boost options_description.hpp="" program_options=""></boost>
cc8279
//#include <boost program_options="" variables_map.hpp=""></boost>
3e2a84
//#include <boost format.hpp=""></boost>
a96363
#include <chrono></chrono>
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
#include <autorevision.h></autorevision.h>
Diego Barrios Romero 53159b
#include <synfig general.h=""></synfig>
bw 94d8a6
#include <synfig localization.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig canvas.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig target.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig layer.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig time.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig target_scanline.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig paramdesc.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig module.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig importer.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig loadcanvas.h=""></synfig>
Diego Barrios Romero 53159b
#include <synfig savecanvas.h=""></synfig>
d4b6c4
#include <synfig filesystemnative.h=""></synfig>
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
#include "definitions.h"
Diego Barrios Romero 53159b
#include "job.h"
Diego Barrios Romero 53159b
#include "synfigtoolexception.h"
Diego Barrios Romero 53159b
#include "printing_functions.h"
Diego Barrios Romero 53159b
#include "renderprogress.h"
Diego Barrios Romero 53159b
#include "joblistprocessor.h"
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
#endif
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
using namespace synfig;
Diego Barrios Romero 53159b
Diego Barrios Romero 46977c
void process_job_list(std::list<job>& job_list, const TargetParam& target_params)</job>
Diego Barrios Romero 53159b
{
3b3e3e
	if (job_list.empty())
Diego Barrios Romero 53159b
		throw (SynfigToolException(SYNFIGTOOL_BORED, _("Nothing to do!")));
Diego Barrios Romero 53159b
3b3e3e
	for(; !job_list.empty(); job_list.pop_front())
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 53159b
		if (setup_job(job_list.front(), target_params))
Diego Barrios Romero 53159b
			process_job(job_list.front());
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
}
Diego Barrios Romero 53159b
509b0d
std::string get_extension(const std::string &filename)
509b0d
{
509b0d
	std::size_t found = filename.rfind(".");
509b0d
	if (found == std::string::npos) return ""; // extension not found
509b0d
509b0d
	return filename.substr(found);
509b0d
}
509b0d
509b0d
std::string replace_extension(const std::string &filename, const std::string &new_extension)
509b0d
{
509b0d
	std::size_t found = filename.rfind(".");
509b0d
	if (found == std::string::npos) return filename + "." + new_extension; // extension not found
509b0d
	
509b0d
	return filename.substr(0, found) + "." + new_extension;
509b0d
}
509b0d
509b0d
std::string get_absolute_path(std::string relative_path) {
509b0d
  Glib::RefPtr<gio::file> file = Gio::File::create_for_path(relative_path);</gio::file>
509b0d
  return file->get_path();
509b0d
}
509b0d
Diego Barrios Romero 53159b
bool setup_job(Job& job, const TargetParam& target_parameters)
Diego Barrios Romero 53159b
{
Diego Barrios Romero 46977c
	VERBOSE_OUT(4) << _("Attempting to determine target/outfile...") << std::endl;
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	// If the target type is not yet defined,
Diego Barrios Romero 53159b
	// try to figure it out from the outfile.
Diego Barrios Romero 66ac9b
	if(job.target_name.empty() && !job.outfilename.empty())
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 53159b
		VERBOSE_OUT(3) << _("Target name undefined, attempting to figure it out")
Diego Barrios Romero 46977c
					   << std::endl;
509b0d
		//std::string ext = bfs::path(job.outfilename).extension().string();
509b0d
		std::string ext = get_extension(job.outfilename);
Diego Barrios Romero 53159b
		if (ext.length())
Diego Barrios Romero 53159b
			ext = ext.substr(1);
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
		if(Target::ext_book().count(ext))
Diego Barrios Romero 53159b
		{
Diego Barrios Romero 53159b
			job.target_name = Target::ext_book()[ext];
Diego Barrios Romero 53159b
			info("target name not specified - using %s", job.target_name.c_str());
Diego Barrios Romero 53159b
		}
Diego Barrios Romero 53159b
		else
Diego Barrios Romero 53159b
		{
Diego Barrios Romero a58581
			std::string lower_ext;
Diego Barrios Romero a58581
			std::transform(ext.begin(), ext.end(), std::back_inserter(lower_ext), ::tolower);
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
			if(Target::ext_book().count(lower_ext))
Diego Barrios Romero 53159b
			{
Diego Barrios Romero 53159b
				job.target_name=Target::ext_book()[lower_ext];
Diego Barrios Romero 53159b
				info("target name not specified - using %s", job.target_name.c_str());
Diego Barrios Romero 53159b
			}
Diego Barrios Romero 53159b
			else
Diego Barrios Romero 53159b
				job.target_name=ext;
Diego Barrios Romero 53159b
		}
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	// If the target type is STILL not yet defined, then
Diego Barrios Romero 53159b
	// set it to a some sort of default
Diego Barrios Romero 53159b
	if(job.target_name.empty())
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 46977c
		VERBOSE_OUT(2) << _("Defaulting to PNG target...") << std::endl;
Diego Barrios Romero 53159b
		job.target_name = "png";
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	// If no output filename was provided, then create a output filename
Diego Barrios Romero 53159b
	// based on the given input filename and the selected target.
Diego Barrios Romero 53159b
	// (ie: change the extension)
Diego Barrios Romero 53159b
	if(job.outfilename.empty())
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 39836f
        std::string new_extension;
Diego Barrios Romero 53159b
		if(Target::book().count(job.target_name))
Diego Barrios Romero 279cbb
			new_extension = Target::book()[job.target_name].filename;
Diego Barrios Romero 53159b
		else
Diego Barrios Romero 279cbb
			new_extension = job.target_name;
Diego Barrios Romero 39836f
509b0d
        //job.outfilename = bfs::path(job.filename).replace_extension(new_extension).string();
509b0d
		job.outfilename = replace_extension(job.filename, new_extension);
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 46977c
	VERBOSE_OUT(4) << "Target name = " << job.target_name.c_str() << std::endl;
Diego Barrios Romero 46977c
	VERBOSE_OUT(4) << "Outfilename = " << job.outfilename.c_str() << std::endl;
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	// Check permissions
509b0d
	//if (access(bfs::canonical(bfs::path(job.outfilename).parent_path()).string().c_str(), W_OK) == -1)
509b0d
	// az: fixme
509b0d
	if (access(get_absolute_path(job.outfilename + "/../").c_str(), W_OK) == -1)
Diego Barrios Romero 53159b
	{
3e2a84
	    /*const std::string message =
Diego Barrios Romero 38d7c8
            (boost::format(_("Unable to create output for \"%s\": %s"))
Diego Barrios Romero 38d7c8
                           % job.filename % strerror(errno)).str();
3e2a84
		synfig::error(message.c_str());*/
103db6
		synfig::error(_("Unable to create output for \"%s\": %s"), job.filename.c_str(), strerror(errno));
da7b5a
		synfig::error(_("Throwing out job..."));
Diego Barrios Romero 53159b
		return false;
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 46977c
	VERBOSE_OUT(4) << _("Creating the target...") << std::endl;
Diego Barrios Romero 53159b
	job.target =
Diego Barrios Romero 53159b
		synfig::Target::create(job.target_name,
Diego Barrios Romero 53159b
							   job.outfilename,
Diego Barrios Romero 53159b
							   target_parameters);
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	if(job.target_name == "sif")
Diego Barrios Romero 53159b
		job.sifout=true;
Diego Barrios Romero 53159b
	else
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 53159b
		if(!job.target)
Diego Barrios Romero 53159b
		{
3e2a84
		    /*const std::string message =
Diego Barrios Romero 38d7c8
                (boost::format(_("Unknown target for \"%s\": %s"))
Diego Barrios Romero 38d7c8
                               % job.filename % strerror(errno)).str();
3e2a84
		    synfig::error(message.c_str());*/
3e2a84
103db6
			synfig::error(_("Unknown target for \"%s\": %s"), job.filename.c_str(), strerror(errno));
3e2a84
			synfig::error(_("Throwing out job..."));
3e2a84
			return false;
Diego Barrios Romero 53159b
		}
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
		job.sifout=false;
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	// Set the Canvas on the Target
Diego Barrios Romero 53159b
	if(job.target)
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 46977c
		VERBOSE_OUT(4) << _("Setting the canvas on the target...") << std::endl;
Diego Barrios Romero 53159b
		job.target->set_canvas(job.canvas);
Diego Barrios Romero 53159b
Diego Barrios Romero 46977c
		VERBOSE_OUT(4) << _("Setting the quality of the target...") << std::endl;
Diego Barrios Romero 53159b
		job.target->set_quality(job.quality);
Diego Barrios Romero 8e8abf
4010f4
		if (job.alpha_mode!=TARGET_ALPHA_MODE_KEEP)
4010f4
		{
Diego Barrios Romero 46977c
			VERBOSE_OUT(4) << _("Setting the alpha mode of the target...") << std::endl;
4010f4
			job.target->set_alpha_mode(job.alpha_mode);
4010f4
		}
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	// Set the threads for the target
4010f4
	if (job.target && Target_Scanline::Handle::cast_dynamic(job.target))
Diego Barrios Romero 0b9c51
		Target_Scanline::Handle::cast_dynamic(job.target)->set_threads(SynfigToolGeneralOptions::instance()->get_threads());
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	return true;
Diego Barrios Romero 53159b
}
Diego Barrios Romero 53159b
Diego Barrios Romero 0b9c51
void process_job (Job& job)
Diego Barrios Romero 53159b
{
Diego Barrios Romero 46977c
	VERBOSE_OUT(3) << job.filename.c_str() << " -- " << std::endl;
3e2a84
	synfig::info("\tw: %d, h: %d, a: %d, pxaspect: %f, imaspect: %f, span: %f", 
3e2a84
		job.desc.get_w(), job.desc.get_h(), job.desc.get_antialias(),
3e2a84
		job.desc.get_pixel_aspect(), job.desc.get_image_aspect(), job.desc.get_span());
3e2a84
	/*VERBOSE_OUT(3) << '\t'
Diego Barrios Romero 0d46b7
				   << boost::format("w:%d, h:%d, a:%d, pxaspect:%f, imaspect:%f, span:%f")
Diego Barrios Romero 0d46b7
                                    % job.desc.get_w()
Diego Barrios Romero 0d46b7
                                    % job.desc.get_h()
Diego Barrios Romero 0d46b7
                                    % job.desc.get_antialias()
Diego Barrios Romero 0d46b7
                                    % job.desc.get_pixel_aspect()
Diego Barrios Romero 0d46b7
                                    % job.desc.get_image_aspect()
Diego Barrios Romero 0d46b7
                                    % job.desc.get_span()
3e2a84
                    << std::endl;*/
3e2a84
3e2a84
	synfig::info("\ttl: [%f,%f], br: [%f,%f], focus: [%f,%f]",
3e2a84
				job.desc.get_tl()[0], job.desc.get_tl()[1],
3e2a84
				job.desc.get_br()[0], job.desc.get_br()[1],
3e2a84
				job.desc.get_focus()[0], job.desc.get_focus()[1]
3e2a84
	);
Diego Barrios Romero 53159b
3e2a84
	/*VERBOSE_OUT(3) << '\t'
Diego Barrios Romero 0d46b7
				   << boost::format("tl:[%f,%f], br:[%f,%f], focus:[%f,%f]")
Diego Barrios Romero 0d46b7
                                    % job.desc.get_tl()[0]
Diego Barrios Romero 0d46b7
                                    % job.desc.get_tl()[1]
Diego Barrios Romero 0d46b7
                                    % job.desc.get_br()[0]
Diego Barrios Romero 0d46b7
                                    % job.desc.get_br()[1]
Diego Barrios Romero 0d46b7
                                    % job.desc.get_focus()[0]
Diego Barrios Romero 0d46b7
                                    % job.desc.get_focus()[1]
3e2a84
                    << std::endl;*/
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	RenderProgress p;
Diego Barrios Romero 53159b
	p.task(job.filename + " ==> " + job.outfilename);
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
	if(job.sifout)
Diego Barrios Romero 53159b
	{
d4b6c4
		// todo: support containers
d4b6c4
		if(!save_canvas(FileSystemNative::instance()->get_identifier(job.outfilename), job.canvas))
Diego Barrios Romero 53159b
			throw (SynfigToolException(SYNFIGTOOL_RENDERFAILURE, _("Render Failure.")));
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
	else
Diego Barrios Romero 53159b
	{
Diego Barrios Romero 46977c
		VERBOSE_OUT(1) << _("Rendering...") << std::endl;
a96363
		std::chrono::system_clock::time_point start_timepoint =
a96363
            std::chrono::system_clock::now();
Diego Barrios Romero 53159b
Diego Barrios Romero 53159b
		// Call the render member of the target
Diego Barrios Romero 53159b
		if(!job.target->render(&p))
Diego Barrios Romero 53159b
			throw (SynfigToolException(SYNFIGTOOL_RENDERFAILURE, _("Render Failure.")));
Diego Barrios Romero 53159b
Diego Barrios Romero 0b9c51
		if(SynfigToolGeneralOptions::instance()->should_print_benchmarks())
Diego Barrios Romero 46977c
        {
a96363
            std::chrono::duration<double> duration =</double>
a96363
                std::chrono::system_clock::now() - start_timepoint;
Diego Barrios Romero baa5df
Diego Barrios Romero 46977c
            std::cout << job.filename.c_str()
Diego Barrios Romero baa5df
                      << _(": Rendered in ")
Diego Barrios Romero baa5df
                      << duration.count()
Diego Barrios Romero 46977c
                      << _(" seconds.") << std::endl;
Diego Barrios Romero 46977c
        }
Diego Barrios Romero 53159b
	}
Diego Barrios Romero 53159b
Diego Barrios Romero 46977c
	VERBOSE_OUT(1) << _("Done.") << std::endl;
Diego Barrios Romero 53159b
}
Yu Chen 99f412