-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPV_Grayscale.pas
256 lines (210 loc) · 8.15 KB
/
PV_Grayscale.pas
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
unit PV_Grayscale;
//Lazzy Image Viewer
//github.com/PascalVault
//License: MIT
interface
uses Graphics, Math, PV_Bitmap;
procedure Grayscale(Bmp: TPV_Bitmap; MaxColors: Byte; Dither: TDither = ddFloyd);
procedure BlackWhite(Bmp: TPV_Bitmap; Dither: TDither = ddFloyd);
implementation
function Clip(Val: Extended): Byte;
begin
if Val > 255 then Result := 255
else if Val < 0 then Result := 0
else Result := Round(Val);
end;
procedure _atkinson(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Bill Atkinson dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 2) or (x > Bmp.Width-3) or (y > Bmp.Height-3) then Exit;
DiffR := (1/8) * error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR));
Bmp.SetR(x+2, y , Clip(Bmp[x+2, y ].R + DiffR));
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR));
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR));
Bmp.SetR(x+1, y+1, Clip(Bmp[x+1, y+1].R + DiffR));
Bmp.SetR(x , y+2, Clip(Bmp[x , y+2].R + DiffR));
end;
procedure _jarvis(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Jarvis-Judice-Ninke dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 2) or (x > Bmp.Width-3) or (y > Bmp.Height-3) then Exit;
DiffR := (1/48) * error.R;
Bmp.SetR(x + 1, y , Clip(Bmp[x+1, y ].R + DiffR * 7));
Bmp.SetR(x + 2, y , Clip(Bmp[x+2, y ].R + DiffR * 5));
Bmp.SetR(x - 2, y + 1, Clip(Bmp[x-2, y+1].R + DiffR * 3));
Bmp.SetR(x - 1, y + 1, Clip(Bmp[x-1, y+1].R + DiffR * 5));
Bmp.SetR(x , y + 1, Clip(Bmp[x , y+1].R + DiffR * 7));
Bmp.SetR(x + 1, y + 1, Clip(Bmp[x+1, y+1].R + DiffR * 5));
Bmp.SetR(x + 2, y + 1, Clip(Bmp[x+2, y+1].R + DiffR * 3));
Bmp.SetR(x - 2, y + 2, Clip(Bmp[x-2, y+2].R + DiffR * 1));
Bmp.SetR(x - 1, y + 2, Clip(Bmp[x-1, y+2].R + DiffR * 3));
Bmp.SetR(x , y + 2, Clip(Bmp[x , y+2].R + DiffR * 5));
Bmp.SetR(x + 1, y + 2, Clip(Bmp[x+1, y+2].R + DiffR * 3));
Bmp.SetR(x + 2, y + 2, Clip(Bmp[x+2, y+2].R + DiffR * 1));
end;
procedure _sierra2(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Sierra 2 dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 2) or (x > Bmp.Width-3) or (y > Bmp.Height-3) then Exit;
DiffR := (1/16) * error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR * 4));
Bmp.SetR(x+2, y , Clip(Bmp[x+2, y ].R + DiffR * 3));
Bmp.SetR(x-2, y+1, Clip(Bmp[x-2, y+1].R + DiffR * 1));
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR * 2));
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR * 3));
Bmp.SetR(x+1, y+1, Clip(Bmp[x+1, y+1].R + DiffR * 2));
Bmp.SetR(x+2, y+1, Clip(Bmp[x+2, y+1].R + DiffR * 1));
end;
procedure _sierra3(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Sierra 3 dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 2) or (x > Bmp.Width-3) or (y > Bmp.Height-3) then Exit;
DiffR := (1/32) * error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR * 5));
Bmp.SetR(x+2, y , Clip(Bmp[x+2, y ].R + DiffR * 3));
Bmp.SetR(x-2, y+1, Clip(Bmp[x-2, y+1].R + DiffR * 2));
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR * 4));
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR * 5));
Bmp.SetR(x+1, y+1, Clip(Bmp[x+1, y+1].R + DiffR * 4));
Bmp.SetR(x+2, y+1, Clip(Bmp[x+2, y+1].R + DiffR * 2));
Bmp.SetR(x-1, y+2, Clip(Bmp[x-1, y+2].R + DiffR * 2));
Bmp.SetR(x , y+2, Clip(Bmp[x , y+2].R + DiffR * 3));
Bmp.SetR(x+1, y+2, Clip(Bmp[x+1, y+2].R + DiffR * 2));
end;
procedure _sierra4(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Sierra 2-4a dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 1) or (x > Bmp.Width-1) or (y > Bmp.Height-2) then Exit;
DiffR := (1/4) * error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR * 2));
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR * 1));
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR * 1));
end;
procedure _stucki(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Stucki dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 2) or (x > Bmp.Width-3) or (y > Bmp.Height-3) then Exit;
DiffR := (1/42) * error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR * 8));
Bmp.SetR(x+2, y , Clip(Bmp[x+2, y ].R + DiffR * 4));
Bmp.SetR(x-2, y+1, Clip(Bmp[x-2, y+1].R + DiffR * 2));
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR * 4));
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR * 8));
Bmp.SetR(x+1, y+1, Clip(Bmp[x+1, y+1].R + DiffR * 4));
Bmp.SetR(x+2, y+1, Clip(Bmp[x+2, y+1].R + DiffR * 2));
Bmp.SetR(x-2, y+2, Clip(Bmp[x-2, y+2].R + DiffR * 1));
Bmp.SetR(x-1, y+2, Clip(Bmp[x-1, y+2].R + DiffR * 2));
Bmp.SetR(x , y+2, Clip(Bmp[x , y+2].R + DiffR * 4));
Bmp.SetR(x+1, y+2, Clip(Bmp[x+1, y+2].R + DiffR * 2));
Bmp.SetR(x+2, y+2, Clip(Bmp[x+2, y+2].R + DiffR * 1));
end;
procedure _burkes(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Burkes dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 2) or (x > Bmp.Width-3) or (y > Bmp.Height-2) then Exit;
DiffR := (1/32) * error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR * 8));
Bmp.SetR(x+2, y , Clip(Bmp[x+2, y ].R + DiffR * 4));
Bmp.SetR(x-2, y+1, Clip(Bmp[x-2, y+1].R + DiffR * 2));
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR * 4));
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR * 8));
Bmp.SetR(x+1, y+1, Clip(Bmp[x+1, y+1].R + DiffR * 4));
Bmp.SetR(x+2, y+1, Clip(Bmp[x+2, y+1].R + DiffR * 2));
end;
procedure _floyd(Error: TPixInt; x, y: Integer; Bmp: TPV_Bitmap);
//Floyd-Steinberg dithering
var DiffR, DiffG, DiffB: Extended;
begin
if (x < 1) or (x > Bmp.Width-2) or (y > Bmp.Height-2) then Exit;
DiffR := (1/16) * Error.R;
Bmp.SetR(x+1, y , Clip(Bmp[x+1, y ].R + DiffR * 7) );
Bmp.SetR(x-1, y+1, Clip(Bmp[x-1, y+1].R + DiffR * 3) );
Bmp.SetR(x , y+1, Clip(Bmp[x , y+1].R + DiffR * 5) );
Bmp.SetR(x+1, y+1, Clip(Bmp[x+1, y+1].R + DiffR * 1) );
end;
procedure Grayscale(Bmp: TPV_Bitmap; MaxColors: Byte; Dither: TDither);
var i: Integer;
x,y: Integer;
P: PPix;
R,G,B: Byte;
Error: TPixInt;
Ratio: Extended;
begin
Ratio := 255/MaxColors;
//Bmp.FBitmap.BeginUpdate(False);
for y:=0 to Bmp.Height-1 do begin
P := Bmp.Scanline[y];
for x:=0 to Bmp.Width-1 do begin
//R := Floor((P^.R + P^.G + P^.B)/3);
R := Round(0.2126*P^.R + 0.7152*P^.G + 0.0722*P^.B);
R := Floor(Ratio * Floor(R/Ratio));
Error.R := P^.R-R;
P^.R := R;
case Dither of
ddSierra2 : _Sierra2(Error, x,y, Bmp);
ddSierra3 : _Sierra3(Error, x,y, Bmp);
ddSierra4 : _Sierra4(Error, x,y, Bmp);
ddJarvis : _Jarvis(Error, x,y, Bmp);
ddAtkinson : _Atkinson(Error, x,y, Bmp);
ddStucki : _Stucki(Error, x,y, Bmp);
ddFloyd : _Floyd(Error, x,y, Bmp);
ddBurkes : _Burkes(Error, x,y, Bmp);
end;
Inc(P);
end;
end;
//set G,B to R
for y:=0 to Bmp.Height-1 do
for x:=0 to Bmp.Width-1 do begin
R := Bmp[x,y].R;
Bmp.SetG(x,y, R);
Bmp.SetB(x,y, R);
end;
//Bmp.FBitmap.EndUpdate();
end;
procedure BlackWhite(Bmp: TPV_Bitmap; Dither: TDither);
var i: Integer;
x,y: Integer;
P: PPix;
R,G,B: Byte;
Error: TPixInt;
begin
//Bmp.FBitmap.BeginUpdate(False);
for y:=0 to Bmp.Height-1 do begin
P := Bmp.Scanline[y];
for x:=0 to Bmp.Width-1 do begin
if P^.R > 127 then R := 255
else R := 0;
Error.R := P^.R-R;
P^.R := R;
case Dither of
ddSierra2 : _Sierra2(Error, x,y, Bmp);
ddSierra3 : _Sierra3(Error, x,y, Bmp);
ddSierra4 : _Sierra4(Error, x,y, Bmp);
ddJarvis : _Jarvis(Error, x,y, Bmp);
ddAtkinson : _Atkinson(Error, x,y, Bmp);
ddStucki : _Stucki(Error, x,y, Bmp);
ddFloyd : _Floyd(Error, x,y, Bmp);
ddBurkes : _Burkes(Error, x,y, Bmp);
end;
Inc(P);
end;
end;
//set G,B to R
for y:=0 to Bmp.Height-1 do
for x:=0 to Bmp.Width-1 do begin
R := Bmp[x,y].R;
Bmp.SetG(x,y, R);
Bmp.SetB(x,y, R);
end;
//Bmp.FBitmap.EndUpdate();
end;
end.