2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
20 #include "image_scroll_producer.h"
\r
22 #include "../util/image_loader.h"
\r
24 #include <core/video_format.h>
\r
26 #include <core/producer/frame/basic_frame.h>
\r
27 #include <core/producer/frame/image_transform.h>
\r
28 #include <core/mixer/write_frame.h>
\r
30 #include <common/env.h>
\r
31 #include <common/memory/memclr.h>
\r
32 #include <common/exception/exceptions.h>
\r
34 #include <boost/assign.hpp>
\r
35 #include <boost/filesystem.hpp>
\r
36 #include <boost/foreach.hpp>
\r
38 #include <algorithm>
\r
40 using namespace boost::assign;
\r
44 struct image_scroll_producer : public core::frame_producer
\r
46 const std::wstring filename_;
\r
47 std::vector<safe_ptr<core::basic_frame>> frames_;
\r
48 core::video_format_desc format_desc_;
\r
55 std::array<double, 2> start_offset_;
\r
57 explicit image_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int speed)
\r
58 : filename_(filename)
\r
60 , format_desc_(frame_factory->get_video_format_desc())
\r
63 start_offset_.assign(0.0);
\r
65 auto bitmap = load_image(filename_);
\r
66 FreeImage_FlipVertical(bitmap.get());
\r
68 width_ = FreeImage_GetWidth(bitmap.get());
\r
69 height_ = FreeImage_GetHeight(bitmap.get());
\r
71 auto bytes = FreeImage_GetBits(bitmap.get());
\r
72 int count = width_*height_*4;
\r
74 if(height_ > format_desc_.height)
\r
78 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), width_, format_desc_.height);
\r
79 if(count >= frame->image_data().size())
\r
81 std::copy_n(bytes + count - frame->image_data().size(), frame->image_data().size(), frame->image_data().begin());
\r
82 count -= frame->image_data().size();
\r
86 fast_memclr(frame->image_data().begin(), frame->image_data().size());
\r
87 std::copy_n(bytes, count, frame->image_data().begin() + format_desc_.size - count);
\r
92 frames_.push_back(frame);
\r
97 auto offset = format_desc_.height - (height_ % format_desc_.height);
\r
98 auto offset2 = offset * 0.5/static_cast<double>(format_desc_.height);
\r
99 start_offset_[1] = (std::ceil(static_cast<double>(height_) / static_cast<double>(format_desc_.height)) + 1.0) * 0.5 - offset2;// - 1.5;
\r
107 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), format_desc_.width, height_);
\r
108 if(count >= frame->image_data().size())
\r
110 for(size_t y = 0; y < height_; ++y)
\r
111 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, format_desc_.width*4, frame->image_data().begin() + y * format_desc_.width*4);
\r
114 count -= frame->image_data().size();
\r
118 fast_memclr(frame->image_data().begin(), frame->image_data().size());
\r
119 int width2 = width_ % format_desc_.width;
\r
120 for(size_t y = 0; y < height_; ++y)
\r
121 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, width2*4, frame->image_data().begin() + y * format_desc_.width*4);
\r
127 frames_.push_back(frame);
\r
130 std::reverse(frames_.begin(), frames_.end());
\r
134 auto offset = format_desc_.width - (width_ % format_desc_.width);
\r
135 start_offset_[0] = offset * 0.5/static_cast<double>(format_desc_.width);
\r
139 start_offset_[0] = (std::ceil(static_cast<double>(width_) / static_cast<double>(format_desc_.width)) + 1.0) * 0.5;// - 1.5;
\r
146 virtual safe_ptr<core::basic_frame> receive()
\r
148 delta_ = 1;//+= speed_;
\r
150 if(frames_.empty())
\r
151 return core::basic_frame::eof();
\r
153 if(height_ > format_desc_.height)
\r
155 if(static_cast<size_t>(std::abs(delta_)) >= height_ - format_desc_.height)
\r
156 return core::basic_frame::eof();
\r
158 for(size_t n = 0; n < frames_.size(); ++n)
\r
159 frames_[n]->get_image_transform().set_fill_translation(start_offset_[0], start_offset_[1] -0.5*(n+1) + delta_ * 0.5/static_cast<double>(format_desc_.height));
\r
163 if(static_cast<size_t>(std::abs(delta_)) >= width_ - format_desc_.width)
\r
164 return core::basic_frame::eof();
\r
166 for(size_t n = 0; n < frames_.size(); ++n)
\r
167 frames_[n]->get_image_transform().set_fill_translation(start_offset_[0] -0.5*(n+1) + delta_ * 0.5/static_cast<double>(format_desc_.height), start_offset_[1]);
\r
170 return core::basic_frame(frames_);
\r
173 virtual std::wstring print() const
\r
175 return L"image_scroll_producer[" + filename_ + L"]";
\r
178 virtual int64_t nb_frames() const
\r
180 if(height_ > format_desc_.height)
\r
182 auto length = (height_ - format_desc_.height);
\r
183 return (length/std::abs(speed_) + length % std::abs(delta_));
\r
187 auto length = (width_ - format_desc_.width);
\r
188 auto result = (length/std::abs(speed_) + length % std::abs(delta_));
\r
194 safe_ptr<core::frame_producer> create_image_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
196 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
197 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
199 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
201 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
204 if(ext == extensions.end())
\r
205 return core::frame_producer::empty();
\r
208 auto speed_it = std::find(params.begin(), params.end(), L"SPEED");
\r
209 if(speed_it != params.end())
\r
211 if(++speed_it != params.end())
\r
212 speed = boost::lexical_cast<int>(*speed_it);
\r
216 return core::frame_producer::empty();
\r
218 return make_safe<image_scroll_producer>(frame_factory, filename + L"." + *ext, speed);
\r