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
23 #include "image_scroll_producer.h"
\r
25 #include "../util/image_loader.h"
\r
27 #include <core/video_format.h>
\r
29 #include <core/producer/frame/basic_frame.h>
\r
30 #include <core/producer/frame/frame_factory.h>
\r
31 #include <core/producer/frame/image_transform.h>
\r
32 #include <core/mixer/write_frame.h>
\r
34 #include <common/env.h>
\r
35 #include <common/memory/memclr.h>
\r
36 #include <common/exception/exceptions.h>
\r
38 #include <boost/assign.hpp>
\r
39 #include <boost/filesystem.hpp>
\r
40 #include <boost/foreach.hpp>
\r
41 #include <boost/lexical_cast.hpp>
\r
43 #include <algorithm>
\r
45 using namespace boost::assign;
\r
49 struct image_scroll_producer : public core::frame_producer
\r
51 const std::wstring filename_;
\r
52 std::vector<safe_ptr<core::basic_frame>> frames_;
\r
53 core::video_format_desc format_desc_;
\r
60 std::array<double, 2> start_offset_;
\r
62 safe_ptr<core::basic_frame> last_frame_;
\r
64 explicit image_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int speed)
\r
65 : filename_(filename)
\r
67 , format_desc_(frame_factory->get_video_format_desc())
\r
69 , last_frame_(core::basic_frame::empty())
\r
71 start_offset_.assign(0.0);
\r
73 auto bitmap = load_image(filename_);
\r
74 FreeImage_FlipVertical(bitmap.get());
\r
76 width_ = FreeImage_GetWidth(bitmap.get());
\r
77 height_ = FreeImage_GetHeight(bitmap.get());
\r
79 auto bytes = FreeImage_GetBits(bitmap.get());
\r
80 int count = width_*height_*4;
\r
82 if(height_ > format_desc_.height)
\r
86 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), width_, format_desc_.height);
\r
87 if(count >= frame->image_data().size())
\r
89 std::copy_n(bytes + count - frame->image_data().size(), frame->image_data().size(), frame->image_data().begin());
\r
90 count -= frame->image_data().size();
\r
94 fast_memclr(frame->image_data().begin(), frame->image_data().size());
\r
95 std::copy_n(bytes, count, frame->image_data().begin() + format_desc_.size - count);
\r
100 frames_.push_back(frame);
\r
105 auto offset = format_desc_.height - (height_ % format_desc_.height);
\r
106 auto offset2 = offset * 0.5/static_cast<double>(format_desc_.height);
\r
107 start_offset_[1] = (std::ceil(static_cast<double>(height_) / static_cast<double>(format_desc_.height)) + 1.0) * 0.5 - offset2;// - 1.5;
\r
115 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), format_desc_.width, height_);
\r
116 if(count >= frame->image_data().size())
\r
118 for(size_t y = 0; y < height_; ++y)
\r
119 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
122 count -= frame->image_data().size();
\r
126 fast_memclr(frame->image_data().begin(), frame->image_data().size());
\r
127 int width2 = width_ % format_desc_.width;
\r
128 for(size_t y = 0; y < height_; ++y)
\r
129 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, width2*4, frame->image_data().begin() + y * format_desc_.width*4);
\r
135 frames_.push_back(frame);
\r
138 std::reverse(frames_.begin(), frames_.end());
\r
142 auto offset = format_desc_.width - (width_ % format_desc_.width);
\r
143 start_offset_[0] = offset * 0.5/static_cast<double>(format_desc_.width);
\r
147 start_offset_[0] = (std::ceil(static_cast<double>(width_) / static_cast<double>(format_desc_.width)) + 1.0) * 0.5;// - 1.5;
\r
154 virtual safe_ptr<core::basic_frame> receive(int)
\r
158 if(frames_.empty())
\r
159 return core::basic_frame::eof();
\r
161 if(height_ > format_desc_.height)
\r
163 if(static_cast<size_t>(std::abs(delta_)) >= height_ - format_desc_.height)
\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], start_offset_[1] -0.5*(n+1) + delta_ * 0.5/static_cast<double>(format_desc_.height));
\r
171 if(static_cast<size_t>(std::abs(delta_)) >= width_ - format_desc_.width)
\r
172 return core::basic_frame::eof();
\r
174 for(size_t n = 0; n < frames_.size(); ++n)
\r
175 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
178 return last_frame_ = core::basic_frame(frames_);
\r
181 virtual safe_ptr<core::basic_frame> last_frame() const
\r
183 return last_frame_;
\r
186 virtual std::wstring print() const
\r
188 return L"image_scroll_producer[" + filename_ + L"]";
\r
191 virtual int64_t nb_frames() const
\r
193 if(height_ > format_desc_.height)
\r
195 auto length = (height_ - format_desc_.height);
\r
196 return (length/std::abs(speed_) + length % std::abs(delta_));
\r
200 auto length = (width_ - format_desc_.width);
\r
201 auto result = (length/std::abs(speed_) + length % std::abs(delta_));
\r
207 safe_ptr<core::frame_producer> create_image_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
209 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
210 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
212 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
214 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
217 if(ext == extensions.end())
\r
218 return core::frame_producer::empty();
\r
221 auto speed_it = std::find(params.begin(), params.end(), L"SPEED");
\r
222 if(speed_it != params.end())
\r
224 if(++speed_it != params.end())
\r
225 speed = boost::lexical_cast<int>(*speed_it);
\r
229 return core::frame_producer::empty();
\r
231 return make_safe<image_scroll_producer>(frame_factory, filename + L"." + *ext, speed);
\r