Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / 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         while(!stream.eof())
68         {
69                 getline(stream,line);
70                 if(line.empty())
71                         continue;
72                 // If we have a framerate, then use it
73                 if(line.find(String("FPS "))==0)
74                 {
75                         fps=atof(String(line.begin()+4,line.end()).c_str());
76                         //synfig::warning("FPS=%f",fps);
77                         if(!fps)
78                                 fps=15;
79                         continue;
80                 }
81                 filename_list.push_back(prefix+line);
82         }
83 }
84
85 Importer*
86 ListImporter::create(const char *filename)
87 {
88         return new ListImporter(filename);
89 }
90
91 ListImporter::~ListImporter()
92 {
93 }
94
95 bool
96 ListImporter::get_frame(Surface &surface,Time time, ProgressCallback *cb)
97 {
98 //                      DEBUGPOINT();
99         int frame=static_cast<int>(time*fps);
100 //                      DEBUGPOINT();
101
102         if(!filename_list.size())
103         {
104                 if(cb)cb->error(_("No images in list"));
105                 else synfig::error(_("No images in list"));
106                 return false;
107         }
108
109 //                      DEBUGPOINT();
110         if(frame<0)frame=0;
111         if(frame>=(signed)filename_list.size())frame=filename_list.size()-1;
112
113 //                      DEBUGPOINT();
114         // See if that frame is cached
115         std::list<std::pair<int,Surface> >::iterator iter;
116         for(iter=frame_cache.begin();iter!=frame_cache.end();++iter)
117         {
118                 if(iter->first==frame)
119                 {
120 //                      DEBUGPOINT();
121                         surface.mirror(iter->second);
122                         return static_cast<bool>(surface);
123                 }
124         }
125
126         Importer::Handle importer(Importer::open(filename_list[frame]));
127
128 //      DEBUGPOINT();
129
130         if(!importer)
131         {
132                 if(cb)cb->error(_("Unable to open ")+filename_list[frame]);
133                 else synfig::error(_("Unable to open ")+filename_list[frame]);
134                 return false;
135         }
136
137 //      DEBUGPOINT();
138
139         if(!importer->get_frame(surface,0,cb))
140         {
141                 if(cb)cb->error(_("Unable to get frame from ")+filename_list[frame]);
142                 else synfig::error(_("Unable to get frame from ")+filename_list[frame]);
143                 return false;
144         }
145
146 //      DEBUGPOINT();
147
148         if(frame_cache.size()>=LIST_IMPORTER_CACHE_SIZE)
149                 frame_cache.pop_front();
150
151 //      DEBUGPOINT();
152
153         frame_cache.push_back(std::pair<int,Surface>(frame,surface));
154
155 //      DEBUGPOINT();
156
157         surface.mirror(frame_cache.back().second);
158
159 //      DEBUGPOINT();
160
161         return static_cast<bool>(surface);
162 }
163
164 bool
165 ListImporter::is_animated()
166 {
167         return true;
168 }