Skip to content

Commit

Permalink
Add cluster dot dither
Browse files Browse the repository at this point in the history
  • Loading branch information
hajduakos committed Nov 3, 2024
1 parent 89c8851 commit 29d8fce
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 1 deletion.
6 changes: 6 additions & 0 deletions FilterLib.Tests/FilterTests/DitherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ internal static IEnumerable<TestCaseData> Data()
yield return new TestCaseData("Grayscale_30_59_11.bmp", "BayerDither_2_5_bw.bmp", new BayerDitherFilter(2, 5), 1);
yield return new TestCaseData("Grayscale_30_59_11.bmp", "BayerDither_4_4_bw.bmp", new BayerDitherFilter(4, 4), 1);

yield return new TestCaseData("_input.bmp", "ClusterDotDither_2.bmp", new ClusterDotDitherFilter(2), 1);
yield return new TestCaseData("_input.bmp", "ClusterDotDither_4.bmp", new ClusterDotDitherFilter(4), 1);
yield return new TestCaseData("_input.bmp", "_input.bmp", new ClusterDotDitherFilter(256), 1);
yield return new TestCaseData("Grayscale_30_59_11.bmp", "ClusterDotDither_2_bw.bmp", new ClusterDotDitherFilter(2), 1);
yield return new TestCaseData("Grayscale_30_59_11.bmp", "ClusterDotDither_4_bw.bmp", new ClusterDotDitherFilter(4), 1);

yield return new TestCaseData("_input.bmp", "AtkinsonDither_2.bmp", new AtkinsonDitherFilter(2), 1);
yield return new TestCaseData("_input.bmp", "AtkinsonDither_4.bmp", new AtkinsonDitherFilter(4), 1);
yield return new TestCaseData("_input.bmp", "_input.bmp", new AtkinsonDitherFilter(256), 1);
Expand Down
3 changes: 3 additions & 0 deletions FilterLib.Tests/FilterTests/ToStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ internal static IEnumerable<TestCaseData> Dither()
yield return new TestCaseData(
new BurkesDitherFilter(123),
"BurkesDitherFilter(Levels: 123)");
yield return new TestCaseData(
new ClusterDotDitherFilter(123),
"ClusterDotDitherFilter(Levels: 123)");
yield return new TestCaseData(
new FanDitherFilter(123),
"FanDitherFilter(Levels: 123)");
Expand Down
2 changes: 1 addition & 1 deletion FilterLib.Tests/ReflectiveApiTests/ApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ApiTests
{
[Test]
public void TestListFilters() =>
Assert.That(ReflectiveApi.GetFilterTypes().Count(), Is.EqualTo(73));
Assert.That(ReflectiveApi.GetFilterTypes().Count(), Is.EqualTo(74));

[Test]
public void TestListBlends() =>
Expand Down
12 changes: 12 additions & 0 deletions FilterLib.Tests/ReflectiveApiTests/DitherApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public void TestBayerDither()

[Test]
public void TestBayerDitherParCnt() => Assert.That(Common.ParamCount(typeof(BayerDitherFilter)), Is.EqualTo(2));
[Test]
public void TestClusterDotDither()
{
IFilter f = ReflectiveApi.ConstructFilterByName("ClusterDotDither");
Assert.That(f, Is.InstanceOf<ClusterDotDitherFilter>());
ReflectiveApi.SetFilterPropertyByName(f, "Levels", "50");
ClusterDotDitherFilter ff = f as ClusterDotDitherFilter;
Assert.That(ff.Levels, Is.EqualTo(50));
}

[Test]
public void TestClusterDotDitherParCnt() => Assert.That(Common.ParamCount(typeof(ClusterDotDitherFilter)), Is.EqualTo(1));

[Test]
public void TestAtkinsonDither()
Expand Down
Binary file added FilterLib.Tests/TestImages/ClusterDotDither_2.bmp
Binary file not shown.
Binary file not shown.
Binary file added FilterLib.Tests/TestImages/ClusterDotDither_4.bmp
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions FilterLib/Filters/Dither/ClusterDotDitherFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace FilterLib.Filters.Dither
{
/// <summary>
/// Cluster dot dither is an ordered dither with a specifically defined matrix
/// </summary>
[Filter]
public class ClusterDotDitherFilter : OrderedDitherFilterBase
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="levels">Number of levels [2:256]</param>
public ClusterDotDitherFilter(int levels = 256) : base(levels, new ClusterDotDitherMatrix()) { }
}
}
40 changes: 40 additions & 0 deletions FilterLib/Filters/Dither/ClusterDotDitherMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace FilterLib.Filters.Dither
{
/// <summary>
/// Cluster dot dither matrix
/// </summary>
public sealed class ClusterDotDitherMatrix : IOrderedDitherMatrix
{
private readonly float[,] matrix;

/// <summary>
/// Constructor
/// </summary>
public ClusterDotDitherMatrix()
{
matrix = new float[8, 8]
{
{ 24, 8, 22, 30, 34, 44, 42, 32 },
{ 10, 0, 6, 20, 46, 58, 56, 40 },
{ 12, 2, 4, 18, 48, 60, 62, 54 },
{ 26, 14, 16, 28, 36, 50, 52, 38 },
{ 35, 45, 43, 33, 25, 9, 23, 31 },
{ 47, 59, 57, 41, 11, 1, 7, 21 },
{ 49, 61, 63, 55, 13, 3, 5, 19 },
{ 37, 51, 53, 39, 27, 15, 17, 29 },
};
for (int x = 0; x < matrix.GetLength(0); x++)
for (int y = 0; y < matrix.GetLength(1); y++)
matrix[x, y] /= 64f;
}

/// <inheritdoc/>
public float this[int x, int y] => matrix[x, y];

/// <inheritdoc/>
public int Width { get { return matrix.GetLength(0); } }

/// <inheritdoc/>
public int Height { get { return matrix.GetLength(1); } }
}
}

0 comments on commit 29d8fce

Please sign in to comment.