mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
36 changed files with 6440 additions and 1235 deletions
File diff suppressed because it is too large
File diff suppressed because it is too large
@ -1,112 +1,188 @@ |
|||||
<# |
<# |
||||
// Copyright (c) Six Labors and contributors. |
// Copyright (c) Six Labors and contributors. |
||||
// Licensed under the Apache License, Version 2.0. |
// Licensed under the Apache License, Version 2.0. |
||||
#> |
#> |
||||
<#@ template debug="false" hostspecific="false" language="C#" #> |
<#@ template debug="false" hostspecific="false" language="C#" #> |
||||
<#@ assembly name="System.Core" #> |
<#@ assembly name="System.Core" #> |
||||
<#@ import namespace="System.Linq" #> |
<#@ import namespace="System.Linq" #> |
||||
<#@ import namespace="System.Text" #> |
<#@ import namespace="System.Text" #> |
||||
<#@ import namespace="System.Collections.Generic" #> |
<#@ import namespace="System.Collections.Generic" #> |
||||
<#@ output extension=".cs" #> |
<#@ output extension=".cs" #> |
||||
// Copyright (c) Six Labors and contributors. |
// Copyright (c) Six Labors and contributors. |
||||
// Licensed under the Apache License, Version 2.0. |
// Licensed under the Apache License, Version 2.0. |
||||
|
|
||||
// <auto-generated /> |
// <auto-generated /> |
||||
|
|
||||
using System; |
using System; |
||||
using System.Numerics; |
using System.Numerics; |
||||
using System.Runtime.CompilerServices; |
using System.Runtime.CompilerServices; |
||||
|
|
||||
namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders |
namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders |
||||
{ |
{ |
||||
internal static partial class PorterDuffFunctions |
internal static partial class PorterDuffFunctions |
||||
{ |
{ |
||||
<# |
|
||||
|
<# void GeneratePixelBlenders(string blender) { #> |
||||
void GeneratePixelBlender (string blender) |
|
||||
{ |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
#> |
public static Vector4 <#=blender#>Src(Vector4 backdrop, Vector4 source, float opacity) |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
{ |
||||
public static TPixel <#=blender#><TPixel>(TPixel backdrop, TPixel source, float amount) |
opacity = opacity.Clamp(0, 1); |
||||
where TPixel : struct, IPixel<TPixel> |
source.W *= opacity; |
||||
{ |
|
||||
TPixel dest = default; |
return source; |
||||
dest.PackFromVector4(<#=blender#>(backdrop.ToVector4(), source.ToVector4(), amount)); |
} |
||||
return dest; |
|
||||
} |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static Vector4 <#=blender#>SrcAtop(Vector4 backdrop, Vector4 source, float opacity) |
||||
<# |
{ |
||||
} |
opacity = opacity.Clamp(0, 1); |
||||
|
source.W *= opacity; |
||||
void GenerateVectorCompositor(string name, string sourceVar, string destVar, string blendVar) |
|
||||
{ |
return Atop(backdrop, source, <#=blender#>(backdrop, source)); |
||||
int a_s = sourceVar == "Vector4.Zero" ? 0 : 1; |
} |
||||
int a_b = destVar == "Vector4.Zero" ? 0 : 1; |
|
||||
int a_x = blendVar == "Vector4.Zero" ? 0 : 1; |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
#> |
public static Vector4 <#=blender#>SrcOver(Vector4 backdrop, Vector4 source, float opacity) |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
{ |
||||
public static Vector4 <#=name#>(Vector4 backdrop, Vector4 source, float opacity) |
opacity = opacity.Clamp(0, 1); |
||||
{ |
source.W *= opacity; |
||||
opacity = opacity.Clamp(0, 1); |
|
||||
<# if(sourceVar != "Vector4.Zero" ) { #> |
return Over(backdrop, source, <#=blender#>(backdrop, source)); |
||||
source.W *= opacity; |
} |
||||
<# } #> |
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
// calculate weights |
public static Vector4 <#=blender#>SrcIn(Vector4 backdrop, Vector4 source, float opacity) |
||||
float xw = backdrop.W * source.W; |
{ |
||||
float bw = backdrop.W - xw; |
opacity = opacity.Clamp(0, 1); |
||||
float sw = source.W - xw; |
source.W *= opacity; |
||||
|
|
||||
// calculate final alpha |
return In(backdrop, source, <#=blender#>(backdrop, source)); |
||||
float fw = (sw * <#=a_s#>) + (bw * <#=a_b#>) + (xw * <#=a_x#>); |
} |
||||
|
|
||||
// calculate final value |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
Vector4 xform = ((<#=blendVar#> * xw) + (<#=destVar#> * bw) + (<#=sourceVar#> * sw)) / MathF.Max(fw, Constants.Epsilon); |
public static Vector4 <#=blender#>SrcOut(Vector4 backdrop, Vector4 source, float opacity) |
||||
xform.W = fw; |
{ |
||||
|
opacity = opacity.Clamp(0, 1); |
||||
return Vector4.Lerp(backdrop, xform, opacity); |
source.W *= opacity; |
||||
} |
|
||||
|
return Out(backdrop, source); |
||||
<# |
} |
||||
} |
|
||||
GenerateVectorCompositor("Src", "source", "Vector4.Zero", "source"); |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
GenerateVectorCompositor("Atop", "Vector4.Zero", "backdrop", "source"); |
public static Vector4 <#=blender#>Dest(Vector4 backdrop, Vector4 source, float opacity) |
||||
GenerateVectorCompositor("Over", "source", "backdrop", "source"); |
{ |
||||
GenerateVectorCompositor("In", "Vector4.Zero", "Vector4.Zero", "source"); |
return backdrop; |
||||
GenerateVectorCompositor("Out", "source", "Vector4.Zero", "Vector4.Zero"); |
} |
||||
GenerateVectorCompositor("Dest", "Vector4.Zero", "backdrop", "backdrop"); |
|
||||
GenerateVectorCompositor("DestAtop", "source", "Vector4.Zero", "backdrop"); |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
GenerateVectorCompositor("DestOver", "source", "backdrop", "backdrop"); |
public static Vector4 <#=blender#>DestAtop(Vector4 backdrop, Vector4 source, float opacity) |
||||
GenerateVectorCompositor("DestIn", "Vector4.Zero", "Vector4.Zero", "backdrop"); |
{ |
||||
GenerateVectorCompositor("DestOut", "Vector4.Zero", "backdrop", "Vector4.Zero"); |
opacity = opacity.Clamp(0, 1); |
||||
GenerateVectorCompositor("Clear", "Vector4.Zero", "Vector4.Zero", "Vector4.Zero"); |
source.W *= opacity; |
||||
GenerateVectorCompositor("Xor", "source", "backdrop", "Vector4.Zero"); |
|
||||
|
return Atop(source, backdrop, <#=blender#>(source, backdrop)); |
||||
|
} |
||||
GeneratePixelBlender("Normal"); |
|
||||
GeneratePixelBlender("Multiply"); |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
GeneratePixelBlender("Add"); |
public static Vector4 <#=blender#>DestOver(Vector4 backdrop, Vector4 source, float opacity) |
||||
GeneratePixelBlender("Subtract"); |
{ |
||||
GeneratePixelBlender("Screen"); |
opacity = opacity.Clamp(0, 1); |
||||
GeneratePixelBlender("Darken"); |
source.W *= opacity; |
||||
GeneratePixelBlender("Lighten"); |
|
||||
GeneratePixelBlender("Overlay"); |
return Over(source, backdrop, <#=blender#>(source, backdrop)); |
||||
GeneratePixelBlender("HardLight"); |
} |
||||
|
|
||||
GeneratePixelBlender("Src"); |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
GeneratePixelBlender("Atop"); |
public static Vector4 <#=blender#>DestIn(Vector4 backdrop, Vector4 source, float opacity) |
||||
GeneratePixelBlender("Over"); |
{ |
||||
GeneratePixelBlender("In"); |
opacity = opacity.Clamp(0, 1); |
||||
GeneratePixelBlender("Out"); |
source.W *= opacity; |
||||
GeneratePixelBlender("Dest"); |
|
||||
GeneratePixelBlender("DestAtop"); |
return In(source, backdrop, <#=blender#>(source, backdrop)); |
||||
GeneratePixelBlender("DestOver"); |
} |
||||
GeneratePixelBlender("DestIn"); |
|
||||
GeneratePixelBlender("DestOut"); |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
GeneratePixelBlender("Clear"); |
public static Vector4 <#=blender#>DestOut(Vector4 backdrop, Vector4 source, float opacity) |
||||
GeneratePixelBlender("Xor"); |
{ |
||||
|
opacity = opacity.Clamp(0, 1); |
||||
|
source.W *= opacity; |
||||
#> |
|
||||
} |
return Out(source, backdrop); |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static Vector4 <#=blender#>Xor(Vector4 backdrop, Vector4 source, float opacity) |
||||
|
{ |
||||
|
opacity = opacity.Clamp(0, 1); |
||||
|
source.W *= opacity; |
||||
|
|
||||
|
return Xor(backdrop, source); |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static Vector4 <#=blender#>Clear(Vector4 backdrop, Vector4 source, float opacity) |
||||
|
{ |
||||
|
opacity = opacity.Clamp(0, 1); |
||||
|
source.W *= opacity; |
||||
|
|
||||
|
return Clear(backdrop, source); |
||||
|
} |
||||
|
<# } #> |
||||
|
|
||||
|
|
||||
|
<# void GenerateGenericPixelBlender(string blender, string composer) { #> |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel <#=blender#><#=composer#><TPixel>(TPixel backdrop, TPixel source, float opacity) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
TPixel dest = default; |
||||
|
dest.PackFromVector4(<#=blender#><#=composer#>(backdrop.ToVector4(),source.ToVector4(),opacity)); |
||||
|
return dest; |
||||
|
} |
||||
|
|
||||
|
<# } #> |
||||
|
|
||||
|
<# |
||||
|
|
||||
|
string[] composers = new []{ |
||||
|
"Src", |
||||
|
"SrcAtop", |
||||
|
"SrcOver", |
||||
|
"SrcIn", |
||||
|
"SrcOut", |
||||
|
"Dest", |
||||
|
"DestAtop", |
||||
|
"DestOver", |
||||
|
"DestIn", |
||||
|
"DestOut", |
||||
|
"Clear", |
||||
|
"Xor", |
||||
|
}; |
||||
|
|
||||
|
string[] blenders = new []{ |
||||
|
"Normal", |
||||
|
"Multiply", |
||||
|
"Add", |
||||
|
"Subtract", |
||||
|
"Screen", |
||||
|
"Darken", |
||||
|
"Lighten", |
||||
|
"Overlay", |
||||
|
"HardLight" |
||||
|
}; |
||||
|
|
||||
|
foreach(var blender in blenders) |
||||
|
{ |
||||
|
GeneratePixelBlenders(blender); |
||||
|
|
||||
|
foreach(var composer in composers) |
||||
|
{ |
||||
|
GenerateGenericPixelBlender(blender,composer); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
#> |
||||
|
} |
||||
} |
} |
||||
@ -1,194 +1,257 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
// Copyright (c) Six Labors and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
using System; |
using System; |
||||
using System.Numerics; |
using System.Numerics; |
||||
using System.Runtime.CompilerServices; |
using System.Runtime.CompilerServices; |
||||
|
|
||||
namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders |
namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Collection of Porter Duff alpha blending functions applying an the 'Over' composition model.
|
/// Collection of Porter Duff alpha blending functions applying an the 'Over' composition model.
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <remarks>
|
/// <remarks>
|
||||
/// These functions are designed to be a general solution for all color cases,
|
/// These functions are designed to be a general solution for all color cases,
|
||||
/// that is, they take in account the alpha value of both the backdrop
|
/// that is, they take in account the alpha value of both the backdrop
|
||||
/// and source, and there's no need to alpha-premultiply neither the backdrop
|
/// and source, and there's no need to alpha-premultiply neither the backdrop
|
||||
/// nor the source.
|
/// nor the source.
|
||||
/// Note there are faster functions for when the backdrop color is known
|
/// Note there are faster functions for when the backdrop color is known
|
||||
/// to be opaque
|
/// to be opaque
|
||||
/// </remarks>
|
/// </remarks>
|
||||
internal static partial class PorterDuffFunctions |
internal static partial class PorterDuffFunctions |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Source over backdrop
|
/// Source over backdrop
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <param name="backdrop">Backdrop color</param>
|
/// <param name="backdrop">Backdrop color</param>
|
||||
/// <param name="source">Source color</param>
|
/// <param name="source">Source color</param>
|
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
/// <returns>Output color</returns>
|
||||
/// <returns>Output color</returns>
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
public static Vector4 Normal(Vector4 backdrop, Vector4 source) |
||||
public static Vector4 Normal(Vector4 backdrop, Vector4 source, float opacity) |
{ |
||||
{ |
return source; |
||||
source.W *= opacity; |
} |
||||
return Compose(backdrop, source, source); |
|
||||
} |
/// <summary>
|
||||
|
/// Source multiplied by backdrop
|
||||
/// <summary>
|
/// </summary>
|
||||
/// Source multiplied by backdrop
|
/// <param name="backdrop">Backdrop color</param>
|
||||
/// </summary>
|
/// <param name="source">Source color</param>
|
||||
/// <param name="backdrop">Backdrop color</param>
|
/// <returns>Output color</returns>
|
||||
/// <param name="source">Source color</param>
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
public static Vector4 Multiply(Vector4 backdrop, Vector4 source) |
||||
/// <returns>Output color</returns>
|
{ |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
return backdrop * source; |
||||
public static Vector4 Multiply(Vector4 backdrop, Vector4 source, float opacity) |
} |
||||
{ |
|
||||
source.W *= opacity; |
/// <summary>
|
||||
return Compose(backdrop, source, backdrop * source); |
/// Source added to backdrop
|
||||
} |
/// </summary>
|
||||
|
/// <param name="backdrop">Backdrop color</param>
|
||||
/// <summary>
|
/// <param name="source">Source color</param>
|
||||
/// Source added to backdrop
|
/// <returns>Output color</returns>
|
||||
/// </summary>
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
/// <param name="backdrop">Backdrop color</param>
|
public static Vector4 Add(Vector4 backdrop, Vector4 source) |
||||
/// <param name="source">Source color</param>
|
{ |
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
return Vector4.Min(Vector4.One, backdrop + source); |
||||
/// <returns>Output color</returns>
|
} |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static Vector4 Add(Vector4 backdrop, Vector4 source, float opacity) |
/// <summary>
|
||||
{ |
/// Source subtracted from backdrop
|
||||
source.W *= opacity; |
/// </summary>
|
||||
return Compose(backdrop, source, Vector4.Min(Vector4.One, backdrop + source)); |
/// <param name="backdrop">Backdrop color</param>
|
||||
} |
/// <param name="source">Source color</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
/// <summary>
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
/// Source subtracted from backdrop
|
public static Vector4 Subtract(Vector4 backdrop, Vector4 source) |
||||
/// </summary>
|
{ |
||||
/// <param name="backdrop">Backdrop color</param>
|
return Vector4.Max(Vector4.Zero, backdrop - source); |
||||
/// <param name="source">Source color</param>
|
} |
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
|
||||
/// <returns>Output color</returns>
|
/// <summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
/// Complement of source multiplied by the complement of backdrop
|
||||
public static Vector4 Subtract(Vector4 backdrop, Vector4 source, float opacity) |
/// </summary>
|
||||
{ |
/// <param name="backdrop">Backdrop color</param>
|
||||
source.W *= opacity; |
/// <param name="source">Source color</param>
|
||||
return Compose(backdrop, source, Vector4.Max(Vector4.Zero, backdrop - source)); |
/// <returns>Output color</returns>
|
||||
} |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static Vector4 Screen(Vector4 backdrop, Vector4 source) |
||||
/// <summary>
|
{ |
||||
/// Complement of source multiplied by the complement of backdrop
|
return Vector4.One - ((Vector4.One - backdrop) * (Vector4.One - source)); |
||||
/// </summary>
|
} |
||||
/// <param name="backdrop">Backdrop color</param>
|
|
||||
/// <param name="source">Source color</param>
|
/// <summary>
|
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
/// Per element, chooses the smallest value of source and backdrop
|
||||
/// <returns>Output color</returns>
|
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
/// <param name="backdrop">Backdrop color</param>
|
||||
public static Vector4 Screen(Vector4 backdrop, Vector4 source, float opacity) |
/// <param name="source">Source color</param>
|
||||
{ |
/// <returns>Output color</returns>
|
||||
source.W *= opacity; |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
return Compose(backdrop, source, Vector4.One - ((Vector4.One - backdrop) * (Vector4.One - source))); |
public static Vector4 Darken(Vector4 backdrop, Vector4 source) |
||||
} |
{ |
||||
|
return Vector4.Min(backdrop, source); |
||||
/// <summary>
|
} |
||||
/// Per element, chooses the smallest value of source and backdrop
|
|
||||
/// </summary>
|
/// <summary>
|
||||
/// <param name="backdrop">Backdrop color</param>
|
/// Per element, chooses the largest value of source and backdrop
|
||||
/// <param name="source">Source color</param>
|
/// </summary>
|
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
/// <param name="backdrop">Backdrop color</param>
|
||||
/// <returns>Output color</returns>
|
/// <param name="source">Source color</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
/// <returns>Output color</returns>
|
||||
public static Vector4 Darken(Vector4 backdrop, Vector4 source, float opacity) |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
{ |
public static Vector4 Lighten(Vector4 backdrop, Vector4 source) |
||||
source.W *= opacity; |
{ |
||||
return Compose(backdrop, source, Vector4.Min(backdrop, source)); |
return Vector4.Max(backdrop, source); |
||||
} |
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Per element, chooses the largest value of source and backdrop
|
/// Overlays source over backdrop
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <param name="backdrop">Backdrop color</param>
|
/// <param name="backdrop">Backdrop color</param>
|
||||
/// <param name="source">Source color</param>
|
/// <param name="source">Source color</param>
|
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
/// <returns>Output color</returns>
|
||||
/// <returns>Output color</returns>
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
public static Vector4 Overlay(Vector4 backdrop, Vector4 source) |
||||
public static Vector4 Lighten(Vector4 backdrop, Vector4 source, float opacity) |
{ |
||||
{ |
float cr = OverlayValueFunction(backdrop.X, source.X); |
||||
source.W *= opacity; |
float cg = OverlayValueFunction(backdrop.Y, source.Y); |
||||
return Compose(backdrop, source, Vector4.Max(backdrop, source)); |
float cb = OverlayValueFunction(backdrop.Z, source.Z); |
||||
} |
|
||||
|
return Vector4.Min(Vector4.One, new Vector4(cr, cg, cb, 0)); |
||||
/// <summary>
|
} |
||||
/// Overlays source over backdrop
|
|
||||
/// </summary>
|
/// <summary>
|
||||
/// <param name="backdrop">Backdrop color</param>
|
/// Hard light effect
|
||||
/// <param name="source">Source color</param>
|
/// </summary>
|
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
/// <param name="backdrop">Backdrop color</param>
|
||||
/// <returns>Output color</returns>
|
/// <param name="source">Source color</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
/// <returns>Output color</returns>
|
||||
public static Vector4 Overlay(Vector4 backdrop, Vector4 source, float opacity) |
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
{ |
public static Vector4 HardLight(Vector4 backdrop, Vector4 source) |
||||
source.W *= opacity; |
{ |
||||
float cr = OverlayValueFunction(backdrop.X, source.X); |
float cr = OverlayValueFunction(source.X, backdrop.X); |
||||
float cg = OverlayValueFunction(backdrop.Y, source.Y); |
float cg = OverlayValueFunction(source.Y, backdrop.Y); |
||||
float cb = OverlayValueFunction(backdrop.Z, source.Z); |
float cb = OverlayValueFunction(source.Z, backdrop.Z); |
||||
|
|
||||
return Compose(backdrop, source, Vector4.Min(Vector4.One, new Vector4(cr, cg, cb, 0))); |
return Vector4.Min(Vector4.One, new Vector4(cr, cg, cb, 0)); |
||||
} |
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Hard light effect
|
/// Helper function for Overlay andHardLight modes
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <param name="backdrop">Backdrop color</param>
|
/// <param name="backdrop">Backdrop color element</param>
|
||||
/// <param name="source">Source color</param>
|
/// <param name="source">Source color element</param>
|
||||
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
/// <returns>Overlay value</returns>
|
||||
/// <returns>Output color</returns>
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
private static float OverlayValueFunction(float backdrop, float source) |
||||
public static Vector4 HardLight(Vector4 backdrop, Vector4 source, float opacity) |
{ |
||||
{ |
return backdrop <= 0.5f ? (2 * backdrop * source) : 1 - ((2 * (1 - source)) * (1 - backdrop)); |
||||
source.W *= opacity; |
} |
||||
float cr = OverlayValueFunction(source.X, backdrop.X); |
|
||||
float cg = OverlayValueFunction(source.Y, backdrop.Y); |
/// <summary>
|
||||
float cb = OverlayValueFunction(source.Z, backdrop.Z); |
/// General composition function for all modes, with a general solution for alpha channel
|
||||
|
/// </summary>
|
||||
return Compose(backdrop, source, Vector4.Min(Vector4.One, new Vector4(cr, cg, cb, 0))); |
/// <param name="backdrop">Original Backdrop color</param>
|
||||
} |
/// <param name="source">Original source color</param>
|
||||
|
/// <param name="xform">Desired transformed color, without taking Alpha channel in account</param>
|
||||
/// <summary>
|
/// <returns>The final color</returns>
|
||||
/// Helper function for Overlay andHardLight modes
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
/// </summary>
|
private static Vector4 SrcOverReference(Vector4 backdrop, Vector4 source, Vector4 xform) |
||||
/// <param name="backdrop">Backdrop color element</param>
|
{ |
||||
/// <param name="source">Source color element</param>
|
// calculate weights
|
||||
/// <returns>Overlay value</returns>
|
float xw = backdrop.W * source.W; |
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
float bw = backdrop.W - xw; |
||||
private static float OverlayValueFunction(float backdrop, float source) |
float sw = source.W - xw; |
||||
{ |
|
||||
return backdrop <= 0.5f ? (2 * backdrop * source) : 1 - ((2 * (1 - source)) * (1 - backdrop)); |
// calculate final alpha
|
||||
} |
float a = xw + bw + sw; |
||||
|
|
||||
/// <summary>
|
// calculate final value
|
||||
/// General composition function for all modes, with a general solution for alpha channel
|
xform = ((xform * xw) + (backdrop * bw) + (source * sw)) / MathF.Max(a, Constants.Epsilon); |
||||
/// </summary>
|
xform.W = a; |
||||
/// <param name="backdrop">Original Backdrop color</param>
|
|
||||
/// <param name="source">Original source color</param>
|
return xform; |
||||
/// <param name="xform">Desired transformed color, without taking Alpha channel in account</param>
|
} |
||||
/// <returns>The final color</returns>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
public static Vector4 Over(Vector4 dst, Vector4 src, Vector4 blend) |
||||
private static Vector4 Compose(Vector4 backdrop, Vector4 source, Vector4 xform) |
{ |
||||
{ |
// calculate weights
|
||||
// calculate weights
|
float blendW = dst.W * src.W; |
||||
float xw = backdrop.W * source.W; |
float dstW = dst.W - blendW; |
||||
float bw = backdrop.W - xw; |
float srcW = src.W - blendW; |
||||
float sw = source.W - xw; |
|
||||
|
// calculate final alpha
|
||||
// calculate final alpha
|
float alpha = dstW + srcW + blendW; |
||||
float a = xw + bw + sw; |
|
||||
|
// calculate final color
|
||||
// calculate final value
|
Vector4 color = (dst * dstW) + (src * srcW) + (blend * blendW); |
||||
xform = ((xform * xw) + (backdrop * bw) + (source * sw)) / MathF.Max(a, Constants.Epsilon); |
|
||||
xform.W = a; |
// unpremultiply
|
||||
|
color /= MathF.Max(alpha, Constants.Epsilon); |
||||
return xform; |
color.W = alpha; |
||||
} |
|
||||
} |
return color; |
||||
|
} |
||||
|
|
||||
|
public static Vector4 Atop(Vector4 dst, Vector4 src, Vector4 blend) |
||||
|
{ |
||||
|
// calculate weights
|
||||
|
float blendW = dst.W * src.W; |
||||
|
float dstW = dst.W - blendW; |
||||
|
|
||||
|
// calculate final alpha
|
||||
|
float alpha = dstW + blendW; |
||||
|
|
||||
|
// calculate final color
|
||||
|
Vector4 color = (dst * dstW) + (blend * blendW); |
||||
|
|
||||
|
// unpremultiply
|
||||
|
color /= MathF.Max(alpha, Constants.Epsilon); |
||||
|
color.W = alpha; |
||||
|
|
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
public static Vector4 In(Vector4 dst, Vector4 src, Vector4 blend) |
||||
|
{ |
||||
|
float alpha = dst.W * src.W; |
||||
|
|
||||
|
Vector4 color = src * alpha; // premultiply
|
||||
|
color /= MathF.Max(alpha, Constants.Epsilon); // unpremultiply
|
||||
|
color.W = alpha; |
||||
|
|
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
public static Vector4 Out(Vector4 dst, Vector4 src) |
||||
|
{ |
||||
|
float alpha = (1 - dst.W) * src.W; |
||||
|
|
||||
|
Vector4 color = src * alpha; // premultiply
|
||||
|
color /= MathF.Max(alpha, Constants.Epsilon); // unpremultiply
|
||||
|
color.W = alpha; |
||||
|
|
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
public static Vector4 Xor(Vector4 dst, Vector4 src) |
||||
|
{ |
||||
|
float srcW = 1 - dst.W; |
||||
|
float dstW = 1 - src.W; |
||||
|
|
||||
|
float alpha = (src.W * srcW) + (dst.W * dstW); |
||||
|
Vector4 color = (src.W * src * srcW) + (dst.W * dst * dstW); |
||||
|
|
||||
|
// unpremultiply
|
||||
|
color /= MathF.Max(alpha, Constants.Epsilon); |
||||
|
color.W = alpha; |
||||
|
|
||||
|
return color; |
||||
|
} |
||||
|
|
||||
|
private static Vector4 Clear(Vector4 backdrop, Vector4 source) |
||||
|
{ |
||||
|
return Vector4.Zero; |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
@ -1,153 +1,158 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
// Copyright (c) Six Labors and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
using System; |
using System; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.Linq; |
using System.Linq; |
||||
using SixLabors.ImageSharp.PixelFormats; |
using SixLabors.ImageSharp.PixelFormats; |
||||
using SixLabors.ImageSharp.Processing; |
using SixLabors.ImageSharp.Processing; |
||||
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
||||
using SixLabors.Primitives; |
using SixLabors.Primitives; |
||||
using Xunit; |
using Xunit; |
||||
|
|
||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||
namespace SixLabors.ImageSharp.Tests.Drawing |
namespace SixLabors.ImageSharp.Tests.Drawing |
||||
{ |
{ |
||||
[GroupOutput("Drawing")] |
[GroupOutput("Drawing")] |
||||
public class SolidFillBlendedShapesTests |
public class SolidFillBlendedShapesTests |
||||
{ |
{ |
||||
public static IEnumerable<object[]> modes = |
public static IEnumerable<object[]> modes = |
||||
((PixelBlenderMode[])Enum.GetValues(typeof(PixelBlenderMode))).Select(x => new object[] { x }); |
((PixelBlenderMode[])Enum.GetValues(typeof(PixelBlenderMode))).Select(x => new object[] { x }); |
||||
|
|
||||
[Theory] |
[Theory] |
||||
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
||||
public void _1DarkBlueRect_2BlendHotPinkRect<TPixel>( |
public void _1DarkBlueRect_2BlendHotPinkRect<TPixel>( |
||||
TestImageProvider<TPixel> provider, |
TestImageProvider<TPixel> provider, |
||||
PixelBlenderMode mode) |
PixelBlenderMode mode) |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
using (Image<TPixel> img = provider.GetImage()) |
using (Image<TPixel> img = provider.GetImage()) |
||||
{ |
{ |
||||
int scaleX = img.Width / 100; |
int scaleX = img.Width / 100; |
||||
int scaleY = img.Height / 100; |
int scaleY = img.Height / 100; |
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
NamedColors<TPixel>.DarkBlue, |
NamedColors<TPixel>.DarkBlue, |
||||
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY) |
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY) |
||||
) |
) |
||||
.Fill(new GraphicsOptions(true) { BlenderMode = mode }, |
.Fill(new GraphicsOptions(true) { BlenderMode = mode }, |
||||
NamedColors<TPixel>.HotPink, |
NamedColors<TPixel>.HotPink, |
||||
new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY)) |
new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY)) |
||||
); |
); |
||||
|
|
||||
VerifyImage(provider, mode, img); |
VerifyImage(provider, mode, img); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
[Theory] |
[Theory] |
||||
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
||||
public void _1DarkBlueRect_2BlendHotPinkRect_3BlendTransparentEllipse<TPixel>( |
public void _1DarkBlueRect_2BlendHotPinkRect_3BlendTransparentEllipse<TPixel>( |
||||
TestImageProvider<TPixel> provider, |
TestImageProvider<TPixel> provider, |
||||
PixelBlenderMode mode) |
PixelBlenderMode mode) |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
using (Image<TPixel> img = provider.GetImage()) |
using (Image<TPixel> img = provider.GetImage()) |
||||
{ |
{ |
||||
int scaleX = img.Width / 100; |
int scaleX = img.Width / 100; |
||||
int scaleY = img.Height / 100; |
int scaleY = img.Height / 100; |
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
NamedColors<TPixel>.DarkBlue, |
NamedColors<TPixel>.DarkBlue, |
||||
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); |
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); |
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
new GraphicsOptions(true) { BlenderMode = mode }, |
new GraphicsOptions(true) { BlenderMode = mode }, |
||||
NamedColors<TPixel>.HotPink, |
NamedColors<TPixel>.HotPink, |
||||
new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY))); |
new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY))); |
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
new GraphicsOptions(true) { BlenderMode = mode }, |
new GraphicsOptions(true) { BlenderMode = mode }, |
||||
NamedColors<TPixel>.Transparent, |
NamedColors<TPixel>.Transparent, |
||||
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)) |
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)) |
||||
); |
); |
||||
|
|
||||
VerifyImage(provider, mode, img); |
VerifyImage(provider, mode, img); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
[Theory] |
[Theory] |
||||
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
||||
public void _1DarkBlueRect_2BlendHotPinkRect_3BlendSemiTransparentRedEllipse<TPixel>( |
public void _1DarkBlueRect_2BlendHotPinkRect_3BlendSemiTransparentRedEllipse<TPixel>( |
||||
TestImageProvider<TPixel> provider, |
TestImageProvider<TPixel> provider, |
||||
PixelBlenderMode mode) |
PixelBlenderMode mode) |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
using (Image<TPixel> img = provider.GetImage()) |
using (Image<TPixel> img = provider.GetImage()) |
||||
{ |
{ |
||||
int scaleX = (img.Width / 100); |
int scaleX = (img.Width / 100); |
||||
int scaleY = (img.Height / 100); |
int scaleY = (img.Height / 100); |
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
NamedColors<TPixel>.DarkBlue, |
NamedColors<TPixel>.DarkBlue, |
||||
new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY))); |
new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY))); |
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
new GraphicsOptions(true) { BlenderMode = mode }, |
new GraphicsOptions(true) { BlenderMode = mode }, |
||||
NamedColors<TPixel>.HotPink, |
NamedColors<TPixel>.HotPink, |
||||
new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY))); |
new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY))); |
||||
var c = NamedColors<TPixel>.Red.ToVector4(); |
var c = NamedColors<TPixel>.Red.ToVector4(); |
||||
c.W *= 0.5f; |
c.W *= 0.5f; |
||||
var pixel = default(TPixel); |
var pixel = default(TPixel); |
||||
pixel.PackFromVector4(c); |
pixel.PackFromVector4(c); |
||||
|
|
||||
img.Mutate( |
img.Mutate( |
||||
x => x.Fill( |
x => x.Fill( |
||||
new GraphicsOptions(true) { BlenderMode = mode }, |
new GraphicsOptions(true) { BlenderMode = mode }, |
||||
pixel, |
pixel, |
||||
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)) |
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)) |
||||
); |
); |
||||
|
|
||||
VerifyImage(provider, mode, img); ; |
VerifyImage(provider, mode, img); ; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
[Theory] |
[Theory] |
||||
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)] |
||||
public void _1DarkBlueRect_2BlendBlackEllipse<TPixel>(TestImageProvider<TPixel> provider, PixelBlenderMode mode) |
public void _1DarkBlueRect_2BlendBlackEllipse<TPixel>(TestImageProvider<TPixel> provider, PixelBlenderMode mode) |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
using (Image<TPixel> img = provider.GetImage()) |
using(Image<TPixel> dstImg = provider.GetImage(), srcImg = provider.GetImage()) |
||||
{ |
{ |
||||
int scaleX = (img.Width / 100); |
int scaleX = (dstImg.Width / 100); |
||||
int scaleY = (img.Height / 100); |
int scaleY = (dstImg.Height / 100); |
||||
img.Mutate( |
|
||||
x => x.Fill( |
dstImg.Mutate( |
||||
NamedColors<TPixel>.DarkBlue, |
x => x.Fill( |
||||
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); |
NamedColors<TPixel>.DarkBlue, |
||||
img.Mutate( |
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY))); |
||||
x => x.Fill( |
|
||||
new GraphicsOptions(true) { BlenderMode = mode }, |
srcImg.Mutate( |
||||
NamedColors<TPixel>.Black, |
x => x.Fill( |
||||
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))); |
NamedColors<TPixel>.Black, |
||||
|
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))); |
||||
VerifyImage(provider, mode, img); |
|
||||
} |
dstImg.Mutate( |
||||
} |
x => x.DrawImage(new GraphicsOptions(true) { BlenderMode = mode }, srcImg) |
||||
|
); |
||||
private static void VerifyImage<TPixel>(TestImageProvider<TPixel> provider, PixelBlenderMode mode, Image<TPixel> img) |
|
||||
where TPixel : struct, IPixel<TPixel> |
VerifyImage(provider, mode, dstImg); |
||||
{ |
} |
||||
img.DebugSave( |
} |
||||
provider, |
|
||||
new { mode }, |
private static void VerifyImage<TPixel>(TestImageProvider<TPixel> provider, PixelBlenderMode mode, Image<TPixel> img) |
||||
appendPixelTypeToFileName: false, |
where TPixel : struct, IPixel<TPixel> |
||||
appendSourceFileOrDescription: false); |
{ |
||||
|
img.DebugSave( |
||||
var comparer = ImageComparer.TolerantPercentage(0.01f, 3); |
provider, |
||||
img.CompareFirstFrameToReferenceOutput(comparer, |
new { mode }, |
||||
provider, |
appendPixelTypeToFileName: false, |
||||
new { mode }, |
appendSourceFileOrDescription: false); |
||||
appendPixelTypeToFileName: false, |
|
||||
appendSourceFileOrDescription: false); |
var comparer = ImageComparer.TolerantPercentage(0.01f, 3); |
||||
} |
img.CompareFirstFrameToReferenceOutput(comparer, |
||||
} |
provider, |
||||
|
new { mode }, |
||||
|
appendPixelTypeToFileName: false, |
||||
|
appendSourceFileOrDescription: false); |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
@ -1 +1 @@ |
|||||
Subproject commit 98fb7e2e4d5935b1c733bd2b206b6145b71ef378 |
Subproject commit 825220cdc4e9d1b4b3b474c63139e18e1cdb800e |
||||
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 4.1 KiB |
Loading…
Reference in new issue