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/frame/frame.h>
30 #include <core/frame/draw_frame.h>
31 #include <core/frame/frame_factory.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/monitor/monitor.h>
35 #include <common/env.h>
36 #include <common/log.h>
37 #include <common/array.h>
39 #include <boost/assign.hpp>
40 #include <boost/filesystem.hpp>
41 #include <boost/property_tree/ptree.hpp>
45 using namespace boost::assign;
47 namespace caspar { namespace image {
49 struct image_producer : public core::frame_producer_base
51 monitor::basic_subject event_subject_;
52 const std::wstring filename_;
53 core::draw_frame frame_;
54 core::constraints constraints_;
56 explicit image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename)
58 , frame_(core::draw_frame::empty())
60 auto bitmap = load_image(filename_);
61 FreeImage_FlipVertical(bitmap.get());
63 core::pixel_format_desc desc = core::pixel_format::bgra;
64 auto width = FreeImage_GetWidth(bitmap.get());
65 auto height = FreeImage_GetHeight(bitmap.get());
66 desc.planes.push_back(core::pixel_format_desc::plane(width, height, 4));
67 auto frame = frame_factory->create_frame(this, desc);
69 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data(0).size(), frame.image_data(0).begin());
70 frame_ = core::draw_frame(std::move(frame));
71 constraints_.width.set(width);
72 constraints_.height.set(height);
74 CASPAR_LOG(info) << print() << L" Initialized";
79 core::draw_frame receive_impl() override
81 event_subject_ << monitor::event("file/path") % filename_;
86 core::draw_frame create_thumbnail_frame() override
91 core::constraints& pixel_constraints() override
96 std::wstring print() const override
98 return L"image_producer[" + filename_ + L"]";
101 std::wstring name() const override
106 boost::property_tree::wptree info() const override
108 boost::property_tree::wptree info;
109 info.add(L"type", L"image");
110 info.add(L"filename", filename_);
114 void subscribe(const monitor::observable::observer_ptr& o) override
116 return event_subject_.subscribe(o);
119 void unsubscribe(const monitor::observable::observer_ptr& o) override
121 return event_subject_.unsubscribe(o);
125 spl::shared_ptr<core::frame_producer> create_raw_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
127 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");
128 std::wstring filename = env::media_folder() + L"\\" + params[0];
130 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
132 return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
135 if(ext == extensions.end())
136 return core::frame_producer::empty();
138 return spl::make_shared<image_producer>(frame_factory, filename + *ext);
141 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)
143 return create_raw_producer(frame_factory, format_desc, params);
146 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)
148 return create_raw_producer(frame_factory, format_desc, params);