forked from neurolabusc/MRIcroGL10_OLD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavethreshold.pas
executable file
·250 lines (230 loc) · 7.84 KB
/
savethreshold.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
unit savethreshold;
{$IFDEF FPC}{$mode delphi} {$ENDIF}
{$H+}
interface
uses
Classes, SysUtils, Dialogs, define_types, nifti_hdr, scaleimageintensity, clustering, nifti_types;
//function SaveImg (lOutname: string; var lHdrx: TMRIcroHdr): boolean;
function SaveImg (lOutname: string; var lHdr: TNIFTIhdr; lImg: Bytep): boolean;
function SaveThresholdedUI(lThresh, lClusterMM3: single; lSaveToDisk: boolean): boolean;
function SaveThresholded(lInname: string; lThresh, lClusterMM3: single; lSaveToDisk: boolean): integer;
implementation
uses
{$IFNDEF FPC}
gziod,
{$ELSE}
gzio2,
{$ENDIF}
mainunit;
procedure ThreshImgIntensity8(var lHdr: TMRIcroHdr; lThresh: single);
var
lInc,lItems: integer;
l8Buf : ByteP;
lThreshSI: smallint;
lThreshSS: single;
begin
lItems := lHdr.ImgBufferItems ;
if (lHdr.ImgBufferBPP <> 2) or (lItems < 2) then exit;
l8Buf := ByteP(lHdr.ImgBuffer );
lThreshSS := Scaled2RawIntensity (lHdr, lThresh);
if lThreshSS < 0 then lThreshSS := 0;
if lThreshSS > 255 then lThreshSS := 255;
lThreshSI := round(lThreshSS);
if lThresh < 0 then begin
for lInc := 1 to lItems do
if l8Buf^[lInc] > lThreshSI then
l8Buf^[lInc] := 0;
end else begin
for lInc := 1 to lItems do
if l8Buf^[lInc] < lThreshSI then
l8Buf^[lInc] := 0;
end
end;
procedure ThreshImgIntensity16(var lHdr: TMRIcroHdr; lThresh: single);
var
lInc,lItems: integer;
l16Buf : SmallIntP;
lThreshSI: smallint;
lThreshSS: single;
begin
lItems := lHdr.ImgBufferItems ;
if (lHdr.ImgBufferBPP <> 2) or (lItems < 2) then exit;
l16Buf := SmallIntP(lHdr.ImgBuffer );
lThreshSS := Scaled2RawIntensity (lHdr, lThresh);
if lThreshSS < -32768 then lThreshSS := -32768;
if lThreshSS > 32767 then lThreshSS := 32767;
lThreshSI := round(lThreshSS);
if lThresh < 0 then begin
for lInc := 1 to lItems do
if l16Buf^[lInc] > lThreshSI then
l16Buf^[lInc] := 0;
end else begin
for lInc := 1 to lItems do
if l16Buf^[lInc] < lThreshSI then
l16Buf^[lInc] := 0;
end
end;
procedure ThreshImgIntensity32(var lHdr: TMRIcroHdr; lThresh: single);
var
lInc,lItems: integer;
l32Buf : SingleP;
begin
lItems := lHdr.ImgBufferItems ;
if (lHdr.ImgBufferBPP <> 4) or (lItems< 2) then exit;
l32Buf := SingleP(lHdr.ImgBuffer );
if lThresh < 0 then begin
for lInc := 1 to lItems do
if l32Buf^[lInc] > lThresh then
l32Buf^[lInc] := 0;
end else begin
for lInc := 1 to lItems do
if l32Buf^[lInc] < lThresh then
l32Buf^[lInc] := 0;
end
end;
procedure ApplyClustering(var lHdr: TMRIcroHdr; lClusterVoxels: integer);
var
i, lVolVox: integer;
l8Buf : ByteP;
l16Buf : SmallIntP;
l32Buf : SingleP;
begin
lVolVox := lHdr.ImgBufferItems;
if (lVolVox < 1) or (lClusterVoxels < 2) then exit;
for i := 1 to lVolVox do
lHdr.ScrnBuffer^[i] := 0;
if (lHdr.ImgBufferBPP = 4) then begin
l32Buf := SingleP(lHdr.ImgBuffer );
for i := 1 to lVolVox do
if l32Buf[i] <> 0 then
lHdr.ScrnBuffer^[i] := 1;
ClusterScrnImg (lHdr, lClusterVoxels);
for i := 1 to lVolVox do
if lHdr.ScrnBuffer^[i] = 0 then
l32Buf[i] := 0;
end else if (lHdr.ImgBufferBPP = 2) then begin
l16Buf := SmallIntP(lHdr.ImgBuffer );
for i := 1 to lVolVox do
if l16Buf[i] <> 0 then
lHdr.ScrnBuffer^[i] := 1;
ClusterScrnImg (lHdr, lClusterVoxels);
for i := 1 to lVolVox do
if lHdr.ScrnBuffer^[i] = 0 then
l16Buf[i] := 0;
end else if lHdr.ImgBufferBPP = 1 then begin
l8Buf := ByteP(lHdr.ImgBuffer );
for i := 1 to lVolVox do
if l8Buf[i] <> 0 then
lHdr.ScrnBuffer^[i] := 1;
ClusterScrnImg (lHdr, lClusterVoxels);
for i := 1 to lVolVox do
if lHdr.ScrnBuffer^[i] = 0 then
l8Buf[i] := 0;
end else
exit;
end;
//function SaveImg (lOutname: string; var lHdrx: TMRIcroHdr): boolean;
function SaveImg (lOutname: string; var lHdr: TNIFTIhdr; lImg: Bytep): boolean;
var
i : integer;
outbytes : int64;
//lOutname,
lOutnameGz : string;
lF: File;
h: TNIFTIHdr;
doGz: boolean;
begin
doGz := ExtGZ(lOutname);
result := false;
//lOutname := ChangeFilePrefixExt (lInName,'r','.nii');
if fileexists(lOutname) then begin
showmessage('Error: file already exists '+lOutname);
exit;
end;
//loutnameGz := lOutname +'.gz';
if doGz then begin
lOutnameGz := lOutname;
lOutname := ChangeFileExtX(lOutname,'.tmp');
if fileexists(lOutname) then begin
showmessage('Error: file already exists '+lOutname);
exit;
end;
end;
h := lHdr;
outbytes := 1;
for i := 1 to 7 do
if h.dim[i] > 0 then
outbytes := outbytes * h.dim[i];
if (outbytes < 2) then exit;
h.magic := kNIFTI_MAGIC_EMBEDDED_HDR; //save as .nii not hdr/img
h.vox_offset:= 352;
outbytes := outbytes * (h.bitpix div 8);// lHdrx.ImgBufferBPP;
Filemode := 1;
AssignFile(lF, lOutname);
Rewrite(lF,1);
BlockWrite(lF,h,sizeof(TNIFTIHdr) );
i := 0;
BlockWrite(lF,i, 4 ); //NIFTI .nii requires 4 byte pad 348->352
BlockWrite(lF,lImg^, outbytes); //lHdrx.ImgBuffer^,outbytes);
CloseFile(lF);
Filemode := 2;
if doGz then GZipFile(lOutname, lOutnameGz, true);
//showmessage(lOutname +' -> '+lOutnameGz);
result := true;
end;
function SaveThresholded(lInname: string; lThresh, lClusterMM3: single; lSaveToDisk: boolean): integer;
var
i, lClusterVox: integer;
loutmm3: single;
lOpt : TOpenOptions;
lInterp: boolean;
begin
result := -1; //assume failure
loutmm3 := abs(gTexture3D.NIFTIhdr.pixdim[1]*gTexture3D.NIFTIhdr.pixdim[2]*gTexture3D.NIFTIhdr.pixdim[3]);
if (loutmm3 = 0 ) then
lClusterVox := 0
else
lClusterVox := round (abs(lClusterMM3)/ loutmm3);
lOpt := GLForm1.OpenDialog1.Options;
lInterp := gPrefs.InterpolateOverlays;
gPrefs.InterpolateOverlays := true;
i := GLForm1.AddOverlay(lInname,1);
GLForm1.OpenDialog1.Options := lOpt;
gPrefs.InterpolateOverlays := lInterp;
if (i < 1) then
exit;
if (gOverlayImg[i].ImgBufferBPP = 4) then
ThreshImgIntensity32(gOverlayImg[i], lThresh)
else if (gOverlayImg[i].ImgBufferBPP = 2) then
ThreshImgIntensity16(gOverlayImg[i], lThresh)
else if gOverlayImg[i].ImgBufferBPP = 1 then
ThreshImgIntensity8(gOverlayImg[i], lThresh)
else
exit;
ApplyClustering (gOverlayImg[i], lClusterVox);
if lSaveToDisk then
//SaveImg (ChangeFilePrefixExt (lInName,'r','.nii.gz'), gOverlayImg[i]);
SaveImg (ChangeFilePrefixExt (lInName,'r','.nii.gz'), gOverlayImg[i].NIFTIhdr, gOverlayImg[i].ImgBuffer);
result := i;
GLForm1.UpdateImageIntensityMinMax (i, lThresh, lThresh);
end;
function SaveThresholdedUI (lThresh, lClusterMM3: single; lSaveToDisk: boolean): boolean;
var
lF, lIndex: integer;
begin
result := false;
lIndex := 0;
if (gTexture3D.FiltDim[1] < 1) or (gTexture3D.FiltDim[2] < 1) or (gTexture3D.FiltDim[3] < 1) then begin
showmessage('Please load a background image before loading an overlay.');
exit;
end;
GLForm1.OpenDialog1.Options := [ofAllowMultiSelect,ofFileMustExist];
if (not GLForm1.OpenDialog1.Execute) or (GLForm1.OpenDialog1.Files.Count < 1) then
exit;
for lF := 0 to (GLForm1.OpenDialog1.Files.Count-1) do begin
GLForm1.Closeoverlays1Click(nil);
lIndex := SaveThresholded(GLForm1.OpenDialog1.Files[lF], lThresh, lClusterMM3, lSaveToDisk);
end;
result := lIndex > 0;
end;
end.