]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
Rebase parameters branch on current master.
[casparcg] / modules / image / consumer / image_consumer.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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/parameters/parameters.h>\r
31 #include <core/consumer/frame_consumer.h>\r
32 #include <core/video_format.h>\r
33 #include <core/mixer/read_frame.h>\r
34 \r
35 #include <boost/date_time/posix_time/posix_time.hpp>\r
36 #include <boost/thread.hpp>\r
37 \r
38 #include <tbb/concurrent_queue.h>\r
39 \r
40 #include <FreeImage.h>\r
41 #include <vector>\r
42 #include <algorithm>\r
43 \r
44 #include "../util/image_view.h"\r
45 \r
46 namespace caspar { namespace image {\r
47 \r
48 void write_cropped_png(\r
49                 const safe_ptr<core::read_frame>& frame,\r
50                 const core::video_format_desc& format_desc,\r
51                 const boost::filesystem::wpath& output_file,\r
52                 int width,\r
53                 int height)\r
54 {\r
55         auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(width, height, 32), FreeImage_Unload);\r
56         image_view<bgra_pixel> destination_view(FreeImage_GetBits(bitmap.get()), width, height);\r
57         image_view<bgra_pixel> complete_frame(const_cast<uint8_t*>(frame->image_data().begin()), format_desc.width, format_desc.height);\r
58         auto thumbnail_view = complete_frame.subview(0, 0, width, height);\r
59 \r
60         std::copy(thumbnail_view.begin(), thumbnail_view.end(), destination_view.begin());\r
61         FreeImage_FlipVertical(bitmap.get());\r
62         FreeImage_SaveU(FIF_PNG, bitmap.get(), output_file.string().c_str(), 0);\r
63 }\r
64 \r
65 struct image_consumer : public core::frame_consumer\r
66 {\r
67         core::video_format_desc format_desc_;\r
68         std::wstring                    filename_;\r
69 public:\r
70 \r
71         // frame_consumer\r
72 \r
73         image_consumer(const std::wstring& filename)\r
74                 : filename_(filename)\r
75         {\r
76         }\r
77 \r
78         virtual void initialize(const core::video_format_desc& format_desc, int) override\r
79         {\r
80                 format_desc_ = format_desc;\r
81         }\r
82         \r
83         virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override\r
84         {                               \r
85                 auto format_desc = format_desc_;\r
86                 auto filename = filename_;\r
87 \r
88                 boost::thread async([format_desc, frame, filename]\r
89                 {\r
90                         try\r
91                         {\r
92                                 auto filename2 = filename;\r
93 \r
94                                 if (filename2.empty())\r
95                                         filename2 = env::media_folder() + widen(boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time())) + L".png";\r
96                                 else\r
97                                         filename2 = env::media_folder() + filename2 + L".png";\r
98 \r
99                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(format_desc.width, format_desc.height, 32), FreeImage_Unload);\r
100                                 memcpy(FreeImage_GetBits(bitmap.get()), frame->image_data().begin(), frame->image_size());\r
101                                 FreeImage_FlipVertical(bitmap.get());\r
102                                 FreeImage_SaveU(FIF_PNG, bitmap.get(), filename2.c_str(), 0);\r
103                         }\r
104                         catch(...)\r
105                         {\r
106                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
107                         }\r
108                 });\r
109                 async.detach();\r
110 \r
111                 return wrap_as_future(false);\r
112         }\r
113 \r
114         virtual std::wstring print() const override\r
115         {\r
116                 return L"image[]";\r
117         }\r
118 \r
119         virtual boost::property_tree::wptree info() const override\r
120         {\r
121                 boost::property_tree::wptree info;\r
122                 info.add(L"type", L"image-consumer");\r
123                 return info;\r
124         }\r
125 \r
126         virtual size_t buffer_depth() const override\r
127         {\r
128                 return 0;\r
129         }\r
130 \r
131         virtual int index() const override\r
132         {\r
133                 return 100;\r
134         }\r
135 };\r
136 \r
137 safe_ptr<core::frame_consumer> create_consumer(const core::parameters& params)\r
138 {\r
139         if(params.size() < 1 || params[0] != L"IMAGE")\r
140                 return core::frame_consumer::empty();\r
141 \r
142         std::wstring filename;\r
143 \r
144         if (params.size() > 1)\r
145                 filename = params[1];\r
146 \r
147         return make_safe<image_consumer>(filename);\r
148 }\r
149 \r
150 }}\r