1 /* === S Y N F I G ========================================================= */
2 /*! \file import.cpp
3 ** \brief Implementation of the "Import Image" 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 ** === N O T E S ===========================================================
24 **
25 ** ========================================================================= */
26
27 /* === H E A D E R S ======================================================= */
28
29 #ifdef USING_PCH
30 # include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 # include <config.h>
34 #endif
35
36 #include "import.h"
37
38 #include <synfig/localization.h>
39 #include <synfig/general.h>
40
41 #include <synfig/string.h>
42 #include <synfig/time.h>
43 #include <synfig/context.h>
44 #include <synfig/paramdesc.h>
45 #include <synfig/renddesc.h>
46 #include <synfig/surface.h>
47 #include <synfig/value.h>
48 #include <synfig/valuenode.h>
49 #include <synfig/canvas.h>
50 #include <synfig/canvasfilenaming.h>
51 #include <synfig/filesystem.h>
52
53 #include <synfig/rendering/software/surfacesw.h>
54
55 #endif
56
57 using namespace std;
58 using namespace etl;
59 using namespace synfig;
60 using namespace modules;
61 using namespace lyr_std;
62
63 /* === M A C R O S ========================================================= */
64
65 /* === G L O B A L S ======================================================= */
66
67 SYNFIG_LAYER_INIT(Import);
68 SYNFIG_LAYER_SET_NAME(Import,"import");
69 SYNFIG_LAYER_SET_LOCAL_NAME(Import,N_("Import Image"));
70 SYNFIG_LAYER_SET_CATEGORY(Import,N_("Other"));
71 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
72 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id$");
73
74 /* === P R O C E D U R E S ================================================= */
75
76 /* === M E T H O D S ======================================================= */
77
Import()78 Import::Import():
79 param_filename(ValueBase(String())),
80 param_time_offset(ValueBase(Time(0)))
81 {
82 SET_INTERPOLATION_DEFAULTS();
83 SET_STATIC_DEFAULTS();
84 }
85
~Import()86 Import::~Import()
87 {
88 }
89
90 void
on_canvas_set()91 Import::on_canvas_set()
92 {
93 if(get_canvas())set_param("filename",param_filename);
94 }
95
96 bool
set_param(const String & param,const ValueBase & value)97 Import::set_param(const String & param, const ValueBase &value)
98 {
99 try{
100 IMPORT_VALUE(param_time_offset);
101
102 IMPORT_VALUE_PLUS_BEGIN(param_filename)
103 {
104 if(!get_canvas() || !get_canvas()->get_file_system())
105 {
106 importer.reset();
107 cimporter.reset();
108 rendering_surface.reset();
109 param_filename.set(value.get(String()));
110 return true;
111 }
112
113 if (is_surface_modified())
114 {
115 error("Unable to load new file, already opened file is not saved");
116 return false;
117 }
118
119 String filename = value.get(String());
120 String fixed_filename = filename;
121
122 // TODO: find source of this sreening of unicode characters
123 // Get rid of any %20 crap
124 for(String::size_type n; (n = fixed_filename.find("%20")) != String::npos;)
125 fixed_filename.replace(n,3," ");
126
127 String full_filename = CanvasFileNaming::make_full_filename(get_canvas()->get_file_name(), fixed_filename);
128 if (full_filename.empty())
129 {
130 importer.reset();
131 cimporter.reset();
132 rendering_surface.reset();
133 param_filename.set(filename);
134 return true;
135 }
136
137 String independent_filename = CanvasFileNaming::make_canvas_independent_filename(get_canvas()->get_file_name(), full_filename);
138
139 // If we are already loaded, don't reload
140 // here we need something to force reload if file is changed
141 if(this->independent_filename==independent_filename && importer)
142 {
143 param_filename.set(filename);
144 return true;
145 }
146
147 this->independent_filename = independent_filename;
148
149 handle<Importer> newimporter;
150 newimporter = Importer::open(get_canvas()->get_file_system()->get_identifier(full_filename));
151
152 if (!newimporter)
153 {
154 String local_filename = CanvasFileNaming::make_local_filename(get_canvas()->get_file_name(), full_filename);
155 newimporter = Importer::open(get_canvas()->get_file_system()->get_identifier(local_filename));
156 if(!newimporter)
157 {
158 error(strprintf("Unable to create an importer object with file \"%s\"", independent_filename.c_str()));
159 importer.reset();
160 cimporter.reset();
161 param_filename.set(filename);
162 rendering_surface.reset();
163 return true;
164 }
165 }
166
167 Time time_offset = param_time_offset.get(Time());
168 Time time = get_time_mark() + time_offset;
169 if (!newimporter->is_animated())
170 time = Time(0);
171
172 rendering_surface = newimporter->get_frame(get_canvas()->rend_desc(), time);
173 importer=newimporter;
174 param_filename.set(filename);
175
176 return true;
177 }
178 IMPORT_VALUE_PLUS_END
179 } catch(...) { return false; }
180
181 return Layer_Bitmap::set_param(param,value);
182 }
183
184 ValueBase
get_param(const String & param) const185 Import::get_param(const String & param)const
186 {
187 EXPORT_VALUE(param_time_offset);
188 EXPORT_VALUE(param_filename);
189
190 EXPORT_NAME();
191 EXPORT_VERSION();
192
193 return Layer_Bitmap::get_param(param);
194 }
195
196 Layer::Vocab
get_param_vocab() const197 Import::get_param_vocab()const
198 {
199 Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
200
201 ret.push_back(ParamDesc("filename")
202 .set_local_name(_("Filename"))
203 .set_description(_("File to import"))
204 .set_hint("filename")
205 );
206 ret.push_back(ParamDesc("time_offset")
207 .set_local_name(_("Time Offset"))
208 .set_description(_("Time Offset to apply to the imported file"))
209 );
210
211 return ret;
212 }
213
214 void
set_time_vfunc(IndependentContext context,Time time) const215 Import::set_time_vfunc(IndependentContext context, Time time)const
216 {
217 Time time_offset=param_time_offset.get(Time());
218 if(get_amount() && importer && importer->is_animated())
219 rendering_surface = importer->get_frame(get_canvas()->rend_desc(), time+time_offset);
220 context.set_time(time);
221 }
222