]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / producer / color / color_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../stdafx.h"
23
24 #include "color_producer.h"
25
26 #include <core/producer/frame_producer.h>
27 #include <core/frame/frame.h>
28 #include <core/frame/draw_frame.h>
29 #include <core/frame/frame_factory.h>
30 #include <core/frame/pixel_format.h>
31 #include <core/monitor/monitor.h>
32
33 #include <common/except.h>
34 #include <common/array.h>
35
36 #include <boost/algorithm/string.hpp>
37
38 #include <sstream>
39
40 namespace caspar { namespace core {
41
42 class color_producer : public frame_producer_base
43 {
44         monitor::basic_subject  event_subject_;
45
46         const std::wstring              color_str_;
47         constraints                             constraints_;
48         draw_frame                              frame_;
49
50 public:
51         color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, uint32_t value)
52                 : color_str_(L"")
53                 , constraints_(1, 1)
54                 , frame_(create_color_frame(this, frame_factory, value))
55         {
56                 CASPAR_LOG(info) << print() << L" Initialized";
57         }
58
59
60         color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color) 
61                 : color_str_(color)
62                 , constraints_(1, 1)
63                 , frame_(create_color_frame(this, frame_factory, color))
64         {
65                 CASPAR_LOG(info) << print() << L" Initialized";
66         }
67
68         // frame_producer
69                         
70         draw_frame receive_impl() override
71         {
72                 event_subject_ << monitor::event("color") % color_str_;
73
74                 return frame_;
75         }
76
77         constraints& pixel_constraints() override
78         {
79                 return constraints_;
80         }
81         
82         std::wstring print() const override
83         {
84                 return L"color[" + color_str_ + L"]";
85         }
86
87         std::wstring name() const override
88         {
89                 return L"color";
90         }
91         
92         boost::property_tree::wptree info() const override
93         {
94                 boost::property_tree::wptree info;
95                 info.add(L"type", L"color");
96                 info.add(L"color", color_str_);
97                 return info;
98         }
99
100         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     
101         {
102                 return event_subject_.subscribe(o);
103         }
104
105         void unsubscribe(const monitor::observable::observer_ptr& o) override           
106         {
107                 return event_subject_.unsubscribe(o);
108         }
109 };
110
111 std::wstring get_hex_color(const std::wstring& str)
112 {
113         if(str.at(0) == '#')
114                 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
115         
116         std::wstring col_str = boost::to_upper_copy(str);
117
118         if(col_str == L"EMPTY")
119                 return L"#00000000";
120
121         if(col_str == L"BLACK")
122                 return L"#FF000000";
123         
124         if(col_str == L"WHITE")
125                 return L"#FFFFFFFF";
126         
127         if(col_str == L"RED")
128                 return L"#FFFF0000";
129         
130         if(col_str == L"GREEN")
131                 return L"#FF00FF00";
132         
133         if(col_str == L"BLUE")
134                 return L"#FF0000FF";    
135         
136         if(col_str == L"ORANGE")
137                 return L"#FFFFA500";    
138         
139         if(col_str == L"YELLOW")
140                 return L"#FFFFFF00";
141         
142         if(col_str == L"BROWN")
143                 return L"#FFA52A2A";
144         
145         if(col_str == L"GRAY")
146                 return L"#FF808080";
147         
148         if(col_str == L"TEAL")
149                 return L"#FF008080";
150         
151         return str;
152 }
153
154 bool try_get_color(const std::wstring& str, uint32_t& value)
155 {
156         auto color_str = get_hex_color(str);
157         if(color_str.length() != 9 || color_str[0] != '#')
158                 return false;
159
160         std::wstringstream ss(color_str.substr(1));
161         if(!(ss >> std::hex >> value) || !ss.eof())
162                 return false;
163         
164         return true;
165 }
166
167 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
168 {
169         return spl::make_shared<color_producer>(frame_factory, value);
170 }
171
172 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
173 {
174         if(params.size() < 0)
175                 return core::frame_producer::empty();
176
177         uint32_t value = 0;
178         if(!try_get_color(params[0], value))
179                 return core::frame_producer::empty();
180
181         return spl::make_shared<color_producer>(frame_factory, params[0]);
182 }
183
184 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
185 {
186         core::pixel_format_desc desc(pixel_format::bgra);
187         desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
188         auto frame = frame_factory->create_frame(tag, desc);
189         
190         *reinterpret_cast<uint32_t*>(frame.image_data(0).begin()) = value;
191
192         return core::draw_frame(std::move(frame));
193 }
194
195 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& str)
196 {
197         uint32_t value = 0;
198         if(!try_get_color(str, value))
199                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(str) << msg_info("Invalid color."));
200         
201         return create_color_frame(tag, frame_factory, value);
202 }
203
204 }}