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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 5 additions & 9 deletions src/ImageSharp/Common/Helpers/ColorNumerics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ CicpColorPrimaries.ItuRBt601_7_525 or CicpColorPrimaries.SmpteSt240
/// <returns>The <see cref="byte"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Get8BitBT709Luminance(byte r, byte g, byte b)
=> (byte)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);
=> (byte)MathF.Round((r * .2126F) + (g * .7152F) + (b * .0722F));

/// <summary>
/// Gets the luminance from the rgb components using the formula
Expand All @@ -96,9 +96,7 @@ public static byte Get8BitBT709Luminance(byte r, byte g, byte b)
/// <returns>The <see cref="byte"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Get8BitBT709Luminance(ushort r, ushort g, ushort b)
=> (byte)((From16BitTo8Bit(r) * .2126F) +
(From16BitTo8Bit(g) * .7152F) +
(From16BitTo8Bit(b) * .0722F) + 0.5F);
=> (byte)MathF.Round((From16BitTo8Bit(r) * .2126F) + (From16BitTo8Bit(g) * .7152F) + (From16BitTo8Bit(b) * .0722F));

/// <summary>
/// Gets the luminance from the rgb components using the formula as
Expand All @@ -110,9 +108,7 @@ public static byte Get8BitBT709Luminance(ushort r, ushort g, ushort b)
/// <returns>The <see cref="ushort"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort Get16BitBT709Luminance(byte r, byte g, byte b)
=> (ushort)((From8BitTo16Bit(r) * .2126F) +
(From8BitTo16Bit(g) * .7152F) +
(From8BitTo16Bit(b) * .0722F) + 0.5F);
=> (ushort)MathF.Round((From8BitTo16Bit(r) * .2126F) + (From8BitTo16Bit(g) * .7152F) + (From8BitTo16Bit(b) * .0722F));

/// <summary>
/// Gets the luminance from the rgb components using the formula as
Expand All @@ -124,7 +120,7 @@ public static ushort Get16BitBT709Luminance(byte r, byte g, byte b)
/// <returns>The <see cref="ushort"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b)
=> (ushort)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);
=> (ushort)MathF.Round((r * .2126F) + (g * .7152F) + (b * .0722F));

/// <summary>
/// Gets the luminance from the rgb components using the formula as specified
Expand All @@ -136,7 +132,7 @@ public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b)
/// <returns>The <see cref="ushort"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort Get16BitBT709Luminance(float r, float g, float b)
=> (ushort)((r * .2126F) + (g * .7152F) + (b * .0722F) + 0.5F);
=> (ushort)MathF.Round((r * .2126F) + (g * .7152F) + (b * .0722F));

/// <summary>
/// Scales a value from a 16 bit <see cref="ushort"/> to an
Expand Down
104 changes: 104 additions & 0 deletions src/ImageSharp/Common/Helpers/IBitwiseReduction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

namespace SixLabors.ImageSharp;

/// <summary>
/// Defines an associative and commutative bitwise operation that reduces packed 32-bit values to one value.
/// </summary>
/// <remarks>
/// The operation must give the same result for any order and grouping of its operands.
/// <see cref="SimdUtils.Reduce{TReduction}(ReadOnlySpan{uint})"/> relies on this rule when it combines lanes and vectors.
/// </remarks>
internal interface IBitwiseReduction
{
/// <summary>
/// Gets the value that does not change an operand when the operation combines them.
/// </summary>
public static abstract uint Identity { get; }

/// <summary>
/// Combines two values.
/// </summary>
/// <param name="left">The first value.</param>
/// <param name="right">The second value.</param>
/// <returns>The combined value.</returns>
public static abstract uint Invoke(uint left, uint right);

/// <summary>
/// Combines two 128-bit vectors lane by lane.
/// </summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The combined vector.</returns>
public static abstract Vector128<uint> Invoke(Vector128<uint> left, Vector128<uint> right);

/// <summary>
/// Combines two 256-bit vectors lane by lane.
/// </summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The combined vector.</returns>
public static abstract Vector256<uint> Invoke(Vector256<uint> left, Vector256<uint> right);

/// <summary>
/// Combines two 512-bit vectors lane by lane.
/// </summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The combined vector.</returns>
public static abstract Vector512<uint> Invoke(Vector512<uint> left, Vector512<uint> right);
}

/// <summary>
/// Reduces values with bitwise AND. A bit of the result is set only when that bit is set in every value.
/// </summary>
internal readonly struct BitwiseAndReduction : IBitwiseReduction
{
/// <inheritdoc/>
public static uint Identity => uint.MaxValue;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Invoke(uint left, uint right) => left & right;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<uint> Invoke(Vector128<uint> left, Vector128<uint> right) => left & right;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<uint> Invoke(Vector256<uint> left, Vector256<uint> right) => left & right;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<uint> Invoke(Vector512<uint> left, Vector512<uint> right) => left & right;
}

/// <summary>
/// Reduces values with bitwise OR. A bit of the result is set when that bit is set in any value.
/// </summary>
internal readonly struct BitwiseOrReduction : IBitwiseReduction
{
/// <inheritdoc/>
public static uint Identity => 0;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Invoke(uint left, uint right) => left | right;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<uint> Invoke(Vector128<uint> left, Vector128<uint> right) => left | right;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<uint> Invoke(Vector256<uint> left, Vector256<uint> right) => left | right;

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<uint> Invoke(Vector512<uint> left, Vector512<uint> right) => left | right;
}
Loading
Loading