]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
Refactored to use range based for instead of BOOST_FOREACH
[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
41 #include <boost/filesystem.hpp>
42 #include <boost/property_tree/ptree.hpp>
43 #include <boost/algorithm/string.hpp>
44
45 #include <algorithm>
46 #include <set>
47
48 namespace caspar { namespace image {
49
50 std::pair<core::draw_frame, core::constraints> load_image(
51                 const spl::shared_ptr<core::frame_factory>& frame_factory,
52                 const std::wstring& filename)
53 {
54         auto bitmap = load_image(filename);
55         FreeImage_FlipVertical(bitmap.get());
56                 
57         core::pixel_format_desc desc = core::pixel_format::bgra;
58         auto width = FreeImage_GetWidth(bitmap.get());
59         auto height = FreeImage_GetHeight(bitmap.get());
60         desc.planes.push_back(core::pixel_format_desc::plane(width, height, 4));
61         auto frame = frame_factory->create_frame(bitmap.get(), desc);
62
63         std::copy_n(
64                         FreeImage_GetBits(bitmap.get()),
65                         frame.image_data(0).size(),
66                         frame.image_data(0).begin());
67         
68         return std::make_pair(
69                         core::draw_frame(std::move(frame)),
70                         core::constraints(width, height));
71 }
72
73 struct image_producer : public core::frame_producer_base
74 {       
75         core::monitor::subject                                          monitor_subject_;
76         const std::wstring                                                      description_;
77         const spl::shared_ptr<core::frame_factory>      frame_factory_;
78         core::draw_frame                                                        frame_;
79         core::constraints                                                       constraints_;
80         
81         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& description) 
82                 : description_(description)
83                 , frame_factory_(frame_factory)
84                 , frame_(core::draw_frame::empty())     
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                 , frame_(core::draw_frame::empty())     
95         {
96                 load(load_png_from_memory(png_data, size));
97
98                 CASPAR_LOG(info) << print() << L" Initialized";
99         }
100
101         void load(const std::shared_ptr<FIBITMAP>& bitmap)
102         {
103                 FreeImage_FlipVertical(bitmap.get());
104                 core::pixel_format_desc desc;
105                 desc.format = core::pixel_format::bgra;
106                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));
107                 auto frame = frame_factory_->create_frame(this, desc);
108  
109                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data().size(), frame.image_data().begin());
110                 frame_ = core::draw_frame(std::move(frame));
111                 constraints_.width.set(FreeImage_GetWidth(bitmap.get()));
112                 constraints_.height.set(FreeImage_GetHeight(bitmap.get()));
113         }
114         
115         // frame_producer
116
117         core::draw_frame receive_impl() override
118         {
119                 monitor_subject_ << core::monitor::message("/file/path") % description_;
120
121                 return frame_;
122         }
123
124         core::draw_frame create_thumbnail_frame() override
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 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
174 {
175         static const auto extensions = {
176                 L".png",
177                 L".tga",
178                 L".bmp",
179                 L".jpg",
180                 L".jpeg",
181                 L".gif",
182                 L".tiff",
183                 L".tif",
184                 L".jp2",
185                 L".jpx",
186                 L".j2k",
187                 L".j2c"
188         };
189
190         if (boost::iequals(params[0], L"[IMG_SEQUENCE]"))
191         {
192                 if (params.size() != 2)
193                         return core::frame_producer::empty();
194
195                 auto dir = boost::filesystem::path(env::media_folder() + params[1]).parent_path();
196                 auto basename = boost::filesystem::basename(params[1]);
197                 std::set<std::wstring> files;
198                 boost::filesystem::directory_iterator end;
199
200                 for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
201                 {
202                         auto name = it->path().filename().wstring();
203
204                         if (!boost::algorithm::istarts_with(name, basename))
205                                 continue;
206
207                         auto extension = it->path().extension().wstring();
208
209                         if (std::find_if(extensions.begin(), extensions.end(), ieq(extension)) == extensions.end())
210                                 continue;
211
212                         files.insert(it->path().wstring());
213                 }
214
215                 if (files.empty())
216                         return core::frame_producer::empty();
217
218                 int width = -1;
219                 int height = -1;
220                 std::vector<core::draw_frame> frames;
221                 frames.reserve(files.size());
222
223                 for (auto& file : files)
224                 {
225                         auto frame = load_image(frame_factory, file);
226
227                         if (width == -1)
228                         {
229                                 width = static_cast<int>(frame.second.width.get());
230                                 height = static_cast<int>(frame.second.height.get());
231                         }
232
233                         frames.push_back(std::move(frame.first));
234                 }
235
236                 return core::create_const_producer(std::move(frames), width, height);
237         }
238         else if(boost::iequals(params[0], L"[PNG_BASE64]"))
239         {
240                 if (params.size() < 2)
241                         return core::frame_producer::empty();
242
243                 auto png_data = from_base64(std::string(params[1].begin(), params[1].end()));
244
245                 return spl::make_shared<image_producer>(frame_factory, png_data.data(), png_data.size());
246         }
247
248         std::wstring filename = env::media_folder() + params[0];
249
250         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
251                 {                       
252                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
253                 });
254
255         if(ext == extensions.end())
256                 return core::frame_producer::empty();
257
258         return spl::make_shared<image_producer>(frame_factory, filename + *ext);
259 }
260
261
262 spl::shared_ptr<core::frame_producer> create_thumbnail_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
263 {
264         return caspar::image::create_producer(frame_factory, format_desc, params);
265 }
266 }}