mirror of https://github.com/SixLabors/ImageSharp
12 changed files with 648 additions and 318 deletions
@ -0,0 +1,151 @@ |
|||||
|
// <copyright file="PorterDuffFunctions.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.PixelFormats.PixelBlenders |
||||
|
{ |
||||
|
using System.Numerics; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Collection of Porter Duff alpha blending functions
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TPixel">Pixel Format</typeparam>
|
||||
|
/// <remarks>
|
||||
|
/// 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
|
||||
|
/// and source, and there's no need to alpha-premultiply neither the backdrop
|
||||
|
/// nor the source.
|
||||
|
/// Note there are faster functions for when the backdrop color is known
|
||||
|
/// to be opaque
|
||||
|
/// </remarks>
|
||||
|
internal static class PorterDuffFunctions<TPixel> |
||||
|
where TPixel : IPixel |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Source over backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel NormalBlendFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.NormalBlendFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Source multiplied by backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel MultiplyFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.MultiplyFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Source added to backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel AddFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.AddFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Source substracted from backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel SubstractFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.SubstractFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Complement of source multiplied by the complement of backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel ScreenFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.ScreenFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Per element, chooses the smallest value of source and backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel DarkenFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.DarkenFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Per element, chooses the largest value of source and backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel LightenFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.LightenFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Overlays source over backdrop
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel OverlayFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.OverlayFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Hard light effect
|
||||
|
/// </summary>
|
||||
|
/// <param name="backdrop">Backgrop color</param>
|
||||
|
/// <param name="source">Source color</param>
|
||||
|
/// <param name="opacity">Opacity applied to Source Alpha</param>
|
||||
|
/// <returns>Output color</returns>
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
public static TPixel HardLightFunction(TPixel backdrop, TPixel source, float opacity) |
||||
|
{ |
||||
|
return ToPixel(PorterDuffFunctions.HardLightFunction(backdrop.ToVector4(), source.ToVector4(), opacity)); |
||||
|
} |
||||
|
|
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
private static TPixel ToPixel(Vector4 vector) |
||||
|
{ |
||||
|
TPixel p = default(TPixel); |
||||
|
p.PackFromVector4(vector); |
||||
|
return p; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,103 @@ |
|||||
|
// <copyright file="Crop.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Benchmarks |
||||
|
{ |
||||
|
|
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
using ImageSharp.PixelFormats; |
||||
|
using ImageSharp.Drawing; |
||||
|
using ImageSharp.Processing.Processors; |
||||
|
using CoreImage = ImageSharp.Image; |
||||
|
using CoreSize = ImageSharp.Size; |
||||
|
using System.Numerics; |
||||
|
using ImageSharp.PixelFormats.PixelBlenders; |
||||
|
|
||||
|
public class PorterDuffBulkVsPixel : BenchmarkBase |
||||
|
{ |
||||
|
private void BulkVectorConvert<TPixel>(BufferSpan<TPixel> destination, BufferSpan<TPixel> background, BufferSpan<TPixel> source, BufferSpan<float> amount) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); |
||||
|
Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); |
||||
|
Guard.MustBeGreaterThanOrEqualTo(amount.Length, destination.Length, nameof(amount.Length)); |
||||
|
|
||||
|
using (Buffer<Vector4> buffer = new Buffer<Vector4>(destination.Length * 3)) |
||||
|
{ |
||||
|
BufferSpan<Vector4> destinationSpan = buffer.Slice(0, destination.Length); |
||||
|
BufferSpan<Vector4> backgroundSpan = buffer.Slice(destination.Length, destination.Length); |
||||
|
BufferSpan<Vector4> sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); |
||||
|
|
||||
|
PixelOperations<TPixel>.Instance.ToVector4(background, backgroundSpan, destination.Length); |
||||
|
PixelOperations<TPixel>.Instance.ToVector4(source, sourceSpan, destination.Length); |
||||
|
|
||||
|
for (int i = 0; i < destination.Length; i++) |
||||
|
{ |
||||
|
destinationSpan[i] = PorterDuffFunctions.NormalBlendFunction(backgroundSpan[i], sourceSpan[i], amount[i]); |
||||
|
} |
||||
|
|
||||
|
PixelOperations<TPixel>.Instance.PackFromVector4(destinationSpan, destination, destination.Length); |
||||
|
} |
||||
|
} |
||||
|
private void BulkPixelConvert<TPixel>(BufferSpan<TPixel> destination, BufferSpan<TPixel> background, BufferSpan<TPixel> source, BufferSpan<float> amount) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
Guard.MustBeGreaterThanOrEqualTo(destination.Length, background.Length, nameof(destination)); |
||||
|
Guard.MustBeGreaterThanOrEqualTo(source.Length, background.Length, nameof(destination)); |
||||
|
Guard.MustBeGreaterThanOrEqualTo(amount.Length, background.Length, nameof(destination)); |
||||
|
|
||||
|
for (int i = 0; i < destination.Length; i++) |
||||
|
{ |
||||
|
destination[i] = PorterDuffFunctions<TPixel>.NormalBlendFunction(destination[i], source[i], amount[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Benchmark(Description = "ImageSharp BulkVectorConvert")] |
||||
|
public CoreSize BulkVectorConvert() |
||||
|
{ |
||||
|
using (CoreImage image = new CoreImage(800, 800)) |
||||
|
{ |
||||
|
Buffer<float> amounts = new Buffer<float>(image.Width); |
||||
|
|
||||
|
for (int x = 0; x < image.Width; x++) |
||||
|
{ |
||||
|
amounts[x] = 1; |
||||
|
} |
||||
|
using (PixelAccessor<Rgba32> pixels = image.Lock()) |
||||
|
{ |
||||
|
for (int y = 0; y < image.Height; y++) |
||||
|
{ |
||||
|
BufferSpan<Rgba32> span = pixels.GetRowSpan(y); |
||||
|
BulkVectorConvert(span, span, span, amounts); |
||||
|
} |
||||
|
} |
||||
|
return new CoreSize(image.Width, image.Height); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Benchmark(Description = "ImageSharp BulkPixelConvert")] |
||||
|
public CoreSize BulkPixelConvert() |
||||
|
{ |
||||
|
using (CoreImage image = new CoreImage(800, 800)) |
||||
|
{ |
||||
|
Buffer<float> amounts = new Buffer<float>(image.Width); |
||||
|
|
||||
|
for (int x = 0; x < image.Width; x++) |
||||
|
{ |
||||
|
amounts[x] = 1; |
||||
|
} |
||||
|
using (PixelAccessor<Rgba32> pixels = image.Lock()) |
||||
|
{ |
||||
|
for (int y = 0; y < image.Height; y++) |
||||
|
{ |
||||
|
BufferSpan<Rgba32> span = pixels.GetRowSpan(y); |
||||
|
BulkPixelConvert(span, span, span, amounts); |
||||
|
} |
||||
|
} |
||||
|
return new CoreSize(image.Width, image.Height); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue