X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Fcolor%2Fcolor_producer.cpp;h=2879623ef3bb0bc27454a8dd0905f179cf2a11c7;hb=f31deb820b9c31a81989e5a5325dc09c73c270a9;hp=d6a48a9ebe4324d02d4e49a0f3b075d9f5449119;hpb=5330bed3a2561cb356dbe530c5deb71ade0802e2;p=casparcg diff --git a/core/producer/color/color_producer.cpp b/core/producer/color/color_producer.cpp index d6a48a9eb..2879623ef 100644 --- a/core/producer/color/color_producer.cpp +++ b/core/producer/color/color_producer.cpp @@ -1,104 +1,154 @@ /* -* copyright (c) 2010 Sveriges Television AB +* Copyright (c) 2011 Sveriges Television AB * -* This file is part of CasparCG. +* This file is part of CasparCG (www.casparcg.com). * -* CasparCG is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. * -* CasparCG is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. - -* You should have received a copy of the GNU General Public License -* along with CasparCG. If not, see . +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . * +* Author: Robert Nagy, ronag89@gmail.com */ - + #include "../../stdafx.h" #include "color_producer.h" -#include "../../frame/frame_format.h" +#include +#include +#include +#include +#include -#include -#pragma intrinsic(__movsd, __stosd) +#include -namespace caspar { namespace core { +#include + +#include +namespace caspar { namespace core { + class color_producer : public frame_producer { + spl::shared_ptr frame_; + const std::wstring color_str_; + public: - explicit color_producer(unsigned int color_value, const frame_format_desc& format_desc) - : color_value_(color_value), format_desc_(format_desc){} + explicit color_producer(const spl::shared_ptr& frame_factory, const std::wstring& color) + : color_str_(color) + , frame_(create_color_frame(this, frame_factory, color)) + {} + + // frame_producer + + virtual spl::shared_ptr receive(int) override + { + return frame_; + } + + virtual std::wstring print() const override + { + return L"color[" + color_str_ + L"]"; + } - ~color_producer() + virtual std::wstring name() const override { - if(factory_) - factory_->release_frames(this); + return L"color"; } - gpu_frame_ptr get_frame() - { + virtual spl::shared_ptr last_frame() const override + { return frame_; } - const frame_format_desc& get_frame_format_desc() const { return format_desc_; } - - void initialize(const frame_factory_ptr& factory) + boost::property_tree::wptree info() const override { - factory_ = factory; - frame_ = factory->create_frame(format_desc_, this); - __stosd(reinterpret_cast(frame_->data()), color_value_, frame_->size() / sizeof(unsigned long)); + boost::property_tree::wptree info; + info.add(L"type", L"color"); + info.add(L"color", color_str_); + return info; } - - frame_factory_ptr factory_; - frame_format_desc format_desc_; - gpu_frame_ptr frame_; - unsigned int color_value_; }; -union Color +std::wstring get_hex_color(const std::wstring& str) { - struct Components - { - unsigned char a; - unsigned char r; - unsigned char g; - unsigned char b; - } comp; - - unsigned int value; -}; + if(str.at(0) == '#') + return str.length() == 7 ? L"#FF" + str.substr(1) : str; + + if(boost::iequals(str, L"EMPTY")) + return L"#00000000"; -unsigned int get_pixel_color_value(const std::wstring& parameter) -{ - std::wstring color_code; - if(parameter.length() != 9 || parameter[0] != '#') - BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("parameter") << arg_value_info(common::narrow(parameter)) << msg_info("Invalid color code")); + if(boost::iequals(str, L"BLACK")) + return L"#FF000000"; + + if(boost::iequals(str, L"WHITE")) + return L"#FFFFFFFF"; + + if(boost::iequals(str, L"RED")) + return L"#FFFF0000"; + + if(boost::iequals(str, L"GREEN")) + return L"#FF00FF00"; + + if(boost::iequals(str, L"BLUE")) + return L"#FF0000FF"; + + if(boost::iequals(str, L"ORANGE")) + return L"#FFFFA500"; + + if(boost::iequals(str, L"YELLOW")) + return L"#FFFFFF00"; - color_code = parameter.substr(1); - - Color color; - color.value = _tcstoul(color_code.c_str(), 0, 16); - unsigned char temp = color.comp.a; - color.comp.a = color.comp.b; - color.comp.b = temp; - temp = color.comp.r; - color.comp.r = color.comp.g; - color.comp.g = temp; - - return color.value; + if(boost::iequals(str, L"BROWN")) + return L"#FFA52A2A"; + + if(boost::iequals(str, L"GRAY")) + return L"#FF808080"; + + if(boost::iequals(str, L"TEAL")) + return L"#FF008080"; + + return str; } -frame_producer_ptr create_color_producer(const std::vector& params, const frame_format_desc& format_desc) +spl::shared_ptr create_color_producer(const spl::shared_ptr& frame_factory, const std::vector& params) +{ + if(params.size() < 0) + return core::frame_producer::empty(); + + auto color2 = get_hex_color(params[0]); + if(color2.length() != 9 || color2[0] != '#') + return core::frame_producer::empty(); + + return core::wrap_producer(spl::make_shared(frame_factory, color2)); +} +spl::shared_ptr create_color_frame(void* tag, const spl::shared_ptr& frame_factory, const std::wstring& color) { - if(params.empty() || params[0].at(0) != '#') - return nullptr; - return std::make_shared(get_pixel_color_value(params[0]), format_desc); + auto color2 = get_hex_color(color); + if(color2.length() != 9 || color2[0] != '#') + BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color.")); + + core::pixel_format_desc desc(pixel_format::bgra); + desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4)); + auto frame = frame_factory->create_frame(tag, desc); + + // Read color from hex-string and write to frame pixel. + + auto& value = *reinterpret_cast(frame->image_data(0).begin()); + std::wstringstream str(color2.substr(1)); + if(!(str >> std::hex >> value) || !str.eof()) + BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color.")); + + return spl::make_shared(frame); } }} \ No newline at end of file