]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
529fdab42868c2dd92429fc87cfe00d1e76d5220
[casparcg] / modules / image / consumer / image_consumer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20  \r
21 #include "image_consumer.h"\r
22 \r
23 #include <common/exception/exceptions.h>\r
24 #include <common/env.h>\r
25 #include <common/log/log.h>\r
26 #include <common/utility/string.h>\r
27 \r
28 #include <core/consumer/frame_consumer.h>\r
29 #include <core/video_format.h>\r
30 #include <core/mixer/read_frame.h>\r
31 \r
32 #include <boost/date_time/posix_time/posix_time.hpp>\r
33 #include <boost/thread.hpp>\r
34 \r
35 #include <tbb/concurrent_queue.h>\r
36 \r
37 #include <FreeImage.h>\r
38 \r
39 #include <vector>\r
40 \r
41 namespace caspar {\r
42         \r
43 struct image_consumer : public core::frame_consumer\r
44 {\r
45         core::video_format_desc                                 format_desc_;\r
46         std::vector<safe_ptr<core::read_frame>> frames_;\r
47 public:\r
48 \r
49         virtual void initialize(const core::video_format_desc& format_desc)\r
50         {\r
51                 format_desc_ = format_desc;\r
52         }\r
53         \r
54         virtual bool send(const safe_ptr<core::read_frame>& frame)\r
55         {                               \r
56                 frames_.push_back(frame);\r
57 \r
58                 if(frames_.size() < core::consumer_buffer_depth())\r
59                         return true;\r
60 \r
61                 auto my_frame = frames_.front();\r
62                 boost::thread async([=]\r
63                 {\r
64                         try\r
65                         {\r
66                                 auto filename = narrow(env::data_folder()) +  boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time()) + ".png";\r
67 \r
68                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(format_desc_.width, format_desc_.height, 32), FreeImage_Unload);\r
69                                 memcpy(FreeImage_GetBits(bitmap.get()), my_frame->image_data().begin(), my_frame->image_size());\r
70                                 FreeImage_FlipVertical(bitmap.get());\r
71                                 FreeImage_Save(FIF_PNG, bitmap.get(), filename.c_str(), 0);\r
72                         }\r
73                         catch(...)\r
74                         {\r
75                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
76                         }\r
77                 });\r
78                 async.detach();\r
79 \r
80                 return false;\r
81         }\r
82 \r
83         virtual std::wstring print() const\r
84         {\r
85                 return L"image[]";\r
86         }\r
87 \r
88         virtual const core::video_format_desc& get_video_format_desc() const\r
89         {\r
90                 return format_desc_;\r
91         }\r
92 };\r
93 \r
94 safe_ptr<core::frame_consumer> create_image_consumer(const std::vector<std::wstring>& params)\r
95 {\r
96         if(params.size() < 1 || params[0] != L"IMAGE")\r
97                 return core::frame_consumer::empty();\r
98 \r
99         return make_safe<image_consumer>();\r
100 }\r
101 \r
102 \r
103 }\r