Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54,892 changes: 51,422 additions & 3,470 deletions src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,65 @@ public static Vector512<float> HardLight(Vector512<float> backdrop, Vector512<fl
return Vector512.Min(Vector512.Create(1F), Vector512.ConditionalSelect(AlphaMask512(), Vector512<float>.Zero, color));
}

/// <summary>
/// Applies raster coverage to a Porter-Duff composition result.
/// </summary>
/// <param name="backdrop">The backdrop vector.</param>
/// <param name="source">The Porter-Duff composition result.</param>
/// <param name="coverage">The coverage. Range 0..1.</param>
/// <returns>The <see cref="Vector4"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector4 BlendWithCoverage(Vector4 backdrop, Vector4 source, float coverage)
{
Vector4 backdropAlpha = Numerics.PermuteW(backdrop);
Vector4 sourceAlpha = Numerics.PermuteW(source);
Vector4 backdropPremultiplied = Numerics.WithW(backdrop * backdropAlpha, backdropAlpha);
Vector4 sourcePremultiplied = Numerics.WithW(source * sourceAlpha, sourceAlpha);
Vector4 result = backdropPremultiplied + ((sourcePremultiplied - backdropPremultiplied) * coverage);

Numerics.UnPremultiply(ref result);
return result;
}

/// <summary>
/// Applies raster coverage to a Porter-Duff composition result.
/// </summary>
/// <param name="backdrop">The backdrop vector.</param>
/// <param name="source">The Porter-Duff composition result.</param>
/// <param name="coverage">The coverage. Range 0..1.</param>
/// <returns>The <see cref="Vector256{Single}"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<float> BlendWithCoverage(Vector256<float> backdrop, Vector256<float> source, Vector256<float> coverage)
{
Vector256<float> backdropAlpha = Avx.Permute(backdrop, ShuffleAlphaControl);
Vector256<float> sourceAlpha = Avx.Permute(source, ShuffleAlphaControl);
Vector256<float> backdropPremultiplied = Avx.Blend(backdrop * backdropAlpha, backdropAlpha, BlendAlphaControl);
Vector256<float> sourcePremultiplied = Avx.Blend(source * sourceAlpha, sourceAlpha, BlendAlphaControl);
Vector256<float> result = Vector256_.MultiplyAdd(backdropPremultiplied, sourcePremultiplied - backdropPremultiplied, coverage);

return Numerics.UnPremultiply(result, Avx.Permute(result, ShuffleAlphaControl));
}

/// <summary>
/// Applies raster coverage to a Porter-Duff composition result.
/// </summary>
/// <param name="backdrop">The backdrop vector.</param>
/// <param name="source">The Porter-Duff composition result.</param>
/// <param name="coverage">The coverage. Range 0..1.</param>
/// <returns>The <see cref="Vector512{Single}"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<float> BlendWithCoverage(Vector512<float> backdrop, Vector512<float> source, Vector512<float> coverage)
{
Vector512<float> backdropAlpha = Vector512_.ShuffleNative(backdrop, ShuffleAlphaControl);
Vector512<float> sourceAlpha = Vector512_.ShuffleNative(source, ShuffleAlphaControl);
Vector512<float> alphaMask = AlphaMask512();
Vector512<float> backdropPremultiplied = Vector512.ConditionalSelect(alphaMask, backdropAlpha, backdrop * backdropAlpha);
Vector512<float> sourcePremultiplied = Vector512.ConditionalSelect(alphaMask, sourceAlpha, source * sourceAlpha);
Vector512<float> result = Vector512_.MultiplyAdd(backdropPremultiplied, sourcePremultiplied - backdropPremultiplied, coverage);

return Numerics.UnPremultiply(result, Vector512_.ShuffleNative(result, ShuffleAlphaControl));
}

/// <summary>
/// Helper function for Overlay and HardLight modes
/// </summary>
Expand Down
461 changes: 461 additions & 0 deletions src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ public override void ToAbgr32(Configuration configuration, ReadOnlySpan<Abgr32>
source.CopyTo(destination.Slice(0, source.Length));
}


/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Abgr32> destination,
PixelConversionModifiers modifiers)
{
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale));
Guard.DestinationShouldNotBeTooShort(sourceVectors, destination, nameof(destination));

destination = destination[..sourceVectors.Length];
Vector4Converters.ApplyBackwardConversionModifiers(sourceVectors, modifiers.Remove(PixelConversionModifiers.Scale));

Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);

// The SIMD saturating conversion produces RGBA byte order. Reusing the destination
// buffer and shuffling it in place avoids the row-sized Rgba32 temporary used by
// the generic Rgba-compatible path for non-Rgba32 formats.
SimdUtils.NormalizedFloatToByteSaturate(
MemoryMarshal.Cast<Vector4, float>(sourceVectors),
destinationBytes);
PixelConverter.FromRgba32.ToAbgr32(destinationBytes, destinationBytes);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ public override void ToArgb32(Configuration configuration, ReadOnlySpan<Argb32>
source.CopyTo(destination.Slice(0, source.Length));
}


/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Argb32> destination,
PixelConversionModifiers modifiers)
{
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale));
Guard.DestinationShouldNotBeTooShort(sourceVectors, destination, nameof(destination));

destination = destination[..sourceVectors.Length];
Vector4Converters.ApplyBackwardConversionModifiers(sourceVectors, modifiers.Remove(PixelConversionModifiers.Scale));

Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);

// The SIMD saturating conversion produces RGBA byte order. Reusing the destination
// buffer and shuffling it in place avoids the row-sized Rgba32 temporary used by
// the generic Rgba-compatible path for non-Rgba32 formats.
SimdUtils.NormalizedFloatToByteSaturate(
MemoryMarshal.Cast<Vector4, float>(sourceVectors),
destinationBytes);
PixelConverter.FromRgba32.ToArgb32(destinationBytes, destinationBytes);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ public override void ToBgra32(Configuration configuration, ReadOnlySpan<Bgra32>
source.CopyTo(destination.Slice(0, source.Length));
}


/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Bgra32> destination,
PixelConversionModifiers modifiers)
{
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale));
Guard.DestinationShouldNotBeTooShort(sourceVectors, destination, nameof(destination));

destination = destination[..sourceVectors.Length];
Vector4Converters.ApplyBackwardConversionModifiers(sourceVectors, modifiers.Remove(PixelConversionModifiers.Scale));

Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);

// The SIMD saturating conversion produces RGBA byte order. Reusing the destination
// buffer and shuffling it in place avoids the row-sized Rgba32 temporary used by
// the generic Rgba-compatible path for non-Rgba32 formats.
SimdUtils.NormalizedFloatToByteSaturate(
MemoryMarshal.Cast<Vector4, float>(sourceVectors),
destinationBytes);
PixelConverter.FromRgba32.ToBgra32(destinationBytes, destinationBytes);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ using SixLabors.ImageSharp.PixelFormats.Utils;
{
removeTheseModifiers += " | PixelConversionModifiers.Premultiply";
}

if (hasAlpha)
{
GenerateRgba32Compatible32BitVector4ConversionMethods(pixelType, removeTheseModifiers);
return;
}
#>

/// <inheritdoc />
Expand All @@ -176,6 +182,45 @@ using SixLabors.ImageSharp.PixelFormats.Utils;
<#+
}

void GenerateRgba32Compatible32BitVector4ConversionMethods(string pixelType, string removeTheseModifiers)
{
#>

/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<<#=pixelType#>> destination,
PixelConversionModifiers modifiers)
{
Guard.DestinationShouldNotBeTooShort(sourceVectors, destination, nameof(destination));

destination = destination[..sourceVectors.Length];
Vector4Converters.ApplyBackwardConversionModifiers(sourceVectors, modifiers.Remove(<#=removeTheseModifiers#>));

Span<byte> destinationBytes = MemoryMarshal.Cast<<#=pixelType#>, byte>(destination);

// The SIMD saturating conversion produces RGBA byte order. Reusing the destination
// buffer and shuffling it in place avoids the row-sized Rgba32 temporary used by
// the generic Rgba-compatible path for non-Rgba32 formats.
SimdUtils.NormalizedFloatToByteSaturate(
MemoryMarshal.Cast<Vector4, float>(sourceVectors),
destinationBytes);
PixelConverter.FromRgba32.To<#=pixelType#>(destinationBytes, destinationBytes);
}

/// <inheritdoc />
public override void ToVector4(
Configuration configuration,
ReadOnlySpan<<#=pixelType#>> source,
Span<Vector4> destination,
PixelConversionModifiers modifiers)
{
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, source, destination, modifiers.Remove(<#=removeTheseModifiers#>));
}
<#+
}

void GenerateAllDefaultConversionMethods(string pixelType)
{
GenerateDefaultSelfConversionMethods(pixelType);
Expand Down
Loading
Loading