Browse Source

Implement noise shared logic

pull/3153/head
winscripter 23 hours ago
parent
commit
57eadbb3f9
  1. 28
      src/ImageSharp/Formats/Jxl/Processing/JxlNoiseHelper.cs
  2. 14
      src/ImageSharp/Formats/Jxl/Processing/JxlNoiseIndexAndFraction.cs
  3. 14
      src/ImageSharp/Formats/Jxl/Processing/JxlNoiseLevel.cs
  4. 15
      src/ImageSharp/Formats/Jxl/Processing/JxlNoiseParameters.cs

28
src/ImageSharp/Formats/Jxl/Processing/JxlNoiseHelper.cs

@ -0,0 +1,28 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
internal static class JxlNoiseHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JxlNoiseIndexAndFraction IndexAndFraction(float x)
{
const int scaleNumerator = JxlNoiseParameters.NoisePoints - 2;
const float scale = scaleNumerator / 1.0f;
float scaledX = MathF.Max(0f, x * scale);
float floorX = MathF.Floor(scaledX);
float fractionalX = scaledX - floorX;
if (scaledX >= scaleNumerator + 1)
{
floorX = scaleNumerator;
fractionalX = 1f;
}
return new((int)floorX, fractionalX);
}
}

14
src/ImageSharp/Formats/Jxl/Processing/JxlNoiseIndexAndFraction.cs

@ -0,0 +1,14 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
[StructLayout(LayoutKind.Sequential)]
internal struct JxlNoiseIndexAndFraction(int index, float fraction)
{
public int Index = index;
public float Fraction = fraction;
}

14
src/ImageSharp/Formats/Jxl/Processing/JxlNoiseLevel.cs

@ -0,0 +1,14 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
[StructLayout(LayoutKind.Sequential)]
internal struct JxlNoiseLevel(float noiseLevel, float intensity)
{
public float NoiseLevel = noiseLevel;
public float Intensity = intensity;
}

15
src/ImageSharp/Formats/Jxl/Processing/JxlNoiseParameters.cs

@ -0,0 +1,15 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Formats.Jxl.Processing;
internal sealed class JxlNoiseParameters
{
public const int NoisePoints = 8;
public float[] Lookup { get; set; } = new float[NoisePoints];
public bool ContainsAny => this.Lookup.Any(x => MathF.Abs(x) > 1e-3f);
public void Clear() => Array.Fill(this.Lookup, 0f);
}
Loading…
Cancel
Save