-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
358 lines (294 loc) · 8.83 KB
/
main.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Types, scan2track, scancorrelator, utils, math, inputscan, FilterIIRLPBessel, FilterIIRHPBessel;
type
{ TMainForm }
TMainForm = class(TForm)
btInPNGs: TButton;
btOutPNG: TButton;
btInPNG: TButton;
btOutWAV: TButton;
btScan2Track: TButton;
btScansCorrelator: TButton;
btTest: TButton;
chkBlend: TCheckBox;
chkCorrect: TCheckBox;
cbDPI: TComboBox;
cbSR: TComboBox;
cbMethod: TComboBox;
edInputPNG: TEdit;
edOutputPNG: TEdit;
edOutputWAV: TEdit;
Image: TImage;
mmInputPNGs: TMemo;
odInPNGs: TOpenDialog;
odInPNG: TOpenDialog;
pnSettings: TPanel;
sdOutPNG: TSaveDialog;
sdOutWAV: TSaveDialog;
procedure btOutPNGClick(Sender: TObject);
procedure btInPNGClick(Sender: TObject);
procedure btOutWAVClick(Sender: TObject);
procedure btInPNGsClick(Sender: TObject);
procedure btScan2TrackClick(Sender: TObject);
procedure btScansCorrelatorClick(Sender: TObject);
procedure btTestClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FReducRatio: Integer;
FReducFactor: Double;
public
procedure SetReduc(AImageWidth, AImageHeight: Integer);
procedure DrawExtents(AScan: TInputScan);
procedure DrawImage(const Img: TWordDynArray2);
procedure DrawPoints(const APoints: TPointFList; AColor: TColor);
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
{ TMainForm }
procedure TMainForm.btScan2TrackClick(Sender: TObject);
var
s2t: TScan2Track;
begin
s2t := TScan2Track.Create(StrToIntDef(cbSR.Text, 48000), 16, StrToIntDef(cbDPI.Text, 2400));
try
s2t.OutputWAVFileName := edOutputWAV.Text;
s2t.Scan.PNGFileName := edInputPNG.Text;
s2t.Scan.LoadPNG;
DrawImage(s2t.Scan.Image);
s2t.Scan.FindTrack;
DrawExtents(s2t.Scan);
s2t.EvalTrack;
finally
s2t.Free;
end;
end;
procedure TMainForm.btInPNGsClick(Sender: TObject);
begin
if odInPNGs.Execute then
mmInputPNGs.Lines.Assign(odInPNGs.Files);
end;
procedure TMainForm.btInPNGClick(Sender: TObject);
begin
if odInPNG.Execute then
edInputPNG.Text := odInPNG.FileName;
end;
procedure TMainForm.btOutWAVClick(Sender: TObject);
begin
if sdOutWAV.Execute then
edOutputWAV.Text := sdOutWAV.FileName;
end;
procedure TMainForm.btOutPNGClick(Sender: TObject);
begin
if sdOutPNG.Execute then
edOutputPNG.Text := sdOutPNG.FileName;
end;
procedure TMainForm.btScansCorrelatorClick(Sender: TObject);
var
sc: TScanCorrelator;
begin
sc := TScanCorrelator.Create(mmInputPNGs.Lines, StrToIntDef(cbDPI.Text, 2400));
try
sc.Method := TMinimizeMethod(cbMethod.ItemIndex);
sc.OutputPNGFileName := edOutputPNG.Text;
sc.CorrectAngles := chkCorrect.Checked;
sc.RebuildBlended := chkBlend.Checked;
sc.LoadPNGs;
if Length(sc.InputScans) > 0 then
begin
DrawImage(sc.InputScans[0].Image);
DrawExtents(sc.InputScans[0]);
end;
sc.Process;
DrawImage(sc.OutputImage);
if Trim(sc.OutputPNGFileName) <> '' then
sc.Save;
finally
sc.Free;
end;
end;
procedure TMainForm.btTestClick(Sender: TObject);
var
img: TWordDynArray2;
i: Integer;
fn: String;
sc1, sc100, scm: TScanCorrelator;
sl: TStringList;
fltLP: TFilterIIRLPBessel;
fltHP: TFilterIIRHPBessel;
smps: TSmallIntDynArray;
mm: TMinimizeMethod;
begin
SetLength(smps, 48000 * 2);
fltLP := TFilterIIRLPBessel.Create(nil);
fltHP := TFilterIIRHPBessel.Create(nil);
try
fltLP.SampleRate := 48000;
fltLP.FreqCut1 := 1000;
fltLP.Order := 4;
fltHP.SampleRate := 48000;
fltHP.FreqCut1 := 1000;
fltHP.Order := 4;
for i := 0 to 48000 - 1 do
begin
smps[i] := Make16BitSample(fltLP.FilterFilter((i mod 1000) / 1000 * 2.0 - 1.0));
smps[i + 48000] := Make16BitSample(fltHP.FilterFilter((i mod 1000) / 1000 * 2.0 - 1.0));
end;
finally
fltLP.Free;
fltHP.Free;
end;
CreateWAV(1, 16, 48000, 'ut.wav', smps);
SetLength(img, 2048, 2048);
img[512, 16 * 2 + 1024] := High(Word);
img[1024, 128 * 2 + 1024] := High(Word);
img[1536, 64 * 2 + 1024] := High(Word);
img[2047, 256 * 2 + 1024] := High(Word);
for i := 0 to 511 do
begin
img[i + 512, round(herp(0, 16, 128, 64, i / 512) * 2 + 1024)] := High(Word);
img[i + 1024, round(herp(16, 128, 64, 256, i / 512) * 2 + 1024)] := High(Word);
img[i + 1536, round(herp(128, 64, 256, 512, i / 512) * 2 + 1024)] := High(Word);
img[i + 512, round(cerp(0, 16, 128, 64, i / 512) * 2 + 1024) - 256] := High(Word);
img[i + 1024, round(cerp(16, 128, 64, 256, i / 512) * 2 + 1024) - 256] := High(Word);
img[i + 1536, round(cerp(128, 64, 256, 512, i / 512) * 2 + 1024) - 256] := High(Word);
img[i + 512, round(lerp(16, 128, i / 512) * 2 + 1024) - 512] := High(Word);
img[i + 1024, round(lerp(128, 64, i / 512) * 2 + 1024) - 512] := High(Word);
img[i + 1536, round(lerp(64, 256, i / 512) * 2 + 1024) - 512] := High(Word);
end;
DrawImage(img);
fn := GetTempFileName;
sl := TStringList.Create;
try
sc1 := TScanCorrelator.Create(sl, 1);
sc100 := TScanCorrelator.Create(sl, 100);
try
sc1.OutputPNGFileName := fn;
sc100.OutputPNGFileName := fn;
sc1.LoadPNGs;
sc1.Process;
sc1.Save;
sc100.LoadPNGs;
sc100.Process;
sc100.Save;
finally
sc100.Free;
sc1.Free;
end;
sl.Add('data\think_mock.png');
sl.Add('data\think_mock2.png');
sl.Add('data\think_mock3.png');
for mm := Succ(mmNone) to High(TMinimizeMethod) do
begin
scm := TScanCorrelator.Create(sl);
try
scm.OutputPNGFileName := fn;
scm.Method := mm;
scm.LoadPNGs;
scm.Process;
DrawImage(scm.OutputImage);
scm.Save;
finally
scm.Free;
end;
end;
finally
sl.Free;
DeleteFile(fn);
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
pnSettings.ControlStyle := pnSettings.ControlStyle + [csOpaque];
Image.ControlStyle := Image.ControlStyle + [csOpaque];
end;
procedure TMainForm.SetReduc(AImageWidth, AImageHeight: Integer);
begin
FReducRatio := Floor(AImageWidth / Width);
FReducFactor := 1.0 / FReducRatio;
end;
procedure TMainForm.DrawExtents(AScan: TInputScan);
var
C: TCanvas;
cx, cy, sx, sy, rfx, rfy, rcx, rcy, rax, ray: Integer;
begin
C := Image.Picture.Bitmap.Canvas;
C.Brush.Style := bsClear;
C.Pen.Color := clRed;
C.Pen.Style := psDot;
cx := Round(AScan.Center.X * FReducFactor);
cy := Round(AScan.Center.Y * FReducFactor);
sx := Round(AScan.GrooveStartPoint.X * FReducFactor);
sy := Round(AScan.GrooveStartPoint.Y * FReducFactor);
rfx := Round(AScan.FirstGrooveRadius) div FReducRatio;
rfy := Round(AScan.FirstGrooveRadius) div FReducRatio;
rcx := Round(AScan.ConcentricGrooveRadius) div FReducRatio;
rcy := Round(AScan.ConcentricGrooveRadius) div FReducRatio;
rax := Round(C45RpmAdapterSize * 0.5 * AScan.DPI) div FReducRatio;
ray := Round(C45RpmAdapterSize * 0.5 * AScan.DPI) div FReducRatio;
C.Line(cx - 8, cy, cx + 9, cy);
C.Line(cx, cy - 8, cx, cy + 9);
C.Line(sx - 8, sy, sx + 9, sy);
C.Line(sx, sy - 8, sx, sy + 9);
C.EllipseC(cx, cy, rfx, rfy);
C.EllipseC(cx, cy, rcx, rcy);
C.EllipseC(cx, cy, rax, ray);
HorzScrollBar.Position := cx - Width div 2;
VertScrollBar.Position := cy - Height div 2;
Application.ProcessMessages;
end;
procedure TMainForm.DrawImage(const Img: TWordDynArray2);
var
x, y, ix, iy: Integer;
sc: PCardinal;
b: Byte;
acc: Integer;
begin
SetReduc(Length(Img[0]), Length(Img));
Image.Picture.Bitmap.PixelFormat := pf32bit;
Image.Picture.Bitmap.Width := Length(Img[0]) div FReducRatio;
Image.Picture.Bitmap.Height := Length(Img) div FReducRatio;
Image.Picture.Bitmap.BeginUpdate;
try
for y := 0 to Image.Picture.Bitmap.Height - 1 do
begin
sc := Image.Picture.Bitmap.ScanLine[y];
for x := 0 to Image.Picture.Bitmap.Width - 1 do
begin
acc := 0;
for iy := 0 to FReducRatio - 1 do
for ix := 0 to FReducRatio - 1 do
acc += Img[(y * FReducRatio) + iy, (x * FReducRatio) + ix];
b := EnsureRange(Round(acc * (High(Byte) / (High(Word) * Sqr(FReducRatio)))), 0, High(Byte));
sc^ := ToRGB(b, b, b);
Inc(sc);
end;
end;
finally
Image.Picture.Bitmap.EndUpdate;
end;
Application.ProcessMessages;
end;
procedure TMainForm.DrawPoints(const APoints: TPointFList; AColor: TColor);
var
i: Integer;
sc: PCardinal;
begin
Image.Picture.Bitmap.BeginUpdate;
try
for i := 0 to APoints.Count - 1 do
begin
sc := Image.Picture.Bitmap.ScanLine[round(APoints[i].Y) div FReducRatio];
Inc(sc, round(APoints[i].X) div FReducRatio);
sc^ := SwapRB(AColor);
end;
finally
Image.Picture.Bitmap.EndUpdate;
end;
Application.ProcessMessages;
end;
end.