2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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 (size_t i
= 0; i
< 3; ++i
) {
51 const char* ext
= matches
[i
].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 matches
[i
].type
;
58 return GNASH_FILETYPE_PNG
;
61 ScreenShotter::ScreenShotter(const std::string
& fileName
, int quality
)
66 _type(typeFromFileName(fileName
)),
71 ScreenShotter::ScreenShotter(const std::string
& fileName
, FileType type
,
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 boost::shared_ptr
<IOChannel
> t(makeFileChannel(f
, true));
96 r
.renderToImage(t
, _type
, _quality
);
99 log_error("Failed to open screenshot file \"%s\"!", outfile
);
104 ScreenShotter::screenShot(const Renderer
& r
, size_t frameAdvance
)
106 // Save an image if a spontaneous screenshot was requested or the
107 // frame is in the list of requested frames.
108 if (_immediate
|| std::binary_search(_frames
.begin(), _frames
.end(),
111 // Check whether we've rendered an image for this frame.
112 if (_done
.find(frameAdvance
) != _done
.end()) {
115 _done
.insert(frameAdvance
);
117 saveImage(r
, boost::lexical_cast
<std::string
>(frameAdvance
));
123 ScreenShotter::last(const Renderer
& r
) const
126 saveImage(r
, "last");
131 ScreenShotter::setFrames(const FrameList
& frames
)
134 std::sort(_frames
.begin(), _frames
.end());
141 // indent-tabs-mode: nil