1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Import Image" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/canvas.h>
48 using namespace synfig;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 SYNFIG_LAYER_INIT(Import);
57 SYNFIG_LAYER_SET_NAME(Import,"import");
58 SYNFIG_LAYER_SET_LOCAL_NAME(Import,N_("Import Image"));
59 SYNFIG_LAYER_SET_CATEGORY(Import,N_("Other"));
60 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
61 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id$");
63 /* === P R O C E D U R E S ================================================= */
65 /* === M E T H O D S ======================================================= */
70 Layer::Vocab voc(get_param_vocab());
71 Layer::fill_static(voc);
79 Import::on_canvas_set()
81 if(get_canvas())set_param("filename",filename);
85 Import::set_param(const String & param, const ValueBase &value)
89 if(param=="filename" && value.same_type_as(filename))
91 set_param_static(param, value.get_static());
94 filename=value.get(filename);
100 String newfilename=value.get(string());
101 String filename_with_path;
103 // Get rid of any %20 crap
106 while((n=newfilename.find("%20"))!=String::npos)
107 newfilename.replace(n,3," ");
110 //if(get_canvas()->get_file_path()==dirname(newfilename))
112 // synfig::info("Image seems to be in local directory. Adjusting path...");
113 // newfilename=basename(newfilename);
117 if(is_absolute_path(newfilename))
119 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
120 while(basename(curpath)==".")curpath=dirname(curpath);
122 newfilename=relative_path(curpath,newfilename);
123 synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
128 filename=newfilename;
130 if(newfilename.empty())
132 filename=newfilename;
138 // If we are already loaded, don't reload
139 if(filename==newfilename && importer)
141 synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
145 assert(get_canvas());
147 if(is_absolute_path(newfilename))
148 filename_with_path=newfilename;
150 filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+newfilename;
152 handle<Importer> newimporter;
154 newimporter=Importer::open(absolute_path(filename_with_path));
158 newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+basename(newfilename));
161 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
168 if(!newimporter->get_frame(surface,get_canvas()->rend_desc(),Time(0),trimmed,width,height,top,left))
170 synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
173 importer=newimporter;
174 filename=newfilename;
175 abs_filename=absolute_path(filename_with_path);
179 } catch(...) { set_amount(0); return false; }
181 return Layer_Bitmap::set_param(param,value);
185 Import::get_param(const String & param)const
191 if(param=="filename")
193 ValueBase ret(ValueBase::TYPE_STRING);
194 ret.set_static(get_param_static(param));
195 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
196 ret=relative_path(curpath,abs_filename);
206 return Layer_Bitmap::get_param(param);
210 Import::get_param_vocab()const
212 Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
214 ret.push_back(ParamDesc("filename")
215 .set_local_name(_("Filename"))
216 .set_description(_("File to import"))
217 .set_hint("filename")
219 ret.push_back(ParamDesc("time_offset")
220 .set_local_name(_("Time Offset"))
227 Import::set_time(Context context, Time time)const
229 if(get_amount() && importer &&
230 importer->is_animated())
231 importer->get_frame(surface,get_canvas()->rend_desc(),time+time_offset,trimmed,width,height,top,left);
233 context.set_time(time);
237 Import::set_time(Context context, Time time, const Point &pos)const
239 if(get_amount() && importer &&
240 importer->is_animated())
241 importer->get_frame(surface,get_canvas()->rend_desc(),time+time_offset,trimmed,width,height,top,left);
243 context.set_time(time,pos);