-
Notifications
You must be signed in to change notification settings - Fork 1
/
dithering.cpp
240 lines (194 loc) · 5.86 KB
/
dithering.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
[Description("Stucki")]
public sealed class StuckiDithering : ErrorDiffusionDithering
{
#region Constructors
public StuckiDithering()
: base(new byte[,]
{
{
0, 0, 0, 8, 4
},
{
2, 4, 8, 4, 2
},
{
1, 2, 4, 2, 1
}
}, 42, false)
{ }
#endregion
}
[Description("Atkinson")]
public sealed class AtkinsonDithering : ErrorDiffusionDithering
{
#region Constructors
public AtkinsonDithering()
: base(new byte[,]
{
{
0, 0, 1, 1
},
{
1, 1, 1, 0
},
{
0, 1, 0, 0
}
}, 3, true)
{ }
#endregion
}
public sealed class JarvisJudiceNinkeDithering : ErrorDiffusionDithering
{
#region Constructors
public JarvisJudiceNinkeDithering()
: base(new byte[,]
{
{
0, 0, 0, 7, 5
},
{
3, 5, 7, 5, 3
},
{
1, 3, 5, 3, 1
}
}, 48, false)
{ }
#endregion
}
internal static byte ToByte(this int value)
{
if (value < 0)
{
value = 0;
}
else if (value > 255)
{
value = 255;
}
return (byte)value;
}
D:\src\Dithering\src\Dithering\ErrorDiffusionDithering.cs
bool IErrorDiffusion.Prescan
{ get { return false; } }
void IErrorDiffusion.Diffuse(ArgbColor[] data, ArgbColor original, ArgbColor transformed, int x, int y, int width, int height)
{
int redError;
int blueError;
int greenError;
redError = original.R - transformed.R;
blueError = original.G - transformed.G;
greenError = original.B - transformed.B;
for (int row = 0; row < _matrixHeight; row++)
{
int offsetY;
offsetY = y + row;
for (int col = 0; col < _matrixWidth; col++)
{
int coefficient;
int offsetX;
coefficient = _matrix[row, col];
offsetX = x + (col - _startingOffset);
if (coefficient != 0 && offsetX > 0 && offsetX < width && offsetY > 0 && offsetY < height)
{
ArgbColor offsetPixel;
int offsetIndex;
int newR;
int newG;
int newB;
byte r;
byte g;
byte b;
offsetIndex = offsetY * width + offsetX;
offsetPixel = data[offsetIndex];
// if the UseShifting property is set, then bit shift the values by the specified
// divisor as this is faster than integer division. Otherwise, use integer division
if (_useShifting)
{
newR = (redError * coefficient) >> _divisor;
newG = (greenError * coefficient) >> _divisor;
newB = (blueError * coefficient) >> _divisor;
}
else
{
newR = redError * coefficient / _divisor;
newG = greenError * coefficient / _divisor;
newB = blueError * coefficient / _divisor;
}
r = (offsetPixel.R + newR).ToByte();
g = (offsetPixel.G + newG).ToByte();
b = (offsetPixel.B + newB).ToByte();
data[offsetIndex] = ArgbColor.FromArgb(offsetPixel.A, r, g, b);
}
}
}
}
#endregion
}
}
public ArgbColor Transform(ArgbColor[] data, ArgbColor pixel, int x, int y, int width, int height)
{
byte gray;
gray = (byte)(0.299 * pixel.R + 0.587 * pixel.G + 0.114 * pixel.B);
/*
* I'm leaving the alpha channel untouched instead of making it fully opaque
* otherwise the transparent areas become fully black, and I was getting annoyed
* by this when testing images with large swathes of transparency!
*/
return gray < _threshold ? _black : _white;
}
private void ProcessPixels(ArgbColor[] pixelData, Size size, IPixelTransform pixelTransform, IErrorDiffusion dither)
{
for (int row = 0; row < size.Height; row++)
{
for (int col = 0; col < size.Width; col++)
{
int index;
ArgbColor current;
ArgbColor transformed;
index = row * size.Width + col;
current = pixelData[index];
// transform the pixel
if (pixelTransform != null)
{
transformed = pixelTransform.Transform(pixelData, current, col, row, size.Width, size.Height);
pixelData[index] = transformed;
}
else
{
transformed = current;
}
// apply a dither algorithm to this pixel
// assuming it wasn't done before
dither?.Diffuse(pixelData, current, transformed, col, row, size.Width, size.Height);
}
}
}
private Bitmap GetTransformedImage(WorkerData workerData)
{
Bitmap image;
Bitmap result;
ArgbColor[] pixelData;
Size size;
IPixelTransform transform;
IErrorDiffusion dither;
transform = workerData.Transform;
dither = workerData.Dither;
image = workerData.Image;
size = image.Size;
pixelData = image.GetPixelsFrom32BitArgbImage();
if (dither != null && dither.Prescan)
{
// perform the dithering on the source data before
// it is transformed
this.ProcessPixels(pixelData, size, null, dither);
dither = null;
}
// scan each pixel, apply a transform the pixel
// and then dither it
this.ProcessPixels(pixelData, size, transform, dither);
// create the final bitmap
result = pixelData.ToBitmap(size);
return result;
}