Blame synfig-core/src/modules/lyr_std/warp.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file warp.cpp
Carlos Lopez a09598
**	\brief Implementation of the "Warp" layer
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 a09598
**	Copyright (c) 2007, 2008 Chris Moore
Carlos Lopez e83454
**	Copyright (c) 2011-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
** === N O T E S ===========================================================
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
cf1df9
#include <etl misc=""></etl>
bw 94d8a6
bw 94d8a6
#include <synfig localization.h=""></synfig>
bw 94d8a6
#include <synfig general.h=""></synfig>
bw 94d8a6
Carlos Lopez a09598
#include <synfig string.h=""></synfig>
Carlos Lopez a09598
#include <synfig time.h=""></synfig>
Carlos Lopez a09598
#include <synfig context.h=""></synfig>
Carlos Lopez a09598
#include <synfig paramdesc.h=""></synfig>
Carlos Lopez a09598
#include <synfig renddesc.h=""></synfig>
Carlos Lopez a09598
#include <synfig surface.h=""></synfig>
Carlos Lopez a09598
#include <synfig value.h=""></synfig>
Carlos Lopez a09598
#include <synfig valuenode.h=""></synfig>
Carlos Lopez a09598
#include <synfig transform.h=""></synfig>
Carlos Lopez ad6565
#include <synfig cairo_renddesc.h=""></synfig>
cf1df9
cf1df9
#include <synfig common="" rendering="" task="" tasktransformation.h=""></synfig>
cf1df9
#include <synfig common="" rendering="" task="" taskcontour.h=""></synfig>
cf1df9
#include <synfig common="" rendering="" task="" taskblend.h=""></synfig>
cf1df9
#include <synfig rendering="" software="" task="" tasksw.h=""></synfig>
cf1df9
cf1df9
#include "warp.h"
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
9f3c68
using namespace synfig;
144d3f
using namespace modules;
9f3c68
using namespace lyr_std;
9f3c68
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
SYNFIG_LAYER_INIT(Warp);
Carlos Lopez a09598
SYNFIG_LAYER_SET_NAME(Warp,"warp");
Carlos Lopez a09598
SYNFIG_LAYER_SET_LOCAL_NAME(Warp,N_("Warp"));
Carlos Lopez a09598
SYNFIG_LAYER_SET_CATEGORY(Warp,N_("Distortions"));
9abc9e
SYNFIG_LAYER_SET_VERSION(Warp,"0.2");
Carlos Lopez a09598
SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id$");
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
/* === E N T R Y P O I N T ================================================= */
Carlos Lopez a09598
cf1df9
namespace {
cf1df9
	bool truncate_line(
cf1df9
		Point *out_points,
cf1df9
		const Rect &bounds,
cf1df9
		Real a,
cf1df9
		Real b,
cf1df9
		Real c )
cf1df9
	{
cf1df9
		// equatation of line is a*x + b*y + c = 0
cf1df9
		
cf1df9
		if (!bounds.valid()) return false;
cf1df9
		
cf1df9
		int count = 0;
cf1df9
		
cf1df9
		if (approximate_not_zero(a)) {
cf1df9
			const Real x0 = -(c + bounds.miny*b)/a;
cf1df9
			if ( approximate_greater_or_equal(x0, bounds.minx)
cf1df9
			  && approximate_less_or_equal   (x0, bounds.maxx) )
cf1df9
			{
cf1df9
				if (out_points) out_points[count] = Point(x0, bounds.miny);
cf1df9
				if (count++) return true;
cf1df9
			}
cf1df9
			
cf1df9
			const Real x1 = -(c + bounds.maxy*b)/a;
cf1df9
			if ( approximate_greater_or_equal(x1, bounds.minx)
cf1df9
			  && approximate_less_or_equal   (x1, bounds.maxx) )
cf1df9
			{
cf1df9
				if (out_points) out_points[count] = Point(x1, bounds.maxy);
cf1df9
				if (count++) return true;
cf1df9
			}
cf1df9
		}
cf1df9
cf1df9
		if (approximate_not_zero(b)) {
cf1df9
			const Real y0 = -(c + bounds.minx*a)/b;
cf1df9
			if ( approximate_greater_or_equal(y0, bounds.miny)
cf1df9
			  && approximate_less_or_equal   (y0, bounds.maxy) )
cf1df9
			{
cf1df9
				if (out_points) out_points[count] = Point(bounds.minx, y0);
cf1df9
				if (count++) return true;
cf1df9
			}
cf1df9
			
cf1df9
			const Real y1 = -(c + bounds.maxx*a)/b;
cf1df9
			if ( approximate_greater_or_equal(y1, bounds.miny)
cf1df9
			  && approximate_less_or_equal   (y1, bounds.maxy) )
cf1df9
			{
cf1df9
				if (out_points) out_points[count] = Point(bounds.maxx, y1);
cf1df9
				if (count++) return true;
cf1df9
			}
cf1df9
		}
cf1df9
		
cf1df9
		return false;
cf1df9
	}
cf1df9
	
cf1df9
	Matrix3
cf1df9
	make_matrix(
cf1df9
		const Vector &p0,
cf1df9
		const Vector &px,
cf1df9
		const Vector &py,
cf1df9
		const Vector &p1 )
cf1df9
	{
cf1df9
		// source rect corners are:          (0, 0), (1, 0), (1, 1), (0, 1)
cf1df9
		// target quadrilateral corners are:     p0,     px,     p1,     py
cf1df9
		// so
cf1df9
		// vector will (1, 0) mapped to p0->px
cf1df9
		// vector will (0, 1) mapped to p0->py
cf1df9
		// vector will (1, 1) mapped to p0->p1
cf1df9
		
cf1df9
		const Vector A = px - p1;
cf1df9
		const Vector B = py - p1;
cf1df9
		const Vector C = p0 + p1 - px - py;
cf1df9
		
cf1df9
		Real cw = A[1]*B[0] - A[0]*B[1];
cf1df9
		Real aw = B[0]*C[1] - B[1]*C[0];
cf1df9
		Real bw = A[1]*C[0] - A[0]*C[1];
cf1df9
		
cf1df9
		// normalize and force cw to be positive
cf1df9
		Real k = aw*aw + bw*bw + cw*cw;
cf1df9
		k = k > real_precision<real>() * real_precision<real>() ? 1/sqrt(k) : 1;</real></real>
cf1df9
		if (cw < 0) k = -k;
cf1df9
		aw *= k;
cf1df9
		bw *= k;
cf1df9
		cw *= k;
cf1df9
		
cf1df9
		const Vector c = p0*cw;
cf1df9
		const Vector a = px*(cw + aw) - c;
cf1df9
		const Vector b = py*(cw + bw) - c;
cf1df9
cf1df9
		return Matrix3( a[0], a[1], aw,
cf1df9
						b[0], b[1], bw,
cf1df9
						c[0], c[1], cw );
cf1df9
	}
cf1df9
cf1df9
	Vector3 make_alpha_matrix_col(
cf1df9
		Real w0,
cf1df9
		Real w1,
cf1df9
		const Vector3 &w_col )
cf1df9
	{
cf1df9
		Real k = w1 - w0;
cf1df9
		if (approximate_zero(k))
cf1df9
			return w_col;
cf1df9
		k = w1/k;
cf1df9
		return Vector3(
cf1df9
			k*w_col[0],
cf1df9
			k*w_col[1],
cf1df9
			k*(w_col[2] - w0) );
cf1df9
	}
cf1df9
cf1df9
cf1df9
	Matrix make_alpha_matrix(
cf1df9
		Real aw0, Real aw1,
cf1df9
		Real bw0, Real bw1,
cf1df9
		const Vector3 &w_col )
cf1df9
	{
cf1df9
		const Vector3 a_col = make_alpha_matrix_col(aw0, aw1, w_col);
cf1df9
		const Vector3 b_col = make_alpha_matrix_col(bw0, bw1, w_col);
cf1df9
		return Matrix3(
cf1df9
			a_col[0], b_col[0], w_col[0],
cf1df9
			a_col[1], b_col[1], w_col[1],
cf1df9
			a_col[2], b_col[2], w_col[2] );
cf1df9
	}
cf1df9
	
cf1df9
	
fc7a04
	class OptimalResolutionSolver {
fc7a04
	private:
fc7a04
		Matrix matrix;
fc7a04
		bool affine;
fc7a04
		Vector affine_resolution;
fc7a04
		Vector focus_a;
fc7a04
		Vector focus_b;
fc7a04
		Vector focus_m;
fc7a04
		Vector fp_kw;
fc7a04
		Vector dir;
fc7a04
		Real len;
fc7a04
		
fc7a04
	public:
fc7a04
		explicit OptimalResolutionSolver(const Matrix &matrix):
fc7a04
			affine(), len()
fc7a04
		{
fc7a04
			this->matrix = matrix;
fc7a04
			
fc7a04
			// w-horizon line equatation is A[2]*x + B[2]*y + C[2] = w
fc7a04
			const Vector3 &A = this->matrix.row_x();
fc7a04
			const Vector3 &B = this->matrix.row_y();
fc7a04
			const Vector3 &C = this->matrix.row_z();
fc7a04
			
fc7a04
			const Real wsqr = A[2]*A[2] + B[2]*B[2];
fc7a04
			affine = (wsqr <= real_precision<real>()*real_precision<real>());</real></real>
fc7a04
			affine_resolution = approximate_zero(C[2])
fc7a04
							  ? Vector()
fc7a04
							  : rendering::TransformationAffine::calc_optimal_resolution(Matrix2(
fc7a04
									this->matrix.row_x().to_2d()/C[2],
fc7a04
									this->matrix.row_y().to_2d()/C[2] ));
fc7a04
			const Real wsqr_div = !affine ? 1/wsqr : Real(0);
fc7a04
			
fc7a04
			// focus points
fc7a04
			bool invertible = this->matrix.is_invertible();
fc7a04
			const Matrix back_matrix = this->matrix.get_inverted();
fc7a04
			const bool focus_a_exists = invertible && approximate_not_zero(back_matrix.m02);
fc7a04
			const bool focus_b_exists = invertible && approximate_not_zero(back_matrix.m12);
fc7a04
			const bool focus_m_exists = focus_a_exists && focus_b_exists;
fc7a04
			assert(focus_a_exists || focus_b_exists);
fc7a04
			focus_a = focus_a_exists ? back_matrix.row_x().to_2d()/back_matrix.m02 : Vector();
fc7a04
			focus_b = focus_b_exists ? back_matrix.row_y().to_2d()/back_matrix.m12 : Vector();
fc7a04
			focus_m = focus_m_exists ? (focus_a + focus_b)*0.5
fc7a04
									 : focus_a_exists ? focus_a : focus_b;
fc7a04
fc7a04
			const Vector dist = focus_m_exists ? focus_b - focus_a : Vector();
fc7a04
			len = dist.mag()*0.5;
fc7a04
			dir = approximate_zero(len) ? Vector() : dist/(2*len);
fc7a04
			
fc7a04
			// projection of focus points to w-horizon line
fc7a04
			fp_kw = Vector(A[2], B[2])*wsqr_div;
fc7a04
		}
fc7a04
		
fc7a04
	private:
fc7a04
		Real ratio_for_point(const Vector &point, Real w) const {
fc7a04
			const Vector v = matrix.get_transformed(point);
fc7a04
			const Vector ox( matrix.m00 - matrix.m02*v[0]*w,
fc7a04
							 matrix.m10 - matrix.m12*v[0]*w );
fc7a04
			const Vector oy( matrix.m01 - matrix.m02*v[1]*w,
fc7a04
							 matrix.m11 - matrix.m12*v[1]*w );
fc7a04
			return -ox.mag()-oy.mag();
fc7a04
		}
fc7a04
fc7a04
		Vector resolution_for_point(const Vector &point, Real w) const {
fc7a04
			const Vector v = matrix.get_transformed(point);
fc7a04
			const Matrix2 m(
fc7a04
				Vector( (matrix.m00 - matrix.m02*v[0]*w)*w,
fc7a04
						(matrix.m01 - matrix.m02*v[1]*w)*w ),
fc7a04
				Vector( (matrix.m10 - matrix.m12*v[0]*w)*w,
fc7a04
						(matrix.m11 - matrix.m12*v[1]*w)*w ));
fc7a04
			return rendering::TransformationAffine::calc_optimal_resolution(m);
fc7a04
		}
fc7a04
		
fc7a04
		// returns Vector(l, ratio)
fc7a04
		Vector find_max(const Vector &point, const Vector &dir, Real maxl, Real w) const {
fc7a04
			if (maxl <= 1 || maxl >= 1e+10)
fc7a04
				return Vector(0, ratio_for_point(point, w));
fc7a04
			
fc7a04
			Real l0  = 0;
fc7a04
			Real l1  = maxl;
fc7a04
			Real ll0 = (l0 + l1)*0.5;
fc7a04
			Real vv0 = ratio_for_point(point + dir*ll0, w);
fc7a04
			while(l1 - l0 > 1) {
fc7a04
				Real ll1, vv1;
fc7a04
				if (ll0 - l0 < l1 - ll0) {
fc7a04
					ll1 = (ll0 + l1)*0.5;
fc7a04
					vv1 = ratio_for_point(point + dir*ll1, w);
fc7a04
				} else {
fc7a04
					ll1 = ll0;
fc7a04
					vv1 = vv0;
fc7a04
					ll0 = (l0 + ll0)*0.5;
fc7a04
					vv0 = ratio_for_point(point + dir*ll0, w);
fc7a04
				}
fc7a04
				
fc7a04
				if (vv0 > vv1) {
fc7a04
					l1 = ll1;
fc7a04
				} else {
fc7a04
					l0 = ll0;
fc7a04
					ll0 = ll1;
fc7a04
					vv0 = vv1;
fc7a04
				}
fc7a04
			}
fc7a04
			
fc7a04
			return Vector(ll0, vv0);
fc7a04
		}
fc7a04
	
fc7a04
	public:
fc7a04
		Vector solve(Real w) const {
fc7a04
			if (affine)
fc7a04
				return affine_resolution;
fc7a04
			if (w < real_precision<real>())</real>
fc7a04
				return Vector();
fc7a04
			
fc7a04
			Vector center;
fc7a04
			const Vector offset_w = fp_kw/w;
fc7a04
			if (len <= 1) {
fc7a04
				center = focus_m + offset_w;
fc7a04
			} else {
fc7a04
				const Vector solution_a = find_max(focus_a + offset_w,  dir, len, w);
fc7a04
				const Vector solution_b = find_max(focus_b + offset_w, -dir, len, w);
fc7a04
				center = solution_a[1] > solution_b[1]
fc7a04
					   ? focus_a + offset_w + dir*solution_a[0]
fc7a04
					   : focus_b + offset_w - dir*solution_b[0];
fc7a04
			}
fc7a04
			
fc7a04
			return resolution_for_point(center, w);
fc7a04
		}
fc7a04
	};
fc7a04
	
fc7a04
	
cf1df9
	class TransformationPerspective: public rendering::Transformation
cf1df9
	{
cf1df9
	public:
cf1df9
		typedef etl::handle<transformationperspective> Handle;</transformationperspective>
cf1df9
		
cf1df9
		class Layer {
cf1df9
		public:
cf1df9
			Rect orig_rect;
cf1df9
			Bounds bounds;
cf1df9
			Matrix alpha_matrix;
cf1df9
cf1df9
			explicit Layer(
cf1df9
				const Rect &orig_rect = Rect(),
cf1df9
				const Bounds &bounds = Bounds(),
cf1df9
				const Matrix &alpha_matrix = Matrix()
cf1df9
			):
cf1df9
				orig_rect(orig_rect),
cf1df9
				bounds(bounds),
cf1df9
				alpha_matrix(alpha_matrix)
cf1df9
			{ }
cf1df9
		};
cf1df9
		
cf1df9
		typedef std::vector<layer> LayerList;</layer>
cf1df9
		
cf1df9
		Matrix matrix;
cf1df9
		
cf1df9
		TransformationPerspective() { }
cf1df9
		explicit TransformationPerspective(const Matrix &matrix): matrix(matrix) { }
cf1df9
		
cf1df9
	protected:
cf1df9
		virtual Transformation* clone_vfunc() const
cf1df9
			{ return new TransformationPerspective(matrix); }
cf1df9
		
cf1df9
		virtual Transformation* create_inverted_vfunc() const
cf1df9
			{ return new TransformationPerspective(matrix.get_inverted()); }
cf1df9
		
cf1df9
		virtual Point transform_vfunc(const Point &x) const
cf1df9
			{ return (matrix*Vector3(x[0], x[1], 1)).divide_z().to_2d(); }
cf1df9
		
cf1df9
		virtual Matrix2 derivative_vfunc(const Point &x) const {
cf1df9
			Real w = matrix.m02*x[0] + matrix.m12*x[1] + matrix.m22;
cf1df9
			w = approximate_zero(w) ? Real(0) : 1/w;
cf1df9
			return Matrix2(matrix.m00*w, matrix.m01*w, matrix.m10*w, matrix.m11*w);
cf1df9
		}
cf1df9
		
cf1df9
		virtual Bounds transform_bounds_vfunc(const Bounds &bounds) const
cf1df9
			{ return transform_bounds_perspective(matrix, bounds); }
cf1df9
		
cf1df9
		virtual bool can_merge_outer_vfunc(const Transformation &other) const {
cf1df9
			return (bool)dynamic_cast<const transformationperspective*="">(&other)</const>
cf1df9
				|| (bool)dynamic_cast<const rendering::transformationaffine*="">(&other);</const>
cf1df9
		}
cf1df9
		
cf1df9
		virtual bool can_merge_inner_vfunc(const Transformation &other) const
cf1df9
			{ return can_merge_outer_vfunc(other); }
cf1df9
			
cf1df9
		virtual void merge_outer_vfunc(const Transformation &other) {
cf1df9
			if (const TransformationPerspective *perspective = dynamic_cast<const transformationperspective*="">(&other)) {</const>
cf1df9
				matrix = perspective->matrix * matrix;
cf1df9
			} else
cf1df9
			if (const rendering::TransformationAffine *affine = dynamic_cast<const rendering::transformationaffine*="">(&other)) {</const>
cf1df9
				matrix = affine->matrix * matrix;
cf1df9
			} else {
cf1df9
				assert(false);
cf1df9
			}
cf1df9
		}
cf1df9
		
cf1df9
		virtual void merge_inner_vfunc(const Transformation &other) {
cf1df9
			if (const TransformationPerspective *perspective = dynamic_cast<const transformationperspective*="">(&other)) {</const>
cf1df9
				matrix *= perspective->matrix;
cf1df9
			} else
cf1df9
			if (const rendering::TransformationAffine *affine = dynamic_cast<const rendering::transformationaffine*="">(&other)) {</const>
cf1df9
				matrix *= affine->matrix;
cf1df9
			} else {
cf1df9
				assert(false);
cf1df9
			}
cf1df9
		}
cf1df9
		
cf1df9
	public:
cf1df9
		void transform_bounds_layered(
cf1df9
			LayerList &out_layers,
cf1df9
			const Bounds &bounds,
cf1df9
			Real step ) const
cf1df9
		{
cf1df9
			transform_bounds_layered(out_layers, matrix, bounds, step);
cf1df9
		}
cf1df9
		
cf1df9
		static Bounds transform_bounds_perspective(
cf1df9
			const Matrix &matrix,
cf1df9
			const Bounds &bounds )
cf1df9
		{
cf1df9
			if (!bounds.rect.valid())
cf1df9
				return Bounds();
cf1df9
			
fc7a04
			const Matrix norm_matrix = matrix.get_normalized_by_det();
cf1df9
			const Real A = norm_matrix.m02;
cf1df9
			const Real B = norm_matrix.m12;
cf1df9
			const Real C = norm_matrix.m22;
fc7a04
			if (A*A + B*B <= real_precision<real>() * real_precision<real>()) {</real></real>
fc7a04
				const Real C_inv = approximate_zero(C) ? Real(0) : 1/C;
fc7a04
				return rendering::TransformationAffine::transform_bounds_affine(norm_matrix*C_inv, bounds);
fc7a04
			}
cf1df9
			
cf1df9
			const Real horizonw = real_precision<real>();</real>
cf1df9
			
cf1df9
			Bounds out_bounds;
cf1df9
			
cf1df9
			Real minw =  INFINITY;
cf1df9
			Real maxw = -INFINITY;
cf1df9
			bool found = false;
cf1df9
			const Vector3 in_corners[4] = {
cf1df9
				Vector3(bounds.rect.minx, bounds.rect.miny, 1),
cf1df9
				Vector3(bounds.rect.minx, bounds.rect.maxy, 1),
cf1df9
				Vector3(bounds.rect.maxx, bounds.rect.miny, 1),
cf1df9
				Vector3(bounds.rect.maxx, bounds.rect.maxy, 1) };
cf1df9
			for(int i = 0; i < 4; ++i) {
cf1df9
				const Vector3 v = norm_matrix*in_corners[i];
cf1df9
				const Real w = v[2];
cf1df9
				if (w > horizonw) {
cf1df9
					const Real k = 1/w;
cf1df9
					const Vector p(v[0]*k, v[1]*k);
cf1df9
					if (found) out_bounds.rect.expand(p);
cf1df9
						  else out_bounds.rect.set_point(p);
cf1df9
					found = true;
cf1df9
					if (w < minw) minw = w;
cf1df9
					if (w > maxw) maxw = w;
cf1df9
				}
cf1df9
			}
cf1df9
			if (!found)
cf1df9
				return Bounds();
cf1df9
			
cf1df9
			if (horizonw >= real_precision<real>()) {</real>
cf1df9
				Vector line[2];
cf1df9
				if (truncate_line(line, bounds.rect, A, B, C - horizonw)) {
cf1df9
					const Real horizonw_div = 1/horizonw;
cf1df9
					for(int i = 0; i < 2; ++i) {
cf1df9
						Vector3 v = norm_matrix*Vector3(line[i][0], line[i][1], 1);
cf1df9
						out_bounds.rect.expand( Vector(v[0]*horizonw_div, v[1]*horizonw_div) );
cf1df9
					}
cf1df9
					minw = horizonw;
cf1df9
				}
cf1df9
			}
cf1df9
			
cf1df9
			const Real midw = exp((log(minw) + log(maxw))*0.5);
fc7a04
			const Real krx = 1/bounds.resolution[0];
fc7a04
			const Real kry = 1/bounds.resolution[1];
fc7a04
			const OptimalResolutionSolver resolution_solver( norm_matrix*Matrix().set_scale(krx, kry) );
fc7a04
			out_bounds.resolution = resolution_solver.solve(midw);
cf1df9
			
cf1df9
			return out_bounds;
cf1df9
		}
cf1df9
		
cf1df9
		static void transform_bounds_layered(
cf1df9
			LayerList &out_layers,
cf1df9
			const Matrix &matrix,
cf1df9
			const Bounds &bounds,
cf1df9
			Real step )
cf1df9
		{
cf1df9
			if (!bounds.is_valid())
cf1df9
				return;
cf1df9
			
fc7a04
			const Matrix norm_matrix = matrix.get_normalized_by_det();
cf1df9
			step = std::max(Real(1.1), step);
cf1df9
			
cf1df9
			// calc coefficient for equatation of "horizontal" line: A*x + B*y + C = 1/w (aka A*x + B*y = 1/w - C)
cf1df9
			//                     equatation of line of horizon is: A*x + B*y + C = 0   (aka A*x + B*y = -C)
cf1df9
			const Real A = norm_matrix.m02;
cf1df9
			const Real B = norm_matrix.m12;
cf1df9
			const Real C = norm_matrix.m22;
cf1df9
			
cf1df9
			const Real krx = 1/bounds.resolution[0];
cf1df9
			const Real kry = 1/bounds.resolution[1];
cf1df9
			
cf1df9
			const Real hd = sqrt(A*A*krx*krx + B*B*kry*kry);
cf1df9
			if (hd <= real_precision<real>()) {</real>
cf1df9
				if (fabs(C) < real_precision<real>())</real>
cf1df9
					return; // matrix is not invertible
cf1df9
				
cf1df9
				// orthogonal projection, no perspective, no subdiviosions, just make single layer
cf1df9
				out_layers.push_back(Layer(
cf1df9
					bounds.rect,
fc7a04
					rendering::TransformationAffine::transform_bounds_affine(norm_matrix*(1/C), bounds),
cf1df9
					Matrix3( 0, 0, 0, // alpha always one
cf1df9
							 0, 0, 0,
cf1df9
							 1, 1, 1 ) ));
cf1df9
				return;
cf1df9
			}
cf1df9
fc7a04
			// resolution solver
fc7a04
			const OptimalResolutionSolver resolution_solver( norm_matrix*Matrix().set_scale(krx, kry) );
cf1df9
cf1df9
			// corners
cf1df9
			Vector3 corners[4] = {
cf1df9
				Vector3(bounds.rect.minx, bounds.rect.miny, 1),
cf1df9
				Vector3(bounds.rect.minx, bounds.rect.maxy, 1),
cf1df9
				Vector3(bounds.rect.maxx, bounds.rect.miny, 1),
cf1df9
				Vector3(bounds.rect.maxx, bounds.rect.maxy, 1) };
cf1df9
			
cf1df9
			// find visible w range
cf1df9
			const Real horizonw1_inv = hd;
cf1df9
			const Real horizonw1 = 1/hd;
cf1df9
			const Real horizonw2 = 1/(hd*std::min(Real(2), step));
cf1df9
			const Real horizonw3 = 1/(hd*step);
cf1df9
			Real maxw = -INFINITY, minw = INFINITY;
cf1df9
			Vector3 transformed_corners[4];
cf1df9
			for(int i = 0; i < 4; ++i) {
cf1df9
				Vector3 p = norm_matrix * corners[i];
cf1df9
				if (approximate_greater(p[2], horizonw1_inv)) {
cf1df9
					Real w = 1/p[2];
cf1df9
					transformed_corners[i] = Vector3(p[0]*w, p[1]*w, w);
cf1df9
					if (minw > w) minw = w;
cf1df9
					if (maxw < w) maxw = w;
cf1df9
				} else {
cf1df9
					maxw = horizonw1;
cf1df9
				}
cf1df9
			}
cf1df9
			if (approximate_greater_or_equal(minw, maxw))
cf1df9
				return; // all bounds too thin
cf1df9
			const Real maxw3 = std::min(maxw, horizonw3);
cf1df9
cf1df9
			// steps
cf1df9
			const Real stepLog = log(step);
cf1df9
			int minlog = (int)approximate_floor( log(minw)/stepLog );
cf1df9
			int maxlog = (int)approximate_ceil( log(maxw3)/stepLog );
cf1df9
			if (maxlog < minlog) maxlog = minlog;
cf1df9
			
cf1df9
			Real w = pow(step, Real(minlog));
cf1df9
			for(int i = minlog; i <= maxlog; ++i, w *= step) {
cf1df9
				// w range
cf1df9
				const Real w0  = w/step;
cf1df9
				const Real w1  = std::min(w*step, horizonw1);
cf1df9
				
cf1df9
				// alpha ranges
cf1df9
				const Real aw0 = w0;
cf1df9
				const Real aw1 = w;
cf1df9
				const Real bw0 = i == maxlog ? horizonw1 : w1;
cf1df9
				const Real bw1 = i == maxlog ? horizonw2 : w;
cf1df9
				
cf1df9
				// find corners of layer region
cf1df9
				int corners_count = 0;
cf1df9
				Vector layer_corners[8];
cf1df9
				Vector layer_corners_orig[8];
cf1df9
				for(int j = 0; j < 4; ++j) {
cf1df9
					if ( transformed_corners[j][2]
cf1df9
					  && approximate_greater_or_equal(transformed_corners[j][2], w0)
cf1df9
					  && approximate_less_or_equal(transformed_corners[j][2], w1) )
cf1df9
					{
cf1df9
						layer_corners[corners_count] = transformed_corners[j].to_2d();
cf1df9
						layer_corners_orig[corners_count] = corners[j].to_2d();
cf1df9
						++corners_count;
cf1df9
					}
cf1df9
				}
cf1df9
				if (truncate_line(layer_corners_orig + corners_count, bounds.rect, A, B, C - 1/w0)) {
cf1df9
					for(int j = 0; j < 2; ++j, ++corners_count)
cf1df9
						layer_corners[corners_count] =
cf1df9
							( norm_matrix * Vector3(
cf1df9
								layer_corners_orig[corners_count][0],
cf1df9
								layer_corners_orig[corners_count][1], 1 )).to_2d() * w0;
cf1df9
				}
cf1df9
				if (truncate_line(layer_corners_orig + corners_count, bounds.rect, A, B, C - 1/w1)) {
cf1df9
					for(int j = 0; j < 2; ++j, ++corners_count)
cf1df9
						layer_corners[corners_count] =
cf1df9
							( norm_matrix * Vector3(
cf1df9
								layer_corners_orig[corners_count][0],
cf1df9
								layer_corners_orig[corners_count][1], 1 )).to_2d() * w1;
cf1df9
				}
cf1df9
				if (!corners_count)
cf1df9
					continue;
cf1df9
				
cf1df9
				// calc bounds
cf1df9
				Rect layer_rect(layer_corners[0]);
cf1df9
				Rect layer_rect_orig(layer_corners_orig[0]);
cf1df9
				for(int j = 1; j < corners_count; ++j) {
cf1df9
					layer_rect.expand(layer_corners[j]);
cf1df9
					layer_rect_orig.expand(layer_corners_orig[j]);
cf1df9
				}
cf1df9
				if (!layer_rect.valid() || !layer_rect_orig.valid())
cf1df9
					continue;
cf1df9
				
fc7a04
				// calc resolution
fc7a04
				Vector resolution = resolution_solver.solve(w);
fc7a04
				if (resolution[0] <= real_precision<real>() || resolution[1] <= real_precision<real>())</real></real>
fc7a04
					continue;
fc7a04
				
cf1df9
				// make layer
cf1df9
				out_layers.push_back( Layer(
cf1df9
					layer_rect_orig,
fc7a04
					Bounds(
fc7a04
						layer_rect,
fc7a04
						resolution ),
cf1df9
					make_alpha_matrix(
cf1df9
						1/aw0, 1/aw1, 1/bw0, 1/bw1,
cf1df9
						Vector3(norm_matrix.m02, norm_matrix.m12, norm_matrix.m22) ) ));
cf1df9
			}
cf1df9
		}
cf1df9
	};
cf1df9
	
cf1df9
	
cf1df9
	class TaskTransformationPerspective: public rendering::TaskTransformation
cf1df9
	{
cf1df9
	public:
cf1df9
		typedef etl::handle<tasktransformationperspective> Handle;</tasktransformationperspective>
cf1df9
		static Token token;
cf1df9
		virtual Token::Handle get_token() const { return token.handle(); }
cf1df9
cf1df9
	protected:
cf1df9
		TransformationPerspective::LayerList layers;
cf1df9
cf1df9
	public:
cf1df9
		rendering::Holder<transformationperspective> transformation;</transformationperspective>
cf1df9
cf1df9
		virtual rendering::Transformation::Handle get_transformation() const
cf1df9
			{ return transformation.handle(); }
cf1df9
cf1df9
		virtual int get_pass_subtask_index() const {
cf1df9
			if (is_simple() && transformation->matrix.is_identity())
cf1df9
				return 0;
cf1df9
			return TaskTransformation::get_pass_subtask_index();
cf1df9
		}
cf1df9
		
cf1df9
		virtual void set_coords_sub_tasks() {
cf1df9
			const Real step = 2;
cf1df9
cf1df9
			const Task::Handle task = sub_task();
cf1df9
			const rendering::Transformation::Bounds bounds(
cf1df9
				source_rect, get_pixels_per_unit().multiply_coords(supersample));
cf1df9
			const Matrix back_matrix = transformation->matrix
fc7a04
									  .get_normalized_by_det()
fc7a04
									  .get_inverted();
cf1df9
cf1df9
			sub_tasks.clear();
cf1df9
			layers.clear();
cf1df9
			
cf1df9
			TransformationPerspective::LayerList new_layers;
cf1df9
			
cf1df9
			if ( task
cf1df9
			  && is_valid_coords()
cf1df9
			  && approximate_greater(supersample[0], Real(0))
cf1df9
			  && approximate_greater(supersample[1], Real(0)) )
cf1df9
			{
cf1df9
				TransformationPerspective::transform_bounds_layered(new_layers, back_matrix, bounds, step);
cf1df9
			}
cf1df9
			
cf1df9
			Rect sum_rect;
cf1df9
			for(TransformationPerspective::LayerList::const_iterator i = new_layers.begin(); i != new_layers.end(); ++i) {
cf1df9
				rendering::Transformation::DiscreteBounds discrete_bounds =
cf1df9
					rendering::Transformation::make_discrete_bounds( i->bounds );
cf1df9
				if (discrete_bounds.is_valid()) {
cf1df9
					Task::Handle t = task->clone_recursive();
cf1df9
					sub_tasks.push_back(t);
cf1df9
					layers.push_back(*i);
cf1df9
					t->set_coords(discrete_bounds.rect, discrete_bounds.size);
cf1df9
					
cf1df9
					sum_rect |= i->orig_rect;
cf1df9
				}
cf1df9
			}
cf1df9
			trunc_source_rect(sum_rect);
cf1df9
			
cf1df9
			if (sub_tasks.empty()) trunc_to_zero();
cf1df9
		}
cf1df9
	};
cf1df9
	
cf1df9
	
cf1df9
	class TaskTransformationPerspectiveSW: public TaskTransformationPerspective, public rendering::TaskSW
cf1df9
	{
cf1df9
	public:
cf1df9
		typedef etl::handle<tasktransformationperspectivesw> Handle;</tasktransformationperspectivesw>
cf1df9
		static Token token;
cf1df9
		virtual Token::Handle get_token() const { return token.handle(); }
cf1df9
cf1df9
	private:
cf1df9
		template<synfig::surface::sampler_cook::func bool="" func,="" interpolate="true"></synfig::surface::sampler_cook::func>
cf1df9
		static void process_layer(
cf1df9
			synfig::Surface &dst_surface,
cf1df9
			const synfig::Surface &src_surface,
cf1df9
			const RectInt &rect,
cf1df9
			const Matrix &matrix,
cf1df9
			const Matrix &alpha_matrix )
cf1df9
		{
cf1df9
			const int width = rect.get_width();
cf1df9
			
cf1df9
			const Vector3 coord_dx = matrix.row_x();
cf1df9
			const Vector3 coord_dy = matrix.row_y() - coord_dx*width;
cf1df9
			Vector3 coord = matrix*Vector3(rect.minx, rect.miny, 1);
cf1df9
			
cf1df9
			// assume that alpha w coord is equal to coord w
cf1df9
			const Vector alpha_dx = alpha_matrix.row_x().to_2d();
cf1df9
			const Vector alpha_dy = alpha_matrix.row_y().to_2d() - alpha_dx*width;
cf1df9
			Vector alpha = (alpha_matrix*Vector3(rect.minx, rect.miny, 1)).to_2d();
cf1df9
			
cf1df9
			const int dr = dst_surface.get_pitch()/sizeof(Color) - width;
cf1df9
			Color *c = &dst_surface[rect.miny][rect.minx];
cf1df9
			
cf1df9
			for(int r = rect.miny; r < rect.maxy; ++r, c += dr, coord += coord_dy, alpha += alpha_dy) {
cf1df9
				for(Color *end = c + width; c != end; ++c, coord += coord_dx, alpha += alpha_dx) {
cf1df9
					if (coord[2] > real_precision<real>()) {</real>
cf1df9
						const Real w = 1/coord[2];
cf1df9
						const Real a = clamp(alpha[0]*w, Real(0), Real(1)) * clamp(alpha[1]*w, Real(0), Real(1));
cf1df9
						if (interpolate) {
cf1df9
							if (a > real_precision<real>())</real>
cf1df9
								*c += func(&src_surface, coord[0]*w, coord[1]*w).premult_alpha() * a;
cf1df9
						} else {
cf1df9
							if (approximate_greater(a, Real(0.5)))
cf1df9
								*c = func(&src_surface, coord[0]*w, coord[1]*w);
cf1df9
						}
cf1df9
					}
cf1df9
				}
cf1df9
			}
cf1df9
		}
cf1df9
cf1df9
	public:
cf1df9
		virtual bool run(RunParams&) const
cf1df9
		{
cf1df9
			if (!is_valid())
cf1df9
				return true;
cf1df9
cf1df9
			LockWrite ldst(this);
cf1df9
			if (!ldst)
cf1df9
				return false;
cf1df9
			synfig::Surface &dst_surface = ldst->get_surface();
cf1df9
cf1df9
			// target rect
cf1df9
			const RectInt base_rect = RectInt(0, 0, dst_surface.get_w(), dst_surface.get_h()) & target_rect;
cf1df9
			if (!base_rect.is_valid())
cf1df9
				return true;
cf1df9
cf1df9
			// transformation matrix
cf1df9
			const Matrix from_pixels_matrix =
cf1df9
			    Matrix3().set_translate( source_rect.get_min() )
cf1df9
			  * Matrix3().set_scale( get_units_per_pixel() )
cf1df9
			  * Matrix3().set_translate( -target_rect.minx, -target_rect.miny );
cf1df9
			const Matrix to_pixels_matrix = from_pixels_matrix.get_inverted();
cf1df9
			const Matrix back_matrix =
cf1df9
				transformation->matrix
fc7a04
			   .get_normalized_by_det()
fc7a04
			   .get_inverted();
cf1df9
			const Matrix base_matrix =
cf1df9
				back_matrix
cf1df9
			  * from_pixels_matrix;
cf1df9
cf1df9
			// process layers
cf1df9
			bool demult = false;
cf1df9
			TransformationPerspective::LayerList::const_iterator li = layers.begin();
cf1df9
			for(List::const_iterator i = sub_tasks.begin(); i != sub_tasks.end() && li != layers.end(); ++i, ++li) {
cf1df9
				if (!*i) continue;
cf1df9
				
cf1df9
				// rect
cf1df9
				const Rect orig_rect = li->orig_rect & source_rect;
cf1df9
				const Rect rect_float(
cf1df9
					to_pixels_matrix.get_transformed(orig_rect.get_min()),
cf1df9
					to_pixels_matrix.get_transformed(orig_rect.get_max()) );
cf1df9
				const RectInt rect = RectInt(
cf1df9
					(int)approximate_floor(rect_float.minx),
cf1df9
					(int)approximate_floor(rect_float.miny),
cf1df9
					(int)approximate_ceil (rect_float.maxx),
cf1df9
					(int)approximate_ceil (rect_float.maxy) ) & base_rect;
cf1df9
				if (!rect.is_valid())
cf1df9
					continue;
cf1df9
				
cf1df9
				// get source
cf1df9
				LockRead lsrc(*i);
cf1df9
				if (!lsrc)
cf1df9
					continue;
cf1df9
				const synfig::Surface &src_surface = lsrc->get_surface();
cf1df9
				
cf1df9
				// matrix
cf1df9
				const Matrix matrix =
cf1df9
					Matrix3().set_translate( (*i)->target_rect.minx, (*i)->target_rect.miny )
cf1df9
				  * Matrix3().set_scale( (*i)->get_pixels_per_unit() )
cf1df9
				  * Matrix3().set_translate( -(*i)->source_rect.get_min() )
cf1df9
				  * base_matrix;
cf1df9
				const Matrix alpha_matrix =
cf1df9
					li->alpha_matrix
cf1df9
				  * from_pixels_matrix;
cf1df9
				
cf1df9
				// process
cf1df9
				switch(interpolation) {
cf1df9
					case Color::INTERPOLATION_LINEAR:
cf1df9
						process_layer<synfig::surface::sampler_cook::linear_sample>(</synfig::surface::sampler_cook::linear_sample>
cf1df9
							dst_surface, src_surface, rect, matrix, alpha_matrix );
cf1df9
						demult = true;
cf1df9
						break;
cf1df9
					case Color::INTERPOLATION_COSINE:
cf1df9
						process_layer<synfig::surface::sampler_cook::cosine_sample>(</synfig::surface::sampler_cook::cosine_sample>
cf1df9
							dst_surface, src_surface, rect, matrix, alpha_matrix );
cf1df9
						demult = true;
cf1df9
						break;
cf1df9
					case Color::INTERPOLATION_CUBIC:
cf1df9
						process_layer<synfig::surface::sampler_cook::cubic_sample>(</synfig::surface::sampler_cook::cubic_sample>
cf1df9
							dst_surface, src_surface, rect, matrix, alpha_matrix );
cf1df9
						demult = true;
cf1df9
						break;
cf1df9
					default: // nearest
cf1df9
						process_layer<synfig::surface::sampler_cook::nearest_sample, false="">(</synfig::surface::sampler_cook::nearest_sample,>
cf1df9
							dst_surface, src_surface, rect, matrix, alpha_matrix );
cf1df9
						break;
cf1df9
				};
cf1df9
			}
cf1df9
			
cf1df9
			// demult alpha
cf1df9
			if (demult) {
cf1df9
				const int width = base_rect.get_width();
cf1df9
				const int dr = dst_surface.get_pitch()/sizeof(Color) - width;
cf1df9
				Color *c = &dst_surface[base_rect.miny][base_rect.minx];
cf1df9
				for(int r = base_rect.miny; r < base_rect.maxy; ++r, c += dr)
cf1df9
					for(Color *end = c + width; c != end; ++c)
cf1df9
						*c = c->demult_alpha();
cf1df9
			}
cf1df9
			
cf1df9
			return true;
cf1df9
		}
cf1df9
	};
cf1df9
	
cf1df9
	
cf1df9
	rendering::Task::Token TaskTransformationPerspective::token(
cf1df9
		DescAbstract<tasktransformationperspective>("TransformationPerspective") );</tasktransformationperspective>
cf1df9
	rendering::Task::Token TaskTransformationPerspectiveSW::token(
cf1df9
		DescReal< TaskTransformationPerspectiveSW,
cf1df9
				  TaskTransformationPerspective >
cf1df9
					("TaskTransformationPerspectiveSW") );
cf1df9
}
cf1df9
cf1df9
cf1df9
class lyr_std::Warp_Trans: public Transform
cf1df9
{
cf1df9
private:
cf1df9
	etl::handle<const warp=""> layer;</const>
cf1df9
public:
cf1df9
	Warp_Trans(const Warp* x):
cf1df9
		Transform(x->get_guid()), layer(x) { }
cf1df9
	Vector perform(const Vector& x) const
cf1df9
		{ return layer->transform(x); }
cf1df9
	Vector unperform(const Vector& x) const
cf1df9
		{ return layer->back_transform(x); }
cf1df9
	Rect perform(const Rect& x) const
cf1df9
		{ return layer->transform(x); }
cf1df9
	Rect unperform(const Rect& x) const
cf1df9
		{ return layer->back_transform(x); }
cf1df9
	String get_string() const
cf1df9
		{ return "warp"; }
cf1df9
};
cf1df9
cf1df9
Carlos Lopez a09598
Warp::Warp():
cf1df9
	param_src_tl  ( Point(-2  ,  2  ) ),
cf1df9
	param_src_br  ( Point( 2  , -2  ) ),
cf1df9
	param_dest_tl ( Point(-1.8,  2.1) ),
cf1df9
	param_dest_tr ( Point( 1.8,  2.1) ),
cf1df9
	param_dest_bl ( Point(-2.2, -2  ) ),
cf1df9
	param_dest_br ( Point( 2.2, -2  ) ),
cf1df9
	param_clip    ( true ),
7edc15
	param_interpolation( int(3) ),
cf1df9
	valid         ( ),
cf1df9
	affine        ( ),
cf1df9
	clip          ( )
Carlos Lopez a09598
{
Carlos Lopez a09598
	sync();
Carlos Lopez 3a7fde
	SET_INTERPOLATION_DEFAULTS();
Carlos Lopez 3a7fde
	SET_STATIC_DEFAULTS();
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Warp::~Warp()
cf1df9
	{ }
cf1df9
cf1df9
void
cf1df9
Warp::sync()
Carlos Lopez a09598
{
cf1df9
	valid = false;
cf1df9
	
cf1df9
	const Point src_tl  = param_src_tl.get(Point());
cf1df9
	const Point src_br  = param_src_br.get(Point());
cf1df9
	const Point dest_tl = param_dest_tl.get(Point());
cf1df9
	const Point dest_tr = param_dest_tr.get(Point());
cf1df9
	const Point dest_bl = param_dest_bl.get(Point());
cf1df9
	const Point dest_br = param_dest_br.get(Point());
cf1df9
	
cf1df9
	clip = param_clip.get(bool());
cf1df9
	clip_rect = Rect(src_tl, src_br);
cf1df9
	
cf1df9
	const Vector src_dist = src_br - src_tl;
cf1df9
	if (approximate_not_zero(src_dist[0]) && approximate_not_zero(src_dist[1])) {
cf1df9
		matrix = make_matrix( dest_tl, dest_tr, dest_bl, dest_br )
cf1df9
			   * Matrix().set_scale( 1/src_dist[0], 1/src_dist[1] ) 
cf1df9
			   * Matrix().set_translate( -src_tl );
cf1df9
		if (matrix.is_invertible()) {
fc7a04
			back_matrix = matrix.get_normalized_by_det().get_inverted();
cf1df9
			affine = approximate_zero(matrix.m02) && approximate_zero(matrix.m12);
cf1df9
			valid = true;
cf1df9
		}
cf1df9
	}
cf1df9
	
cf1df9
	if (clip && !clip_rect.is_valid() && !clip_rect.is_nan_or_inf())
cf1df9
		valid = false;
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Point
cf1df9
Warp::transform(const Point &x) const
Carlos Lopez a09598
{
cf1df9
	if (!valid) return Vector::nan();
cf1df9
	Vector3 p = matrix*Vector3(x[0], x[1], 1);
cf1df9
	return p[2] > real_precision<real>() ? p.to_2d()/p[2] : Vector::nan();</real>
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Point
cf1df9
Warp::back_transform(const Point &x) const
Carlos Lopez a09598
{
cf1df9
	if (!valid) return Vector::nan();
cf1df9
	Vector3 p = back_matrix*Vector3(x[0], x[1], 1);
cf1df9
	return p[2] > real_precision<real>() ? p.to_2d()/p[2] : Vector::nan();</real>
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Rect
cf1df9
Warp::transform(const Rect &x) const
Carlos Lopez a09598
{
cf1df9
	return valid
cf1df9
		 ? TransformationPerspective::transform_bounds_perspective(
cf1df9
			 matrix, rendering::Transformation::Bounds(x)).rect
cf1df9
		 : Rect();
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Rect
cf1df9
Warp::back_transform(const Rect &x) const
Carlos Lopez a09598
{
cf1df9
	return valid
cf1df9
		 ? TransformationPerspective::transform_bounds_perspective(
cf1df9
			 back_matrix, rendering::Transformation::Bounds(x)).rect
cf1df9
		 : Rect();
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Layer::Handle
cf1df9
Warp::hit_check(Context context, const Point &p)const
Carlos Lopez a09598
{
cf1df9
	if (!valid) return Layer::Handle();
cf1df9
	const Point pp = back_transform(p);
cf1df9
	if (clip && !clip_rect.is_inside(pp)) return Layer::Handle();
cf1df9
	return context.hit_check(pp);
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Color
cf1df9
Warp::get_color(Context context, const Point &p)const
Carlos Lopez a09598
{
cf1df9
	if (!valid) return Color::alpha();
cf1df9
	const Point pp = back_transform(p);
cf1df9
	if (clip && !clip_rect.is_inside(pp)) return Color::alpha();
cf1df9
	return context.get_color(pp);
Carlos Lopez a09598
}
Carlos Lopez a09598
cf1df9
Rect
cf1df9
Warp::get_bounding_rect() const
cf1df9
	{ return Rect(); }
Carlos Lopez a09598
cf1df9
Rect
cf1df9
Warp::get_full_bounding_rect(Context context)const
cf1df9
{
cf1df9
	if (!valid)
cf1df9
		return Rect();
cf1df9
	Rect sub_rect = context.get_full_bounding_rect();
cf1df9
	sub_rect |= get_bounding_rect();
cf1df9
	if (clip)
cf1df9
		sub_rect &= clip_rect;
cf1df9
	return transform(sub_rect);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
Warp::set_param(const String & param, const ValueBase &value)
Carlos Lopez a09598
{
cf1df9
	IMPORT_VALUE_PLUS(param_src_tl ,sync());
cf1df9
	IMPORT_VALUE_PLUS(param_src_br ,sync());
Carlos Lopez 3a7fde
	IMPORT_VALUE_PLUS(param_dest_tl,sync());
Carlos Lopez 3a7fde
	IMPORT_VALUE_PLUS(param_dest_tr,sync());
Carlos Lopez 3a7fde
	IMPORT_VALUE_PLUS(param_dest_bl,sync());
Carlos Lopez 3a7fde
	IMPORT_VALUE_PLUS(param_dest_br,sync());
cf1df9
	IMPORT_VALUE_PLUS(param_clip   ,sync());
9abc9e
	IMPORT_VALUE(param_interpolation);
Carlos Lopez a09598
	return false;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueBase
Carlos Lopez a09598
Warp::get_param(const String ¶m)const
Carlos Lopez a09598
{
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_src_tl);
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_src_br);
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_dest_tl);
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_dest_tr);
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_dest_bl);
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_dest_br);
Carlos Lopez 3a7fde
	EXPORT_VALUE(param_clip);
9abc9e
	EXPORT_VALUE(param_interpolation);
Carlos Lopez a09598
Carlos Lopez a09598
	EXPORT_NAME();
Carlos Lopez a09598
	EXPORT_VERSION();
Carlos Lopez a09598
Carlos Lopez a09598
	return ValueBase();
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Layer::Vocab
Carlos Lopez a09598
Warp::get_param_vocab()const
Carlos Lopez a09598
{
Carlos Lopez a09598
	Layer::Vocab ret;
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("src_tl")
Carlos Lopez a09598
		.set_local_name(_("Source TL"))
Carlos Lopez a09598
		.set_box("src_br")
Carlos Lopez 42e8f2
		.set_description(_("Top Left corner of the source to warp"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("src_br")
Carlos Lopez a09598
		.set_local_name(_("Source BR"))
Carlos Lopez 42e8f2
		.set_description(_("Bottom Right corner of the source to warp"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("dest_tl")
Carlos Lopez a09598
		.set_local_name(_("Dest TL"))
Carlos Lopez a09598
		.set_connect("dest_tr")
Carlos Lopez 42e8f2
		.set_description(_("Top Left corner of the destination"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("dest_tr")
Carlos Lopez a09598
		.set_local_name(_("Dest TR"))
Carlos Lopez a09598
		.set_connect("dest_br")
Carlos Lopez 42e8f2
		.set_description(_("Top Right corner of the destination"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("dest_br")
Carlos Lopez a09598
		.set_local_name(_("Dest BR"))
Carlos Lopez a09598
		.set_connect("dest_bl")
Carlos Lopez 42e8f2
		.set_description(_("Bottom Right corner of the destination"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("dest_bl")
Carlos Lopez a09598
		.set_local_name(_("Dest BL"))
Carlos Lopez a09598
		.set_connect("dest_tl")
Carlos Lopez 42e8f2
		.set_description(_("Bottom Left corner of the destination"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("clip")
Carlos Lopez a09598
		.set_local_name(_("Clip"))
Carlos Lopez a09598
	);
9abc9e
	
9abc9e
	ret.push_back(ParamDesc("interpolation")
9abc9e
		.set_local_name(_("Interpolation"))
9abc9e
		.set_description(_("What type of interpolation to use"))
9abc9e
		.set_hint("enum")
9abc9e
		.add_enum_value(0,"nearest",_("Nearest Neighbor"))
9abc9e
		.add_enum_value(1,"linear",_("Linear"))
9abc9e
		.add_enum_value(2,"cosine",_("Cosine"))
9abc9e
		.add_enum_value(3,"cubic",_("Cubic"))
9abc9e
		.set_static(true)
9abc9e
	);
Carlos Lopez a09598
Carlos Lopez a09598
	return ret;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
etl::handle<transform></transform>
Carlos Lopez a09598
Warp::get_transform()const
cf1df9
	{ return new Warp_Trans(this); }
Carlos Lopez a09598
cf1df9
rendering::Task::Handle
cf1df9
Warp::build_rendering_task_vfunc(Context context) const
Carlos Lopez a09598
{
9abc9e
	const Color::Interpolation interpolation = (Color::Interpolation)param_interpolation.get(int());
9abc9e
	
cf1df9
	if (!valid)
cf1df9
		return rendering::Task::Handle();
cf1df9
	
cf1df9
	rendering::Task::Handle sub_task = context.build_rendering_task();
cf1df9
	if (!sub_task)
cf1df9
		return rendering::Task::Handle();
cf1df9
	
cf1df9
	if (clip) {
cf1df9
		rendering::TaskContour::Handle task_contour(new rendering::TaskContour());
cf1df9
		task_contour->contour = new rendering::Contour();
cf1df9
		task_contour->contour->move_to( Vector(clip_rect.minx, clip_rect.miny) );
cf1df9
		task_contour->contour->line_to( Vector(clip_rect.minx, clip_rect.maxy) );
cf1df9
		task_contour->contour->line_to( Vector(clip_rect.maxx, clip_rect.maxy) );
cf1df9
		task_contour->contour->line_to( Vector(clip_rect.maxx, clip_rect.miny) );
cf1df9
		task_contour->contour->close();
cf1df9
		task_contour->contour->color = Color(1, 1, 1, 1);
cf1df9
		task_contour->contour->invert = true;
cf1df9
		task_contour->contour->antialias = true;
cf1df9
cf1df9
		rendering::TaskBlend::Handle task_blend(new rendering::TaskBlend());
cf1df9
		task_blend->amount = 1;
cf1df9
		task_blend->blend_method = Color::BLEND_ALPHA_OVER;
cf1df9
		task_blend->sub_task_a() = sub_task;
cf1df9
		task_blend->sub_task_b() = task_contour;
Carlos Lopez ad6565
		
cf1df9
		sub_task = task_blend;
Carlos Lopez ad6565
	}
Carlos Lopez ad6565
	
cf1df9
	if (affine) {
cf1df9
		rendering::TaskTransformationAffine::Handle task_transformation(new rendering::TaskTransformationAffine());
9abc9e
		task_transformation->interpolation = interpolation;
cf1df9
		task_transformation->transformation->matrix = matrix;
cf1df9
		task_transformation->sub_task() = sub_task;
cf1df9
		return task_transformation;
Carlos Lopez ad6565
	}
Carlos Lopez ad6565
	
cf1df9
	TaskTransformationPerspective::Handle task_transformation(new TaskTransformationPerspective());
9abc9e
	task_transformation->interpolation = interpolation;
cf1df9
	task_transformation->transformation->matrix = matrix;
cf1df9
	task_transformation->sub_task() = sub_task;
cf1df9
	return task_transformation;
Carlos Lopez a09598
}