-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdicom.pas
More file actions
6918 lines (6730 loc) · 272 KB
/
dicom.pas
File metadata and controls
6918 lines (6730 loc) · 272 KB
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 dicom;
// Limitations
//- compiling for Pascal other than Delphi 2.0+: gDynStr gets VERY big, e.g. not standard Pascal string with maximum size of 256 bytes
//- write_dicom: currently only writes little endian, data should be little_endian
//- rev 7 has disk caching: speeds DCOM header reading
//- rev 8 can read interfile format images
//- rev 9 Siemens Magnetom, GELX
//- rev 10 ECAT6/7, DICOM runlengthencoding[RLE] parameters
// *NOTE: If your software does not decompress images, check to make sure that
// DICOMdata.CompressOffset = 0
// This value will be > 0 for any DICOM/GE/Elscint file with compressed image data
// modified ACC 27/3/2018 to handle regional settings
{$IFDEF FPC} //ACC 1/7/2009
{$mode delphi}
{$ENDIF}
interface
{$IFDEF LINUX}
{$IFDEF FPC} //workaround for Kylix-ACC 12/6/2009
uses SysUtils,Dialogs,Controls,define_types,classes;
{$ELSE}
uses SysUtils,QDialogs,QControls,define_types,classes;
{$ENDIF}
{$ELSE}
uses
SysUtils,Dialogs,Controls,define_types,classes {tstrings};
{$ENDIF}
{$H+} //use long, dynamic strings
const
kCR = chr (13);//PC EOLN
kA = ord('A');
kB = ord('B');
kC = ord('C');
kD = ord('D');
kE = ord('E');
kF = ord('F');
kH = ord('H');
kI = ord('I');
kL = ord('L');
kM = ord('M');
kN = ord('N');
kO = ord('O');
kP = ord('P');
kQ = ord('Q');
kS = ord('S');
kT = ord('T');
kU = ord('U');
kW = ord('W');
procedure write_vista (lAnzFileStrs: Tstrings);
procedure read_afni_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string; var lRotation1,lRotation2,lRotation3: integer);
procedure read_ecat_data(var lDICOMdata: DICOMdata;lVerboseRead,lReadECAToffsetTables:boolean; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;lFileName: string);
{- if lReadECAToffsetTables is true, you will need to freemem gECAT_slice_table if it is filled: see example}
{-for analysis, you should also take scaling and calibration factors into account!}
procedure read_siemens_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;lFileName: string);
//procedure write_slc (lFileName: string; var pDICOMdata: DICOMdata;var lSz: integer; lDICOM3: boolean);
procedure read_ge_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;lFileName: string);
procedure read_interfile_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string);
procedure read_vista_data(lConvertToAnalyze,lAnonymize: boolean; var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string);
procedure read_voxbo_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string);
procedure read_PAR_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string; lReadOffsetTables: boolean; var lOffset_pos_table: LongIntp; var lOffsetTableEntries: integer; lReadVaryingScaleFactors: boolean; var lVaryingScaleFactors_table,lVaryingIntercept_table: Singlep; var lVaryingScaleFactorsTableEntries, lnum4Ddatasets: integer);
procedure read_VFF_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string);
procedure read_picker_data(lVerboseRead: boolean; var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;lFileName: string);
procedure write_dicom (lFileName: string; var lInputDICOMdata: DICOMdata;var lSz: integer; lDICOM3: boolean);
procedure read_tiff_data(var lDICOMdata: DICOMdata; var lReadOffsets,lHdrOK, lImageFormatOK: boolean; var lDynStr: string;lFileName: string);
procedure read_dicom_data(lReadJPEGtables,lVerboseRead,lAutoDECAT7,lReadECAToffsetTables,lAutodetectInterfile,lAutoDetectGenesis,lReadColorTables: boolean; var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string);
procedure clear_dicom_data (var lDicomdata:Dicomdata);
{- if lReadECAToffsetTables is true, you will need to freemem gECAT_slice_table if it is filled: see example}
{- if lReadColorTables is true, you will need to freemem red_table/green_table/blue_table if it is filled: see example}
procedure write_interfile_hdr (lHdrName,lImgName: string; var pDICOMdata: DICOMdata);
var
gSizeMMWarningShown : boolean = false;
//gAnonymize: boolean = true;
gECATJPEG_table_entries: integer = 0;
gECATJPEG_pos_table,gECATJPEG_size_table : LongIntP;
red_table_size : Integer = 0;
green_table_size : Integer = 0;
blue_table_size : Integer = 0;
red_table : ByteP;
green_table : ByteP;
blue_table : ByteP;
implementation
function RobustStrToFloat(s:string):extended;
var fs: TFormatSettings;
begin
fs := DefaultFormatSettings;
Result := 0;
try
Result := StrToFloat(s);
except
on EConvertError do {try again using other decimal separator}
begin
if fs.DecimalSeparator = ',' then
fs.DecimalSeparator := '.'
else
fs.DecimalSeparator := ',';
Result := StrToFloat(s,fs);
end;
end;
end;
procedure write_interfile_hdr (lHdrName,lImgName: string; var pDICOMdata: DICOMdata);
var
lTextFile: textfile;
//creates interfile text header "lHdrName" that points to the image "lImgName")
//pass pDICOMdata that contains the relevant image details
begin
if (pDICOMdata.Allocbits_per_pixel <> 8) and (pDICOMdata.Allocbits_per_pixel <> 16) then begin
showmessage('Can only create Interfile headers for 8 or 16 bit images.');
end;
if fileexists(lHdrName) then begin
showmessage('The file '+lHdrName+' already exists. Unable to create Interfile format header.');
exit;
end;
assignfile(lTextFile,lHdrName);
rewrite(lTextFile);
writeln(lTextFile,'!INTERFILE :=');
writeln(lTextFile,'!imaging modality:=nucmed');
writeln(lTextFile,'!originating system:=MS-DOS');
writeln(lTextFile,'!version of keys:=3.3');
writeln(lTextFile,'conversion program:=DICOMxv');
writeln(lTextFile,'program author:=C. Rorden');
writeln(lTextFile,'!GENERAL DATA:=');
writeln(lTextFile,'!data offset in bytes:='+inttostr(pDicomData.imagestart));
writeln(lTextFile,'!name of data file:='+extractfilename(lImgName));
writeln(lTextFile,'data compression:=none');
writeln(lTextFile,'data encode:=none');
writeln(lTextFile,'!GENERAL IMAGE DATA :=');
if pDICOMdata.little_endian = 1 then
writeln(lTextFile,'imagedata byte order := LITTLEENDIAN')
else
writeln(lTextFile,'imagedata byte order := BIGENDIAN');
writeln(lTextFile,'!matrix size [1] :='+inttostr(pDICOMdata.XYZdim[1]));
writeln(lTextFile,'!matrix size [2] :='+inttostr(pDICOMdata.XYZdim[2]));
writeln(lTextFile,'!matrix size [3] :='+inttostr(pDICOMdata.XYZdim[3]));
if pDICOMdata.Allocbits_per_pixel = 8 then begin
writeln(lTextFile,'!number format := unsigned integer');
writeln(lTextFile,'!number of bytes per pixel := 1');
end else begin
writeln(lTextFile,'!number format := signed integer');
writeln(lTextFile,'!number of bytes per pixel := 2');
end;
writeln(lTextFile,'scaling factor (mm/pixel) [1] :='+floattostrf(pDicomData.XYZmm[1],ffFixed,7,7));
writeln(lTextFile,'scaling factor (mm/pixel) [2] :='+floattostrf(pDicomData.XYZmm[2],ffFixed,7,7));
writeln(lTextFile,'scaling factor (mm/pixel) [3] :='+floattostrf(pDicomData.XYZmm[3],ffFixed,7,7));
writeln(lTextFile,'!number of slices :='+inttostr(pDICOMdata.XYZdim[3]));
writeln(lTextFile,'slice thickness := '+floattostrf(pDicomData.XYZmm[3],ffFixed,7,7));
writeln(lTextFile,'!END OF INTERFILE:=');
closefile(lTextFile);
end; (**)
procedure clear_dicom_data (var lDicomdata:Dicomdata);
begin
red_table_size := 0;
green_table_size := 0;
blue_table_size := 0;
red_table := nil;
green_table := nil;
blue_table := nil;
with lDicomData do begin
PatientIDInt := 0;
PatientName := 'NO NAME';
PatientID := 'NO ID';
StudyDate := '';
AcqTime := '';
ImgTime := '';
TR := 0;
TE := 0;
kV := 0;
mA := 0;
Rotate180deg := false;
MaxIntensity := 0;
MinIntensity := 0;
MinIntensitySet := false;
ElscintCompress := false;
Float := false;
ImageNum := 0;
SiemensInterleaved := 2; //0=no,1=yes,2=undefined
SiemensSlices := 0;
SiemensMosaicX := 1;
SiemensMosaicY := 1;
IntenScale := 1;
intenIntercept := 0;
SeriesNum := 1;
AcquNum := 0;
ImageNum := 1;
Accession := 1;
PlanarConfig:= 0; //only used in RGB values
runlengthencoding := false;
CompressSz := 0;
CompressOffset := 0;
SamplesPerPixel := 1;
WindowCenter := 0;
WindowWidth := 0;
monochrome := 2; {most common}
XYZmm[1] := 1;
XYZmm[2] := 1;
XYZmm[3] := 1;
XYZdim[1] := 1;
XYZdim[2] := 1;
XYZdim[3] := 1;
XYZdim[4] := 1;
lDicomData.XYZori[1] := 0;
lDicomData.XYZori[2] := 0;
lDicomData.XYZori[3] := 0;
ImageStart := 0;
Little_Endian := 0;
Allocbits_per_pixel := 16;//bits
Storedbits_per_pixel:= Allocbits_per_pixel;
GenesisCpt := false;
JPEGlosslesscpt := false;
JPEGlossycpt := false;
GenesisPackHdr := 0;
StudyDatePos := 0;
NamePos := 0;
RLEredOffset:= 0;
RLEgreenOffset:= 0;
RLEblueOffset:= 0;
RLEredSz:= 0;
RLEgreenSz:= 0;
RLEblueSz:= 0;
Spacing:=0;
Location:=0;
//Frames:=1;
Modality:='MR';
serietag:='';
end;
end;
procedure read_ecat_data(var lDICOMdata: DICOMdata;lVerboseRead,lReadECAToffsetTables:boolean; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;lFileName: string);
label
121,539;
const
kMaxnSLices = 6000;
kStrSz = 40;
var
lLongRA: LongIntp;
lECAT7sigUpcase,lECAT7sig : array [0..6] of Char;
lParse,lSPos,lFPos{,lScomplement},lF,lS,lYear,lFrames,lVox,lHlfVox,lJ,lPass,lVolume,lNextDirectory,lSlice,lSliceSz,lVoxelType,lPos,lEntry,
lSlicePos,lLongRApos,lLongRAsz,{lSingleRApos,lSingleRAsz,}{lMatri,}lX,lY,lZ,lCacheSz,lImgSz,lTransferred,lSubHeadStart,lMatrixStart,lMatrixEnd,lInt,lInt2,lInt3,lINt4,n,filesz: LongInt;
lPlanes,lGates,lAqcType,lFileType,lI,lWord, lWord22: word;
lXmm,lYmm,lZmm,lCalibrationFactor, lQuantScale: real;
FP: file;
lCreateTable,lSwapBytes,lMR,lECAT6: boolean;
function xWord(lPos: longint): word;
var
s: word;
begin
seek(fp,lPos);
BlockRead(fp, s, 2, n);
if lSwapBytes then
result := swap(s)
else result := s; //assign address of s to inguy
end;
function swap32i(lPos: longint): Longint;
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
1:(Long:LongInt);
end;
swaptypep = ^swaptype;
var
s : LongInt;
inguy:swaptypep;
outguy:swaptype;
begin
seek(fp,lPos);
BlockRead(fp, s, 4, n);
inguy := @s; //assign address of s to inguy
if not lSwapBytes then begin
result := inguy^.long; //added ^ - ACC 12/6/2009
exit;
end;
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
swap32i:=outguy.Long;
end;
function StrRead (lPos, lSz: longint) : string;
var
I: integer;
tx : array [1..kStrSz] of Char;
begin
result := '';
if lSz > kStrSz then exit;
seek(fp, lPos{-1});
BlockRead(fp, tx, lSz*SizeOf(Char), n);
for I := 1 to (lSz-1) do begin
if tx[I] in [' ','[',']','+','-','.','\','~','/', '0'..'9','a'..'z','A'..'Z'] then
{if (tx[I] <> kCR) and (tx[I] <> UNIXeoln) then}
result := result + tx[I];
end;
end;
function fswap4r (lPos: longint): single;
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
1:(float:single);
end;
swaptypep = ^swaptype;
var
s:single;
inguy:swaptypep;
outguy:swaptype;
begin
seek(fp,lPos);
if not lSwapBytes then begin
BlockRead(fp, result, 4, n);
exit;
end;
BlockRead(fp, s, 4, n);
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
fswap4r:=outguy.float;
end;
function fvax4r (lPos: longint): single;
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
1:(float:single);
end;
swaptypep = ^swaptype;
var
s:single;
lT1,lT2 : word;
inguy:swaptypep;
begin
seek(fp,lPos);
BlockRead(fp, s, 4, n);
inguy := @s;
if (inguy^.Word1 =0) and (inguy^.Word2 = 0) then begin //added ^ - ACC 12/6/2009
result := 0;
exit;
end;
lT1 := inguy^.Word1 and $80FF;
lT2 := ((inguy^.Word1 and $7F00) +$FF00) and $7F00;
inguy^.Word1 := inguy^.Word2;
inguy^.Word2 := (lt1+lT2);
fvax4r:=inguy^.float;
end;
begin
Clear_Dicom_Data(lDicomData);
if gECATJPEG_table_entries <> 0 then begin
freemem (gECATJPEG_pos_table);
freemem (gECATJPEG_size_table);
gECATJPEG_table_entries := 0;
end;
lHdrOK:= false;
lQuantScale:= 1;
lCalibrationFactor := 1;
lLongRASz := 0;
lLongRAPos := 0;
lImageFormatOK := false;
lVolume := 1;
if not fileexists(lFileName) then begin
showmessage('Unable to find the image '+lFileName);
exit;
end;
FileMode := 0; //set to readonly
AssignFile(fp, lFileName);
Reset(fp, 1);
FileSz := FileSize(fp);
if filesz < (2048) then begin
showmessage('This file is to small to be a ECAT format image.');
goto 539;
end;
seek(fp, 0);
BlockRead(fp, lECAT7Sig, 6*SizeOf(Char){, n});
for lInt4 := 0 to (5) do begin
if lECAT7Sig[lInt4] in ['a'..'z','A'..'Z'] then
lECAT7SigUpCase[lInt4] := upcase(lECAT7Sig[lInt4])
else
lECAT7SigUpCase[lInt4] := ' ';
end;
if (lECAT7SigUpCase[0]='M') and (lECAT7SigUpCase[1]='A') and (lECAT7SigUpCase[2]='T') and (lECAT7SigUpCase[3]='R') and
(lECAT7SigUpCase[4]='I') and (lECAT7SigUpCase[5]='X') then
lECAT6 := false
else
lECAT6 := true;
if lEcat6 then begin
lSwapBytes := false;
lFileType := xWord(27*2);
if lFileType > 255 then lSwapBytes := not lSwapBytes;
lFileType := xWord(27*2);
lAqcType := xWord(175*2);
lPlanes := xWord(188*2);
lFrames := xword(189*2);
lGates := xWord(190*2);
lYear := xWord(70);
if (lPlanes < 1) or (lFrames < 1) or (lGates < 1) then begin
case MessageDlg('Warning: one of the planes/frames/gates values is less than 1 ['+inttostr(lPlanes)+'/'+inttostr(lFrames)+'/'+inttostr(lGates)+']. Is this file really ECAT 6 format? Press abort to cancel conversion. ',
mterror,[mbOK,mbAbort], 0) of
mrAbort: goto 539;
end; //case
end else if (lYear < 1940) or (lYear > 3000) then begin
case MessageDlg('Warning: the year value appears invalid ['+inttostr(lYear)+']. Is this file really ECAT 6 format? Press abort to cancel conversion. ',
mterror,[mbOK,mbAbort], 0) of
mrAbort: goto 539;
end; //case
end;
if lVerboseRead then begin
lDynStr :='ECAT6 data';
lDynStr :=lDynStr+kCR+('Patient Name:'+StrRead(190,32));
lDynStr :=lDynStr+kCR+('Patient ID:'+StrRead(174,16));
lDynStr :=lDynStr+kCR+('Study Desc:'+StrRead(318,32));
lDynStr := lDynStr+kCR+('Facility: '+StrRead(356,20));
lDynStr := lDynStr+kCR+('Planes: '+inttostr(lPlanes));
lDynStr := lDynStr+kCR+('Frames: '+inttostr(lFrames));
lDynStr := lDynStr+kCR+('Gates: '+inttostr(lGates));
lDynStr := lDynStr+kCR+('Date DD/MM/YY: '+ inttostr(xWord(66))+'/'+inttostr(xWord(68))+'/'+inttostr(lYear));
end; {show summary}
end else begin //NOT ECAT6
lSwapBytes := true;
lFileType := xWord(50);
if lFileType > 255 then lSwapBytes := not lSwapBytes;
lFileType := xWord(50);
lAqcType := xWord(328);
lPlanes := xWord(352);
lFrames := xWord(354);
lGates := xWord(356);
lCalibrationFactor := fswap4r(144);
if {(true) or} (lPlanes < 1) or (lFrames < 1) or (lGates < 1) then begin
case MessageDlg('Warning: on of the planes/frames/gates values is less than 1 ['+inttostr(lPlanes)+'/'+inttostr(lFrames)+'/'+inttostr(lGates)+']. Is this file really ECAT 7 format? Press abort to cancel conversion. ',
mterror,[mbOK,mbAbort], 0) of
mrAbort: goto 539;
end; //case
end; //error
if lVerboseRead then begin
lDynStr := 'ECAT 7 format';
lDynStr := lDynStr+kCR+('Serial Number:'+StrRead(52,10));
lDynStr := lDynStr+kCR+('Patient Name:'+StrRead(182,32));
lDynStr := lDynStr+kCR+('Patient ID:'+StrRead(166,16));
lDynStr := lDynStr+kCR+('Study Desc:'+StrRead(296,32));
lDynStr := lDynStr+kCR+('Facility: '+StrRead(332,20));
lDynStr := lDynStr+kCR+('Scanner: '+inttostr(xWord(48)));
lDynStr := lDynStr+kCR+('Planes: '+inttostr(lPlanes));
lDynStr := lDynStr+kCR+('Frames: '+inttostr(lFrames));
lDynStr := lDynStr+kCR+('Gates: '+inttostr(lGates));
lDynStr := lDynStr+kCR+'Calibration: '+floattostr(lCalibrationFactor);
end; {lShow Summary}
end; //lECAT7
if lFiletype = 9 then lFiletype := 7; //1364: treat projections as Volume16's
if not (lFileType in [1,2,3,4,7]) then begin
Showmessage('This software does not recognize the ECAT file type. Selected filetype: '+inttostr(lFileType));
goto 539;
end;
lVoxelType := 2;
if lFileType = 3 then lVoxelType := 4;
if lVerboseRead then begin
case lFileType of
1: lDynStr := lDynStr+kCR+('File type: Scan File');
2: lDynStr := lDynStr+kCR+('File type: Image File'); //x
3: lDynStr := lDynStr+kCR+('File type: Attn File');
4: lDynStr := lDynStr+kCR+('File type: Norm File');
7: lDynStr := lDynStr+kCR+('File type: Volume 16'); //x
end; //lfiletye case
case lAqcType of
1:lDynStr := lDynStr+kCR+('Acquisition type: Blank');
2:lDynStr := lDynStr+kCR+('Acquisition type: Transmission');
3:lDynStr := lDynStr+kCR+('Acquisition type: Static Emission');
4:lDynStr := lDynStr+kCR+('Acquisition type: Dynamic Emission');
5:lDynStr := lDynStr+kCR+('Acquisition type: Gated Emission');
6:lDynStr := lDynStr+kCR+('Acquisition type: Transmission Rect');
7:lDynStr := lDynStr+kCR+('Acquisition type: Emission Rect');
8:lDynStr := lDynStr+kCR+('Acquisition type: Whole Body Transm');
9:lDynStr := lDynStr+kCR+('Acquisition type: Whole Body Static');
else lDynStr := lDynStr+kCR+('Acquisition type: Undefined');
end; //case AqcType
end; //verbose read
if ((lECAT6) and (lFiletype =2)) or ({(not lECAT6) and} (lFileType=7)) then //Kludge
else begin
Showmessage('Unusual ECAT filetype. Please contact the author.');
goto 539;
end;
lHdrOK:= true;
lImageFormatOK := true;
lLongRASz := kMaxnSlices * sizeof(longint);
getmem(lLongRA,lLongRAsz);
lPos := 512;
//lSingleRASz := kMaxnSlices * sizeof(single);
//getmem(lSingleRA,lSingleRAsz);
//lMatri := 0;
lVolume := 1;
lPass := 0;
121:
lEntry := 1;
lInt := swap32i(lPos);
lInt2 := swap32i(lPos+4);
lNextDirectory := lInt2;
while true do begin
inc(lEntry);
lPos := lPos + 16;
lInt := swap32i(lPos);
lInt2 := swap32i(lPos+4);
lInt3 := swap32i(lPos+8);
lInt4 := swap32i(lPos+12);
lInt2 := lInt2 - 1;
lSubHeadStart := lINt2 *512;
lMatrixStart := ((lInt2) * 512)+512 {add subhead sz};
lMatrixEnd := lInt3 * 512;
if (lInt4 = 1) and (lMatrixStart < FileSz) and (lMatrixEnd <= FileSz) then begin
if (lFileType= 7) {or (lFileType = 4) } or (lFileType = 2) then begin //Volume of 16-bit integers
if lEcat6 then begin
lX := xWord(lSubHeadStart+(66*2));
lY := xWord(lSubHeadStart+(67*2));
lZ := 1;//uxWord(lSubHeadStart+8);
lXmm := 10*fvax4r(lSubHeadStart+(92*2));// fswap4r(lSubHeadStart+(92*2));
lYmm := lXmm;//read32r(lSubHeadStart+(94*2));
lZmm := 10 * fvax4r(lSubHeadStart+(94*2));
lCalibrationFactor := fvax4r(lSubHeadStart+(194*2));
lQuantScale := fvax4r(lSubHeadStart+(86*2));
if lVerboseRead then
lDynStr := lDynStr+kCR+'Plane '+inttostr(lPass+1)+' Calibration/Scale Factor: '+floattostr(lCalibrationFactor)+'/'+floattostr(lQuantScale);
end else begin
//02 or 07
lX := xWord(lSubHeadStart+4);
lY := xWord(lSubHeadStart+6);
lZ := xWord(lSubHeadStart+8);
//if lFileType <> 4 then begin
lXmm := 10*fswap4r(lSubHeadStart+34);
lYmm := 10*fswap4r(lSubHeadStart+38);
lZmm := 10*fswap4r(lSubHeadStart+42);
lQuantScale := fswap4r(lSubHeadStart+26);
if lVerboseRead then
lDynStr := lDynStr+kCR+'Volume: '+inttostr(lPass+1)+' Scale Factor: '+floattostr(lQuantScale);
//end; //filetype <> 4
end; //ecat7
if true then begin
//FileMode := 2; //set to read/write
inc(lPass);
lImgSz := lX * lY * lZ * lVoxelType; {2 bytes per voxel}
lSliceSz := lX * lY * lVoxelType;
if lZ < 1 then begin
lHdrOK := false;
goto 539;
end;
lSlicePos := lMatrixStart;
if ((lECAT6) and (lPass = 1)) or ( (not lECAT6)) then begin
lDICOMdata.XYZdim[1] := lX;
lDICOMdata.XYZdim[2] := lY;
lDICOMdata.XYZdim[3] := lZ;
lDICOMdata.XYZmm[1] := lXmm;
lDICOMdata.XYZmm[2] := lYmm;
lDICOMdata.XYZmm[3] := lZmm;
case lVoxelType of
1: begin
Showmessage('Error: 8-bit data not supported [yet]. Please contact the author.');
lDicomData.Allocbits_per_pixel := 8;
lHdrOK := false;
goto 539;
end;
4: begin
Showmessage('Error: 32-bit data not supported [yet]. Please contact the author.');
lHdrOK := false;
goto 539;
end;
else begin //16-bit integers
lDicomData.Allocbits_per_pixel := 16;
end;
end; {case lVoxelType}
end else begin //if lECAT6
if (lDICOMdata.XYZdim[1] <> lX) or (lDICOMdata.XYZdim[2] <> lY) or (lDICOMdata.XYZdim[3] <> lZ) then begin
Showmessage('Error: different slices in this volume have different slice sizes. Please contact the author.');
lHdrOK := false;
goto 539;
end; //dimensions have changed
//lSlicePos :=((lMatri-1)*lImgSz);
end; //ECAT6
lVox := lSliceSz div 2;
lHlfVox := lSliceSz div 4;
for lSlice := 1 to lZ do begin
if (not lECAT6) then
lSlicePos := ((lSlice-1)*lSliceSz)+lMatrixStart;
if lLongRAPos >= kMaxnSLices then begin
lHdrOK := false;
goto 539;
end;
inc(lLongRAPos);
lLongRA[lLongRAPos] := lSlicePos;
{inc(lSingleRAPos);
if lCalibTableType = 1 then
lSingleRA[lSingleRAPos] := lQuantScale
else
lSingleRA[lSingleRAPos] := lCalibrationFactor *lQuantScale;}
end; //slice 1..lZ
if not lECAT6 then inc(lVolume);
end; //fileexistsex
end; //correct filetype
end; //matrix start/end within filesz
if (lMatrixStart > FileSz) or (lMatrixEnd >= FileSz) then goto 539;
if ((lEntry mod 32) = 0) then begin
if ((lNextDirectory-1)*512) <= lPos then goto 539; //no more directories
lPos := (lNextDirectory-1)*512;
goto 121;
end; //entry 32
end ; //while true
539:
CloseFile(fp);
FileMode := 2; //set to read/write
lDicomData.XYZdim[3] := lLongRApos;
if not lECAT6 then dec(lVolume); //ECAT7 increments immediately before exiting loop - once too often
lDicomData.XYZdim[4] :=(lVolume);
if lSwapBytes then
lDicomData.little_endian := 0
else
lDicomData.little_endian := 1;
if (lLongRApos > 0) and (lHdrOK) then begin
lDicomData.ImageStart := lLongRA[1];
lCreateTable := false;
if (lLongRApos > 1) then begin
lFPos := lDICOMdata.ImageStart;
for lS := 2 to lLongRApos do begin
lFPos := lFPos + lSliceSz;
if lFPos <> lLongRA[lS] then lCreateTable := true;
end;
if (lCreateTable) and (lReadECAToffsetTables) then begin
gECATJPEG_table_entries := lLongRApos;
getmem (gECATJPEG_pos_table, gECATJPEG_table_entries*sizeof(longint));
getmem (gECATJPEG_size_table, gECATJPEG_table_entries*sizeof(longint));
for lS := 1 to gECATJPEG_table_entries do
gECATJPEG_pos_table[lS] := lLongRA[lS]
end else if (lCreateTable) then
lImageFormatOK := false; //slices are offset within this file
end;
if (lVerboseRead) and (lHdrOK) then begin
lDynStr :=lDynStr+kCR+('XYZdim:'+inttostr(lX)+'/'+inttostr(lY)+'/'+inttostr(gECATJPEG_table_entries));
lDynStr :=lDynStr+kCR+('XYZmm: '+floattostrf(lDicomData.XYZmm[1],ffFixed,7,7)+'/'+floattostrf(lDicomData.XYZmm[2],ffFixed,7,7)
+'/'+floattostrf(lDicomData.XYZmm[3],ffFixed,7,7));
lDynStr :=lDynStr+kCR+('Bits per voxel: '+inttostr(lDicomData.Storedbits_per_pixel));
lDynStr :=lDynStr+kCR+('Image Start: '+inttostr(lDicomData.ImageStart));
if lCreateTable then
lDynStr :=lDynStr+kCR+('Note: staggered slice offsets');
end
end;
lDicomData.Storedbits_per_pixel:= lDicomData.Allocbits_per_pixel;
if lLongRASz > 0 then
freemem(lLongRA);
(*if (lSingleRApos > 0) and (lHdrOK) and (lCalibTableType <> 0) then begin
gECAT_scalefactor_entries := lSingleRApos;
getmem (gECAT_scalefactor_table, gECAT_scalefactor_entries*sizeof(single));
for lS := 1 to gECAT_scalefactor_entries do
gECAT_scalefactor_table[lS] := lSingleRA[lS];
end;
if lSingleRASz > 0 then
freemem(lSingleRA);*)
end;
procedure write_vista (lAnzFileStrs: Tstrings);
const
kMaxImages = 256;
UNIXeoln = chr(10);
UNIXformfeed = chr(12);
var
lAHdr: AHdr;
lByteSwap: boolean;
lAnalyzeHdrRA: array [1..kMaxImages] of AHdr;
lByteSwapRA: array [1..kMaxImages] of boolean;
lImageOffset,lTimePoint,l3rdDim,lnSubVolumes,lSubVolume,lDataOffset,lVoxels,lInc,lLine,lHalfLines,l2DSz,lLowLine,lHighLine,lLineLen,lZSlice,lZSlices,lHdrSz,lFileSz,lXYZImgSz,lXYTImgSz,lHdr,lnHdr: integer;
lCondStr,lHdrFileName,lImgFileName,lVistaFileName,lRepnStr: string;
lSliceP,lLineP,lP: bytep;
lWordP: wordp;
lDoublep: Doublep;
lLongIntP: LongIntP;
lInFile,lOutFile: file;
lTextFile: textfile;
procedure riteln(lStr: string);
begin
write(lTextFile,lStr+UNIXeoln);
end;
begin
lnHdr := lAnzFileStrs.count;
if lnHdr < 1 then exit;
for lHdr := 1 to lnHdr do begin
lHdrFileName := lAnzFileStrs[lHdr-1];//-1 because tstrings are indexed from 0
lHdrFileName := changefileext(lHdrFileName,'.hdr');
if not fileexists(lHdrFileName) then exit;
lImgFileName := changefileext(lHdrFileName,'.img');
{$I-}
AssignFile(lInFile, lHdrFileName);
FileMode := 0; { Set file access to read only }
Reset(lInFile, 1);
lFileSz := FileSize(lInFile);
if lFileSz <> sizeof(AHdr) then begin
CloseFile(lInFile);
ShowMessage('The file "'+lHdrFileName+
'"is the wrong size to be an Analyze header.');
FileMode := 2; //set to read/write
exit;
end;
{$I+}
if ioresult <> 0 then
ShowMessage('Potential error in reading Analyze header.'+inttostr(IOResult));
BlockRead(lInFile, lAHdr, lFileSz);
CloseFile(lInFile);
FileMode := 2;
if (IOResult <> 0) then exit;
lHdrSz := lAHdr.HdrSz;
Swap4(lHdrSz);
if lAHdr.HdrSz = sizeof(AHdr) then begin
lByteSwap := false;
//little endian header
end else if SizeOf(AHdr) = lHdrSz then begin
SwapBytes (lAHdr); //big-endian: swap bytes
lByteSwap := true;
end else begin
ShowMessage('This software can not read the file "'+lHdrFileName+
'". The header file is not in Analyze format [the first 4 bytes do not have the value 348].');
exit;
end;
lXYZImgSz := lAHdr.Dim[1]*lAHdr.Dim[2]*lAHdr.Dim[3]*(lAHdr.bitpix div 8);
if FSize(lImgFileName) < lXYZImgSz then begin
ShowMessage('The image file "'+lImgFileName+'" is the wrong size');
exit;
end;
lAnalyzeHdrRA[lHdr] := lAHdr;
lByteSwapRA[lHdr] := lByteSwap;
end; //for each analyze header
lVistaFileName := changefileext(lAnzFileStrs[0],'.v');
if fileexists(lVistaFileName) then begin
showmessage('The file '+lVistaFileName+' already exists. Unable to create Interfile format header.');
exit;
end;
lDataOffset := 0; //bytes between end of header and image data
assignfile(lTextFile,lVistaFileName);
rewrite(lTextFile);
riteln('V-data 2 {'); //open bracket1: start header
for lHdr := 1 to lnHdr do begin //for each image
lAHdr := lAnalyzeHdrRA[lHdr];
if lAHdr.Dim[4] > 1 then begin
l3rdDim := lAHdr.Dim[4]; //with Lipsia, timepoints are contiguous
lnSubVolumes := lAHdr.Dim[3];//with Lipsia, 1 image per slice for 4d images
end else begin
l3rdDim := lAHdr.Dim[3]; //
lnSubVolumes := 1;
end;
for lSubvolume := 1 to lnSubVolumes do begin //once for each 2D/3D image, once per slice for each 4D image
riteln(kTab+'image: image {'); //open bracket2: start image
riteln(kTab+kTab+'data: '+inttostr(lDataOffset));//offset
lXYTImgSz := lAHdr.Dim[1]*lAHdr.Dim[2]*l3rdDim*(lAHdr.bitpix div 8);
lDataOffset := lDataOffset + lXYTImgSz; //increment for next image
riteln(kTab+kTab+'length: '+inttostr(lXYTImgSz));
riteln(kTab+kTab+'nbands: '+inttostr(l3rdDim));
riteln(kTab+kTab+'nframes: '+inttostr(l3rdDim));
riteln(kTab+kTab+'nrows: '+inttostr(lAHdr.Dim[2]));
riteln(kTab+kTab+'ncolumns: '+inttostr(lAHdr.Dim[1]));
case lAHdr.datatype of//compute voxel data type
1: lRepnStr := 'bit';
2: lRepnStr := 'ubyte';
4: lRepnStr := 'short';
8: lRepnStr := 'long';
16: lRepnStr := 'float';
64: lRepnStr := 'double';
end;
riteln(kTab+kTab+'repn: '+lRepnStr);
riteln(kTab+kTab+'patient: '+'NO_NAME');
riteln(kTab+kTab+'date: '+'"'+DateTimeToStr(Now)+'"');
//riteln(kTab+kTab+'date: '+'"07:54:50 11 Jan 2001"');
riteln(kTab+kTab+'modality: '+'"MR '+ extractfilename(lAnzFileStrs[lHdr-1])+'"');
riteln(kTab+kTab+'angle: '+'"0 0 0"');
riteln(kTab+kTab+'voxel: '+'"'+floattostrf(lAHdr.pixdim[2],ffFixed,7,5)+
' '+floattostrf(lAHdr.pixdim[3],ffFixed,7,5)+' '+floattostrf(lAHdr.pixdim[4],ffFixed,7,5)+'"');
riteln(kTab+kTab+'location: 0');
if lnSubVolumes > 1 then begin //4D volume
lCondStr := '';
for lTimePoint := 1 to l3rdDim do
lCondStr := lCondStr+'1';
riteln(kTab+kTab+'condition: '+lCondStr); //set condition for all timepoints to 1
end;
if lnSubVolumes > 1 then begin //4D volume
riteln(kTab+kTab+'name: MR '+inttostr(lHdr)+'-'+inttostr(lSubVolume-1)+' FUNCTIONAL');
end else
riteln(kTab+kTab+'name: MR '+inttostr(lHdr)+' ANATOMICAL');
if lnSubVolumes > 1 then begin
riteln(kTab+kTab+'slice_time: 0');
end;
riteln(kTab+kTab+'orientation: axial');
riteln(kTab+kTab+'birth: 07.04.75');
riteln(kTab+kTab+'sex: female');
if lnSubVolumes > 1 then begin
riteln(kTab+kTab+'MPIL_vista_0: " repition_time=2000 packed_data=1 '+inttostr(l3rdDim)+' "');
end;
riteln(kTab+kTab+'convention: natural');
riteln(kTab+'}'); //close bracket2: end image
end; //for lSubvolume... once for 2D/3D volumes, once per slice for 4D images
end; //for lHdr: each image
riteln('}'+chr(12)); //close bracket1: close header
riteln(UNIXformfeed); //end of Vista Header
closefile(lTextFile);
//end: we have now written the Lipsia/Vista text header
//next: copy binary image data
for lHdr := 1 to lnHdr do begin //for each image
lImgFileName := lAnzFileStrs[lHdr-1];//-1 because tstrings are indexed from 0
lImgFileName := changefileext(lImgFileName,'.img');
lAHdr := lAnalyzeHdrRA[lHdr];
lByteSwap := lByteSwapRA[lHdr];
AssignFile(lInFile, lImgFileName);
FileMode := 0; { Set file access to read only }
if lAHdr.Dim[4] > 1 then begin
l3rdDim := lAHdr.Dim[4]; //with Lipsia, timepoints are contiguous
lnSubVolumes := lAHdr.Dim[3];//with Lipsia, 1 image per slice for 4d images
end else begin
l3rdDim := lAHdr.Dim[3];
lnSubVolumes := 1;
end;
for lSubvolume := 1 to lnSubVolumes do begin //once for each 2D/3D image, once per slice for each 4D image
Reset(lInFile, 1);
seek(lInFile,round(lAHdr.vox_offset));
AssignFile(lOutFile, lVistaFileName);
FileMode := 2; { Set file access to read/write }
Reset(lOutFile, 1);
lFileSz := FileSize(lOutFile);
seek(lOutFile,lFileSz);
lXYTImgSz := lAHdr.Dim[1]*lAHdr.Dim[2]*l3rdDim*(lAHdr.bitpix div 8);
lXYZImgSz := lAHdr.Dim[1]*lAHdr.Dim[2]*lAHdr.Dim[3]*(lAHdr.bitpix div 8);
l2DSz := lAHdr.Dim[1]*lAHdr.Dim[2]*(lAHdr.bitpix div 8);
lLineLen := lAHdr.Dim[1]*(lAHdr.bitpix div 8);
lZSlices := l3rdDim;
lHalfLines := lAHdr.Dim[2] div 2;
GetMem( lP, lXYTImgSz );
GetMem(lLineP,lLineLen);
FileMode := 0; //set read only
if lnSubvolumes > 1 then begin //Analyze 4D data saved XYZTime, Lipsia saved XYTimeZ
GetMem(lSliceP,l2DSz);
lDataOffset := 1; //bytes between end of header and image data
for lTimePoint := 1 to l3rdDim do begin
lImageOffset := ((lSubvolume-1)*l2DSz)+((lTimePoint-1)*lXYZImgSz)+round(lAHdr.vox_offset);
//showmessage(inttostr(lImageOffset)+'@'+inttostr(lTimePoint)+'/'+inttostr(l3rdDim));
seek(lInFile,lImageOffset);
BlockRead(lInFile, lSliceP^, l2DSz);
Move(lSliceP[1],lP[lDataOffset],l2DSz);
lDataOffset := lDataOffset + l2DSz;
end; //for each
FreeMem(lSliceP);
end else //if 4D, reorder dimensions, else direct copy
BlockRead(lInFile, lP^, lXYTImgSz);
//NEXT: Vista is BIGENDIAN, so we need to byte-swap multibyte datatypes
if (not lByteSwap) and (lAHdr.datatype> 3) then begin //convert littleendian to bigendian
if (lAHdr.datatype=4) then begin //16-bit data
lVoxels := lXYTImgSz div 2;
lWordP := WordP(lP);
for lInc := 1 to lVoxels do
lWordP[lInc] := swap(lWordP[lInc]);
end else if (lAHdr.datatype=64) then begin //64-bit data
lVoxels := lXYTImgSz div 8;
lDoubleP := DoubleP(lP);
for lInc := 1 to lVoxels do
Xswap8r(lDoubleP[lInc]);
end else begin //32-bit data
lVoxels := lXYTImgSz div 4;
lLongIntP := LongIntP(lP);
for lInc := 1 to lVoxels do
swap4 (lLongIntP[lInc]);
end; //data swapping
end; //end.. little to bigendian
//NEXT: Analyze goes Bottom-to-Top, so flip line order
for lZSlice := 1 to lZSlices do begin
lLowLine := 1+(l2DSz*(lZSlice-1));
lHighLine := l2DSz-lLineLen+1+(l2DSz*(lZSlice-1));
for lLine := 1 to lHalfLines do begin
Move(lP[lLowLine],lLineP^,lLineLen);
Move(lP[lHighLine],lP[lLowLine],lLineLen);
Move(lLineP^,lP[lHighLine],lLineLen);
lLowLine := lLowLine+lLineLen;
lHighLine := lHighLine-lLineLen;
end;
end;
//Write Image Data
FileMode := 2; //set read/write
BlockWrite(lOutFile,lP^,lXYTImgSz);
end; //for subvolumes: once for 2D/3D volumes, once per slice for 4D fMRI data
close(lOutFile);
close(lInFile);
freemem(lLineP);
freemem(lP);//
end; //for lHdr to lnHdr: copy each image
end; (**)
(*procedure write_slc (lFileName: string; var pDICOMdata: DICOMdata;var lSz: integer; lDICOM3: boolean);
const kMaxRA = 41;
lXra: array [1..kMaxRA] of byte = (7,8,9,21,22,26,27,
35,36,44,45,
50,62,66,78,
81,95,
97,103,104,105,106,111,
113,123,127,
129,139,142,
146,147,148,149,155,156,157,
166,167,168,169,170);
var
fp: file;
lX,lClr,lPos,lRApos: integer;
lP: bytep;
procedure WriteString(lStr: string; lCR: boolean);
var
n,lStrLen : Integer;
begin
lStrLen := length(lStr);
for n := 1 to lstrlen do begin
lPos := lPos + 1;
lP[lPos] := ord(lStr[n]);
end;
if lCR then begin
lPos := lPos + 1;
lP[lPos] := ord(kCR);
end;
end;
begin
lSz := 0;
getmem(lP,2048);
lPos := 0;
WriteString('11111',true);
WriteString(inttostr(pDicomData.XYZdim[1])+' '+inttostr(pDicomData.XYZdim[2])+' '+inttostr(pDicomData.XYZdim[3])+' 8',true);
WriteString(floattostrf(pDicomData.XYZmm[1],ffFixed,7,7)+' '+floattostrf(pDicomData.XYZmm[2],ffFixed,7,7)+' '+floattostrf(pDicomData.XYZmm[3],ffFixed,7,7),true);
WriteString('1 1 0 0',true); //mmunits,MR,original,nocompress
WriteString('16 12 X',false); //icon is 8x8 grid, so 64 bytes for red,green blue
for lClr := 1 to 3 do begin
lRApos := 1;
for lX := 1 to 192 do begin
inc(lPos);
if (lRApos <= kMaxRA) and (lX = lXra[lRApos]) then begin
inc(lRApos);
lP[lPos] := 200;
end else
lP[lPos] := 0;
end; {icongrid 1..192}
end; {RGB}
if lFileName <> '' then begin
AssignFile(fp, lFileName);
Rewrite(fp, 1);
blockwrite(fp,lP^,lPos);
close(fp);
end;
freemem(lP);
lSz := lPos;
end;*)
procedure read_interfile_data(var lDICOMdata: DICOMdata; var lHdrOK, lImageFormatOK: boolean; var lDynStr: string;var lFileName: string);
label 333;
const UNIXeoln = chr(10);
var lTmpStr,lInStr,lUpCaseStr: string;
lHdrEnd,lFloat,lUnsigned: boolean;
lPos,lLen,FileSz,linPos: integer;
fp: file;
lCharRA: bytep;
function readInterFloat:real;
var lStr: string;
begin
lStr := '';
While (lPos <= lLen) and (lInStr[lPos] <> ';') do begin
if lInStr[lPos] in ['+','-','e','E','.','0'..'9'] then
lStr := lStr+(linStr[lPos]);
inc(lPos);
end;
try
result := Robuststrtofloat(lStr); //ACC
except
on EConvertError do begin
showmessage('Unable to convert the string '+lStr+' to a number');
result := 1;
exit;
end;
end; {except}
end;
function readInterStr:string;
var lStr: string;
begin
lStr := '';
While (lPos <= lLen) and (lInStr[lPos] = ' ') do begin
inc(lPos);
end;
While (lPos <= lLen) and (lInStr[lPos] <> ';') do begin
lStr := lStr+upcase(linStr[lPos]); //zebra upcase
inc(lPos);
end;
result := lStr;
end; //interstr func
begin
lHdrOK := false;
lFloat := false;
lUnsigned := false;
lImageFormatOK := true;
Clear_Dicom_Data(lDicomData);
lDynStr := '';
FileMode := 0; //set to readonly
AssignFile(fp, lFileName);
Reset(fp, 1);
FileSz := FileSize(fp);
lHdrEnd := false;
//lDicomData.ImageStart := FileSz;