diff --git a/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs b/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs
index 5ba759957..13475af09 100644
--- a/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs
+++ b/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs
@@ -377,23 +377,6 @@ namespace ImageSharp.Formats.Jpg
}
}
- ///
- /// Performs division and rounding of a rational number represented by a dividend and a divisior into an integer.
- ///
- /// The dividend
- /// The divisor
- /// The result integer
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int DivideRound(int dividend, int divisor)
- {
- if (dividend >= 0)
- {
- return (dividend + (divisor >> 1)) / divisor;
- }
-
- return -((-dividend + (divisor >> 1)) / divisor);
- }
-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void DivideRoundAll(ref Block8x8F a, ref Block8x8F b)
{
@@ -418,9 +401,11 @@ namespace ImageSharp.Formats.Jpg
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector4 DivideRound(Vector4 dividend, Vector4 divisor)
{
+ // sign(v) = max(min(v, 1), -1)
Vector4 sign = Vector4.Min(dividend, Vector4.One);
sign = Vector4.Max(sign, new Vector4(-1));
+ // AlmostRound(dividend/divisor) = dividend/divisior + 0.5*sign(dividend)
return (dividend / divisor) + (sign * new Vector4(0.5f));
}
}
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs b/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs
index 06882719c..e76a11cec 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs
@@ -21,7 +21,7 @@ namespace ImageSharp.Tests
internal static class ReferenceImplementations
{
///
- /// Transpose 8x8 block stored linearly in a span (inplace)
+ /// Transpose 8x8 block stored linearly in a (inplace)
///
///
internal static void Transpose8x8(MutableSpan data)
@@ -39,7 +39,7 @@ namespace ImageSharp.Tests
}
///
- /// Transpose 8x8 block stored linearly in a span
+ /// Transpose 8x8 block stored linearly in a
///
internal static void Transpose8x8(MutableSpan src, MutableSpan dest)
{
@@ -876,6 +876,14 @@ namespace ImageSharp.Tests
}
}
+ ///
+ /// Reference implementation to test .
+ /// Rounding is done used an integer-based algorithm defined in .
+ ///
+ /// The input block
+ /// The destination block of integers
+ /// The quantization table
+ /// Pointer to
public static unsafe void UnZigDivRoundRational(Block8x8F* src, int* dest, Block8x8F* qt, int* unzigPtr)
{
float* s = (float*)src;
@@ -891,6 +899,12 @@ namespace ImageSharp.Tests
}
}
+ ///
+ /// Rounds a rational number defined as dividend/divisor into an integer
+ ///
+ /// The dividend
+ /// The divisior
+ ///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int RationalRound(int dividend, int divisor)
{