2 * Copyright (c) 2011 Sveriges Television AB <info@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_scroll_producer.h"
\r
24 #include "../util/image_loader.h"
\r
26 #include <core/video_format.h>
\r
28 #include <core/frame/frame.h>
\r
29 #include <core/frame/draw_frame.h>
\r
30 #include <core/frame/frame_factory.h>
\r
31 #include <core/frame/frame_transform.h>
\r
32 #include <core/frame/pixel_format.h>
\r
34 #include <common/env.h>
\r
35 #include <common/log.h>
\r
36 #include <common/except.h>
\r
37 #include <common/memory/array.h>
\r
39 #include <boost/assign.hpp>
\r
40 #include <boost/filesystem.hpp>
\r
41 #include <boost/foreach.hpp>
\r
42 #include <boost/lexical_cast.hpp>
\r
43 #include <boost/property_tree/ptree.hpp>
\r
45 #include <algorithm>
\r
48 using namespace boost::assign;
\r
50 namespace caspar { namespace image {
\r
52 struct image_scroll_producer : public core::frame_producer
\r
54 const std::wstring filename_;
\r
55 std::vector<core::draw_frame> frames_;
\r
56 core::video_format_desc format_desc_;
\r
63 std::array<double, 2> start_offset_;
\r
65 core::draw_frame last_frame_;
\r
67 explicit image_scroll_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filename, int speed)
\r
68 : filename_(filename)
\r
70 , format_desc_(format_desc)
\r
72 , last_frame_(core::draw_frame::empty())
\r
74 start_offset_.assign(0.0);
\r
76 auto bitmap = load_image(filename_);
\r
77 FreeImage_FlipVertical(bitmap.get());
\r
79 width_ = FreeImage_GetWidth(bitmap.get());
\r
80 height_ = FreeImage_GetHeight(bitmap.get());
\r
82 auto bytes = FreeImage_GetBits(bitmap.get());
\r
83 auto count = width_*height_*4;
\r
85 if(height_ > format_desc_.height)
\r
89 core::pixel_format_desc desc = core::pixel_format::bgra;
\r
90 desc.planes.push_back(core::pixel_format_desc::plane(width_, format_desc_.height, 4));
\r
91 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc);
\r
93 if(count >= frame.image_data(0).size())
\r
95 std::copy_n(bytes + count - frame.image_data(0).size(), frame.image_data(0).size(), frame.image_data(0).begin());
\r
96 count -= static_cast<int>(frame.image_data(0).size());
\r
100 memset(frame.image_data(0).begin(), 0, frame.image_data(0).size());
\r
101 std::copy_n(bytes, count, frame.image_data(0).begin() + format_desc_.size - count);
\r
105 frames_.push_back(core::draw_frame(std::move(frame)));
\r
110 auto offset = format_desc_.height - (height_ % format_desc_.height);
\r
111 auto offset2 = offset * 0.5/static_cast<double>(format_desc_.height);
\r
112 start_offset_[1] = (std::ceil(static_cast<double>(height_) / static_cast<double>(format_desc_.height)) + 1.0) * 0.5 - offset2;// - 1.5;
\r
120 core::pixel_format_desc desc = core::pixel_format::bgra;
\r
121 desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width, height_, 4));
\r
122 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc);
\r
123 if(count >= frame.image_data(0).size())
\r
125 for(int y = 0; y < height_; ++y)
\r
126 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, format_desc_.width*4, frame.image_data(0).begin() + y * format_desc_.width*4);
\r
129 count -= static_cast<int>(frame.image_data(0).size());
\r
133 memset(frame.image_data(0).begin(), 0, frame.image_data(0).size());
\r
134 auto width2 = width_ % format_desc_.width;
\r
135 for(int y = 0; y < height_; ++y)
\r
136 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, width2*4, frame.image_data(0).begin() + y * format_desc_.width*4);
\r
141 frames_.push_back(core::draw_frame(std::move(frame)));
\r
144 std::reverse(frames_.begin(), frames_.end());
\r
148 auto offset = format_desc_.width - (width_ % format_desc_.width);
\r
149 start_offset_[0] = offset * 0.5/static_cast<double>(format_desc_.width);
\r
153 start_offset_[0] = (std::ceil(static_cast<double>(width_) / static_cast<double>(format_desc_.width)) + 1.0) * 0.5;// - 1.5;
\r
157 CASPAR_LOG(info) << print() << L" Initialized";
\r
162 virtual core::draw_frame receive(int) override
\r
166 if(frames_.empty())
\r
167 return core::draw_frame::eof();
\r
169 if(height_ > format_desc_.height)
\r
171 if(static_cast<int>(std::abs(delta_)) >= height_ - format_desc_.height)
\r
172 return core::draw_frame::eof();
\r
174 for(int n = 0; n < frames_.size(); ++n)
\r
176 frames_[n].transform().image_transform.fill_translation[0] = start_offset_[0];
\r
177 frames_[n].transform().image_transform.fill_translation[1] = start_offset_[1] - (n+1) + delta_ * 0.5/static_cast<double>(format_desc_.height);
\r
182 if(static_cast<int>(std::abs(delta_)) >= width_ - format_desc_.width)
\r
183 return core::draw_frame::eof();
\r
185 for(int n = 0; n < frames_.size(); ++n)
\r
187 frames_[n].transform().image_transform.fill_translation[0] = start_offset_[0] - (n+1) + delta_ * 0.5/static_cast<double>(format_desc_.width);
\r
188 frames_[n].transform().image_transform.fill_translation[1] = start_offset_[1];
\r
192 return last_frame_ = core::draw_frame(frames_);
\r
195 virtual core::draw_frame last_frame() const override
\r
197 return core::draw_frame::still(last_frame_);
\r
200 virtual std::wstring print() const override
\r
202 return L"image_scroll_producer[" + filename_ + L"]";
\r
205 virtual std::wstring name() const override
\r
207 return L"image-scroll";
\r
210 virtual boost::property_tree::wptree info() const override
\r
212 boost::property_tree::wptree info;
\r
213 info.add(L"type", L"image-scroll");
\r
214 info.add(L"filename", filename_);
\r
218 virtual uint32_t nb_frames() const override
\r
220 if(height_ > format_desc_.height)
\r
222 auto length = (height_ - format_desc_.height);
\r
223 return length/std::abs(speed_);// + length % std::abs(delta_));
\r
227 auto length = (width_ - format_desc_.width);
\r
228 auto result = length/std::abs(speed_);// + length % std::abs(delta_));
\r
234 spl::shared_ptr<core::frame_producer> create_scroll_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
\r
236 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
237 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
239 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
241 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
244 if(ext == extensions.end())
\r
245 return core::frame_producer::empty();
\r
248 auto speed_it = std::find(params.begin(), params.end(), L"SPEED");
\r
249 if(speed_it != params.end())
\r
251 if(++speed_it != params.end())
\r
252 speed = boost::lexical_cast<int>(*speed_it);
\r
256 return core::frame_producer::empty();
\r
258 return spl::make_shared<image_scroll_producer>(frame_factory, format_desc, filename + L"." + *ext, speed);
\r