]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
Fix a few Clang warnings.
[casparcg] / modules / image / producer / image_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "image_producer.h"
23
24 #include "../util/image_loader.h"
25
26 #include <core/video_format.h>
27
28 #include <core/producer/frame_producer.h>
29 #include <core/producer/scene/const_producer.h>
30 #include <core/frame/frame.h>
31 #include <core/frame/draw_frame.h>
32 #include <core/frame/frame_factory.h>
33 #include <core/frame/pixel_format.h>
34 #include <core/frame/audio_channel_layout.h>
35 #include <core/monitor/monitor.h>
36 #include <core/help/help_sink.h>
37 #include <core/help/help_repository.h>
38
39 #include <common/env.h>
40 #include <common/log.h>
41 #include <common/array.h>
42 #include <common/base64.h>
43 #include <common/param.h>
44 #include <common/os/filesystem.h>
45
46 #include <boost/filesystem.hpp>
47 #include <boost/property_tree/ptree.hpp>
48 #include <boost/algorithm/string.hpp>
49
50 #include <algorithm>
51 #include <set>
52
53 namespace caspar { namespace image {
54
55 std::pair<core::draw_frame, core::constraints> load_image(
56                 const spl::shared_ptr<core::frame_factory>& frame_factory,
57                 const std::wstring& filename)
58 {
59         auto bitmap = load_image(filename);
60         FreeImage_FlipVertical(bitmap.get());
61
62         core::pixel_format_desc desc = core::pixel_format::bgra;
63         auto width = FreeImage_GetWidth(bitmap.get());
64         auto height = FreeImage_GetHeight(bitmap.get());
65         desc.planes.push_back(core::pixel_format_desc::plane(width, height, 4));
66         auto frame = frame_factory->create_frame(bitmap.get(), desc, core::audio_channel_layout::invalid());
67
68         std::copy_n(
69                         FreeImage_GetBits(bitmap.get()),
70                         frame.image_data(0).size(),
71                         frame.image_data(0).begin());
72
73         return std::make_pair(
74                         core::draw_frame(std::move(frame)),
75                         core::constraints(width, height));
76 }
77
78 struct image_producer : public core::frame_producer_base
79 {
80         core::monitor::subject                                          monitor_subject_;
81         const std::wstring                                                      description_;
82         const spl::shared_ptr<core::frame_factory>      frame_factory_;
83         const uint32_t                                                          length_;
84         core::draw_frame                                                        frame_                          = core::draw_frame::empty();
85         core::constraints                                                       constraints_;
86
87         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& description, bool thumbnail_mode, uint32_t length)
88                 : description_(description)
89                 , frame_factory_(frame_factory)
90                 , length_(length)
91         {
92                 load(load_image(description_));
93
94                 if (thumbnail_mode)
95                         CASPAR_LOG(debug) << print() << L" Initialized";
96                 else
97                         CASPAR_LOG(info) << print() << L" Initialized";
98         }
99
100         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size, uint32_t length)
101                 : description_(L"png from memory")
102                 , frame_factory_(frame_factory)
103                 , length_(length)
104         {
105                 load(load_png_from_memory(png_data, size));
106
107                 CASPAR_LOG(info) << print() << L" Initialized";
108         }
109
110         void load(const std::shared_ptr<FIBITMAP>& bitmap)
111         {
112                 FreeImage_FlipVertical(bitmap.get());
113                 auto longest_side = static_cast<int>(std::max(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get())));
114
115                 if (longest_side > frame_factory_->get_max_frame_size())
116                         CASPAR_THROW_EXCEPTION(user_error() << msg_info("Image too large for texture"));
117
118                 core::pixel_format_desc desc;
119                 desc.format = core::pixel_format::bgra;
120                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));
121                 auto frame = frame_factory_->create_frame(this, desc, core::audio_channel_layout::invalid());
122
123                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data().size(), frame.image_data().begin());
124                 frame_ = core::draw_frame(std::move(frame));
125                 constraints_.width.set(FreeImage_GetWidth(bitmap.get()));
126                 constraints_.height.set(FreeImage_GetHeight(bitmap.get()));
127         }
128
129         // frame_producer
130
131         core::draw_frame receive_impl() override
132         {
133                 monitor_subject_ << core::monitor::message("/file/path") % description_;
134
135                 return frame_;
136         }
137
138         uint32_t nb_frames() const override
139         {
140                 return length_;
141         }
142
143         core::constraints& pixel_constraints() override
144         {
145                 return constraints_;
146         }
147
148         std::wstring print() const override
149         {
150                 return L"image_producer[" + description_ + L"]";
151         }
152
153         std::wstring name() const override
154         {
155                 return L"image";
156         }
157
158         boost::property_tree::wptree info() const override
159         {
160                 boost::property_tree::wptree info;
161                 info.add(L"type", L"image");
162                 info.add(L"location", description_);
163                 return info;
164         }
165
166         core::monitor::subject& monitor_output() override
167         {
168                 return monitor_subject_;
169         }
170 };
171
172 class ieq
173 {
174         std::wstring test_;
175 public:
176         ieq(const std::wstring& test)
177                 : test_(test)
178         {
179         }
180
181         bool operator()(const std::wstring& elem) const
182         {
183                 return boost::iequals(elem, test_);
184         }
185 };
186
187 void describe_producer(core::help_sink& sink, const core::help_repository& repo)
188 {
189         sink.short_description(L"Loads a still image.");
190         sink.syntax(L"{[image_file:string]},{[PNG_BASE64] [encoded:string]} {LENGTH [length:int]}");
191         sink.para()->text(L"Loads a still image, either from disk or via a base64 encoded image submitted via AMCP.");
192         sink.para()->text(L"The ")->code(L"length")->text(L" parameter can be used to limit the number of frames that the image will be shown for.");
193         sink.para()->text(L"Examples:");
194         sink.example(L">> PLAY 1-10 image_file", L"Plays an image from the media folder.");
195         sink.example(L">> PLAY 1-10 [PNG_BASE64] data...", L"Plays a PNG image transferred as a base64 encoded string.");
196         sink.example(
197                                 L">> PLAY 1-10 slide_show1 LENGTH 100\n"
198                                 L">> LOADBG 1-10 slide_show2 LENGTH 100 MIX 20 AUTO\n"
199                                 L"delay until foreground layer becomes slide_show2 and then\n"
200                                 L">> LOADBG 1-10 slide_show3 LENGTH 100 MIX 20 AUTO\n"
201                                 L"delay until foreground layer becomes slide_show3 and then\n"
202                                 L">> LOADBG 1-10 EMPTY MIX 20 AUTO\n", L"Plays a slide show of 3 images for 100 frames each and fades to black.");
203 }
204
205 spl::shared_ptr<core::frame_producer> create_producer(const core::frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
206 {
207         auto length = get_param(L"LENGTH", params, std::numeric_limits<uint32_t>::max());
208
209         if (boost::iequals(params.at(0), L"[IMG_SEQUENCE]"))
210         {
211                 if (params.size() != 2)
212                         return core::frame_producer::empty();
213
214                 auto dir = boost::filesystem::path(env::media_folder() + params.at(1)).parent_path();
215                 auto basename = boost::filesystem::basename(params.at(1));
216                 std::set<std::wstring> files;
217                 boost::filesystem::directory_iterator end;
218
219                 for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
220                 {
221                         auto name = it->path().filename().wstring();
222
223                         if (!boost::algorithm::istarts_with(name, basename))
224                                 continue;
225
226                         auto extension = it->path().extension().wstring();
227
228                         if (std::find_if(supported_extensions().begin(), supported_extensions().end(), ieq(extension)) == supported_extensions().end())
229                                 continue;
230
231                         files.insert(it->path().wstring());
232                 }
233
234                 if (files.empty())
235                         return core::frame_producer::empty();
236
237                 int width = -1;
238                 int height = -1;
239                 std::vector<core::draw_frame> frames;
240                 frames.reserve(files.size());
241
242                 for (auto& file : files)
243                 {
244                         auto frame = load_image(dependencies.frame_factory, file);
245
246                         if (width == -1)
247                         {
248                                 width = static_cast<int>(frame.second.width.get());
249                                 height = static_cast<int>(frame.second.height.get());
250                         }
251
252                         frames.push_back(std::move(frame.first));
253                 }
254
255                 return core::create_const_producer(std::move(frames), width, height);
256         }
257         else if(boost::iequals(params.at(0), L"[PNG_BASE64]"))
258         {
259                 if (params.size() < 2)
260                         return core::frame_producer::empty();
261
262                 auto png_data = from_base64(std::string(params.at(1).begin(), params.at(1).end()));
263
264                 return spl::make_shared<image_producer>(dependencies.frame_factory, png_data.data(), png_data.size(), length);
265         }
266
267         std::wstring filename = env::media_folder() + params.at(0);
268
269         auto ext = std::find_if(supported_extensions().begin(), supported_extensions().end(), [&](const std::wstring& ex) -> bool
270         {
271                 auto file = caspar::find_case_insensitive(boost::filesystem::path(filename).wstring() + ex);
272
273                 return static_cast<bool>(file);
274         });
275
276         if(ext == supported_extensions().end())
277                 return core::frame_producer::empty();
278
279         return spl::make_shared<image_producer>(dependencies.frame_factory, *caspar::find_case_insensitive(filename + *ext), false, length);
280 }
281
282
283 core::draw_frame create_thumbnail(const core::frame_producer_dependencies& dependencies, const std::wstring& media_file)
284 {
285         std::wstring filename = env::media_folder() + media_file;
286
287         auto ext = std::find_if(supported_extensions().begin(), supported_extensions().end(), [&](const std::wstring& ex) -> bool
288         {
289                 auto file = caspar::find_case_insensitive(boost::filesystem::path(filename).wstring() + ex);
290
291                 return static_cast<bool>(file);
292         });
293
294         if (ext == supported_extensions().end())
295                 return core::draw_frame::empty();
296
297         spl::shared_ptr<core::frame_producer> producer = spl::make_shared<image_producer>(
298                         dependencies.frame_factory,
299                         *caspar::find_case_insensitive(filename + *ext),
300                         true,
301                         1);
302
303         return producer->receive();
304 }
305
306 }}