-
Notifications
You must be signed in to change notification settings - Fork 23
/
define_types.pas
executable file
·646 lines (600 loc) · 15.6 KB
/
define_types.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
unit define_types;
{$IFDEF Darwin}
{$modeswitch objectivec1}
{$ENDIF}
interface
{$ifndef isTerminalApp}
uses graphics;
{$endif}
const
{$include vers.inc} //kVers = 'v1.0.20210825';
NaN : double = 1/0;
kTab = chr(9);
kCR = chr (13);
kDel = #127 ; // Delete
kBS = #8 ; // Backspace
kUNIXeoln = chr(10);
{$IFDEF UNIX} //end of line
kEOLN = kUNIXeoln; //Windows CRLF ;
{$ELSE}
kEOLN = #13#10; //Windows CRLF
{$ENDIF}
type
TRGBA = packed record //Next: analyze Format Header structure
R,G,B,A : byte;
end;
{$ifdef isTerminalApp}
TColor = -$7FFFFFFF-1..$7FFFFFFF;
TLUT = array [0..255] of TRGBA;
{$endif}
TPoint4f = packed record
X: single;
Y: single;
Z: single;
W: single
end;
TPoint3f = packed record
X: single;
Y: single;
Z: single
end;
TPoint3i = packed record
X: longint; //ensure 32-bit for simple GIfTI writing
Y: longint;
Z: longint;
end;
TFaces = array of TPoint3i;
TVertices = array of TPoint3f;
TVertexRGBA = array of TRGBA;
TBools = array of boolean;
TInts = array of integer;
TUInt8s = array of uint8;
TInt16s = array of int16;
TUInt16s = array of uint16;
TInt32s = array of int32;
TFloats = array of single;
TDoubles = array of double;
TMat33 = array [1..3, 1..3] of single;
TMat44 = array [1..4, 1..4] of single;
TFByte = File of Byte;
TStrRA = Array of String;
TUnitRect = record
L,T,R,B: single;
end;
function ParseFileName(Filename: string): string;
procedure FilenameParts (lInName: string; out lPath,lName,lExt: string);
procedure SensibleUnitRect (var U: TUnitRect);
procedure SortSingle(var lLo,lHi: single);
function RealToStr(lR: double; lDec: integer): string;
function RGBA(lR,lG,lB,lA: byte): TRGBA;
function CreateUnitRect (L,T,R,B: single) : TUnitRect;
procedure IntBound (var lVal: integer; lMin, lMax: integer);
function UnitBound (lS: single): single;
//procedure ReadLnBin(var f: TFByte; var s: string);
function ReadLnBin(var f: TFByte; var s: string): boolean; inline;
procedure SwapSingle(var s : single);
procedure SwapDouble(var d : double);
procedure SwapLongInt(var s : LongInt);
procedure SwapLongWord(var s : LongWord);
function asPt4f(x,y,z,w: single): TPoint4f;
function asSingle(i : longint): single; overload;
function asSingle(b0,b1,b2,b3: byte): single; overload;
function asInt(s : single): longint;
function StrToFloatX(Const S : String) : Extended;
function specialsingle (var s:single): boolean; //isFinite
function ExtractFileExtGzUpper(FileName: string): string;
function FileExistsF(fnm: string): boolean; //returns false if file exists but is directory
function FSize (lFName: String): longint;
function ChangeFileExtX( lFilename: string; lExt: string): string;
function ReadNumBin(var f: TFByte): string; //read next ASCII number in binary file
function float2str(Avalue:double; ADigits:integer):string; //e.g x:single=2.6; floattostrf(x,8,4);
function DefaultToHomeDir(FileName: string; Force: boolean = false): string; //set path to home if not provided
function UpCaseExt(lFileName: string): string; // "file.gii.dset" -> ".DSET"
function UpCaseExt2(lFileName: string): string; // "file.gii.dset" -> ".GII.DSET"
{$ifdef isTerminalApp}
function RGBToColor(R, G, B: Byte): TColor;
procedure ShowMessage(msg: string);
function Red(rgb: TColor): BYTE;
function Green(rgb: TColor): BYTE;
function Blue(rgb: TColor): BYTE;
{$else}
function asRGBA(clr: TColor): TRGBA;
{$endif}
procedure Xswap4r ( var s:single);
implementation
uses
{$IFDEF UNIX} BaseUnix, {$ELSE} windows, shlobj, {$ENDIF}
{$IFDEF Darwin}CocoaAll,{$ENDIF}
fileutil, sysutils, math;
procedure Xswap4r ( var s:single);
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
inguy^.Word1 := outguy.Word1;
inguy^.Word2 := outguy.Word2;
end;
function UpCaseExt(lFileName: string): string; // "file.gii.dset" -> ".GII.DSET"
var
fnm : string;
begin
result := UpperCase(ExtractFileExt(lFileName));
end;
function UpCaseExt2(lFileName: string): string; // "file.gii.dset" -> ".GII.DSET"
var
fnm : string;
begin
result := UpperCase(ExtractFileExt(lFileName));
fnm := ExtractFileNameWithoutExt(lFileName);
result := UpperCase(ExtractFileExt(fnm))+ result;
end;
function StrToFloatX(Const S : String) : Extended;
//like StrToFloat but accepts either decimal separator: '1.23' or '1,23'
var
fmt: TFormatSettings;
begin
fmt := DefaultFormatSettings;
fmt.DecimalSeparator := '.';
if TryStrToFloat(s, result, fmt) then
exit;
fmt.DecimalSeparator := ',';
result := StrToFloat(S,fmt);
end;
function FileExistsF(fnm: string): boolean; //returns false if file exists but is directory
begin
result := FileExists(fnm);
if result = false then exit;
result := not DirectoryExists(fnm);
{$IFDEF UNIX}
if result = false then exit;
//showmessage(fnm + inttostr( fpAccess (fnm,R_OK)));
if fpAccess(fnm,R_OK) < 0 then
result := false;
{$ENDIF}
end;
function HomeDir: string; //set path to home if not provided
{$IFDEF UNIX}
begin
result := expandfilename('~/');
end;
{$ELSE}
var
SpecialPath: PWideChar;
begin
Result := '';
SpecialPath := WideStrAlloc(MAX_PATH);
try
FillChar(SpecialPath^, MAX_PATH, 0);
if SHGetSpecialFolderPathW(0, SpecialPath, CSIDL_PERSONAL, False) then
Result := SpecialPath+pathdelim;
finally
StrDispose(SpecialPath);
end;
end;
{$ENDIF}
function DefaultToHomeDir(FileName: string; Force: boolean = false ): string; //set path to home if not provided
var
p,n,x: string;
begin
result := FileName;
FilenameParts (Filename, p,n,x);
if (not Force) and (p <> '') and (DirectoryExists(p)) then exit;
{$IFDEF LCLCocoa}
//p := HomeDir; //set path to home if not provided
p := NSTemporaryDirectory.UTF8String;
{$ELSE}
p := HomeDir; //set path to home if not provided
{$ENDIF}
result := p+n+x;
end;
{$IFDEF oldFloat2Str}
function float2str(Avalue:double; ADigits:integer):string; //e.g x:single=2.6; floattostrf(x,8,4);
begin
result := FloatToStrF(Avalue, ffFixed,7,ADigits);
end;
{$ELSE}
function float2str(Avalue:double; ADigits:integer):string; //e.g x:single=2.6; floattostrf(x,8,4);
//http://stackoverflow.com/questions/5650051/how-to-keep-2-decimal-places-in-delphi
var v:double; p:integer; e:string;
begin
if abs(Avalue)<1 then
begin
result:=floatTostr(Avalue);
p:=pos('E',result);
if p>0 then
begin
e:=copy(result,p,length(result));
setlength(result,p-1);
v:=RoundTo(StrToFloat(result),-Adigits);
result:=FloatToStr(v)+e;
end else
result:=FloatToStr(RoundTo(Avalue,-Adigits));
end
else
result:=FloatToStr(RoundTo(Avalue,-Adigits));
end;
{$ENDIF}
{$ifdef isTerminalApp}
function Blue(rgb: TColor): BYTE;
begin
Result := (rgb shr 16) and $000000ff;
end;
function Green(rgb: TColor): BYTE;
begin
Result := (rgb shr 8) and $000000ff;
end;
function Red(rgb: TColor): BYTE;
begin
Result := rgb and $000000ff;
end;
function RGBToColor(R, G, B: Byte): TColor;
begin
Result := (B shl 16) or (G shl 8) or R;
end;
procedure ShowMessage(msg: string);
begin
writeln(msg);
end;
{$else}
function asRGBA(clr: TColor): TRGBA;
begin
result.R := red(clr);
result.G := green(clr);
result.B := blue(clr);
result.A := 255;
end;
{$endif}
function ReadNumBin(var f: TFByte): string; //read next ASCII number in binary file
var
bt : Byte;
ch : Char;
begin
result := '';
while (not EOF(f)) do begin
Read(f,bt);
ch := Chr(bt);
if ch in ['-','.','E','e','0'..'9'] then
result := result + ch
else if length(result) > 0 then
exit;
end;
end;
function asPt4f(x,y,z,w: single): TPoint4f;
begin
result.x := x;
result.y := y;
result.z := z;
result.w := w;
end;
function ChangeFileExtX( lFilename: string; lExt: string): string;
//sees .nii.gz as single extension
var
lPath,lName,lOrigExt: string;
begin
FilenameParts (lFilename, lPath,lName,lOrigExt);
result := lPath+lName+lExt;
end;
function FSize (lFName: String): longint;
var F : File Of byte;
begin
result := 0;
if not fileexistsF(lFName) then exit;
FileMode := fmOpenRead;
Assign (F, lFName);
Reset (F);
result := FileSize(F);
Close (F);
end;
function ParseFileName(Filename: string): string;
var
lPath,lName,lExt: string;
begin
FilenameParts (FileName, lPath,lName,lExt);
result := (lName);
end;
function ExtractFileExtGzUpper(FileName: string): string;
//the file 'img.nii.gz' returns '.NII.GZ', not just '.gz'
var
lPath,lName,lExt: string;
begin
//result := UpperCase(ExtractFileExt(FileName));
FilenameParts (FileName, lPath,lName,lExt);
result := UpperCase(lExt);
end;
procedure IntBound (var lVal: integer; lMin, lMax: integer);
begin
if lVal < lMin then lVal := lMin;
if lVal > lMax then lVal := lMax;
end;
function specialsingle (var s:single): boolean;
//returns true if s is Infinity, NAN or Indeterminate
//4byte IEEE: msb[31] = signbit, bits[23-30] exponent, bits[0..22] mantissa
//exponent of all 1s = Infinity, NAN or Indeterminate
const kSpecialExponent = 255 shl 23;
var Overlay: LongInt absolute s;
begin
if ((Overlay and kSpecialExponent) = kSpecialExponent) then
RESULT := true
else
RESULT := false;
end;
function asSingle(i : longint): single; overload;
type
swaptype = packed record
case byte of
0:(Lng : longint);
1:(Sngl : single);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
begin
inguy := @i; //assign address of s to inguy
result := inguy^.Sngl;
end; // asSingle()
function asSingle(b0,b1,b2,b3: byte): single; overload;
type
swaptype = packed record
case byte of
0:(b0,b1,b2,b3 : byte);
1:(Sngl : single);
end;
//swaptypep = ^swaptype;
var
//inguy:swaptypep;
outguy:swaptype;
begin //should work with both little and big endian, as order is same
outguy.b0 := b0;
outguy.b1 := b1;
outguy.b2 := b2;
outguy.b3 := b3;
result := outguy.Sngl;
end; // asSingle()
function asInt(s : single): longint;
type
swaptype = packed record
case byte of
0:(Lng : longint);
1:(Sngl : single);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
begin
inguy := @s; //assign address of s to inguy
result := inguy^.Lng;
end; // asInt()
procedure SwapDouble(var d : double);
type
swaptype = packed record
case byte of
0:(Word1,Word2,Word3,Word4 : word); //word is 16 bit
1:(float:double);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @d; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word4);
outguy.Word2 := swap(inguy^.Word3);
outguy.Word3 := swap(inguy^.Word2);
outguy.Word4 := swap(inguy^.Word1);
try
d:=outguy.float;
except
d := 0;
exit;
end;
end; //func SwapDouble
procedure SwapSingle(var s : single);
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word);
1:(Sngl : single);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
s := outguy.Sngl;
end; // SwapSingle()
procedure SwapLongInt(var s : LongInt);
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
1:(Long:LongInt);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
s := outguy.Long;
end; // SwapLongInt()
procedure SwapLongWord(var s : LongWord);
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
1:(Long:LongWord);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
s := outguy.Long;
end; // SwapLongWord()
{$IFDEF SLOWREADLNBIN}
procedure ReadLnBin(var f: TFByte; var s: string);
const
kEOLN = $0A;
var
bt : Byte;
begin
s := '';
while (not EOF(f)) do begin
Read(f,bt);
if bt = kEOLN then exit;
s := s + Chr(bt);
end;
end;
{$ELSE}
function ReadLnBin(var f: TFByte; var s: string): boolean; inline;
const
kEOLN = $0A;
var
bt : Byte;
begin
s := '';
//while (not EOF(f)) do begin //<- half the speed!
while (true) do begin
try
Read(f,bt);
except
exit(false);
end;
if bt = kEOLN then exit(true);
s := s + Chr(bt);
end;
end;
{$ENDIF}
function RGBA(lR,lG,lB,lA: byte): TRGBA;
//set red,green,blue and alpha of a Quad
begin
result.r := lR;
result.g := lG;
result.b := lB;
result.a := lA;
end;
function RealToStr(lR: double; lDec: integer): string;
begin
result := FloatToStrF(lR, ffFixed,7,lDec);
end;
procedure FilenameParts (lInName: string; out lPath,lName,lExt: string);
var
lX: string;
begin
lPath := ExtractFilePath(lInName);
lName := ExtractFileName(lInName);
lExt := ExtractFileExt(lInName);
if lExt = '' then exit;
Delete(lName, length(lName)-length(lExt)+1, length(lExt)); //nam.ext -> nam
lX := lExt;
if UpperCase(lX) <> '.GZ' then exit;
lExt := ExtractFileExt(lName);
Delete(lName, length(lName)-length(lExt)+1, length(lExt)); //nam.ext -> nam
lExt := lExt + lX;
//showmessage(lName+':'+lExt);
end;
(*function FilenameParts (lInName: string; var lPath,lName,lExt: string): boolean;
var
lLen,lPos,lExtPos,lPathPos: integer;
begin
result := false;
lPath := '';
lName := '';
lExt := '';
lLen := length(lInName);
if lLen < 1 then exit;
//next find final pathdelim
lPathPos := lLen;
while (lPathPos > 0) and (lInName[lPathPos] <> '\') and (lInName[lPathPos] <> '/') do
dec(lPathPos);
if (lInName[lPathPos] = '\') or (lInName[lPathPos] = '/') then begin
for lPos := 1 to lPathPos do
lPath := lPath + lInName[lPos];
end;
// else
// dec(lPathPos);
inc(lPathPos);
//next find first ext
lExtPos := 1;
while (lExtPos <= lLen) and (lInName[lExtPos] <> '.') do
inc(lExtPos);
if (lInName[lExtPos] = '.') then begin
for lPos := lExtPos to lLen do
lExt := lExt + lInName[lPos];
end;
// else
// inc(lExtPos);
dec(lExtPos);
//next extract filename
//fx(lPathPos,lExtPos);
if (lPathPos <= lExtPos) then
for lPos := lPathPos to lExtPos do
lName := lName + lInName[lPos];
result := true;
end; *)
procedure SortSingle(var lLo,lHi: single);
var lSwap: single;
begin
if lLo > lHi then begin
lSwap := lLo;
lLo := lHi;
lHi := lSwap;
end; //if Lo>Hi
end; //proc SortSingle
function UnitBound (lS: single): single;
begin
if lS < 0 then
result := 0
else if lS > 1 then
result := 1
else
result := lS;
end;
procedure SensibleUnitRect (var U: TUnitRect);
begin
U.L := UnitBound(U.L);
U.T := UnitBound(U.T);
U.R := UnitBound(U.R);
U.B := UnitBound(U.B);
//left should be lower value than right
SortSingle(U.L,U.R);
if U.L = U.R then begin
if U.R < 0.1 then
U.R := 0.1
else
U.L := U.R -0.1;
end;
//bottom should lower value than top
SortSingle(U.B,U.T);
if U.B = U.T then begin
if U.T < 0.1 then
U.T := 0.1
else
U.B := U.T -0.1;
end;
end;
function CreateUnitRect (L,T,R,B: single) : TUnitRect;
begin
result.L := UnitBound(L);
result.T := UnitBound(T);
result.R := UnitBound(R);
result.B := UnitBound(B);
end;
end.