]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_scroll_producer.cpp
- more gpu refactoring.
[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/producer/frame/basic_frame.h>\r
29 #include <core/producer/frame/frame_factory.h>\r
30 #include <core/producer/frame/frame_transform.h>\r
31 #include <core/producer/frame/pixel_format.h>\r
32 #include <core/mixer/device_frame.h>\r
33 \r
34 #include <common/env.h>\r
35 #include <common/log.h>\r
36 #include <common/exception/exceptions.h>\r
37 \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
42 #include <boost/property_tree/ptree.hpp>\r
43 \r
44 #include <algorithm>\r
45 #include <array>\r
46 \r
47 using namespace boost::assign;\r
48 \r
49 namespace caspar { namespace image {\r
50                 \r
51 struct image_scroll_producer : public core::frame_producer\r
52 {       \r
53         const std::wstring                                                      filename_;\r
54         std::vector<safe_ptr<core::basic_frame>>        frames_;\r
55         core::video_format_desc                                         format_desc_;\r
56         int                                                                             width_;\r
57         int                                                                             height_;\r
58 \r
59         int                                                                                     delta_;\r
60         int                                                                                     speed_;\r
61 \r
62         std::array<double, 2>                                           start_offset_;\r
63 \r
64         safe_ptr<core::basic_frame>                                     last_frame_;\r
65         \r
66         explicit image_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int speed) \r
67                 : filename_(filename)\r
68                 , delta_(0)\r
69                 , format_desc_(frame_factory->get_video_format_desc())\r
70                 , speed_(speed)\r
71                 , last_frame_(core::basic_frame::empty())\r
72         {\r
73                 start_offset_.assign(0.0);\r
74 \r
75                 auto bitmap = load_image(filename_);\r
76                 FreeImage_FlipVertical(bitmap.get());\r
77 \r
78                 width_  = FreeImage_GetWidth(bitmap.get());\r
79                 height_ = FreeImage_GetHeight(bitmap.get());\r
80 \r
81                 auto bytes = FreeImage_GetBits(bitmap.get());\r
82                 int count = width_*height_*4;\r
83 \r
84                 if(height_ > format_desc_.height)\r
85                 {\r
86                         while(count > 0)\r
87                         {\r
88                                 core::pixel_format_desc desc;\r
89                                 desc.pix_fmt = 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, [&](const core::frame_factory::range_vector_type& ranges)\r
92                                 {                                       \r
93                                         if(count >= ranges.at(0).size())\r
94                                         {       \r
95                                                 std::copy_n(bytes + count - ranges.at(0).size(), ranges.at(0).size(), ranges.at(0).begin());\r
96                                                 count -= static_cast<int>(ranges.at(0).size());\r
97                                         }\r
98                                         else\r
99                                         {\r
100                                                 memset(ranges.at(0).begin(), 0, ranges.at(0).size());   \r
101                                                 std::copy_n(bytes, count, ranges.at(0).begin() + format_desc_.size - count);\r
102                                                 count = 0;\r
103                                         }                       \r
104                                 });\r
105 \r
106                                 frames_.push_back(frame);\r
107                         }\r
108                         \r
109                         if(speed_ < 0.0)\r
110                         {\r
111                                 auto offset = format_desc_.height - (height_ % format_desc_.height);\r
112                                 auto offset2 = offset * 0.5/static_cast<double>(format_desc_.height);\r
113                                 start_offset_[1] = (std::ceil(static_cast<double>(height_) / static_cast<double>(format_desc_.height)) + 1.0) * 0.5 - offset2;// - 1.5;\r
114                         }\r
115                 }\r
116                 else\r
117                 {\r
118                         int i = 0;\r
119                         while(count > 0)\r
120                         {\r
121                                 core::pixel_format_desc desc;\r
122                                 desc.pix_fmt = core::pixel_format::bgra;\r
123                                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width, height_, 4));\r
124                                 auto frame = frame_factory->create_frame(reinterpret_cast<void*>(rand()), desc, [&](const core::frame_factory::range_vector_type& ranges)\r
125                                 {       \r
126                                         if(count >= ranges.at(0).size())\r
127                                         {       \r
128                                                 for(int y = 0; y < height_; ++y)\r
129                                                         std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, format_desc_.width*4, ranges.at(0).begin() + y * format_desc_.width*4);\r
130                                         \r
131                                                 ++i;\r
132                                                 count -= static_cast<int>(ranges.at(0).size());\r
133                                         }\r
134                                         else\r
135                                         {\r
136                                                 memset(ranges.at(0).begin(), 0, ranges.at(0).size());   \r
137                                                 int width2 = width_ % format_desc_.width;\r
138                                                 for(int y = 0; y < height_; ++y)\r
139                                                         std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, width2*4, ranges.at(0).begin() + y * format_desc_.width*4);\r
140 \r
141                                                 count = 0;\r
142                                         }\r
143                                 });\r
144                         \r
145                                 frames_.push_back(frame);\r
146                         }\r
147 \r
148                         std::reverse(frames_.begin(), frames_.end());\r
149 \r
150                         if(speed_ > 0.0)\r
151                         {\r
152                                 auto offset = format_desc_.width - (width_ % format_desc_.width);\r
153                                 start_offset_[0] = offset * 0.5/static_cast<double>(format_desc_.width);\r
154                         }\r
155                         else\r
156                         {\r
157                                 start_offset_[0] = (std::ceil(static_cast<double>(width_) / static_cast<double>(format_desc_.width)) + 1.0) * 0.5;// - 1.5;\r
158                         }\r
159                 }\r
160 \r
161                 CASPAR_LOG(info) << print() << L" Initialized";\r
162         }\r
163         \r
164         // frame_producer\r
165 \r
166         virtual safe_ptr<core::basic_frame> receive(int) override\r
167         {               \r
168                 delta_ += speed_;\r
169 \r
170                 if(frames_.empty())\r
171                         return core::basic_frame::eof();\r
172                 \r
173                 if(height_ > format_desc_.height)\r
174                 {\r
175                         if(static_cast<int>(std::abs(delta_)) >= height_ - format_desc_.height)\r
176                                 return core::basic_frame::eof();\r
177 \r
178                         for(int n = 0; n < frames_.size(); ++n)\r
179                         {\r
180                                 frames_[n]->get_frame_transform().fill_translation[0] = start_offset_[0];\r
181                                 frames_[n]->get_frame_transform().fill_translation[1] = start_offset_[1] - (n+1) + delta_ * 0.5/static_cast<double>(format_desc_.height);\r
182                         }\r
183                 }\r
184                 else\r
185                 {\r
186                         if(static_cast<int>(std::abs(delta_)) >= width_ - format_desc_.width)\r
187                                 return core::basic_frame::eof();\r
188 \r
189                         for(int n = 0; n < frames_.size(); ++n)\r
190                         {\r
191                                 frames_[n]->get_frame_transform().fill_translation[0] = start_offset_[0] - (n+1) + delta_ * 0.5/static_cast<double>(format_desc_.width);                                \r
192                                 frames_[n]->get_frame_transform().fill_translation[1] = start_offset_[1];\r
193                         }\r
194                 }\r
195 \r
196                 return last_frame_ = make_safe<core::basic_frame>(frames_);\r
197         }\r
198 \r
199         virtual safe_ptr<core::basic_frame> last_frame() const override\r
200         {\r
201                 return last_frame_;\r
202         }\r
203                 \r
204         virtual std::wstring print() const override\r
205         {\r
206                 return L"image_scroll_producer[" + filename_ + L"]";\r
207         }\r
208 \r
209         virtual boost::property_tree::wptree info() const override\r
210         {\r
211                 boost::property_tree::wptree info;\r
212                 info.add(L"type", L"image-scroll-producer");\r
213                 info.add(L"filename", filename_);\r
214                 return info;\r
215         }\r
216 \r
217         virtual uint32_t nb_frames() const override\r
218         {\r
219                 if(height_ > format_desc_.height)\r
220                 {\r
221                         auto length = (height_ - format_desc_.height);\r
222                         return length/std::abs(speed_);// + length % std::abs(delta_));\r
223                 }\r
224                 else\r
225                 {\r
226                         auto length = (width_ - format_desc_.width);\r
227                         auto result = length/std::abs(speed_);// + length % std::abs(delta_));\r
228                         return result;\r
229                 }\r
230         }\r
231 };\r
232 \r
233 safe_ptr<core::frame_producer> create_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
234 {\r
235         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
236         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
237         \r
238         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
239                 {                                       \r
240                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
241                 });\r
242 \r
243         if(ext == extensions.end())\r
244                 return core::frame_producer::empty();\r
245         \r
246         int speed = 0;\r
247         auto speed_it = std::find(params.begin(), params.end(), L"SPEED");\r
248         if(speed_it != params.end())\r
249         {\r
250                 if(++speed_it != params.end())\r
251                         speed = boost::lexical_cast<int>(*speed_it);\r
252         }\r
253 \r
254         if(speed == 0)\r
255                 return core::frame_producer::empty();\r
256 \r
257         return create_producer_print_proxy(\r
258                         make_safe<image_scroll_producer>(frame_factory, filename + L"." + *ext, speed));\r
259 }\r
260 \r
261 }}