]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
0da1a981c39e69ec3efdf7093db789262eadf51b
[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/os/filesystem.h>
44
45 #include <boost/filesystem.hpp>
46 #include <boost/property_tree/ptree.hpp>
47 #include <boost/algorithm/string.hpp>
48
49 #include <algorithm>
50 #include <set>
51
52 namespace caspar { namespace image {
53
54 std::pair<core::draw_frame, core::constraints> load_image(
55                 const spl::shared_ptr<core::frame_factory>& frame_factory,
56                 const std::wstring& filename)
57 {
58         auto bitmap = load_image(filename);
59         FreeImage_FlipVertical(bitmap.get());
60                 
61         core::pixel_format_desc desc = core::pixel_format::bgra;
62         auto width = FreeImage_GetWidth(bitmap.get());
63         auto height = FreeImage_GetHeight(bitmap.get());
64         desc.planes.push_back(core::pixel_format_desc::plane(width, height, 4));
65         auto frame = frame_factory->create_frame(bitmap.get(), desc, core::audio_channel_layout::invalid());
66
67         std::copy_n(
68                         FreeImage_GetBits(bitmap.get()),
69                         frame.image_data(0).size(),
70                         frame.image_data(0).begin());
71         
72         return std::make_pair(
73                         core::draw_frame(std::move(frame)),
74                         core::constraints(width, height));
75 }
76
77 struct image_producer : public core::frame_producer_base
78 {       
79         core::monitor::subject                                          monitor_subject_;
80         const std::wstring                                                      description_;
81         const spl::shared_ptr<core::frame_factory>      frame_factory_;
82         core::draw_frame                                                        frame_                          = core::draw_frame::empty();
83         core::constraints                                                       constraints_;
84         
85         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& description, bool thumbnail_mode) 
86                 : description_(description)
87                 , frame_factory_(frame_factory)
88         {
89                 load(load_image(description_));
90
91                 if (thumbnail_mode)
92                         CASPAR_LOG(debug) << print() << L" Initialized";
93                 else
94                         CASPAR_LOG(info) << print() << L" Initialized";
95         }
96
97         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size) 
98                 : description_(L"png from memory")
99                 , frame_factory_(frame_factory)
100         {
101                 load(load_png_from_memory(png_data, size));
102
103                 CASPAR_LOG(info) << print() << L" Initialized";
104         }
105
106         void load(const std::shared_ptr<FIBITMAP>& bitmap)
107         {
108                 FreeImage_FlipVertical(bitmap.get());
109                 core::pixel_format_desc desc;
110                 desc.format = core::pixel_format::bgra;
111                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));
112                 auto frame = frame_factory_->create_frame(this, desc, core::audio_channel_layout::invalid());
113  
114                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data().size(), frame.image_data().begin());
115                 frame_ = core::draw_frame(std::move(frame));
116                 constraints_.width.set(FreeImage_GetWidth(bitmap.get()));
117                 constraints_.height.set(FreeImage_GetHeight(bitmap.get()));
118         }
119         
120         // frame_producer
121
122         core::draw_frame receive_impl() override
123         {
124                 monitor_subject_ << core::monitor::message("/file/path") % description_;
125
126                 return frame_;
127         }
128
129         core::constraints& pixel_constraints() override
130         {
131                 return constraints_;
132         }
133                         
134         std::wstring print() const override
135         {
136                 return L"image_producer[" + description_ + L"]";
137         }
138
139         std::wstring name() const override
140         {
141                 return L"image";
142         }
143
144         boost::property_tree::wptree info() const override
145         {
146                 boost::property_tree::wptree info;
147                 info.add(L"type", L"image");
148                 info.add(L"location", description_);
149                 return info;
150         }
151
152         core::monitor::subject& monitor_output() 
153         {
154                 return monitor_subject_;
155         }
156 };
157
158 class ieq
159 {
160         std::wstring test_;
161 public:
162         ieq(const std::wstring& test)
163                 : test_(test)
164         {
165         }
166
167         bool operator()(const std::wstring& elem) const
168         {
169                 return boost::iequals(elem, test_);
170         }
171 };
172
173 void describe_producer(core::help_sink& sink, const core::help_repository& repo)
174 {
175         sink.short_description(L"Loads a still image.");
176         sink.syntax(L"{[image_file:string]},{[PNG_BASE64] [encoded:string]}");
177         sink.para()->text(L"Loads a still image, either from disk or via a base64 encoded image submitted via AMCP.");
178         sink.para()->text(L"Examples:");
179         sink.example(L">> PLAY 1-10 image_file", L"Plays an image from the media folder.");
180         sink.example(L">> PLAY 1-10 [PNG_BASE64] data...", L"Plays a PNG image transferred as a base64 encoded string.");
181 }
182
183 static const auto g_extensions = {
184         L".png",
185         L".tga",
186         L".bmp",
187         L".jpg",
188         L".jpeg",
189         L".gif",
190         L".tiff",
191         L".tif",
192         L".jp2",
193         L".jpx",
194         L".j2k",
195         L".j2c"
196 };
197
198 spl::shared_ptr<core::frame_producer> create_producer(const core::frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
199 {
200
201         if (boost::iequals(params.at(0), L"[IMG_SEQUENCE]"))
202         {
203                 if (params.size() != 2)
204                         return core::frame_producer::empty();
205
206                 auto dir = boost::filesystem::path(env::media_folder() + params.at(1)).parent_path();
207                 auto basename = boost::filesystem::basename(params.at(1));
208                 std::set<std::wstring> files;
209                 boost::filesystem::directory_iterator end;
210
211                 for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
212                 {
213                         auto name = it->path().filename().wstring();
214
215                         if (!boost::algorithm::istarts_with(name, basename))
216                                 continue;
217
218                         auto extension = it->path().extension().wstring();
219
220                         if (std::find_if(g_extensions.begin(), g_extensions.end(), ieq(extension)) == g_extensions.end())
221                                 continue;
222
223                         files.insert(it->path().wstring());
224                 }
225
226                 if (files.empty())
227                         return core::frame_producer::empty();
228
229                 int width = -1;
230                 int height = -1;
231                 std::vector<core::draw_frame> frames;
232                 frames.reserve(files.size());
233
234                 for (auto& file : files)
235                 {
236                         auto frame = load_image(dependencies.frame_factory, file);
237
238                         if (width == -1)
239                         {
240                                 width = static_cast<int>(frame.second.width.get());
241                                 height = static_cast<int>(frame.second.height.get());
242                         }
243
244                         frames.push_back(std::move(frame.first));
245                 }
246
247                 return core::create_const_producer(std::move(frames), width, height);
248         }
249         else if(boost::iequals(params.at(0), L"[PNG_BASE64]"))
250         {
251                 if (params.size() < 2)
252                         return core::frame_producer::empty();
253
254                 auto png_data = from_base64(std::string(params.at(1).begin(), params.at(1).end()));
255
256                 return spl::make_shared<image_producer>(dependencies.frame_factory, png_data.data(), png_data.size());
257         }
258
259         std::wstring filename = env::media_folder() + params.at(0);
260
261         auto ext = std::find_if(g_extensions.begin(), g_extensions.end(), [&](const std::wstring& ex) -> bool
262         {
263                 auto file = caspar::find_case_insensitive(boost::filesystem::path(filename).wstring() + ex);
264
265                 return static_cast<bool>(file);
266         });
267
268         if(ext == g_extensions.end())
269                 return core::frame_producer::empty();
270
271         return spl::make_shared<image_producer>(dependencies.frame_factory, *caspar::find_case_insensitive(filename + *ext), false);
272 }
273
274
275 core::draw_frame create_thumbnail(const core::frame_producer_dependencies& dependencies, const std::wstring& media_file)
276 {
277         std::wstring filename = env::media_folder() + media_file;
278
279         auto ext = std::find_if(g_extensions.begin(), g_extensions.end(), [&](const std::wstring& ex) -> bool
280         {
281                 auto file = caspar::find_case_insensitive(boost::filesystem::path(filename).wstring() + ex);
282
283                 return static_cast<bool>(file);
284         });
285
286         if (ext == g_extensions.end())
287                 return core::draw_frame::empty();
288
289         spl::shared_ptr<core::frame_producer> producer = spl::make_shared<image_producer>(
290                         dependencies.frame_factory,
291                         *caspar::find_case_insensitive(filename + *ext),
292                         true);
293         
294         return producer->receive();
295 }
296
297 }}