]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
2.0.0.2: - Removed parent printer to reduce complexity. Might be re-added in the...
[casparcg] / modules / image / producer / image_producer.cpp
1 #include "image_producer.h"\r
2 \r
3 #include "../util/image_loader.h"\r
4 \r
5 #include <core/video_format.h>\r
6 \r
7 #include <core/producer/frame/basic_frame.h>\r
8 #include <core/producer/frame/write_frame.h>\r
9 \r
10 #include <common/env.h>\r
11 \r
12 #include <boost/assign.hpp>\r
13 #include <boost/filesystem.hpp>\r
14 \r
15 #include <algorithm>\r
16 \r
17 using namespace boost::assign;\r
18 \r
19 namespace caspar {\r
20 \r
21 struct image_producer : public core::frame_producer\r
22 {       \r
23         std::shared_ptr<core::frame_factory> frame_factory_;\r
24         std::wstring filename_;\r
25         safe_ptr<core::basic_frame> frame_;\r
26         decltype(load_image(L"")) bitmap_;\r
27         \r
28         explicit image_producer(const std::wstring& filename) \r
29                 : filename_(filename)\r
30                 , frame_(core::basic_frame::empty())    \r
31                 , bitmap_(load_image(filename_))\r
32         {\r
33                 FreeImage_FlipVertical(bitmap_.get());\r
34         }\r
35         \r
36         virtual safe_ptr<core::basic_frame> receive(){return frame_;}\r
37 \r
38         virtual void set_frame_factory(const safe_ptr<core::frame_factory>& frame_factory)\r
39         {\r
40                 frame_factory_ = frame_factory;\r
41                 auto frame = frame_factory->create_frame(FreeImage_GetWidth(bitmap_.get()), FreeImage_GetHeight(bitmap_.get()));\r
42                 std::copy_n(FreeImage_GetBits(bitmap_.get()), frame->image_data().size(), frame->image_data().begin());\r
43                 bitmap_.reset();\r
44                 frame_ = std::move(frame);\r
45         }\r
46         \r
47         virtual std::wstring print() const\r
48         {\r
49                 return L"image_producer[" + filename_ + L"]";\r
50         }\r
51 };\r
52 \r
53 safe_ptr<core::frame_producer> create_image_producer(const std::vector<std::wstring>& params)\r
54 {\r
55         static const std::vector<std::wstring> extensions = list_of(L"png")(L"tga")(L"bmp")(L"jpg")(L"jpeg");\r
56         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
57         \r
58         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
59                 {                                       \r
60                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
61                 });\r
62 \r
63         if(ext == extensions.end())\r
64                 return core::frame_producer::empty();\r
65 \r
66         return make_safe<image_producer>(filename + L"." + *ext);\r
67 }\r
68 \r
69 \r
70 }