]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
manually merged 44a5538 from master
[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/assign.hpp>
42 #include <boost/filesystem.hpp>
43 #include <boost/property_tree/ptree.hpp>
44 #include <boost/algorithm/string.hpp>
45
46 #include <algorithm>
47
48 using namespace boost::assign;
49
50 namespace caspar { namespace image {
51
52 std::pair<core::draw_frame, core::constraints> load_image(
53                 const spl::shared_ptr<core::frame_factory>& frame_factory,
54                 const std::wstring& filename)
55 {
56         auto bitmap = load_image(filename);
57         FreeImage_FlipVertical(bitmap.get());
58                 
59         core::pixel_format_desc desc = core::pixel_format::bgra;
60         auto width = FreeImage_GetWidth(bitmap.get());
61         auto height = FreeImage_GetHeight(bitmap.get());
62         desc.planes.push_back(core::pixel_format_desc::plane(width, height, 4));
63         auto frame = frame_factory->create_frame(bitmap.get(), desc);
64
65         std::copy_n(
66                         FreeImage_GetBits(bitmap.get()),
67                         frame.image_data(0).size(),
68                         frame.image_data(0).begin());
69         
70         return std::make_pair(
71                         core::draw_frame(std::move(frame)),
72                         core::constraints(width, height));
73 }
74
75 struct image_producer : public core::frame_producer_base
76 {       
77         monitor::basic_subject  event_subject_;
78         const std::wstring              description_;
79         const spl::shared_ptr<core::frame_factory> frame_factory_;
80         core::draw_frame                frame_;
81         core::constraints               constraints_;
82         
83         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& description) 
84                 : description_(description)
85                 , frame_factory_(frame_factory)
86                 , frame_(core::draw_frame::empty())     
87         {
88                 load(load_image(description_));
89
90                 CASPAR_LOG(info) << print() << L" Initialized";
91         }
92
93         image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size) 
94                 : description_(L"png from memory")
95                 , frame_factory_(frame_factory)
96                 , frame_(core::draw_frame::empty())     
97         {
98                 load(load_png_from_memory(png_data, size));
99
100                 CASPAR_LOG(info) << print() << L" Initialized";
101         }
102
103         void load(const std::shared_ptr<FIBITMAP>& bitmap)
104         {
105                 FreeImage_FlipVertical(bitmap.get());
106                 core::pixel_format_desc desc;
107                 desc.format = core::pixel_format::bgra;
108                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));
109                 auto frame = frame_factory_->create_frame(this, desc);
110  
111                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data().size(), frame.image_data().begin());
112                 frame_ = core::draw_frame(std::move(frame));
113         }
114         
115         // frame_producer
116
117         core::draw_frame receive_impl() override
118         {
119                 event_subject_ << monitor::event("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         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     
153         {
154                 return event_subject_.subscribe(o);
155         }
156
157         void unsubscribe(const monitor::observable::observer_ptr& o) override           
158         {
159                 return event_subject_.unsubscribe(o);
160         }
161 };
162
163 class ieq
164 {
165         std::wstring test_;
166 public:
167         ieq(const std::wstring& test)
168                 : test_(test)
169         {
170         }
171
172         bool operator()(const std::wstring& elem) const
173         {
174                 return boost::iequals(elem, test_);
175         }
176 };
177
178 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)
179 {
180         static const std::vector<std::wstring> extensions = list_of(L".png")(L".tga")(L".bmp")(L".jpg")(L".jpeg")(L".gif")(L".tiff")(L".tif")(L".jp2")(L".jpx")(L".j2k")(L".j2c");
181
182         if (boost::iequals(params[0], L"[IMG_SEQUENCE]"))
183         {
184                 if (params.size() != 2)
185                         return core::frame_producer::empty();
186
187                 auto dir = boost::filesystem::path(env::media_folder() + params[1]).parent_path();
188                 auto basename = boost::filesystem3::basename(params[1]);
189                 std::set<std::wstring> files;
190                 boost::filesystem::directory_iterator end;
191
192                 for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
193                 {
194                         auto name = it->path().filename().wstring();
195
196                         if (!boost::algorithm::istarts_with(name, basename))
197                                 continue;
198
199                         auto extension = it->path().extension().wstring();
200
201                         if (std::find_if(extensions.begin(), extensions.end(), ieq(extension)) == extensions.end())
202                                 continue;
203
204                         files.insert(it->path().wstring());
205                 }
206
207                 if (files.empty())
208                         return core::frame_producer::empty();
209
210                 int width = -1;
211                 int height = -1;
212                 std::vector<core::draw_frame> frames;
213                 frames.reserve(files.size());
214
215                 BOOST_FOREACH(auto& file, files)
216                 {
217                         auto frame = load_image(frame_factory, file);
218
219                         if (width == -1)
220                         {
221                                 width = static_cast<int>(frame.second.width.get());
222                                 height = static_cast<int>(frame.second.height.get());
223                         }
224
225                         frames.push_back(std::move(frame.first));
226                 }
227
228                 return core::create_const_producer(std::move(frames), width, height);
229         }
230         else if(boost::iequals(params[0], L"[PNG_BASE64]"))
231         {
232                 if (params.size() < 2)
233                         return core::frame_producer::empty();
234
235                 auto png_data = from_base64(std::string(params[1].begin(), params[1].end()));
236
237                 return spl::make_shared<image_producer>(frame_factory, png_data.data(), png_data.size());
238         }
239
240         std::wstring filename = env::media_folder() + params[0];
241
242         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
243                 {                       
244                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
245                 });
246
247         if(ext == extensions.end())
248                 return core::frame_producer::empty();
249
250         return spl::make_shared<image_producer>(frame_factory, filename + *ext);
251 }
252
253
254 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)
255 {
256         return caspar::image::create_producer(frame_factory, format_desc, params);
257 }
258 }}