1 /* === S Y N F I G ========================================================= */
2 /*! \file importer.cpp
3 ** \brief It is the base class for all the importers.
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 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 <cctype>
34 #include <cstdlib>
35
36 #include <algorithm>
37 #include <functional>
38 #include <map>
39
40 #include <glibmm.h>
41
42 #include "general.h"
43 #include <synfig/localization.h>
44
45 #include "canvas.h"
46 #include "importer.h"
47 #include "string.h"
48 #include "surface.h"
49
50 #include <synfig/rendering/software/surfacesw.h>
51 #include <synfig/rendering/software/surfaceswpacked.h>
52
53 #endif
54
55 /* === M A C R O S ========================================================= */
56
57 /* === G L O B A L S ======================================================= */
58
59 using namespace etl;
60 using namespace std;
61 using namespace synfig;
62
63 Importer::Book* synfig::Importer::book_;
64
65 static map<FileSystem::Identifier,Importer::LooseHandle> *__open_importers;
66
67 /* === P R O C E D U R E S ================================================= */
68
69 /* === M E T H O D S ======================================================= */
70
71 bool
subsys_init()72 Importer::subsys_init()
73 {
74 book_=new Book();
75 __open_importers=new map<FileSystem::Identifier,Importer::LooseHandle>();
76 return true;
77 }
78
79 bool
subsys_stop()80 Importer::subsys_stop()
81 {
82 delete book_;
83 delete __open_importers;
84 return true;
85 }
86
87 Importer::Book&
book()88 Importer::book()
89 {
90 return *book_;
91 }
92
93 Importer::Handle
open(const FileSystem::Identifier & identifier,bool force)94 Importer::open(const FileSystem::Identifier &identifier, bool force)
95 {
96 if (force) forget(identifier); // force reload
97
98 if(identifier.filename.empty())
99 {
100 synfig::error(_("Importer::open(): Cannot open empty filename"));
101 return 0;
102 }
103
104 // If we already have an importer open under that filename,
105 // then use it instead.
106 if(__open_importers->count(identifier))
107 {
108 //synfig::info("Found importer already open, using it...");
109 return (*__open_importers)[identifier];
110 }
111
112 if(filename_extension(identifier.filename) == "")
113 {
114 synfig::error(_("Importer::open(): Couldn't find extension"));
115 return 0;
116 }
117
118 String ext(filename_extension(identifier.filename));
119 if (ext.size()) ext = ext.substr(1); // skip initial '.'
120 std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
121
122
123 if(!Importer::book().count(ext))
124 {
125 synfig::error(_("Importer::open(): Unknown file type -- ")+ext);
126 return 0;
127 }
128
129 try {
130 Importer::Handle importer;
131 importer=Importer::book()[ext].factory(identifier);
132 (*__open_importers)[identifier]=importer;
133 return importer;
134 }
135 catch (String str)
136 {
137 synfig::error(str);
138 }
139 return 0;
140 }
141
forget(const FileSystem::Identifier & identifier)142 void Importer::forget(const FileSystem::Identifier &identifier)
143 {
144 __open_importers->erase(identifier);
145 }
146
Importer(const FileSystem::Identifier & identifier)147 Importer::Importer(const FileSystem::Identifier &identifier):
148 gamma_(2.2),
149 identifier(identifier)
150 {
151 }
152
153
~Importer()154 Importer::~Importer()
155 {
156 // Remove ourselves from the open importer list
157 map<FileSystem::Identifier,Importer::LooseHandle>::iterator iter;
158 for(iter=__open_importers->begin();iter!=__open_importers->end();)
159 if(iter->second==this)
160 __open_importers->erase(iter++); else ++iter;
161 }
162
163 rendering::Surface::Handle
get_frame(const RendDesc &,const Time & time)164 Importer::get_frame(const RendDesc & /* renddesc */, const Time &time)
165 {
166 if (last_surface_ && last_surface_->is_created())
167 return last_surface_;
168
169 Surface surface;
170 bool trimmed = false;
171 unsigned int width = 0, height = 0, top = 0, left = 0;
172 if(!get_frame(surface, RendDesc(), time, trimmed, width, height, top, left))
173 warning(strprintf("Unable to get frame from \"%s\"", identifier.filename.c_str()));
174
175
176 const char *s = getenv("SYNFIG_PACK_IMAGES");
177 if (s == NULL || atoi(s) != 0)
178 last_surface_ = new rendering::SurfaceSWPacked();
179 else
180 last_surface_ = new rendering::SurfaceSW();
181
182 if (surface.is_valid())
183 last_surface_->assign(surface[0], surface.get_w(), surface.get_h());
184
185 return last_surface_;
186 }
187