-
Notifications
You must be signed in to change notification settings - Fork 24
/
nifti_hdr.pas
executable file
·1299 lines (1241 loc) · 47.9 KB
/
nifti_hdr.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_hdr;
{$include opts.inc}
{$D-,O+,Q-,R-,S-}
interface
uses
{$H+}
{$Include isgui.inc}
{$IFNDEF FPC}
gziod,
{$ELSE}
gzio2,
{$ENDIF}
{$IFNDEF FPC} Windows, {$ENDIF}
{$IFDEF DGL} dglOpenGL, {$ELSE} gl, {$ENDIF}
nifti_types, clipbrd,
define_types,SysUtils,nii_mat,nifti_foreign, //GLMisc, //GLTexture, GLContext,
{$IFDEF GUI}dialogs;{$ELSE} dialogsx;{$ENDIF}
type
TMRIcroHdr = record //Next: analyze Format Header structure
NIFTIhdr : TNIFTIhdr;
AutoBalMinUnscaled,AutoBalMaxUnscaled
,WindowScaledMin,WindowScaledMax
//,GlMaxNegUnscaledS,GlMinPosUnscaledS //<- used for thresholded images
,GlMinUnscaledS,GlMaxUnscaledS,Zero8Bit,Slope8bit: double; //brightness and contrast
NIfTItransform,DiskDataNativeEndian,UsesCustomPalette,RGB,LutFromZero,LutVisible: boolean;
HdrFileName,ImgFileName: string;
gzBytes: Int64;
//ClusterSize,
LUTindex,ScrnBufferItems,ImgBufferItems,RenderBufferItems,ImgBufferBPP,RenderDim: longint;
ImgBufferUnaligned: Pointer; //raw address of Image Buffer: address may not be aligned
ScrnBuffer,ImgBuffer: Bytep;
LUT: TLUT;
Mat: TMatrix;
end; //TNIFTIhdr Header Structure
function NIFTIvolumes (var lFilename: string): integer;
function FixDataType (var lHdr: TNIFTIhdr): boolean; overload;
function FixDataType (var lHdr: TMRIcroHdr): boolean; overload;
function ComputeImageDataBytes (var lHdr: TMRIcroHdr): longint; //size of image data in bytes
function ComputeImageDataBytes8bpp (var lHdr: TMRIcroHdr): longint; //size of image as 32-bit per voxel data in bytes
function ComputeImageDataBytes32bpp (var lHdr: TMRIcroHdr): longint; //size of image as 32-bit per voxel data in bytes
procedure NIFTIhdr_SwapBytes (var lAHdr: TNIFTIhdr); //Swap Byte order for the Analyze type
//procedure NIFTIhdr_ClearHdr (var lHdr: TNIfTIHdr); overload; //set all values of header to something reasonable
procedure NIFTIhdr_ClearHdr (out lHdr: TMRIcroHdr); overload;//set all values of header to something reasonable
function NIFTIhdr_LoadHdr (var lFilename: string; out lHdr: TMRIcroHdr; lFlipYZ: boolean): boolean;
function NIFTIhdr_SaveHdr (var lFilename: string; var lHdr: TMRIcroHdr; lAllowOverwrite: boolean): boolean; overload;
function NIFTIhdr_SaveHdr (var lFilename: string; var lHdr: TNIFTIHdr; lAllowOverwrite,lSPM2: boolean): boolean; overload;
//procedure NIFTIhdr_SetIdentityMatrix (var lHdr: TMRIcroHdr); //create neutral rotation matrix
function CopyNiftiHdr (var lInHdr,lOutHdr: TNIFTIhdr): boolean;
function IsNIfTIHdrExt (var lFName: string):boolean; //1494
function IsNifTiMagic (var lHdr: TNIFTIhdr): boolean;
procedure WriteNiftiMatrix (var lHdr: TNIFTIhdr;
m11,m12,m13,m14,
m21,m22,m23,m24,
m31,m32,m33,m34: Single);
function niftiflip (var lHdr: TMRIcrohdr): boolean;
procedure nifti_mat44_to_quatern( lR :TMatrix;
var qb, qc, qd,
qx, qy, qz,
dx, dy, dz, qfac : single);
function IsVOIROIExt (var lFName: string):boolean;
implementation
uses mainunit;
function NIFTIvolumes (var lFilename: string): integer;
var lHdr: TMRIcroHdr;
begin
result := -1;
if not NIFTIhdr_LoadHdr (lFilename, lHdr, gPrefs.FlipYZ) then
exit;
result := lHdr.NIFTIhdr.dim[4];
if (result < 1) then result := 1;
end;
function NIFTIhdr_SaveHdr (var lFilename: string; var lHdr: TNIFTIHdr; lAllowOverwrite,lSPM2: boolean): boolean; overload;
var lOutHdr: TNIFTIhdr;
lExt: string;
lF: File;
lOverwrite: boolean;
begin
lOverwrite := false; //will we overwrite existing file?
result := false; //assume failure
if lHdr.magic = kNIFTI_MAGIC_EMBEDDED_HDR then begin
lExt := UpCaseExt(lFileName);
if (lExt = '.GZ') or (lExt = '.NII.GZ') then begin
showmessage('Unable to save .nii.gz headers (first ungzip your image if you wish to edit the header)');
exit;
end;
lFilename := changefileext(lFilename,'.nii')
end else begin
lFilename := changefileext(lFilename,'.hdr');
lHdr.magic := kNIFTI_MAGIC_SEPARATE_HDR;
end;
if ((sizeof(TNIFTIhdr))> DiskFreeEx(lFileName)) then begin
ShowMessage('There is not enough free space on the destination disk to save the header. '+kCR+
lFileName+ kCR+' Bytes Required: '+inttostr(sizeof(TNIFTIhdr)) );
exit;
end;
if Fileexists(lFileName) then begin
if lAllowOverwrite then begin
case MessageDlg('Do you wish to modify the existing file '+lFilename+'?', mtConfirmation,[mbYes, mbNo], 0) of { produce the message dialog box }
6: lOverwrite := true; //6= mrYes, 7=mrNo... not sure what this is for Linux. Hardcoded as we do not include Form values
end;//case
end else
showmessage('Error: the file '+lFileName+' already exists.');
if not lOverwrite then Exit;
end;
if lHdr.magic = kNIFTI_MAGIC_EMBEDDED_HDR then
if lHdr.vox_offset < sizeof(TNIFTIHdr) then
lHdr.vox_offset := sizeof(TNIFTIHdr); //embedded images MUST start after header
if lHdr.magic = kNIFTI_MAGIC_SEPARATE_HDR then
lHdr.vox_offset := 0; //embedded images MUST start after header
if lSPM2 then begin //SPM2 does not recognize NIfTI - origin values will be wrong
lHdr.magic := 0;
end;
result := true;
move(lHdr, lOutHdr, sizeof(lOutHdr));
Filemode := 1;
AssignFile(lF, lFileName); {WIN}
if lOverwrite then //this allows us to modify just the 348byte header of an existing NII header without touching image data
Reset(lF,sizeof(TNIFTIhdr))
else
Rewrite(lF,sizeof(TNIFTIhdr));
BlockWrite(lF,lOutHdr, 1 {, NumWritten});
CloseFile(lF);
Filemode := 2;
end; //func NIFTIhdr_SaveHdr
function CopyNiftiHdr (var lInHdr,lOutHdr: TNIFTIhdr): boolean;
begin
move(lInHdr,lOutHdr,sizeof(TNIFTIhdr));
result := true;
end;
function IsVOIROIExt (var lFName: string):boolean;
var
lExt: string;
begin
lExt := UpCaseExt(lFName);
if (lExt = '.VOI') or (lExt = '.ROI') then
result := true
else
result := false;
end;
procedure WriteNiftiMatrix (var lHdr: TNIFTIhdr;
m11,m12,m13,m14,
m21,m22,m23,m24,
m31,m32,m33,m34: Single);
begin
with lHdr do begin
srow_x[0] := m11;
srow_x[1] := m12;
srow_x[2] := m13;
srow_x[3] := m14;
srow_y[0] := m21;
srow_y[1] := m22;
srow_y[2] := m23;
srow_y[3] := m24;
srow_z[0] := m31;
srow_z[1] := m32;
srow_z[2] := m33;
srow_z[3] := m34;
end; //with lHdr
end;
procedure FromMatrix (M: TMatrix; out m11,m12,m13, m21,m22,m23,
m31,m32,m33: DOUBLE) ;
BEGIN
m11 := M.Matrix[1,1];
m12 := M.Matrix[1,2];
m13 := M.Matrix[1,3];
m21 := M.Matrix[2,1];
m22 := M.Matrix[2,2];
m23 := M.Matrix[2,3];
m31 := M.Matrix[3,1];
m32 := M.Matrix[3,2];
m33 := M.Matrix[3,3];
END {FromMatrix3D};
procedure WriteNiftiMatrix2 (var lHdr: TNIFTIhdr;
M: TMatrix);
begin
with lHdr do begin
srow_x[0] := M.Matrix[1,1];
srow_x[1] := M.Matrix[1,2];
srow_x[2] := M.Matrix[1,3];
srow_x[3] := M.Matrix[1,4];
srow_y[0] := M.Matrix[2,1];
srow_y[1] := M.Matrix[2,2];
srow_y[2] := M.Matrix[2,3];
srow_y[3] := M.Matrix[2,4];
srow_z[0] := M.Matrix[3,1];
srow_z[1] := M.Matrix[3,2];
srow_z[2] := M.Matrix[3,3];
srow_z[3] := M.Matrix[3,4];
end; //with lHdr
end;
function niftiflip (var lHdr: TMRIcrohdr): boolean;
var
lR,lF,lO: TMatrix;
begin
result := false;
if (lHdr.NIFTIhdr.srow_x[0]+lHdr.NIFTIhdr.srow_y[0]+lHdr.NIFTIhdr.srow_z[0]) > 0 then
exit;
result := true;
lR := Matrix3D (
lHdr.NIFTIhdr.srow_x[0],lHdr.NIFTIhdr.srow_x[1],lHdr.NIFTIhdr.srow_x[2],lHdr.NIFTIhdr.srow_x[3],
lHdr.NIFTIhdr.srow_y[0],lHdr.NIFTIhdr.srow_y[1],lHdr.NIFTIhdr.srow_y[2],lHdr.NIFTIhdr.srow_y[3],
lHdr.NIFTIhdr.srow_z[0],lHdr.NIFTIhdr.srow_z[1],lHdr.NIFTIhdr.srow_z[2],lHdr.NIFTIhdr.srow_z[3]);
lF := Matrix3D (-1,0,0,0, 0,1,0,0, 0,0,1,0 );
lO := MultiplyMatrices(lR,lF);
WriteNiftiMatrix2(lHdr.NIFTIhdr,lO);
end;
function IsNifTiMagic (var lHdr: TNIFTIhdr): boolean;
begin
if (lHdr.magic =kNIFTI_MAGIC_SEPARATE_HDR) or (lHdr.Magic = kNIFTI_MAGIC_EMBEDDED_HDR ) then
result := true
else
result :=false; //analyze
end;
function IsNIfTIHdrExt (var lFName: string):boolean;
var
lExt: string;
begin
lExt := UpCaseExt(lFName);
if (lExt='.NII') or (lExt = '.HDR') or (lExt = '.NII.GZ') or (lExt = '.VOI') then
result := true
else
result := false;
end;
function ComputeImageDataBytes32bpp (var lHdr: TMRIcroHdr): integer;
var
lDim, lBytes : integer;
begin
//result := 0;
with lHdr.NIFTIhdr do begin
if Dim[0] < 1 then begin
showmessage('NIFTI format error: datasets must have at least one dimension (dim[0] < 1).');
Dim[0] := 3;
//exit;
end;
lBytes := 4; //bits per voxel
for lDim := 1 to 3 {Dim[0]} do
lBytes := lBytes * Dim[lDim];
end; //with niftihdr
result := lBytes; //+7 to ensure binary data not clipped
end; //func ComputeImageDataBytes32bpp
function ComputeImageDataBytes8bpp (var lHdr: TMRIcroHdr): integer;
var
lDim, lBytes : integer;
begin
result := 0;
with lHdr.NIFTIhdr do begin
if Dim[0] < 1 then begin
showmessage('NIFTI format error: datasets must have at least one dimension (dim[0] < 1).');
exit;
end;
lBytes := 1; //bits per voxel
for lDim := 1 to 3 {Dim[0]} do
lBytes := lBytes * Dim[lDim];
end; //with niftihdr
result := lBytes;
end; //func ComputeImageDataBytes8bpp
function ComputeImageDataBytes (var lHdr: TMRIcroHdr): integer;
var
lDim : integer;
lSzInBits : Int64;
begin
result := 0;
with lHdr.NIFTIhdr do begin
if Dim[0] < 1 then begin
showmessage('NIFTI format error: datasets must have at least one dimension (dim[0] < 1).');
exit;
end;
lSzInBits := bitpix; //bits per voxel
//showmessage(inttostr(Dim[0]));
for lDim := 1 to 3 {Dim[0]} do
lSzInBits := lSzInBits * Dim[lDim];
end; //with niftihdr
result := (lSzInBits + 7) div 8; //+7 to ensure binary data not clipped
end; //func ComputeImageDataBytes
function orthogonalMatrix(var lHdr: TMRIcroHdr): boolean;
var
lM: TMatrix;
lRow,lCol,lN0: integer;
begin
result := false;
lM := Matrix3D (
lHdr.NIFTIhdr.srow_x[0],lHdr.NIFTIhdr.srow_x[1],lHdr.NIFTIhdr.srow_x[2],lHdr.NIFTIhdr.srow_x[3],
lHdr.NIFTIhdr.srow_y[0],lHdr.NIFTIhdr.srow_y[1],lHdr.NIFTIhdr.srow_y[2],lHdr.NIFTIhdr.srow_y[3],
lHdr.NIFTIhdr.srow_z[0],lHdr.NIFTIhdr.srow_z[1],lHdr.NIFTIhdr.srow_z[2],lHdr.NIFTIhdr.srow_z[3]);
for lRow := 1 to 3 do begin
lN0 := 0;
for lCol := 1 to 3 do
if lM.matrix[lRow,lCol] = 0 then
inc(lN0);
if lN0 <> 2 then exit; //exactly two values are zero
end;
for lCol := 1 to 3 do begin
lN0 := 0;
for lRow := 1 to 3 do
if lM.matrix[lRow,lCol] = 0 then
inc(lN0);
if lN0 <> 2 then exit; //exactly two values are zero
end;
result := true;
end;
procedure showmat(lMat: TMatrix);
begin
clipboard.AsText:= format('o=[%g %g %g %g; %g %g %g %g; %g %g %g %g; 0 0 0 1]',[
lMat.matrix[1,1], lMat.matrix[1,2], lMat.matrix[1,3], lMat.matrix[1,4],
lMat.matrix[2,1], lMat.matrix[2,2], lMat.matrix[2,3], lMat.matrix[2,4],
lMat.matrix[3,1], lMat.matrix[3,2], lMat.matrix[3,3], lMat.matrix[3,4]
]);
end;
function EmptyMatrix(var lHdr: TMRIcroHdr): boolean;
var
lM: TMatrix;
lRow,lCol: integer;
isUnity: boolean;
begin
result := false;
lM := Matrix3D (
lHdr.NIFTIhdr.srow_x[0],lHdr.NIFTIhdr.srow_x[1],lHdr.NIFTIhdr.srow_x[2],lHdr.NIFTIhdr.srow_x[3],
lHdr.NIFTIhdr.srow_y[0],lHdr.NIFTIhdr.srow_y[1],lHdr.NIFTIhdr.srow_y[2],lHdr.NIFTIhdr.srow_y[3],
lHdr.NIFTIhdr.srow_z[0],lHdr.NIFTIhdr.srow_z[1],lHdr.NIFTIhdr.srow_z[2],lHdr.NIFTIhdr.srow_z[3]);
isUnity := true;
for lRow := 1 to 3 do begin {3/2008}
for lCol := 1 to 4 do begin
if (lRow = lCol) then begin
if lM.matrix[lRow,lCol] <> 1 then
isUnity := false;
end else begin
if lM.matrix[lRow,lCol] <> 0 then
isUnity := false;
end// unity matrix does not count - mriconvert creates bogus [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 0]
end; //each col
end;//each row
if isUnity then exit;
for lRow := 1 to 3 do begin {3/2008}
for lCol := 1 to 4 do begin
if lM.matrix[lRow,lCol] <> 0 then
exit;
end; //each col
end;//each row
(* for lRow := 1 to 3 do
for lCol := 1 to 4 do
if lM.matrix[lRow,lCol] <> 0 then
exit;*)
result := true;
end;
procedure nifti_quatern_to_mat44( out lR :TMatrix;
var qb, qc, qd,
qx, qy, qz,
dx, dy, dz, qfac : single);
var
a,b,c,d,xd,yd,zd: double;
begin
//a := qb;
b := qb;
c := qc;
d := qd;
//* last row is always [ 0 0 0 1 ] */
lR.matrix[4,1] := 0;
lR.matrix[4,2] := 0;
lR.matrix[4,3] := 0;
lR.matrix[4,4] := 1;
//* compute a parameter from b,c,d */
a := 1.0 - (b*b + c*c + d*d) ;
if( a < 1.e-7 ) then begin//* special case */
a := 1.0 / sqrt(b*b+c*c+d*d) ;
b := b*a ; c := c*a ; d := d*a ;//* normalize (b,c,d) vector */
a := 0.0 ;//* a = 0 ==> 180 degree rotation */
end else begin
a := sqrt(a) ; //* angle = 2*arccos(a) */
end;
//* load rotation matrix, including scaling factors for voxel sizes */
if dx > 0 then
xd := dx
else
xd := 1;
if dy > 0 then
yd := dy
else
yd := 1;
if dz > 0 then
zd := dz
else
zd := 1;
if( qfac < 0.0 ) then zd := -zd ;//* left handedness? */
lR.matrix[1,1]:= (a*a+b*b-c*c-d*d) * xd ;
lR.matrix[1,2]:= 2.0 * (b*c-a*d ) * yd ;
lR.matrix[1,3]:= 2.0 * (b*d+a*c ) * zd ;
lR.matrix[2,1]:= 2.0 * (b*c+a*d ) * xd ;
lR.matrix[2,2]:= (a*a+c*c-b*b-d*d) * yd ;
lR.matrix[2,3]:= 2.0 * (c*d-a*b ) * zd ;
lR.matrix[3,1]:= 2.0 * (b*d-a*c ) * xd ;
lR.matrix[3,2]:= 2.0 * (c*d+a*b ) * yd ;
lR.matrix[3,3]:= (a*a+d*d-c*c-b*b) * zd ;
//* load offsets */
lR.matrix[1,4]:= qx ;
lR.matrix[2,4]:= qy ;
lR.matrix[3,4]:= qz ;
end;
function HasQuat( var lHdr: TNIfTIHdr ): boolean;
//var lR :TMatrix;
begin
result := false;
if (lHdr.qform_code <= kNIFTI_XFORM_UNKNOWN) or (lHdr.qform_code > kNIFTI_XFORM_MNI_152) then
exit;
result := true;
end;
function Quat2Mat( var lHdr: TNIfTIHdr ): boolean;
var lR :TMatrix;
begin
result := false;
if (lHdr.qform_code <= kNIFTI_XFORM_UNKNOWN) or (lHdr.qform_code > kNIFTI_XFORM_MNI_152) then
exit;
result := true;
nifti_quatern_to_mat44(lR,lHdr.quatern_b,lHdr.quatern_c,lHdr.quatern_d,
lHdr.qoffset_x,lHdr.qoffset_y,lHdr.qoffset_z,
lHdr.pixdim[1],lHdr.pixdim[2],lHdr.pixdim[3],
lHdr.pixdim[0]);
lHdr.srow_x[0] := lR.matrix[1,1];
lHdr.srow_x[1] := lR.matrix[1,2];
lHdr.srow_x[2] := lR.matrix[1,3];
lHdr.srow_x[3] := lR.matrix[1,4];
lHdr.srow_y[0] := lR.matrix[2,1];
lHdr.srow_y[1] := lR.matrix[2,2];
lHdr.srow_y[2] := lR.matrix[2,3];
lHdr.srow_y[3] := lR.matrix[2,4];
lHdr.srow_z[0] := lR.matrix[3,1];
lHdr.srow_z[1] := lR.matrix[3,2];
lHdr.srow_z[2] := lR.matrix[3,3];
lHdr.srow_z[3] := lR.matrix[3,4];
lHdr.sform_code := 1;
showmat(lR);
end;
function nifti_mat33_determ( R: TMatrix ):double; //* determinant of 3x3 matrix */
begin
result := r.matrix[1,1]*r.matrix[2,2]*r.matrix[3,3]
-r.matrix[1,1]*r.matrix[3,2]*r.matrix[2,3]
-r.matrix[2,1]*r.matrix[1,2]*r.matrix[3,3]
+r.matrix[2,1]*r.matrix[3,2]*r.matrix[1,3]
+r.matrix[3,1]*r.matrix[1,2]*r.matrix[2,3]
-r.matrix[3,1]*r.matrix[2,2]*r.matrix[1,3] ;
end;
function nifti_mat33_rownorm( A: TMatrix ): single; //* max row norm of 3x3 matrix */
var
r1,r2,r3: single ;
begin
r1 := abs(A.matrix[1,1])+abs(A.matrix[1,2])+abs(A.matrix[1,3]) ;
r2 := abs(A.matrix[2,1])+abs(A.matrix[2,2])+abs(A.matrix[2,3]) ;
r3 := abs(A.matrix[3,1])+abs(A.matrix[3,2])+abs(A.matrix[3,3]) ;
if( r1 < r2 ) then r1 := r2 ;
if( r1 < r3 ) then r1 := r3 ;
result := r1 ;
end;
function nifti_mat33_colnorm( A: TMatrix ): single; //* max column norm of 3x3 matrix */
var
r1,r2,r3: single ;
begin
r1 := abs(A.matrix[1,1])+abs(A.matrix[2,1])+abs(A.matrix[3,1]) ;
r2 := abs(A.matrix[1,2])+abs(A.matrix[2,2])+abs(A.matrix[3,2]) ;
r3 := abs(A.matrix[1,3])+abs(A.matrix[2,3])+abs(A.matrix[3,3]) ;
if( r1 < r2 ) then r1 := r2 ;
if( r1 < r3 ) then r1 := r3 ;
result := r1 ;
end;
function nifti_mat33_inverse( R: TMatrix ): TMatrix; //* inverse of 3x3 matrix */
var
r11,r12,r13,r21,r22,r23,r31,r32,r33 , deti: double ;
Q: TMatrix ;
begin
FromMatrix(R,r11,r12,r13,r21,r22,r23,r31,r32,r33);
deti := r11*r22*r33-r11*r32*r23-r21*r12*r33
+r21*r32*r13+r31*r12*r23-r31*r22*r13 ;
if( deti <> 0.0 ) then deti := 1.0 / deti ;
Q.matrix[1,1] := deti*( r22*r33-r32*r23) ;
Q.matrix[1,2] := deti*(-r12*r33+r32*r13) ;
Q.matrix[1,3] := deti*( r12*r23-r22*r13) ;
Q.matrix[2,1] := deti*(-r21*r33+r31*r23) ;
Q.matrix[2,2] := deti*( r11*r33-r31*r13) ;
Q.matrix[2,3] := deti*(-r11*r23+r21*r13) ;
Q.matrix[3,1] := deti*( r21*r32-r31*r22) ;
Q.matrix[3,2] := deti*(-r11*r32+r31*r12) ;
Q.matrix[3,3] := deti*( r11*r22-r21*r12) ;
result := Q;
end;
function nifti_mat33_polar( A: TMatrix ): TMatrix;
var
dif: single;
k: integer;
X , Y , Z: TMatrix ;
alp,bet,gam,gmi : single;
begin
dif:=1.0 ;
k:=0 ;
X := A ;
// force matrix to be nonsingular
//reportmatrix('x',X);
gam := nifti_mat33_determ(X) ;
while( gam = 0.0 )do begin //perturb matrix
gam := 0.00001 * ( 0.001 + nifti_mat33_rownorm(X) ) ;
X.matrix[1,1] := X.matrix[1,1]+gam ;
X.matrix[2,2] := X.matrix[2,2]+gam ;
X.matrix[3,3] := X.matrix[3,3] +gam ;
gam := nifti_mat33_determ(X) ;
end;
while true do begin
Y := nifti_mat33_inverse(X) ;
if( dif > 0.3 )then begin // far from convergence
alp := sqrt( nifti_mat33_rownorm(X) * nifti_mat33_colnorm(X) ) ;
bet := sqrt( nifti_mat33_rownorm(Y) * nifti_mat33_colnorm(Y) ) ;
gam := sqrt( bet / alp ) ;
gmi := 1.0 / gam ;
end else begin
gam := 1.0;
gmi := 1.0 ; //close to convergence
end;
Z.matrix[1,1] := 0.5 * ( gam*X.matrix[1,1] + gmi*Y.matrix[1,1] ) ;
Z.matrix[1,2] := 0.5 * ( gam*X.matrix[1,2] + gmi*Y.matrix[2,1] ) ;
Z.matrix[1,3] := 0.5 * ( gam*X.matrix[1,3] + gmi*Y.matrix[3,1] ) ;
Z.matrix[2,1] := 0.5 * ( gam*X.matrix[2,1] + gmi*Y.matrix[1,2] ) ;
Z.matrix[2,2] := 0.5 * ( gam*X.matrix[2,2] + gmi*Y.matrix[2,2] ) ;
Z.matrix[2,3] := 0.5 * ( gam*X.matrix[2,3] + gmi*Y.matrix[3,2] ) ;
Z.matrix[3,1] := 0.5 * ( gam*X.matrix[3,1] + gmi*Y.matrix[1,3] ) ;
Z.matrix[3,2] := 0.5 * ( gam*X.matrix[3,2] + gmi*Y.matrix[2,3] ) ;
Z.matrix[3,3] := 0.5 * ( gam*X.matrix[3,3] + gmi*Y.matrix[3,3] ) ;
dif := abs(Z.matrix[1,1]-X.matrix[1,1])+abs(Z.matrix[1,2]-X.matrix[1,2])
+abs(Z.matrix[1,3]-X.matrix[1,3])+abs(Z.matrix[2,1]-X.matrix[2,1])
+abs(Z.matrix[2,2]-X.matrix[2,2])+abs(Z.matrix[2,3]-X.matrix[2,3])
+abs(Z.matrix[3,1]-X.matrix[3,1])+abs(Z.matrix[3,2]-X.matrix[3,2])
+abs(Z.matrix[3,3]-X.matrix[3,3]) ;
k := k+1 ;
if( k > 100) or (dif < 3.e-6 ) then begin
result := Z;
break ; //convergence or exhaustion
end;
X := Z ;
end;
result := Z ;
end;
procedure nifti_mat44_to_quatern( lR :TMatrix;
var qb, qc, qd,
qx, qy, qz,
dx, dy, dz, qfac : single);
var
r11,r12,r13 , r21,r22,r23 , r31,r32,r33, xd,yd,zd , a,b,c,d : double;
P,Q: TMatrix; //3x3
begin
(* offset outputs are read write out of input matrix *)
qx := lR.matrix[1,4];
qy := lR.matrix[2,4];
qz := lR.matrix[3,4];
(* load 3x3 matrix into local variables *)
FromMatrix(lR,r11,r12,r13,r21,r22,r23,r31,r32,r33);
(* compute lengths of each column; these determine grid spacings *)
xd := sqrt( r11*r11 + r21*r21 + r31*r31 ) ;
yd := sqrt( r12*r12 + r22*r22 + r32*r32 ) ;
zd := sqrt( r13*r13 + r23*r23 + r33*r33 ) ;
(* if a column length is zero, patch the trouble *)
if( xd = 0.0 )then begin r11 := 1.0 ; r21 := 0; r31 := 0.0 ; xd := 1.0 ; end;
if( yd = 0.0 )then begin r22 := 1.0 ; r12 := 0; r32 := 0.0 ; yd := 1.0 ; end;
if( zd = 0.0 )then begin r33 := 1.0 ; r13 := 0; r23 := 0.0 ; zd := 1.0 ; end;
(* assign the output lengths *)
dx := xd;
dy := yd;
dz := zd;
(* normalize the columns *)
r11 := r11/xd ; r21 := r21/xd ; r31 := r31/xd ;
r12 := r12/yd ; r22 := r22/yd ; r32 := r32/yd ;
r13 := r13/zd ; r23 := r23/zd ; r33 := r33/zd ;
(* At this point, the matrix has normal columns, but we have to allow
for the fact that the hideous user may not have given us a matrix
with orthogonal columns.
So, now find the orthogonal matrix closest to the current matrix.
One reason for using the polar decomposition to get this
orthogonal matrix, rather than just directly orthogonalizing
the columns, is so that inputting the inverse matrix to R
will result in the inverse orthogonal matrix at this point.
If we just orthogonalized the columns, this wouldn't necessarily hold. *)
Q := Matrix2D (r11,r12,r13, // 2D "graphics" matrix
r21,r22,r23,
r31,r32,r33);
P := nifti_mat33_polar(Q) ; (* P is orthog matrix closest to Q *)
FromMatrix(P,r11,r12,r13,r21,r22,r23,r31,r32,r33);
(* [ r11 r12 r13 ] *)
(* at this point, the matrix [ r21 r22 r23 ] is orthogonal *)
(* [ r31 r32 r33 ] *)
(* compute the determinant to determine if it is proper *)
zd := r11*r22*r33-r11*r32*r23-r21*r12*r33
+r21*r32*r13+r31*r12*r23-r31*r22*r13 ; (* should be -1 or 1 *)
if( zd > 0 )then begin (* proper *)
qfac := 1.0 ;
end else begin (* improper ==> flip 3rd column *)
qfac := -1.0 ;
r13 := -r13 ; r23 := -r23 ; r33 := -r33 ;
end;
(* now, compute quaternion parameters *)
a := r11 + r22 + r33 + 1.0;
if( a > 0.5 ) then begin (* simplest case *)
a := 0.5 * sqrt(a) ;
b := 0.25 * (r32-r23) / a ;
c := 0.25 * (r13-r31) / a ;
d := 0.25 * (r21-r12) / a ;
end else begin (* trickier case *)
xd := 1.0 + r11 - (r22+r33) ; (* 4*b*b *)
yd := 1.0 + r22 - (r11+r33) ; (* 4*c*c *)
zd := 1.0 + r33 - (r11+r22) ; (* 4*d*d *)
if( xd > 1.0 ) then begin
b := 0.5 * sqrt(xd) ;
c := 0.25* (r12+r21) / b ;
d := 0.25* (r13+r31) / b ;
a := 0.25* (r32-r23) / b ;
end else if( yd > 1.0 ) then begin
c := 0.5 * sqrt(yd) ;
b := 0.25* (r12+r21) / c ;
d := 0.25* (r23+r32) / c ;
a := 0.25* (r13-r31) / c ;
end else begin
d := 0.5 * sqrt(zd) ;
b := 0.25* (r13+r31) / d ;
c := 0.25* (r23+r32) / d ;
a := 0.25* (r21-r12) / d ;
end;
if( a < 0.0 )then begin b:=-b ; c:=-c ; d:=-d; {a:=-a; not used} end;
end;
qb := b ;
qc := c ;
qd := d ;
end;
procedure FixCrapMat(var lMat: TMatrix);
var
lVec000,lVec100,lVec010,lVec001: TVector;
begin
lVec000 := Vec3D (0, 0, 0);
lVec100 := Vec3D (1, 0, 0);
lVec010 := Vec3D (0, 1, 0);
lVec001 := Vec3D (0, 0, 1);
lVec000 := Transform3D (lVec000, lMat);
lVec100 := Transform3D (lVec100, lMat);
lVec010 := Transform3D (lVec010, lMat);
lVec001 := Transform3D (lVec001, lMat);
if SameVec(lVec000,lVec100) or
SameVec(lVec000,lVec010) or
SameVec(lVec000,lVec001) then begin
lMat := eye3D;
showmessage('Warning: the transformation matrix is corrupt [some dimensions have zero size]');
end;
end;
procedure Mat2Hdr(var lMat: TMatrix; var lHdr: TNIFTIhdr);
begin
lHdr.srow_x[0]:= lMat.matrix[1,1]; lHdr.srow_x[1] := lMat.matrix[1,2]; lHdr.srow_x[2] := lMat.matrix[1,3]; lHdr.srow_x[3] := lMat.matrix[1,4];
lHdr.srow_y[0]:= lMat.matrix[2,1]; lHdr.srow_y[1] := lMat.matrix[2,2]; lHdr.srow_y[2] := lMat.matrix[2,3]; lHdr.srow_y[3] := lMat.matrix[2,4];
lHdr.srow_z[0]:= lMat.matrix[3,1]; lHdr.srow_z[1] := lMat.matrix[3,2]; lHdr.srow_z[2] := lMat.matrix[3,3]; lHdr.srow_z[3] := lMat.matrix[3,4];
end;
procedure MatFlipYZ(var lMat: TMatrix; var lHdr: TNIFTIhdr);
var
lRot: TMatrix;
lOrigin: TVector;
begin
lOrigin := Vec3D (lMat.matrix[1,4], lMat.matrix[2,4], lMat.matrix[3,4]);
lMat.matrix[1,4] := 0; lMat.matrix[2,4] := 0; lMat.matrix[3,4] := 0; //
lRot := Matrix3D(1,0,0,0, 0,0,1,0, 0,1,0,0);
lMat := multiplymatrices(lMat, lRot);
lMat.matrix[1,4] := lOrigin.vector[1]; lMat.matrix[2,4] := lOrigin.vector[3]; lMat.matrix[3,4] := lOrigin.vector[2]; //
//GLForm1.Caption := 'xxx'+inttostr(random(888));
(*clipboard.AsText:= format('x=[%g %g %g %g; %g %g %g %g; %g %g %g %g; 0 0 0 1]',[
lMat.matrix[1,1], lMat.matrix[1,2], lMat.matrix[1,3], lMat.matrix[1,4],
lMat.matrix[2,1], lMat.matrix[2,2], lMat.matrix[2,3], lMat.matrix[2,4],
lMat.matrix[3,1], lMat.matrix[3,2], lMat.matrix[3,3], lMat.matrix[3,4]
]); *)
Mat2Hdr(lMat,lHdr);
end;
function FixDataType (var lHdr: TNIFTIhdr): boolean; overload;
label
191;
var
ldatatypebpp,lbitpix: integer;
begin
result := true;
lbitpix := lHdr.bitpix;
case lHdr.datatype of
kDT_BINARY : ldatatypebpp := 1;
kDT_UNSIGNED_CHAR : ldatatypebpp := 8; // unsigned char (8 bits/voxel)
kDT_SIGNED_SHORT : ldatatypebpp := 16; // signed short (16 bits/voxel)
kDT_SIGNED_INT : ldatatypebpp := 32; // signed int (32 bits/voxel)
kDT_FLOAT : ldatatypebpp := 32; // float (32 bits/voxel)
kDT_COMPLEX : ldatatypebpp := 64; // complex (64 bits/voxel)
kDT_DOUBLE : ldatatypebpp := 64; // double (64 bits/voxel)
kDT_RGB : ldatatypebpp := 24; // RGB triple (24 bits/voxel)
kDT_INT8 : ldatatypebpp := 8; // signed char (8 bits)
kDT_UINT16 : ldatatypebpp := 16; // unsigned short (16 bits)
kDT_UINT32 : ldatatypebpp := 32; // unsigned int (32 bits)
kDT_INT64 : ldatatypebpp := 64; // long long (64 bits)
kDT_UINT64 : ldatatypebpp := 64; // unsigned long long (64 bits)
kDT_FLOAT128 : ldatatypebpp := 128; // long double (128 bits)
kDT_COMPLEX128 : ldatatypebpp := 128; // double pair (128 bits)
kDT_COMPLEX256 : ldatatypebpp := 256; // long double pair (256 bits)
else
ldatatypebpp := 0;
end;
if (ldatatypebpp = lHdr.bitpix) and (ldatatypebpp <> 0) then
exit;
//showmessage(inttostr(ldatatypebpp));
if (ldatatypebpp <> 0) then begin
//use bitpix from datatype...
lHdr.bitpix := ldatatypebpp;
exit;
end;
if (lbitpix <> 0) and (ldatatypebpp = 0) then begin
//assume bitpix is correct....
//note that several datatypes correspond to each bitpix, so assume most popular...
case lbitpix of
1: lHdr.datatype := kDT_BINARY;
8: lHdr.datatype := kDT_UNSIGNED_CHAR;
16: lHdr.datatype := kDT_SIGNED_SHORT;
24: lHdr.datatype := kDT_RGB;
32: lHdr.datatype := kDT_FLOAT;
64: lHdr.datatype := kDT_DOUBLE;
else goto 191; //impossible bitpix
end;
exit;
end;
191:
//Both bitpix and datatype are wrong... assume most popular format
result := false;
lHdr.bitpix := 16;
lHdr.datatype := kDT_SIGNED_SHORT;
end;
function FixDataType (var lHdr: TMRIcroHdr): boolean; overload;
begin
result := FixDataType(lHdr.NIFTIhdr);
end;
procedure SwapNifti2(var h: TNIFTI2hdr);
var
i: integer;
begin
h.HdrSz := swap(h.HdrSz);
h.datatype := swap(h.datatype);
h.bitpix := swap(h.bitpix);
for i := 0 to 7 do
h.dim[i] := swap(h.dim[i]);
h.intent_p1 := swap64r(h.intent_p1);
h.intent_p2 := swap64r(h.intent_p2);
h.intent_p3 := swap64r(h.intent_p3);
for i := 0 to 7 do
h.pixdim[i] := swap64r(h.pixdim[i]);
h.vox_offset := swap(h.vox_offset);
h.scl_slope := swap64r(h.scl_slope);
h.scl_inter := swap64r(h.scl_inter);
h.cal_max := swap64r(h.cal_max);
h.cal_min := swap64r(h.cal_min);
h.slice_duration := swap64r(h.slice_duration);
h.toffset := swap64r(h.toffset);
h.slice_start := swap(h.slice_start);
h.slice_end := swap(h.slice_end);
h.qform_code := swap(h.qform_code);
h.sform_code := swap(h.sform_code);
h.quatern_b := swap64r(h.quatern_b);
h.quatern_c := swap64r(h.quatern_c);
h.quatern_d := swap64r(h.quatern_d);
h.qoffset_x := swap64r(h.qoffset_x);
h.qoffset_y := swap64r(h.qoffset_y);
h.qoffset_z := swap64r(h.qoffset_z);
for i := 0 to 3 do begin
h.srow_x[i] := swap64r(h.srow_x[i]);
h.srow_y[i] := swap64r(h.srow_y[i]);
h.srow_z[i] := swap64r(h.srow_z[i]);
end;
h.slice_code := swap(h.slice_code);
h.xyzt_units := swap(h.xyzt_units);
h.intent_code := swap(h.intent_code);
end;
function LoadNifti2(var lFilename: string; isGz: boolean): TNIFTIhdr;
var
lHdr1: TNIFTIhdr;
lHdr2: TNIFTI2hdr;
i: integer;
lHdrFile: file;
lBuff: Bytep;
begin
FileMode := 0; { Set file access to read only }
if (isGz) then begin
lBuff := @lHdr2;
UnGZip(lFileName,lBuff,0,sizeof(TNIFTI2hdr));
end else begin
{$I-}
AssignFile(lHdrFile, lFileName);
FileMode := 0; { Set file access to read only }
Reset(lHdrFile, 1);
{$I+}
if ioresult <> 0 then begin
ShowMessage('Error in reading NIFTI header.'+inttostr(IOResult));
FileMode := 2;
exit;
end;
BlockRead(lHdrFile, lHdr2, sizeof(TNIFTI2hdr));
CloseFile(lHdrFile);
end;
FileMode := 2;
//swap
if lHdr2.HdrSz <> sizeof(TNIFTI2hdr) then
SwapNifti2(lHdr2);
//FILL NIFTI-1 header with NIFTI-2
lHdr1.HdrSz := sizeof(TNIFTIhdr);
lHdr1.magic := kNIFTI_MAGIC_EMBEDDED_HDR;
lHdr1.datatype := lHdr2.datatype;
lHdr1.bitpix := lHdr2.bitpix;
for i := 0 to 7 do begin
if (lHdr2.dim[i] > 32767) then
lHdr2.dim[i] := 32767;
lHdr1.dim[i] := lHdr2.dim[i];
end;
lHdr1.intent_p1 := lHdr2.intent_p1;
lHdr1.intent_p2 := lHdr2.intent_p2;
lHdr1.intent_p3 := lHdr2.intent_p3;
for i := 0 to 7 do
lHdr1.pixdim[i] := lHdr2.pixdim[i];
lHdr1.vox_offset := lHdr2.vox_offset;
lHdr1.scl_slope := lHdr2.scl_slope;
lHdr1.scl_inter := lHdr2.scl_inter;
lHdr1.cal_max := lHdr2.cal_max;
lHdr1.cal_min := lHdr2.cal_min;
lHdr1.slice_duration := lHdr2.slice_duration;
lHdr1.toffset := lHdr2.toffset;
lHdr1.slice_start := lHdr2.slice_start;
lHdr1.slice_end := lHdr2.slice_end;
for i := 1 to 80 do
lHdr1.descrip[i] := lHdr2.descrip[i];
for i := 1 to 24 do
lHdr1.aux_file[i] := lHdr2.aux_file[i];
lHdr1.qform_code := lHdr2.qform_code;
lHdr1.sform_code := lHdr2.sform_code;
lHdr1.quatern_b := lHdr2.quatern_b;
lHdr1.quatern_c := lHdr2.quatern_c;
lHdr1.quatern_d := lHdr2.quatern_d;
lHdr1.qoffset_x := lHdr2.qoffset_x;
lHdr1.qoffset_y := lHdr2.qoffset_y;
lHdr1.qoffset_z := lHdr2.qoffset_z;
for i := 0 to 3 do begin
lHdr1.srow_x[i] := lHdr2.srow_x[i];
lHdr1.srow_y[i] := lHdr2.srow_y[i];
lHdr1.srow_z[i] := lHdr2.srow_z[i];
end;
lHdr1.slice_code := lHdr2.slice_code;
lHdr1.xyzt_units := lHdr2.xyzt_units;
lHdr1.intent_code := lHdr2.intent_code;
for i := 1 to 16 do
lHdr1.intent_name[i] := lHdr2.intent_name[i];
lHdr1.dim_info := lHdr2.dim_info;
//removed fields https://brainder.org/2015/04/03/the-nifti-2-file-format/
for i := 1 to 10 do
lHdr1.data_type[i] := chr(0);
for i := 1 to 16 do
lHdr1.db_name[i] := chr(0);
lHdr1.extents := 0;
lHdr1.session_error := 0;
lHdr1.regular := 'r';
lHdr1.glmax := 0;
lHdr1.glmin := 0;
result := lHdr1;
end;
function NIFTIhdr_LoadHdr (var lFilename: string; out lHdr: TMRIcroHdr; lFlipYZ: boolean): boolean;
var
lHdrFile: file;
lOri: array [1..3] of single;
lBuff: Bytep;
lAHdr: TAnalyzeHdrSection;
lHdrSz,lReportedSz, lSwappedReportedSz: integer;
lFileSz: Int64;
lExt: string; //1494
swapEndian, isDimPermute2341: boolean;
begin
Result := false; //assume error
swapEndian := false;
isDimPermute2341 := false;
NIFTIhdr_ClearHdr(lHdr);
if lFilename = '' then exit;
lExt := UpCaseExt(lFilename);
lHdr.ImgFileName:= lFilename;
if (lExt = '.HDR') then
lHdr.ImgFileName:= changefileext(lFilename,'.img');
if lExt = '.IMG' then begin
lHdr.ImgFileName := lFilename;
lFilename := changeFileExt(lFilename,'.hdr');
end;
if (lExt = '.BRIK') or (lExt='.BRIK.GZ') then
lFilename := changeFileExtX(lFilename,'.head');
if not FileExistsEX(lFilename) then exit;
lHdr.HdrFileName:= lFilename;
if (lExt <> '.IMG') and (lExt <> '.NII') and (lExt <> '.VOI') and (lExt <> '.HDR') and (lExt <> '.NII.GZ') then begin
result := readForeignHeader (lFilename, lHdr.NIFTIhdr, lHdr.gzBytes, swapEndian, isDimPermute2341);
lHdr.ImgFileName := lfilename;
lfilename := lHdr.HdrFileName; //expects filename to be header not image!
lHdr.DiskDataNativeEndian := not swapEndian;
exit;
end else if (lExt = '.NII.GZ') or (lExt = '.VOI') then
lHdr.gzBytes := K_gzBytes_headerAndImageCompressed;
lHdrSz := sizeof(TniftiHdr);
lFileSz := FSize (lFilename);
if lFileSz = 0 then begin
ShowMessage('Unable to find NIFTI header named '+lFilename);
exit;
end;
if (lFileSz < lHdrSz) and (lHdr.gzBytes = K_gzBytes_headerAndImageUncompressed) then begin
ShowMessage('Error in reading NIFTI header: NIfTI headers need to be at least '+inttostr(lHdrSz)+ ' bytes: '+lFilename);
exit;
end;
FileMode := 0; { Set file access to read only }
if (lHdr.gzBytes <> K_gzBytes_headerAndImageUncompressed) then begin//1388
lBuff := @lHdr;
UnGZip(lFileName,lBuff,0,lHdrSz);
end else begin //if gzip
{$I-}
AssignFile(lHdrFile, lFileName);
FileMode := 0; { Set file access to read only }
Reset(lHdrFile, 1);
{$I+}
if ioresult <> 0 then begin
ShowMessage('Error in reading NIFTI header.'+inttostr(IOResult));
FileMode := 2;
exit;
end;
BlockRead(lHdrFile, lHdr, lHdrSz);
CloseFile(lHdrFile);
end;
FileMode := 2;
if (IOResult <> 0) then exit;
lReportedSz := lHdr.niftiHdr.HdrSz;
lSwappedReportedSz := lReportedSz;
swap4(lSwappedReportedSz);
if lReportedSz = lHdrSz then begin
lHdr.DiskDataNativeEndian := true;
end else if lSwappedReportedSz = lHdrSz then begin
lHdr.DiskDataNativeEndian := false;
NIFTIhdr_SwapBytes (lHdr.niftiHdr);
end else if (lReportedSz = 540) or (lSwappedReportedSz = 540) then begin
lHdr.niftiHdr := LoadNifti2(lFilename, (lHdr.gzBytes <> K_gzBytes_headerAndImageUncompressed));
lHdr.DiskDataNativeEndian := (lReportedSz = 540);
end else begin
//result := NIFTIhdr_LoadDCM (lFilename,lHdr); //2/2008
//if not result then
ShowMessage('Warning: the header file is not in NIfTi format [the first 4 bytes do not have the value 348]. Assuming big-endian data.');
exit;
end;
if (lHdr.NIFTIhdr.dim[0] > 7) or (lHdr.NIFTIhdr.dim[0] < 1) then begin //only 1..7 dims, so this
Showmessage('Illegal NIfTI Format Header: this header does not specify 1..7 dimensions, but '+inttostr(lHdr.NIFTIhdr.dim[0]));
exit;
end;
FixDataType(lHdr);
result := true;
if IsNifTiMagic(lHdr.niftiHdr) then begin //must match MAGMA in nifti_img
lOri[1] := (lHdr.NIFTIhdr.dim[1]+1) div 2;
lOri[2] := (lHdr.NIFTIhdr.dim[2]+1) div 2;
lOri[3] := (lHdr.NIFTIhdr.dim[3]+1) div 2;
if (not HasQuat(lHdr.NiftiHdr)) {3/2008} and (lHdr.NIFTIhdr.sform_code = 0) and (orthogonalMatrix(lHdr)) then