]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
2.1.0: Fully ported (apart from legacy protocol proect) to UTF8, instead of a big...
[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/basic_frame.h>\r
29 #include <core/producer/frame/frame_factory.h>\r
30 #include <core/mixer/write_frame.h>\r
31 \r
32 #include <common/env.h>\r
33 #include <common/log/log.h>\r
34 \r
35 #include <boost/assign.hpp>\r
36 #include <boost/filesystem.hpp>\r
37 #include <boost/property_tree/ptree.hpp>\r
38 \r
39 #include <algorithm>\r
40 \r
41 namespace caspar { namespace image {\r
42 \r
43 struct image_producer : public core::frame_producer\r
44 {       \r
45         const std::string filename_;\r
46         safe_ptr<core::basic_frame> frame_;\r
47         \r
48         explicit image_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::string& filename) \r
49                 : filename_(filename)\r
50                 , frame_(core::basic_frame::empty())    \r
51         {\r
52                 auto bitmap = load_image(filename_);\r
53                 FreeImage_FlipVertical(bitmap.get());\r
54                 \r
55                 core::pixel_format_desc desc;\r
56                 desc.pix_fmt = core::pixel_format::bgra;\r
57                 desc.planes.push_back(core::pixel_format_desc::plane(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4));\r
58                 auto frame = frame_factory->create_frame(this, desc);\r
59 \r
60                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame->image_data().size(), frame->image_data().begin());\r
61                 frame->commit();\r
62                 frame_ = std::move(frame);\r
63 \r
64                 CASPAR_LOG(info) << print() << " Initialized";\r
65         }\r
66         \r
67         // frame_producer\r
68 \r
69         virtual safe_ptr<core::basic_frame> receive(int) override\r
70         {\r
71                 return frame_;\r
72         }\r
73                 \r
74         virtual safe_ptr<core::basic_frame> last_frame() const override\r
75         {\r
76                 return frame_;\r
77         }\r
78 \r
79         virtual std::string print() const override\r
80         {\r
81                 return "image_producer[" + filename_ + "]";\r
82         }\r
83 \r
84         virtual boost::property_tree::ptree info() const override\r
85         {\r
86                 boost::property_tree::ptree info;\r
87                 info.add("type", "image-producer");\r
88                 info.add("filename", filename_);\r
89                 return info;\r
90         }\r
91 };\r
92 \r
93 safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::string>& params)\r
94 {\r
95         static const std::vector<std::string> extensions = boost::assign::list_of("png")("tga")("bmp")("jpg")("jpeg")("gif")("tiff")("tif")("jp2")("jpx")("j2k")("j2c");\r
96         std::string filename = env::media_folder() + "\\" + params[0];\r
97         \r
98         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::string& ex) -> bool\r
99                 {                                       \r
100                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));\r
101                 });\r
102 \r
103         if(ext == extensions.end())\r
104                 return core::frame_producer::empty();\r
105 \r
106         return make_safe<image_producer>(frame_factory, filename + "." + *ext);\r
107 }\r
108 \r
109 \r
110 }}