]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
set svn:eol-style native on .h and .cpp files
[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         draw_frame                              frame_;
47         const std::wstring              color_str_;
48
49 public:
50         explicit color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color) 
51                 : color_str_(color)
52                 , frame_(create_color_frame(this, frame_factory, color))
53         {
54                 CASPAR_LOG(info) << print() << L" Initialized";
55         }
56
57         // frame_producer
58                         
59         draw_frame receive_impl() override
60         {
61                 event_subject_ << monitor::event("color") % color_str_;
62
63                 return frame_;
64         }       
65         
66         std::wstring print() const override
67         {
68                 return L"color[" + color_str_ + L"]";
69         }
70
71         std::wstring name() const override
72         {
73                 return L"color";
74         }
75         
76         boost::property_tree::wptree info() const override
77         {
78                 boost::property_tree::wptree info;
79                 info.add(L"type", L"color");
80                 info.add(L"color", color_str_);
81                 return info;
82         }
83
84         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     
85         {
86                 return event_subject_.subscribe(o);
87         }
88
89         void unsubscribe(const monitor::observable::observer_ptr& o) override           
90         {
91                 return event_subject_.unsubscribe(o);
92         }
93 };
94
95 std::wstring get_hex_color(const std::wstring& str)
96 {
97         if(str.at(0) == '#')
98                 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
99         
100         if(boost::iequals(str, L"EMPTY"))
101                 return L"#00000000";
102
103         if(boost::iequals(str, L"BLACK"))
104                 return L"#FF000000";
105         
106         if(boost::iequals(str, L"WHITE"))
107                 return L"#FFFFFFFF";
108         
109         if(boost::iequals(str, L"RED"))
110                 return L"#FFFF0000";
111         
112         if(boost::iequals(str, L"GREEN"))
113                 return L"#FF00FF00";
114         
115         if(boost::iequals(str, L"BLUE"))
116                 return L"#FF0000FF";    
117         
118         if(boost::iequals(str, L"ORANGE"))
119                 return L"#FFFFA500";    
120         
121         if(boost::iequals(str, L"YELLOW"))
122                 return L"#FFFFFF00";
123         
124         if(boost::iequals(str, L"BROWN"))
125                 return L"#FFA52A2A";
126         
127         if(boost::iequals(str, L"GRAY"))
128                 return L"#FF808080";
129         
130         if(boost::iequals(str, L"TEAL"))
131                 return L"#FF008080";
132         
133         return str;
134 }
135
136 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
137 {
138         if(params.size() < 0)
139                 return core::frame_producer::empty();
140
141         auto color2 = get_hex_color(params[0]);
142         if(color2.length() != 9 || color2[0] != '#')
143                 return core::frame_producer::empty();
144
145         return spl::make_shared<color_producer>(frame_factory, color2);
146 }
147
148 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& color)
149 {
150         auto color2 = get_hex_color(color);
151         if(color2.length() != 9 || color2[0] != '#')
152                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));
153         
154         core::pixel_format_desc desc(pixel_format::bgra);
155         desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
156         auto frame = frame_factory->create_frame(tag, desc);
157                 
158         // Read color from hex-string and write to frame pixel.
159
160         auto& value = *reinterpret_cast<uint32_t*>(frame.image_data(0).begin());
161         std::wstringstream str(color2.substr(1));
162         if(!(str >> std::hex >> value) || !str.eof())
163                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));
164                         
165         return core::draw_frame(std::move(frame));
166 }
167
168 }}