]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
88882372793287bfda52588e393a91a02efd1577
[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/frame/audio_channel_layout.h>
32 #include <core/monitor/monitor.h>
33
34 #include <common/except.h>
35 #include <common/array.h>
36
37 #include <boost/algorithm/string.hpp>
38
39 #include <sstream>
40
41 namespace caspar { namespace core {
42
43 class color_producer : public frame_producer_base
44 {
45         monitor::subject                monitor_subject_;
46
47         const std::wstring              color_str_;
48         constraints                             constraints_;
49         draw_frame                              frame_;
50
51 public:
52         color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, uint32_t value)
53                 : color_str_(L"")
54                 , constraints_(1, 1)
55                 , frame_(create_color_frame(this, frame_factory, value))
56         {
57                 CASPAR_LOG(info) << print() << L" Initialized";
58         }
59
60
61         color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color) 
62                 : color_str_(color)
63                 , constraints_(1, 1)
64                 , frame_(create_color_frame(this, frame_factory, color))
65         {
66                 CASPAR_LOG(info) << print() << L" Initialized";
67         }
68
69         // frame_producer
70                         
71         draw_frame receive_impl() override
72         {
73                 monitor_subject_ << monitor::message("color") % color_str_;
74
75                 return frame_;
76         }
77
78         constraints& pixel_constraints() override
79         {
80                 return constraints_;
81         }
82         
83         std::wstring print() const override
84         {
85                 return L"color[" + color_str_ + L"]";
86         }
87
88         std::wstring name() const override
89         {
90                 return L"color";
91         }
92         
93         boost::property_tree::wptree info() const override
94         {
95                 boost::property_tree::wptree info;
96                 info.add(L"type", L"color");
97                 info.add(L"color", color_str_);
98                 return info;
99         }
100
101         monitor::subject& monitor_output() override {return monitor_subject_;}                                                                          
102 };
103
104 std::wstring get_hex_color(const std::wstring& str)
105 {
106         if(str.at(0) == '#')
107                 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
108         
109         std::wstring col_str = boost::to_upper_copy(str);
110
111         if(col_str == L"EMPTY")
112                 return L"#00000000";
113
114         if(col_str == L"BLACK")
115                 return L"#FF000000";
116         
117         if(col_str == L"WHITE")
118                 return L"#FFFFFFFF";
119         
120         if(col_str == L"RED")
121                 return L"#FFFF0000";
122         
123         if(col_str == L"GREEN")
124                 return L"#FF00FF00";
125         
126         if(col_str == L"BLUE")
127                 return L"#FF0000FF";    
128         
129         if(col_str == L"ORANGE")
130                 return L"#FFFFA500";    
131         
132         if(col_str == L"YELLOW")
133                 return L"#FFFFFF00";
134         
135         if(col_str == L"BROWN")
136                 return L"#FFA52A2A";
137         
138         if(col_str == L"GRAY")
139                 return L"#FF808080";
140         
141         if(col_str == L"TEAL")
142                 return L"#FF008080";
143         
144         return str;
145 }
146
147 bool try_get_color(const std::wstring& str, uint32_t& value)
148 {
149         auto color_str = get_hex_color(str);
150         if(color_str.length() != 9 || color_str[0] != '#')
151                 return false;
152
153         std::wstringstream ss(color_str.substr(1));
154         if(!(ss >> std::hex >> value) || !ss.eof())
155                 return false;
156         
157         return true;
158 }
159
160 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
161 {
162         return spl::make_shared<color_producer>(frame_factory, value);
163 }
164
165 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
166 {
167         if(params.size() < 0)
168                 return core::frame_producer::empty();
169
170         uint32_t value = 0;
171         if(!try_get_color(params.at(0), value))
172                 return core::frame_producer::empty();
173
174         return spl::make_shared<color_producer>(frame_factory, params.at(0));
175 }
176
177 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
178 {
179         core::pixel_format_desc desc(pixel_format::bgra);
180         desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
181         auto frame = frame_factory->create_frame(tag, desc, core::audio_channel_layout::invalid());
182         
183         *reinterpret_cast<uint32_t*>(frame.image_data(0).begin()) = value;
184
185         return core::draw_frame(std::move(frame));
186 }
187
188 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& str)
189 {
190         uint32_t value = 0;
191         if(!try_get_color(str, value))
192                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid color: " + str));
193         
194         return create_color_frame(tag, frame_factory, value);
195 }
196
197 }}