]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
aa6e3a6317139fc6ca2795b048d8c11a4ca9d842
[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 <sstream>\r
31 \r
32 namespace caspar { namespace core {\r
33         \r
34 class color_producer : public frame_producer\r
35 {\r
36         safe_ptr<basic_frame> frame_;\r
37         const std::wstring color_str_;\r
38 \r
39 public:\r
40         explicit color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color) \r
41                 : color_str_(color)\r
42                 , frame_(create_color_frame(this, frame_factory, color))\r
43         {}\r
44 \r
45         // frame_producer\r
46                         \r
47         virtual safe_ptr<basic_frame> receive(int) { return frame_; }   \r
48         virtual safe_ptr<basic_frame> last_frame() const { return frame_; }     \r
49         virtual std::wstring print() const { return L"color[" + color_str_ + L"]"; }\r
50 };\r
51 \r
52 safe_ptr<frame_producer> create_color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
53 {\r
54         if(params.empty() || params[0].at(0) != '#')\r
55                 return frame_producer::empty();\r
56         return make_safe<color_producer>(frame_factory, params[0]);\r
57 }\r
58 \r
59 safe_ptr<core::write_frame> create_color_frame(void* tag, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color)\r
60 {\r
61         if(color.length() != 9 || color[0] != '#')\r
62                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color)) << msg_info("Invalid color code"));\r
63 \r
64         auto frame = frame_factory->create_frame(tag, 1, 1, pixel_format::bgra);\r
65                 \r
66         // Read color from hex-string and write to frame pixel.\r
67         auto& value = *reinterpret_cast<uint32_t*>(frame->image_data().begin());\r
68         std::wstringstream str(color.substr(1));\r
69         str >> std::hex >> value;\r
70 \r
71         frame->commit();\r
72                 \r
73         return frame;\r
74 }\r
75 \r
76 }}