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