]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
Merged most recent OSC changes
[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         core::monitor::subject                                          monitor_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                 constraints_.width.set(FreeImage_GetWidth(bitmap.get()));
114                 constraints_.height.set(FreeImage_GetHeight(bitmap.get()));
115         }
116         
117         // frame_producer
118
119         core::draw_frame receive_impl() override
120         {
121                 monitor_subject_ << core::monitor::message("/file/path") % description_;
122
123                 return frame_;
124         }
125
126         core::draw_frame create_thumbnail_frame() override
127         {
128                 return frame_;
129         }
130
131         core::constraints& pixel_constraints() override
132         {
133                 return constraints_;
134         }
135                         
136         std::wstring print() const override
137         {
138                 return L"image_producer[" + description_ + L"]";
139         }
140
141         std::wstring name() const override
142         {
143                 return L"image";
144         }
145
146         boost::property_tree::wptree info() const override
147         {
148                 boost::property_tree::wptree info;
149                 info.add(L"type", L"image");
150                 info.add(L"location", description_);
151                 return info;
152         }
153
154         core::monitor::subject& monitor_output() 
155         {
156                 return monitor_subject_;
157         }
158 };
159
160 class ieq
161 {
162         std::wstring test_;
163 public:
164         ieq(const std::wstring& test)
165                 : test_(test)
166         {
167         }
168
169         bool operator()(const std::wstring& elem) const
170         {
171                 return boost::iequals(elem, test_);
172         }
173 };
174
175 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)
176 {
177         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");
178
179         if (boost::iequals(params[0], L"[IMG_SEQUENCE]"))
180         {
181                 if (params.size() != 2)
182                         return core::frame_producer::empty();
183
184                 auto dir = boost::filesystem::path(env::media_folder() + params[1]).parent_path();
185                 auto basename = boost::filesystem3::basename(params[1]);
186                 std::set<std::wstring> files;
187                 boost::filesystem::directory_iterator end;
188
189                 for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
190                 {
191                         auto name = it->path().filename().wstring();
192
193                         if (!boost::algorithm::istarts_with(name, basename))
194                                 continue;
195
196                         auto extension = it->path().extension().wstring();
197
198                         if (std::find_if(extensions.begin(), extensions.end(), ieq(extension)) == extensions.end())
199                                 continue;
200
201                         files.insert(it->path().wstring());
202                 }
203
204                 if (files.empty())
205                         return core::frame_producer::empty();
206
207                 int width = -1;
208                 int height = -1;
209                 std::vector<core::draw_frame> frames;
210                 frames.reserve(files.size());
211
212                 BOOST_FOREACH(auto& file, files)
213                 {
214                         auto frame = load_image(frame_factory, file);
215
216                         if (width == -1)
217                         {
218                                 width = static_cast<int>(frame.second.width.get());
219                                 height = static_cast<int>(frame.second.height.get());
220                         }
221
222                         frames.push_back(std::move(frame.first));
223                 }
224
225                 return core::create_const_producer(std::move(frames), width, height);
226         }
227         else if(boost::iequals(params[0], L"[PNG_BASE64]"))
228         {
229                 if (params.size() < 2)
230                         return core::frame_producer::empty();
231
232                 auto png_data = from_base64(std::string(params[1].begin(), params[1].end()));
233
234                 return spl::make_shared<image_producer>(frame_factory, png_data.data(), png_data.size());
235         }
236
237         std::wstring filename = env::media_folder() + params[0];
238
239         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
240                 {                       
241                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
242                 });
243
244         if(ext == extensions.end())
245                 return core::frame_producer::empty();
246
247         return spl::make_shared<image_producer>(frame_factory, filename + *ext);
248 }
249
250
251 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)
252 {
253         return caspar::image::create_producer(frame_factory, format_desc, params);
254 }
255 }}