Skip to content

Commit f177b91

Browse files
Replace clamp extensions
1 parent 4565fda commit f177b91

27 files changed

Lines changed: 492 additions & 634 deletions

src/ImageSharp/Common/Extensions/ComparableExtensions.cs

Lines changed: 0 additions & 140 deletions
This file was deleted.

src/ImageSharp/Common/Helpers/Buffer2DUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public static void Convolve4<TPixel>(
5050

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

@@ -93,13 +93,13 @@ public static void Convolve4AndAccumulatePartials(
9393
int radiusX = kernelLength >> 1;
9494
int sourceOffsetColumnBase = column + minColumn;
9595

96-
int offsetY = row.Clamp(minRow, maxRow);
96+
int offsetY = Numerics.Clamp(row, minRow, maxRow);
9797
ref ComplexVector4 sourceRef = ref MemoryMarshal.GetReference(sourceValues.GetRowSpan(offsetY));
9898
ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel);
9999

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

src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ public static void Convolve2DImpl<TPixel>(
133133

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

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

@@ -263,12 +263,12 @@ private static void ConvolveImpl<TPixel>(
263263

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

269269
for (int x = 0; x < matrixWidth; x++)
270270
{
271-
int offsetX = (sourceOffsetColumnBase + x - radiusX).Clamp(minColumn, maxColumn);
271+
int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn);
272272
var currentColor = sourceRowSpan[offsetX].ToVector4();
273273
Vector4Utilities.Premultiply(ref currentColor);
274274
vector += matrix[y, x] * currentColor;

src/ImageSharp/Common/Helpers/ImageMath.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ int GetMaxX(ImageFrame<TPixel> pixels)
248248

249249
topLeft.Y = GetMinY(bitmap);
250250
topLeft.X = GetMinX(bitmap);
251-
bottomRight.Y = (GetMaxY(bitmap) + 1).Clamp(0, height);
252-
bottomRight.X = (GetMaxX(bitmap) + 1).Clamp(0, width);
251+
bottomRight.Y = Numerics.Clamp(GetMaxY(bitmap) + 1, 0, height);
252+
bottomRight.X = Numerics.Clamp(GetMaxX(bitmap) + 1, 0, width);
253253

254254
return GetBoundingRectangle(topLeft, bottomRight);
255255
}

src/ImageSharp/Common/Helpers/SimdUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private static void ConvertNormalizedFloatToByteRemainder(ReadOnlySpan<float> so
190190
}
191191

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

195195
[Conditional("DEBUG")]
196196
private static void VerifyHasVector8(string operation)

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
212212
ImageMetadata metadata = image.Metadata;
213213

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

218218
// Convert from a quality rating to a scaling factor.
219219
int scale;

src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static byte CalculateBitDepth<TPixel>(
101101
byte bitDepth;
102102
if (options.ColorType == PngColorType.Palette)
103103
{
104-
byte quantizedBits = (byte)ImageMath.GetBitsNeededForColorDepth(quantizedFrame.Palette.Length).Clamp(1, 8);
104+
byte quantizedBits = (byte)Numerics.Clamp(ImageMath.GetBitsNeededForColorDepth(quantizedFrame.Palette.Length), 1, 8);
105105
byte bits = Math.Max((byte)options.BitDepth, quantizedBits);
106106

107107
// Png only supports in four pixel depths: 1, 2, 4, and 8 bits when using the PLTE chunk

src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public IccDataReader(byte[] data)
4040
/// <param name="index">The new index position</param>
4141
public void SetIndex(int index)
4242
{
43-
this.currentIndex = index.Clamp(0, this.data.Length);
43+
this.currentIndex = Numerics.Clamp(index, 0, this.data.Length);
4444
}
4545

4646
/// <summary>

src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Lut.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

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

2323
return value.Values.Length;
@@ -32,7 +32,7 @@ public int WriteLut16(IccLut value)
3232
{
3333
foreach (float item in value.Values)
3434
{
35-
this.WriteUInt16((ushort)((item * ushort.MaxValue) + 0.5f).Clamp(0, ushort.MaxValue));
35+
this.WriteUInt16((ushort)Numerics.Clamp((item * ushort.MaxValue) + 0.5F, 0, ushort.MaxValue));
3636
}
3737

3838
return value.Values.Length * 2;
@@ -78,7 +78,7 @@ public int WriteClut8(IccClut value)
7878
{
7979
foreach (float item in inArray)
8080
{
81-
count += this.WriteByte((byte)((item * byte.MaxValue) + 0.5f).Clamp(0, byte.MaxValue));
81+
count += this.WriteByte((byte)Numerics.Clamp((item * byte.MaxValue) + 0.5F, 0, byte.MaxValue));
8282
}
8383
}
8484

@@ -97,7 +97,7 @@ public int WriteClut16(IccClut value)
9797
{
9898
foreach (float item in inArray)
9999
{
100-
count += this.WriteUInt16((ushort)((item * ushort.MaxValue) + 0.5f).Clamp(0, ushort.MaxValue));
100+
count += this.WriteUInt16((ushort)Numerics.Clamp((item * ushort.MaxValue) + 0.5F, 0, ushort.MaxValue));
101101
}
102102
}
103103

src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public int WriteDateTime(DateTime value)
3333
/// <returns>the number of bytes written</returns>
3434
public int WriteVersionNumber(in IccVersion value)
3535
{
36-
int major = value.Major.Clamp(0, byte.MaxValue);
37-
int minor = value.Minor.Clamp(0, 15);
38-
int bugfix = value.Patch.Clamp(0, 15);
36+
int major = Numerics.Clamp(value.Major, 0, byte.MaxValue);
37+
int minor = Numerics.Clamp(value.Minor, 0, 15);
38+
int bugfix = Numerics.Clamp(value.Patch, 0, 15);
3939

4040
int version = (major << 24) | (minor << 20) | (bugfix << 16);
4141
return this.WriteInt32(version);

0 commit comments

Comments
 (0)