]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
2.0.2: INFO further improved. STATUS 1-1 removed in favor of INFO 1-1.
[casparcg] / modules / image / producer / image_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "image_producer.h"\r
21 \r
22 #include "../util/image_loader.h"\r
23 \r
24 #include <core/video_format.h>\r
25 \r
26 #include <core/producer/frame/basic_frame.h>\r
27 #include <core/producer/frame/frame_factory.h>\r
28 #include <core/mixer/write_frame.h>\r
29 \r
30 #include <common/env.h>\r
31 \r
32 #include <boost/assign.hpp>\r
33 #include <boost/filesystem.hpp>\r
34 #include <boost/property_tree/ptree.hpp>\r
35 \r
36 #include <algorithm>\r
37 \r
38 using namespace boost::assign;\r
39 \r
40 namespace caspar { namespace image {\r
41 \r
42 struct image_producer : public core::frame_producer\r
43 {       \r
44         const std::wstring filename_;\r
45         safe_ptr<core::basic_frame> frame_;\r
46         \r
47         explicit image_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
48                 : filename_(filename)\r
49                 , frame_(core::basic_frame::empty())    \r
50         {\r
51                 auto bitmap = load_image(filename_);\r
52                 FreeImage_FlipVertical(bitmap.get());\r
53                 \r
54                 core::pixel_format_desc desc;\r
55                 desc.pix_fmt = core::pixel_format::bgra;\r
56                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));\r
57                 auto frame = frame_factory->create_frame(this, desc);\r
58 \r
59                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame->image_data().size(), frame->image_data().begin());\r
60                 frame->commit();\r
61                 frame_ = std::move(frame);\r
62         }\r
63         \r
64         // frame_producer\r
65 \r
66         virtual safe_ptr<core::basic_frame> receive(int) override\r
67         {\r
68                 return frame_;\r
69         }\r
70                 \r
71         virtual safe_ptr<core::basic_frame> last_frame() const override\r
72         {\r
73                 return frame_;\r
74         }\r
75 \r
76         virtual std::wstring print() const override\r
77         {\r
78                 return L"image_producer[" + filename_ + L"]";\r
79         }\r
80 \r
81         virtual boost::property_tree::wptree info() const override\r
82         {\r
83                 boost::property_tree::wptree info;\r
84                 info.add(L"type", L"image-producer");\r
85                 info.add(L"filename", filename_);\r
86                 return info;\r
87         }\r
88 };\r
89 \r
90 safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
91 {\r
92         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
93         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
94         \r
95         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
96                 {                                       \r
97                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
98                 });\r
99 \r
100         if(ext == extensions.end())\r
101                 return core::frame_producer::empty();\r
102 \r
103         return make_safe<image_producer>(frame_factory, filename + L"." + *ext);\r
104 }\r
105 \r
106 \r
107 }}