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
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "listimporter.h"
38 /* === U S I N G =========================================================== */
42 using namespace synfig;
44 /* === M A C R O S ========================================================= */
46 #define LIST_IMPORTER_CACHE_SIZE 20
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
52 /* === M E T H O D S ======================================================= */
54 ListImporter::ListImporter(const String &filename)
58 ifstream stream(filename.c_str());
62 synfig::error("Unable to open "+filename);
66 String prefix=etl::dirname(filename)+ETL_DIRECTORY_SEPARATOR;
67 getline(stream,line); // read first line and check whether it is a Papagayo lip sync file
69 if (line == "MohoSwitch1") // it is a Papagayo lipsync file
71 String phoneme, prevphoneme, prevext, ext(".jpg"); // default image format
72 int frame, prevframe = -1; // it means that the previous phoneme is not known
78 if(line.find(String("FPS ")) == 0)
80 float f = atof(String(line.begin()+4,line.end()).c_str());
91 ext = String(".") + line;
95 size_t pos = line.find(String(" ")); // find space position. The format is "frame phoneme-name".
96 if(pos != String::npos)
98 frame = atoi(String(line.begin(),line.begin()+pos).c_str());
99 phoneme = String(line.begin()+pos+1, line.end());
102 while (prevframe < frame)
104 filename_list.push_back(prefix + prevphoneme + prevext);
105 synfig::info("frame %d, phoneme = %s, path = '%s'", prevframe, prevphoneme.c_str(), (prefix + prevphoneme + prevext).c_str());
111 prevphoneme = phoneme;
115 filename_list.push_back(prefix + prevphoneme + prevext); // do it one more time for the last phoneme
116 synfig::info("finally, frame %d, phoneme = %s, path = '%s'", prevframe, prevphoneme.c_str(), (prefix + prevphoneme + prevext).c_str());
121 stream.seekg(ios_base::beg);
124 getline(stream,line);
127 // If we have a framerate, then use it
128 if(line.find(String("FPS "))==0)
130 fps=atof(String(line.begin()+4,line.end()).c_str());
131 //synfig::warning("FPS=%f",fps);
136 filename_list.push_back(prefix+line);
141 ListImporter::create(const char *filename)
143 return new ListImporter(filename);
146 ListImporter::~ListImporter()
151 ListImporter::get_frame(Surface &surface,Time time, ProgressCallback *cb)
153 int frame=round_to_int(time*fps);
155 if(!filename_list.size())
157 if(cb)cb->error(_("No images in list"));
158 else synfig::error(_("No images in list"));
163 if(frame>=(signed)filename_list.size())frame=filename_list.size()-1;
165 // See if that frame is cached
166 std::list<std::pair<String,Surface> >::iterator iter;
167 for(iter=frame_cache.begin();iter!=frame_cache.end();++iter)
169 if(iter->first==filename_list[frame])
171 surface.mirror(iter->second);
172 return static_cast<bool>(surface);
176 Importer::Handle importer(Importer::open(filename_list[frame]));
180 if(cb)cb->error(_("Unable to open ")+filename_list[frame]);
181 else synfig::error(_("Unable to open ")+filename_list[frame]);
185 if(!importer->get_frame(surface,0,cb))
187 if(cb)cb->error(_("Unable to get frame from ")+filename_list[frame]);
188 else synfig::error(_("Unable to get frame from ")+filename_list[frame]);
192 if(frame_cache.size()>=LIST_IMPORTER_CACHE_SIZE)
193 frame_cache.pop_front();
195 frame_cache.push_back(std::pair<String,Surface>(filename_list[frame],surface));
197 surface.mirror(frame_cache.back().second);
199 return static_cast<bool>(surface);
203 ListImporter::is_animated()