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