From: ronag Date: Wed, 22 Jun 2011 08:02:27 +0000 (+0000) Subject: 2.0. mixer: Added screen blending mode. X-Git-Tag: 2.0.1~379 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=b96f7196dbf9693ba5ee1c530274946ce6da0fe8;p=casparcg 2.0. mixer: Added screen blending mode. git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches/2.0.0.2@937 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d --- diff --git a/core/core.vcxproj b/core/core.vcxproj index e70447e5a..b2ab9d6b6 100644 --- a/core/core.vcxproj +++ b/core/core.vcxproj @@ -247,6 +247,7 @@ + diff --git a/core/core.vcxproj.filters b/core/core.vcxproj.filters index 17ccc81b9..3530b1272 100644 --- a/core/core.vcxproj.filters +++ b/core/core.vcxproj.filters @@ -115,6 +115,9 @@ source + + source\mixer\image + diff --git a/core/mixer/image/image_kernel.cpp b/core/mixer/image/image_kernel.cpp index 7e6704e44..0e8412494 100644 --- a/core/mixer/image/image_kernel.cpp +++ b/core/mixer/image/image_kernel.cpp @@ -160,7 +160,6 @@ public: { GL(glEnable(GL_POLYGON_STIPPLE)); GL(glEnable(GL_BLEND)); - GL(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE)); if(shaders_.empty()) { @@ -321,6 +320,15 @@ void image_kernel::draw(size_t width, size_t height, const core::pixel_format_de if(transform.get_opacity() < 0.001) return; + switch(transform.get_blend_mode()) + { + case image_transform::screen: + GL(glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE)); + break; + default: + GL(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE)); + } + GL(glEnable(GL_TEXTURE_2D)); GL(glDisable(GL_DEPTH_TEST)); diff --git a/core/mixer/image/photoshop_math.h b/core/mixer/image/photoshop_math.h new file mode 100644 index 000000000..bfa8fe1d6 --- /dev/null +++ b/core/mixer/image/photoshop_math.h @@ -0,0 +1,251 @@ +#pragma once + +static std::string get_math_glsl() +{ + static std::string glsl = + "/* " + "** Photoshop & misc math " + "** Blending modes, RGB/HSL/Contrast/Desaturate, levels control " + "** " + "** Romain Dura | Romz " + "** Blog: http://blog.mouaif.org " + "** Post: http://blog.mouaif.org/?p=94 " + "*/ " + " " + " " + "/* " + "** Desaturation " + "*/ " + " " + "vec4 Desaturate(vec3 color, float Desaturation) " + "{ " + " vec3 grayXfer = vec3(0.3, 0.59, 0.11); " + " vec3 gray = vec3(dot(grayXfer, color)); " + " return vec4(mix(color, gray, Desaturation), 1.0); " + "} " + " " + " " + "/* " + "** Hue, saturation, luminance " + "*/ " + " " + "vec3 RGBToHSL(vec3 color) " + "{ " + " vec3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part) " + " " + " float fmin = min(min(color.r, color.g), color.b); //Min. value of RGB " + " float fmax = max(max(color.r, color.g), color.b); //Max. value of RGB " + " float delta = fmax - fmin; //Delta RGB value " + " " + " hsl.z = (fmax + fmin) / 2.0; // Luminance " + " " + " if (delta == 0.0) //This is a gray, no chroma... " + " { " + " hsl.x = 0.0; // Hue " + " hsl.y = 0.0; // Saturation " + " } " + " else //Chromatic data... " + " { " + " if (hsl.z < 0.5) " + " hsl.y = delta / (fmax + fmin); // Saturation " + " else " + " hsl.y = delta / (2.0 - fmax - fmin); // Saturation " + " " + " float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta; " + " float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta; " + " float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta; " + " " + " if (color.r == fmax ) " + " hsl.x = deltaB - deltaG; // Hue " + " else if (color.g == fmax) " + " hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue " + " else if (color.b == fmax) " + " hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue " + " " + " if (hsl.x < 0.0) " + " hsl.x += 1.0; // Hue " + " else if (hsl.x > 1.0) " + " hsl.x -= 1.0; // Hue " + " } " + " " + " return hsl; " + "} " + " " + "float HueToRGB(float f1, float f2, float hue) " + "{ " + " if (hue < 0.0) " + " hue += 1.0; " + " else if (hue > 1.0) " + " hue -= 1.0; " + " float res; " + " if ((6.0 * hue) < 1.0) " + " res = f1 + (f2 - f1) * 6.0 * hue; " + " else if ((2.0 * hue) < 1.0) " + " res = f2; " + " else if ((3.0 * hue) < 2.0) " + " res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; " + " else " + " res = f1; " + " return res; " + "} " + " " + "vec3 HSLToRGB(vec3 hsl) " + "{ " + " vec3 rgb; " + " " + " if (hsl.y == 0.0) " + " rgb = vec3(hsl.z); // Luminance " + " else " + " { " + " float f2; " + " " + " if (hsl.z < 0.5) " + " f2 = hsl.z * (1.0 + hsl.y); " + " else " + " f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); " + " " + " float f1 = 2.0 * hsl.z - f2; " + " " + " rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); " + " rgb.g = HueToRGB(f1, f2, hsl.x); " + " rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0)); " + " } " + " " + " return rgb; " + "} " + " " + " " + "/* " + "** Contrast, saturation, brightness " + "** Code of this function is from TGM's shader pack " + "** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057 " + "*/ " + " " + "// For all settings: 1.0 = 100% 0.5=50% 1.5 = 150% " + "vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) " + "{ " + " // Increase or decrease theese values to adjust r, g and b color channels seperately " + " const float AvgLumR = 0.5; " + " const float AvgLumG = 0.5; " + " const float AvgLumB = 0.5; " + " " + " const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721); " + " " + " vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB); " + " vec3 brtColor = color * brt; " + " vec3 intensity = vec3(dot(brtColor, LumCoeff)); " + " vec3 satColor = mix(intensity, brtColor, sat); " + " vec3 conColor = mix(AvgLumin, satColor, con); " + " return conColor; " + "} " + " " + " " + "/* " + "** Float blending modes " + "** Adapted from here: http://www.nathanm.com/photoshop-blending-math/ " + "** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results " + "*/ " + " " + "#define BlendLinearDodgef BlendAddf " + "#define BlendLinearBurnf BlendSubstractf " + "#define BlendAddf(base, blend) min(base + blend, 1.0) " + "#define BlendSubstractf(base, blend) max(base + blend - 1.0, 0.0) " + "#define BlendLightenf(base, blend) max(blend, base) " + "#define BlendDarkenf(base, blend) min(blend, base) " + "#define BlendLinearLightf(base, blend) (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5)))) " + "#define BlendScreenf(base, blend) (1.0 - ((1.0 - base) * (1.0 - blend))) " + "#define BlendOverlayf(base, blend) (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend))) " + "#define BlendSoftLightf(base, blend) ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend))) " + "#define BlendColorDodgef(base, blend) ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0)) " + "#define BlendColorBurnf(base, blend) ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0)) " + "#define BlendVividLightf(base, blend) ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5)))) " + "#define BlendPinLightf(base, blend) ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5)))) " + "#define BlendHardMixf(base, blend) ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0) " + "#define BlendReflectf(base, blend) ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0)) " + " " + " " + "/* " + "** Vector3 blending modes " + "*/ " + " " + "// Component wise blending " + "#define Blend(base, blend, funcf) vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b)) " + " " + "#define BlendNormal(base, blend) (blend) " + "#define BlendLighten BlendLightenf " + "#define BlendDarken BlendDarkenf " + "#define BlendMultiply(base, blend) (base * blend) " + "#define BlendAverage(base, blend) ((base + blend) / 2.0) " + "#define BlendAdd(base, blend) min(base + blend, vec3(1.0)) " + "#define BlendSubstract(base, blend) max(base + blend - vec3(1.0), vec3(0.0)) " + "#define BlendDifference(base, blend) abs(base - blend) " + "#define BlendNegation(base, blend) (vec3(1.0) - abs(vec3(1.0) - base - blend)) " + "#define BlendExclusion(base, blend) (base + blend - 2.0 * base * blend) " + "#define BlendScreen(base, blend) Blend(base, blend, BlendScreenf) " + "#define BlendOverlay(base, blend) Blend(base, blend, BlendOverlayf) " + "#define BlendSoftLight(base, blend) Blend(base, blend, BlendSoftLightf) " + "#define BlendHardLight(base, blend) BlendOverlay(blend, base) " + "#define BlendColorDodge(base, blend) Blend(base, blend, BlendColorDodgef) " + "#define BlendColorBurn(base, blend) Blend(base, blend, BlendColorBurnf) " + "#define BlendLinearDodge BlendAdd " + "#define BlendLinearBurn BlendSubstract " + "// Linear Light is another contrast-increasing mode " + "// If the blend color is darker than midgray, Linear Light darkens the image by decreasing the brightness. If the blend color is lighter than midgray, the result is a brighter image due to increased brightness." + "#define BlendLinearLight(base, blend) Blend(base, blend, BlendLinearLightf) " + "#define BlendVividLight(base, blend) Blend(base, blend, BlendVividLightf) " + "#define BlendPinLight(base, blend) Blend(base, blend, BlendPinLightf) " + "#define BlendHardMix(base, blend) Blend(base, blend, BlendHardMixf) " + "#define BlendReflect(base, blend) Blend(base, blend, BlendReflectf) " + "#define BlendGlow(base, blend) BlendReflect(blend, base) " + "#define BlendPhoenix(base, blend) (min(base, blend) - max(base, blend) + vec3(1.0)) " + "#define BlendOpacity(base, blend, F, O) (F(base, blend) * O + blend * (1.0 - O)) " + " " + " " + "// Hue Blend mode creates the result color by combining the luminance and saturation of the base color with the hue of the blend color. " + "vec3 BlendHue(vec3 base, vec3 blend) " + "{ " + " vec3 baseHSL = RGBToHSL(base); " + " return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b)); " + "} " + " " + "// Saturation Blend mode creates the result color by combining the luminance and hue of the base color with the saturation of the blend color. " + "vec3 BlendSaturation(vec3 base, vec3 blend) " + "{ " + " vec3 baseHSL = RGBToHSL(base); " + " return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b)); " + "} " + " " + "// Color Mode keeps the brightness of the base color and applies both the hue and saturation of the blend color. " + "vec3 BlendColor(vec3 base, vec3 blend) " + "{ " + " vec3 blendHSL = RGBToHSL(blend); " + " return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b)); " + "} " + " " + "// Luminosity Blend mode creates the result color by combining the hue and saturation of the base color with the luminance of the blend color. " + "vec3 BlendLuminosity(vec3 base, vec3 blend) " + "{ " + " vec3 baseHSL = RGBToHSL(base); " + " return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b)); " + "} " + " " + " " + "/* " + "** Gamma correction " + "** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/ " + "*/ " + " " + "#define GammaCorrection(color, gamma) pow(color, 1.0 / gamma) " + " " + "/* " + "** Levels control (input (+gamma), output) " + "** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/ " + "*/ " + " " + "#define LevelsControlInputRange(color, minInput, maxInput) min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0)) " + "#define LevelsControlInput(color, minInput, gamma, maxInput) GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma) " + "#define LevelsControlOutputRange(color, minOutput, maxOutput) mix(vec3(minOutput), vec3(maxOutput), color) " + "#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput) LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput) "; + + return glsl; +} \ No newline at end of file diff --git a/core/producer/frame/image_transform.cpp b/core/producer/frame/image_transform.cpp index fba4e5c28..d916ca101 100644 --- a/core/producer/frame/image_transform.cpp +++ b/core/producer/frame/image_transform.cpp @@ -31,6 +31,7 @@ image_transform::image_transform() , mode_(video_mode::invalid) , is_key_(false) , deinterlace_(false) + , blend_mode_(image_transform::normal) { std::fill(fill_translation_.begin(), fill_translation_.end(), 0.0); std::fill(fill_scale_.begin(), fill_scale_.end(), 1.0); @@ -122,6 +123,16 @@ bool image_transform::get_deinterlace() const return deinterlace_; } +void image_transform::set_blend_mode(image_transform::blend_mode value) +{ + blend_mode_ = value; +} + +image_transform::blend_mode image_transform::get_blend_mode() const +{ + return blend_mode_; +} + image_transform& image_transform::operator*=(const image_transform &other) { opacity_ *= other.opacity_; @@ -129,6 +140,7 @@ image_transform& image_transform::operator*=(const image_transform &other) if(other.mode_ != video_mode::invalid) mode_ = other.mode_; + blend_mode_ = other.blend_mode_; gain_ *= other.gain_; deinterlace_ |= other.deinterlace_; is_key_ |= other.is_key_; @@ -162,6 +174,7 @@ image_transform tween(double time, const image_transform& source, const image_tr image_transform result; result.set_mode (dest.get_mode() != video_mode::invalid ? dest.get_mode() : source.get_mode()); + result.set_blend_mode (dest.get_blend_mode()); result.set_is_key (source.get_is_key() | dest.get_is_key()); result.set_deinterlace (source.get_deinterlace() | dest.get_deinterlace()); result.set_gain (do_tween(time, source.get_gain(), dest.get_gain(), duration, tweener)); diff --git a/core/producer/frame/image_transform.h b/core/producer/frame/image_transform.h index 6a48bc6e3..194e7b4f7 100644 --- a/core/producer/frame/image_transform.h +++ b/core/producer/frame/image_transform.h @@ -32,6 +32,13 @@ struct pixel_format_desc; class image_transform { public: + + enum blend_mode + { + normal, + screen + }; + image_transform(); void set_opacity(double value); @@ -64,6 +71,9 @@ public: void set_deinterlace(bool value); bool get_deinterlace() const; + void set_blend_mode(blend_mode value); + blend_mode get_blend_mode() const; + private: double opacity_; double gain_; @@ -74,6 +84,7 @@ private: video_mode::type mode_; bool is_key_; bool deinterlace_; + blend_mode blend_mode_; }; image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener_t& tweener); diff --git a/protocol/amcp/AMCPCommandsImpl.cpp b/protocol/amcp/AMCPCommandsImpl.cpp index 72be70888..5b249b303 100644 --- a/protocol/amcp/AMCPCommandsImpl.cpp +++ b/protocol/amcp/AMCPCommandsImpl.cpp @@ -328,6 +328,26 @@ bool MixerCommand::DoExecute() } } } + else if(_parameters[1] == L"BLEND") + { + auto blend_str = _parameters.at(2); + auto transform = [=](image_transform transform) -> image_transform + { + if(blend_str == L"NORMAL") + transform.set_blend_mode(image_transform::normal); + else if(blend_str == L"SCREEN") + transform.set_blend_mode(image_transform::screen); + + return transform; + }; + + int layer = GetLayerIndex(std::numeric_limits::min()); + if(layer != std::numeric_limits::min()) + GetChannel()->mixer()->apply_image_transform(GetLayerIndex(), transform, 0); + else + GetChannel()->mixer()->apply_image_transform(transform, 0); + + } else if(_parameters[1] == L"RESET") { int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;