2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "image_producer.h"
24 #include "../util/image_loader.h"
26 #include <core/video_format.h>
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>
36 #include <common/env.h>
37 #include <common/log.h>
38 #include <common/array.h>
39 #include <common/base64.h>
41 #include <boost/filesystem.hpp>
42 #include <boost/property_tree/ptree.hpp>
43 #include <boost/algorithm/string.hpp>
44 #include <boost/foreach.hpp>
49 namespace caspar { namespace image {
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)
55 auto bitmap = load_image(filename);
56 FreeImage_FlipVertical(bitmap.get());
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);
65 FreeImage_GetBits(bitmap.get()),
66 frame.image_data(0).size(),
67 frame.image_data(0).begin());
69 return std::make_pair(
70 core::draw_frame(std::move(frame)),
71 core::constraints(width, height));
74 struct image_producer : public core::frame_producer_base
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_;
80 core::constraints constraints_;
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 , frame_(core::draw_frame::empty())
87 load(load_image(description_));
89 CASPAR_LOG(info) << print() << L" Initialized";
92 image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size)
93 : description_(L"png from memory")
94 , frame_factory_(frame_factory)
95 , frame_(core::draw_frame::empty())
97 load(load_png_from_memory(png_data, size));
99 CASPAR_LOG(info) << print() << L" Initialized";
102 void load(const std::shared_ptr<FIBITMAP>& bitmap)
104 FreeImage_FlipVertical(bitmap.get());
105 core::pixel_format_desc desc;
106 desc.format = core::pixel_format::bgra;
107 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));
108 auto frame = frame_factory_->create_frame(this, desc);
110 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data().size(), frame.image_data().begin());
111 frame_ = core::draw_frame(std::move(frame));
112 constraints_.width.set(FreeImage_GetWidth(bitmap.get()));
113 constraints_.height.set(FreeImage_GetHeight(bitmap.get()));
118 core::draw_frame receive_impl() override
120 monitor_subject_ << core::monitor::message("/file/path") % description_;
125 core::draw_frame create_thumbnail_frame() override
130 core::constraints& pixel_constraints() override
135 std::wstring print() const override
137 return L"image_producer[" + description_ + L"]";
140 std::wstring name() const override
145 boost::property_tree::wptree info() const override
147 boost::property_tree::wptree info;
148 info.add(L"type", L"image");
149 info.add(L"location", description_);
153 core::monitor::subject& monitor_output()
155 return monitor_subject_;
163 ieq(const std::wstring& test)
168 bool operator()(const std::wstring& elem) const
170 return boost::iequals(elem, test_);
174 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 static const auto extensions = {
191 if (boost::iequals(params[0], L"[IMG_SEQUENCE]"))
193 if (params.size() != 2)
194 return core::frame_producer::empty();
196 auto dir = boost::filesystem::path(env::media_folder() + params[1]).parent_path();
197 auto basename = boost::filesystem::basename(params[1]);
198 std::set<std::wstring> files;
199 boost::filesystem::directory_iterator end;
201 for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
203 auto name = it->path().filename().wstring();
205 if (!boost::algorithm::istarts_with(name, basename))
208 auto extension = it->path().extension().wstring();
210 if (std::find_if(extensions.begin(), extensions.end(), ieq(extension)) == extensions.end())
213 files.insert(it->path().wstring());
217 return core::frame_producer::empty();
221 std::vector<core::draw_frame> frames;
222 frames.reserve(files.size());
224 BOOST_FOREACH(auto& file, files)
226 auto frame = load_image(frame_factory, file);
230 width = static_cast<int>(frame.second.width.get());
231 height = static_cast<int>(frame.second.height.get());
234 frames.push_back(std::move(frame.first));
237 return core::create_const_producer(std::move(frames), width, height);
239 else if(boost::iequals(params[0], L"[PNG_BASE64]"))
241 if (params.size() < 2)
242 return core::frame_producer::empty();
244 auto png_data = from_base64(std::string(params[1].begin(), params[1].end()));
246 return spl::make_shared<image_producer>(frame_factory, png_data.data(), png_data.size());
249 std::wstring filename = env::media_folder() + params[0];
251 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
253 return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
256 if(ext == extensions.end())
257 return core::frame_producer::empty();
259 return spl::make_shared<image_producer>(frame_factory, filename + *ext);
263 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)
265 return caspar::image::create_producer(frame_factory, format_desc, params);