]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
2.1.0: Don't use virtual for override functions.
[casparcg] / modules / image / consumer / image_consumer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "image_consumer.h"\r
23 \r
24 #include <common/except.h>\r
25 #include <common/env.h>\r
26 #include <common/log.h>\r
27 #include <common/utf.h>\r
28 #include <common/array.h>\r
29 \r
30 #include <core/consumer/frame_consumer.h>\r
31 #include <core/video_format.h>\r
32 #include <core/frame/frame.h>\r
33 \r
34 #include <boost/date_time/posix_time/posix_time.hpp>\r
35 #include <boost/thread.hpp>\r
36 \r
37 #include <tbb/concurrent_queue.h>\r
38 \r
39 #include <asmlib.h>\r
40 \r
41 #include <FreeImage.h>\r
42 \r
43 #include <vector>\r
44 \r
45 namespace caspar { namespace image {\r
46         \r
47 struct image_consumer : public core::frame_consumer\r
48 {\r
49 public:\r
50 \r
51         // frame_consumer\r
52 \r
53         void initialize(const core::video_format_desc&, int) override\r
54         {\r
55         }\r
56         \r
57         bool send(core::const_frame frame) override\r
58         {                               \r
59                 boost::thread async([frame]\r
60                 {\r
61                         try\r
62                         {\r
63                                 auto filename = u8(env::data_folder()) +  boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time()) + ".png";\r
64 \r
65                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(static_cast<int>(frame.width()), static_cast<int>(frame.height()), 32), FreeImage_Unload);\r
66                                 A_memcpy(FreeImage_GetBits(bitmap.get()), frame.image_data().begin(), frame.image_data().size());\r
67                                 FreeImage_FlipVertical(bitmap.get());\r
68                                 FreeImage_Save(FIF_PNG, bitmap.get(), filename.c_str(), 0);\r
69                         }\r
70                         catch(...)\r
71                         {\r
72                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
73                         }\r
74                 });\r
75                 async.detach();\r
76 \r
77                 return false;\r
78         }\r
79 \r
80         std::wstring print() const override\r
81         {\r
82                 return L"image[]";\r
83         }\r
84         \r
85         std::wstring name() const override\r
86         {\r
87                 return L"image";\r
88         }\r
89 \r
90         boost::property_tree::wptree info() const override\r
91         {\r
92                 boost::property_tree::wptree info;\r
93                 info.add(L"type", L"image");\r
94                 return info;\r
95         }\r
96 \r
97         int buffer_depth() const override\r
98         {\r
99                 return 0;\r
100         }\r
101 \r
102         int index() const override\r
103         {\r
104                 return 100;\r
105         }\r
106 };\r
107 \r
108 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
109 {\r
110         if(params.size() < 1 || params[0] != L"IMAGE")\r
111                 return core::frame_consumer::empty();\r
112 \r
113         return spl::make_shared<image_consumer>();\r
114 }\r
115 \r
116 }}\r