1 /* === S Y N F I G ========================================================= */
2 /*!	\file synfig/rendering/common/optimizer/optimizerblendzero.cpp
3 **	\brief OptimizerBlendZero
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	......... ... 2015 Ivan Mahonin
9 **
10 **	This package is free software; you can redistribute it and/or
11 **	modify it under the terms of the GNU General Public License as
12 **	published by the Free Software Foundation; either version 2 of
13 **	the License, or (at your option) any later version.
14 **
15 **	This package is distributed in the hope that it will be useful,
16 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **	General Public License for more details.
19 **	\endlegal
20 */
21 /* ========================================================================= */
22 
23 /* === H E A D E R S ======================================================= */
24 
25 #ifdef USING_PCH
26 #	include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #	include <config.h>
30 #endif
31 
32 #ifndef _WIN32
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <signal.h>
36 #endif
37 
38 #include <synfig/general.h>
39 #include <synfig/localization.h>
40 
41 #include "optimizerblendzero.h"
42 
43 #include "../task/taskblend.h"
44 #include "../task/tasksurfaceempty.h"
45 
46 #endif
47 
48 using namespace synfig;
49 using namespace rendering;
50 
51 /* === M A C R O S ========================================================= */
52 
53 /* === G L O B A L S ======================================================= */
54 
55 /* === P R O C E D U R E S ================================================= */
56 
57 /* === M E T H O D S ======================================================= */
58 
59 void
apply_zero(const RunParams & params,const TaskBlend::Handle & blend,const Task::Handle & task) const60 OptimizerBlendZero::apply_zero(const RunParams &params, const TaskBlend::Handle &blend, const Task::Handle &task) const
61 {
62 	if (!task || !task->valid_target())
63 	{
64 		Task::Handle empty = new TaskSurfaceEmpty();
65 		empty->target_surface = params.ref_task->target_surface;
66 		apply(params, empty);
67 		return;
68 	}
69 
70 	if (task->target_surface == blend->target_surface)
71 	{
72 		apply(params, task);
73 		return;
74 	}
75 
76 	apply(params, task->clone());
77 	params.ref_task->target_surface = blend->target_surface;
78 	params.ref_task->move_target_rect(
79 		  blend->get_target_offset()
80 		- task->get_target_offset()
81 		+ (task == blend->sub_task_a() ? blend->get_offset_a() : blend->get_offset_b()) );
82 	assert( params.ref_task->check() );
83 }
84 
85 void
run(const RunParams & params) const86 OptimizerBlendZero::run(const RunParams& params) const
87 {
88 	TaskBlend::Handle blend = TaskBlend::Handle::cast_dynamic(params.ref_task);
89 	if ( blend
90 	  && blend->target_surface
91 	  && blend->sub_task_a()
92 	  && blend->sub_task_a()->target_surface
93 	  && blend->sub_task_b()
94 	  && blend->sub_task_a()->target_surface)
95 	{
96 		bool zero_amount = fabsf(blend->amount) <= 1e-6;
97 
98 		if (zero_amount)
99 			{ apply_zero(params, blend, blend->sub_task_a()); return; }
100 
101 		bool valid_a = blend->sub_task_a()->valid_target();
102 		bool valid_b = blend->sub_task_b()->valid_target();
103 
104 		if (!valid_b && !valid_a)
105 			{ apply_zero(params, blend, Task::Handle()); return; }
106 
107 		bool one_amount = fabsf(blend->amount - 1.f) <= 1e-6;
108 		bool intertsects = valid_a && valid_b
109 						&& etl::intersect(
110 							blend->sub_task_a()->get_target_rect() + blend->get_offset_a(),
111 							blend->sub_task_b()->get_target_rect() + blend->get_offset_b() );
112 
113 		if (one_amount && !intertsects)
114 		{
115 			bool onto = Color::is_onto(blend->blend_method);
116 			bool straight = Color::is_straight(blend->blend_method);
117 
118 			if ( onto && straight)
119 				{ apply_zero(params, blend, Task::Handle()); return; }
120 			if ( onto )
121 				{ apply_zero(params, blend, blend->sub_task_a()); return; }
122 			//if ( straight )
123 			//	{ apply_zero(params, blend, blend->sub_task_b()); return; }
124 		}
125 	}
126 }
127 
128 /* === E N T R Y P O I N T ================================================= */
129