]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
2.0.0.2:
[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 "../../processor/draw_frame.h"\r
26 #include "../../format/video_format.h"\r
27 \r
28 #include <intrin.h>\r
29 #pragma intrinsic(__movsd, __stosd)\r
30 \r
31 namespace caspar { namespace core {\r
32         \r
33 unsigned int get_pixel_color_value(const std::wstring& parameter)\r
34 {\r
35         union Color \r
36         {\r
37                 struct Components \r
38                 {\r
39                         unsigned char a;\r
40                         unsigned char r;\r
41                         unsigned char g;\r
42                         unsigned char b;\r
43                 } comp;\r
44 \r
45                 unsigned int value;\r
46         };\r
47 \r
48         std::wstring color_code;\r
49         if(parameter.length() != 9 || parameter[0] != '#')\r
50                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("parameter") << arg_value_info(narrow(parameter)) << msg_info("Invalid color code"));\r
51         \r
52         color_code = parameter.substr(1);\r
53 \r
54         Color color;\r
55         color.value = _tcstoul(color_code.c_str(), 0, 16);\r
56         unsigned char temp = color.comp.a;\r
57         color.comp.a = color.comp.b;\r
58         color.comp.b = temp;\r
59         temp = color.comp.r;\r
60         color.comp.r = color.comp.g;\r
61         color.comp.g = temp;\r
62 \r
63         return color.value;\r
64 }\r
65 \r
66 class color_producer : public frame_producer\r
67 {\r
68 public:\r
69         explicit color_producer(const std::wstring& color) : color_str_(color), color_value_(get_pixel_color_value(color)), frame_(draw_frame::empty()){}\r
70         color_producer(color_producer&& other) : frame_(std::move(other.frame_)), color_value_(std::move(other.color_value_)), color_str_(std::move(other.color_str_)){}\r
71         \r
72         safe_ptr<draw_frame> receive()\r
73         { \r
74                 return frame_;\r
75         }\r
76 \r
77         void initialize(const frame_processor_device_ptr& frame_processor)\r
78         {\r
79                 auto frame = std::move(frame_processor->create_frame());\r
80                 __stosd(reinterpret_cast<unsigned long*>(frame->pixel_data().begin()), color_value_, frame->pixel_data().size() / sizeof(unsigned long));\r
81                 frame_ = std::move(frame);\r
82         }\r
83         \r
84         std::wstring print() const\r
85         {\r
86                 return + L"color_producer. color: " + color_str_;\r
87         }\r
88 \r
89         safe_ptr<draw_frame> frame_;\r
90         unsigned int color_value_;\r
91         std::wstring color_str_;\r
92 };\r
93 \r
94 safe_ptr<frame_producer> create_color_producer(const std::vector<std::wstring>& params)\r
95 {\r
96         if(params.empty() || params[0].at(0) != '#')\r
97                 return frame_producer::empty();\r
98         return make_safe<color_producer>(params[0]);\r
99 }\r
100 \r
101 }}