-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStuckiDithering.cs
95 lines (79 loc) · 3 KB
/
StuckiDithering.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
This file implements error pushing of dithering via (Peter) Stucki kernel.
This is free and unencumbered software released into the public domain.
*/
class StuckiDithering : DitheringBase
{
public StuckiDithering(FindColor colorfunc) : base(colorfunc)
{
this.methodLongName = "Stucki";
this.fileNameAddition = "_STU";
}
override protected void PushError(int x, int y, short[] quantError)
{
// Push error
// X 8/42 4/42
// 2/42 4/42 8/42 4/42 2/42
// 1/42 2/42 4/42 2/42 1/42
int xMinusOne = x - 1;
int xMinusTwo = x - 2;
int xPlusOne = x + 1;
int xPlusTwo = x + 2;
int yPlusOne = y + 1;
int yPlusTwo = y + 2;
// Current row
int currentRow = y;
if (this.IsValidCoordinate(xPlusOne, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xPlusOne, currentRow, quantError, 8.0f / 42.0f);
}
if (this.IsValidCoordinate(xPlusTwo, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xPlusTwo, currentRow, quantError, 4.0f / 42.0f);
}
// Next row
currentRow = yPlusOne;
if (this.IsValidCoordinate(xMinusTwo, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xMinusTwo, currentRow, quantError, 2.0f / 42.0f);
}
if (this.IsValidCoordinate(xMinusOne, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xMinusOne, currentRow, quantError, 4.0f / 42.0f);
}
if (this.IsValidCoordinate(x, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(x, currentRow, quantError, 8.0f / 42.0f);
}
if (this.IsValidCoordinate(xPlusOne, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xPlusOne, currentRow, quantError, 4.0f / 42.0f);
}
if (this.IsValidCoordinate(xPlusTwo, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xPlusTwo, currentRow, quantError, 2.0f / 42.0f);
}
// Next row
currentRow = yPlusTwo;
if (this.IsValidCoordinate(xMinusTwo, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xMinusTwo, currentRow, quantError, 1.0f / 42.0f);
}
if (this.IsValidCoordinate(xMinusOne, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xMinusOne, currentRow, quantError, 2.0f / 42.0f);
}
if (this.IsValidCoordinate(x, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(x, currentRow, quantError, 4.0f / 42.0f);
}
if (this.IsValidCoordinate(xPlusOne, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xPlusOne, currentRow, quantError, 2.0f / 42.0f);
}
if (this.IsValidCoordinate(xPlusTwo, currentRow))
{
this.ModifyImageWithErrorAndMultiplier(xPlusTwo, currentRow, quantError, 1.0f / 42.0f);
}
}
}