1 /* === S Y N F I G ========================================================= */
2 /*!	\file noise.cpp
3 **	\brief Implementation of the "Noise Gradient" layer
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **	Copyright (c) 2007 Chris Moore
10 **	Copyright (c) 2011-2013 Carlos López
11 **
12 **	This package is free software; you can redistribute it and/or
13 **	modify it under the terms of the GNU General Public License as
14 **	published by the Free Software Foundation; either version 2 of
15 **	the License, or (at your option) any later version.
16 **
17 **	This package is distributed in the hope that it will be useful,
18 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **	General Public License for more details.
21 **	\endlegal
22 */
23 /* ========================================================================= */
24 
25 /* === H E A D E R S ======================================================= */
26 
27 #ifdef USING_PCH
28 #	include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #	include <config.h>
32 #endif
33 
34 #include "noise.h"
35 
36 #include <synfig/localization.h>
37 #include <synfig/general.h>
38 
39 #include <synfig/string.h>
40 #include <synfig/time.h>
41 #include <synfig/context.h>
42 #include <synfig/paramdesc.h>
43 #include <synfig/renddesc.h>
44 #include <synfig/surface.h>
45 #include <synfig/value.h>
46 #include <synfig/valuenode.h>
47 #include <time.h>
48 
49 #endif
50 
51 /* === M A C R O S ========================================================= */
52 
53 using namespace synfig;
54 using namespace std;
55 using namespace etl;
56 
57 /* === G L O B A L S ======================================================= */
58 
59 SYNFIG_LAYER_INIT(Noise);
60 SYNFIG_LAYER_SET_NAME(Noise,"noise");
61 SYNFIG_LAYER_SET_LOCAL_NAME(Noise,N_("Noise Gradient"));
62 SYNFIG_LAYER_SET_CATEGORY(Noise,N_("Gradients"));
63 SYNFIG_LAYER_SET_VERSION(Noise,"0.0");
64 SYNFIG_LAYER_SET_CVS_ID(Noise,"$Id$");
65 
66 /* === P R O C E D U R E S ================================================= */
67 
68 /* === M E T H O D S ======================================================= */
69 
Noise()70 Noise::Noise():
71 	Layer_Composite(1.0,Color::BLEND_COMPOSITE),
72 	param_gradient(ValueBase(Gradient(Color::black(), Color::white()))),
73 	param_random(ValueBase(int(time(NULL)))),
74 	param_size(ValueBase(Vector(1,1))),
75 	param_smooth(ValueBase(int(RandomNoise::SMOOTH_COSINE))),
76 	param_detail(ValueBase(int(4))),
77 	param_speed(ValueBase(Real(0))),
78 	param_turbulent(ValueBase(bool(false))),
79 	param_do_alpha(ValueBase(bool(false))),
80 	param_super_sample(ValueBase(bool(false)))
81 {
82 	//displacement=Vector(1,1);
83 	//do_displacement=false;
84 	SET_INTERPOLATION_DEFAULTS();
85 	SET_STATIC_DEFAULTS();
86 }
87 
88 
89 
90 inline Color
color_func(const Point & point,float pixel_size,Context) const91 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
92 {
93 	Gradient gradient=param_gradient.get(Gradient());
94 	Vector size=param_size.get(Vector());
95 	RandomNoise random;
96 	random.set_seed(param_random.get(int()));
97 	int smooth_=param_smooth.get(int());
98 	int detail=param_detail.get(int());
99 	Real speed=param_speed.get(Real());
100 	bool turbulent=param_turbulent.get(bool());
101 	bool do_alpha=param_do_alpha.get(bool());
102 	bool super_sample=param_super_sample.get(bool());
103 
104 
105 	Color ret(0,0,0,0);
106 
107 	float x(point[0]/size[0]*(1<<detail));
108 	float y(point[1]/size[1]*(1<<detail));
109 	float x2(0),y2(0);
110 
111 	if(super_sample&&pixel_size)
112 	{
113 		x2=(point[0]+pixel_size)/size[0]*(1<<detail);
114 		y2=(point[1]+pixel_size)/size[1]*(1<<detail);
115 	}
116 
117 	int i;
118 	Time time;
119 	time=speed*get_time_mark();
120 	int smooth((!speed && smooth_ == (int)RandomNoise::SMOOTH_SPLINE) ? (int)RandomNoise::SMOOTH_FAST_SPLINE : smooth_);
121 
122 	float ftime(time);
123 
124 	{
125 		float amount=0.0f;
126 		float amount2=0.0f;
127 		float amount3=0.0f;
128 		float alpha=0.0f;
129 		for(i=0;i<detail;i++)
130 		{
131 			amount=random(RandomNoise::SmoothType(smooth),0+(detail-i)*5,x,y,ftime)+amount*0.5;
132 			if(amount<-1)amount=-1;if(amount>1)amount=1;
133 
134 			if(super_sample&&pixel_size)
135 			{
136 				amount2=random(RandomNoise::SmoothType(smooth),0+(detail-i)*5,x2,y,ftime)+amount2*0.5;
137 				if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
138 
139 				amount3=random(RandomNoise::SmoothType(smooth),0+(detail-i)*5,x,y2,ftime)+amount3*0.5;
140 				if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
141 
142 				if(turbulent)
143 				{
144 					amount2=abs(amount2);
145 					amount3=abs(amount3);
146 				}
147 
148 				x2*=0.5f;
149 				y2*=0.5f;
150 			}
151 
152 			if(do_alpha)
153 			{
154 				alpha=random(RandomNoise::SmoothType(smooth),3+(detail-i)*5,x,y,ftime)+alpha*0.5;
155 				if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
156 			}
157 
158 			if(turbulent)
159 			{
160 				amount=abs(amount);
161 				alpha=abs(alpha);
162 			}
163 
164 			x*=0.5f;
165 			y*=0.5f;
166 			//ftime*=0.5f;
167 		}
168 
169 		if(!turbulent)
170 		{
171 			amount=amount/2.0f+0.5f;
172 			alpha=alpha/2.0f+0.5f;
173 
174 			if(super_sample&&pixel_size)
175 			{
176 				amount2=amount2/2.0f+0.5f;
177 				amount3=amount3/2.0f+0.5f;
178 			}
179 		}
180 
181 		if(super_sample && pixel_size)
182 			ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
183 		else
184 			ret=gradient(amount);
185 
186 		if(do_alpha)
187 			ret.set_a(ret.get_a()*(alpha));
188 	}
189 	return ret;
190 }
191 
192 inline float
calc_supersample(const synfig::Point &,float,float) const193 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
194 {
195 	return 0.0f;
196 }
197 
198 synfig::Layer::Handle
hit_check(synfig::Context context,const synfig::Point & point) const199 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
200 {
201 	if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
202 		return const_cast<Noise*>(this);
203 	if(get_amount()==0.0)
204 		return context.hit_check(point);
205 	if(color_func(point,0,context).get_a()>0.5)
206 		return const_cast<Noise*>(this);
207 	return synfig::Layer::Handle();
208 }
209 
210 bool
set_param(const String & param,const ValueBase & value)211 Noise::set_param(const String & param, const ValueBase &value)
212 {
213 	IMPORT_VALUE(param_gradient);
214 	IMPORT_VALUE(param_size);
215 	IMPORT_VALUE(param_random);
216 	IMPORT_VALUE(param_detail);
217 	IMPORT_VALUE(param_smooth);
218 	IMPORT_VALUE(param_speed);
219 	IMPORT_VALUE(param_turbulent);
220 	IMPORT_VALUE(param_do_alpha);
221 	IMPORT_VALUE(param_super_sample);
222 
223 	if(param=="seed")
224 		return set_param("random", value);
225 
226 	return Layer_Composite::set_param(param,value);
227 }
228 
229 ValueBase
get_param(const String & param) const230 Noise::get_param(const String & param)const
231 {
232 	EXPORT_VALUE(param_gradient);
233 	EXPORT_VALUE(param_size);
234 	EXPORT_VALUE(param_random);
235 	EXPORT_VALUE(param_detail);
236 	EXPORT_VALUE(param_smooth);
237 	EXPORT_VALUE(param_speed);
238 	EXPORT_VALUE(param_turbulent);
239 	EXPORT_VALUE(param_do_alpha);
240 	EXPORT_VALUE(param_super_sample);
241 
242 	if(param=="seed")
243 		return get_param("random");
244 
245 	EXPORT_NAME();
246 	EXPORT_VERSION();
247 
248 	return Layer_Composite::get_param(param);
249 }
250 
251 Layer::Vocab
get_param_vocab() const252 Noise::get_param_vocab()const
253 {
254 	Layer::Vocab ret(Layer_Composite::get_param_vocab());
255 
256 	ret.push_back(ParamDesc("gradient")
257 		.set_local_name(_("Gradient"))
258 		.set_description(_("Gradient to apply"))
259 	);
260 	ret.push_back(ParamDesc("seed")
261 		.set_local_name(_("RandomNoise Seed"))
262 		.set_description(_("Change to modify the random seed of the noise"))
263 	);
264 	ret.push_back(ParamDesc("size")
265 		.set_local_name(_("Size"))
266 		.set_description(_("Size of the noise"))
267 	);
268 	ret.push_back(ParamDesc("smooth")
269 		.set_local_name(_("Interpolation"))
270 		.set_description(_("What type of interpolation to use"))
271 		.set_hint("enum")
272 		.add_enum_value(RandomNoise::SMOOTH_DEFAULT,	"nearest",	_("Nearest Neighbor"))
273 		.add_enum_value(RandomNoise::SMOOTH_LINEAR,	"linear",	_("Linear"))
274 		.add_enum_value(RandomNoise::SMOOTH_COSINE,	"cosine",	_("Cosine"))
275 		.add_enum_value(RandomNoise::SMOOTH_SPLINE,	"spline",	_("Spline"))
276 		.add_enum_value(RandomNoise::SMOOTH_CUBIC,	"cubic",	_("Cubic"))
277 	);
278 	ret.push_back(ParamDesc("detail")
279 		.set_local_name(_("Detail"))
280 		.set_description(_("Increase to obtain fine details of the noise"))
281 	);
282 	ret.push_back(ParamDesc("speed")
283 		.set_local_name(_("Animation Speed"))
284 		.set_description(_("In cycles per second"))
285 	);
286 	ret.push_back(ParamDesc("turbulent")
287 		.set_local_name(_("Turbulent"))
288 		.set_description(_("When checked produces turbulent noise"))
289 	);
290 	ret.push_back(ParamDesc("do_alpha")
291 		.set_local_name(_("Do Alpha"))
292 		.set_description(_("Uses transparency"))
293 	);
294 	ret.push_back(ParamDesc("super_sample")
295 		.set_local_name(_("Super Sampling"))
296 		.set_description(_("When checked the gradient is supersampled"))
297 	);
298 
299 	return ret;
300 }
301 
302 Color
get_color(Context context,const Point & point) const303 Noise::get_color(Context context, const Point &point)const
304 {
305 	const Color color(color_func(point,0,context));
306 
307 	if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
308 		return color;
309 	else
310 		return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
311 }
312 
313 CairoColor
get_cairocolor(Context context,const Point & point) const314 Noise::get_cairocolor(Context context, const Point &point)const
315 {
316 	const CairoColor color(color_func(point,0,context));
317 
318 	if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
319 		return color;
320 	else
321 		return CairoColor::blend(color,context.get_cairocolor(point),get_amount(),get_blend_method());
322 }
323 
324 bool
accelerated_render(Context context,Surface * surface,int quality,const RendDesc & renddesc,ProgressCallback * cb) const325 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
326 {
327 	RENDER_TRANSFORMED_IF_NEED(__FILE__, __LINE__)
328 
329 	SuperCallback supercb(cb,0,9500,10000);
330 
331 	if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
332 	{
333 		surface->set_wh(renddesc.get_w(),renddesc.get_h());
334 	}
335 	else
336 	{
337 		if(!context.accelerated_render(surface,quality,renddesc,&supercb))
338 			return false;
339 		if(get_amount()==0)
340 			return true;
341 	}
342 
343 
344 	int x,y;
345 
346 	Surface::pen pen(surface->begin());
347 	const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
348 	Point pos;
349 	Point tl(renddesc.get_tl());
350 	const int w(surface->get_w());
351 	const int h(surface->get_h());
352 	float supersampleradius((abs(pw)+abs(ph))*0.5f);
353 	if(quality>=8)
354 		supersampleradius=0;
355 
356 	if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
357 	{
358 		for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
359 			for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
360 				pen.put_value(color_func(pos,supersampleradius,context));
361 	}
362 	else
363 	{
364 		for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
365 			for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
366 				pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
367 	}
368 
369 	// Mark our progress as finished
370 	if(cb && !cb->amount_complete(10000,10000))
371 		return false;
372 
373 	return true;
374 }
375