-
Notifications
You must be signed in to change notification settings - Fork 24
/
histogram.pas
executable file
·291 lines (270 loc) · 9.82 KB
/
histogram.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
unit histogram;
interface
{$include opts.inc}
{$D-,L-,O+,Q-,R-,Y-,S-}
uses
{$IFDEF USETRANSFERTEXTURE}texture_3d_unit_transfertexture, {$ELSE} texture_3d_unit,{$ENDIF}
define_types, math,nifti_hdr, nifti_types;
procedure ComputeThreshOverlay (var lM: TMRIcroHdr);
procedure CreateHisto (var lHdr: TTexture; lMin, lMax: single; var lHisto: HistoRA; lLogarithm: boolean);
procedure CreateHistoThresh (var lHdr: TTexture; lMin, lMax: single; var lHisto: HistoRA; lLogarithm: boolean; lThreshFrac: single; var lLoPct,lHiPct: single);
procedure ComputeMinMax(var lHdr: TTexture);
function Scaled2Unscaled (lVal: single; var lHdr: TTexture): single;
function Unscaled2Scaled (lVal: single; var lHdr: TTexture): single;
implementation
uses mainunit,dialogs,sysutils;
function Unscaled2Scaled (lVal: single; var lHdr: TTexture): single;
begin
if lHdr.NIFTIhdr.scl_slope = 0 then
result := lVal + lHdr.NIFTIhdr.scl_inter
else
result := (lVal * lHdr.NIFTIhdr.scl_slope) + lHdr.NIFTIhdr.scl_inter;
end;
function Scaled2Unscaled (lVal: single; var lHdr: TTexture): single;
begin
if lHdr.NIFTIhdr.scl_slope = 0 then begin
result := lVal-lHdr.NIFTIhdr.scl_inter;
exit;
end;
result := (lVal-lHdr.NIFTIhdr.scl_inter) / lHdr.NIFTIhdr.scl_slope ;
end;
function M2T (var lM: TMRIcroHdr; var lT : TTexture): boolean;
var
i: integer;
begin
result := false;
if lM.ImgBuffer = nil then
exit;
for i := 1 to 3 do
lT.FiltDim[i] := lM.NIFTIhdr.Dim[i];
lT.NIFTIhdr.scl_inter := lM.NIFTIhdr.scl_inter;
lT.NIFTIhdr.scl_slope := lM.NIFTIhdr.scl_slope;
lT.RawUnscaledImg8 := nil;
lT.RawUnscaledImg16 := nil;
lT.RawUnscaledImg32 := nil;
case lM.NIFTIhdr.datatype of
kDT_UNSIGNED_CHAR : lT.RawUnscaledImg8 := @lM.ImgBuffer^;
kDT_SIGNED_SHORT: lT.RawUnscaledImg16 := SmallIntP0(@lM.ImgBuffer^ );
kDT_SIGNED_INT: msg('Help: 32bit int not supported');//l32i := LongIntP(@lDestHdr.ImgBuffer^);
kDT_FLOAT: lT.RawUnscaledImg32 := SingleP0(@lM.ImgBuffer^ );
end; //case
result := true;
end;
procedure ComputeThreshOverlay (var lM: TMRIcroHdr);
var
lT: TTexture;
lLog10: integer;
lMinPreRound: single;
begin
if not M2T (lM,lT) then
exit;
ComputeMinMax(lT);
CreateHistoThresh(lT, lT.WindowScaledMin, lT.WindowScaledMax, lT.UnscaledHisto, true, 0.005,lT.MinThreshScaled,lT.MaxThreshScaled);
//lT.MaxThreshScaled := 1;
//lT.MinThreshScaled := 0; //xxxxxxx
lLog10 := trunc(log10( lT.MaxThreshScaled-lT.MinThreshScaled))-1;
lMinPreRound := (lM.WindowScaledMin);
lM.WindowScaledMin := roundto(lT.MinThreshScaled,lLog10);
if (lMinPreRound > 0) and (lM.WindowScaledMin = 0) then
lM.WindowScaledMin := power(10,lLog10);
lM.WindowScaledMax := roundto(lT.MaxThreshScaled,lLog10);
if (lM.WindowScaledMax > 0.1) and (lM.WindowScaledMin = 0) then
lM.WindowScaledMin := lM.WindowScaledMax * 0.01;
lM.AutoBalMinUnscaled := lM.WindowScaledMin;
lM.AutoBalMaxUnscaled := lM.WindowScaledMax;
end;
procedure ComputeMinMax(var lHdr: TTexture);
var
lMxs,lMns,lVs: single;
lMxi,lMni,lVi,lC,lImgBufferItems: integer;
begin
lImgBufferItems := lHdr.FiltDim[1]*lHdr.FiltDim[2]*lHdr.FiltDim[3];
if lImgBufferItems < 1 then
exit;
if (lHdr.RawUnscaledImg8 = nil) and (lHdr.RawUnscaledImg16 =nil) and (lHdr.RawUnscaledImg32 = nil) then begin
//RGB data
lHdr.WindowScaledMax := 255;
lHdr.WindowScaledMin := 0;
lHdr.WindowScaledMax := Unscaled2Scaled(lHdr.WindowScaledMax,lHdr);
lHdr.WindowScaledMin := Unscaled2Scaled(lHdr.WindowScaledMin,lHdr);
exit;
end;
if (lHdr.RawUnscaledImg32 <> nil) then begin //32bit
lMxs := lHdr.RawUnscaledImg32^[0];
lMns := lHdr.RawUnscaledImg32^[0];
for lC := 0 to (lImgBufferItems-1) do begin
lVs := lHdr.RawUnscaledImg32^[lC];
if lVs < lMns then
lMns := lVs;
if lVs > lMxs then
lMxs := lVs;
end;
lHdr.WindowScaledMax := lMxs;
lHdr.WindowScaledMin := lMns;
end else begin //8 or 16bit integers
lMxi := 255; //removes compiler warning...
lMni := 0; //removes compiler warning...
if (lHdr.RawUnscaledImg16 <> nil ) then begin //if 16bit ints
lMxi := lHdr.RawUnscaledImg16^[0];
lMni := lHdr.RawUnscaledImg16^[0];
for lC := 0 to (lImgBufferItems-1) do begin
lVi := lHdr.RawUnscaledImg16^[lC];
if lVi < lMni then
lMni := lVi;
if lVi > lMxi then
lMxi := lVi;
end;
end else if (lHdr.RawUnscaledImg8 <> nil) then begin//else 8 bit data
lMxi := lHdr.RawUnscaledImg8^[0];
lMni := lHdr.RawUnscaledImg8^[0];
for lC := 0 to (lImgBufferItems-1) do begin
lVi := lHdr.RawUnscaledImg8^[lC];
if lVi < lMni then
lMni := lVi;
if lVi > lMxi then
lMxi := lVi;
end;
end;//8-bit
lHdr.WindowScaledMax := lMxi;
lHdr.WindowScaledMin := lMni;
end; //not 32bit
lHdr.WindowScaledMax := Unscaled2Scaled(lHdr.WindowScaledMax,lHdr);
lHdr.WindowScaledMin := Unscaled2Scaled(lHdr.WindowScaledMin,lHdr);
end;
{$define FX9}
procedure CreateHistoThresh (var lHdr: TTexture; lMin, lMax: single; var lHisto: HistoRA; lLogarithm: boolean; lThreshFrac: single; var lLoPct,lHiPct: single);
var
lModShl10,lMinIx10,lC,lImgBufferItems,lV,lNum, lNumHi: integer;
lHisto2,lHisto8bit: HistoRA;
lByte: byte;
lThreshVox,lMinU,lMaxU,lMod,lRng,lVs: single;
lRGBdata: boolean;
begin
lImgBufferItems := lHdr.FiltDim[1]*lHdr.FiltDim[2]*lHdr.FiltDim[3];
if lImgBufferItems < 1 then
exit;
lRGBdata := false;
if (lHdr.RawUnscaledImg8 = nil) and (lHdr.RawUnscaledImg16 =nil) and (lHdr.RawUnscaledImg32 = nil) and (lHdr.RawUnscaledImgRGBA = nil) then exit;
for lC := 0 to kHistoBins do
lHisto[lC] := 0;
lMinU := Scaled2Unscaled(lMin,lHdr);
lMaxU := Scaled2Unscaled(lMax,lHdr);
if lMin > lMax then begin
lVs := lMaxU;
lMaxU := lMinU;
lMinU := lVs;
end;
lRng := lMaxU - lMinU;
if (lHdr.RawUnscaledImg32 <> nil ) then begin //32bit
if lRng > 0 then
lMod := kHistoBins/lRng
else
lMod := 0;
for lC := 0 to (lImgBufferItems-1) do begin
lVs := lHdr.RawUnscaledImg32^[lC];
if (lVs >= lMinU) and (lVs <= lMaxU) then begin
lByte := round((lVs-lMinU)*lMod);
inc(lHisto[lByte]);
end;
end;
end else begin //not 32-bit : 8, 16, 24 bit data
if lRng > 0 then
lMod := (kHistoBins)/lRng
//lMod := lRng/(kHistoBins)
else
lMod := 0;
lModShl10 := trunc(lMod * 1024);
lMinIx10 := round(lMinU*lModShl10);
if (lHdr.RawUnscaledImg16 <> nil ) then begin //if 16bit ints
for lC := 1 to (lImgBufferItems-1) do begin
lV := lHdr.RawUnscaledImg16^[lC];
if (lV >= lMinU) and (lV <= lMaxU) then begin
lByte := ((lV*lModShl10)-lMinIx10) shr 10;
inc(lHisto[lByte]);
end;
end;
end else if (lHdr.RawUnscaledImg8 <> nil) then begin//else 8 bit data
{$IFDEF FX9}
//use lookup table to accelerate...
for lC := 0 to 255 do begin
if (lC >= lMinU) and (lC <= lMaxU) then begin
lByte := ((lC*lModShl10)-lMinIx10) shr 10;
lHisto8bit[lC] := lByte;
end else
lHisto8bit[lC] := kHistoBins;
end;//for each voxel
//CLUTform.Caption := floattostr(lMinU)+' '+floattostr(lmaxU)+' 1 = '+inttostr(lMinIx10);
for lC := 1 to (lImgBufferItems-1) do
inc(lHisto[lHisto8Bit[lHdr.RawUnscaledImg8^[lC]]]);
lHisto[kHistoBins] := 0;
{$ELSE}
for lC := 1 to (lImgBufferItems-1) do begin
lV := lHdr.RawUnscaledImg8^[lC];
if (lV >= lMinU) and (lV <= lMaxU) then begin
lByte := ((lC*lModShl10)-lMinIx10) shr 10;
inc(lHisto[lByte]);
end;
end;//for each voxel
{$ENDIF}
end else if (lHdr.RawUnscaledImgRGBA<> nil) then begin//else rgba
lRGBdata := true;
lNum := 1; //read green
for lC := 1 to (lImgBufferItems-1) do begin
lV := lHdr.RawUnscaledImgRGBA^[lNum];
lNum := lNum + 3;
if (lV >= lMinU) and (lV <= lMaxU) then begin
lByte := ((lV*lModShl10)-lMinIx10 )shr 10;
inc(lHisto[lByte]);
end;
end;//for each voxel
end;//24-bit
end; //not 32bit
//next compute thresholds...
lThreshVox := lImgBufferItems * lThreshFrac;
lLoPct := 0;
lHiPct := 255;
if (lThreshVox > 0) and (lThreshVox < lImgBufferItems) and (not lRGBdata) then begin
//count down
lNum := 0;
lC := kHistoBins;
lNumHi := kHistoBins;
repeat
lNum := lNum + lHisto[lC];
if (lHisto[lC] > 0) then lNumHi := lC;
dec(lC);
until (lC = 0) or (lNum >= lThreshVox);
if lC = 0 then
lC := lNumHi;
if (lC < (kHistoBins shr 1)) then
lC := (kHistoBins shr 1);
lHiPct := ((lC/kHistoBins*(lMaxU-lMinU))+lMinU);
//count up
lNum := 0;
lC := 0;
repeat
lNum := lNum + lHisto[lC];
inc(lC);
until (lC >= kHistoBins) or (lNum >= lThreshVox);
lLoPct := ((lC/kHistoBIns*(lMaxU-lMinU))+lMinU);
//showmessage(inttostr(lNumHi)+'xxxx '+floattostr(lLoPct)+'>>>'+floattostr(lHiPct) );
end; //if threshvox > 0 --- compute thresholded range
lLoPct := Unscaled2Scaled(lLoPct,lHdr);
lHiPct := Unscaled2Scaled(lHiPct,lHdr);
//next smooth
if true then begin
for lC := 1 to (kHistoBins-1) do
lHisto2[lC]:= (lHisto[lC-1]+lHisto[lC]+lHisto[lC+1]) div 3;
for lC := 1 to (kHistoBins-1) do
lHisto[lC]:= lHisto2[lC];
end;
if lLogarithm then
for lC := 0 to kHistoBins do
lHisto[lC] := round(10*log2(lHisto[lC]));
end; //CreateHistoThresh
procedure CreateHisto (var lHdr: TTexture; lMin, lMax: single; var lHisto: HistoRA; lLogarithm: boolean);
var
l1,l2: single;
begin
CreateHistoThresh (lHdr,lMin, lMax, lHisto, lLogarithm, 0, l1,l2);
end;
end.