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