Browse Source

Update OrderedDither.cs

pull/1568/head
James Jackson-South 5 years ago
parent
commit
5f4a7accfb
  1. 26
      src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs

26
src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs

@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
/// Initializes a new instance of the <see cref="OrderedDither"/> struct. /// Initializes a new instance of the <see cref="OrderedDither"/> struct.
/// </summary> /// </summary>
/// <param name="length">The length of the matrix sides</param> /// <param name="length">The length of the matrix sides</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public OrderedDither(uint length) public OrderedDither(uint length)
{ {
DenseMatrix<uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length); DenseMatrix<uint> ditherMatrix = OrderedDitherFactory.CreateDitherMatrix(length);
@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
=> !(left == right); => !(left == right);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public void ApplyQuantizationDither<TFrameQuantizer, TPixel>( public void ApplyQuantizationDither<TFrameQuantizer, TPixel>(
ref TFrameQuantizer quantizer, ref TFrameQuantizer quantizer,
ImageFrame<TPixel> source, ImageFrame<TPixel> source,
@ -124,7 +124,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
} }
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public void ApplyPaletteDither<TPaletteDitherImageProcessor, TPixel>( public void ApplyPaletteDither<TPaletteDitherImageProcessor, TPixel>(
in TPaletteDitherImageProcessor processor, in TPaletteDitherImageProcessor processor,
ImageFrame<TPixel> source, ImageFrame<TPixel> source,
@ -145,12 +145,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
} }
// Spread assumes an even colorspace distribution and precision. // Spread assumes an even colorspace distribution and precision.
// Cubed root used because we always compare to Rgb. // TODO: Cubed root is currently used to represent 3 color channels
// but we should introduce something to PixelTypeInfo.
// https://bisqwit.iki.fi/story/howto/dither/jy/ // https://bisqwit.iki.fi/story/howto/dither/jy/
// https://en.wikipedia.org/wiki/Ordered_dithering#Algorithm // https://en.wikipedia.org/wiki/Ordered_dithering#Algorithm
internal static int CalculatePaletteSpread(int colors) => (int)(255 / (Math.Pow(colors, 1.0 / 3) - 1)); internal static int CalculatePaletteSpread(int colors)
=> (int)(255 / Math.Max(1, Math.Pow(colors, 1.0 / 3) - 1));
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
internal TPixel Dither<TPixel>( internal TPixel Dither<TPixel>(
TPixel source, TPixel source,
int x, int x,
@ -181,7 +183,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
=> obj is OrderedDither dither && this.Equals(dither); => obj is OrderedDither dither && this.Equals(dither);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public bool Equals(OrderedDither other) public bool Equals(OrderedDither other)
=> this.thresholdMatrix.Equals(other.thresholdMatrix) && this.modulusX == other.modulusX && this.modulusY == other.modulusY; => this.thresholdMatrix.Equals(other.thresholdMatrix) && this.modulusX == other.modulusX && this.modulusY == other.modulusY;
@ -190,7 +192,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
=> this.Equals((object)other); => this.Equals((object)other);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public override int GetHashCode() public override int GetHashCode()
=> HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY); => HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY);
@ -205,7 +207,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
private readonly Rectangle bounds; private readonly Rectangle bounds;
private readonly int spread; private readonly int spread;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public QuantizeDitherRowOperation( public QuantizeDitherRowOperation(
ref TFrameQuantizer quantizer, ref TFrameQuantizer quantizer,
in OrderedDither dither, in OrderedDither dither,
@ -221,7 +223,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
this.spread = CalculatePaletteSpread(destination.Palette.Length); this.spread = CalculatePaletteSpread(destination.Palette.Length);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int y) public void Invoke(int y)
{ {
ref TFrameQuantizer quantizer = ref Unsafe.AsRef(this.quantizer); ref TFrameQuantizer quantizer = ref Unsafe.AsRef(this.quantizer);
@ -251,7 +253,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
private readonly float scale; private readonly float scale;
private readonly int spread; private readonly int spread;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public PaletteDitherRowOperation( public PaletteDitherRowOperation(
in TPaletteDitherImageProcessor processor, in TPaletteDitherImageProcessor processor,
in OrderedDither dither, in OrderedDither dither,
@ -266,7 +268,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
this.spread = CalculatePaletteSpread(processor.Palette.Length); this.spread = CalculatePaletteSpread(processor.Palette.Length);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int y) public void Invoke(int y)
{ {
ref TPaletteDitherImageProcessor processor = ref Unsafe.AsRef(this.processor); ref TPaletteDitherImageProcessor processor = ref Unsafe.AsRef(this.processor);

Loading…
Cancel
Save