-
Notifications
You must be signed in to change notification settings - Fork 33
/
nifti.pas
executable file
·9634 lines (9350 loc) · 332 KB
/
nifti.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit nifti;
{$IFDEF FPC}{$mode objfpc}{$H+}{$ENDIF}
//{$IFDEF FPC}{$mode delphi} {$H+}{$ENDIF}
{$DEFINE CUSTOMCOLORS}
interface
{$include opts.inc} //for DEFINE FASTGZ
{$DEFINE TIMER} //reports load times to stdout (Unix only)
{$IFDEF FPC}
{$DEFINE GZIP}
{$ENDIF}
//{$DEFINE PARALLEL} //for parallel Unix also edit CThreads in LPR file
{$DEFINE OPENFOREIGN} //used for q and s form as well as opening
{$DEFINE AFNI} //read Head/Brik statistics
{$IFNDEF OPENFOREIGN}{$IFDEF AFNI} error: AFNI requires OPENFOREIGN {$ENDIF} {$ENDIF}
{$DEFINE CPUGRADIENTS} //Computing volume gradients on the GPU is much faster than using the CPU
{$DEFINE CACHEUINT8} //save 8-bit data: faster requires RAM
{$DEFINE SSE}
{$DEFINE TIF} //load bitmaps, e.g. PNG, BMP, TIFFs not handled by NIfTI_TIFF
{$DEFINE BMP} //load bitmaps, e.g. PNG, BMP, TIFFs not handled by NIfTI_TIFF
{$DEFINE BZIP2}
//{$IFDEF MYTHREADS}
{$ModeSwitch nestedprocvars}
//{$ENDIF}
uses
//{$IFDEF UNIX}cthreads, cmem,{$ENDIF}
{$IFDEF AFNI}afni_fdr,{$ENDIF}
{$IFDEF BZIP2}bzip2stream,{$ENDIF}
{$IFDEF BMP} Graphics, GraphType,{$ENDIF}
{$IFDEF PARALLEL}mtprocs,mtpcpu,{$ENDIF}
{$IFDEF TIMER} DateUtils,{$ENDIF}
{$IFDEF FASTGZ}
{$IFDEF LIBDEFLATE}
libdeflate,
{$ENDIF}
SynZip,
{$ENDIF}
{$IFDEF OPENFOREIGN} nifti_foreign, {$ENDIF}
{$IFDEF CUSTOMCOLORS} colorTable, {$ENDIF}
{$IFDEF GZIP}zstream, umat, GZIPUtils, {$ENDIF} //Freepascal includes the handy zstream function for decompressing GZip files
{$IFDEF TIF} nifti_tiff, {$ENDIF}
{$IFDEF SSE}
{$IFDEF CPUAARCH64}
neon,
{$ELSE}
sse,
{$ENDIF}
{$ENDIF}
nifti_save,
sortu, strutils, dialogs, clipbrd, SimdUtils, sysutils,Classes, nifti_types, Math, VectorMath, otsuml;
//Written by Chris Rorden, released under BSD license
//This is the header NIfTI format http://nifti.nimh.nih.gov/nifti-1/
//NIfTI is popular in neuroimaging - should be compatible for Analyze format
// http://eeg.sourceforge.net/ANALYZE75.pdf
//NIfTI format images have two components:
// 1.) Header data provides image dimensions and details
// 2.) Image data
//These two components can be separate files: MRI.hdr, MRI.img
// or a single file with the header at the start MRI.nii
//Note raw image daya begins vox_offset bytes into the image data file
// For example, in a typical NII file, the header is the first 348 bytes,
// but the image data begins at byte 352 (as this is evenly divisible by 8)
(*const
kDTIno = -1; //this is NOT a DTI V1 image
kDTIscalar = 0; //this might be a DTI V1 image, but display as typical image
kDTIrgb = 1; //display as RGB V1 image
kDTIlines = 2; //display as lines *)
Type
TCluster = record
CogXYZ, PeakXYZ: TVec3; //Center of Gravity, Peak
Peak, SzMM3: single;
PeakStructure, Structure: string[248];
end;
TClusters = array of TCluster;
{$IFNDEF DYNRGBA}
TRGBA0 = array [0..MAXINT] of TRGBA;
TRGBAp = ^TRGBA0; //pointer to RGBA array
{$ENDIF}
TNIfTI = Class(TObject) // This is an actual class definition :
// Internal class field definitions - only accessible in this unit
private
fMat, fInvMat, fMatInOrient: TMat4;
fMin, fMax, fAutoBalMin, fAutoBalMax, fWindowMin, fWindowMax: single;
fOpacityPct: integer;
fScale, fCutoutLow, fCutoutHigh : TVec3;
fDim, fPermInOrient: TVec3i;
fHdr, fHdrNoRotation : TNIFTIhdr;
fKnownOrientation, fIsNativeEndian: boolean;
fFileName,fShortName, fBidsName: string;
{$IFDEF DYNRGBA}
fVolRGBA: TRGBAs;
{$ELSE}
fVolRGBA: TRGBAp;
{$ENDIF}
//fLabels: TStringList;
fhistogram: TLUT;
fisLinearReslice, fIsOverlay, fIsDrawing: boolean; //booleans do not generate 32-bit RGBA images
{$IFDEF CACHEUINT8}
fVolumesTotal, fVolumeDisplayed, fVolumesLoaded, fOpacityPctCache8: integer;
fWindowMinCache8, fWindowMaxCache8: single;
fCache8: TUInt8s;
{$ENDIF}
{$IFDEF CUSTOMCOLORS}
clut: TCLUT;
{$ELSE}
fLUT: TLUT;
{$ENDIF}
//init functions compute volume min/max and set default min/max brightness/contrast
procedure InitUInt8();
procedure InitInt16();
procedure InitFloat32();
procedure DetectV1();
//display functions apply window min/max to generate image with desired brightness/contrast
procedure SetDisplayMinMaxRGB24();
//procedure SetDisplayMinMaxRGBV1();
procedure LoadRGBVector();
procedure DisplayLabel2Uint8();
//procedure SetDisplayMinMaxFloat32();
//procedure SetDisplayMinMaxInt16();
//procedure SetDisplayMinMaxUint8();
{$IFDEF PARALLEL}
//procedure SetDisplayMinMaxParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
{$ENDIF}
procedure ConvertUint16Int16();
procedure Convert2Float();
procedure Convert2RGB(); //convert RGBA -> RGB
procedure Convert2UInt8(); //convert int8 -> int16
procedure VolumeReslice(tarMat: TMat4; tarDim: TVec3i; isLinearReslice: boolean);
procedure VolumeReorient();
//procedure ApplyVolumeReorient(perm: TVec3i; outR: TMat4);
function OpenNIfTI(): boolean;
procedure MakeBorg(voxelsPerDimension: integer);
procedure ApplyCutout();
function loadForeign(FileName : AnsiString; var rawData: TUInt8s; var lHdr: TNIFTIHdr): boolean;// Load 3D data
function LoadRaw(FileName : AnsiString; out isNativeEndian: boolean): boolean;
//function SaveBVox(bVoxFileName: string; var hdr: TNIFTIhdr; var img8: TUInt8s): boolean;
//function SaveOsp(OspFileName: string; var hdr: TNIFTIhdr; var img8: TUInt8s): boolean;
//function SaveNii(niftiFileName: string): boolean; overload;
//function SaveFormatBasedOnExt(fnm: string; var hdr: TNIFTIhdr; var img8: TUInt8s): boolean; overload;
//function SaveNii(fnm: string; var hdr: TNIFTIhdr; var img8: TUInt8s): boolean; overload;
{$IFDEF GZIP}
//function SaveGz(niftiFileName: string; var hdr: TNIFTIhdr; var img8: TUInt8s): boolean;
{$IFDEF FASTGZ}function LoadFastGz(FileName : AnsiString; out isNativeEndian: boolean): boolean;{$ENDIF}
function LoadGz(FileName : AnsiString; out isNativeEndian: boolean): boolean;
{$ENDIF}
function skipVox(): int64; //if fVolumeDisplayed > 0, skip this many VOXELS for first byte of data
//function skipBytes(): int64; //if fVolumeDisplayed > 0, skip this many BYTES for first byte of data
//function skipRawVolBytes(): TUInt8s;
function VoiDescriptivesLabels(VoiRawBytes: TUInt8s): TStringList;
//function CenterOfMass(idx: integer; out sizeMM3: single): TVec3;
//procedure GenerateAtlasClusters8bit;
procedure GenerateAtlasClusters;
function rawAtlasMax: integer;
function mm3toVox(mm3: single) : integer; //e.g. if 3x3x3mm voxels (27mm) and input is 28, return 2
procedure initHistogram(Histo: TUInt32s = nil);
//procedure robustMinMax(var rMin, rMax: single);
public
RefreshCount: integer;
fLabels: TStringList;
fLabelMask: TUInt8s;
fRawVolBytes: TUInt8s;
LoadFewVolumes: boolean;
SortClustersBySize : boolean;
HiddenByCutout: boolean;
//fDTImode: integer; //kDTIno, kDTIscalar,kDTIrgb,kDTIlines
IsShrunken : boolean;
IsFightInterpolationBleeding: boolean;
IsInterpolated : boolean;
ZeroIntensityInvisible: boolean;
MaxTexMb: integer; //maximum number of voxels in any dimension
isAntiAliasHugeTexMb: boolean;
MinPixDim, MaxMM: single; //maximum mm in any dimension
mmAsFrac: TVec3;
{$IFDEF AFNI}
afnis: TAFNIs;
{$ENDIF}
clusters: TClusters;
clusterNotes: string[128]; //interpolated, label map, etc
procedure MakeHistogram(Histo: TUInt32s; mn, mx: single; isClampExtremeValues : boolean = true; isIgnoreZeros: boolean = false);
property InputReorientPermute: TVec3i read fPermInOrient;
property IsNativeEndian: boolean read fIsNativeEndian;
property VolumeDisplayed: integer read fVolumeDisplayed; //indexed from 0 (0..VolumesLoaded-1)
property VolumesLoaded: integer read fVolumesLoaded; //1 for 3D data, for 4D 1..hdr.dim[4] depending on RAM
property VolumesTotal: integer read fVolumesTotal; //1 for 3D data, for 4D 1..hdr.dim[4] depending on RAM
property KnownOrientation: boolean read fKnownOrientation;
{$IFDEF CUSTOMCOLORS}
function GetColorTable: TLUT;
//property ColorTable: TLUT read clut.fLUT write clut.fLUT;
function FullColorTable: TCLUTrec;
property CX: TCLUT read clut write clut;
{$ELSE}
property ColorTable: TLUT read fLUT write fLUT;
{$ENDIF}
//procedure SmoothMaskedImages();
property Drawing: boolean read fIsDrawing;
function AsFloats(): TFloat32s;
procedure SortClusters;
function NotZero(): TInt16s; //volume where voxels non-zero voxels are set to 1, voxels with intensity zero set to 0
function NeedsUpdate(): boolean;
function DisplayMinMax2Uint8(isForceRefresh: boolean = false): TUInt8s;
procedure SaveRotated(fnm: string; perm: TVec3i);
function RemoveSmallClusters(thresh, mm: double; NeighborMethod: integer = 1): single; //returns surviving mm3
function SaveCropped(fnm: string; crop: TVec6i; cropVols: TPoint): boolean;
function SaveAs8Bit(fnm: string; Lo,Hi: single): boolean;
function SaveRescaled(fnm: string; xFrac, yFrac, zFrac: single; OutDataType, Filter: integer; isAllVolumes: boolean): boolean;
procedure SaveAsSourceOrient(NiftiOutName: string; rawVolBytesIn: TUInt8s); overload;
procedure SaveAsSourceOrient(NiftiOutName, HdrDescrip, IntentName: string; rawVolBytesIn: TUInt8s; dataType: integer; intentCode: integer = 0; mn: single = 0; mx: single = 0);
function FracToSlice(Frac: TVec3): TVec3i; //given volume fraction return zero-indexed slice
function FracShiftSlice(Frac: TVec3; sliceMove: TVec3i): TVec3; //move a desired number of slices
function FracMM(Frac: TVec3): TVec3; //return mm coordinates given volume fraction
function MMFrac(MM: TVec3): TVec3; //return frac coordinates given volume mm
function VoiDescriptives(VoiRawBytes: TUInt8s): TStringList;
function VoxMM3: single; //voxel volume of resliced data
function VoxIntensityString(vox: int64): string; overload;
function VoxIntensityString(vox: TVec3i): string; overload;
function VoxIntensity(vox: int64): single; overload; //return intensity of voxel at coordinate
function VoxIntensity(vox: TVec3i): single; overload; //return intensity of voxel at coordinate
function VoxIntensity(frac: TVec3): single; overload;//return intensity of voxel fraction
function VoxIntensityArray(vox: TVec3i): TFloat32s; overload;
function VoxIntensityArray(roi: TUInt8s): TFloat32s; overload;
function nifti_smooth_gauss(var img: TFloat32s; SigmammX, SigmammY, SigmammZ: single; nVol: int64): integer;
function EdgeMap(isSmooth: boolean): TUInt8s;
function Scaled2Raw(lScaled: single): single;
function SeedCorrelationMap(vox: TVec3i; isZ: boolean): TFloat32s; overload;
function SeedCorrelationMap(roi: TUInt8s; isZ: boolean): TFloat32s; overload;
function SeedCorrelationMap(vSeed: TFloat32s; isZ: boolean): TFloat32s; overload;
property OpacityPercent: integer read fOpacityPct write fOpacityPct; //0=transparent, 50=translucent, 100=opaque
property Histogram: TLUT read fhistogram;
property VolumeMin: single read fMin; //darkest voxel in volume
property VolumeMax: single read fMax; //brightest voxel in volume
property SuggestedDisplayMin: single read fAutoBalMin;
property SuggestedDisplayMax: single read fAutoBalMax;
property DisplayMin: single read fWindowMin; //write warning: changes only isplayed if you call SetDisplayMinMax()
property DisplayMax: single read fWindowMax; //write warning: changes only isplayed if you call SetDisplayMinMax()
property Header: TNIFTIhdr read fhdr;
property HeaderNoRotation: TNIfTIhdr read fHdrNoRotation;
property Scale: TVec3 read fScale;
property ShortName: String read fShortName;
property Mat: TMat4 read fMat;
property InvMat: TMat4 read fInvMat;
property Dim: TVec3i read fDim;
{$IFDEF DYNRGBA}
property VolRGBA: TRGBAs read fVolRGBA;
{$IFDEF CPUGRADIENTS}
procedure CreateGradientVolume (rData: TRGBAs; Xsz,Ysz,Zsz: integer; out Vol : TRGBAs);
function GenerateGradientVolume: TRGBAs; overload;
{$ENDIF}
{$ELSE}
property VolRGBA: TRGBAp read fVolRGBA;
{$IFDEF CPUGRADIENTS}
procedure CreateGradientVolume (rData: TRGBAp; Xsz,Ysz,Zsz: integer; out Vol : TRGBAp);
function GenerateGradientVolume: TRGBAp; overload;
{$ENDIF}
{$ENDIF}
property VolRawBytes: TUInt8s read fRawVolBytes;
property Filename: String read fFileName;
property BidsName: String read fBidsName;
procedure Sharpen();
procedure Smooth(Mask: TUInt8s = nil);
procedure Mask(MaskingVolume: TUInt8s; isPreserveMask: boolean);
procedure RemoveHaze(isSmoothEdges: boolean = true; isSingleObject: boolean = true; OtsuLevels : integer = 5);
procedure GPULoadDone();
function SaveFormatBasedOnExt(fnm: string): boolean; overload; //filename determines format.nii, .tif, .bvox, .nrrd, .nhdr etc...
function Load(niftiFileName: string): boolean; overload;
function Load(niftiFileName: string; tarMat: TMat4; tarDim: TVec3i; isInterpolate: boolean; hdr: TNIFTIhdr; img: TFloat32s; isUINT8: boolean = false): boolean; overload;
function Load(niftiFileName: string; tarMat: TMat4; tarDim: TVec3i; isInterpolate: boolean; volumeNumber: integer = 0; isKeepContrast: boolean = false): boolean; overload;
procedure SetDisplayMinMax(newMin, newMax: single); overload;
procedure SetDisplayMinMax(isForceRefresh: boolean = false); overload;
procedure SetDisplayMinMaxNoUpdate(newMin, newMax: single); overload;
procedure SetCutoutNoUpdate(CutoutLow, CutoutHigh: TVec3);
procedure GenerateClusters(thresh, smallestClusterMM3: single; NeighborMethod: integer = 1; isDarkAndBright: boolean = false); overload;
//thresh, mm: double; NeighborMethod: integer
procedure GenerateClusters(LabelMap: TNIfTI; thresh, smallestClusterMM3: single; NeighborMethod: integer = 1; isDarkAndBright: boolean = false); overload;
function DisplayRGBGreen(): TUInt8s;
procedure DisplayRGB();
procedure SetDisplayColorScheme(clutFileName: string; cTag: integer);
procedure SetDisplayVolume(newDisplayVolume: integer);
//procedure SetLabelMask(Idx: integer; isMasked: boolean);
procedure ForceUpdate;
procedure SetClusters(c: TClusters; notes: string);
constructor Create(); overload; //empty volume
constructor Create(niftiFileName: string; backColor: TRGBA; tarMat: TMat4; tarDim: TVec3i; isInterpolate: boolean; hdr: TNIFTIhdr; img: TFloat32s; isUINT8: boolean = false); overload;
constructor Create(niftiFileName: string; backColor: TRGBA; lLoadFewVolumes: boolean; lMaxTexMb: integer; out isOK: boolean); overload; //background
constructor Create(niftiFileName: string; backColor: TRGBA; tarMat: TMat4; tarDim: TVec3i; isInterpolate: boolean; out isOK: boolean; lLoadFewVolumes: boolean = true; lMaxTexMb: integer = 640); overload; //overlay
constructor Create(tarMat: TMat4; tarDim: TVec3i); overload; //blank drawing
procedure SetIsLabels(b: boolean);
function IsLabels: boolean; //e.g. template map: should be drawn nearest neighbor (border of area 19 and 17 is NOT 18)
destructor Destroy; override;
end;
{$IFDEF CPUGRADIENTS}
{$IFDEF DYNRGBA}
procedure CreateGradientVolumeX (rData: TUInt8s; Xsz,Ysz,Zsz, isRGBA: integer; out VolRGBA : TRGBAs);
{$ELSE}
procedure CreateGradientVolumeX (rData: TUInt8s; Xsz,Ysz,Zsz, isRGBA: integer; out VolRGBA : TRGBAp);
procedure SetLengthP(var S: TRGBAp; Len: SizeInt);
{$ENDIF}
{$ENDIF}
function extractfileextX(fnm: string): string;
implementation
uses nifti_resize;
//uses reorient;
(*procedure TNIfTI.SetLabelMask(Idx: integer; isMasked: boolean);
var
i: integer;
begin
if fLabels.Count < 1 then exit;
if fLabelMask = nil then begin
SetLength(fLabelMask, fLabels.Count );
for i := 0 to fLabels.Count - 1 do
fLabelMask[i] := 0;
end;
end;*)
{$IFDEF CUSTOMCOLORS}
function TNIfTI.GetColorTable: TLUT;
begin
result := clut.LUT;
end;
{$ENDIF}
FUNCTION specialsingle (var s:single): boolean; inline;
//returns true if s is 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; //specialsingle()
procedure TNIfTI.SetClusters(c: TClusters; notes: string);
begin
clusterNotes := notes+ 'From AFNI: volume in voxels, peak intensity not reported';
//zz setlength(clusters, length(c));
if length(c) < 1 then begin
setlength(clusters, 0);
exit;
end;
clusters := copy(c, 0, maxint);
end;
procedure Coord(var lV: TVec4; lMat: TMat4);
//transform X Y Z by matrix
var
lXi,lYi,lZi: single;
begin
lXi := lV[0]; lYi := lV[1]; lZi := lV[2];
lV[0] := (lXi*lMat[0,0]+lYi*lMat[0,1]+lZi*lMat[0,2]+lMat[0,3]);
lV[1] := (lXi*lMat[1,0]+lYi*lMat[1,1]+lZi*lMat[1,2]+lMat[1,3]);
lV[2] := (lXi*lMat[2,0]+lYi*lMat[2,1]+lZi*lMat[2,2]+lMat[2,3]);
end;
function voxelCenter(frac: single; dim: integer): single;
//if 3 voxels span 0..1 then centers are at 0.25/0.5/0.75 - voxels frac is 1/(n+1)
//if 4 voxels, centers 0.125, 0.375. 0.625. 0.875
// so val 0..0.33 will return 0.25
var
sliceFrac: double;
slice: integer;
begin
if (dim < 2) then exit(0.5);
sliceFrac := 1/dim;
slice := trunc(frac/sliceFrac);
if (slice >= dim) then slice := dim - 1; //e.g. frac 1.0 = top slice
if (slice < 0) then slice := 0; //only with negative fraction!
result := (sliceFrac * (slice+0.5));
end;
function frac2slice(frac: single; dim: integer): integer;
var
sliceFrac: double;
begin
if (dim < 2) then exit(0);
sliceFrac := 1/dim;
result := trunc(frac/sliceFrac);
if (result >= dim) then result := dim - 1; //e.g. frac 1.0 = top slice
if (result < 0) then result := 0; //only with negative fraction!
end;
function SliceMM(vox0: TVec3; Mat: TMat4): TVec3;
var
v4: TVec4;
begin
v4 := Vec4(vox0.x, vox0.y, vox0.z, 1.0); //Vec4(X-1,Y-1,Z-1, 1); //indexed from 0, use -1 if indexed from 1
Coord(v4, Mat);
result := Vec3(v4.x, v4.y, v4.z);
end;
function TNIfTI.skipVox(): int64;
begin
if (fVolumeDisplayed <= 0) or (fIsOverlay) then exit(0);
result := prod(fDim) * fVolumeDisplayed;
end;
procedure TNIfTI.ForceUpdate;
begin
fWindowMinCache8 := infinity;
end;
procedure TNIfTI.SetIsLabels(b: boolean);
var
i: smallint;
begin
//see todo in main unit
i := fHdr.intent_code;
if b then
fHdr.intent_code := kNIFTI_INTENT_LABEL
else if fHdrNoRotation.intent_code = kNIFTI_INTENT_LABEL then
fHdr.intent_code := kNIFTI_INTENT_NONE
else
fHdr.intent_code := fHdrNoRotation.intent_code;
if (i <> fHdr.intent_code ) then
fWindowMinCache8 := infinity; //force update
end;
function TNIfTI.IsLabels: boolean;
//return TRUE for neurolabels
begin
result := (fHdr.intent_code= kNIFTI_INTENT_LABEL);
end;
function TNIfTI.VoiDescriptivesLabels(VoiRawBytes: TUInt8s): TStringList;
const
kTab = chr(9);
var
i, j, nVox, nVoi, sumTotal : int64;
sumVois, sumVoisNot0: TUInt32s;
frac: single;
begin
result := TStringList.Create();
if fLabels.Count < 1 then exit;
nVox := length(VoiRawBytes);
nVoi := fLabels.Count;
if (nVox <> (dim.x * dim.y * dim.z)) then exit;
setlength(sumVois, nVoi);
setlength(sumVoisNot0, nVoi);
for i := 0 to (nVoi -1) do begin
sumVois[i] := 0;
sumVoisNot0[i] := 0;
end;
sumTotal := 0;
for i := 0 to (nVox -1) do begin
j := round(VoxIntensity(i));
if j = 0 then continue;
sumVois[j] := sumVois[j] + 1;
if (VoiRawBytes[i] = 0) then continue;
sumTotal := sumTotal + 1;
if (j >= nVox) then continue;
sumVoisNot0[j] := sumVoisNot0[j] + 1;
end;
result.Add('Background image: '+ShortName);
result.Add(format('Regions: ', [nVoi]));
result.Add(format('TotalVoxelsNotZero%s%d', [kTab, sumTotal]));
result.Add('Background image: '+ShortName);
result.Add('Index'+kTab+'Name'+kTab+'numVox'+kTab+'numVoxNotZero'+kTab+'fracVoxNotZero');
for i := 0 to (fLabels.Count -1) do begin
if sumVoisNot0[i] < 1 then continue;
if sumVois[i] < 1 then continue;
frac := sumVoisNot0[i] / sumVois[i];
if fLabels[i] = '' then
result.Add(format('%d%s<%d>%s%d%s%d%s%g', [i,kTab, i, kTab, sumVois[i], kTab, sumVoisNot0[i],kTab,frac]))
else
result.Add(format('%d%s"%s"%s%d%s%d%s%g', [i,kTab, fLabels[i], kTab, sumVois[i], kTab, sumVoisNot0[i],kTab,frac]));
end;
sumVois := nil;
sumVoisNot0 := nil;
end;
function RealToStr(lR: double {was extended}; lDec: integer): string;
begin
result := FloatToStrF(lR, ffFixed,7,lDec);
end;
function TNIfTI.VoiDescriptives(VoiRawBytes: TUInt8s): TStringList;
const
kTab = ' ';//chr(9);
var
x, y, z, lInc, i, nVox, nVoi : int64;
lROIVol: array [1..3] of int64;
lROISum,lROISumSqr,lROImin,lROImax:array [1..3] of double;
lCC,lVal,lSD,lROImean: double;
lLabelStr, lStr: string;
mm: TVec3;
procedure AddVal( lRA: integer);
begin
inc(lROIVol[lRA]);
lROISum[lRA] := lROISum[lRA]+lVal;
lROISumSqr[lRA] := lROISumSqr[lRA] + sqr(lVal);
if lVal > lROImax[lRA] then
lROImax[lRA] := lVal;
if lVal < lROImin[lRA] then
lROImin[lRA] := lVal;
end;//nested AddVal()
begin
if fLabels.Count > 0 then begin
result := VoiDescriptivesLabels(VoiRawBytes);
exit;
end;
lLabelStr := 'Drawing ';
result := TStringList.Create();
result.Add('Background image: '+ShortName);
nVox := length(VoiRawBytes);
if (nVox <> (dim.x * dim.y * dim.z)) then exit;
//center of mass
for i := 1 to 3 do
lROISum[i] := 0;
i := 0;
nVoi := 0;
for z := 0 to (dim.z-1) do
for y := 0 to (dim.y-1) do
for x := 0 to (dim.x-1) do begin
if (VoiRawBytes[i] > 0) then begin
nVoi := nVoi + 1;
lROISum[1] := lROISum[1] + x;
lROISum[2] := lROISum[2] + y;
lROISum[3] := lROISum[3] + z;
end;
i := i + 1;
end;
if (nVoi = 0) then begin
result.Add('Drawing empty');
exit;
end;
lROISum[1] := lROISum[1] / nVoi;
lROISum[2] := lROISum[2] / nVoi;
lROISum[3] := lROISum[3] / nVoi;
//mm := FracMM(Vec3(lROISum[1],lROISum[2],lROISum[3]));
result.Add('Drawing Center of mass XYZvox '+RealToStr(lROISum[1],2)+'x'+RealToStr(lROISum[2],2)+'x'+RealToStr(lROISum[3],2));
mm := SliceMM(Vec3(lROISum[1],lROISum[2],lROISum[3]), Mat);
result.Add('Drawing Center of mass XYZmm '+RealToStr(mm.x,2)+'x'+RealToStr(mm.y,2)+'x'+RealToStr(mm.z,2));
//statistics
for i := 1 to 3 do begin
lROIVol[i] := 0;
lROISum[i] := 0;
lROISumSqr[i] := 0;
lROImax[i] := NegInfinity;
lROImin[i] := Infinity;
end;
for i := 0 to (nVox -1) do begin
if (VoiRawBytes[i] = 0) then continue;
lVal := VoxIntensity(i);
AddVal(1);
if lVal <> 0 then
AddVal(2);
if lVal > 0 then
AddVal(3);
end;
result.Add('Background image: '+ShortName);
for lInc := 1 to 3 do begin
if lROIVol[lInc] > 1 then begin
lSD := (lROISumSqr[lInc] - ((Sqr(lROISum[lInc]))/lROIVol[lInc]));
if (lSD > 0) then
lSD := Sqrt ( lSD/(lROIVol[lInc]-1))
else
lSD := 0;
end else
lSD := 0;
//next compute mean
if lROIVol[lInc] > 0 then begin
lROImean := lROISum[lInc]/lROIVol[lInc];
end else begin //2/2008
lROImin[lInc] := 0;
lROImax[lInc] := 0;
lROImean := 0;
end;
lcc := ((lROIVol[lInc]/1000)*fHdr.pixdim[1]*fHdr.pixdim[2]*fHdr.pixdim[3]);
case lInc of
3: lStr := 'VOI >0 ';
2: lStr := 'VOI <>0 ';
else lStr := 'VOI ';
end;
lStr := lStr+' nvox(cc)=min/mean/max=SD: '+inttostr(round(lROIVol[lInc]))+kTab+RealToStr(lCC,2)+kTab+'='+kTab+RealToStr(lROIMin[lInc],4)+kTab+realToStr(lROIMean,4)+kTab+realToStr(lROIMax[lInc],4)+kTab+'='+kTab+realtostr(lSD,4);
result.Add(lLabelStr+ lStr);
end;
end;
function isStat(statCode: integer): boolean;
begin
result := (statCode = kFUNC_ZT_TYPE) or (statCode = kFUNC_TT_TYPE) or (statCode = kFUNC_FT_TYPE) or (statCode = kFUNC_CT_TYPE) ;
end;
function TNIfTI.VoxIntensityString(vox: int64): string; overload;//return intensity or label of voxel at coordinate
var
f, q : single;
i: integer;
begin
if fHdr.datatype = kDT_RGB then begin
i := vox * 3;
result := format('%d,%d,%d',[fRawVolBytes[i],fRawVolBytes[i+1],fRawVolBytes[i+2]]);
exit;
end;
f := VoxIntensity(vox);
{$IFDEF AFNI}
if (f <> 0) and (length(AFNIs) > 0) and (fVolumeDisplayed < length(AFNIs)) and (isStat(AFNIs[fVolumeDisplayed].jv) ) then begin
q := VoxelIntensity2q(AFNIs[fVolumeDisplayed].FDRcurv, f);
q := max(q, 0.0001);
result := format(' %3.6g (q<%.4f)', [f,q]);
exit;
end;
{$ENDIF}
if IsLabels then begin
i := round(f);
if (i >= 0) and (i < fLabels.Count) then
result := fLabels[i]
else
result := inttostr(i);//''; //todo
end else
result := format(' %3.6g', [f]);
end;
function TNIfTI.VoxIntensityString(vox: TVec3i): string; overload;//return intensity of voxel at coordinate
var
i: int64;
begin
i := vox.x + (vox.y * dim.x) + (vox.z * (dim.x * dim.y));
result := '';
if (i < 0) or (i >= (dim.x * dim.y * dim.z)) then exit;
result := VoxIntensityString(i);
end;
function TNIfTI.VoxIntensity(frac: TVec3): single; overload;//return intensity of voxel fraction
begin
result := VoxIntensity(FracToSlice(Frac));
end;
procedure printf (str: AnsiString);
begin
{$IFNDEF WINDOWS}
writeln(str);
{$ELSE}
if IsConsole then writeln(str);
{$ENDIF}
end;
//{$DEFINE FASTCORREL}
{$IFDEF FASTCORREL} //~10% faster, but may be less precise for large values.
function correl(var v: TFloat32s): double;
var
i, n: int64;
sum, mn: double;
begin
n := length(v);
if n < 2 then exit(0); //avoid div by zero
//compute mean for seed
sum := 0.0;
for i := 0 to (n - 1) do
sum := sum + v[i];
mn := sum / n;
result := 0.0;
for i := 0 to (n - 1) do begin
v[i] := (v[i]-mn);
result := result + sqr(v[i]);
end;
end;
function TNIfTI.SeedCorrelationMap(vox: TVec3i): TFloat32s;
//Based on Numerical Recipes. One suspects this might have poor precision for large values
// https://www.johndcook.com/blog/2008/11/05/how-to-calculate-pearson-correlation-accurately/
var
vx, nVx, vol, nVol, volBytes: int64;
vol8: TUInt8s;
vol16: TInt16s;
x, y, vol32: TFloat32s;
sxx, syy, sxy: double;
r: double;
{$IFDEF TIMER}StartTime: TDateTime;{$ENDIF}
begin
setlength(result, 0);
if IsLabels then exit;
if volumesLoaded < 2 then exit;
if fHdr.bitpix = 24 then exit;
{$IFDEF TIMER}startTime := now;{$ENDIF}
nVx := fHdr.Dim[1]*fHdr.Dim[2]*fHdr.Dim[3];
volBytes := nVx * (fHdr.bitpix shr 3);
nVol := length(fRawVolBytes) div volBytes;
if nVol <> fVolumesLoaded then exit;
vx := vox.x + (vox.y * dim.x) + (vox.z * (dim.x * dim.y));
if (vx < 0) or (vx >= nVx) then exit;
setlength(result, nVx);
setlength(x, nVol);
setlength(y, nVol);
vol8 := fRawVolBytes;
vol16 := TInt16s(vol8);
vol32 := TFloat32s(vol8);
//load values for seed
if fHdr.datatype = kDT_UINT8 then begin
for vol := 0 to (nVol - 1) do
x[vol] := vol8[vx + (vol * nVx)];
end else if fHdr.datatype = kDT_INT16 then begin
for vol := 0 to (nVol - 1) do
x[vol] := vol16[vx + (vol * nVx)];
end else if fHdr.datatype = kDT_FLOAT then begin
for vol := 0 to (nVol - 1) do
x[vol] := vol32[vx + (vol * nVx)];
end;
sxx := correl(x);
//compute correlation for each voxel
for vx := 0 to (nVx - 1) do begin
if fHdr.datatype = kDT_UINT8 then begin
for vol := 0 to (nVol - 1) do
y[vol] := vol8[vx + (vol * nVx)];
end else if fHdr.datatype = kDT_INT16 then begin
for vol := 0 to (nVol - 1) do
y[vol] := vol16[vx + (vol * nVx)];
end else if fHdr.datatype = kDT_FLOAT then begin
for vol := 0 to (nVol - 1) do
y[vol] := vol32[vx + (vol * nVx)];
end;
syy := correl(y);
sxy := 0.0;
for vol := 0 to (nVol-1) do
sxy := sxy + (x[vol] * y[vol]);
r := sxy / sqrt(sxx*syy);
result[vx] := r;
end;
{$IFDEF TIMER}printf(format('Correl time %d',[MilliSecondsBetween(Now,startTime)]));{$ENDIF}
end;
{$ELSE}
//{$DEFINE ONEPASSCORREL}
{$IFDEF ONEPASSCORREL}
function correl(var v: TFloat32s): double;
//stable one-pass method: extra divisions mean this is slower than 2 pass
//http://jonisalonen.com/2013/deriving-welfords-method-for-computing-variance/
//aka Welford’s method for computing variance
//https://www.strchr.com/standard_deviation_in_one_pass
var
i, n: int64;
sd, mn, delta: double;
begin
n := length(v);
if n < 2 then exit(0.0); //avoid div by zero
//compute mean for seed
sd := 0.0;
mn := v[0];
for i := 1 to (n - 1) do begin
delta := v[i] - mn;
mn := mn + delta / (i+1);
sd := sd + delta*(v[i]- mn);
end;
sd := sqrt(sd / (n - 1));
if (sd = 0.0) then exit; //no variance
sd := 1/sd;
for i := 0 to (n - 1) do
v[i] := (v[i]-mn)*sd;
result := sd;
end;
{$ELSE}
function correl(var v: TFloat32s): double;
var
i, n: int64;
sd, sum, mn: double;
begin
n := length(v);
if n < 2 then exit(0.0); //avoid div by zero
//compute mean for seed
sd := 0.0;
sum := 0.0;
for i := 0 to (n - 1) do
sum := sum + v[i];
mn := sum / n;
for i := 0 to (n - 1) do
sd := sd + sqr(v[i] - mn);
sd := sqrt(sd / (n - 1));
if (sd = 0.0) then exit(0.0); //no variance
sd := 1/sd;
for i := 0 to (n - 1) do
v[i] := (v[i]-mn)*sd;
result := sd;
end;
{$ENDIF}//ONEPASSCORREL
{$DEFINE FASTCORREL2}
{$IFDEF FASTCORREL2}
function correlR(var x, y: TFloat32s): single;
//assumes X already processed with correl()
// about 10% faster in practice than running correl() twice
var
i, n: int64;
r, sum, mn, sd: double;
begin
n := length(x);
if (n < 2) or (n <> length(y)) then exit(0.0); //avoid div by zero
sum := 0.0;
for i := 0 to (n - 1) do
sum := sum + y[i];
mn := sum / n;
sd := 0.0;
for i := 0 to (n - 1) do
sd := sd + sqr(y[i] - mn);
sd := sqrt(sd / (n - 1));
if (sd = 0) then exit(0.0);
sd := 1/sd;
r := 0.0;
for i := 0 to (n - 1) do begin
r := r + (x[i] * ((y[i]-mn)*sd));
end;
r := r / (n - 1);
exit(r);
end;
{$ENDIF} //FASTCORREL2
procedure transposeXY( img3Din: TFloat32s; var img3Dout: TFloat32s; var nxp, nyp, nz: int64);
//transpose X and Y dimensions: rows <-> columns
var
nx, ny, vi, x, xo, y, z, zo: int64;
begin
nx := nxp;
ny := nyp;
vi := 0; //volume offset
for z := 0 to (nz -1) do begin
zo := z * nx * ny;
for y := 0 to (ny -1) do begin
xo := 0;
for x := 0 to (nx -1) do begin
img3Dout[zo + xo + y] := img3Din[vi];
xo += ny;
vi += 1;
end;
end;
end;
nxp := ny;
nyp := nx;
end;
procedure transposeXZ( img3Din: TFloat32s; var img3Dout: TFloat32s; var nxp, ny, nzp: int64);
//transpose X and Z dimensions: slices <-> columns
var
nx, nyz, nz, vi, x, y, yo, z, zo: int64;
begin
nx := nxp;
nz := nzp;
nyz := ny * nzp;
vi := 0; //volume offset
for z := 0 to (nzp -1) do begin
for y := 0 to (ny -1) do begin
yo := y * nz;
zo := 0;
for x := 0 to (nx -1) do begin
img3Dout[z + yo + zo] := img3Din[vi];
zo += nyz;
vi += 1;
end;
end;
end;
nxp := nz;
nzp := nx;
end;
//Gaussian blur, both serial and parallel variants, https://github.com/neurolabusc/niiSmooth
procedure blurS(var img: TFloat32s; nx, ny: integer; xmm, Sigmamm: single);
var
sigma, expd, wt, sum: single;
cutoffvox, i, j, y: integer;
k, kWeight, tmp: TFloat32s;
kStart, kEnd: TInt32s;
x, imgp: int64;
begin
//make kernels
if ((xmm = 0) or (nx < 2) or (ny < 1) or (Sigmamm <= 0.0)) then
exit();
sigma := (Sigmamm / xmm); //mm to vox
//cutoffvox := ceil(4 * sigma); //filter width to 4 sigma (FSL): faster but lower precision AFNI_BLUR_FIRFAC = 2.5
cutoffvox := ceil(2.5 * sigma); //filter width to 6 sigma: faster but lower precision AFNI_BLUR_FIRFAC = 2.5
//printf(".Blur Cutoff (%g) %d\n", 4*sigma, cutoffvox);
//validated on SPM12's 1.5mm isotropic mask_ICV.nii (discrete jump in number of non-zero voxels)
//fslmaths mask -s 2.26 f6.nii //Blur Cutoff (6.02667) 7
//fslmaths mask -s 2.24 f4.nii //Blur Cutoff (5.97333) 6
cutoffvox := MAX(cutoffvox, 1);
setlength(k, cutoffvox + 1); //FIR Gaussian
expd := 2 * sigma * sigma;
for i := 0 to cutoffvox do
k[i] := exp(-1.0 * (i * i) / expd);
//calculate start, end for each voxel in
setlength(kStart, nx); //-cutoff except left left columns, e.g. 0, -1, -2... cutoffvox
setlength(kEnd, nx); //+cutoff except right columns
setlength(kWeight, nx); //ensure sum of kernel = 1.0
for i := 0 to (nx - 1) do begin
kStart[i] := MAX(-cutoffvox, -i); //do not read below 0
kEnd[i] := MIN(cutoffvox, nx - i - 1); //do not read beyond final columnn
if ((i > 0) and (kStart[i] = (kStart[i - 1])) and (kEnd[i] = (kEnd[i - 1]))) then begin //reuse weight
kWeight[i] := kWeight[i - 1];
continue;
end;
wt := 0.0;
for j := kStart[i] to kEnd[i] do
wt += k[abs(j)];
kWeight[i] := 1 / wt;
//printf("%d %d->%d %g\n", i, kStart[i], kEnd[i], kWeight[i]);
end;
//apply kernel to each row
setlength(tmp, nx); //input values prior to blur
imgp := 0; //pointer
for y := 0 to (ny - 1) do begin
//tmp := copy(img, imgp, nx);
Move(img[imgp], tmp[0],nx*4);//src,dst
for x := 0 to (nx - 1) do begin
sum := 0;
for i := kStart[x] to kEnd[x] do
sum += tmp[x + i] * k[abs(i)];
img[imgp + x] := sum * kWeight[x];
end;
imgp += nx;
end; //blurX
//free kernel
tmp := nil;
k := nil;
kStart := nil;
kEnd := nil;
kWeight := nil;
end;
function TNIfTI.nifti_smooth_gauss(var img: TFloat32s; SigmammX, SigmammY, SigmammZ: single; nVol: int64): integer;
label
DO_Y_BLUR, DO_Z_BLUR;
var
nRow, nvox3D, nx, ny, nz, v, vo: int64;
dx, dy, dz: single;
img3D: TFloat32s;
begin
//https://github.com/afni/afni/blob/699775eba3c58c816d13947b81cf3a800cec606f/src/edt_blur.c
if (nVol <> 1) then
exit(123);//only supports 3D
nx := fHdr.Dim[1];
ny := fHdr.Dim[2];
nz := fHdr.Dim[3];
dx := abs(fHdr.PixDim[1]);
dy := abs(fHdr.PixDim[2]);
dz := abs(fHdr.PixDim[3]);
if ((nx < 2) or (ny < 2) or (nz < 1) or (dx = 0) or (dy = 0) or (dz = 0)) then
exit( 1);
//if (nim->datatype != DT_CALC) then
// exit( 1);
if ((SigmammX = 0) and (SigmammY = 0) and (SigmammZ = 0)) then
exit(0); //all done: no smoothing, e.g. small kernel for difference of Gaussian
if (SigmammX < 0) then //negative values for voxels, not mm
SigmammX := -SigmammX * dx;
if (SigmammY < 0) then //negative values for voxels, not mm
SigmammY := -SigmammY * dy;
if (SigmammZ < 0) then //negative values for voxels, not mm
SigmammZ := -SigmammZ * dz;
nvox3D := nx * ny * MAX(nz, 1);
if ((nvox3D * nVol) < 1) then
exit( 1);
if (SigmammX <= 0.0) then
goto DO_Y_BLUR;
//BLUR X
nRow := ny * nz * nVol;
blurS(img, nx, nRow, dx, SigmammX);
//BLUR Y
DO_Y_BLUR:
if (SigmammY <= 0.0) then
goto DO_Z_BLUR;
nRow := nx * nz; //transpose XYZ to YXZ and blur Y columns with XZ Rows
setlength(img3D, nvox3D);
for v := 0 to (nVol - 1) do begin //transpose each volume separately
vo := v * nvox3D; //volume offset
transposeXY(@img[vo], img3D, nx, ny, nz);
blurS(img3D, fHdr.Dim[2], nRow, dy, SigmammY);
transposeXY(img3D, img, nx, ny, nz);
end;
img3D := nil;
//BLUR Z
DO_Z_BLUR:
if ((SigmammZ <= 0.0) or (nz < 2)) then
exit(0); //all done!
nRow := nx * ny; //transpose XYZ to ZXY and blur Z columns with XY Rows
setlength(img3D, nvox3D);
for v := 0 to (nVol - 1) do begin //transpose each volume separately
vo := v * nvox3D; //volume offset
transposeXZ(@img[vo], img3D, nx, ny, nz);
blurS(img3D, fHdr.Dim[3], nRow, dz, SigmammZ);
transposeXZ(img3D, img, nx, ny, nz);
end;
img3D := nil;
exit(0);
end;
function TNIfTI.EdgeMap(isSmooth: boolean): TUInt8s;
var
i, vx, nx, ny, nz, nxy, nxyz, x, y, z: int64;
vol8: TUInt8s;
vol25, vol40: TFloat32s;
val: single;
begin
setlength(result, 0);