1 /* === S Y N F I G ========================================================= */
2 /*!	\file synfig/rendering/common/optimizer/optimizersurfacecreate.cpp
3 **	\brief OptimizerSurfaceCreate
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 "optimizersurfacecreate.h"
39 
40 #include "../task/tasksurfacecreate.h"
41 #include "../task/tasksurfaceconvert.h"
42 
43 #endif
44 
45 using namespace synfig;
46 using namespace rendering;
47 
48 /* === M A C R O S ========================================================= */
49 
50 /* === G L O B A L S ======================================================= */
51 
52 /* === P R O C E D U R E S ================================================= */
53 
54 /* === M E T H O D S ======================================================= */
55 
56 void
insert_task(std::set<Surface::Handle> & created_surfaces,const RunParams & params,Task::List::iterator & i,const Task::Handle & task) const57 OptimizerSurfaceCreate::insert_task(
58 	std::set<Surface::Handle> &created_surfaces,
59 	const RunParams& params,
60 	Task::List::iterator &i,
61 	const Task::Handle &task ) const
62 {
63 	if ( task
64 	  && task->valid_target()
65 	  && !task->target_surface->is_created()
66 	  && !created_surfaces.count(task->target_surface) )
67 	{
68 		created_surfaces.insert(task->target_surface);
69 		TaskSurfaceCreate::Handle surface_create = new TaskSurfaceCreate();
70 		surface_create->target_surface = task->target_surface;
71 
72 		VectorInt size = task->target_surface->get_size();
73 		RectInt rect = task->get_target_rect();
74 		Vector lt = task->get_source_rect_lt();
75 		Vector rb = task->get_source_rect_rb();
76 		Vector k( (rb[0] - lt[0])/(rect.maxx - rect.minx),
77 				  (rb[1] - lt[1])/(rect.maxy - rect.miny) );
78 		Vector nlt( lt[0] - k[0]*rect.minx,
79 				    lt[1] - k[1]*rect.miny);
80 		Vector nrb( lt[0] + k[0]*(size[0] - rect.minx),
81 				    lt[1] + k[1]*(size[1] - rect.miny) );
82 		surface_create->init_target_rect(RectInt(VectorInt::zero(), size), nlt, nrb);
83 		assert( surface_create->check() );
84 		i = params.list.insert(i, surface_create);
85 		++i;
86 		apply(params);
87 	}
88 }
89 
90 void
run(const RunParams & params) const91 OptimizerSurfaceCreate::run(const RunParams& params) const
92 {
93 	std::set<Surface::Handle> created_surfaces;
94 	for(Task::List::iterator i = params.list.begin(); i != params.list.end();)
95 	{
96 		if (*i && (*i)->valid_target())
97 		{
98 			if (TaskSurfaceCreate::Handle::cast_dynamic(*i))
99 			{
100 				if (created_surfaces.count((*i)->target_surface))
101 					{ i = params.list.erase(i); continue; }
102 				created_surfaces.insert((*i)->target_surface);
103 			}
104 			else
105 			if (TaskSurfaceConvert::Handle::cast_dynamic(*i))
106 			{
107 				created_surfaces.insert((*i)->target_surface);
108 			}
109 			else
110 			{
111 				for(std::vector<Task::Handle>::const_iterator j = (*i)->sub_tasks.begin(); j != (*i)->sub_tasks.end(); ++j)
112 					insert_task(created_surfaces, params, i, *j);
113 				insert_task(created_surfaces, params, i, *i);
114 			}
115 		}
116 		++i;
117 	}
118 }
119 
120 /* === E N T R Y P O I N T ================================================= */
121