Skip to content

Commit 5113da5

Browse files
committed
Added ScaleToDouble to the quantum scalers.
1 parent 0826081 commit 5113da5

File tree

11 files changed

+107
-15
lines changed

11 files changed

+107
-15
lines changed

src/Magick.NET.Core/Scaler/ByteQuantumScaler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ public byte ScaleFromUnsignedShort(ushort value)
1818
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1919
public byte ScaleToByte(byte value)
2020
=> value;
21+
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
public double ScaleToDouble(byte value)
24+
=> value / 255.0;
2125
}

src/Magick.NET.Core/Scaler/FloatQuantumScaler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public byte ScaleToByte(float value)
2727

2828
return (byte)(scaledValue + 0.5f);
2929
}
30+
31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32+
public double ScaleToDouble(float value)
33+
=> value / 65535.0;
3034
}

src/Magick.NET.Core/Scaler/IQuantumScaler{TQuantumType}.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public interface IQuantumScaler<TQuantumType>
3232
/// <param name="value">The value to scale.</param>
3333
/// <returns>The value scaled to a byte.</returns>
3434
byte ScaleToByte(TQuantumType value);
35+
36+
/// <summary>
37+
/// Scales the specified value to a double (range is 0.0 to 1.0).
38+
/// </summary>
39+
/// <param name="value">The value to scale.</param>
40+
/// <returns>The value scaled to a double.</returns>
41+
double ScaleToDouble(TQuantumType value);
3542
}

src/Magick.NET.Core/Scaler/UnsignedShortQuantumScaler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ public ushort ScaleFromUnsignedShort(ushort value)
1818
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1919
public byte ScaleToByte(ushort value)
2020
=> (byte)((value + 128U - ((value + 128U) >> 8)) >> 8);
21+
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
public double ScaleToDouble(ushort value)
24+
=> value / 65535.0;
2125
}

src/Magick.NET/Colors/ColorGray.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ public ColorGray(double shade)
3535
private ColorGray(IMagickColor<QuantumType> color)
3636
: base(color)
3737
{
38+
var quantum = QuantumScalerFactory.Create<QuantumType>();
3839
_shade =
39-
(0.212656 * Quantum.ScaleToDouble(color.R)) +
40-
(0.715158 * Quantum.ScaleToDouble(color.G)) +
41-
(0.072186 * Quantum.ScaleToDouble(color.B));
40+
(0.212656 * quantum.ScaleToDouble(color.R)) +
41+
(0.715158 * quantum.ScaleToDouble(color.G)) +
42+
(0.072186 * quantum.ScaleToDouble(color.B));
4243
}
4344

4445
/// <summary>

src/Magick.NET/MagickImage.CloneMutator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,18 +611,19 @@ public void SparseColor(Channels channels, SparseColorMethod method, IEnumerable
611611

612612
var arguments = new List<double>();
613613

614+
var quantum = QuantumScalerFactory.Create<QuantumType>();
614615
foreach (var arg in args)
615616
{
616617
arguments.Add(arg.X);
617618
arguments.Add(arg.Y);
618619
if (hasRed)
619-
arguments.Add(Quantum.ScaleToDouble(arg.Color.R));
620+
arguments.Add(quantum.ScaleToDouble(arg.Color.R));
620621
if (hasGreen)
621-
arguments.Add(Quantum.ScaleToDouble(arg.Color.G));
622+
arguments.Add(quantum.ScaleToDouble(arg.Color.G));
622623
if (hasBlue)
623-
arguments.Add(Quantum.ScaleToDouble(arg.Color.B));
624+
arguments.Add(quantum.ScaleToDouble(arg.Color.B));
624625
if (hasAlpha)
625-
arguments.Add(Quantum.ScaleToDouble(arg.Color.A));
626+
arguments.Add(quantum.ScaleToDouble(arg.Color.A));
626627
}
627628

628629
Throw.IfTrue(arguments.Count == 0, nameof(args), "Value cannot be empty");

src/Magick.NET/Quantum.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,4 @@ internal static QuantumType ConvertFromInteger(int value)
7070

7171
internal static QuantumType ScaleToQuantum(double value)
7272
=> (QuantumType)Math.Min(Math.Max(0, value * Max), Max);
73-
74-
internal static double ScaleToDouble(QuantumType value)
75-
=> (1.0 / Max) * value;
7673
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using ImageMagick;
5+
using Xunit;
6+
7+
namespace Magick.NET.Core.Tests;
8+
9+
public partial class ByteQuantumScalerTests
10+
{
11+
public class TheScaleToDoubleMethod
12+
{
13+
[Theory]
14+
[InlineData(byte.MinValue, 0.0)]
15+
[InlineData(byte.MaxValue, 1.0)]
16+
public void ShouldScaleByteToDouble(byte input, double output)
17+
{
18+
var scaler = new ByteQuantumScaler();
19+
var result = scaler.ScaleToDouble(input);
20+
21+
Assert.Equal(output, result);
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using ImageMagick;
5+
using Xunit;
6+
7+
namespace Magick.NET.Core.Tests;
8+
9+
public partial class FloatQuantumScalerTests
10+
{
11+
public class TheScaleToDoubleMethod
12+
{
13+
[Theory]
14+
[InlineData(ushort.MinValue, 0.0)]
15+
[InlineData(ushort.MaxValue, 1.0)]
16+
public void ShouldScaleFloatToDouble(float input, double output)
17+
{
18+
var scaler = new FloatQuantumScaler();
19+
var result = scaler.ScaleToDouble(input);
20+
21+
Assert.Equal(output, result);
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using ImageMagick;
5+
using Xunit;
6+
7+
namespace Magick.NET.Core.Tests;
8+
9+
public partial class UnsignedShortQuantumScalerTests
10+
{
11+
public class TheScaleToDoubleMethod
12+
{
13+
[Theory]
14+
[InlineData(ushort.MinValue, 0.0)]
15+
[InlineData(ushort.MaxValue, 1.0)]
16+
public void ShouldScaleUnsignedShortToDouble(ushort input, double output)
17+
{
18+
var scaler = new UnsignedShortQuantumScaler();
19+
var result = scaler.ScaleToDouble(input);
20+
21+
Assert.Equal(output, result);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)