Add 'gif' to the list of extensions recognised by the Papagayo importer.
[synfig.git] / synfig-core / trunk / src / synfig / listimporter.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file listimporter.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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 #include "listimporter.h"
33 #include "general.h"
34 #include <fstream>
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43
44 /* === M A C R O S ========================================================= */
45
46 #define LIST_IMPORTER_CACHE_SIZE        20
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ListImporter::ListImporter(const String &filename)
55 {
56         fps=15;
57
58         ifstream stream(filename.c_str());
59
60         if(!stream)
61         {
62                 synfig::error("Unable to open "+filename);
63                 return;
64         }
65         String line;
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
68
69         if (line == "MohoSwitch1")      // it is a Papagayo lipsync file
70         {
71                 String phoneme, prevphoneme, prevext, ext(".jpg"); // default image format
72                 int frame, prevframe = -1; // it means that the previous phoneme is not known
73
74                 while(!stream.eof())
75                 {
76                         getline(stream,line);
77
78                         if(line.find(String("FPS ")) == 0)
79                         {
80                                 float f = atof(String(line.begin()+4,line.end()).c_str());
81                                 if (f) fps = f;
82                                 continue;
83                         }
84
85                         if (line == "bmp"  ||
86                                 line == "gif"  ||
87                                 line == "jpg"  ||
88                                 line == "png"  ||
89                                 line == "ppm"  ||
90                                 line == "tiff" )
91                         {
92                                 ext = String(".") + line;
93                                 continue;
94                         }
95
96                         size_t pos = line.find(String(" ")); // find space position. The format is "frame phoneme-name".
97                         if(pos != String::npos)
98                         {
99                                 frame = atoi(String(line.begin(),line.begin()+pos).c_str());
100                                 phoneme = String(line.begin()+pos+1, line.end());
101
102                                 if (prevframe != -1)
103                                         while (prevframe < frame)
104                                         {
105                                                 filename_list.push_back(prefix + prevphoneme + prevext);
106                                                 synfig::info("frame %d, phoneme = %s, path = '%s'", prevframe, prevphoneme.c_str(), (prefix + prevphoneme + prevext).c_str());
107                                                 prevframe++;
108                                         }
109
110                                 prevext = ext;
111                                 prevframe = frame;
112                                 prevphoneme = phoneme;
113                         }
114                 }
115
116                 filename_list.push_back(prefix + prevphoneme + prevext);        // do it one more time for the last phoneme
117                 synfig::info("finally, frame %d, phoneme = %s, path = '%s'", prevframe, prevphoneme.c_str(), (prefix + prevphoneme + prevext).c_str());
118
119                 return;
120         }
121
122         stream.seekg(ios_base::beg);
123         while(!stream.eof())
124         {
125                 getline(stream,line);
126                 if(line.empty())
127                         continue;
128                 // If we have a framerate, then use it
129                 if(line.find(String("FPS "))==0)
130                 {
131                         fps=atof(String(line.begin()+4,line.end()).c_str());
132                         //synfig::warning("FPS=%f",fps);
133                         if(!fps)
134                                 fps=15;
135                         continue;
136                 }
137                 filename_list.push_back(prefix+line);
138         }
139 }
140
141 Importer*
142 ListImporter::create(const char *filename)
143 {
144         return new ListImporter(filename);
145 }
146
147 ListImporter::~ListImporter()
148 {
149 }
150
151 bool
152 ListImporter::get_frame(Surface &surface,Time time, ProgressCallback *cb)
153 {
154         int frame=round_to_int(time*fps);
155
156         if(!filename_list.size())
157         {
158                 if(cb)cb->error(_("No images in list"));
159                 else synfig::error(_("No images in list"));
160                 return false;
161         }
162
163         if(frame<0)frame=0;
164         if(frame>=(signed)filename_list.size())frame=filename_list.size()-1;
165
166         // See if that frame is cached
167         std::list<std::pair<String,Surface> >::iterator iter;
168         for(iter=frame_cache.begin();iter!=frame_cache.end();++iter)
169         {
170                 if(iter->first==filename_list[frame])
171                 {
172                         surface.mirror(iter->second);
173                         return static_cast<bool>(surface);
174                 }
175         }
176
177         Importer::Handle importer(Importer::open(filename_list[frame]));
178
179         if(!importer)
180         {
181                 if(cb)cb->error(_("Unable to open ")+filename_list[frame]);
182                 else synfig::error(_("Unable to open ")+filename_list[frame]);
183                 return false;
184         }
185
186         if(!importer->get_frame(surface,0,cb))
187         {
188                 if(cb)cb->error(_("Unable to get frame from ")+filename_list[frame]);
189                 else synfig::error(_("Unable to get frame from ")+filename_list[frame]);
190                 return false;
191         }
192
193         if(frame_cache.size()>=LIST_IMPORTER_CACHE_SIZE)
194                 frame_cache.pop_front();
195
196         frame_cache.push_back(std::pair<String,Surface>(filename_list[frame],surface));
197
198         surface.mirror(frame_cache.back().second);
199
200         return static_cast<bool>(surface);
201 }
202
203 bool
204 ListImporter::is_animated()
205 {
206         return true;
207 }