]> 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 "../../frame/frame_format.h"\r
26 \r
27 #include <intrin.h>\r
28 #pragma intrinsic(__movsd, __stosd)\r
29 \r
30 namespace caspar { namespace core {\r
31 \r
32 class color_producer : public frame_producer\r
33 {\r
34 public:\r
35         explicit color_producer(unsigned int color_value, const frame_format_desc& format_desc) \r
36                 : color_value_(color_value), format_desc_(format_desc){}\r
37 \r
38         ~color_producer()\r
39         {\r
40                 if(factory_)\r
41                         factory_->release_frames(this);\r
42         }\r
43 \r
44         gpu_frame_ptr get_frame()\r
45         { \r
46                 return frame_;\r
47         }\r
48 \r
49         const frame_format_desc& get_frame_format_desc() const { return format_desc_; }\r
50         \r
51         void initialize(const frame_factory_ptr& factory)\r
52         {\r
53                 factory_ = factory;\r
54                 frame_ = factory->create_frame(format_desc_, this);\r
55                 __stosd(reinterpret_cast<unsigned long*>(frame_->data()), color_value_, frame_->size() / sizeof(unsigned long));\r
56         }\r
57 \r
58         frame_factory_ptr factory_;\r
59         frame_format_desc format_desc_;\r
60         gpu_frame_ptr frame_;\r
61         unsigned int color_value_;\r
62 };\r
63 \r
64 union Color \r
65 {\r
66         struct Components \r
67         {\r
68                 unsigned char a;\r
69                 unsigned char r;\r
70                 unsigned char g;\r
71                 unsigned char b;\r
72         } comp;\r
73 \r
74         unsigned int value;\r
75 };\r
76 \r
77 unsigned int get_pixel_color_value(const std::wstring& parameter)\r
78 {\r
79         std::wstring color_code;\r
80         if(parameter.length() != 9 || parameter[0] != '#')\r
81                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("parameter") << arg_value_info(common::narrow(parameter)) << msg_info("Invalid color code"));\r
82         \r
83         color_code = parameter.substr(1);\r
84 \r
85         Color color;\r
86         color.value = _tcstoul(color_code.c_str(), 0, 16);\r
87         unsigned char temp = color.comp.a;\r
88         color.comp.a = color.comp.b;\r
89         color.comp.b = temp;\r
90         temp = color.comp.r;\r
91         color.comp.r = color.comp.g;\r
92         color.comp.g = temp;\r
93 \r
94         return color.value;\r
95 }\r
96 \r
97 frame_producer_ptr create_color_producer(const std::vector<std::wstring>& params, const frame_format_desc& format_desc)\r
98 {\r
99         if(params.empty() || params[0].at(0) != '#')\r
100                 return nullptr;\r
101         return std::make_shared<color_producer>(get_pixel_color_value(params[0]), format_desc);\r
102 }\r
103 \r
104 }}