Blame synfig-core/src/synfig/target_cairo.cpp

Carlos Lopez 33e2f1
/* === S Y N F I G ========================================================= */
Carlos Lopez de6bcb
/*!	\file target_cairo.cpp
Carlos Lopez 33e2f1
**	\brief Template File
Carlos Lopez 33e2f1
**
Carlos Lopez 33e2f1
**	$Id$
Carlos Lopez 33e2f1
**
Carlos Lopez 33e2f1
**	\legal
Carlos Lopez 33e2f1
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez e83454
**	Copyright (c) 2012-2013 Carlos Lรณpez
Carlos Lopez 33e2f1
**
Carlos Lopez 33e2f1
**	This package is free software; you can redistribute it and/or
Carlos Lopez 33e2f1
**	modify it under the terms of the GNU General Public License as
Carlos Lopez 33e2f1
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez 33e2f1
**	the License, or (at your option) any later version.
Carlos Lopez 33e2f1
**
Carlos Lopez 33e2f1
**	This package is distributed in the hope that it will be useful,
Carlos Lopez 33e2f1
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez 33e2f1
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez 33e2f1
**	General Public License for more details.
Carlos Lopez 33e2f1
**	\endlegal
Carlos Lopez 33e2f1
*/
Carlos Lopez 33e2f1
/* ========================================================================= */
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
/* === H E A D E R S ======================================================= */
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
#ifdef USING_PCH
Carlos Lopez 33e2f1
#	include "pch.h"
Carlos Lopez 33e2f1
#else
Carlos Lopez 33e2f1
#ifdef HAVE_CONFIG_H
Carlos Lopez 33e2f1
#	include <config.h></config.h>
Carlos Lopez 33e2f1
#endif
Carlos Lopez 33e2f1
Carlos Lopez de6bcb
#include "target_cairo.h"
bw 94d8a6
bw 94d8a6
#include "general.h"
bw 94d8a6
#include <synfig localization.h=""></synfig>
bw 94d8a6
Carlos Lopez 33e2f1
#include "canvas.h"
Carlos Lopez 33e2f1
#include "context.h"
bw 94d8a6
#include "render.h"
bw 94d8a6
#include "string.h"
bw 94d8a6
#include "surface.h"
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
#endif
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
/* === U S I N G =========================================================== */
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
using namespace std;
Carlos Lopez 33e2f1
using namespace etl;
Carlos Lopez 33e2f1
using namespace synfig;
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
/* === M A C R O S ========================================================= */
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
// note that if this isn't defined then the rendering is incorrect for
Carlos Lopez 33e2f1
// the straight blend method since the optimize_layers() function in
Carlos Lopez 33e2f1
// canvas.cpp which makes the straight blend method work correctly
Carlos Lopez 33e2f1
// isn't called.  ie. leave this defined.  to see the problem, draw a
Carlos Lopez 33e2f1
// small circle over a solid background.  set circle to amount 0.99
Carlos Lopez 33e2f1
// and blend method 'straight'.  the background should vanish but doesn't
Carlos Lopez 33e2f1
#define SYNFIG_OPTIMIZE_LAYER_TREE
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
/* === G L O B A L S ======================================================= */
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
/* === P R O C E D U R E S ================================================= */
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
/* === M E T H O D S ======================================================= */
Carlos Lopez 33e2f1
Carlos Lopez de6bcb
Target_Cairo::Target_Cairo()
Carlos Lopez 33e2f1
{
Carlos Lopez 33e2f1
	curr_frame_=0;
Carlos Lopez 33e2f1
}
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
int
Carlos Lopez de6bcb
Target_Cairo::next_frame(Time& time)
Carlos Lopez 33e2f1
{
Carlos Lopez 33e2f1
	return Target::next_frame(time);
Carlos Lopez 33e2f1
}
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
bool
Carlos Lopez de6bcb
synfig::Target_Cairo::render(ProgressCallback *cb)
Carlos Lopez 33e2f1
{
Carlos Lopez 33e2f1
	int
Carlos Lopez 33e2f1
		frames=0,
Carlos Lopez 33e2f1
		total_frames,
Carlos Lopez 33e2f1
		quality=get_quality(),
Carlos Lopez 33e2f1
		frame_start,
Carlos Lopez 33e2f1
		frame_end;
Carlos Lopez 33e2f1
	Time
Carlos Lopez 33e2f1
		t=0;
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
	assert(canvas);
Carlos Lopez 33e2f1
	curr_frame_=0;
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
	if( !init() ){
Carlos Lopez 33e2f1
		if(cb) cb->error(_("Target initialization failure"));
Carlos Lopez 33e2f1
		return false;
Carlos Lopez 33e2f1
	}
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
	frame_start=desc.get_frame_start();
Carlos Lopez 33e2f1
	frame_end=desc.get_frame_end();
Carlos Lopez 33e2f1
c74593
	ContextParams context_params(desc.get_render_excluded_contexts());
c74593
Carlos Lopez 33e2f1
	// Calculate the number of frames
Carlos Lopez 33e2f1
	total_frames=frame_end-frame_start+1;
Carlos Lopez 33e2f1
	if(total_frames<=0)total_frames=1;
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
	try {
Carlos Lopez 33e2f1
		do{
Carlos Lopez 33e2f1
			// Grab the time
Carlos Lopez 8fb44b
			if(total_frames>=1)
Carlos Lopez de6bcb
				frames=next_frame(t);
Carlos Lopez de6bcb
			else
Carlos Lopez de6bcb
				frames=0;
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
			// If we have a callback, and it returns
Carlos Lopez 33e2f1
			// false, go ahead and bail. (it may be a user cancel)
Carlos Lopez 33e2f1
			if(cb && !cb->amount_complete(total_frames-frames,total_frames))
Carlos Lopez 33e2f1
				return false;
Carlos Lopez c1fe21
			
Carlos Lopez c1fe21
			Context context;
Carlos Lopez c1fe21
			// pass the Render Method to the context
c74593
			context=canvas->get_context(context_params);
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
			// Set the time that we wish to render
c3039f
			if(!get_avoid_time_sync() || canvas->get_time()!=t) {
2ffd0a
				canvas->set_time(t);
c3039f
				canvas->load_resources(t);
c3039f
			}
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
	#ifdef SYNFIG_OPTIMIZE_LAYER_TREE
Carlos Lopez 33e2f1
			Canvas::Handle op_canvas;
Carlos Lopez 33e2f1
			if (!getenv("SYNFIG_DISABLE_OPTIMIZE_LAYER_TREE"))
Carlos Lopez 33e2f1
			{
Carlos Lopez 33e2f1
				op_canvas = Canvas::create();
Carlos Lopez 33e2f1
				op_canvas->set_file_name(canvas->get_file_name());
c74593
				optimize_layers(canvas->get_time(), canvas->get_context(context_params), op_canvas);
c74593
				context=op_canvas->get_context(context_params);
Carlos Lopez 33e2f1
			}
Carlos Lopez 33e2f1
			else
c74593
				context=canvas->get_context(context_params);
Carlos Lopez 33e2f1
	#else
Carlos Lopez 33e2f1
			context=canvas->get_context();
Carlos Lopez 33e2f1
	#endif
Carlos Lopez 623fab
			// Obtain a pointer to the cairo_surface_t given by the Target instance.
Carlos Lopez 623fab
			cairo_surface_t* surface;
Carlos Lopez 623fab
			if(obtain_surface(surface))
Carlos Lopez 33e2f1
			{
Carlos Lopez 413401
				cairo_t* cr=cairo_create(surface);
Carlos Lopez e0d481
				double tx=desc.get_tl()[0];
Carlos Lopez e0d481
				double ty=desc.get_tl()[1];
Carlos Lopez e0d481
				double sx=1.0/desc.get_pw();
Carlos Lopez e0d481
				double sy=1.0/desc.get_ph();
Carlos Lopez e0d481
				cairo_scale(cr, sx, sy);
Carlos Lopez e0d481
				cairo_translate(cr, -tx, -ty);
Carlos Lopez 413401
				if(!context.accelerated_cairorender(cr,quality,desc,cb))
Carlos Lopez 33e2f1
				{
Carlos Lopez de6bcb
					// For some reason, the accelerated renderer failed.
Carlos Lopez 3025c9
					if(cb)cb->error(_("Frame Renderer Failure"));
Carlos Lopez 413401
					cairo_destroy(cr);
Carlos Lopez de6bcb
					return false;
Carlos Lopez 33e2f1
				}
Carlos Lopez 33e2f1
				else
Carlos Lopez 33e2f1
				{
Carlos Lopez 4d328a
					// Put the surface we renderer onto the target's device.
luz.paz 99f3ef
					// and destroys cairo_surface_t
Carlos Lopez 413401
					if(!put_surface(cairo_surface_reference(surface), cb))
Carlos Lopez 33e2f1
					{
Carlos Lopez de6bcb
						if(cb)cb->error(_("Unable to put surface on target"));
Carlos Lopez 33e2f1
						return false;
Carlos Lopez 33e2f1
					}
Carlos Lopez 33e2f1
				}
Carlos Lopez 413401
				cairo_destroy(cr);
Carlos Lopez 33e2f1
			}
Carlos Lopez 33e2f1
			else
Carlos Lopez 33e2f1
			{
Carlos Lopez de6bcb
				if(cb)cb->error(_("Not supported render method"));
Carlos Lopez de6bcb
				return false;						
Carlos Lopez 33e2f1
			}
Carlos Lopez de6bcb
		}while(frames);
Carlos Lopez 33e2f1
	}
Rodolfo Ribeiro Gomes f89643
	catch(const String& str)
Carlos Lopez 33e2f1
	{
Firas Hanife b4b6a7
		if(cb)cb->error(_("Caught string: ")+str);
Carlos Lopez 33e2f1
		return false;
Carlos Lopez 33e2f1
	}
0d77fe
	catch(std::bad_alloc&)
Carlos Lopez 33e2f1
	{
Carlos Lopez 33e2f1
		if(cb)cb->error(_("Ran out of memory (Probably a bug)"));
Carlos Lopez 33e2f1
		return false;
Carlos Lopez 33e2f1
	}
Carlos Lopez 33e2f1
	catch(...)
Carlos Lopez 33e2f1
	{
Carlos Lopez 33e2f1
		if(cb)cb->error(_("Caught unknown error, rethrowing..."));
Carlos Lopez 33e2f1
		throw;
Carlos Lopez 33e2f1
	}
Carlos Lopez 33e2f1
	return true;
Carlos Lopez 33e2f1
}
Carlos Lopez 33e2f1
Carlos Lopez 33e2f1
bool
Carlos Lopez 623fab
Target_Cairo::put_surface(cairo_surface_t *surface, ProgressCallback *cb)
Carlos Lopez 33e2f1
{
Carlos Lopez 623fab
	if(cairo_surface_status(surface))
Carlos Lopez 4d328a
	{
Carlos Lopez 4d328a
		if(cb) cb->error(_("Cairo Surface bad status"));
Carlos Lopez 4d328a
		return false;
Carlos Lopez 4d328a
	}	
Carlos Lopez 623fab
	cairo_surface_flush(surface);
Carlos Lopez 623fab
	cairo_surface_destroy(surface);
Carlos Lopez 33e2f1
	return true;
Carlos Lopez 33e2f1
}
Carlos Lopez c03706
Carlos Lopez c03706
void
e198f4
Target_Cairo::gamma_filter(cairo_surface_t *surface, const synfig::Gamma &gamma)
Carlos Lopez c03706
{
Carlos Lopez 56e542
	CairoSurface cairo_s;
Carlos Lopez 56e542
	cairo_s.set_cairo_surface(surface);
Carlos Lopez 56e542
	cairo_s.map_cairo_image();
Carlos Lopez 56e542
	int w=cairo_s.get_w();
Carlos Lopez 56e542
	int h=cairo_s.get_h();
Carlos Lopez 56e542
	for(int y=0; y
Carlos Lopez 56e542
		for(int x=0; x
Carlos Lopez c03706
		{
Carlos Lopez 56e542
			CairoColor c=cairo_s[y][x];
23b4d4
			if (c.get_alpha()) {
a4bbdd
				float a = c.get_alpha();
a4bbdd
				c.set_r( (unsigned char)(a*gamma.apply_r(c.get_r()/a)) );
a4bbdd
				c.set_r( (unsigned char)(a*gamma.apply_g(c.get_g()/a)) );
a4bbdd
				c.set_r( (unsigned char)(a*gamma.apply_b(c.get_b()/a)) );
23b4d4
			}
Carlos Lopez 56e542
			cairo_s[y][x]=c;
Carlos Lopez c03706
		}
Carlos Lopez 56e542
	cairo_s.unmap_cairo_image();
Carlos Lopez c03706
}