keep NEXT_RELEASE at 0.8.10 till after the release
[gnash.git] / gui / ScreenShotter.h
blob5545bf8377b7c93978cd155c97a9d0a561aa8223
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 #ifndef GNASH_SCREENSHOT_H
20 #define GNASH_SCREENSHOT_H
22 #include <vector>
23 #include <string>
24 #include <boost/shared_ptr.hpp>
25 #include <set>
26 #include <algorithm>
27 #include <boost/lexical_cast.hpp>
29 #include "GnashEnums.h"
31 namespace gnash {
32 class Renderer;
35 namespace gnash {
37 /// Handles screen dumps.
38 class ScreenShotter
40 public:
42 typedef std::vector<size_t> FrameList;
44 /// Create a ScreenShotter with output type selected from filename
45 ScreenShotter(const std::string& fileName, int quality = 100);
47 /// Create a ScreenShotter, specifying the output type.
48 ScreenShotter(const std::string& fileName, FileType f, int quality = 100);
50 ~ScreenShotter();
52 /// Take a screenshot at the next possible moment.
53 void now() {
54 _immediate = true;
57 /// Take a screenshot when the last frame is reached.
58 void lastFrame() {
59 _last = true;
62 struct NoAction { void operator()() const {} };
64 /// To be called on the last frame before exit.
66 /// @param r The renderer to use to render the image.
68 /// Which frame is last depends on the execution path of the SWF, whether
69 /// the SWF loops, whether a timeout was requested or a maximum number of
70 /// advances set. Those conditions are not knowable in advance, so
71 /// the last frame is a special case.
72 void last(const Renderer& r) const {
73 last<NoAction>(r);
76 /// To be called on the last frame before exit.
78 /// @tparam Action The functor to call only when a screenshot is
79 /// due.
80 /// @param r The renderer to use to render the image.
82 /// Which frame is last depends on the execution path of the SWF, whether
83 /// the SWF loops, whether a timeout was requested or a maximum number of
84 /// advances set. Those conditions are not knowable in advance, so
85 /// the last frame is a special case.
86 template<typename Action>
87 void last(const Renderer& r, Action* t = 0) const
89 if (_last) {
90 if (t) (*t)();
91 saveImage(r, "last");
95 /// Takes a screenshot if required.
97 /// Called on each advance.
99 /// @param frameAdvance used to check whether a screenshot is required
100 /// as well as to construct the filename.
101 /// @param r The renderer to use to render the image.
102 void screenShot(const Renderer& r, size_t frameAdvance) {
103 screenShot<NoAction>(r, frameAdvance);
106 /// Takes a screenshot if required.
108 /// Called on each advance, invoking a functor before any screenshot is
109 /// taken.
111 /// @tparam Action The functor to call only when a screenshot is
112 /// due.
113 /// @param frameAdvance used to check whether a screenshot is required
114 /// as well as to construct the filename.
115 /// @param r The renderer to use to render the image.
116 template<typename Action>
117 void screenShot(const Renderer& r, size_t frameAdvance, Action* t = 0) {
118 // Save an image if a spontaneous screenshot was requested or the
119 // frame is in the list of requested frames.
120 if (_immediate || std::binary_search(_frames.begin(), _frames.end(),
121 frameAdvance)) {
123 // Check whether we've rendered an image for this frame.
124 if (_done.find(frameAdvance) != _done.end()) {
125 return;
127 if (t) (*t)();
128 _done.insert(frameAdvance);
130 saveImage(r, boost::lexical_cast<std::string>(frameAdvance));
131 _immediate = false;
136 /// Request a list of frames to be rendered to image files.
137 void setFrames(const FrameList& frames);
139 private:
141 /// Take the screenshot.
142 void saveImage(const Renderer& r, const std::string& filename) const;
144 /// If true, the next call to screenshot will take a screenshot
145 bool _immediate;
147 /// Name used to generate output file.
148 const std::string _fileName;
150 /// Whether to take a screenshot on the last frame.
151 bool _last;
153 FrameList _frames;
155 const FileType _type;
157 const int _quality;
159 std::set<int> _done;
163 } // end of gnash namespace
165 #endif
167 // Local Variables:
168 // mode: C++
169 // indent-tabs-mode: nil
170 // End: