forked from neurolabusc/surf-ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifti_foreign.pas
executable file
·1551 lines (1496 loc) · 58.8 KB
/
nifti_foreign.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_foreign;
interface
{$H+}
uses
dialogs, nifti_types, define_types, sysutils, classes, StrUtils, zstream;
function readForeignHeader (var lFilename: string; var lHdr: TNIFTIhdr; var gzBytes: int64; var swapEndian: boolean): boolean;
procedure NII_Clear (var lHdr: TNIFTIHdr);
procedure NII_SetIdentityMatrix (var lHdr: TNIFTIHdr); //create neutral rotation matrix
implementation
Type
mat44 = array [0..3, 0..3] of Single;
vect4 = array [0..3] of Single;
mat33 = array [0..2, 0..2] of Single;
vect3 = array [0..2] of Single;
ivect3 = array [0..2] of integer;
ByteRA = array [1..1] of byte;
Bytep = ^ByteRA;
procedure ShowMsg(s: string);
begin
Showmessage(s);
end;
procedure fromMatrix (m: mat44; var r11,r12,r13,r21,r22,r23,r31,r32,r33: double);
begin
r11 := m[0,0];
r12 := m[0,1];
r13 := m[0,2];
r21 := m[1,0];
r22 := m[1,1];
r23 := m[1,2];
r31 := m[2,0];
r32 := m[2,1];
r33 := m[2,2];
end;
function Matrix2D (r11,r12,r13,r21,r22,r23,r31,r32,r33: double): mat33;
begin
result[0,0] := r11;
result[0,1] := r12;
result[0,2] := r13;
result[1,0] := r21;
result[1,1] := r22;
result[1,2] := r23;
result[2,0] := r31;
result[2,1] := r32;
result[2,2] := r33;
end;
function nifti_mat33_determ( R: mat33 ):double; //* determinant of 3x3 matrix */
begin
result := r[0,0]*r[1,1]*r[2,2]
-r[0,0]*r[2,1]*r[1,2]
-r[1,0]*r[0,1]*r[2,2]
+r[1,0]*r[2,1]*r[0,2]
+r[2,0]*r[0,1]*r[1,2]
-r[2,0]*r[1,1]*r[0,2] ;
end;
function nifti_mat33_rownorm( A: mat33 ): single; // max row norm of 3x3 matrix
var
r1,r2,r3: single ;
begin
r1 := abs(A[0,0])+abs(A[0,1])+abs(A[0,2]);
r2 := abs(A[1,0])+abs(A[1,1])+abs(A[1,2]);
r3 := abs(A[2,0])+abs(A[2,1])+abs(A[2,2]);
if( r1 < r2 ) then r1 := r2 ;
if( r1 < r3 ) then r1 := r3 ;
result := r1 ;
end;
procedure fromMatrix33 (m: mat33; var r11,r12,r13,r21,r22,r23,r31,r32,r33: double);
begin
r11 := m[0,0];
r12 := m[0,1];
r13 := m[0,2];
r21 := m[1,0];
r22 := m[1,1];
r23 := m[1,2];
r31 := m[2,0];
r32 := m[2,1];
r33 := m[2,2];
end;
function nifti_mat33_inverse( R: mat33 ): mat33; //* inverse of 3x3 matrix */
var
r11,r12,r13,r21,r22,r23,r31,r32,r33 , deti: double ;
begin
FromMatrix33(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 ;
result[0,0] := deti*( r22*r33-r32*r23) ;
result[0,1] := deti*(-r12*r33+r32*r13) ;
result[0,2] := deti*( r12*r23-r22*r13) ;
result[1,0] := deti*(-r21*r33+r31*r23) ;
result[1,1] := deti*( r11*r33-r31*r13) ;
result[1,2] := deti*(-r11*r23+r21*r13) ;
result[2,0] := deti*( r21*r32-r31*r22) ;
result[2,1] := deti*(-r11*r32+r31*r12) ;
result[2,2] := deti*( r11*r22-r21*r12) ;
end;
function nifti_mat33_colnorm( A: mat33 ): single; //* max column norm of 3x3 matrix */
var
r1,r2,r3: single ;
begin
r1 := abs(A[0,0])+abs(A[1,0])+abs(A[2,0]) ;
r2 := abs(A[0,1])+abs(A[1,1])+abs(A[2,1]) ;
r3 := abs(A[0,2])+abs(A[1,2])+abs(A[2,2]) ;
if( r1 < r2 ) then r1 := r2 ;
if( r1 < r3 ) then r1 := r3 ;
result := r1 ;
end;
function nifti_mat33_polar( A: mat33 ): mat33;
var
k:integer;
X , Y , Z: mat33 ;
dif,alp,bet,gam,gmi : single;
begin
dif := 1;
k := 0;
X := A ;
gam := nifti_mat33_determ(X) ;
while( gam = 0.0 )do begin //perturb matrix
gam := 0.00001 * ( 0.001 + nifti_mat33_rownorm(X) ) ;
X[0,0] := X[0,0]+gam ;
X[1,1] := X[1,1]+gam ;
X[2,2] := X[2,2] +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[0,0] := 0.5 * ( gam*X[0,0] + gmi*Y[0,0] ) ;
Z[0,1] := 0.5 * ( gam*X[0,1] + gmi*Y[1,0] ) ;
Z[0,2] := 0.5 * ( gam*X[0,2] + gmi*Y[2,0] ) ;
Z[1,0] := 0.5 * ( gam*X[1,0] + gmi*Y[0,1] ) ;
Z[1,1] := 0.5 * ( gam*X[1,1] + gmi*Y[1,1] ) ;
Z[1,2] := 0.5 * ( gam*X[1,2] + gmi*Y[2,1] ) ;
Z[2,0] := 0.5 * ( gam*X[2,0] + gmi*Y[0,2] ) ;
Z[2,1] := 0.5 * ( gam*X[2,1] + gmi*Y[1,2] ) ;
Z[2,2] := 0.5 * ( gam*X[2,2] + gmi*Y[2,2] ) ;
dif := abs(Z[0,0]-X[0,0])+abs(Z[0,1]-X[0,1])+abs(Z[0,2]-X[0,2])
+abs(Z[1,0]-X[1,0])+abs(Z[1,1]-X[1,1])+abs(Z[1,2]-X[1,2])
+abs(Z[2,0]-X[2,0])+abs(Z[2,1]-X[2,1])+abs(Z[2,2]-X[2,2]);
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 :mat44;
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: mat33; //3x3
begin
// offset outputs are read write out of input matrix
qx := lR[0,3];
qy := lR[1,3];
qz := lR[2,3];
//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
FromMatrix33(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; this is not used} end;
end;
qb := b ;
qc := c ;
qd := d ;
end;
procedure NII_SetIdentityMatrix (var lHdr: TNIFTIHdr); //create neutral rotation matrix
var lInc: integer;
begin
with lHdr do begin
for lInc := 0 to 3 do
srow_x[lInc] := 0;
for lInc := 0 to 3 do
srow_y[lInc] := 0;
for lInc := 0 to 3 do
srow_z[lInc] := 0;
for lInc := 1 to 16 do
intent_name[lInc] := chr(0);
//next: create identity matrix: if code is switched on there will not be a problem
srow_x[0] := 1;
srow_y[1] := 1;
srow_z[2] := 1;
end;
end; //proc NIFTIhdr_IdentityMatrix
procedure NII_Clear (var lHdr: TNIFTIHdr);
var
lInc: integer;
begin
with lHdr do begin
HdrSz := sizeof(TNIFTIhdr);
for lInc := 1 to 10 do
Data_Type[lInc] := chr(0);
for lInc := 1 to 18 do
db_name[lInc] := chr(0);
extents:=0;
session_error:= 0;
regular:='r'{chr(0)};
dim_info:=(0);
dim[0] := 4;
for lInc := 1 to 7 do
dim[lInc] := 0;
intent_p1 := 0;
intent_p2 := 0;
intent_p3 := 0;
intent_code:=0;
datatype:=0 ;
bitpix:=0;
slice_start:=0;
for lInc := 1 to 7 do
pixdim[linc]:= 1.0;
vox_offset:= 0.0;
scl_slope := 1.0;
scl_inter:= 0.0;
slice_end:= 0;
slice_code := 0;
xyzt_units := 10;
cal_max:= 0.0;
cal_min:= 0.0;
slice_duration:=0;
toffset:= 0;
glmax:= 0;
glmin:= 0;
for lInc := 1 to 80 do
descrip[lInc] := chr(0);{80 spaces}
for lInc := 1 to 24 do
aux_file[lInc] := chr(0);{80 spaces}
{below are standard settings which are not 0}
bitpix := 16;//vc16; {8bits per pixel, e.g. unsigned char 136}
DataType := 4;//vc4;{2=unsigned char, 4=16bit int 136}
Dim[0] := 3;
Dim[1] := 256;
Dim[2] := 256;
Dim[3] := 128;
Dim[4] := 1; {n vols}
Dim[5] := 1;
Dim[6] := 1;
Dim[7] := 1;
glMin := 0;
glMax := 255;
qform_code := kNIFTI_XFORM_UNKNOWN;
sform_code:= kNIFTI_XFORM_UNKNOWN;
quatern_b := 0;
quatern_c := 0;
quatern_d := 0;
qoffset_x := 0;
qoffset_y := 0;
qoffset_z := 0;
NII_SetIdentityMatrix(lHdr);
magic := kNIFTI_MAGIC_SEPARATE_HDR;
end; //with the NIfTI header...
end;
procedure ZERO_MAT44(var m: mat44); //note sets m[3,3] to one
var
i,j: integer;
begin
for i := 0 to 3 do
for j := 0 to 3 do
m[i,j] := 0.0;
m[3,3] := 1;
end;
procedure LOAD_MAT33(var m: mat33; m00,m01,m02, m10,m11,m12, m20,m21,m22: single);
begin
m[0,0] := m00;
m[0,1] := m01;
m[0,2] := m02;
m[1,0] := m10;
m[1,1] := m11;
m[1,2] := m12;
m[2,0] := m20;
m[2,1] := m21;
m[2,2] := m22;
end;
function nifti_mat33_mul( A,B: mat33): mat33;
var
i,j: integer;
begin
for i:=0 to 3 do
for j:=0 to 3 do
result[i,j] := A[i,0] * B[0,j]
+ A[i,1] * B[1,j]
+ A[i,2] * B[2,j] ;
end;
procedure LOAD_MAT44(var m: mat44; m00,m01,m02,m03, m10,m11,m12,m13, m20,m21,m22,m23: single);
begin
m[0,0] := m00;
m[0,1] := m01;
m[0,2] := m02;
m[0,3] := m03;
m[1,0] := m10;
m[1,1] := m11;
m[1,2] := m12;
m[1,3] := m13;
m[2,0] := m20;
m[2,1] := m21;
m[2,2] := m22;
m[2,3] := m23;
m[3,0] := 0.0;
m[3,1] := 0.0;
m[3,2] := 0.0;
m[3,3] := 1.0;
end;
function validMatrix(var m: mat44): boolean;
var
i: integer;
begin
result := false;
for i := 0 to 2 do begin
if (m[0,i] = 0.0) and (m[1,i] = 0.0) and (m[2,i] = 0.0) then exit;
if (m[i,0] = 0.0) and (m[i,1] = 0.0) and (m[i,2] = 0.0) then exit;
end;
result := true;
end;
procedure convertForeignToNifti(var nhdr: TNIFTIhdr);
var
i,nonSpatialMult: integer;
qto_xyz: mat44;
//dumqx, dumqy, dumqz,
dumdx, dumdy, dumdz: single;
begin
nhdr.HdrSz := 348; //used to signify header does not need to be byte-swapped
nhdr.magic:=kNIFTI_MAGIC_EMBEDDED_HDR;
if (nhdr.dim[3] = 0) then nhdr.dim[3] := 1; //for 2D images the 3rd dim is not specified and set to zero
nhdr.dim[0] := 3; //for 2D images the 3rd dim is not specified and set to zero
nonSpatialMult := 1;
for i := 4 to 7 do
if nhdr.dim[i] > 0 then
nonSpatialMult := nonSpatialMult * nhdr.dim[i];
if (nonSpatialMult > 1) then begin
nhdr.dim[0] := 4;
nhdr.dim[4] := nonSpatialMult;
for i := 5 to 7 do
nhdr.dim[i] := 0;
end;
nhdr.bitpix := 8;
if (nhdr.datatype = 4) or (nhdr.datatype = 512) then nhdr.bitpix := 16;
if (nhdr.datatype = 8) or (nhdr.datatype = 16) or (nhdr.datatype = 768) then nhdr.bitpix := 32;
if (nhdr.datatype = 32) or (nhdr.datatype = 64) or (nhdr.datatype = 1024) or (nhdr.datatype = 1280) then nhdr.bitpix := 64;
LOAD_MAT44(qto_xyz, nhdr.srow_x[0], nhdr.srow_x[1], nhdr.srow_x[2], nhdr.srow_x[3],
nhdr.srow_y[0], nhdr.srow_y[1], nhdr.srow_y[2], nhdr.srow_y[3],
nhdr.srow_z[0], nhdr.srow_z[1], nhdr.srow_z[2], nhdr.srow_z[3]);
if not validMatrix(qto_xyz) then begin
nhdr.sform_code := 0;
nhdr.qform_code := 0;
for i := 0 to 3 do begin
nhdr.srow_x[i] := 0;
nhdr.srow_y[i] := 0;
nhdr.srow_z[i] := 0;
end;
nhdr.srow_x[0] := 1;
nhdr.srow_y[1] := 1;
nhdr.srow_z[2] := 1;
exit;
end;
nhdr.sform_code := 1;
nifti_mat44_to_quatern( qto_xyz , nhdr.quatern_b, nhdr.quatern_c, nhdr.quatern_d,nhdr.qoffset_x,nhdr.qoffset_y,nhdr.qoffset_z, dumdx, dumdy, dumdz,nhdr.pixdim[0]) ;
nhdr.qform_code := kNIFTI_XFORM_SCANNER_ANAT;
end;
procedure NSLog( str: string);
begin
showmsg(str);
end;
function parsePicString(s: string): single;
//given "AXIS_4 001 0.000000e+00 4.000000e-01 microns" return 0.4
var
sList : TStringList;
begin
result := 0.0;
DecimalSeparator := '.';
sList := TStringList.Create;
sList.Delimiter := ' '; // Each list item will be blank separated
sList.DelimitedText := s;
if sList.Count > 4 then begin
//ShowMessage(sList[3]);
try
result := StrToFloat(sList[3]); // Middle blanks are not supported
except
//ShowMessage(Exception.Message);
end;
end;
sList.Free;
end;
function nii_readpic (var fname: string; var nhdr: TNIFTIhdr; var gzBytes: int64; var swapEndian: boolean): boolean;
const
kBIORAD_HEADER_SIZE = 76;
kBIORAD_NOTE_HEADER_SIZE = 16;
kBIORAD_NOTE_SIZE = 80;
Type
Tbiorad_header = packed record //Next: PIC Format Header structure
nx, ny : word; // 0 2*2 image width and height in pixels
npic: SmallInt; // 4 2 number of images in file
ramp1_min: SmallInt; // 6 2*2 LUT1 ramp min. and max.
ramp1_max: SmallInt;
notes: LongInt; // 10 4 no notes=0; has notes=non zero
byte_format: SmallInt; // 14 2 bytes=TRUE(1); words=FALSE(0)
n : word; // 16 2 image number within file
name: array [1..32] of char; // 18 32 file name
merged: SmallInt; // 50 2 merged format
color1 : word; // 52 2 LUT1 color status
file_id : word; // 54 2 valid .PIC file=12345
ramp2_min: SmallInt; // 56 2*2 LUT2 ramp min. and max.
ramp2_max: SmallInt;
color2: word; // 60 2 LUT2 color status
edited: SmallInt; // 62 2 image has been edited=TRUE(1)
lens: SmallInt; // 64 2 Integer part of lens magnification
mag_factor: single; // 66 4 4 byte real mag. factor (old ver.)
dummy1, dummy2, dummy3: word; // 70 6 NOT USED (old ver.=real lens mag.)
end; // biorad_header;
Tbiorad_note_header = packed record
blank: SmallInt; // 0 2
note_flag: LongInt; // 2 4
blank2: LongInt; // 6 4
note_type: SmallInt; // 10 2
blank3: LongInt; // 12 4
note: array[1..kBIORAD_NOTE_SIZE] of char;
end;//biorad_note_header;
var
bhdr : Tbiorad_header;
nh: Tbiorad_note_header;
lHdrFile: file;
//s: string;
i, bytesHdrImg, nNotes: integer;
begin
result := false;
FileMode := fmOpenRead;
{$I-}
AssignFile(lHdrFile, fname);
FileMode := 0; //Set file access to read only
Reset(lHdrFile, 1);
{$I+}
if ioresult <> 0 then begin
NSLog('Error in reading BioRad PIC header.'+inttostr(IOResult));
FileMode := 2;
exit;
end;
BlockRead(lHdrFile, bhdr, sizeof(Tbiorad_header));
if (bhdr.file_id <> 12345) then begin //signature not found!
CloseFile(lHdrFile);
NSLog('Error in reading BioRad PIC header file ID not 12345.');
exit;
end;
nhdr.dim[0]:=3;//3D
nhdr.dim[1]:=bhdr.nx;
nhdr.dim[2]:=bhdr.ny;
nhdr.dim[3]:=bhdr.npic;
nhdr.dim[4]:=1;
nhdr.pixdim[1]:=1.0;
nhdr.pixdim[2]:=1.0;
nhdr.pixdim[3]:=1.0;
if (bhdr.byte_format = 1) then
nhdr.datatype := kDT_UINT8 // 2
else
nhdr.datatype := kDT_UINT16;
nhdr.vox_offset := kBIORAD_HEADER_SIZE;
bytesHdrImg := sizeof(Tbiorad_header)+bhdr.nx*bhdr.ny*bhdr.npic*bhdr.byte_format;
nNotes := (Filesize(lHdrFile) - bytesHdrImg) div (kBIORAD_NOTE_HEADER_SIZE+kBIORAD_NOTE_SIZE);
if (nNotes > 0) then begin
seek(lHdrFile, bytesHdrImg);
for i := 1 to nNotes do begin
BlockRead(lHdrFile, nh, sizeof(Tbiorad_note_header));
if(nh.note_type=1) then continue; // These are not interesting notes
if AnsiStartsStr('AXIS_2 ', nh.note) then
nhdr.pixdim[1] := parsePicString(nh.note);
if AnsiStartsStr('AXIS_3 ', nh.note) then
nhdr.pixdim[2] := parsePicString(nh.note);
if AnsiStartsStr('AXIS_4 ', nh.note) then
nhdr.pixdim[3] := parsePicString(nh.note);
end;
end;
CloseFile(lHdrFile);
nhdr.sform_code := 1;
nhdr.srow_x[0]:=nhdr.pixdim[1];nhdr.srow_x[1]:=0.0;nhdr.srow_x[2]:=0.0;nhdr.srow_x[3]:=0.0;
nhdr.srow_y[0]:=0.0;nhdr.srow_y[1]:=nhdr.pixdim[2];nhdr.srow_y[2]:=0.0;nhdr.srow_y[3]:=0.0;
nhdr.srow_z[0]:=0.0;nhdr.srow_z[1]:=0.0;nhdr.srow_z[2]:=-nhdr.pixdim[3];nhdr.srow_z[3]:=0.0;
convertForeignToNifti(nhdr);
result := true;
end;
//UnGZip(fname,lBuff,0,sizeof(Tmgh));
procedure UnGZip(const FileName: string; buffer: bytep; offset, sz: integer);
var
decomp: TGZFileStream;
skip: array of byte;
begin
decomp := TGZFileStream.create(FileName, gzopenread);
if offset > 0 then begin
setlength(skip, offset);
decomp.Read(skip[0], offset);
end;
decomp.Read(buffer[0], sz);
decomp.free;
end;
function readMGHHeader (var fname: string; var nhdr: TNIFTIhdr; var gzBytes: int64; var swapEndian: boolean): boolean;
Type
Tmgh = packed record //Next: MGH Format Header structure
version, width,height,depth,nframes,mtype,dof : longint;
goodRASFlag: smallint;
spacingX,spacingY,spacingZ,xr,xa,xs,yr,ya,ys,zr,za,zs,cr,ca,cs: single;
end;
var
mgh: Tmgh;
lBuff: Bytep;
lExt: string;
lHdrFile: file;
PxyzOffset, Pcrs: vect4;
i,j: integer;
base: single;
m: mat44;
begin
result := false;
FileMode := fmOpenRead;
lExt := ExtractFileExtGzUpper(fname);
if (lExt = '.MGZ') then begin
lBuff := @mgh;
UnGZip(fname,lBuff,0,sizeof(Tmgh)); //1388
gzBytes := K_gzBytes_headerAndImageCompressed;
end else begin //if MGZ, else assume uncompressed MGH
gzBytes := 0;
{$I-}
AssignFile(lHdrFile, fname);
FileMode := 0; //Set file access to read only
Reset(lHdrFile, 1);
{$I+}
if ioresult <> 0 then begin
NSLog('Error in reading MGH header.'+inttostr(IOResult));
FileMode := 2;
exit;
end;
BlockRead(lHdrFile, mgh, sizeof(Tmgh));
CloseFile(lHdrFile);
end;
{$IFDEF ENDIAN_BIG} //data always stored big endian
swapEndian := false;
{$ELSE}
swapEndian := true;
SwapLongInt(mgh.version);
SwapLongInt(mgh.width);
SwapLongInt(mgh.height);
SwapLongInt(mgh.depth);
SwapLongInt(mgh.nframes);
SwapLongInt(mgh.mtype);
SwapLongInt(mgh.dof);
mgh.goodRASFlag := swap(mgh.goodRASFlag);
SwapSingle(mgh.spacingX);
SwapSingle(mgh.spacingY);
SwapSingle(mgh.spacingZ);
SwapSingle(mgh.xr);
SwapSingle(mgh.xa);
SwapSingle(mgh.xs);
SwapSingle(mgh.yr);
SwapSingle(mgh.ya);
SwapSingle(mgh.ys);
SwapSingle(mgh.zr);
SwapSingle(mgh.za);
SwapSingle(mgh.zs);
SwapSingle(mgh.cr);
SwapSingle(mgh.ca);
SwapSingle(mgh.cs);
{$ENDIF}
if ((mgh.version <> 1) or (mgh.mtype < 0) or (mgh.mtype > 4)) then begin
NSLog('Error: first value in a MGH header should be 1 and data type should be in the range 1..4.');
exit;
end;
if (mgh.mtype = 0) then
nhdr.datatype := kDT_UINT8
else if (mgh.mtype = 4) then
nhdr.datatype := kDT_INT16
else if (mgh.mtype = 1) then
nhdr.datatype := kDT_INT32
else if (mgh.mtype = 3) then
nhdr.datatype := kDT_FLOAT32;
nhdr.dim[1]:=mgh.width;
nhdr.dim[2]:=mgh.height;
nhdr.dim[3]:=mgh.depth;
nhdr.dim[4]:=mgh.nframes;
nhdr.pixdim[1]:=mgh.spacingX;
nhdr.pixdim[2]:=mgh.spacingY;
nhdr.pixdim[3]:=mgh.spacingZ;
nhdr.vox_offset := 284;
nhdr.sform_code := 1;
//convert MGH to NIfTI transform see Bruce Fischl mri.c MRIxfmCRS2XYZ https://github.com/neurodebian/freesurfer/blob/master/utils/mri.c
LOAD_MAT44(m,mgh.xr*nhdr.pixdim[1],mgh.yr*nhdr.pixdim[2],mgh.zr*nhdr.pixdim[3],0,
mgh.xa*nhdr.pixdim[1],mgh.ya*nhdr.pixdim[2],mgh.za*nhdr.pixdim[3],0,
mgh.xs*nhdr.pixdim[1],mgh.ys*nhdr.pixdim[2],mgh.zs*nhdr.pixdim[3],0);
base := 0.0; //0 or 1: are voxels indexed from 0 or 1?
Pcrs[0] := (nhdr.dim[1]/2.0)+base;
Pcrs[1] := (nhdr.dim[2]/2.0)+base;
Pcrs[2] := (nhdr.dim[3]/2.0)+base;
Pcrs[3] := 1;
for i:=0 to 3 do begin //multiply Pcrs * m
PxyzOffset[i] := 0;
for j := 0 to 3 do
PxyzOffset[i] := PxyzOffset[i]+ (m[i,j]*Pcrs[j]);
end;
nhdr.srow_x[0]:=m[0,0]; nhdr.srow_x[1]:=m[0,1]; nhdr.srow_x[2]:=m[0,2]; nhdr.srow_x[3]:=mgh.cr - PxyzOffset[0];
nhdr.srow_y[0]:=m[1,0]; nhdr.srow_y[1]:=m[1,1]; nhdr.srow_y[2]:=m[1,2]; nhdr.srow_y[3]:=mgh.ca - PxyzOffset[1];
nhdr.srow_z[0]:=m[2,0]; nhdr.srow_z[1]:=m[2,1]; nhdr.srow_z[2]:=m[2,2]; nhdr.srow_z[3]:=mgh.cs - PxyzOffset[2];
convertForeignToNifti(nhdr);
result := true;
end;
procedure splitStr(delimiter: char; str: string; mArray: TStrings);
begin
mArray.Clear;
mArray.Delimiter := delimiter;
mArray.DelimitedText := str;
end;
procedure splitStrStrict(delimiter: char; S: string; sl: TStrings);
begin
sl.Clear;
sl.Delimiter := delimiter;
sl.DelimitedText := '"' + StringReplace(S, sl.Delimiter, '"' + sl.Delimiter + '"', [rfReplaceAll]) + '"';
end;
function cleanStr (S:string): string; // "(12.31)" ->"12.31"
begin
result := StringReplace(S, '(', '', [rfReplaceAll]);
result := StringReplace(result, ')', '', [rfReplaceAll]);
end;
(*function FSize (lFName: String): Int64;
var SearchRec: TSearchRec;
begin
result := 0;
if not fileexistsex(lFName) then exit;
FindFirst(lFName, faAnyFile, SearchRec);
result := SearchRec.size;
FindClose(SearchRec);
end; *)
(*procedure report_mat(m: mat33);
begin
showmsg('mat = ['+floattostr(m[0,0])+' ' +floattostr(m[0,1]) +' ' +floattostr(m[0,2]) +'; '
+floattostr(m[1,0])+' ' +floattostr(m[1,1]) +' ' +floattostr(m[1,2]) +'; '
+floattostr(m[2,0])+' ' +floattostr(m[2,1]) +' ' +floattostr(m[2,2]) +'] ');
end;*)
type TFByte = File of Byte;
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;
function readVTKHeader (var fname: string; var nhdr: TNIFTIhdr; var gzBytes: int64; var swapEndian: boolean): boolean;
//VTK Simple Legacy Formats : STRUCTURED_POINTS : BINARY
// http://daac.hpc.mil/gettingStarted/VTK_DataFormats.html
// https://github.com/bonilhamusclab/MRIcroS/blob/master/%2BfileUtils/%2Bvtk/readVtk.m
// http://www.ifb.ethz.ch/education/statisticalphysics/file-formats.pdf
// ftp://ftp.tuwien.ac.at/visual/vtk/www/FileFormats.pdf
// "The VTK data files described here are written in big endian form"
label
666;
var
f: TFByte;//TextFile;
strlst: TStringList;
str: string;
i, num_vox: integer;
begin
gzBytes := 0;
{$IFDEF ENDIAN_BIG}
swapEndian := false;
{$ELSE}
swapEndian := true;
{$ENDIF}
result := false;
strlst:=TStringList.Create;
FileMode := fmOpenRead;
AssignFile(f, fname);
{$IFDEF FPC} Reset(f,1); {$ELSE} Reset(f); {$ENDIF}
ReadLnBin(f, str); //signature: '# vtk DataFile'
if pos('VTK', UpperCase(str)) <> 3 then begin
showmessage('Not a VTK file');
goto 666;
end;
ReadLnBin(f, str); //comment: 'Comment: created with MRIcroS'
ReadLnBin(f, str); //kind: 'BINARY' or 'ASCII'
if pos('BINARY', UpperCase(str)) <> 1 then begin // '# vtk DataFile'
showmessage('Only able to read binary VTK file:'+str);
goto 666;
end;
ReadLnBin(f, str); // kind, e.g. "DATASET POLYDATA" or "DATASET STRUCTURED_ POINTS"
if pos('STRUCTURED_POINTS', UpperCase(str)) = 0 then begin
showmessage('Only able to read VTK images saved as STRUCTURED_POINTS, not '+ str);
goto 666;
end;
while (str <> '') and (pos('POINT_DATA', UpperCase(str)) = 0) do begin
ReadLnBin(f, str);
strlst.DelimitedText := str;
if pos('DIMENSIONS', UpperCase(str)) <> 0 then begin //e.g. "DIMENSIONS 128 128 128"
nhdr.dim[1] := StrToIntDef(strlst[1],1);
nhdr.dim[2] := StrToIntDef(strlst[2],1);
nhdr.dim[3] := StrToIntDef(strlst[3],1);
end; //dimensions
if (pos('ASPECT_RATIO', UpperCase(str)) <> 0) or (pos('SPACING', UpperCase(str)) <> 0) then begin //e.g. "ASPECT_RATIO 1.886 1.886 1.913"
nhdr.pixdim[1] := StrToFloatDef(strlst[1],1);
nhdr.pixdim[2] := StrToFloatDef(strlst[2],1);
nhdr.pixdim[3] := StrToFloatDef(strlst[3],1);
//showmessage(format('%g %g %g',[nhdr.pixdim[1], nhdr.pixdim[2], nhdr.pixdim[3] ]));
end; //aspect ratio
if (pos('ORIGIN', UpperCase(str)) <> 0) then begin //e.g. "ASPECT_RATIO 1.886 1.886 1.913"
nhdr.srow_x[3] := -StrToFloatDef(strlst[1],1);
nhdr.srow_y[3] := -StrToFloatDef(strlst[2],1);
nhdr.srow_z[3] := -StrToFloatDef(strlst[3],1);
//showmessage(format('%g %g %g',[nhdr.pixdim[1], nhdr.pixdim[2], nhdr.pixdim[3] ]));
end; //aspect ratio
end; //not POINT_DATA
if pos('POINT_DATA', UpperCase(str)) = 0 then goto 666;
num_vox := StrToIntDef(strlst[1],0);
if num_vox <> (nhdr.dim[1] * nhdr.dim[2] * nhdr.dim[3]) then begin
showmessage(format('Expected POINT_DATA to equal %dx%dx%d',[nhdr.dim[1], nhdr.dim[2], nhdr.dim[3] ]));
goto 666;
end;
ReadLnBin(f, str);
if pos('SCALARS', UpperCase(str)) = 0 then goto 666; //"SCALARS scalars unsigned_char"
strlst.DelimitedText := str;
str := UpperCase(strlst[2]);
//dataType is one of the types bit, unsigned_char, char, unsigned_short, short, unsigned_int, int, unsigned_long, long, float, or double
if pos('UNSIGNED_CHAR', str) <> 0 then
nhdr.datatype := kDT_UINT8 //
else if pos('SHORT', str) <> 0 then
nhdr.datatype := kDT_INT16 //
else if pos('UNSIGNED_SHORT', str) <> 0 then
nhdr.datatype := kDT_UINT16 //
else if pos('INT', str) <> 0 then
nhdr.datatype := kDT_INT32 //
else if pos('FLOAT', str) <> 0 then
nhdr.datatype := kDT_FLOAT
else if pos('DOUBLE', str) <> 0 then
nhdr.datatype := kDT_DOUBLE
else begin
showmessage('Unknown VTK scalars type '+str);
goto 666;
end;
convertForeignToNifti(nhdr);
//showmessage(inttostr(nhdr.datatype));
ReadLnBin(f, str);
if pos('LOOKUP_TABLE', UpperCase(str)) = 0 then goto 666; //"LOOKUP_TABLE default"
nhdr.vox_offset := filepos(f);
//fill matrix
for i := 0 to 2 do begin
nhdr.srow_x[i] := 0;
nhdr.srow_y[i] := 0;
nhdr.srow_z[i] := 0;
end;
nhdr.srow_x[0] := nhdr.pixdim[1];
nhdr.srow_y[1] := nhdr.pixdim[2];
nhdr.srow_z[2] := nhdr.pixdim[3];
//showmessage('xx' +inttostr( filepos(f) ));
result := true;
666:
closefile(f);
strlst.Free;
end;
function readMHAHeader (var fname: string; var nhdr: TNIFTIhdr; var gzBytes: int64; var swapEndian: boolean): boolean;
//Read VTK "MetaIO" format image
//http://www.itk.org/Wiki/ITK/MetaIO/Documentation#Reading_a_Brick-of-Bytes_.28an_N-Dimensional_volume_in_a_single_file.29
//https://www.assembla.com/spaces/plus/wiki/Sequence_metafile_format
//http://itk-insight-users.2283740.n2.nabble.com/MHA-MHD-File-Format-td7585031.html
var
FP: TextFile;
str, tagName, elementNames, pth, nam, ext: string;
ch: char;
isLocal,compressedData: boolean;
matOrient, mat, d, t: mat33;
//compressedDataSize,
nPosition, nOffset, matElements, matElementsOrient, headerSize, nItems, nBytes, i, channels, fileposBytes: longint;
offset,position, elementSize: array [0..3] of single;
transformMatrix: array [0..11] of single;
mArray: TStringList;
begin
result := false;
if not FileExists(fname) then exit;
{$IFDEF FPC}
DefaultFormatSettings.DecimalSeparator := '.' ;
// DecimalSeparator := '.';
{$ELSE}
DecimalSeparator := '.';
{$ENDIF}
for i := 0 to 3 do begin
position[i] := 0;
offset[i] := 0;
elementSize[i] := 1;
end;
nPosition := 0;
nOffset := 0;
gzBytes := 0;
fileposBytes := 0;
//compressedDataSize := 0;
swapEndian := false;
isLocal := true; //image and header embedded in same file, if false detached image
headerSize := 0;
matElements := 0;
matElementsOrient := 0;
compressedData := false;
mArray := TStringList.Create;
Filemode := fmOpenRead;
AssignFile(fp,fname);
reset(fp);
while not EOF(fp) do begin
str := '';
while not EOF(fp) do begin
read(fp,ch);
inc(fileposBytes);
if (ch = chr($0D)) or (ch = chr($0A)) then break;
str := str+ch;
end;
if (length(str) < 1) or (str[1]='#') then continue;
splitstrStrict('=',str,mArray);
if (mArray.count < 2) then continue;
tagName := cleanStr(mArray[0]);
elementNames := mArray[1];
splitstr(',',elementNames,mArray);
nItems :=mArray.count;
if (nItems < 1) then continue;
for i := 0 to (nItems-1) do
mArray[i] := cleanStr(mArray[i]); //remove '(' and ')',
if AnsiContainsText(tagName, 'ObjectType') and (not AnsiContainsText(mArray.Strings[0], 'Image')) then begin
NSLog('Expecting file with tag "ObjectType = Image" instead of "ObjectType = '+mArray.Strings[0]+'"');
end {else if AnsiContainsText(tagName, 'NDims') then begin
nDims := strtoint(mArray[0]);
if (nDims > 4) then begin
NSLog('Warning: only reading first 4 dimensions');
nDims := 4;
end;
end} else if AnsiContainsText(tagName, 'BinaryDataByteOrderMSB') then begin
{$IFDEF ENDIAN_BIG} //data always stored big endian
if not AnsiContainsText(mArray[0], 'True') then swapEndian := true;
{$ELSE}
if AnsiContainsText(mArray[0], 'True') then swapEndian := true;
{$ENDIF}
end {else if AnsiContainsText(tagName, 'BinaryData') then begin
if AnsiContainsText(mArray[0], 'True') then binaryData := true;
end else if AnsiContainsText(tagName, 'CompressedDataSize') then begin
compressedDataSize := strtoint(mArray[0]);
end} else if AnsiContainsText(tagName, 'CompressedData') then begin
if AnsiContainsText(mArray[0], 'True') then
compressedData := true;
end else if AnsiContainsText(tagName, 'Orientation') and (not AnsiContainsText(tagName, 'Anatomical') ) then begin
if (nItems > 12) then nItems := 12;
matElementsOrient := nItems;
for i := 0 to (nItems-1) do
transformMatrix[i] := strtofloat(mArray[i]);
if (matElementsOrient >= 12) then
LOAD_MAT33(matOrient, transformMatrix[0],transformMatrix[1],transformMatrix[2],
transformMatrix[4],transformMatrix[5],transformMatrix[6],
transformMatrix[8],transformMatrix[9],transformMatrix[10])
else if (matElementsOrient >= 9) then
LOAD_MAT33(matOrient, transformMatrix[0],transformMatrix[1],transformMatrix[2],
transformMatrix[3],transformMatrix[4],transformMatrix[5],
transformMatrix[6],transformMatrix[7],transformMatrix[8]);
end else if AnsiContainsText(tagName, 'TransformMatrix') then begin
if (nItems > 12) then nItems := 12;
matElements := nItems;
for i := 0 to (nItems-1) do
transformMatrix[i] := strtofloat(mArray[i]);
if (matElements >= 12) then
LOAD_MAT33(mat, transformMatrix[0],transformMatrix[1],transformMatrix[2],
transformMatrix[4],transformMatrix[5],transformMatrix[6],
transformMatrix[8],transformMatrix[9],transformMatrix[10])
else if (matElements >= 9) then
LOAD_MAT33(mat, transformMatrix[0],transformMatrix[1],transformMatrix[2],
transformMatrix[3],transformMatrix[4],transformMatrix[5],
transformMatrix[6],transformMatrix[7],transformMatrix[8]);
end else if AnsiContainsText(tagName, 'Position') then begin
if (nItems > 3) then nItems := 3;
nPosition := nItems;
for i := 0 to (nItems-1) do
position[i] := strtofloat(mArray[i]);
end else if AnsiContainsText(tagName, 'Offset') then begin
if (nItems > 3) then nItems := 3;
nOffset := nItems;
for i := 0 to (nItems-1) do
offset[i] := strtofloat(mArray[i]);
end else if AnsiContainsText(tagName, 'AnatomicalOrientation') then begin
//e.g. RAI
end else if AnsiContainsText(tagName, 'ElementSpacing') then begin