X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Fcolor%2Fcolor_producer.cpp;h=7c14aaf281f6e2fe66f1ebbc6bcaa955c664c0d4;hb=22ea91cc0ab8cbcee5a728ad00e02ca73e2bad3e;hp=ef3c8e593170a615f3b5a12e70840d10447230cd;hpb=83c5f97e508b267898ddbbdc19f91195df1bf146;p=casparcg diff --git a/core/producer/color/color_producer.cpp b/core/producer/color/color_producer.cpp index ef3c8e593..7c14aaf28 100644 --- a/core/producer/color/color_producer.cpp +++ b/core/producer/color/color_producer.cpp @@ -16,85 +16,108 @@ * You should have received a copy of the GNU General Public License * along with CasparCG. If not, see . * -*/ - +*/ #include "../../stdafx.h" #include "color_producer.h" -#include "../../processor/draw_frame.h" -#include "../../format/video_format.h" +#include "../frame/basic_frame.h" +#include "../frame/frame_factory.h" +#include "../../mixer/write_frame.h" -#include -#pragma intrinsic(__movsd, __stosd) +#include + +#include + +#include namespace caspar { namespace core { -unsigned int get_pixel_color_value(const std::wstring& parameter) -{ - union Color - { - struct Components - { - unsigned char a; - unsigned char r; - unsigned char g; - unsigned char b; - } comp; - - unsigned int value; - }; - - 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")); - - 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; -} - class color_producer : public frame_producer { + safe_ptr frame_; + const std::wstring color_str_; + public: - explicit color_producer(const std::wstring& color) : color_str_(color), color_value_(get_pixel_color_value(color)){} + explicit color_producer(const safe_ptr& frame_factory, const std::wstring& color) + : color_str_(color) + , frame_(create_color_frame(this, frame_factory, color)) + {} + + // frame_producer + + virtual safe_ptr receive(int) { return frame_; } + virtual safe_ptr last_frame() const { return frame_; } + virtual std::wstring print() const { return L"color[" + color_str_ + L"]"; } +}; + +std::wstring get_hex_color(const std::wstring& str) +{ + if(str.at(0) == '#') + return str.length() == 7 ? L"#FF" + str.substr(1) : str; - draw_frame receive() - { - return frame_; - } - - void initialize(const frame_processor_device_ptr& frame_processor) - { - write_frame frame = std::move(frame_processor->create_frame()); - __stosd(reinterpret_cast(frame.pixel_data().begin()), color_value_, frame.pixel_data().size() / sizeof(unsigned long)); - frame_ = std::move(frame); - } + if(boost::iequals(str, L"BLACK")) + return L"#000000FF"; - std::wstring print() const - { - return + L"color_producer. color: " + color_str_; - } - - draw_frame frame_; - unsigned int color_value_; - std::wstring color_str_; -}; + 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"; + + 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) +safe_ptr create_color_producer(const safe_ptr& frame_factory, const std::vector& params) { - if(params.empty() || params[0].at(0) != '#') - return nullptr; - return std::make_shared(params[0]); + 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 make_safe(frame_factory, color2); +} +safe_ptr create_color_frame(void* tag, const safe_ptr& frame_factory, const std::wstring& color) +{ + auto color2 = get_hex_color(color); + if(color2.length() != 9 || color2[0] != '#') + BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color.")); + + auto frame = frame_factory->create_frame(tag, 1, 1, pixel_format::bgra); + + // Read color from hex-string and write to frame pixel. + + auto& value = *reinterpret_cast(frame->image_data().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(narrow(color2)) << msg_info("Invalid color.")); + + frame->commit(); + + return frame; } }} \ No newline at end of file