]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
2.0.2: INFO further improved. STATUS 1-1 removed in favor of INFO 1-1.
[casparcg] / core / producer / color / color_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 "../../stdafx.h"\r
21 \r
22 #include "color_producer.h"\r
23 \r
24 #include "../frame/basic_frame.h"\r
25 #include "../frame/frame_factory.h"\r
26 #include "../../mixer/write_frame.h"\r
27 \r
28 #include <common/exception/exceptions.h>\r
29 \r
30 #include <boost/algorithm/string.hpp>\r
31 \r
32 #include <sstream>\r
33 \r
34 namespace caspar { namespace core {\r
35         \r
36 class color_producer : public frame_producer\r
37 {\r
38         safe_ptr<basic_frame> frame_;\r
39         const std::wstring color_str_;\r
40 \r
41 public:\r
42         explicit color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color) \r
43                 : color_str_(color)\r
44                 , frame_(create_color_frame(this, frame_factory, color))\r
45         {}\r
46 \r
47         // frame_producer\r
48                         \r
49         virtual safe_ptr<basic_frame> receive(int) override\r
50         {\r
51                 return frame_;\r
52         }       \r
53 \r
54         virtual safe_ptr<basic_frame> last_frame() const override\r
55         {\r
56                 return frame_; \r
57         }       \r
58 \r
59         virtual std::wstring print() const override\r
60         {\r
61                 return L"color[" + color_str_ + L"]";\r
62         }\r
63 \r
64         boost::property_tree::wptree info() const override\r
65         {\r
66                 boost::property_tree::wptree info;\r
67                 info.add(L"type", L"color-producer");\r
68                 info.add(L"color", color_str_);\r
69                 return info;\r
70         }\r
71 };\r
72 \r
73 std::wstring get_hex_color(const std::wstring& str)\r
74 {\r
75         if(str.at(0) == '#')\r
76                 return str.length() == 7 ? L"#FF" + str.substr(1) : str;\r
77         \r
78         if(boost::iequals(str, L"EMPTY"))\r
79                 return L"#00000000";\r
80 \r
81         if(boost::iequals(str, L"BLACK"))\r
82                 return L"#FF000000";\r
83         \r
84         if(boost::iequals(str, L"WHITE"))\r
85                 return L"#FFFFFFFF";\r
86         \r
87         if(boost::iequals(str, L"RED"))\r
88                 return L"#FFFF0000";\r
89         \r
90         if(boost::iequals(str, L"GREEN"))\r
91                 return L"#FF00FF00";\r
92         \r
93         if(boost::iequals(str, L"BLUE"))\r
94                 return L"#FF0000FF";    \r
95         \r
96         if(boost::iequals(str, L"ORANGE"))\r
97                 return L"#FFFFA500";    \r
98         \r
99         if(boost::iequals(str, L"YELLOW"))\r
100                 return L"#FFFFFF00";\r
101         \r
102         if(boost::iequals(str, L"BROWN"))\r
103                 return L"#FFA52A2A";\r
104         \r
105         if(boost::iequals(str, L"GRAY"))\r
106                 return L"#FF808080";\r
107         \r
108         if(boost::iequals(str, L"TEAL"))\r
109                 return L"#FF008080";\r
110         \r
111         return str;\r
112 }\r
113 \r
114 safe_ptr<frame_producer> create_color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
115 {\r
116         if(params.size() < 0)\r
117                 return core::frame_producer::empty();\r
118 \r
119         auto color2 = get_hex_color(params[0]);\r
120         if(color2.length() != 9 || color2[0] != '#')\r
121                 return core::frame_producer::empty();\r
122 \r
123         return make_safe<color_producer>(frame_factory, color2);\r
124 }\r
125 safe_ptr<core::write_frame> create_color_frame(void* tag, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color)\r
126 {\r
127         auto color2 = get_hex_color(color);\r
128         if(color2.length() != 9 || color2[0] != '#')\r
129                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color."));\r
130         \r
131         core::pixel_format_desc desc;\r
132         desc.pix_fmt = pixel_format::bgra;\r
133         desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));\r
134         auto frame = frame_factory->create_frame(tag, desc);\r
135                 \r
136         // Read color from hex-string and write to frame pixel.\r
137 \r
138         auto& value = *reinterpret_cast<uint32_t*>(frame->image_data().begin());\r
139         std::wstringstream str(color2.substr(1));\r
140         if(!(str >> std::hex >> value) || !str.eof())\r
141                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color."));\r
142 \r
143         frame->commit();\r
144                 \r
145         return frame;\r
146 }\r
147 \r
148 }}