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