Skip to content
Prev Previous commit
Next Next commit
Replace clamp extensions
  • Loading branch information
JimBobSquarePants committed Nov 23, 2020
commit f177b91f7fbd8641428bd8c1ae1d6302fb4de89e
140 changes: 0 additions & 140 deletions src/ImageSharp/Common/Extensions/ComparableExtensions.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/ImageSharp/Common/Helpers/Buffer2DUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static void Convolve4<TPixel>(

for (int i = 0; i < kernelLength; i++)
{
int offsetY = (row + i - radiusY).Clamp(minRow, maxRow);
int offsetX = sourceOffsetColumnBase.Clamp(minColumn, maxColumn);
int offsetY = Numerics.Clamp(row + i - radiusY, minRow, maxRow);
int offsetX = Numerics.Clamp(sourceOffsetColumnBase, minColumn, maxColumn);
Span<TPixel> sourceRowSpan = sourcePixels.GetRowSpan(offsetY);
var currentColor = sourceRowSpan[offsetX].ToVector4();

Expand Down Expand Up @@ -93,13 +93,13 @@ public static void Convolve4AndAccumulatePartials(
int radiusX = kernelLength >> 1;
int sourceOffsetColumnBase = column + minColumn;

int offsetY = row.Clamp(minRow, maxRow);
int offsetY = Numerics.Clamp(row, minRow, maxRow);
ref ComplexVector4 sourceRef = ref MemoryMarshal.GetReference(sourceValues.GetRowSpan(offsetY));
ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel);

for (int x = 0; x < kernelLength; x++)
{
int offsetX = (sourceOffsetColumnBase + x - radiusX).Clamp(minColumn, maxColumn);
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
vector.Sum(Unsafe.Add(ref baseRef, x) * Unsafe.Add(ref sourceRef, offsetX));
}

Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ public static void Convolve2DImpl<TPixel>(

for (int y = 0; y < matrixHeight; y++)
{
int offsetY = (row + y - radiusY).Clamp(minRow, maxRow);
int offsetY = Numerics.Clamp(row + y - radiusY, minRow, maxRow);
Span<TPixel> sourceRowSpan = sourcePixels.GetRowSpan(offsetY);

for (int x = 0; x < matrixWidth; x++)
{
int offsetX = (sourceOffsetColumnBase + x - radiusX).Clamp(minColumn, maxColumn);
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
var currentColor = sourceRowSpan[offsetX].ToVector4();
Vector4Utilities.Premultiply(ref currentColor);

Expand Down Expand Up @@ -263,12 +263,12 @@ private static void ConvolveImpl<TPixel>(

for (int y = 0; y < matrixHeight; y++)
{
int offsetY = (row + y - radiusY).Clamp(minRow, maxRow);
int offsetY = Numerics.Clamp(row + y - radiusY, minRow, maxRow);
Span<TPixel> sourceRowSpan = sourcePixels.GetRowSpan(offsetY);

for (int x = 0; x < matrixWidth; x++)
{
int offsetX = (sourceOffsetColumnBase + x - radiusX).Clamp(minColumn, maxColumn);
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
var currentColor = sourceRowSpan[offsetX].ToVector4();
Vector4Utilities.Premultiply(ref currentColor);
vector += matrix[y, x] * currentColor;
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Common/Helpers/ImageMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ int GetMaxX(ImageFrame<TPixel> pixels)

topLeft.Y = GetMinY(bitmap);
topLeft.X = GetMinX(bitmap);
bottomRight.Y = (GetMaxY(bitmap) + 1).Clamp(0, height);
bottomRight.X = (GetMaxX(bitmap) + 1).Clamp(0, width);
bottomRight.Y = Numerics.Clamp(GetMaxY(bitmap) + 1, 0, height);
bottomRight.X = Numerics.Clamp(GetMaxX(bitmap) + 1, 0, width);

return GetBoundingRectangle(topLeft, bottomRight);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Common/Helpers/SimdUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static void ConvertNormalizedFloatToByteRemainder(ReadOnlySpan<float> so
}

[MethodImpl(InliningOptions.ShortMethod)]
private static byte ConvertToByte(float f) => (byte)ComparableExtensions.Clamp((f * 255f) + 0.5f, 0, 255f);
private static byte ConvertToByte(float f) => (byte)Numerics.Clamp((f * 255F) + 0.5F, 0, 255F);

[Conditional("DEBUG")]
private static void VerifyHasVector8(string operation)
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
ImageMetadata metadata = image.Metadata;

// System.Drawing produces identical output for jpegs with a quality parameter of 0 and 1.
int qlty = (this.quality ?? metadata.GetJpegMetadata().Quality).Clamp(1, 100);
this.subsample = this.subsample ?? (qlty >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420);
int qlty = Numerics.Clamp(this.quality ?? metadata.GetJpegMetadata().Quality, 1, 100);
this.subsample ??= qlty >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420;

// Convert from a quality rating to a scaling factor.
int scale;
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static byte CalculateBitDepth<TPixel>(
byte bitDepth;
if (options.ColorType == PngColorType.Palette)
{
byte quantizedBits = (byte)ImageMath.GetBitsNeededForColorDepth(quantizedFrame.Palette.Length).Clamp(1, 8);
byte quantizedBits = (byte)Numerics.Clamp(ImageMath.GetBitsNeededForColorDepth(quantizedFrame.Palette.Length), 1, 8);
byte bits = Math.Max((byte)options.BitDepth, quantizedBits);

// Png only supports in four pixel depths: 1, 2, 4, and 8 bits when using the PLTE chunk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IccDataReader(byte[] data)
/// <param name="index">The new index position</param>
public void SetIndex(int index)
{
this.currentIndex = index.Clamp(0, this.data.Length);
this.currentIndex = Numerics.Clamp(index, 0, this.data.Length);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.ImageSharp.Metadata.Profiles.Icc
Expand All @@ -17,7 +17,7 @@ public int WriteLut8(IccLut value)
{
foreach (float item in value.Values)
{
this.WriteByte((byte)((item * byte.MaxValue) + 0.5f).Clamp(0, byte.MaxValue));
this.WriteByte((byte)Numerics.Clamp((item * byte.MaxValue) + 0.5F, 0, byte.MaxValue));
}

return value.Values.Length;
Expand All @@ -32,7 +32,7 @@ public int WriteLut16(IccLut value)
{
foreach (float item in value.Values)
{
this.WriteUInt16((ushort)((item * ushort.MaxValue) + 0.5f).Clamp(0, ushort.MaxValue));
this.WriteUInt16((ushort)Numerics.Clamp((item * ushort.MaxValue) + 0.5F, 0, ushort.MaxValue));
}

return value.Values.Length * 2;
Expand Down Expand Up @@ -78,7 +78,7 @@ public int WriteClut8(IccClut value)
{
foreach (float item in inArray)
{
count += this.WriteByte((byte)((item * byte.MaxValue) + 0.5f).Clamp(0, byte.MaxValue));
count += this.WriteByte((byte)Numerics.Clamp((item * byte.MaxValue) + 0.5F, 0, byte.MaxValue));
}
}

Expand All @@ -97,7 +97,7 @@ public int WriteClut16(IccClut value)
{
foreach (float item in inArray)
{
count += this.WriteUInt16((ushort)((item * ushort.MaxValue) + 0.5f).Clamp(0, ushort.MaxValue));
count += this.WriteUInt16((ushort)Numerics.Clamp((item * ushort.MaxValue) + 0.5F, 0, ushort.MaxValue));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public int WriteDateTime(DateTime value)
/// <returns>the number of bytes written</returns>
public int WriteVersionNumber(in IccVersion value)
{
int major = value.Major.Clamp(0, byte.MaxValue);
int minor = value.Minor.Clamp(0, 15);
int bugfix = value.Patch.Clamp(0, 15);
int major = Numerics.Clamp(value.Major, 0, byte.MaxValue);
int minor = Numerics.Clamp(value.Minor, 0, 15);
int bugfix = Numerics.Clamp(value.Patch, 0, 15);

int version = (major << 24) | (minor << 20) | (bugfix << 16);
return this.WriteInt32(version);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
Expand Down Expand Up @@ -112,7 +112,7 @@ public int WriteFix16(double value)
const double Max = short.MaxValue + (65535d / 65536d);
const double Min = short.MinValue;

value = value.Clamp(Min, Max);
value = Numerics.Clamp(value, Min, Max);
value *= 65536d;

return this.WriteInt32((int)Math.Round(value, MidpointRounding.AwayFromZero));
Expand All @@ -128,7 +128,7 @@ public int WriteUFix16(double value)
const double Max = ushort.MaxValue + (65535d / 65536d);
const double Min = ushort.MinValue;

value = value.Clamp(Min, Max);
value = Numerics.Clamp(value, Min, Max);
value *= 65536d;

return this.WriteUInt32((uint)Math.Round(value, MidpointRounding.AwayFromZero));
Expand All @@ -144,7 +144,7 @@ public int WriteU1Fix15(double value)
const double Max = 1 + (32767d / 32768d);
const double Min = 0;

value = value.Clamp(Min, Max);
value = Numerics.Clamp(value, Min, Max);
value *= 32768d;

return this.WriteUInt16((ushort)Math.Round(value, MidpointRounding.AwayFromZero));
Expand All @@ -160,7 +160,7 @@ public int WriteUFix8(double value)
const double Max = byte.MaxValue + (255d / 256d);
const double Min = byte.MinValue;

value = value.Clamp(Min, Max);
value = Numerics.Clamp(value, Min, Max);
value *= 256d;

return this.WriteUInt16((ushort)Math.Round(value, MidpointRounding.AwayFromZero));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public int WriteCurveTagDataEntry(IccCurveTagDataEntry value)
count += this.WriteUInt32((uint)value.CurveData.Length);
for (int i = 0; i < value.CurveData.Length; i++)
{
count += this.WriteUInt16((ushort)((value.CurveData[i] * ushort.MaxValue) + 0.5f).Clamp(0, ushort.MaxValue));
count += this.WriteUInt16((ushort)Numerics.Clamp((value.CurveData[i] * ushort.MaxValue) + 0.5F, 0, ushort.MaxValue));
}
}

Expand Down
Loading