1 /* === S Y N F I G ========================================================= */
2 /*! \file listimporter.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "listimporter.h"
39 /* === U S I N G =========================================================== */
43 using namespace synfig;
45 /* === M A C R O S ========================================================= */
47 #define LIST_IMPORTER_CACHE_SIZE 20
49 /* === G L O B A L S ======================================================= */
51 /* === P R O C E D U R E S ================================================= */
53 /* === M E T H O D S ======================================================= */
55 ListImporter::ListImporter(const String &filename)
59 ifstream stream(filename.c_str());
63 synfig::error("Unable to open "+filename);
67 String prefix=etl::dirname(filename)+ETL_DIRECTORY_SEPARATOR;
68 getline(stream,line); // read first line and check whether it is a Papagayo lip sync file
70 if (line == "MohoSwitch1") // it is a Papagayo lipsync file
72 String phoneme, prevphoneme, prevext, ext(".jpg"); // default image format
73 int frame, prevframe = -1; // it means that the previous phoneme is not known
79 if(line.find(String("FPS ")) == 0)
81 float f = atof(String(line.begin()+4,line.end()).c_str());
93 ext = String(".") + line;
97 size_t pos = line.find(String(" ")); // find space position. The format is "frame phoneme-name".
98 if(pos != String::npos)
100 frame = atoi(String(line.begin(),line.begin()+pos).c_str());
101 phoneme = String(line.begin()+pos+1, line.end());
104 while (prevframe < frame)
106 filename_list.push_back(prefix + prevphoneme + prevext);
107 synfig::info("frame %d, phoneme = %s, path = '%s'", prevframe, prevphoneme.c_str(), (prefix + prevphoneme + prevext).c_str());
113 prevphoneme = phoneme;
117 filename_list.push_back(prefix + prevphoneme + prevext); // do it one more time for the last phoneme
118 synfig::info("finally, frame %d, phoneme = %s, path = '%s'", prevframe, prevphoneme.c_str(), (prefix + prevphoneme + prevext).c_str());
123 stream.seekg(ios_base::beg);
126 getline(stream,line);
129 // If we have a framerate, then use it
130 if(line.find(String("FPS "))==0)
132 fps=atof(String(line.begin()+4,line.end()).c_str());
133 //synfig::warning("FPS=%f",fps);
138 filename_list.push_back(prefix+line);
143 ListImporter::create(const char *filename)
145 return new ListImporter(filename);
148 ListImporter::~ListImporter()
153 ListImporter::get_frame(Surface &surface,Time time, ProgressCallback *cb)
155 int frame=round_to_int(time*fps);
157 if(!filename_list.size())
159 if(cb)cb->error(_("No images in list"));
160 else synfig::error(_("No images in list"));
165 if(frame>=(signed)filename_list.size())frame=filename_list.size()-1;
167 // See if that frame is cached
168 std::list<std::pair<String,Surface> >::iterator iter;
169 for(iter=frame_cache.begin();iter!=frame_cache.end();++iter)
171 if(iter->first==filename_list[frame])
173 surface.mirror(iter->second);
174 return static_cast<bool>(surface);
178 Importer::Handle importer(Importer::open(filename_list[frame]));
182 if(cb)cb->error(_("Unable to open ")+filename_list[frame]);
183 else synfig::error(_("Unable to open ")+filename_list[frame]);
187 if(!importer->get_frame(surface,0,cb))
189 if(cb)cb->error(_("Unable to get frame from ")+filename_list[frame]);
190 else synfig::error(_("Unable to get frame from ")+filename_list[frame]);
194 if(frame_cache.size()>=LIST_IMPORTER_CACHE_SIZE)
195 frame_cache.pop_front();
197 frame_cache.push_back(std::pair<String,Surface>(filename_list[frame],surface));
199 surface.mirror(frame_cache.back().second);
201 return static_cast<bool>(surface);
205 ListImporter::is_animated()