-
Notifications
You must be signed in to change notification settings - Fork 33
/
HGM.BlurGaussian.pas
166 lines (138 loc) · 3.56 KB
/
HGM.BlurGaussian.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
unit HGM.BlurGaussian;
interface
uses
Windows, Graphics, SysUtils;
type
PRGBTriple = ^TRGBTriple;
TRGBTriple = packed record
R: Byte;
G: Byte;
B: Byte;
end;
PRow = ^TRow;
TRow = array[0..1000000] of TRGBTriple;
PPRows = ^TPRows;
TPRows = array[0..1000000] of PRow;
const
MaxKernelSize = 100;
type
TKernelSize = 1..MaxKernelSize;
TKernel = record
Size: TKernelSize;
Weights: array[-MaxKernelSize..MaxKernelSize] of Single;
end;
procedure GBlur(BMP: TBitmap; Radius: Double);
implementation
function TrimValue(Lower, Upper, Value: Integer): Integer; overload;
begin
if (Value <= Upper) and (Value >= Lower) then
Result := Value
else if Value > Upper then
Result := Upper
else
Result := Lower;
end;
function TrimValue(Lower, Upper: Integer; Value: Double): Integer; overload;
begin
if (Value < Upper) and (Value >= Lower) then
Result := Trunc(Value)
else if Value > Upper then
Result := Upper
else
Result := Lower;
end;
procedure GBlur(BMP: TBitmap; Radius: Double);
var
Row, Col: Integer;
Rows: PPRows;
K: TKernel;
ACol: PRow;
P: PRow;
procedure BlurRow(var Row: array of TRGBTriple; K: TKernel; P: PRow);
var
j, n: Integer;
tr, tg, tb: Double;
w: Double;
begin
for j := 0 to High(Row) do
begin
tb := 0;
tg := 0;
tr := 0;
for n := -K.Size to K.Size do
begin
w := K.Weights[n];
with Row[TrimValue(0, High(Row), j - n)] do
begin
tb := tb + w * b;
tg := tg + w * g;
tr := tr + w * r;
end;
end;
with P[j] do
begin
b := TrimValue(0, 255, tb);
g := TrimValue(0, 255, tg);
r := TrimValue(0, 255, tr);
end;
end;
Move(P[0], Row[0], (High(Row) + 1) * Sizeof(TRGBTriple));
end;
procedure MakeGaussianKernel(var K: TKernel; Radius: Double; MaxData, DataGranularity: Double);
var
j: Integer;
temp, delta: Double;
KernelSize: TKernelSize;
begin
for j := Low(K.Weights) to High(K.Weights) do
begin
temp := j / Radius;
K.Weights[j] := exp(-temp * temp / 2);
end;
temp := 0;
for j := Low(K.Weights) to High(K.Weights) do
temp := temp + K.Weights[j];
for j := Low(K.Weights) to High(K.Weights) do
K.Weights[j] := K.Weights[j] / temp;
KernelSize := MaxKernelSize;
delta := DataGranularity / (2 * MaxData);
temp := 0;
while (temp < delta) and (KernelSize > 1) do
begin
temp := temp + 2 * K.Weights[KernelSize];
Dec(KernelSize);
end;
K.Size := KernelSize;
temp := 0;
for j := -K.Size to K.Size do
temp := temp + K.Weights[j];
for j := -K.Size to K.Size do
K.Weights[j] := K.Weights[j] / temp;
end;
begin
if (BMP.HandleType <> bmDIB) or (BMP.PixelFormat <> pf24Bit) then
begin
raise Exception.Create('Íåîáõîäèìî 24-áèòíîå èçîáðàæåíèå');
end;
MakeGaussianKernel(K, Radius, 255, 1);
GetMem(Rows, BMP.Height * SizeOf(PRow));
GetMem(ACol, BMP.Height * SizeOf(TRGBTriple));
for Row := 0 to BMP.Height - 1 do
Rows[Row] := BMP.Scanline[Row];
P := AllocMem(BMP.Width * SizeOf(TRGBTriple));
for Row := 0 to BMP.Height - 1 do
BlurRow(Slice(Rows[Row]^, BMP.Width), K, P);
ReAllocMem(P, BMP.Height * SizeOf(TRGBTriple));
for Col := 0 to BMP.Width - 1 do
begin
for Row := 0 to BMP.Height - 1 do
ACol[Row] := Rows[Row][Col];
BlurRow(Slice(ACol^, BMP.Height), K, P);
for Row := 0 to BMP.Height - 1 do
Rows[Row][Col] := ACol[Row];
end;
FreeMem(Rows);
FreeMem(ACol);
ReAllocMem(P, 0);
end;
end.