Update with current status
[gnash.git] / gui / ScreenShotter.cpp
blob587b9bd3a7dfe056c34ca3edd142a8cd6ce560c6
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "ScreenShotter.h"
21 #include <vector>
22 #include <string>
23 #include <cstring>
24 #include <algorithm>
25 #include <cstdio>
26 #include <boost/algorithm/string/replace.hpp>
27 #include <boost/lexical_cast.hpp>
29 #include "log.h"
30 #include "GnashEnums.h"
31 #include "IOChannel.h"
32 #include "Renderer.h"
33 #include "tu_file.h"
35 namespace gnash {
37 /// Guess an appropriate file type from the filename.
39 /// If none can be guessed, we use png.
40 FileType
41 typeFromFileName(const std::string& filename)
43 struct { const char* ext; FileType type; } matches[] =
45 { ".png", GNASH_FILETYPE_PNG },
46 { ".jpg", GNASH_FILETYPE_JPEG },
47 { ".jpeg", GNASH_FILETYPE_JPEG }
50 for (const auto& match : matches) {
51 const char* ext = match.ext;
52 const std::string::size_type pos = filename.rfind(ext);
53 if (pos != std::string::npos &&
54 pos + std::strlen(ext) == filename.size()) {
55 return match.type;
58 return GNASH_FILETYPE_PNG;
61 ScreenShotter::ScreenShotter(const std::string& fileName, int quality)
63 _immediate(false),
64 _fileName(fileName),
65 _last(false),
66 _type(typeFromFileName(fileName)),
67 _quality(quality)
71 ScreenShotter::ScreenShotter(std::string fileName, FileType type,
72 int quality)
74 _immediate(false),
75 _fileName(std::move(fileName)),
76 _last(false),
77 _type(type),
78 _quality(quality)
82 ScreenShotter::~ScreenShotter()
86 void
87 ScreenShotter::saveImage(const Renderer& r, const std::string& id) const
89 // Replace all "%f" in the filename with the frameAdvance.
90 std::string outfile(_fileName);
91 boost::replace_all(outfile, "%f", id);
93 FILE* f = std::fopen(outfile.c_str(), "wb");
94 if (f) {
95 std::unique_ptr<IOChannel> t(makeFileChannel(f, true));
96 r.renderToImage(std::move(t), _type, _quality);
98 else {
99 log_error(_("Failed to open screenshot file \"%s\"!"), outfile);
103 void
104 ScreenShotter::setFrames(const FrameList& frames)
106 _frames = frames;
107 std::sort(_frames.begin(), _frames.end());
112 // Local Variables:
113 // mode: C++
114 // indent-tabs-mode: nil
115 // End: