Blame synfig-core/src/synfig/layers/layer_bitmap.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file layer_bitmap.cpp
Carlos Lopez a09598
**	\brief Template Header
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez e83454
**	Copyright (c) 2012-2013 Carlos Lรณpez
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
*/
Carlos Lopez a09598
/* ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef USING_PCH
Carlos Lopez a09598
#	include "pch.h"
Carlos Lopez a09598
#else
Carlos Lopez a09598
#ifdef HAVE_CONFIG_H
Carlos Lopez a09598
#	include <config.h></config.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
88c541
#include <etl misc=""></etl>
88c541
Carlos Lopez a09598
#include "layer_bitmap.h"
88c541
Diego Barrios Romero dcc14d
#include <synfig time.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig string.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig vector.h=""></synfig>
Diego Barrios Romero dcc14d
Diego Barrios Romero dcc14d
#include <synfig context.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig time.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig color.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig surface.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig renddesc.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig target.h=""></synfig>
Diego Barrios Romero dcc14d
Diego Barrios Romero dcc14d
#include <synfig general.h=""></synfig>
bw 94d8a6
#include <synfig localization.h=""></synfig>
Diego Barrios Romero dcc14d
#include <synfig paramdesc.h=""></synfig>
88c541
3bd54c
#include <synfig rendering="" software="" surfacesw.h=""></synfig>
3bd54c
#include <synfig rendering="" software="" surfaceswpacked.h=""></synfig>
5e2754
#include <synfig common="" rendering="" task="" tasktransformation.h=""></synfig>
5e2754
#include <synfig common="" rendering="" task="" taskpixelprocessor.h=""></synfig>
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* === U S I N G =========================================================== */
Carlos Lopez a09598
Carlos Lopez a09598
using namespace synfig;
Carlos Lopez a09598
using namespace std;
Carlos Lopez a09598
using namespace etl;
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === P R O C E D U R E S ================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === M E T H O D S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
synfig::Layer_Bitmap::Layer_Bitmap():
ab539e
	Layer_Composite	(1.0,Color::BLEND_COMPOSITE),
0dde72
	surface_modification_id (0),
0dde72
	param_tl                (Point(-0.5,0.5)),
0dde72
	param_br                (Point(0.5,-0.5)),
0dde72
	param_c                 (int(1)),
0dde72
	param_gamma_adjust      (Real(1.0)),
ab539e
	trimmed                 (false),
25c3c3
	left                    (0),
25c3c3
	top                     (0),
25c3c3
	width                   (0),
25c3c3
	height                  (0)
Carlos Lopez a09598
{
Carlos Lopez b3fcc2
	SET_INTERPOLATION_DEFAULTS();
Carlos Lopez b3fcc2
	SET_STATIC_DEFAULTS();
Carlos Lopez a09598
}
Carlos Lopez a09598
62f661
/*
3bd54c
synfig::Surface&
3bd54c
Layer_Bitmap::get_surface() const
3bd54c
{
luz.paz 99f3ef
	// TODO: not thread safe, return SurfaceResource instead
5e2754
	if (!rendering_surface || !rendering_surface->is_exists())
5e2754
		rendering_surface->create(128, 128);
5e2754
	rendering::SurfaceResource::LockWrite<rendering::surfacesw> lock(rendering_surface);</rendering::surfacesw>
5e2754
	return lock->get_surface();
3bd54c
}
62f661
*/
3bd54c
Carlos Lopez a09598
bool
Diego Barrios Romero a6f9bd
synfig::Layer_Bitmap::set_param(const String & param, const ValueBase & value)
Carlos Lopez a09598
{
Carlos Lopez fc08b3
	IMPORT_VALUE(param_tl);
Carlos Lopez fc08b3
	IMPORT_VALUE(param_br);
Carlos Lopez fc08b3
	IMPORT_VALUE(param_c);
Carlos Lopez fe419e
	IMPORT_VALUE_PLUS(param_gamma_adjust,
d89783
		if(param=="gamma_adjust"&& value.get_type()==type_real)
Carlos Lopez fe419e
		{
Carlos Lopez fe419e
			param_gamma_adjust.set(Real(1.0/value.get(Real())));
Carlos Lopez fe419e
			return true;
Carlos Lopez fe419e
		}
Carlos Lopez fe419e
		);
Carlos Lopez a09598
Carlos Lopez a09598
	return Layer_Composite::set_param(param,value);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueBase
Carlos Lopez a09598
synfig::Layer_Bitmap::get_param(const String & param)const
Carlos Lopez a09598
{
Carlos Lopez fc08b3
	EXPORT_VALUE(param_tl);
Carlos Lopez fc08b3
	EXPORT_VALUE(param_br);
Carlos Lopez fc08b3
	EXPORT_VALUE(param_c);
Carlos Lopez a09598
	if(param=="gamma_adjust")
Carlos Lopez e8ab69
	{
Carlos Lopez fe419e
		ValueBase ret=param_gamma_adjust;
Carlos Lopez fe419e
		ret.set(1.0/param_gamma_adjust.get(Real()));
Carlos Lopez e8ab69
		return ret;
Carlos Lopez e8ab69
	}
Carlos Lopez a09598
Carlos Lopez a09598
	if(param=="_width")
Carlos Lopez a09598
	{
d89783
		ValueBase ret1(type_integer);
Carlos Lopez e8ab69
		ret1=int(width);
d89783
		ValueBase ret2(type_integer);
3bd54c
		ret2=int(rendering_surface ? rendering_surface->get_width() : 0);
Carlos Lopez e8ab69
		if (trimmed) return ret1;
Carlos Lopez e8ab69
		return ret2;
Carlos Lopez a09598
	}
Carlos Lopez a09598
	if(param=="_height")
Carlos Lopez a09598
	{
d89783
		ValueBase ret1(type_integer);
Carlos Lopez e8ab69
		ret1=int(height);
d89783
		ValueBase ret2(type_integer);
3bd54c
		ret2=int(rendering_surface ? rendering_surface->get_height() : 0);
Carlos Lopez e8ab69
		if (trimmed) return ret1;
Carlos Lopez e8ab69
		return ret2;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	return Layer_Composite::get_param(param);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez fc08b3
Carlos Lopez a09598
Layer::Vocab
Carlos Lopez a09598
Layer_Bitmap::get_param_vocab()const
Carlos Lopez a09598
{
Carlos Lopez a09598
	Layer::Vocab ret(Layer_Composite::get_param_vocab());
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("tl")
Carlos Lopez a09598
		.set_local_name(_("Top-Left"))
Carlos Lopez a09598
		.set_description(_("Upper left-hand Corner of image"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("br")
Carlos Lopez a09598
		.set_local_name(_("Bottom-Right"))
Carlos Lopez a09598
		.set_description(_("Lower right-hand Corner of image"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("c")
Carlos Lopez a09598
		.set_local_name(_("Interpolation"))
Carlos Lopez a09598
		.set_description(_("What type of interpolation to use"))
Carlos Lopez a09598
		.set_hint("enum")
Carlos Lopez a09598
		.add_enum_value(0,"nearest",_("Nearest Neighbor"))
Carlos Lopez a09598
		.add_enum_value(1,"linear",_("Linear"))
Carlos Lopez a09598
		.add_enum_value(2,"cosine",_("Cosine"))
Carlos Lopez a09598
		.add_enum_value(3,"cubic",_("Cubic"))
Carlos Lopez fc08b3
		.set_static(true)
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("gamma_adjust")
Carlos Lopez a09598
		.set_local_name(_("Gamma Adjustment"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	return ret;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
synfig::Layer::Handle
Carlos Lopez a09598
Layer_Bitmap::hit_check(synfig::Context context, const synfig::Point &pos)const
Carlos Lopez a09598
{
Carlos Lopez fc08b3
	Point tl(param_tl.get(Point()));
Carlos Lopez fc08b3
	Point br(param_br.get(Point()));
Carlos Lopez a09598
	Point surface_pos;
Carlos Lopez a09598
	surface_pos=pos-tl;
Carlos Lopez a09598
Carlos Lopez a09598
	surface_pos[0]/=br[0]-tl[0];
Carlos Lopez a09598
	if(surface_pos[0]<=1.0 && surface_pos[0]>=0.0)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		surface_pos[1]/=br[1]-tl[1];
Carlos Lopez a09598
		if(surface_pos[1]<=1.0 && surface_pos[1]>=0.0)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			return const_cast<layer_bitmap*>(this);</layer_bitmap*>
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	return context.hit_check(pos);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
inline
Carlos Lopez a09598
const Color&
Carlos Lopez a09598
synfig::Layer_Bitmap::filter(Color& x)const
Carlos Lopez a09598
{
Carlos Lopez fc08b3
	Real gamma_adjust(param_gamma_adjust.get(Real()));
Carlos Lopez a09598
	if(gamma_adjust!=1.0)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		x.set_r(powf((float)x.get_r(),gamma_adjust));
Carlos Lopez a09598
		x.set_g(powf((float)x.get_g(),gamma_adjust));
Carlos Lopez a09598
		x.set_b(powf((float)x.get_b(),gamma_adjust));
23b4d4
		x.set_a(powf((float)x.get_a(),gamma_adjust));
Carlos Lopez a09598
	}
Carlos Lopez a09598
	return x;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez 8bd3dc
inline
Carlos Lopez 8bd3dc
const CairoColor&
Carlos Lopez 8bd3dc
synfig::Layer_Bitmap::filter(CairoColor& x)const
Carlos Lopez 8bd3dc
{
Carlos Lopez fc08b3
	Real gamma_adjust(param_gamma_adjust.get(Real()));
Carlos Lopez 8bd3dc
	if(gamma_adjust!=1.0)
Carlos Lopez 8bd3dc
	{
Carlos Lopez 58d578
		x.set_r(powf((float)(x.get_r()/CairoColor::range),gamma_adjust)*CairoColor::range);
Carlos Lopez 58d578
		x.set_g(powf((float)(x.get_g()/CairoColor::range),gamma_adjust)*CairoColor::range);
Carlos Lopez 58d578
		x.set_b(powf((float)(x.get_b()/CairoColor::range),gamma_adjust)*CairoColor::range);
23b4d4
		x.set_a(powf((float)(x.get_a()/CairoColor::range),gamma_adjust)*CairoColor::range);
Carlos Lopez 8bd3dc
	}
Carlos Lopez 8bd3dc
	return x;
Carlos Lopez 8bd3dc
}
Carlos Lopez 8bd3dc
Carlos Lopez 8bd3dc
Carlos Lopez a09598
Color
Carlos Lopez a09598
synfig::Layer_Bitmap::get_color(Context context, const Point &pos)const
Carlos Lopez a09598
{
Carlos Lopez fc08b3
	Point tl(param_tl.get(Point()));
Carlos Lopez fc08b3
	Point br(param_br.get(Point()));
Carlos Lopez fc08b3
	int c(param_c.get(int()));
Carlos Lopez fc08b3
Carlos Lopez a09598
	Point surface_pos;
Carlos Lopez a09598
5e2754
	if(!get_amount() || !rendering_surface || !rendering_surface->is_exists())
Carlos Lopez a09598
		return context.get_color(pos);
Carlos Lopez a09598
Carlos Lopez a09598
	surface_pos=pos-tl;
3bd54c
	int w = rendering_surface->get_width();
3bd54c
	int h = rendering_surface->get_height();
Carlos Lopez a09598
Carlos Lopez a09598
	surface_pos[0]/=br[0]-tl[0];
Carlos Lopez a09598
	if(surface_pos[0]<=1.0 && surface_pos[0]>=0.0)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		surface_pos[1]/=br[1]-tl[1];
Carlos Lopez a09598
		if(surface_pos[1]<=1.0 && surface_pos[1]>=0.0)
Carlos Lopez a09598
		{
7cf47e
			Mutex::Lock lock(mutex);
7cf47e
Carlos Lopez a09598
			if (trimmed)
Carlos Lopez a09598
			{
Carlos Lopez a09598
				surface_pos[0]*=width;
Carlos Lopez a09598
				surface_pos[1]*=height;
Carlos Lopez a09598
3bd54c
				if (surface_pos[0] > left+w || surface_pos[0] < left || surface_pos[1] > top+h || surface_pos[1] < top)
Carlos Lopez a09598
					return context.get_color(pos);
Carlos Lopez a09598
Carlos Lopez a09598
				surface_pos[0] -= left;
Carlos Lopez a09598
				surface_pos[1] -= top;
Carlos Lopez a09598
			}
Carlos Lopez a09598
			else
Carlos Lopez a09598
			{
3bd54c
				surface_pos[0]*=w;
3bd54c
				surface_pos[1]*=h;
Carlos Lopez a09598
			}
Carlos Lopez a09598
Carlos Lopez a09598
			Color ret(Color::alpha());
Carlos Lopez a09598
62f661
			rendering::SurfaceResource::LockReadBase lsurf(rendering_surface);
62f661
			if (lsurf.convert<rendering::surfaceswpacked>(false))</rendering::surfaceswpacked>
Carlos Lopez a09598
			{
62f661
				typedef rendering::software::PackedSurface PackedSurface;
62f661
				typedef PackedSurface::Sampler Sampler;
Carlos Lopez a09598
62f661
				assert(lsurf.get_handle().type_is<rendering::surfaceswpacked>());</rendering::surfaceswpacked>
62f661
				reader.open( lsurf.cast<rendering::surfaceswpacked>()->get_surface() );</rendering::surfaceswpacked>
3bd54c
				switch(c)
Carlos Lopez a09598
				{
3bd54c
				case 6:	// Undefined
3bd54c
				case 5:	// Undefined
3bd54c
				case 4:	// Undefined
3bd54c
				case 3:	// Cubic
17812b
					ret = ColorPrep::uncook_static(Sampler::cubic_sample(&reader, surface_pos[0], surface_pos[1]));
3bd54c
					break;
3bd54c
				case 2:	// Cosine
17812b
					ret = ColorPrep::uncook_static(Sampler::cosine_sample(&reader, surface_pos[0], surface_pos[1]));
3bd54c
					break;
3bd54c
				case 1:	// Linear
17812b
					ret = ColorPrep::uncook_static(Sampler::linear_sample(&reader, surface_pos[0], surface_pos[1]));
3bd54c
					break;
3bd54c
				case 0:	// Nearest Neighbor
3bd54c
				default:
17812b
					ret = ColorPrep::uncook_static(Sampler::nearest_sample(&reader, surface_pos[0], surface_pos[1]));
17812b
					break;
3bd54c
				break;
Carlos Lopez a09598
				}
Carlos Lopez a09598
			}
Carlos Lopez a09598
			else
62f661
			if (lsurf.convert<rendering::surfacesw>())</rendering::surfacesw>
Carlos Lopez 036645
			{
62f661
				assert(lsurf.get_handle().type_is<rendering::surfacesw>());</rendering::surfacesw>
62f661
				const Surface &surface = lsurf.cast<rendering::surfacesw>()->get_surface();</rendering::surfacesw>
3bd54c
				switch(c)
3bd54c
				{
Carlos Lopez 036645
				case 6:	// Undefined
Carlos Lopez 036645
				case 5:	// Undefined
Carlos Lopez 036645
				case 4:	// Undefined
Carlos Lopez 036645
				case 3:	// Cubic
3bd54c
					ret=surface.cubic_sample(surface_pos[0],surface_pos[1]);
Carlos Lopez 036645
					break;
3bd54c
Carlos Lopez 036645
				case 2:	// Cosine
3bd54c
					ret=surface.cosine_sample(surface_pos[0],surface_pos[1]);
Carlos Lopez 036645
					break;
Carlos Lopez 036645
				case 1:	// Linear
3bd54c
					ret=surface.linear_sample(surface_pos[0],surface_pos[1]);
Carlos Lopez 036645
					break;
Carlos Lopez 036645
				case 0:	// Nearest Neighbor
Carlos Lopez 036645
				default:
3bd54c
					{
3bd54c
						int x(min(w-1,max(0,round_to_int(surface_pos[0]))));
3bd54c
						int y(min(h-1,max(0,round_to_int(surface_pos[1]))));
3bd54c
						ret= surface[y][x];
3bd54c
					}
3bd54c
				break;
Carlos Lopez 036645
				}
Carlos Lopez 036645
			}
3bd54c
Carlos Lopez 65106c
			ret=filter(ret);
3bd54c
Carlos Lopez 036645
			if(get_amount()==1 && get_blend_method()==Color::BLEND_STRAIGHT)
Carlos Lopez 036645
				return ret;
Carlos Lopez 036645
			else
3bd54c
				return Color::blend(ret,context.get_color(pos),get_amount(),get_blend_method());
Carlos Lopez 036645
		}
Carlos Lopez 036645
	}
3bd54c
3bd54c
	return context.get_color(pos);
3bd54c
}
3bd54c
3bd54c
3bd54c
CairoColor
3bd54c
synfig::Layer_Bitmap::get_cairocolor(Context context, const Point &pos)const
3bd54c
{
3bd54c
	// no cairo implementation
Carlos Lopez 036645
	return context.get_cairocolor(pos);
Carlos Lopez 036645
}
Carlos Lopez 036645
Carlos Lopez 036645
Carlos Lopez a09598
bool
813bad
Layer_Bitmap::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)  const
Carlos Lopez a09598
{
18e76e
	RENDER_TRANSFORMED_IF_NEED(__FILE__, __LINE__)
813bad
7cf47e
	Mutex::Lock lock(mutex);
7cf47e
Carlos Lopez fc08b3
	Point tl(param_tl.get(Point()));
Carlos Lopez fc08b3
	Point br(param_br.get(Point()));
Carlos Lopez fc08b3
	int c(param_c.get(int()));
Carlos Lopez fc08b3
	Real gamma_adjust(param_gamma_adjust.get(Real()));
Carlos Lopez fc08b3
62f661
	rendering::SurfaceResource::LockRead<rendering::surfacesw> lsurf(rendering_surface);</rendering::surfacesw>
62f661
	if (!lsurf)
62f661
		return false;
62f661
	Surface &layer_surface = lsurf.cast<rendering::surfacesw>()->get_surface(); // const cast</rendering::surfacesw>
3bd54c
Carlos Lopez a09598
	int interp=c;
Carlos Lopez a09598
	if(quality>=10)
Carlos Lopez a09598
		interp=0;
Carlos Lopez a09598
	else if(quality>=5 && interp>1)
Carlos Lopez a09598
		interp=1;
Carlos Lopez a09598
Carlos Lopez a09598
	// We can only handle NN and Linear at the moment
Carlos Lopez a09598
	//if(interp>1)
813bad
	//	return Layer_Composite::accelerated_render(context,surface,quality,renddesc,cb);
Carlos Lopez a09598
Carlos Lopez a09598
	//if we don't actually have a valid surface just skip us
3bd54c
	if(!layer_surface.is_valid())
Carlos Lopez a09598
	{
Carlos Lopez a09598
		// Render what is behind us
813bad
		return context.accelerated_render(surface,quality,renddesc,cb);
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	SuperCallback subcb(cb,1,10000,10001+renddesc.get_h());
Carlos Lopez a09598
Carlos Lopez a09598
	if(	get_amount()==1 &&
Carlos Lopez a09598
		get_blend_method()==Color::BLEND_STRAIGHT &&
Carlos Lopez a09598
		!trimmed &&
Carlos Lopez a09598
		renddesc.get_tl()==tl &&
Carlos Lopez a09598
		renddesc.get_br()==br)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		// Check for the trivial case
3bd54c
		if(layer_surface.get_w()==renddesc.get_w() && layer_surface.get_h()==renddesc.get_h() && gamma_adjust==1.0f)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			if(cb && !cb->amount_complete(0,100)) return false;
3bd54c
			*surface=layer_surface;
Carlos Lopez a09598
			if(cb && !cb->amount_complete(100,100)) return false;
Carlos Lopez a09598
			return true;
Carlos Lopez a09598
		}
813bad
		surface->set_wh(renddesc.get_w(),renddesc.get_h());
Carlos Lopez a09598
	}
Carlos Lopez a09598
	else
Carlos Lopez a09598
	{
Carlos Lopez a09598
		// Render what is behind us
813bad
		if(!context.accelerated_render(surface,quality,renddesc,&subcb))
Carlos Lopez a09598
			return false;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
Carlos Lopez a09598
Carlos Lopez a09598
	Point	obr	= renddesc.get_br(),
Carlos Lopez a09598
			otl = renddesc.get_tl();
Carlos Lopez a09598
Carlos Lopez a09598
	//Vector::value_type pw=renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
Carlos Lopez a09598
	//Vector::value_type ph=renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
Carlos Lopez a09598
Carlos Lopez a09598
	//A = representation of input (just tl,br) 	//just a scaling right now
Carlos Lopez a09598
	//B = representation of output (just tl,br)	//just a scaling right now
Carlos Lopez a09598
	//sa = scaling for input (0,1) -> (0,w/h)
Carlos Lopez a09598
	//sb = scaling for output (0,1) -> (0,w/h)
Carlos Lopez a09598
Carlos Lopez a09598
	float	outwf = obr[0] - otl[0];
Carlos Lopez a09598
	float	outhf = obr[1] - otl[1];
Carlos Lopez a09598
3bd54c
	int		inw = layer_surface.get_w();
3bd54c
	int		inh = layer_surface.get_h();
Carlos Lopez a09598
Carlos Lopez a09598
	int		outw = renddesc.get_w();
Carlos Lopez a09598
	int		outh = renddesc.get_h();
Carlos Lopez a09598
Carlos Lopez a09598
	float	inwf, inhf;
Carlos Lopez a09598
	Point	itl, ibr;
Carlos Lopez a09598
Carlos Lopez a09598
	if (trimmed)
Carlos Lopez a09598
	{
3bd54c
		inwf = (br[0] - tl[0])*layer_surface.get_w()/width;
3bd54c
		inhf = (br[1] - tl[1])*layer_surface.get_h()/height;
Carlos Lopez a09598
		itl = Point(tl[0] + (br[0]-tl[0])*left/width,
Carlos Lopez a09598
					tl[1] + (br[1]-tl[1])*top/height);
Carlos Lopez a09598
		ibr = Point(tl[0] + (br[0]-tl[0])*(left+inw)/width,
Carlos Lopez a09598
					tl[1] + (br[1]-tl[1])*(top+inh)/height);
Carlos Lopez a09598
	}
Carlos Lopez a09598
	else
Carlos Lopez a09598
	{
Carlos Lopez a09598
		inwf = br[0] - tl[0];
Carlos Lopez a09598
		inhf = br[1] - tl[1];
Carlos Lopez a09598
		itl = tl;
Carlos Lopez a09598
		ibr = br;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	//need to get the input coords in output space, so we can clip
Carlos Lopez a09598
Carlos Lopez a09598
	//get the desired corners of the bitmap (in increasing order) in integers
Carlos Lopez a09598
	//floating point corners
Carlos Lopez a09598
	float x1f = (itl[0] - otl[0])*outw/outwf;
Carlos Lopez a09598
	float x2f = (ibr[0] - otl[0])*outw/outwf;
Carlos Lopez a09598
	float y1f = (itl[1] - otl[1])*outh/outhf;
Carlos Lopez a09598
	float y2f = (ibr[1] - otl[1])*outh/outhf;
Carlos Lopez a09598
Carlos Lopez a09598
	if(x1f > x2f) swap(x1f,x2f);
Carlos Lopez a09598
	if(y1f > y2f) swap(y1f,y2f);
Carlos Lopez a09598
Carlos Lopez a09598
	int x_start = max(0,(int)floor(x1f));	//probably floor
Carlos Lopez a09598
	int x_end 	= min(outw,(int)ceil(x2f));	//probably ceil
Carlos Lopez a09598
	int y_start = max(0,(int)floor(y1f));	//probably floor
Carlos Lopez a09598
	int y_end	= min(outh,(int)ceil(y2f));	//probably ceil
Carlos Lopez a09598
Carlos Lopez a09598
	//need to get the x,y,dx,dy values from output space to input, so we can do fast interpolation
Carlos Lopez a09598
Carlos Lopez a09598
	//get the starting position in input space... for interpolating
Carlos Lopez a09598
Carlos Lopez a09598
	// in int -> out float:
Carlos Lopez a09598
	// Sb(B^-1)A(Sa^-1) x
Carlos Lopez a09598
	float inx_start = (((x_start/*+0.5f*/)*outwf/outw + otl[0]) - itl[0])*inw/inwf; //may want to bias this (center of pixel)???
Carlos Lopez a09598
	float iny_start = (((y_start/*+0.5f*/)*outhf/outh + otl[1]) - itl[1])*inh/inhf; //may want to bias this (center of pixel)???
Carlos Lopez a09598
Carlos Lopez a09598
	//calculate the delta values in input space for one pixel movement in output space
Carlos Lopez a09598
	//same matrix but with a vector instead of a point...
Carlos Lopez a09598
	float indx = outwf*(inw)/((outw)*inwf);		//translations died
Carlos Lopez a09598
	float indy = outhf*(inh)/((outh)*inhf);		//translations died
Carlos Lopez a09598
Carlos Lopez a09598
	//perhaps use a DDA algorithm... if faster...
Carlos Lopez a09598
	//   will still want pixel fractions to be floating point since colors are
Carlos Lopez a09598
Carlos Lopez a09598
	//synfig::info("xstart:%d ystart:%d xend:%d yend:%d",x_start,y_start,x_end,y_end);
Carlos Lopez a09598
Carlos Lopez a09598
	//start drawing at the start of the bitmap (either origin or corner of input...)
Carlos Lopez a09598
	//and get other info
813bad
	Surface::alpha_pen pen(surface->get_pen(x_start,y_start));
Carlos Lopez a09598
	pen.set_alpha(get_amount());
Carlos Lopez a09598
	pen.set_blend_method(get_blend_method());
Carlos Lopez a09598
Carlos Lopez a09598
	//perform normal interpolation
Carlos Lopez a09598
	if(interp==0)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		//synfig::info("Decided to do nearest neighbor");
Carlos Lopez a09598
		float iny, inx;
Carlos Lopez a09598
		int x,y;
Carlos Lopez a09598
Carlos Lopez a09598
		//Point sample - truncate
Carlos Lopez a09598
		iny = iny_start;//+0.5f;
Carlos Lopez a09598
		for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			inx = inx_start;//+0.5f;
Carlos Lopez a09598
			int yclamp = min(inh-1, max(0, round_to_int(iny)));
Carlos Lopez a09598
			for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
Carlos Lopez a09598
			{
Carlos Lopez a09598
				int xclamp = min(inw-1, max(0, round_to_int(inx)));
3bd54c
				Color c = filter(layer_surface[yclamp][xclamp]);
Carlos Lopez a09598
				pen.put_value(c); //must get rid of the clip
Carlos Lopez a09598
			}
Carlos Lopez a09598
			pen.dec_x(x_end-x_start);
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
	else
Carlos Lopez a09598
	if(interp==1)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		//bilinear filtering
Carlos Lopez a09598
Carlos Lopez a09598
		//float 	xmf,xpf,ymf,ypf;
Carlos Lopez a09598
		//int		xm,xp,ym,yp;
Carlos Lopez a09598
		float 	inx,iny;
Carlos Lopez a09598
		int		x,y;
Carlos Lopez a09598
Carlos Lopez a09598
		//can probably buffer for x values...
Carlos Lopez a09598
Carlos Lopez a09598
		//loop and based on inx,iny sample input image
Carlos Lopez a09598
		iny = iny_start;
Carlos Lopez a09598
		for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			inx = inx_start;
Carlos Lopez a09598
			for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
Carlos Lopez a09598
			{
3bd54c
				Color col(layer_surface.linear_sample(inx,iny));
Carlos Lopez a09598
				pen.put_value(filter(col));
Carlos Lopez a09598
			}
Carlos Lopez a09598
			pen.dec_x(x_end-x_start);
Carlos Lopez a09598
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
	else
Carlos Lopez a09598
	if(interp==2)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		//cosine filtering
Carlos Lopez a09598
Carlos Lopez a09598
		//float 	xmf,xpf,ymf,ypf;
Carlos Lopez a09598
		//int		xm,xp,ym,yp;
Carlos Lopez a09598
		float 	inx,iny;
Carlos Lopez a09598
		int		x,y;
Carlos Lopez a09598
Carlos Lopez a09598
		//can probably buffer for x values...
Carlos Lopez a09598
Carlos Lopez a09598
		//loop and based on inx,iny sample input image
Carlos Lopez a09598
		iny = iny_start;
Carlos Lopez a09598
		for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			inx = inx_start;
Carlos Lopez a09598
			for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
Carlos Lopez a09598
			{
3bd54c
				Color col(layer_surface.cosine_sample(inx,iny));
Carlos Lopez a09598
				pen.put_value(filter(col));
Carlos Lopez a09598
			}
Carlos Lopez a09598
			pen.dec_x(x_end-x_start);
Carlos Lopez a09598
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
	else
Carlos Lopez a09598
	{
Carlos Lopez a09598
		//cubic filtering
Carlos Lopez a09598
Carlos Lopez a09598
		//float 	xmf,xpf,ymf,ypf;
Carlos Lopez a09598
		//int		xm,xp,ym,yp;
Carlos Lopez a09598
		float 	inx,iny;
Carlos Lopez a09598
		int		x,y;
Carlos Lopez a09598
Carlos Lopez a09598
		//can probably buffer for x values...
Carlos Lopez a09598
Carlos Lopez a09598
		//loop and based on inx,iny sample input image
Carlos Lopez a09598
		iny = iny_start;
Carlos Lopez a09598
		for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			inx = inx_start;
Carlos Lopez a09598
			for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
Carlos Lopez a09598
			{
3bd54c
				Color col(layer_surface.cubic_sample(inx,iny));
Carlos Lopez a09598
				pen.put_value(filter(col));
Carlos Lopez a09598
			}
Carlos Lopez a09598
			pen.dec_x(x_end-x_start);
Carlos Lopez a09598
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez 8bd3dc
/////
Carlos Lopez 0595a1
/////
Carlos Lopez 0595a1
Carlos Lopez 0595a1
bool
Carlos Lopez 0595a1
Layer_Bitmap::accelerated_cairorender(Context context, cairo_t *cr, int quality, const RendDesc &renddesc, ProgressCallback *cb)  const
Carlos Lopez 0595a1
{
3bd54c
	// no cairo implementation
3bd54c
	return context.accelerated_cairorender(cr,quality,renddesc,cb);
Carlos Lopez 0595a1
}
Carlos Lopez 0595a1
Carlos Lopez 0595a1
/////
Carlos Lopez 8bd3dc
Carlos Lopez 8bd3dc
Carlos Lopez a09598
Rect
Carlos Lopez a09598
Layer_Bitmap::get_bounding_rect()const
Carlos Lopez a09598
{
Carlos Lopez fc08b3
	Point tl(param_tl.get(Point()));
Carlos Lopez fc08b3
	Point br(param_br.get(Point()));
Carlos Lopez fc08b3
Carlos Lopez a09598
	return Rect(tl,br);
Carlos Lopez a09598
}
Carlos Lopez 1f5c8f
Carlos Lopez 1f5c8f
88c541
rendering::Task::Handle
6f6bb5
Layer_Bitmap::build_composite_task_vfunc(ContextParams /* context_params */) const
88c541
{
88c541
	if ( !rendering_surface
5e2754
	  || !rendering_surface->is_exists() )
ce743b
		return rendering::Task::Handle();
5e2754
5e2754
	ColorReal gamma = (Color::value_type)param_gamma_adjust.get(Real());
5e2754
	Point tl(param_tl.get(Point()));
5e2754
	Point br(param_br.get(Point()));
093a8b
	Matrix m;
5e2754
	m.m00 = (br[0] - tl[0]); m.m20 = tl[0];
5e2754
	m.m11 = (br[1] - tl[1]); m.m21 = tl[1];
5e2754
5e2754
	rendering::Task::Handle task;
88c541
88c541
	rendering::TaskSurface::Handle task_surface(new rendering::TaskSurface());
88c541
	task_surface->target_surface = rendering_surface;
5e2754
	task_surface->target_rect = RectInt(VectorInt(), rendering_surface->get_size());
5e2754
	task_surface->source_rect = Rect(0.0, 0.0, 1.0, 1.0);
5e2754
	task = task_surface;
5e2754
5e2754
	rendering::TaskTransformationAffine::Handle task_transform = new rendering::TaskTransformationAffine();
5e2754
	task_transform->interpolation = (Color::Interpolation)param_c.get(int());
5e2754
	task_transform->transformation->matrix = m;
5e2754
	task_transform->sub_task() = task;
5e2754
	task = task_transform;
5e2754
5e2754
	rendering::TaskPixelGamma::Handle task_gamma = new rendering::TaskPixelGamma();
a4bbdd
	task_gamma->gamma = get_canvas()->get_root()->rend_desc().get_gamma() / gamma;
a4bbdd
	task_gamma->sub_task() = task;
a4bbdd
	if (!task_gamma->is_transparent())
5e2754
		task = task_gamma;
5e2754
5e2754
	return task;
88c541
}