]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
2.0.0.2: Renamed draw_frame to basic_frame.
[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  \r
21 #include "../../stdafx.h"\r
22 \r
23 #include "color_producer.h"\r
24 \r
25 #include <mixer/frame/write_frame.h>\r
26 \r
27 #include <sstream>\r
28 \r
29 namespace caspar { namespace core {\r
30         \r
31 class color_producer : public frame_producer\r
32 {\r
33         safe_ptr<basic_frame> frame_;\r
34         std::wstring color_str_;\r
35         printer parent_printer_;\r
36 \r
37 public:\r
38 \r
39         explicit color_producer(const std::wstring& color) \r
40                 : color_str_(color)\r
41                 , frame_(basic_frame::empty())\r
42         {\r
43                 if(color.length() != 9 || color[0] != '#')\r
44                         BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color)) << msg_info("Invalid color code"));\r
45         }\r
46 \r
47         virtual void initialize(const safe_ptr<frame_factory>& frame_factory)\r
48         {\r
49                 auto frame = frame_factory->create_frame(1, 1, pixel_format::bgra);\r
50                 auto& value = *reinterpret_cast<unsigned long*>(frame->image_data().begin());\r
51                 std::wstringstream str(color_str_.substr(1));\r
52                 str >> std::hex >> value;       \r
53                 frame_ = std::move(frame);\r
54         }\r
55 \r
56         virtual void set_parent_printer(const printer& parent_printer){parent_printer_ = parent_printer;}\r
57         \r
58         virtual safe_ptr<basic_frame> receive() { return frame_; }\r
59         \r
60         virtual std::wstring print() const { return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"color[" + color_str_ + L"]"; }\r
61 };\r
62 \r
63 safe_ptr<frame_producer> create_color_producer(const std::vector<std::wstring>& params)\r
64 {\r
65         if(params.empty() || params[0].at(0) != '#')\r
66                 return frame_producer::empty();\r
67         return make_safe<color_producer>(params[0]);\r
68 }\r
69 \r
70 volatile struct reg\r
71 {\r
72         reg(){register_producer_factory(create_color_producer);}\r
73 } my_reg;\r
74 \r
75 }}