]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_scroll_producer.cpp
Header cleanup.
[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/write_frame.h>\r
33 \r
34 #include <common/env.h>\r
35 #include <common/log/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);\r
92 \r
93                                 if(count >= frame->image_data().size())\r
94                                 {       \r
95                                         std::copy_n(bytes + count - frame->image_data().size(), frame->image_data().size(), frame->image_data().begin());\r
96                                         count -= static_cast<int>(frame->image_data().size());\r
97                                 }\r
98                                 else\r
99                                 {\r
100                                         memset(frame->image_data().begin(), 0, frame->image_data().size());     \r
101                                         std::copy_n(bytes, count, frame->image_data().begin() + format_desc_.size - count);\r
102                                         count = 0;\r
103                                 }\r
104                         \r
105                                 frame->commit();\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);\r
125                                 if(count >= frame->image_data().size())\r
126                                 {       \r
127                                         for(int y = 0; y < height_; ++y)\r
128                                                 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
129                                         \r
130                                         ++i;\r
131                                         count -= static_cast<int>(frame->image_data().size());\r
132                                 }\r
133                                 else\r
134                                 {\r
135                                         memset(frame->image_data().begin(), 0, frame->image_data().size());     \r
136                                         int width2 = width_ % format_desc_.width;\r
137                                         for(int y = 0; y < height_; ++y)\r
138                                                 std::copy_n(bytes + i * format_desc_.width*4 + y * width_*4, width2*4, frame->image_data().begin() + y * format_desc_.width*4);\r
139 \r
140                                         count = 0;\r
141                                 }\r
142                         \r
143                                 frame->commit();\r
144                                 frames_.push_back(frame);\r
145                         }\r
146 \r
147                         std::reverse(frames_.begin(), frames_.end());\r
148 \r
149                         if(speed_ > 0.0)\r
150                         {\r
151                                 auto offset = format_desc_.width - (width_ % format_desc_.width);\r
152                                 start_offset_[0] = offset * 0.5/static_cast<double>(format_desc_.width);\r
153                         }\r
154                         else\r
155                         {\r
156                                 start_offset_[0] = (std::ceil(static_cast<double>(width_) / static_cast<double>(format_desc_.width)) + 1.0) * 0.5;// - 1.5;\r
157                         }\r
158                 }\r
159 \r
160                 CASPAR_LOG(info) << print() << L" Initialized";\r
161         }\r
162         \r
163         // frame_producer\r
164 \r
165         virtual safe_ptr<core::basic_frame> receive(int) override\r
166         {               \r
167                 delta_ += speed_;\r
168 \r
169                 if(frames_.empty())\r
170                         return core::basic_frame::eof();\r
171                 \r
172                 if(height_ > format_desc_.height)\r
173                 {\r
174                         if(static_cast<int>(std::abs(delta_)) >= height_ - format_desc_.height)\r
175                                 return core::basic_frame::eof();\r
176 \r
177                         for(int n = 0; n < frames_.size(); ++n)\r
178                         {\r
179                                 frames_[n]->get_frame_transform().fill_translation[0] = start_offset_[0];\r
180                                 frames_[n]->get_frame_transform().fill_translation[1] = start_offset_[1] - (n+1) + delta_ * 0.5/static_cast<double>(format_desc_.height);\r
181                         }\r
182                 }\r
183                 else\r
184                 {\r
185                         if(static_cast<int>(std::abs(delta_)) >= width_ - format_desc_.width)\r
186                                 return core::basic_frame::eof();\r
187 \r
188                         for(int n = 0; n < frames_.size(); ++n)\r
189                         {\r
190                                 frames_[n]->get_frame_transform().fill_translation[0] = start_offset_[0] - (n+1) + delta_ * 0.5/static_cast<double>(format_desc_.width);                                \r
191                                 frames_[n]->get_frame_transform().fill_translation[1] = start_offset_[1];\r
192                         }\r
193                 }\r
194 \r
195                 return last_frame_ = make_safe<core::basic_frame>(frames_);\r
196         }\r
197 \r
198         virtual safe_ptr<core::basic_frame> last_frame() const override\r
199         {\r
200                 return last_frame_;\r
201         }\r
202                 \r
203         virtual std::wstring print() const override\r
204         {\r
205                 return L"image_scroll_producer[" + filename_ + L"]";\r
206         }\r
207 \r
208         virtual boost::property_tree::wptree info() const override\r
209         {\r
210                 boost::property_tree::wptree info;\r
211                 info.add(L"type", L"image-scroll-producer");\r
212                 info.add(L"filename", filename_);\r
213                 return info;\r
214         }\r
215 \r
216         virtual uint32_t nb_frames() const override\r
217         {\r
218                 if(height_ > format_desc_.height)\r
219                 {\r
220                         auto length = (height_ - format_desc_.height);\r
221                         return length/std::abs(speed_);// + length % std::abs(delta_));\r
222                 }\r
223                 else\r
224                 {\r
225                         auto length = (width_ - format_desc_.width);\r
226                         auto result = length/std::abs(speed_);// + length % std::abs(delta_));\r
227                         return result;\r
228                 }\r
229         }\r
230 };\r
231 \r
232 safe_ptr<core::frame_producer> create_scroll_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
233 {\r
234         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
235         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
236         \r
237         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
238                 {                                       \r
239                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
240                 });\r
241 \r
242         if(ext == extensions.end())\r
243                 return core::frame_producer::empty();\r
244         \r
245         int speed = 0;\r
246         auto speed_it = std::find(params.begin(), params.end(), L"SPEED");\r
247         if(speed_it != params.end())\r
248         {\r
249                 if(++speed_it != params.end())\r
250                         speed = boost::lexical_cast<int>(*speed_it);\r
251         }\r
252 \r
253         if(speed == 0)\r
254                 return core::frame_producer::empty();\r
255 \r
256         return make_safe<image_scroll_producer>(frame_factory, filename + L"." + *ext, speed);\r
257 }\r
258 \r
259 }}