diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs b/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs index 5bd4c601c..a0e4495b6 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs @@ -14,110 +14,80 @@ namespace ImageSharp.PixelFormats.PixelBlenders { [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Src(Vector4 backdrop, Vector4 source, float amount) - { - source.W *= amount; - if (source.W == 0) - { - return Vector4.Zero; - } - + public static Vector4 Src(Vector4 backdrop, Vector4 source, float opacity) + { + source.W *= opacity; return Compose(Vector4.Zero, source, source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Atop(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 Atop(Vector4 backdrop, Vector4 source, float opacity) { - return backdrop; + return Compose(backdrop, Vector4.Zero, source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Over(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 Over(Vector4 backdrop, Vector4 source, float opacity) { - source.W *= amount; - if (source.W == 0) - { - return backdrop; - } - + source.W *= opacity; return Compose(backdrop, source, source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 In(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 In(Vector4 backdrop, Vector4 source, float opacity) { - return Vector4.Zero; + return Compose(Vector4.Zero, Vector4.Zero, source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Out(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 Out(Vector4 backdrop, Vector4 source, float opacity) { - source.W *= amount; - if (source.W == 0) - { - return Vector4.Zero; - } - + source.W *= opacity; return Compose(Vector4.Zero, source, Vector4.Zero); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Dest(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 Dest(Vector4 backdrop, Vector4 source, float opacity) { - return backdrop; + return Compose(backdrop, Vector4.Zero, backdrop); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 DestAtop(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 DestAtop(Vector4 backdrop, Vector4 source, float opacity) { - source.W *= amount; - if (source.W == 0) - { - return Vector4.Zero; - } - + source.W *= opacity; return Compose(Vector4.Zero, source, backdrop); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 DestOver(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 DestOver(Vector4 backdrop, Vector4 source, float opacity) { - source.W *= amount; - if (source.W == 0) - { - return backdrop; - } - + source.W *= opacity; return Compose(backdrop, source, backdrop); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 DestIn(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 DestIn(Vector4 backdrop, Vector4 source, float opacity) { - return Vector4.Zero; + return Compose(Vector4.Zero, Vector4.Zero, backdrop); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 DestOut(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 DestOut(Vector4 backdrop, Vector4 source, float opacity) { - return backdrop; + return Compose(backdrop, Vector4.Zero, Vector4.Zero); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Clear(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 Clear(Vector4 backdrop, Vector4 source, float opacity) { - return Vector4.Zero; + return Compose(Vector4.Zero, Vector4.Zero, Vector4.Zero); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 Xor(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 Xor(Vector4 backdrop, Vector4 source, float opacity) { - source.W *= amount; - if (source.W == 0) - { - return backdrop; - } - + source.W *= opacity; return Compose(backdrop, source, Vector4.Zero); } diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt b/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt index 15b2c46a3..68bd65041 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt +++ b/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt @@ -44,57 +44,37 @@ namespace ImageSharp.PixelFormats.PixelBlenders void GenerateVectorCompositor(string name, string sourceVar, string destVar, string blendVar) { - if(sourceVar == "0") sourceVar= "Vector4.Zero"; - if(destVar == "0") destVar= "Vector4.Zero"; - if(blendVar == "0") blendVar= "Vector4.Zero"; - - if(sourceVar == "s") sourceVar= "source"; - if(destVar == "s") destVar= "source"; - if(blendVar == "s") blendVar= "source"; - - if(sourceVar == "d") sourceVar= "backdrop"; - if(destVar == "d") destVar= "backdrop"; - if(blendVar == "d") blendVar= "backdrop"; #> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vector4 <#=name#>(Vector4 backdrop, Vector4 source, float amount) + public static Vector4 <#=name#>(Vector4 backdrop, Vector4 source, float opacity) { <# - if(sourceVar == "Vector4.Zero") + if(sourceVar != "Vector4.Zero") { #> - return <#=destVar#>; -<# - }else{ -#> - <#=sourceVar#>.W *= amount; - if (<#=sourceVar#>.W == 0) - { - return <#=destVar#>; - } - - return Compose(<#=destVar#>, <#=sourceVar#>, <#=blendVar#>); + <#=sourceVar#>.W *= opacity; <# } #> + return Compose(<#=destVar#>, <#=sourceVar#>, <#=blendVar#>); } <# } - GenerateVectorCompositor("Src", "s", "0", "s"); - GenerateVectorCompositor("Atop", "0", "d", "s"); - GenerateVectorCompositor("Over", "s", "d", "s"); - GenerateVectorCompositor("In", "0", "0", "s"); - GenerateVectorCompositor("Out", "s", "0", "0"); - GenerateVectorCompositor("Dest", "0", "d", "d"); - GenerateVectorCompositor("DestAtop", "s", "0", "d"); - GenerateVectorCompositor("DestOver", "s", "d", "d"); - GenerateVectorCompositor("DestIn", "0", "0", "d"); - GenerateVectorCompositor("DestOut", "0", "d", "0"); - GenerateVectorCompositor("Clear", "0", "0", "0"); - GenerateVectorCompositor("Xor", "s", "d", "0"); + GenerateVectorCompositor("Src", "source", "Vector4.Zero", "source"); + GenerateVectorCompositor("Atop", "Vector4.Zero", "backdrop", "source"); + GenerateVectorCompositor("Over", "source", "backdrop", "source"); + GenerateVectorCompositor("In", "Vector4.Zero", "Vector4.Zero", "source"); + GenerateVectorCompositor("Out", "source", "Vector4.Zero", "Vector4.Zero"); + GenerateVectorCompositor("Dest", "Vector4.Zero", "backdrop", "backdrop"); + GenerateVectorCompositor("DestAtop", "source", "Vector4.Zero", "backdrop"); + GenerateVectorCompositor("DestOver", "source", "backdrop", "backdrop"); + GenerateVectorCompositor("DestIn", "Vector4.Zero", "Vector4.Zero", "backdrop"); + GenerateVectorCompositor("DestOut", "Vector4.Zero", "backdrop", "Vector4.Zero"); + GenerateVectorCompositor("Clear", "Vector4.Zero", "Vector4.Zero", "Vector4.Zero"); + GenerateVectorCompositor("Xor", "source", "backdrop", "Vector4.Zero"); GeneratePixelBlender("Normal"); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.cs b/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.cs index 9e21d3510..bbb6ca4de 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.cs @@ -32,11 +32,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Normal(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, source); } @@ -51,11 +46,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Multiply(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, backdrop * source); } @@ -70,11 +60,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Add(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, Vector4.Min(Vector4.One, backdrop + source)); } @@ -89,11 +74,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Substract(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, Vector4.Max(Vector4.Zero, backdrop - source)); } @@ -108,11 +88,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Screen(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, Vector4.One - ((Vector4.One - backdrop) * (Vector4.One - source))); } @@ -127,11 +102,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Darken(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, Vector4.Min(backdrop, source)); } @@ -146,11 +116,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Lighten(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - return Compose(backdrop, source, Vector4.Max(backdrop, source)); } @@ -165,11 +130,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 Overlay(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - float cr = OverlayValueFunction(backdrop.X, source.X); float cg = OverlayValueFunction(backdrop.Y, source.Y); float cb = OverlayValueFunction(backdrop.Z, source.Z); @@ -188,11 +148,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders public static Vector4 HardLight(Vector4 backdrop, Vector4 source, float opacity) { source.W *= opacity; - if (source.W == 0) - { - return backdrop; - } - float cr = OverlayValueFunction(source.X, backdrop.X); float cg = OverlayValueFunction(source.Y, backdrop.Y); float cb = OverlayValueFunction(source.Z, backdrop.Z); @@ -222,8 +177,6 @@ namespace ImageSharp.PixelFormats.PixelBlenders [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector4 Compose(Vector4 backdrop, Vector4 source, Vector4 xform) { - //DebugGuard.MustBeGreaterThan(source.W, 0, nameof(source.W)); - // calculate weights float xw = backdrop.W * source.W; float bw = backdrop.W - xw; diff --git a/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs b/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs index 23f0569e7..a3df1803d 100644 --- a/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs +++ b/tests/ImageSharp.Tests/Drawing/BlendedShapes.cs @@ -41,7 +41,7 @@ namespace ImageSharp.Tests.Drawing using (var img = provider.GetImage()) { img.Fill(NamedColors.DarkBlue, new Rectangle(0, 40, 100, 20)); - img.Fill(NamedColors.HotPink, new Rectangle(20, 0, 40, 100), new ImageSharp.GraphicsOptions(true) + img.Fill(NamedColors.HotPink, new Rectangle(20, 0, 30, 100), new ImageSharp.GraphicsOptions(true) { BlenderMode = mode }); @@ -52,5 +52,31 @@ namespace ImageSharp.Tests.Drawing img.DebugSave(provider, new { mode }); } } + + + [Theory] + [WithBlankImages(nameof(modes), 100, 100, PixelTypes.Rgba32)] + public void DrawBlendedValues_transparent50Percent(TestImageProvider provider, PixelBlenderMode mode) + where TPixel : struct, IPixel + { + using (var img = provider.GetImage()) + { + img.Fill(NamedColors.DarkBlue, new Rectangle(0, 40, 100, 20)); + img.Fill(NamedColors.HotPink, new Rectangle(20, 0, 30, 100), new ImageSharp.GraphicsOptions(true) + { + BlenderMode = mode + }); + var c = NamedColors.Red.ToVector4(); + c.W *= 0.5f; + TPixel pixel = default(TPixel); + pixel.PackFromVector4(c); + + img.Fill(pixel, new Rectangle(40, 0, 20, 100), new ImageSharp.GraphicsOptions(true) + { + BlenderMode = mode + }); + img.DebugSave(provider, new { mode }); + } + } } }