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
		{
Rodolfo Ribeiro Gomes cfe072
			std::lock_guard<std::mutex> lock(mutex);</std::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
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
}