Added copyright lines for files I've edited this year.
[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 == "tiff" )
92                         {
93                                 ext = String(".") + line;
94                                 continue;
95                         }
96
97                         size_t pos = line.find(String(" ")); // find space position. The format is "frame phoneme-name".
98                         if(pos != String::npos)
99                         {
100                                 frame = atoi(String(line.begin(),line.begin()+pos).c_str());
101                                 phoneme = String(line.begin()+pos+1, line.end());
102
103                                 if (prevframe != -1)
104                                         while (prevframe < frame)
105                                         {
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());
108                                                 prevframe++;
109                                         }
110
111                                 prevext = ext;
112                                 prevframe = frame;
113                                 prevphoneme = phoneme;
114                         }
115                 }
116
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());
119
120                 return;
121         }
122
123         stream.seekg(ios_base::beg);
124         while(!stream.eof())
125         {
126                 getline(stream,line);
127                 if(line.empty())
128                         continue;
129                 // If we have a framerate, then use it
130                 if(line.find(String("FPS "))==0)
131                 {
132                         fps=atof(String(line.begin()+4,line.end()).c_str());
133                         //synfig::warning("FPS=%f",fps);
134                         if(!fps)
135                                 fps=15;
136                         continue;
137                 }
138                 filename_list.push_back(prefix+line);
139         }
140 }
141
142 Importer*
143 ListImporter::create(const char *filename)
144 {
145         return new ListImporter(filename);
146 }
147
148 ListImporter::~ListImporter()
149 {
150 }
151
152 bool
153 ListImporter::get_frame(Surface &surface,Time time, ProgressCallback *cb)
154 {
155         int frame=round_to_int(time*fps);
156
157         if(!filename_list.size())
158         {
159                 if(cb)cb->error(_("No images in list"));
160                 else synfig::error(_("No images in list"));
161                 return false;
162         }
163
164         if(frame<0)frame=0;
165         if(frame>=(signed)filename_list.size())frame=filename_list.size()-1;
166
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)
170         {
171                 if(iter->first==filename_list[frame])
172                 {
173                         surface.mirror(iter->second);
174                         return static_cast<bool>(surface);
175                 }
176         }
177
178         Importer::Handle importer(Importer::open(filename_list[frame]));
179
180         if(!importer)
181         {
182                 if(cb)cb->error(_("Unable to open ")+filename_list[frame]);
183                 else synfig::error(_("Unable to open ")+filename_list[frame]);
184                 return false;
185         }
186
187         if(!importer->get_frame(surface,0,cb))
188         {
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]);
191                 return false;
192         }
193
194         if(frame_cache.size()>=LIST_IMPORTER_CACHE_SIZE)
195                 frame_cache.pop_front();
196
197         frame_cache.push_back(std::pair<String,Surface>(filename_list[frame],surface));
198
199         surface.mirror(frame_cache.back().second);
200
201         return static_cast<bool>(surface);
202 }
203
204 bool
205 ListImporter::is_animated()
206 {
207         return true;
208 }