Skip to content
Prev Previous commit
Next Next commit
Utils FTW
  • Loading branch information
JimBobSquarePants committed Nov 23, 2020
commit d6b38c1803ec50aa360376499edf30237c691a3e
12 changes: 6 additions & 6 deletions src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void Convolve2D3<TPixel>(
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
vector.W = target.W;

Vector4Utilities.UnPremultiply(ref vector);
Vector4Utils.UnPremultiply(ref vector);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numerics or ColorNumerics ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move both to Numerics.

target = vector;
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public static void Convolve2D4<TPixel>(
out Vector4 vector);

ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
Vector4Utilities.UnPremultiply(ref vector);
Vector4Utils.UnPremultiply(ref vector);
target = vector;
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public static void Convolve2DImpl<TPixel>(
{
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
var currentColor = sourceRowSpan[offsetX].ToVector4();
Vector4Utilities.Premultiply(ref currentColor);
Vector4Utils.Premultiply(ref currentColor);

vectorX += matrixX[y, x] * currentColor;
vectorY += matrixY[y, x] * currentColor;
Expand Down Expand Up @@ -193,7 +193,7 @@ public static void Convolve3<TPixel>(
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
vector.W = target.W;

Vector4Utilities.UnPremultiply(ref vector);
Vector4Utils.UnPremultiply(ref vector);
target = vector;
}

Expand Down Expand Up @@ -238,7 +238,7 @@ public static void Convolve4<TPixel>(
ref vector);

ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
Vector4Utilities.UnPremultiply(ref vector);
Vector4Utils.UnPremultiply(ref vector);
target = vector;
}

Expand Down Expand Up @@ -270,7 +270,7 @@ private static void ConvolveImpl<TPixel>(
{
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
var currentColor = sourceRowSpan[offsetX].ToVector4();
Vector4Utilities.Premultiply(ref currentColor);
Vector4Utils.Premultiply(ref currentColor);
vector += matrix[y, x] * currentColor;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Utility methods for the <see cref="Vector4"/> struct.
/// </summary>
internal static class Vector4Utilities
internal static class Vector4Utils
{
private const int BlendAlphaControl = 0b_10_00_10_00;
private const int ShuffleAlphaControl = 0b_11_11_11_11;
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/PixelFormats/Utils/Vector4Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static void ApplyForwardConversionModifiers(Span<Vector4> vectors, Pixe

if (modifiers.IsDefined(PixelConversionModifiers.Premultiply))
{
Vector4Utilities.Premultiply(vectors);
Vector4Utils.Premultiply(vectors);
}
}

Expand All @@ -36,7 +36,7 @@ internal static void ApplyBackwardConversionModifiers(Span<Vector4> vectors, Pix
{
if (modifiers.IsDefined(PixelConversionModifiers.Premultiply))
{
Vector4Utilities.UnPremultiply(vectors);
Vector4Utils.UnPremultiply(vectors);
}

if (modifiers.IsDefined(PixelConversionModifiers.SRgbCompand))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void Invoke(int y, Span<Vector4> span)
Span<TPixel> rowSpan = this.source.GetPixelRowSpan(y).Slice(this.startX, span.Length);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, rowSpan, span);

Vector4Utilities.Transform(span, ref Unsafe.AsRef(this.matrix));
Vector4Utils.Transform(span, ref Unsafe.AsRef(this.matrix));

PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, span, rowSpan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
return;
}

int yRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Height, destination.Height);
int xRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Width, destination.Width);
int yRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Height, destination.Height);
int xRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Width, destination.Width);
var radialExtents = new Vector2(xRadius, yRadius);
int yLength = (yRadius * 2) + 1;
int xLength = (xRadius * 2) + 1;
Expand Down Expand Up @@ -207,7 +207,7 @@ public void Invoke(int y, Span<Vector4> span)
// Use the single precision position to calculate correct bounding pixels
// otherwise we get rogue pixels outside of the bounds.
var point = Vector2.Transform(new Vector2(x, y), this.matrix);
LinearTransformUtilities.Convolve(
LinearTransformUtils.Convolve(
in this.sampler,
point,
sourceBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <summary>
/// Utility methods for affine and projective transforms.
/// </summary>
internal static class LinearTransformUtilities
internal static class LinearTransformUtils
{
[MethodImpl(InliningOptions.ShortMethod)]
internal static int GetSamplingRadius<TResampler>(in TResampler sampler, int sourceSize, int destinationSize)
Expand Down Expand Up @@ -78,13 +78,13 @@ internal static void Convolve<TResampler, TPixel>(

// Values are first premultiplied to prevent darkening of edge pixels.
var current = sourcePixels[x, y].ToVector4();
Vector4Utilities.Premultiply(ref current);
Vector4Utils.Premultiply(ref current);
sum += current * xWeight * yWeight;
}
}

// Reverse the premultiplication
Vector4Utilities.UnPremultiply(ref sum);
Vector4Utils.UnPremultiply(ref sum);
targetRow[column] = sum;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
return;
}

int yRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Height, destination.Height);
int xRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Width, destination.Width);
int yRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Height, destination.Height);
int xRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Width, destination.Width);
var radialExtents = new Vector2(xRadius, yRadius);
int yLength = (yRadius * 2) + 1;
int xLength = (xRadius * 2) + 1;
Expand Down Expand Up @@ -207,7 +207,7 @@ public void Invoke(int y, Span<Vector4> span)
// Use the single precision position to calculate correct bounding pixels
// otherwise we get rogue pixels outside of the bounds.
Vector2 point = TransformUtilities.ProjectiveTransform2D(x, y, this.matrix);
LinearTransformUtilities.Convolve(
LinearTransformUtils.Convolve(
in this.sampler,
point,
sourceBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void PremultiplyBaseline()
[Benchmark]
public void Premultiply()
{
Vector4Utilities.Premultiply(Vectors);
Vector4Utils.Premultiply(Vectors);
}

[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void UnPremultiplyBaseline()
[Benchmark]
public void UnPremultiply()
{
Vector4Utilities.UnPremultiply(Vectors);
Vector4Utils.UnPremultiply(Vectors);
}

[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
8 changes: 4 additions & 4 deletions tests/ImageSharp.Tests/Helpers/Vector4UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void Premultiply_VectorSpan(int length)
Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1);
Vector4[] expected = source.Select(v =>
{
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
return v;
}).ToArray();

Vector4Utilities.Premultiply(source);
Vector4Utils.Premultiply(source);

Assert.Equal(expected, source, this.approximateFloatComparer);
}
Expand All @@ -44,11 +44,11 @@ public void UnPremultiply_VectorSpan(int length)
Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1);
Vector4[] expected = source.Select(v =>
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
return v;
}).ToArray();

Vector4Utilities.UnPremultiply(source);
Vector4Utils.UnPremultiply(source);

Assert.Equal(expected, source, this.approximateFloatComparer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ void SourceAction(ref Vector4 v)
{
if (this.HasUnassociatedAlpha)
{
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
}
}

void ExpectedAction(ref Vector4 v)
{
if (this.HasUnassociatedAlpha)
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
}
}

Expand All @@ -232,15 +232,15 @@ void SourceAction(ref Vector4 v)
{
if (this.HasUnassociatedAlpha)
{
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
}
}

void ExpectedAction(ref Vector4 v)
{
if (this.HasUnassociatedAlpha)
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
}
}

Expand Down Expand Up @@ -273,15 +273,15 @@ void SourceAction(ref Vector4 v)

if (this.HasUnassociatedAlpha)
{
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
}
}

void ExpectedAction(ref Vector4 v)
{
if (this.HasUnassociatedAlpha)
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
}

SRgbCompanding.Compress(ref v);
Expand Down Expand Up @@ -394,12 +394,12 @@ public void ToPremultipliedVector4(int count)
{
void SourceAction(ref Vector4 v)
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
}

void ExpectedAction(ref Vector4 v)
{
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
}

TPixel[] source = CreatePixelTestData(count, (ref Vector4 v) => SourceAction(ref v));
Expand All @@ -417,12 +417,12 @@ public void ToPremultipliedScaledVector4(int count)
{
void SourceAction(ref Vector4 v)
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
}

void ExpectedAction(ref Vector4 v)
{
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
}

TPixel[] source = CreateScaledPixelTestData(count, (ref Vector4 v) => SourceAction(ref v));
Expand All @@ -444,14 +444,14 @@ public void ToCompandedPremultipliedScaledVector4(int count)
{
void SourceAction(ref Vector4 v)
{
Vector4Utilities.UnPremultiply(ref v);
Vector4Utils.UnPremultiply(ref v);
SRgbCompanding.Compress(ref v);
}

void ExpectedAction(ref Vector4 v)
{
SRgbCompanding.Expand(ref v);
Vector4Utilities.Premultiply(ref v);
Vector4Utils.Premultiply(ref v);
}

TPixel[] source = CreateScaledPixelTestData(count, (ref Vector4 v) => SourceAction(ref v));
Expand Down