Browse Source

Make Fast2DArray internal

af/merge-core
James Jackson-South 9 years ago
parent
commit
fe96988727
  1. 2
      src/ImageSharp/Common/Memory/Fast2DArray{T}.cs
  2. 23
      src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
  3. 5
      src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
  4. 17
      src/ImageSharp/Dithering/Ordered/Bayer.cs
  5. 9
      src/ImageSharp/Dithering/Ordered/IOrderedDither.cs
  6. 17
      src/ImageSharp/Dithering/Ordered/Ordered.cs
  7. 38
      src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs

2
src/ImageSharp/Common/Memory/Fast2DArray{T}.cs

@ -13,7 +13,7 @@ namespace ImageSharp
/// Provides fast access to 2D arrays.
/// </summary>
/// <typeparam name="T">The type of elements in the array.</typeparam>
public struct Fast2DArray<T>
internal struct Fast2DArray<T>
{
/// <summary>
/// The 1D representation of the 2D array.

23
src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs

@ -5,12 +5,11 @@
namespace ImageSharp.Dithering
{
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
/// <summary>
/// The base class for performing effor diffusion based dithering.
/// The base class for performing error diffusion based dithering.
/// </summary>
public abstract class ErrorDiffuser : IErrorDiffuser
{
@ -34,19 +33,24 @@ namespace ImageSharp.Dithering
/// </summary>
private readonly int startingOffset;
/// <summary>
/// The diffusion matrix
/// </summary>
private readonly Fast2DArray<float> matrix;
/// <summary>
/// Initializes a new instance of the <see cref="ErrorDiffuser"/> class.
/// </summary>
/// <param name="matrix">The dithering matrix.</param>
/// <param name="divisor">The divisor.</param>
protected ErrorDiffuser(Fast2DArray<float> matrix, byte divisor)
internal ErrorDiffuser(Fast2DArray<float> matrix, byte divisor)
{
Guard.NotNull(matrix, nameof(matrix));
Guard.MustBeGreaterThan(divisor, 0, nameof(divisor));
this.Matrix = matrix;
this.matrixWidth = this.Matrix.Width;
this.matrixHeight = this.Matrix.Height;
this.matrix = matrix;
this.matrixWidth = this.matrix.Width;
this.matrixHeight = this.matrix.Height;
this.divisorVector = new Vector4(divisor);
this.startingOffset = 0;
@ -62,9 +66,6 @@ namespace ImageSharp.Dithering
}
}
/// <inheritdoc />
public Fast2DArray<float> Matrix { get; }
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor transformed, int x, int y, int width, int height)
@ -87,9 +88,9 @@ namespace ImageSharp.Dithering
if (matrixX > 0 && matrixX < width && matrixY > 0 && matrixY < height)
{
float coefficient = this.Matrix[row, col];
float coefficient = this.matrix[row, col];
// Good to disable here as we are not comparing matematical output.
// Good to disable here as we are not comparing mathematical output.
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (coefficient == 0)
{

5
src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs

@ -12,11 +12,6 @@ namespace ImageSharp.Dithering
/// </summary>
public interface IErrorDiffuser
{
/// <summary>
/// Gets the dithering matrix
/// </summary>
Fast2DArray<float> Matrix { get; }
/// <summary>
/// Transforms the image applying the dither matrix. This method alters the input pixels array
/// </summary>

17
src/ImageSharp/Dithering/Ordered/Bayer.cs

@ -5,13 +5,11 @@
namespace ImageSharp.Dithering.Ordered
{
using System;
/// <summary>
/// Applies error diffusion based dithering using the 4x4 Bayer dithering matrix.
/// <see href="http://www.efg2.com/Lab/Library/ImageProcessing/DHALF.TXT"/>
/// </summary>
public class Bayer : IOrderedDither
public sealed class Bayer : OrderedDither4x4
{
/// <summary>
/// The threshold matrix.
@ -26,15 +24,12 @@ namespace ImageSharp.Dithering.Ordered
{ 255, 127, 223, 95 }
};
/// <inheritdoc />
public Fast2DArray<byte> Matrix { get; } = ThresholdMatrix;
/// <inheritdoc />
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPixel<TColor>
/// <summary>
/// Initializes a new instance of the <see cref="Bayer"/> class.
/// </summary>
public Bayer()
: base(ThresholdMatrix)
{
source.ToXyzwBytes(bytes, 0);
pixels[x, y] = ThresholdMatrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
}
}
}

9
src/ImageSharp/Dithering/Ordered/IOrderedDither.cs

@ -5,18 +5,11 @@
namespace ImageSharp.Dithering
{
using System;
/// <summary>
/// Encapsulates properties and methods required to perfom ordered dithering on an image.
/// </summary>
public interface IOrderedDither
{
/// <summary>
/// Gets the dithering matrix
/// </summary>
Fast2DArray<byte> Matrix { get; }
/// <summary>
/// Transforms the image applying the dither matrix. This method alters the input pixels array
/// </summary>
@ -34,4 +27,4 @@ namespace ImageSharp.Dithering
void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPixel<TColor>;
}
}
}

17
src/ImageSharp/Dithering/Ordered/Ordered.cs

@ -5,13 +5,11 @@
namespace ImageSharp.Dithering.Ordered
{
using System;
/// <summary>
/// Applies error diffusion based dithering using the 4x4 ordered dithering matrix.
/// <see href="https://en.wikipedia.org/wiki/Ordered_dithering"/>
/// </summary>
public class Ordered : IOrderedDither
public sealed class Ordered : OrderedDither4x4
{
/// <summary>
/// The threshold matrix.
@ -26,15 +24,12 @@ namespace ImageSharp.Dithering.Ordered
{ 240, 112, 208, 80 }
};
/// <inheritdoc />
public Fast2DArray<byte> Matrix { get; } = ThresholdMatrix;
/// <inheritdoc />
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPixel<TColor>
/// <summary>
/// Initializes a new instance of the <see cref="Ordered"/> class.
/// </summary>
public Ordered()
: base(ThresholdMatrix)
{
source.ToXyzwBytes(bytes, 0);
pixels[x, y] = ThresholdMatrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
}
}
}

38
src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs

@ -0,0 +1,38 @@
// <copyright file="OrderedDither4x4.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Dithering.Ordered
{
/// <summary>
/// The base class for performing ordered ditheroing using a 4x4 matrix.
/// </summary>
public abstract class OrderedDither4x4 : IOrderedDither
{
/// <summary>
/// The dithering matrix
/// </summary>
private Fast2DArray<byte> matrix;
/// <summary>
/// Initializes a new instance of the <see cref="OrderedDither4x4"/> class.
/// </summary>
/// <param name="matrix">The thresholding matrix. </param>
internal OrderedDither4x4(Fast2DArray<byte> matrix)
{
this.matrix = matrix;
}
/// <inheritdoc />
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPixel<TColor>
{
// TODO: This doesn't really cut it for me.
// I'd rather be using float but we need to add some sort of movalization vector methods to all IPixel implementations
// before we can do that as the vectors all cover different ranges.
source.ToXyzwBytes(bytes, 0);
pixels[x, y] = this.matrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
}
}
}
Loading…
Cancel
Save