]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
d14bf07274a6439fb0ae99e73bd0122593c1b35f
[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/exception/exceptions.h>\r
25 #include <common/env.h>\r
26 #include <common/log/log.h>\r
27 #include <common/utility/string.h>\r
28 #include <common/concurrency/future_util.h>\r
29 \r
30 #include <core/consumer/frame_consumer.h>\r
31 #include <core/video_format.h>\r
32 #include <core/mixer/read_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 <FreeImage.h>\r
40 \r
41 #include <vector>\r
42 \r
43 namespace caspar { namespace image {\r
44         \r
45 struct image_consumer : public core::frame_consumer\r
46 {\r
47         core::video_format_desc                                 format_desc_;\r
48 public:\r
49 \r
50         // frame_consumer\r
51 \r
52         virtual void initialize(const core::video_format_desc& format_desc, int) override\r
53         {\r
54                 format_desc_ = format_desc;\r
55         }\r
56         \r
57         virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override\r
58         {                               \r
59                 auto format_desc = format_desc_;\r
60                 boost::thread async([format_desc, frame]\r
61                 {\r
62                         try\r
63                         {\r
64                                 auto filename = narrow(env::data_folder()) +  boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time()) + ".png";\r
65 \r
66                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(format_desc.width, format_desc.height, 32), FreeImage_Unload);\r
67                                 memcpy(FreeImage_GetBits(bitmap.get()), frame->image_data().begin(), frame->image_size());\r
68                                 FreeImage_FlipVertical(bitmap.get());\r
69                                 FreeImage_Save(FIF_PNG, bitmap.get(), filename.c_str(), 0);\r
70                         }\r
71                         catch(...)\r
72                         {\r
73                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
74                         }\r
75                 });\r
76                 async.detach();\r
77 \r
78                 return wrap_as_future(false);\r
79         }\r
80 \r
81         virtual std::wstring print() const override\r
82         {\r
83                 return L"image[]";\r
84         }\r
85 \r
86         virtual boost::property_tree::wptree info() const override\r
87         {\r
88                 boost::property_tree::wptree info;\r
89                 info.add(L"type", L"image-consumer");\r
90                 return info;\r
91         }\r
92 \r
93         virtual size_t buffer_depth() const override\r
94         {\r
95                 return 0;\r
96         }\r
97 \r
98         virtual int index() const override\r
99         {\r
100                 return 100;\r
101         }\r
102 };\r
103 \r
104 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
105 {\r
106         if(params.size() < 1 || params[0] != L"IMAGE")\r
107                 return core::frame_consumer::empty();\r
108 \r
109         return make_safe<image_consumer>();\r
110 }\r
111 \r
112 }}\r