]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_scroll_producer.cpp
Fixed build errors under windows
[casparcg] / modules / image / producer / image_scroll_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 * Author: Helge Norberg, helge.norberg@svt.se
21 */
22
23 #include "image_scroll_producer.h"
24
25 #include "../util/image_loader.h"
26 #include "../util/image_view.h"
27 #include "../util/image_algorithms.h"
28
29 #include <core/video_format.h>
30
31 #include <core/frame/frame.h>
32 #include <core/frame/draw_frame.h>
33 #include <core/frame/frame_factory.h>
34 #include <core/frame/frame_transform.h>
35 #include <core/frame/pixel_format.h>
36 #include <core/monitor/monitor.h>
37
38 #include <common/env.h>
39 #include <common/log.h>
40 #include <common/except.h>
41 #include <common/array.h>
42 #include <common/tweener.h>
43 #include <common/param.h>
44
45 #include <boost/filesystem.hpp>
46 #include <boost/lexical_cast.hpp>
47 #include <boost/property_tree/ptree.hpp>
48 #include <boost/scoped_array.hpp>
49
50 #include <algorithm>
51 #include <array>
52
53 namespace caspar { namespace image {
54                 
55 struct image_scroll_producer : public core::frame_producer_base
56 {       
57         core::monitor::subject                  monitor_subject_;
58
59         const std::wstring                              filename_;
60         std::vector<core::draw_frame>   frames_;
61         core::video_format_desc                 format_desc_;
62         int                                                             width_;
63         int                                                             height_;
64         core::constraints                               constraints_;
65
66         double                                                  delta_                          = 0.0;
67         double                                                  speed_;
68
69         int                                                             start_offset_x_         = 0;
70         int                                                             start_offset_y_         = 0;
71         bool                                                    progressive_;
72         
73         explicit image_scroll_producer(
74                         const spl::shared_ptr<core::frame_factory>& frame_factory,
75                         const core::video_format_desc& format_desc,
76                         const std::wstring& filename,
77                         double speed,
78                         double duration,
79                         int motion_blur_px = 0,
80                         bool premultiply_with_alpha = false,
81                         bool progressive = false)
82                 : filename_(filename)
83                 , format_desc_(format_desc)
84                 , speed_(speed)
85                 , progressive_(progressive)
86         {
87                 auto bitmap = load_image(filename_);
88                 FreeImage_FlipVertical(bitmap.get());
89
90                 width_  = FreeImage_GetWidth(bitmap.get());
91                 height_ = FreeImage_GetHeight(bitmap.get());
92                 constraints_.width.set(width_);
93                 constraints_.height.set(height_);
94
95                 bool vertical = width_ == format_desc_.width;
96                 bool horizontal = height_ == format_desc_.height;
97
98                 if (!vertical && !horizontal)
99                         BOOST_THROW_EXCEPTION(
100                                 caspar::invalid_argument() << msg_info("Neither width nor height matched the video resolution"));
101
102                 if (vertical)
103                 {
104                         if (duration != 0.0)
105                         {
106                                 double total_num_pixels = format_desc_.height * 2 + height_;
107
108                                 speed_ = total_num_pixels / (duration * format_desc_.fps * static_cast<double>(format_desc_.field_count));
109
110                                 if (std::abs(speed_) > 1.0)
111                                         speed_ = std::ceil(speed_);
112                         }
113
114                         if (speed_ < 0.0)
115                         {
116                                 start_offset_y_ = height_ + format_desc_.height;
117                         }
118                 }
119                 else
120                 {
121                         if (duration != 0.0)
122                         {
123                                 double total_num_pixels = format_desc_.width * 2 + width_;
124
125                                 speed_ = total_num_pixels / (duration * format_desc_.fps * static_cast<double>(format_desc_.field_count));
126
127                                 if (std::abs(speed_) > 1.0)
128                                         speed_ = std::ceil(speed_);
129                         }
130
131                         if (speed_ > 0.0)
132                                 start_offset_x_ = format_desc_.width - (width_ % format_desc_.width);
133                         else
134                                 start_offset_x_ = format_desc_.width - (width_ % format_desc_.width) + width_ + format_desc_.width;
135                 }
136
137                 auto bytes = FreeImage_GetBits(bitmap.get());
138                 auto count = width_*height_*4;
139                 image_view<bgra_pixel> original_view(bytes, width_, height_);
140
141                 if (premultiply_with_alpha)
142                         premultiply(original_view);
143
144                 boost::scoped_array<uint8_t> blurred_copy;
145
146                 if (motion_blur_px > 0)
147                 {
148                         double angle = 3.14159265 / 2; // Up
149
150                         if (horizontal && speed_ < 0)
151                                 angle *= 2; // Left
152                         else if (vertical && speed > 0)
153                                 angle *= 3; // Down
154                         else if (horizontal && speed  > 0)
155                                 angle = 0.0; // Right
156
157                         blurred_copy.reset(new uint8_t[count]);
158                         image_view<bgra_pixel> blurred_view(blurred_copy.get(), width_, height_);
159                         caspar::tweener blur_tweener(L"easeInQuad");
160                         blur(original_view, blurred_view, angle, motion_blur_px, blur_tweener);
161                         bytes = blurred_copy.get();
162                         bitmap.reset();
163                 }
164
165                 if (vertical)
166                 {
167                         int n = 1;
168
169                         while(count > 0)
170                         {
171                                 core::pixel_format_desc desc = core::pixel_format::bgra;
172                                 desc.planes.push_back(core::pixel_format_desc::plane(width_, format_desc_.height, 4));
173                                 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc);
174
175                                 if(count >= frame.image_data(0).size())
176                                 {       
177                                         std::copy_n(bytes + count - frame.image_data(0).size(), frame.image_data(0).size(), frame.image_data(0).begin());
178                                         count -= static_cast<int>(frame.image_data(0).size());
179                                 }
180                                 else
181                                 {
182                                         memset(frame.image_data(0).begin(), 0, frame.image_data(0).size());     
183                                         std::copy_n(bytes, count, frame.image_data(0).begin() + format_desc_.size - count);
184                                         count = 0;
185                                 }
186
187                                 core::draw_frame draw_frame(std::move(frame));
188
189                                 // Set the relative position to the other image fragments
190                                 draw_frame.transform().image_transform.fill_translation[1] = - n++;
191
192                                 frames_.push_back(draw_frame);
193                         }
194                 }
195                 else if (horizontal)
196                 {
197                         int i = 0;
198                         while(count > 0)
199                         {
200                                 core::pixel_format_desc desc = core::pixel_format::bgra;
201                                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width, height_, 4));
202                                 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc);
203                                 if(count >= frame.image_data(0).size())
204                                 {       
205                                         for(int y = 0; y < height_; ++y)
206                                                 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);
207                                         
208                                         ++i;
209                                         count -= static_cast<int>(frame.image_data(0).size());
210                                 }
211                                 else
212                                 {
213                                         memset(frame.image_data(0).begin(), 0, frame.image_data(0).size());     
214                                         auto width2 = width_ % format_desc_.width;
215                                         for(int y = 0; y < height_; ++y)
216                                                 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, width2*4, frame.image_data(0).begin() + y * format_desc_.width*4);
217
218                                         count = 0;
219                                 }
220                         
221                                 frames_.push_back(core::draw_frame(std::move(frame)));
222                         }
223
224                         std::reverse(frames_.begin(), frames_.end());
225
226                         // Set the relative positions of the image fragments.
227                         for (size_t n = 0; n < frames_.size(); ++n)
228                         {
229                                 double translation = - (static_cast<double>(n) + 1.0);
230                                 frames_[n].transform().image_transform.fill_translation[0] = translation;
231                         }
232                 }
233
234                 CASPAR_LOG(info) << print() << L" Initialized";
235         }
236
237         std::vector<core::draw_frame> get_visible()
238         {
239                 std::vector<core::draw_frame> result;
240                 result.reserve(frames_.size());
241
242                 for (auto& frame : frames_)
243                 {
244                         auto& fill_translation = frame.transform().image_transform.fill_translation;
245
246                         if (width_ == format_desc_.width)
247                         {
248                                 auto motion_offset_in_screens = (static_cast<double>(start_offset_y_) + delta_) / static_cast<double>(format_desc_.height);
249                                 auto vertical_offset = fill_translation[1] + motion_offset_in_screens;
250
251                                 if (vertical_offset < -1.0 || vertical_offset > 1.0)
252                                 {
253                                         continue;
254                                 }
255                         }
256                         else
257                         {
258                                 auto motion_offset_in_screens = (static_cast<double>(start_offset_x_) + delta_) / static_cast<double>(format_desc_.width);
259                                 auto horizontal_offset = fill_translation[0] + motion_offset_in_screens;
260
261                                 if (horizontal_offset < -1.0 || horizontal_offset > 1.0)
262                                 {
263                                         continue;
264                                 }
265                         }
266
267                         result.push_back(frame);
268                 }
269
270                 return std::move(result);
271         }
272         
273         // frame_producer
274         core::draw_frame render_frame(bool allow_eof)
275         {
276                 if(frames_.empty())
277                         return core::draw_frame::empty();
278                 
279                 core::draw_frame result(get_visible());
280                 auto& fill_translation = result.transform().image_transform.fill_translation;
281
282                 if (width_ == format_desc_.width)
283                 {
284                         if (static_cast<size_t>(std::abs(delta_)) >= height_ + format_desc_.height && allow_eof)
285                                 return core::draw_frame::empty();
286
287                         fill_translation[1] = 
288                                 static_cast<double>(start_offset_y_) / static_cast<double>(format_desc_.height)
289                                 + delta_ / static_cast<double>(format_desc_.height);
290                 }
291                 else
292                 {
293                         if (static_cast<size_t>(std::abs(delta_)) >= width_ + format_desc_.width && allow_eof)
294                                 return core::draw_frame::empty();
295
296                         fill_translation[0] = 
297                                 static_cast<double>(start_offset_x_) / static_cast<double>(format_desc_.width)
298                                 + (delta_) / static_cast<double>(format_desc_.width);
299                 }
300
301                 return result;
302         }
303
304         core::draw_frame render_frame(bool allow_eof, bool advance_delta)
305         {
306                 auto result = render_frame(allow_eof);
307
308                 if (advance_delta)
309                 {
310                         advance();
311                 }
312
313                 return result;
314         }
315
316         void advance()
317         {
318                 delta_ += speed_;
319         }
320
321         core::draw_frame receive_impl() override
322         {
323                 core::draw_frame result;
324
325                 if (format_desc_.field_mode == core::field_mode::progressive || progressive_)
326                 {
327                         result = render_frame(true, true);
328                 }
329                 else
330                 {
331                         auto field1 = render_frame(true, true);
332                         auto field2 = render_frame(true, false);
333
334                         if (field1 != core::draw_frame::empty() && field2 == core::draw_frame::empty())
335                         {
336                                 field2 = render_frame(false, true);
337                         }
338                         else
339                         {
340                                 advance();
341                         }
342
343                         result = core::draw_frame::interlace(field1, field2, format_desc_.field_mode);
344                 }
345                 
346                 monitor_subject_ << core::monitor::message("/file/path") % filename_
347                                                  << core::monitor::message("/delta") % delta_ 
348                                                  << core::monitor::message("/speed") % speed_;
349
350                 return result;
351         }
352
353         core::constraints& pixel_constraints() override
354         {
355                 return constraints_;
356         }
357                                 
358         std::wstring print() const override
359         {
360                 return L"image_scroll_producer[" + filename_ + L"]";
361         }
362
363         std::wstring name() const override
364         {
365                 return L"image-scroll";
366         }
367
368         boost::property_tree::wptree info() const override
369         {
370                 boost::property_tree::wptree info;
371                 info.add(L"type", L"image-scroll");
372                 info.add(L"filename", filename_);
373                 return info;
374         }
375
376         uint32_t nb_frames() const override
377         {
378                 if(width_ == format_desc_.width)
379                 {
380                         auto length = (height_ + format_desc_.height * 2);
381                         return static_cast<uint32_t>(length / std::abs(speed_));// + length % std::abs(delta_));
382                 }
383                 else
384                 {
385                         auto length = (width_ + format_desc_.width * 2);
386                         return static_cast<uint32_t>(length / std::abs(speed_));// + length % std::abs(delta_));
387                 }
388         }
389
390         core::monitor::subject& monitor_output()
391         {
392                 return monitor_subject_;
393         }
394 };
395
396 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)
397 {
398         static const auto extensions = {
399                 L".png",
400                 L".tga",
401                 L".bmp",
402                 L".jpg",
403                 L".jpeg",
404                 L".gif",
405                 L".tiff",
406                 L".tif",
407                 L".jp2",
408                 L".jpx",
409                 L".j2k",
410                 L".j2c"
411         };
412         std::wstring filename = env::media_folder() + L"\\" + params[0];
413         
414         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
415                 {                                       
416                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
417                 });
418
419         if(ext == extensions.end())
420                 return core::frame_producer::empty();
421         
422         double duration = 0.0;
423         double speed = get_param(L"SPEED", params, 0.0);
424
425         if (speed == 0)
426                 duration = get_param(L"DURATION", params, 0.0);
427
428         if(speed == 0 && duration == 0)
429                 return core::frame_producer::empty();
430
431         int motion_blur_px = get_param(L"BLUR", params, 0);
432
433         bool premultiply_with_alpha = contains_param(L"PREMULTIPLY", params);
434         bool progressive = contains_param(L"PROGRESSIVE", params);
435
436         return core::create_destroy_proxy(spl::make_shared<image_scroll_producer>(
437                 frame_factory, 
438                 format_desc, 
439                 filename + *ext, 
440                 -speed, 
441                 -duration, 
442                 motion_blur_px, 
443                 premultiply_with_alpha,
444                 progressive));
445 }
446
447 }}