Browse Source

Move FastClamp to Numerics

js/color-alpha-handling
James Jackson-South 6 years ago
parent
commit
289b5326c1
  1. 2
      src/ImageSharp/ColorSpaces/Cmyk.cs
  2. 13
      src/ImageSharp/Common/Helpers/Numerics.cs
  3. 2
      src/ImageSharp/Common/Helpers/SimdUtils.FallbackIntrinsics128.cs
  4. 2
      src/ImageSharp/Common/Helpers/SimdUtils.cs
  5. 12
      src/ImageSharp/Common/Helpers/Vector4Utilities.cs
  6. 32
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.cs
  7. 2
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.tt
  8. 2
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs
  9. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
  10. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
  11. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs
  12. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs
  13. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs
  14. 2
      src/ImageSharp/PixelFormats/PixelImplementations/L16.cs
  15. 2
      src/ImageSharp/PixelFormats/PixelImplementations/L8.cs
  16. 2
      src/ImageSharp/PixelFormats/PixelImplementations/La16.cs
  17. 2
      src/ImageSharp/PixelFormats/PixelImplementations/La32.cs
  18. 2
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4.cs
  19. 2
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort4.cs
  20. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs
  21. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Rgb48.cs
  22. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Rgba1010102.cs
  23. 4
      src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs
  24. 4
      src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs
  25. 2
      src/ImageSharp/PixelFormats/PixelImplementations/RgbaVector.cs
  26. 2
      src/ImageSharp/PixelFormats/PixelImplementations/Short4.cs
  27. 2
      src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs
  28. 2
      src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtilities.cs

2
src/ImageSharp/ColorSpaces/Cmyk.cs

@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.ColorSpaces
[MethodImpl(InliningOptions.ShortMethod)]
public Cmyk(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Min, Max);
vector = Numerics.Clamp(vector, Min, Max);
this.C = vector.X;
this.M = vector.Y;
this.Y = vector.Z;

13
src/ImageSharp/Common/Helpers/Numerics.cs

@ -253,6 +253,19 @@ namespace SixLabors.ImageSharp
return value;
}
/// <summary>
/// Returns the value clamped to the inclusive range of min and max.
/// 5x Faster than <see cref="Vector4.Clamp(Vector4, Vector4, Vector4)"/>
/// on platforms &lt; NET 5.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum inclusive value.</param>
/// <param name="max">The maximum inclusive value.</param>
/// <returns>The clamped <see cref="Vector4"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max)
=> Vector4.Min(Vector4.Max(value, min), max);
/// <summary>
/// Clamps the span values to the inclusive range of min and max.
/// </summary>

2
src/ImageSharp/Common/Helpers/SimdUtils.FallbackIntrinsics128.cs

@ -125,7 +125,7 @@ namespace SixLabors.ImageSharp
Vector4 s = Unsafe.Add(ref sBase, i);
s *= maxBytes;
s += half;
s = Vector4Utilities.FastClamp(s, Vector4.Zero, maxBytes);
s = Numerics.Clamp(s, Vector4.Zero, maxBytes);
ref ByteVector4 d = ref Unsafe.Add(ref dBase, i);
d.X = (byte)s.X;

2
src/ImageSharp/Common/Helpers/SimdUtils.cs

@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector4 PseudoRound(this Vector4 v)
{
Vector4 sign = Vector4Utilities.FastClamp(v, new Vector4(-1), new Vector4(1));
Vector4 sign = Numerics.Clamp(v, new Vector4(-1), new Vector4(1));
return v + (sign * 0.5f);
}

12
src/ImageSharp/Common/Helpers/Vector4Utilities.cs

@ -20,18 +20,6 @@ namespace SixLabors.ImageSharp
private const int BlendAlphaControl = 0b_10_00_10_00;
private const int ShuffleAlphaControl = 0b_11_11_11_11;
/// <summary>
/// Restricts a vector between a minimum and a maximum value.
/// 5x Faster then <see cref="Vector4.Clamp(Vector4, Vector4, Vector4)"/>.
/// </summary>
/// <param name="x">The vector to restrict.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The <see cref="Vector4"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static Vector4 FastClamp(Vector4 x, Vector4 min, Vector4 max)
=> Vector4.Min(Vector4.Max(x, min), max);
/// <summary>
/// Pre-multiplies the "x", "y", "z" components of a vector by its "w" component leaving the "w" component intact.
/// </summary>

32
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.cs

@ -19,22 +19,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
var CMax4 = new Vector4(maximum);
var COff4 = new Vector4(MathF.Ceiling(maximum / 2));
this.V0L = Vector4Utilities.FastClamp(this.V0L + COff4, CMin4, CMax4);
this.V0R = Vector4Utilities.FastClamp(this.V0R + COff4, CMin4, CMax4);
this.V1L = Vector4Utilities.FastClamp(this.V1L + COff4, CMin4, CMax4);
this.V1R = Vector4Utilities.FastClamp(this.V1R + COff4, CMin4, CMax4);
this.V2L = Vector4Utilities.FastClamp(this.V2L + COff4, CMin4, CMax4);
this.V2R = Vector4Utilities.FastClamp(this.V2R + COff4, CMin4, CMax4);
this.V3L = Vector4Utilities.FastClamp(this.V3L + COff4, CMin4, CMax4);
this.V3R = Vector4Utilities.FastClamp(this.V3R + COff4, CMin4, CMax4);
this.V4L = Vector4Utilities.FastClamp(this.V4L + COff4, CMin4, CMax4);
this.V4R = Vector4Utilities.FastClamp(this.V4R + COff4, CMin4, CMax4);
this.V5L = Vector4Utilities.FastClamp(this.V5L + COff4, CMin4, CMax4);
this.V5R = Vector4Utilities.FastClamp(this.V5R + COff4, CMin4, CMax4);
this.V6L = Vector4Utilities.FastClamp(this.V6L + COff4, CMin4, CMax4);
this.V6R = Vector4Utilities.FastClamp(this.V6R + COff4, CMin4, CMax4);
this.V7L = Vector4Utilities.FastClamp(this.V7L + COff4, CMin4, CMax4);
this.V7R = Vector4Utilities.FastClamp(this.V7R + COff4, CMin4, CMax4);
this.V0L = Numerics.Clamp(this.V0L + COff4, CMin4, CMax4);
this.V0R = Numerics.Clamp(this.V0R + COff4, CMin4, CMax4);
this.V1L = Numerics.Clamp(this.V1L + COff4, CMin4, CMax4);
this.V1R = Numerics.Clamp(this.V1R + COff4, CMin4, CMax4);
this.V2L = Numerics.Clamp(this.V2L + COff4, CMin4, CMax4);
this.V2R = Numerics.Clamp(this.V2R + COff4, CMin4, CMax4);
this.V3L = Numerics.Clamp(this.V3L + COff4, CMin4, CMax4);
this.V3R = Numerics.Clamp(this.V3R + COff4, CMin4, CMax4);
this.V4L = Numerics.Clamp(this.V4L + COff4, CMin4, CMax4);
this.V4R = Numerics.Clamp(this.V4R + COff4, CMin4, CMax4);
this.V5L = Numerics.Clamp(this.V5L + COff4, CMin4, CMax4);
this.V5R = Numerics.Clamp(this.V5R + COff4, CMin4, CMax4);
this.V6L = Numerics.Clamp(this.V6L + COff4, CMin4, CMax4);
this.V6R = Numerics.Clamp(this.V6R + COff4, CMin4, CMax4);
this.V7L = Numerics.Clamp(this.V7L + COff4, CMin4, CMax4);
this.V7R = Numerics.Clamp(this.V7R + COff4, CMin4, CMax4);
}
/// <summary>

2
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.tt

@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
for (int j = 0; j < 2; j++)
{
char side = j == 0 ? 'L' : 'R';
Write($"this.V{i}{side} = Vector4Utilities.FastClamp(this.V{i}{side} + COff4, CMin4, CMax4);\r\n");
Write($"this.V{i}{side} = Numerics.Clamp(this.V{i}{side} + COff4, CMin4, CMax4);\r\n");
}
}
PopIndent();

2
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

@ -671,7 +671,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
private static Vector4 DivideRound(Vector4 dividend, Vector4 divisor)
{
// sign(dividend) = max(min(dividend, 1), -1)
Vector4 sign = Vector4Utilities.FastClamp(dividend, NegativeOne, Vector4.One);
Vector4 sign = Numerics.Clamp(dividend, NegativeOne, Vector4.One);
// AlmostRound(dividend/divisor) = dividend/divisor + 0.5*sign(dividend)
return (dividend / divisor) + (sign * Offset);

2
src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

@ -373,7 +373,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
this.R = (byte)vector.X;
this.G = (byte)vector.Y;

2
src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs

@ -296,7 +296,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
this.R = (byte)vector.X;
this.G = (byte)vector.Y;

2
src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs

@ -162,7 +162,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
private static ushort Pack(ref Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One);
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
return (ushort)((((int)Math.Round(vector.W * 15F) & 0x0F) << 12)
| (((int)Math.Round(vector.X * 15F) & 0x0F) << 8)
| (((int)Math.Round(vector.Y * 15F) & 0x0F) << 4)

2
src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs

@ -163,7 +163,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
private static ushort Pack(ref Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One);
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
return (ushort)(
(((int)Math.Round(vector.X * 31F) & 0x1F) << 10)
| (((int)Math.Round(vector.Y * 31F) & 0x1F) << 5)

2
src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs

@ -171,7 +171,7 @@ namespace SixLabors.ImageSharp.PixelFormats
const float Max = 255F;
// Clamp the value between min and max values
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, new Vector4(Max));
vector = Numerics.Clamp(vector, Vector4.Zero, new Vector4(Max));
uint byte4 = (uint)Math.Round(vector.X) & 0xFF;
uint byte3 = ((uint)Math.Round(vector.Y) & 0xFF) << 0x8;

2
src/ImageSharp/PixelFormats/PixelImplementations/L16.cs

@ -176,7 +176,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
internal void ConvertFromRgbaScaledVector4(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One) * Max;
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One) * Max;
this.PackedValue = ImageMath.Get16BitBT709Luminance(
vector.X,
vector.Y,

2
src/ImageSharp/PixelFormats/PixelImplementations/L8.cs

@ -156,7 +156,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
this.PackedValue = ImageMath.Get8BitBT709Luminance((byte)vector.X, (byte)vector.Y, (byte)vector.Z);
}
}

2
src/ImageSharp/PixelFormats/PixelImplementations/La16.cs

@ -219,7 +219,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
this.L = ImageMath.Get8BitBT709Luminance((byte)vector.X, (byte)vector.Y, (byte)vector.Z);
this.A = (byte)vector.W;
}

2
src/ImageSharp/PixelFormats/PixelImplementations/La32.cs

@ -233,7 +233,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
internal void ConvertFromRgbaScaledVector4(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One) * Max;
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One) * Max;
this.L = ImageMath.Get16BitBT709Luminance(
vector.X,
vector.Y,

2
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4.cs

@ -174,7 +174,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
private static uint Pack(ref Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, MinusOne, Vector4.One) * Half;
vector = Numerics.Clamp(vector, MinusOne, Vector4.One) * Half;
uint byte4 = ((uint)MathF.Round(vector.X) & 0xFF) << 0;
uint byte3 = ((uint)MathF.Round(vector.Y) & 0xFF) << 8;

2
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort4.cs

@ -177,7 +177,7 @@ namespace SixLabors.ImageSharp.PixelFormats
private static ulong Pack(ref Vector4 vector)
{
vector *= Max;
vector = Vector4Utilities.FastClamp(vector, Min, Max);
vector = Numerics.Clamp(vector, Min, Max);
// Round rather than truncate.
ulong word4 = ((ulong)MathF.Round(vector.X) & 0xFFFF) << 0x00;

2
src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs

@ -264,7 +264,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
this.R = (byte)vector.X;
this.G = (byte)vector.Y;

2
src/ImageSharp/PixelFormats/PixelImplementations/Rgb48.cs

@ -85,7 +85,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
public void FromVector4(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One) * Max;
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One) * Max;
this.R = (ushort)MathF.Round(vector.X);
this.G = (ushort)MathF.Round(vector.Y);
this.B = (ushort)MathF.Round(vector.Z);

2
src/ImageSharp/PixelFormats/PixelImplementations/Rgba1010102.cs

@ -163,7 +163,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
private static uint Pack(ref Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One) * Multiplier;
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One) * Multiplier;
return (uint)(
(((int)Math.Round(vector.X) & 0x03FF) << 0)

4
src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs

@ -452,7 +452,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
return new Rgba32((byte)vector.X, (byte)vector.Y, (byte)vector.Z, (byte)vector.W);
}
@ -491,7 +491,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{
vector *= MaxBytes;
vector += Half;
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
this.R = (byte)vector.X;
this.G = (byte)vector.Y;

4
src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs

@ -127,7 +127,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One) * Max;
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One) * Max;
this.R = (ushort)MathF.Round(vector.X);
this.G = (ushort)MathF.Round(vector.Y);
this.B = (ushort)MathF.Round(vector.Z);
@ -209,7 +209,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
public void FromVector4(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One) * Max;
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One) * Max;
this.R = (ushort)MathF.Round(vector.X);
this.G = (ushort)MathF.Round(vector.Y);
this.B = (ushort)MathF.Round(vector.Z);

2
src/ImageSharp/PixelFormats/PixelImplementations/RgbaVector.cs

@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
public void FromVector4(Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, Vector4.One);
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
this.R = vector.X;
this.G = vector.Y;
this.B = vector.Z;

2
src/ImageSharp/PixelFormats/PixelImplementations/Short4.cs

@ -183,7 +183,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(InliningOptions.ShortMethod)]
private static ulong Pack(ref Vector4 vector)
{
vector = Vector4Utilities.FastClamp(vector, Min, Max);
vector = Numerics.Clamp(vector, Min, Max);
// Clamp the value between min and max values
ulong word4 = ((ulong)Math.Round(vector.X) & 0xFFFF) << 0x00;

2
src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs

@ -304,7 +304,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
for (int x = 0; x < this.bounds.Width; x++)
{
ref Vector4 v = ref Unsafe.Add(ref sourceRef, x);
var clamp = Vector4Utilities.FastClamp(v, low, high);
var clamp = Numerics.Clamp(v, low, high);
v.X = MathF.Pow(clamp.X, this.inverseGamma);
v.Y = MathF.Pow(clamp.Y, this.inverseGamma);
v.Z = MathF.Pow(clamp.Z, this.inverseGamma);

2
src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtilities.cs

@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
MathF.Floor(maxXY.X),
MathF.Floor(maxXY.Y));
sourceExtents = Vector4Utilities.FastClamp(sourceExtents, Vector4.Zero, maxSourceExtents);
sourceExtents = Numerics.Clamp(sourceExtents, Vector4.Zero, maxSourceExtents);
int left = (int)sourceExtents.X;
int top = (int)sourceExtents.Y;

Loading…
Cancel
Save