]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
Flash producer
[casparcg] / modules / image / producer / image_producer.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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_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/parameters/parameters.h>\r
29 #include <core/monitor/monitor.h>\r
30 #include <core/producer/frame/basic_frame.h>\r
31 #include <core/producer/frame/frame_factory.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/utility/base64.h>\r
37 #include <common/utility/string.h>\r
38 \r
39 #include <boost/assign.hpp>\r
40 #include <boost/filesystem.hpp>\r
41 #include <boost/property_tree/ptree.hpp>\r
42 \r
43 #include <algorithm>\r
44 \r
45 using namespace boost::assign;\r
46 \r
47 namespace caspar { namespace image {\r
48 \r
49 struct image_producer : public core::frame_producer\r
50 {       \r
51         const std::wstring description_;\r
52         core::monitor::subject          monitor_subject_;\r
53         const safe_ptr<core::frame_factory> frame_factory_;     safe_ptr<core::basic_frame> frame_;\r
54         \r
55         explicit image_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
56                 : description_(filename)\r
57                 , frame_factory_(frame_factory)\r
58                 , frame_(core::basic_frame::empty())    \r
59         {\r
60                 load(load_image(filename));\r
61         }\r
62 \r
63         explicit image_producer(const safe_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size)\r
64                 : description_(L"png from memory")\r
65                 , frame_factory_(frame_factory)\r
66                 , frame_(core::basic_frame::empty())\r
67         {\r
68                 load(load_png_from_memory(png_data, size));\r
69         }\r
70 \r
71         void load(const std::shared_ptr<FIBITMAP>& bitmap)\r
72         {\r
73                 FreeImage_FlipVertical(bitmap.get());\r
74 \r
75                 core::pixel_format_desc desc;\r
76                 desc.pix_fmt = core::pixel_format::bgra;\r
77                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));\r
78                 auto frame = frame_factory_->create_frame(this, desc);\r
79 \r
80                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame->image_data().size(), frame->image_data().begin());\r
81                 frame->commit();\r
82                 frame_ = std::move(frame);\r
83         }\r
84 \r
85         // frame_producer\r
86 \r
87         virtual safe_ptr<core::basic_frame> receive(int) override\r
88         {\r
89                 monitor_subject_ << core::monitor::message("/file/path") % description_;\r
90 \r
91                 return frame_;\r
92         }\r
93                 \r
94         virtual safe_ptr<core::basic_frame> last_frame() const override\r
95         {\r
96                 return frame_;\r
97         }\r
98 \r
99         virtual safe_ptr<core::basic_frame> create_thumbnail_frame() override\r
100         {\r
101                 return frame_;\r
102         }\r
103                 \r
104         virtual std::wstring print() const override\r
105         {\r
106                 return L"image_producer[" + description_ + L"]";\r
107         }\r
108 \r
109         virtual boost::property_tree::wptree info() const override\r
110         {\r
111                 boost::property_tree::wptree info;\r
112                 info.add(L"type", L"image-producer");\r
113                 info.add(L"location", description_);\r
114                 return info;\r
115         }\r
116 \r
117         core::monitor::subject& monitor_output()\r
118         {\r
119                 return monitor_subject_;\r
120         }\r
121 };\r
122 \r
123 safe_ptr<core::frame_producer> create_raw_producer(\r
124         const safe_ptr<core::frame_factory>& frame_factory,\r
125         const core::parameters& params)\r
126 {\r
127         if (params[0] == L"[PNG_BASE64]")\r
128         {\r
129                 if (params.size() < 2)\r
130                         return core::frame_producer::empty();\r
131 \r
132                 auto png_data = from_base64(narrow(params.at_original(1)));\r
133 \r
134                 return make_safe<image_producer>(frame_factory, png_data.data(), png_data.size());\r
135         }\r
136 \r
137         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
138         std::wstring filename = env::media_folder() + params.at_original(0);\r
139         \r
140         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
141                 {                                       \r
142                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
143                 });\r
144 \r
145         if(ext == extensions.end())\r
146                 return core::frame_producer::empty();\r
147 \r
148         return make_safe<image_producer>(frame_factory, filename + L"." + *ext);\r
149 }\r
150 \r
151 safe_ptr<core::frame_producer> create_producer(\r
152                 const safe_ptr<core::frame_factory>& frame_factory,\r
153                 const core::parameters& params)\r
154 {\r
155         auto raw_producer = create_raw_producer(frame_factory, params);\r
156 \r
157         if (raw_producer == core::frame_producer::empty())\r
158                 return raw_producer;\r
159 \r
160         return create_producer_print_proxy(raw_producer);\r
161 }\r
162 \r
163 safe_ptr<core::frame_producer> create_thumbnail_producer(\r
164                 const safe_ptr<core::frame_factory>& frame_factory,\r
165                 const core::parameters& params)\r
166 {\r
167         return create_raw_producer(frame_factory, params);\r
168 }\r
169 \r
170 }}