Browse Source

Refactor & optimize

pull/3153/head
winscripter 18 hours ago
parent
commit
bc7e30e32d
  1. 9
      src/ImageSharp/Common/Helpers/Numerics.cs
  2. 12
      src/ImageSharp/Formats/Jxl/Processing/Encoder/AuxiliaryOutput/JxlAuxiliaryOutput.cs
  3. 2
      src/ImageSharp/Formats/Jxl/Processing/Encoder/JxlFastLosslessEncoder.cs
  4. 40
      src/ImageSharp/Formats/Jxl/Processing/JxlMath.cs
  5. 2
      src/ImageSharp/Formats/Jxl/Processing/Modular/Transforms/JxlPalette.cs
  6. 23
      src/ImageSharp/Formats/Jxl/Processing/Modular/Transforms/JxlSqueeze.cs
  7. 1
      src/ImageSharp/Formats/Jxl/Processing/RenderPipeline/Epf0Stage.cs
  8. 2
      src/ImageSharp/Formats/Jxl/Processing/RenderPipeline/RenderPipelineStageConfiguration.cs

9
src/ImageSharp/Common/Helpers/Numerics.cs

@ -1033,13 +1033,4 @@ internal static class Numerics
public static nuint Vector512Count<TVector>(int length) public static nuint Vector512Count<TVector>(int length)
where TVector : struct where TVector : struct
=> (uint)length / (uint)Vector512<TVector>.Count; => (uint)length / (uint)Vector512<TVector>.Count;
/// <summary>
/// Computes the average of two integers.
/// </summary>
/// <param name="x">First integer</param>
/// <param name="y">Second integer</param>
/// <returns>The average of x, y.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Average(int x, int y) => (x + y + ((x > y) ? 1 : 0)) >> 1;
} }

12
src/ImageSharp/Formats/Jxl/Processing/Encoder/AuxiliaryOutput/JxlAuxiliaryOutput.cs

@ -67,6 +67,16 @@ internal sealed class JxlAuxiliaryOutput
this.NumberOfBlocks += victim.NumberOfBlocks; this.NumberOfBlocks += victim.NumberOfBlocks;
this.NumberOfSmallBlocks += victim.NumberOfSmallBlocks; this.NumberOfSmallBlocks += victim.NumberOfSmallBlocks;
this.NumberOfDct4x8Blocks += victim.NumberOfDct4x8Blocks;
this.NumberOfAfvBlocks += victim.NumberOfAfvBlocks;
this.NumberOfDct8Blocks += victim.NumberOfDct8Blocks;
this.NumberOfDct8x16Blocks += victim.NumberOfDct8x16Blocks;
this.NumberOfDct8x32Blocks += victim.NumberOfDct8x32Blocks;
this.NumberOfDct16Blocks += victim.NumberOfDct16Blocks;
this.NumberOfDct16x32Blocks += victim.NumberOfDct16x32Blocks;
this.NumberOfDct32Blocks += victim.NumberOfDct32Blocks;
this.NumberOfDct32x64Blocks += victim.NumberOfDct32x64Blocks;
this.NumberOfDct64Blocks += victim.NumberOfDct64Blocks;
this.NumberOfButteraugliIterations += victim.NumberOfButteraugliIterations;
} }
} }

2
src/ImageSharp/Formats/Jxl/Processing/Encoder/JxlFastLosslessEncoder.cs

@ -163,7 +163,7 @@ internal sealed class JxlFastLosslessEncoder
/// A wrapper over the color data of the channel at the specified /// A wrapper over the color data of the channel at the specified
/// position. /// position.
/// </returns> /// </returns>
public Span<T> GetColorChannelData<T>(int x, int y, int width, int height, out long rowOffset) Span<T> GetColorChannelData<T>(int x, int y, int width, int height, out long rowOffset)
where T : unmanaged; where T : unmanaged;
} }

40
src/ImageSharp/Formats/Jxl/Processing/JxlMath.cs

@ -3,7 +3,6 @@
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Common.Helpers;
namespace SixLabors.ImageSharp.Formats.Jxl.Processing; namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
@ -687,25 +686,7 @@ internal static class JxlMath
/// <param name="x">X</param> /// <param name="x">X</param>
/// <param name="y">Y</param> /// <param name="y">Y</param>
/// <returns>Hypotenuse of x and y</returns> /// <returns>Hypotenuse of x and y</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Hypot(float x, float y) => Hypot<float>(x, y);
public static double Hypot(double x, double y)
{
x = Math.Abs(x);
y = Math.Abs(y);
if (x < y)
{
RuntimeUtility.Swap(ref x, ref y);
}
if (x == 0.0)
{
return 0.0;
}
double ratio = y / x;
return x * Math.Sqrt(1 + (ratio * ratio));
}
/// <summary> /// <summary>
/// Computes the hypotenuse of x and y. /// Computes the hypotenuse of x and y.
@ -713,23 +694,26 @@ internal static class JxlMath
/// <param name="x">X</param> /// <param name="x">X</param>
/// <param name="y">Y</param> /// <param name="y">Y</param>
/// <returns>Hypotenuse of x and y</returns> /// <returns>Hypotenuse of x and y</returns>
public static double Hypot(double x, double y) => Hypot<double>(x, y);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Hypot(float x, float y) private static T Hypot<T>(T x, T y)
where T : INumber<T>, IRootFunctions<T>
{ {
x = MathF.Abs(x); x = T.Abs(x);
y = MathF.Abs(y); y = T.Abs(y);
if (x < y) if (x < y)
{ {
RuntimeUtility.Swap(ref x, ref y); (y, x) = (x, y);
} }
if (x == 0.0f) if (x == T.Zero)
{ {
return 0.0f; return T.Zero;
} }
float ratio = y / x; T ratio = y / x;
return x * MathF.Sqrt(1 + (ratio * ratio)); return x * T.Sqrt(T.One + (ratio * ratio));
} }
} }

2
src/ImageSharp/Formats/Jxl/Processing/Modular/Transforms/JxlPalette.cs

@ -492,7 +492,7 @@ internal static class JxlPalette
if (a.Length >= 3) if (a.Length >= 3)
{ {
ave3 = (a[0] + b[0] + a[1] + b[1] + a[2] + b[2]) * (1.21f / 3.0f); ave3 = ((a[0] + b[0]) + (a[1] + b[1]) + (a[2] + b[2])) * (1.21f / 3.0f);
} }
float sumA = 0; float sumA = 0;

23
src/ImageSharp/Formats/Jxl/Processing/Modular/Transforms/JxlSqueeze.cs

@ -27,6 +27,15 @@ internal static class JxlSqueeze
{ {
private const int MaxFirstPreviewSize = 8; private const int MaxFirstPreviewSize = 8;
/// <summary>
/// Computes the average of two integers.
/// </summary>
/// <param name="x">First integer</param>
/// <param name="y">Second integer</param>
/// <returns>The average of x, y.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Average(int x, int y) => (x + y + ((x > y) ? 1 : 0)) >> 1;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int SmoothTendency(int b, int a, int n) private static int SmoothTendency(int b, int a, int n)
{ {
@ -534,10 +543,8 @@ internal static class JxlSqueeze
int c1 = parameter.BeginC; int c1 = parameter.BeginC;
int c2 = parameter.BeginC + parameter.NumC - 1; int c2 = parameter.BeginC + parameter.NumC - 1;
if (c1 < 0 || if ((uint)c1 >= numChannels ||
c1 >= numChannels || (uint)c2 >= numChannels ||
c2 < 0 ||
c2 >= numChannels ||
c2 < c1) c2 < c1)
{ {
throw new InvalidOperationException("Invalid channel range"); throw new InvalidOperationException("Invalid channel range");
@ -654,7 +661,7 @@ internal static class JxlSqueeze
int a = pIn[x2]; int a = pIn[x2];
int b = pIn[x2 + 1]; int b = pIn[x2 + 1];
int avg = Numerics.Average(a, b); int avg = Average(a, b);
pOut[x] = avg; pOut[x] = avg;
int diff = a - b; int diff = a - b;
int nextAvg = avg; int nextAvg = avg;
@ -664,7 +671,7 @@ internal static class JxlSqueeze
int c2 = pIn[x2 + 2]; // actually C, but 1. variable 'c' already defined 2. names should be camelCase int c2 = pIn[x2 + 2]; // actually C, but 1. variable 'c' already defined 2. names should be camelCase
int d = pIn[x2 + 3]; int d = pIn[x2 + 3];
nextAvg = Numerics.Average(c2, d); nextAvg = Average(c2, d);
} }
else if ((inputChannel.Width & 1) != 0) else if ((inputChannel.Width & 1) != 0)
{ {
@ -711,7 +718,7 @@ internal static class JxlSqueeze
{ {
int a = pIn[x]; int a = pIn[x];
int b = pIn[x + oneRowInput]; int b = pIn[x + oneRowInput];
int avg = Numerics.Average(a, b); int avg = Average(a, b);
pOut[x] = avg; pOut[x] = avg;
int diff = a - b; int diff = a - b;
int nextAvg = avg; int nextAvg = avg;
@ -720,7 +727,7 @@ internal static class JxlSqueeze
{ {
int c2 = pIn[x + (2 * oneRowInput)]; // actually C, but 1. variable 'c' already defined 2. names should be camelCase int c2 = pIn[x + (2 * oneRowInput)]; // actually C, but 1. variable 'c' already defined 2. names should be camelCase
int d = pIn[x + (3 * oneRowInput)]; int d = pIn[x + (3 * oneRowInput)];
nextAvg = Numerics.Average(c2, d); nextAvg = Average(c2, d);
} }
else if ((inputChannel.Height & 1) != 0) else if ((inputChannel.Height & 1) != 0)
{ {

1
src/ImageSharp/Formats/Jxl/Processing/RenderPipeline/Epf0Stage.cs

@ -103,7 +103,6 @@ internal sealed class Epf0Stage : RenderPipelineStageBase
Vector256<float> vsm = Vector256.Create<float>(sadMul[ix..]); Vector256<float> vsm = Vector256.Create<float>(sadMul[ix..]);
Vector256<float> inverseSigma = Vector256.Create<float>(rowSigma[bx]) * vsm; Vector256<float> inverseSigma = Vector256.Create<float>(rowSigma[bx]) * vsm;
} }
} }
} }

2
src/ImageSharp/Formats/Jxl/Processing/RenderPipeline/RenderPipelineStageConfiguration.cs

@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Formats.Jxl.Processing.RenderPipeline; namespace SixLabors.ImageSharp.Formats.Jxl.Processing.RenderPipeline;
internal record struct RenderPipelineStageConfiguration(int BorderX, int BorderY, int ShiftX, int ShiftY) internal readonly record struct RenderPipelineStageConfiguration(int BorderX, int BorderY, int ShiftX, int ShiftY)
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RenderPipelineStageConfiguration CreateShiftX(int shift, int border) => new(border, 0, shift, 0); public static RenderPipelineStageConfiguration CreateShiftX(int shift, int border) => new(border, 0, shift, 0);

Loading…
Cancel
Save