]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
2.1.0: -common: Restructured files. Moved most files into root for easier inclusion...
[casparcg] / modules / image / producer / image_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_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_producer.h>\r
29 #include <core/frame/frame.h>\r
30 #include <core/frame/draw_frame.h>\r
31 #include <core/frame/frame_factory.h>\r
32 #include <core/frame/pixel_format.h>\r
33 #include <core/monitor/monitor.h>\r
34 \r
35 #include <common/env.h>\r
36 #include <common/log.h>\r
37 #include <common/array.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         monitor::basic_subject  event_subject_;\r
52         const std::wstring              filename_;\r
53         core::draw_frame                frame_;\r
54         \r
55         explicit image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
56                 : filename_(filename)\r
57                 , frame_(core::draw_frame::empty())     \r
58         {\r
59                 auto bitmap = load_image(filename_);\r
60                 FreeImage_FlipVertical(bitmap.get());\r
61                 \r
62                 core::pixel_format_desc desc = core::pixel_format::bgra;\r
63                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));\r
64                 auto frame = frame_factory->create_frame(this, desc);\r
65 \r
66                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data(0).size(), frame.image_data(0).begin());\r
67                 frame_ = core::draw_frame(std::move(frame));\r
68 \r
69                 CASPAR_LOG(info) << print() << L" Initialized";\r
70         }\r
71         \r
72         // frame_producer\r
73 \r
74         virtual core::draw_frame receive(int) override\r
75         {\r
76                 event_subject_ << monitor::event("file/path") % filename_;\r
77 \r
78                 return frame_;\r
79         }\r
80 \r
81         virtual core::draw_frame last_frame() const override\r
82         {\r
83                 return frame_;\r
84         }\r
85                 \r
86         virtual std::wstring print() const override\r
87         {\r
88                 return L"image_producer[" + filename_ + L"]";\r
89         }\r
90 \r
91         virtual std::wstring name() const override\r
92         {\r
93                 return L"image";\r
94         }\r
95 \r
96         virtual boost::property_tree::wptree info() const override\r
97         {\r
98                 boost::property_tree::wptree info;\r
99                 info.add(L"type", L"image");\r
100                 info.add(L"filename", filename_);\r
101                 return info;\r
102         }\r
103 \r
104         virtual void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     \r
105         {\r
106                 return event_subject_.subscribe(o);\r
107         }\r
108 \r
109         virtual void unsubscribe(const monitor::observable::observer_ptr& o) override           \r
110         {\r
111                 return event_subject_.unsubscribe(o);\r
112         }\r
113 };\r
114 \r
115 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)\r
116 {\r
117         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
118         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
119         \r
120         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
121                 {                       \r
122                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));\r
123                 });\r
124 \r
125         if(ext == extensions.end())\r
126                 return core::frame_producer::empty();\r
127 \r
128         return spl::make_shared<image_producer>(frame_factory, filename + *ext);\r
129 }\r
130 \r
131 \r
132 }}