]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
2.0.0.2: Mayor solution reconfiguration.
[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 <mixer/frame_mixer_device.h>\r
8 #include <mixer/frame/draw_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         printer parent_printer_;\r
24         std::shared_ptr<core::frame_factory> frame_factory_;\r
25         std::wstring filename_;\r
26         safe_ptr<core::draw_frame> frame_;\r
27         \r
28         image_producer(const std::wstring& filename) \r
29                 : filename_(filename), frame_(core::draw_frame::empty())        {}\r
30         \r
31         virtual safe_ptr<core::draw_frame> receive(){return frame_;}\r
32 \r
33         virtual void initialize(const safe_ptr<core::frame_factory>& frame_factory)\r
34         {\r
35                 frame_factory_ = frame_factory;\r
36                 auto bitmap = load_image(filename_);\r
37                 FreeImage_FlipVertical(bitmap.get());\r
38                 auto frame = frame_factory->create_frame(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()));\r
39                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame->image_data().size(), frame->image_data().begin());\r
40                 frame_ = std::move(frame);\r
41         }\r
42         \r
43         virtual void set_parent_printer(const printer& parent_printer) \r
44         {\r
45                 parent_printer_ = parent_printer;\r
46         }\r
47 \r
48         virtual std::wstring print() const\r
49         {\r
50                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"image_producer. filename: " + filename_;\r
51         }\r
52 };\r
53 \r
54 safe_ptr<core::frame_producer> create_image_producer(const std::vector<std::wstring>& params)\r
55 {\r
56         static const std::vector<std::wstring> extensions = list_of(L"png")(L"tga")(L"bmp")(L"jpg")(L"jpeg");\r
57         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
58         \r
59         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
60                 {                                       \r
61                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
62                 });\r
63 \r
64         if(ext == extensions.end())\r
65                 return core::frame_producer::empty();\r
66 \r
67         return make_safe<image_producer>(filename + L"." + *ext);\r
68 }\r
69 \r
70 \r
71 }