-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputscan.pas
489 lines (388 loc) · 12.1 KB
/
inputscan.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
unit inputscan;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Types, Math, Graphics, FPReadPNG, FPImage, PNGComn,
utils, powell;
type
TInterpSource = (isImage, isXGradient, isYGradient);
{ TInputScan }
TInputScan = class
private
FPNGFileName: String;
FDPI: Integer;
FPointsPerRevolution: Integer;
FRadiansPerRevolutionPoint: Double;
FSilent: Boolean;
FSinCosLUT: TPointDDynArray;
FCenter: TPointD;
FConcentricGrooveRadius: Double;
FFirstGrooveRadius: Double;
FGrooveStartAngle: Double;
FGrooveStartPoint: TPointD;
FCenterQuality: Double;
FImage: TWordDynArray2;
procedure SetRevolutionFromDPI(ADPI: Integer);
function GetPNGShortName: String;
procedure GradientEvalConcentricGroove(const arg: TVector; var func: Double; grad: TVector; obj: Pointer);
procedure GradientEvalCenter(const arg: TVector; var func: Double; grad: TVector; obj: Pointer);
function GetHeight: Integer;
function GetWidth: Integer;
procedure FindCenter;
procedure FindConcentricGroove;
procedure FindGrooveStart;
public
constructor Create(ADefaultDPI: Integer = 2400; ASilent: Boolean = False);
destructor Destroy; override;
procedure LoadPNG;
procedure FindTrack;
function InRangePointD(Y, X: Double): Boolean; inline;
function GetPointD(Y, X: Double; Source: TInterpSource): Double; inline;
property PNGFileName: String read FPNGFileName write FPNGFileName;
property PNGShortName: String read GetPNGShortName;
property DPI: Integer read FDPI;
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
property Center: TPointD read FCenter write FCenter;
property ConcentricGrooveRadius: Double read FConcentricGrooveRadius;
property FirstGrooveRadius: Double read FFirstGrooveRadius;
property GrooveStartAngle: Double read FGrooveStartAngle;
property GrooveStartPoint: TPointD read FGrooveStartPoint;
property PointsPerRevolution: Integer read FPointsPerRevolution;
property RadiansPerRevolutionPoint: Double read FRadiansPerRevolutionPoint;
property CenterQuality: Double read FCenterQuality;
property Image: TWordDynArray2 read FImage;
end;
TInputScanDynArray = array of TInputScan;
{ TScanImage }
TScanImage = class(TFPCustomImage)
private
FImage: TWordDynArray2;
protected
procedure SetInternalPixel(x,y:integer; Value:integer); override;
function GetInternalPixel(x,y:integer) : integer; override;
procedure SetInternalColor (x,y:integer; const Value:TFPColor); override;
public
property Image: TWordDynArray2 read FImage write FImage;
end;
{ TDPIAwareReaderPNG }
TDPIAwareReaderPNG = class(TFPReaderPNG)
private
FDPI: TPoint;
protected
procedure HandleChunk; override;
public
constructor Create; override;
property DPI: TPoint read FDPI;
end;
implementation
const
CAreaBegin = 0;
CAreaEnd = (C45RpmInnerSize + C45RpmAdapterSize) * 0.5;
CRadiusXOffsets: array[TValueSign] of Double = (-C45RpmLeadOutGrooveWidth, 0, C45RpmLeadOutGrooveWidth);
CRadiusYFactors: array[TValueSign] of Double = (1, -2, 1);
{ TInputScan }
procedure TInputScan.GradientEvalCenter(const arg: TVector; var func: Double; grad: TVector; obj: Pointer);
var
ix, iy, radiusOuter: Integer;
x, y: Double;
begin
func := 0;
if Assigned(grad) then
begin
grad[0] := 0;
grad[1] := 0;
end;
radiusOuter := Round(CAreaEnd * FDPI * 0.5);
for iy := -radiusOuter to radiusOuter do
begin
y := iy + arg[1];
for ix := -radiusOuter to radiusOuter do
begin
x := ix + arg[0];
if (Sqrt(Sqr(ix) + Sqr(iy)) <= radiusOuter) and InRangePointD(y, x) then
begin
func -= GetPointD(y, x, isImage);
if Assigned(grad) then
begin
grad[0] -= GetPointD(y, x, isXGradient);
grad[1] -= GetPointD(y, x, isYGradient);
end;
end;
end;
end;
//WriteLn(arg[0]:20:9,arg[1]:20:9,func:20:9);
end;
procedure TInputScan.FindCenter;
var
x: TVector;
rOut: Double;
begin
rOut := C45RpmOuterSize * FDPI * 0.5;
SetLength(x, 2);
x[0] := FCenter.X;
x[1] := FCenter.Y;
if (x[0] <> rOut) and (x[1] <> rOut) then
FCenterQuality := -BFGSMinimize(@GradientEvalCenter, x, 1e-6);
FCenter.X := x[0];
FCenter.Y := x[1];
end;
procedure TInputScan.GradientEvalConcentricGroove(const arg: TVector; var func: Double; grad: TVector; obj: Pointer);
var
iradius, ilut, trackWidth: Integer;
radius, sn, cs, x, y, gimgx, gimgy: Double;
vs: TValueSign;
begin
func := 0.0;
if Assigned(grad) then
begin
grad[0] := 0.0;
grad[1] := 0.0;
grad[2] := 0.0;
end;
trackWidth := Floor(C45RpmLeadOutGrooveWidth * FDPI * 0.5);
for ilut := 0 to FPointsPerRevolution - 1 do
begin
cs := FSinCosLUT[ilut].X;
sn := FSinCosLUT[ilut].Y;
for iradius := -trackWidth to trackWidth do
for vs := Low(TValueSign) to High(TValueSign) do
begin
radius := arg[0] + iradius + CRadiusXOffsets[vs] * FDPI;
x := cs * radius + arg[1];
y := sn * radius + arg[2];
if InRangePointD(y, x) then
begin
func += GetPointD(y, x, isImage) * CRadiusYFactors[vs];
if Assigned(grad) then
begin
gimgx := GetPointD(y, x, isXGradient) * CRadiusYFactors[vs];
gimgy := GetPointD(y, x, isYGradient) * CRadiusYFactors[vs];
grad[0] += (gimgx * cs + gimgy * sn);
grad[1] += gimgx;
grad[2] += gimgy;
end;
end;
end;
end;
//WriteLn(arg[0]:12:3,arg[1]:12:3,arg[2]:12:3,func:12:3);
end;
procedure TInputScan.SetRevolutionFromDPI(ADPI: Integer);
begin
FPointsPerRevolution := Ceil(Pi * C45RpmOuterSize * ADPI);
FRadiansPerRevolutionPoint := Pi * 2.0 / FPointsPerRevolution;
end;
function TInputScan.GetPNGShortName: String;
begin
Result := ChangeFileExt(ExtractFileName(FPNGFileName), '');
end;
procedure TInputScan.FindConcentricGroove;
var
r, radiusInner, radiusOuter, radiusLimit: Integer;
f, f2, best, bestr: Double;
xrc: TVector;
begin
BuildSinCosLUT(FPointsPerRevolution, FSinCosLUT);
SetLength(xrc, 3);
radiusLimit := Round(C45RpmFirstMusicGroove * 0.5 * FDPI);
radiusInner := Round((C45RpmConcentricGroove + C45RpmLabelOuterSize) * 0.5 * FDPI * 0.5);
radiusOuter := Round((C45RpmConcentricGroove + C45RpmLastMusicGroove) * 0.5 * FDPI * 0.5);
xrc[1] := FCenter.X;
xrc[2] := FCenter.Y;
best := Infinity;
bestr := 0;
for r := radiusInner to radiusOuter do
begin
xrc[0] := r;
GradientEvalConcentricGroove(xrc, f, nil, nil);
if f < best then
begin
best := f;
bestr := xrc[0];
end;
end;
xrc[0] := bestr;
f := ASAMinimize(@GradientEvalConcentricGroove, xrc, [radiusInner, radiusLimit, radiusLimit], [radiusOuter, Width - radiusLimit, Height - radiusLimit]);
FConcentricGrooveRadius := xrc[0];
FCenter.X := xrc[1];
FCenter.Y := xrc[2];
GradientEvalCenter([FCenter.X, FCenter.Y], f2, nil, nil);
FCenterQuality := -(5.0 * f + f2);
end;
procedure TInputScan.FindGrooveStart;
var
i: Integer;
v, best, sn, cs, bestr, x, y, bestx, besty: Double;
begin
best := -Infinity;
bestx := 0;
besty := 0;
bestr := 0;
v := 0;
for i := 0 to FPointsPerRevolution - 1 do
begin
SinCos(i * FRadiansPerRevolutionPoint, sn, cs);
x := cs * FFirstGrooveRadius + FCenter.X;
y := sn * FFirstGrooveRadius + FCenter.Y;
if InRangePointD(y, x) then
begin
v := v * 0.99 + GetPointD(y, x, isImage) * 0.01;
if v > best then
begin
best := v;
bestx := x;
besty := y;
bestr := i * FRadiansPerRevolutionPoint;
end;
end;
//writeln(i:6,x:8,y:8,v:9:3,best:9:3,bestx:8,besty:8);
end;
FGrooveStartAngle := bestr;
FGrooveStartPoint.X := bestx;
FGrooveStartPoint.Y := besty;
end;
function TInputScan.GetHeight: Integer;
begin
Result := Length(FImage);
end;
function TInputScan.GetPointD(Y, X: Double; Source: TInterpSource): Double;
function GetPt(AY, AX: Single): Single; inline;
var
ix, iy: Integer;
y0, y1, y2, y3: Single;
begin
ix := trunc(AX);
iy := trunc(AY);
y0 := herp(FImage[iy - 1, ix - 1], FImage[iy - 1, ix + 0], FImage[iy - 1, ix + 1], FImage[iy - 1, ix + 2], AX - ix);
y1 := herp(FImage[iy + 0, ix - 1], FImage[iy + 0, ix + 0], FImage[iy + 0, ix + 1], FImage[iy + 0, ix + 2], AX - ix);
y2 := herp(FImage[iy + 1, ix - 1], FImage[iy + 1, ix + 0], FImage[iy + 1, ix + 1], FImage[iy + 1, ix + 2], AX - ix);
y3 := herp(FImage[iy + 2, ix - 1], FImage[iy + 2, ix + 0], FImage[iy + 2, ix + 1], FImage[iy + 2, ix + 2], AX - ix);
Result := herp(y0, y1, y2, y3, AY - iy);
end;
const
CH = 1.0;
begin
Result := 0;
case Source of
isImage:
begin
Result := GetPt(Y, X);
end;
isXGradient:
begin
Result := GetPt(Y, X - 3.0 * CH) * (1 / CH) * (-1/60);
Result += GetPt(Y, X - 2.0 * CH) * (1 / CH) * (3/20);
Result += GetPt(Y, X - 1.0 * CH) * (1 / CH) * (-3/4);
Result += GetPt(Y, X + 1.0 * CH) * (1 / CH) * (3/4);
Result += GetPt(Y, X + 2.0 * CH) * (1 / CH) * (-3/20);
Result += GetPt(Y, X + 3.0 * CH) * (1 / CH) * (1/60);
end;
isYGradient:
begin
Result := GetPt(Y - 3.0 * CH, X) * (1 / CH) * (-1/60);
Result += GetPt(Y - 2.0 * CH, X) * (1 / CH) * (3/20);
Result += GetPt(Y - 1.0 * CH, X) * (1 / CH) * (-3/4);
Result += GetPt(Y + 1.0 * CH, X) * (1 / CH) * (3/4);
Result += GetPt(Y + 2.0 * CH, X) * (1 / CH) * (-3/20);
Result += GetPt(Y + 3.0 * CH, X) * (1 / CH) * (1/60);
end;
end;
Result *= (1.0 / High(Word));
end;
function TInputScan.GetWidth: Integer;
begin
Result := Length(FImage[0]);
end;
constructor TInputScan.Create(ADefaultDPI: Integer; ASilent: Boolean);
begin
FDPI := ADefaultDPI;
FSilent := ASilent;
FCenterQuality := NaN;
SetRevolutionFromDPI(FDPI);
end;
destructor TInputScan.Destroy;
begin
inherited Destroy;
end;
procedure TInputScan.LoadPNG;
var
fs: TFileStream;
png: TDPIAwareReaderPNG;
img: TScanImage;
sz: TPoint;
begin
if not FSilent then WriteLn('LoadPNG ', FPNGFileName);
fs := TFileStream.Create(FPNGFileName, fmOpenRead or fmShareDenyNone);
png := TDPIAwareReaderPNG.Create;
try
sz := png.ImageSize(fs);
img := TScanImage.Create(sz.X, sz.Y);
try
SetLength(FImage, sz.Y, sz.X);
img.Image := FImage;
if not FSilent then WriteLn('Size:', Width:6, 'x', Height:6);
png.ImageRead(fs, img);
if (png.DPI.X > 0) and (png.DPI.X = png.DPI.Y) then
begin
FDPI := png.DPI.X;
if not FSilent then WriteLn('DPI:', FDPI:6);
end;
finally
img.Free;
end;
FCenter.X := sz.X * 0.5;
FCenter.Y := sz.Y * 0.5;
SetRevolutionFromDPI(FDPI);
finally
png.Free;
fs.Free;
end;
end;
procedure TInputScan.FindTrack;
begin
if not FSilent then WriteLn('FindTrack');
FFirstGrooveRadius := C45RpmFirstMusicGroove * FDPI * 0.5;
FindCenter;
FindConcentricGroove;
FindGrooveStart;
if not FSilent then
begin
WriteLn('Center:', FCenter.X:12:3, ',', FCenter.Y:12:3);
WriteLn('FirstGrooveRadius:', FFirstGrooveRadius:12:3);
WriteLn('ConcentricGrooveRadius:', FConcentricGrooveRadius:12:3);
WriteLn('GrooveStartPoint:', FGrooveStartPoint.X:12:3, ',', FGrooveStartPoint.Y:12:3);
end;
end;
function TInputScan.InRangePointD(Y, X: Double): Boolean;
begin
Result := InRange(Y, 4, Height - 6) and InRange(X, 4, Width - 6);
end;
{ TScanImage }
procedure TScanImage.SetInternalPixel(x, y: integer; Value: integer);
begin
// not needed
end;
function TScanImage.GetInternalPixel(x, y: integer): integer;
begin
Result := FImage[y, x];
end;
procedure TScanImage.SetInternalColor(x, y: integer; const Value: TFPColor);
begin
FImage[y, x] := ToLuma(Value.Red, Value.Green, Value.Blue) div cLumaDiv;
end;
{ TDPIAwareReaderPNG }
procedure TDPIAwareReaderPNG.HandleChunk;
begin
inherited HandleChunk;
if (Chunk.aType = ctpHYs) and (Chunk.data^[8] = $01) then
begin
FDPI.X := Round(BEtoN(PCardinal(@Chunk.data^[0])^) * 0.0254);
FDPI.Y := Round(BEtoN(PCardinal(@Chunk.data^[4])^) * 0.0254);
end;
end;
constructor TDPIAwareReaderPNG.Create;
begin
inherited create;
FDPI := Point(-1, -1);
end;
end.