]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_scroll_producer.cpp
2.1.0: Put "array" into its own file, common/memory/array.
[casparcg] / modules / image / producer / image_scroll_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "image_scroll_producer.h"\r
23 \r
24 #include "../util/image_loader.h"\r
25 \r
26 #include <core/video_format.h>\r
27 \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
33 \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
38 \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
44 \r
45 #include <algorithm>\r
46 #include <array>\r
47 \r
48 using namespace boost::assign;\r
49 \r
50 namespace caspar { namespace image {\r
51                 \r
52 struct image_scroll_producer : public core::frame_producer\r
53 {       \r
54         const std::wstring                              filename_;\r
55         std::vector<core::draw_frame>   frames_;\r
56         core::video_format_desc                 format_desc_;\r
57         int                                                             width_;\r
58         int                                                             height_;\r
59 \r
60         int                                                             delta_;\r
61         int                                                             speed_;\r
62 \r
63         std::array<double, 2>                   start_offset_;\r
64 \r
65         core::draw_frame                                last_frame_;\r
66 \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
69                 , delta_(0)\r
70                 , format_desc_(format_desc)\r
71                 , speed_(speed)\r
72                 , last_frame_(core::draw_frame::empty())\r
73         {\r
74                 start_offset_.assign(0.0);\r
75 \r
76                 auto bitmap = load_image(filename_);\r
77                 FreeImage_FlipVertical(bitmap.get());\r
78 \r
79                 width_  = FreeImage_GetWidth(bitmap.get());\r
80                 height_ = FreeImage_GetHeight(bitmap.get());\r
81 \r
82                 auto bytes = FreeImage_GetBits(bitmap.get());\r
83                 auto count = width_*height_*4;\r
84 \r
85                 if(height_ > format_desc_.height)\r
86                 {\r
87                         while(count > 0)\r
88                         {\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
92 \r
93                                 if(count >= frame.image_data(0).size())\r
94                                 {       \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
97                                 }\r
98                                 else\r
99                                 {\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
102                                         count = 0;\r
103                                 }\r
104                         \r
105                                 frames_.push_back(core::draw_frame(std::move(frame)));\r
106                         }\r
107                         \r
108                         if(speed_ < 0.0)\r
109                         {\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
113                         }\r
114                 }\r
115                 else\r
116                 {\r
117                         int i = 0;\r
118                         while(count > 0)\r
119                         {\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
124                                 {       \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
127                                         \r
128                                         ++i;\r
129                                         count -= static_cast<int>(frame.image_data(0).size());\r
130                                 }\r
131                                 else\r
132                                 {\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
137 \r
138                                         count = 0;\r
139                                 }\r
140                         \r
141                                 frames_.push_back(core::draw_frame(std::move(frame)));\r
142                         }\r
143 \r
144                         std::reverse(frames_.begin(), frames_.end());\r
145 \r
146                         if(speed_ > 0.0)\r
147                         {\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
150                         }\r
151                         else\r
152                         {\r
153                                 start_offset_[0] = (std::ceil(static_cast<double>(width_) / static_cast<double>(format_desc_.width)) + 1.0) * 0.5;// - 1.5;\r
154                         }\r
155                 }\r
156 \r
157                 CASPAR_LOG(info) << print() << L" Initialized";\r
158         }\r
159         \r
160         // frame_producer\r
161 \r
162         virtual core::draw_frame receive(int) override\r
163         {               \r
164                 delta_ += speed_;\r
165 \r
166                 if(frames_.empty())\r
167                         return core::draw_frame::eof();\r
168                 \r
169                 if(height_ > format_desc_.height)\r
170                 {\r
171                         if(static_cast<int>(std::abs(delta_)) >= height_ - format_desc_.height)\r
172                                 return core::draw_frame::eof();\r
173 \r
174                         for(int n = 0; n < frames_.size(); ++n)\r
175                         {\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
178                         }\r
179                 }\r
180                 else\r
181                 {\r
182                         if(static_cast<int>(std::abs(delta_)) >= width_ - format_desc_.width)\r
183                                 return core::draw_frame::eof();\r
184 \r
185                         for(int n = 0; n < frames_.size(); ++n)\r
186                         {\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
189                         }\r
190                 }\r
191 \r
192                 return last_frame_ = core::draw_frame(frames_);\r
193         }\r
194 \r
195         virtual core::draw_frame last_frame() const override\r
196         {\r
197                 return core::draw_frame::still(last_frame_);\r
198         }\r
199                         \r
200         virtual std::wstring print() const override\r
201         {\r
202                 return L"image_scroll_producer[" + filename_ + L"]";\r
203         }\r
204 \r
205         virtual std::wstring name() const override\r
206         {\r
207                 return L"image-scroll";\r
208         }\r
209 \r
210         virtual boost::property_tree::wptree info() const override\r
211         {\r
212                 boost::property_tree::wptree info;\r
213                 info.add(L"type", L"image-scroll");\r
214                 info.add(L"filename", filename_);\r
215                 return info;\r
216         }\r
217 \r
218         virtual uint32_t nb_frames() const override\r
219         {\r
220                 if(height_ > format_desc_.height)\r
221                 {\r
222                         auto length = (height_ - format_desc_.height);\r
223                         return length/std::abs(speed_);// + length % std::abs(delta_));\r
224                 }\r
225                 else\r
226                 {\r
227                         auto length = (width_ - format_desc_.width);\r
228                         auto result = length/std::abs(speed_);// + length % std::abs(delta_));\r
229                         return result;\r
230                 }\r
231         }\r
232 };\r
233 \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
235 {\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
238         \r
239         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
240                 {                                       \r
241                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
242                 });\r
243 \r
244         if(ext == extensions.end())\r
245                 return core::frame_producer::empty();\r
246         \r
247         int speed = 0;\r
248         auto speed_it = std::find(params.begin(), params.end(), L"SPEED");\r
249         if(speed_it != params.end())\r
250         {\r
251                 if(++speed_it != params.end())\r
252                         speed = boost::lexical_cast<int>(*speed_it);\r
253         }\r
254 \r
255         if(speed == 0)\r
256                 return core::frame_producer::empty();\r
257 \r
258         return spl::make_shared<image_scroll_producer>(frame_factory, format_desc, filename + L"." + *ext, speed);\r
259 }\r
260 \r
261 }}