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
20 * Author: Helge Norberg, helge.norberg@svt.se
\r
23 #include "image_scroll_producer.h"
\r
25 #include "../util/image_loader.h"
\r
26 #include "../util/image_view.h"
\r
27 #include "../util/image_algorithms.h"
\r
29 #include <core/video_format.h>
\r
31 #include <core/frame/frame.h>
\r
32 #include <core/frame/draw_frame.h>
\r
33 #include <core/frame/frame_factory.h>
\r
34 #include <core/frame/frame_transform.h>
\r
35 #include <core/frame/pixel_format.h>
\r
36 #include <core/monitor/monitor.h>
\r
38 #include <common/env.h>
\r
39 #include <common/log.h>
\r
40 #include <common/except.h>
\r
41 #include <common/array.h>
\r
42 #include <common/tweener.h>
\r
44 #include <boost/assign.hpp>
\r
45 #include <boost/filesystem.hpp>
\r
46 #include <boost/foreach.hpp>
\r
47 #include <boost/lexical_cast.hpp>
\r
48 #include <boost/property_tree/ptree.hpp>
\r
49 #include <boost/scoped_array.hpp>
\r
51 #include <algorithm>
\r
54 using namespace boost::assign;
\r
56 namespace caspar { namespace image {
\r
58 struct image_scroll_producer : public core::frame_producer_base
\r
60 monitor::basic_subject event_subject_;
\r
62 const std::wstring filename_;
\r
63 std::vector<core::draw_frame> frames_;
\r
64 core::video_format_desc format_desc_;
\r
71 int start_offset_x_;
\r
72 int start_offset_y_;
\r
75 explicit image_scroll_producer(
\r
76 const spl::shared_ptr<core::frame_factory>& frame_factory,
\r
77 const core::video_format_desc& format_desc,
\r
78 const std::wstring& filename,
\r
81 int motion_blur_px = 0,
\r
82 bool premultiply_with_alpha = false,
\r
83 bool progressive = false)
\r
84 : filename_(filename)
\r
86 , format_desc_(format_desc)
\r
88 , start_offset_x_(0)
\r
89 , start_offset_y_(0)
\r
90 , progressive_(progressive)
\r
92 auto bitmap = load_image(filename_);
\r
93 FreeImage_FlipVertical(bitmap.get());
\r
95 width_ = FreeImage_GetWidth(bitmap.get());
\r
96 height_ = FreeImage_GetHeight(bitmap.get());
\r
98 bool vertical = width_ == format_desc_.width;
\r
99 bool horizontal = height_ == format_desc_.height;
\r
101 if (!vertical && !horizontal)
\r
102 BOOST_THROW_EXCEPTION(
\r
103 caspar::invalid_argument() << msg_info("Neither width nor height matched the video resolution"));
\r
107 if (duration != 0.0)
\r
109 double total_num_pixels = format_desc_.height * 2 + height_;
\r
111 speed_ = total_num_pixels / (duration * format_desc_.fps * static_cast<double>(format_desc_.field_count));
\r
113 if (std::abs(speed_) > 1.0)
\r
114 speed_ = std::ceil(speed_);
\r
119 start_offset_y_ = height_ + format_desc_.height;
\r
124 if (duration != 0.0)
\r
126 double total_num_pixels = format_desc_.width * 2 + width_;
\r
128 speed_ = total_num_pixels / (duration * format_desc_.fps * static_cast<double>(format_desc_.field_count));
\r
130 if (std::abs(speed_) > 1.0)
\r
131 speed_ = std::ceil(speed_);
\r
135 start_offset_x_ = format_desc_.width - (width_ % format_desc_.width);
\r
137 start_offset_x_ = format_desc_.width - (width_ % format_desc_.width) + width_ + format_desc_.width;
\r
140 auto bytes = FreeImage_GetBits(bitmap.get());
\r
141 auto count = width_*height_*4;
\r
142 image_view<bgra_pixel> original_view(bytes, width_, height_);
\r
144 if (premultiply_with_alpha)
\r
145 premultiply(original_view);
\r
147 boost::scoped_array<uint8_t> blurred_copy;
\r
149 if (motion_blur_px > 0)
\r
151 double angle = 3.14159265 / 2; // Up
\r
153 if (horizontal && speed_ < 0)
\r
154 angle *= 2; // Left
\r
155 else if (vertical && speed > 0)
\r
156 angle *= 3; // Down
\r
157 else if (horizontal && speed > 0)
\r
158 angle = 0.0; // Right
\r
160 blurred_copy.reset(new uint8_t[count]);
\r
161 image_view<bgra_pixel> blurred_view(blurred_copy.get(), width_, height_);
\r
162 core::tweener blur_tweener(L"easeInQuad");
\r
163 blur(original_view, blurred_view, angle, motion_blur_px, blur_tweener);
\r
164 bytes = blurred_copy.get();
\r
174 core::pixel_format_desc desc = core::pixel_format::bgra;
\r
175 desc.planes.push_back(core::pixel_format_desc::plane(width_, format_desc_.height, 4));
\r
176 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc);
\r
178 if(count >= frame.image_data(0).size())
\r
180 std::copy_n(bytes + count - frame.image_data(0).size(), frame.image_data(0).size(), frame.image_data(0).begin());
\r
181 count -= static_cast<int>(frame.image_data(0).size());
\r
185 memset(frame.image_data(0).begin(), 0, frame.image_data(0).size());
\r
186 std::copy_n(bytes, count, frame.image_data(0).begin() + format_desc_.size - count);
\r
190 core::draw_frame draw_frame(std::move(frame));
\r
192 // Set the relative position to the other image fragments
\r
193 draw_frame.transform().image_transform.fill_translation[1] = - n++;
\r
195 frames_.push_back(draw_frame);
\r
198 else if (horizontal)
\r
203 core::pixel_format_desc desc = core::pixel_format::bgra;
\r
204 desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width, height_, 4));
\r
205 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc);
\r
206 if(count >= frame.image_data(0).size())
\r
208 for(int y = 0; y < height_; ++y)
\r
209 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
212 count -= static_cast<int>(frame.image_data(0).size());
\r
216 memset(frame.image_data(0).begin(), 0, frame.image_data(0).size());
\r
217 auto width2 = width_ % format_desc_.width;
\r
218 for(int y = 0; y < height_; ++y)
\r
219 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
224 frames_.push_back(core::draw_frame(std::move(frame)));
\r
227 std::reverse(frames_.begin(), frames_.end());
\r
229 // Set the relative positions of the image fragments.
\r
230 for (size_t n = 0; n < frames_.size(); ++n)
\r
232 double translation = - (static_cast<double>(n) + 1.0);
\r
233 frames_[n].transform().image_transform.fill_translation[0] = translation;
\r
237 CASPAR_LOG(info) << print() << L" Initialized";
\r
240 std::vector<core::draw_frame> get_visible()
\r
242 std::vector<core::draw_frame> result;
\r
243 result.reserve(frames_.size());
\r
245 BOOST_FOREACH(auto& frame, frames_)
\r
247 auto& fill_translation = frame.transform().image_transform.fill_translation;
\r
249 if (width_ == format_desc_.width)
\r
251 auto motion_offset_in_screens = (static_cast<double>(start_offset_y_) + delta_) / static_cast<double>(format_desc_.height);
\r
252 auto vertical_offset = fill_translation[1] + motion_offset_in_screens;
\r
254 if (vertical_offset < -1.0 || vertical_offset > 1.0)
\r
261 auto motion_offset_in_screens = (static_cast<double>(start_offset_x_) + delta_) / static_cast<double>(format_desc_.width);
\r
262 auto horizontal_offset = fill_translation[0] + motion_offset_in_screens;
\r
264 if (horizontal_offset < -1.0 || horizontal_offset > 1.0)
\r
270 result.push_back(frame);
\r
273 return std::move(result);
\r
277 core::draw_frame render_frame(bool allow_eof)
\r
279 if(frames_.empty())
\r
280 return core::draw_frame::empty();
\r
282 core::draw_frame result(get_visible());
\r
283 auto& fill_translation = result.transform().image_transform.fill_translation;
\r
285 if (width_ == format_desc_.width)
\r
287 if (static_cast<size_t>(std::abs(delta_)) >= height_ + format_desc_.height && allow_eof)
\r
288 return core::draw_frame::empty();
\r
290 fill_translation[1] =
\r
291 static_cast<double>(start_offset_y_) / static_cast<double>(format_desc_.height)
\r
292 + delta_ / static_cast<double>(format_desc_.height);
\r
296 if (static_cast<size_t>(std::abs(delta_)) >= width_ + format_desc_.width && allow_eof)
\r
297 return core::draw_frame::empty();
\r
299 fill_translation[0] =
\r
300 static_cast<double>(start_offset_x_) / static_cast<double>(format_desc_.width)
\r
301 + (delta_) / static_cast<double>(format_desc_.width);
\r
307 core::draw_frame render_frame(bool allow_eof, bool advance_delta)
\r
309 auto result = render_frame(allow_eof);
\r
324 core::draw_frame receive_impl() override
\r
326 core::draw_frame result;
\r
328 if (format_desc_.field_mode == core::field_mode::progressive || progressive_)
\r
330 result = render_frame(true, true);
\r
334 auto field1 = render_frame(true, true);
\r
335 auto field2 = render_frame(true, false);
\r
337 if (field1 != core::draw_frame::empty() && field2 == core::draw_frame::empty())
\r
339 field2 = render_frame(false, true);
\r
346 result = core::draw_frame::interlace(field1, field2, format_desc_.field_mode);
\r
349 event_subject_ << monitor::event("file/path") % filename_
\r
350 << monitor::event("delta") % delta_
\r
351 << monitor::event("speed") % speed_;
\r
356 std::wstring print() const override
\r
358 return L"image_scroll_producer[" + filename_ + L"]";
\r
361 std::wstring name() const override
\r
363 return L"image-scroll";
\r
366 boost::property_tree::wptree info() const override
\r
368 boost::property_tree::wptree info;
\r
369 info.add(L"type", L"image-scroll");
\r
370 info.add(L"filename", filename_);
\r
374 uint32_t nb_frames() const override
\r
376 if(width_ == format_desc_.width)
\r
378 auto length = (height_ + format_desc_.height * 2);
\r
379 return static_cast<uint32_t>(length / std::abs(speed_));// + length % std::abs(delta_));
\r
383 auto length = (width_ + format_desc_.width * 2);
\r
384 return static_cast<uint32_t>(length / std::abs(speed_));// + length % std::abs(delta_));
\r
388 void subscribe(const monitor::observable::observer_ptr& o) override
\r
390 return event_subject_.subscribe(o);
\r
393 void unsubscribe(const monitor::observable::observer_ptr& o) override
\r
395 return event_subject_.unsubscribe(o);
\r
399 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
401 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
402 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
404 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
406 return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
\r
409 if(ext == extensions.end())
\r
410 return core::frame_producer::empty();
\r
412 double speed = 0.0;
\r
413 double duration = 0.0;
\r
414 auto speed_it = std::find(params.begin(), params.end(), L"SPEED");
\r
415 if(speed_it != params.end())
\r
417 if(++speed_it != params.end())
\r
418 speed = boost::lexical_cast<double>(*speed_it);
\r
423 auto duration_it = std::find(params.begin(), params.end(), L"DURATION");
\r
425 if (duration_it != params.end() && ++duration_it != params.end())
\r
427 duration = boost::lexical_cast<double>(*duration_it);
\r
431 if(speed == 0 && duration == 0)
\r
432 return core::frame_producer::empty();
\r
434 int motion_blur_px = 0;
\r
435 auto blur_it = std::find(params.begin(), params.end(), L"BLUR");
\r
436 if (blur_it != params.end() && ++blur_it != params.end())
\r
438 motion_blur_px = boost::lexical_cast<int>(*blur_it);
\r
441 bool premultiply_with_alpha = std::find(params.begin(), params.end(), L"PREMULTIPLY") != params.end();
\r
442 bool progressive = std::find(params.begin(), params.end(), L"PROGRESSIVE") != params.end();
\r
444 return core::create_destroy_proxy(spl::make_shared<image_scroll_producer>(
\r
451 premultiply_with_alpha,
\r