2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software Foundation, Inc
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.
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.
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"
26 #include <boost/algorithm/string/replace.hpp>
27 #include <boost/lexical_cast.hpp>
30 #include "GnashEnums.h"
31 #include "IOChannel.h"
37 /// Guess an appropriate file type from the filename.
39 /// If none can be guessed, we use png.
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()) {
58 return GNASH_FILETYPE_PNG
;
61 ScreenShotter::ScreenShotter(const std::string
& fileName
, int quality
)
66 _type(typeFromFileName(fileName
)),
71 ScreenShotter::ScreenShotter(std::string fileName
, FileType type
,
75 _fileName(std::move(fileName
)),
82 ScreenShotter::~ScreenShotter()
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");
95 std::unique_ptr
<IOChannel
> t(makeFileChannel(f
, true));
96 r
.renderToImage(std::move(t
), _type
, _quality
);
99 log_error(_("Failed to open screenshot file \"%s\"!"), outfile
);
104 ScreenShotter::setFrames(const FrameList
& frames
)
107 std::sort(_frames
.begin(), _frames
.end());
114 // indent-tabs-mode: nil