2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "image_producer.h"
\r
24 #include "../util/image_loader.h"
\r
26 #include <core/video_format.h>
\r
28 #include <core/parameters/parameters.h>
\r
29 #include <core/monitor/monitor.h>
\r
30 #include <core/producer/frame/basic_frame.h>
\r
31 #include <core/producer/frame/frame_factory.h>
\r
32 #include <core/mixer/write_frame.h>
\r
34 #include <common/env.h>
\r
35 #include <common/log/log.h>
\r
36 #include <common/utility/base64.h>
\r
37 #include <common/utility/string.h>
\r
39 #include <boost/assign.hpp>
\r
40 #include <boost/filesystem.hpp>
\r
41 #include <boost/property_tree/ptree.hpp>
\r
43 #include <algorithm>
\r
45 using namespace boost::assign;
\r
47 namespace caspar { namespace image {
\r
49 struct image_producer : public core::frame_producer
\r
51 const std::wstring description_;
\r
52 core::monitor::subject monitor_subject_;
\r
53 const safe_ptr<core::frame_factory> frame_factory_; safe_ptr<core::basic_frame> frame_;
\r
55 explicit image_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename)
\r
56 : description_(filename)
\r
57 , frame_factory_(frame_factory)
\r
58 , frame_(core::basic_frame::empty())
\r
60 load(load_image(filename));
\r
63 explicit image_producer(const safe_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size)
\r
64 : description_(L"png from memory")
\r
65 , frame_factory_(frame_factory)
\r
66 , frame_(core::basic_frame::empty())
\r
68 load(load_png_from_memory(png_data, size));
\r
71 void load(const std::shared_ptr<FIBITMAP>& bitmap)
\r
73 FreeImage_FlipVertical(bitmap.get());
\r
75 core::pixel_format_desc desc;
\r
76 desc.pix_fmt = core::pixel_format::bgra;
\r
77 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));
\r
78 auto frame = frame_factory_->create_frame(this, desc);
\r
80 std::copy_n(FreeImage_GetBits(bitmap.get()), frame->image_data().size(), frame->image_data().begin());
\r
82 frame_ = std::move(frame);
\r
87 virtual safe_ptr<core::basic_frame> receive(int) override
\r
89 monitor_subject_ << core::monitor::message("/file/path") % description_;
\r
94 virtual safe_ptr<core::basic_frame> last_frame() const override
\r
99 virtual safe_ptr<core::basic_frame> create_thumbnail_frame() override
\r
104 virtual std::wstring print() const override
\r
106 return L"image_producer[" + description_ + L"]";
\r
109 virtual boost::property_tree::wptree info() const override
\r
111 boost::property_tree::wptree info;
\r
112 info.add(L"type", L"image-producer");
\r
113 info.add(L"location", description_);
\r
117 core::monitor::subject& monitor_output()
\r
119 return monitor_subject_;
\r
123 safe_ptr<core::frame_producer> create_raw_producer(
\r
124 const safe_ptr<core::frame_factory>& frame_factory,
\r
125 const core::parameters& params)
\r
127 if (params[0] == L"[PNG_BASE64]")
\r
129 if (params.size() < 2)
\r
130 return core::frame_producer::empty();
\r
132 auto png_data = from_base64(narrow(params.at_original(1)));
\r
134 return make_safe<image_producer>(frame_factory, png_data.data(), png_data.size());
\r
137 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");
\r
138 std::wstring filename = env::media_folder() + params.at_original(0);
\r
140 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
142 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
145 if(ext == extensions.end())
\r
146 return core::frame_producer::empty();
\r
148 return make_safe<image_producer>(frame_factory, filename + L"." + *ext);
\r
151 safe_ptr<core::frame_producer> create_producer(
\r
152 const safe_ptr<core::frame_factory>& frame_factory,
\r
153 const core::parameters& params)
\r
155 auto raw_producer = create_raw_producer(frame_factory, params);
\r
157 if (raw_producer == core::frame_producer::empty())
\r
158 return raw_producer;
\r
160 return create_producer_print_proxy(raw_producer);
\r
163 safe_ptr<core::frame_producer> create_thumbnail_producer(
\r
164 const safe_ptr<core::frame_factory>& frame_factory,
\r
165 const core::parameters& params)
\r
167 return create_raw_producer(frame_factory, params);
\r