File: autoroi.pas

package info (click to toggle)
mricron 0.20140804.1~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,508 kB
  • sloc: pascal: 114,876; sh: 49; makefile: 35
file content (601 lines) | stat: -rwxr-xr-x 21,129 bytes parent folder | download | duplicates (4)
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
unit autoroi;

interface

uses
 {$IFNDEF FPC}
 RXSpin,capmenu,
 {$ELSE}
 Spin,lResources,
 {$ENDIF}
 {$IFNDEF Unix} Windows,{$ENDIF}
 SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, define_types, ExtCtrls, nifti_img,nifti_img_view;

type

  { TAutoROIForm }

  TAutoROIForm = class(TForm)
    OriginLabel: TLabel;
    OriginBtn: TSpeedButton;
    EdgeEdit: TSpinEdit;
    RadiusEdit: TSpinEdit;
    ErodeEdit: TSpinEdit;
    ROIconstraint: TComboBox;
    VarianceEdit: TSpinEdit;
    DiffLabel: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    AutoROIBtn: TSpeedButton;
    CancelBtn: TSpeedButton;
    Timer1: TTimer;
    Label4: TLabel;
    ExcludeBlackCheck: TCheckBox;
	procedure OriginBtnClick(Sender: TObject);
	procedure PreviewBtnClick(Sender: TObject);
	procedure FormShow(Sender: TObject);
	procedure FormCreate(Sender: TObject);
	procedure FormHide(Sender: TObject);
	procedure AutoROIBtnClick(Sender: TObject);
	procedure CancelBtnClick(Sender: TObject);
	procedure AutoROIchange(Sender: TObject);
	procedure Timer1Timer(Sender: TObject);
	procedure FormDestroy(Sender: TObject);
  private
	{ Private declarations }
  public
	{ Public declarations }
  end;

procedure ROICluster ({lInROIBuf: bytep;} lXdim, lYDim, lZDim,lXOriginIn,lYOrigin,lZOrigin: integer; lDeleteNotFill: boolean);
var
  AutoROIForm: TAutoROIForm;
  gOriginX,gOriginY,gOriginZ: integer;
implementation

 {$IFNDEF FPC}
 {$R *.DFM}
 {$ENDIF}


procedure TAutoROIForm.OriginBtnClick(Sender: TObject);
begin
 gOriginX := ImgForm.XViewEdit.value;
 gOriginY := ImgForm.YViewEdit.value;
 gOriginZ := ImgForm.ZViewEdit.value;
 OriginLabel.Caption := 'Origin: '+inttostr(gOriginX)+'x'+inttostr(gOriginY)+'x'+inttostr(gOriginZ);
 PreviewBtnClick(sender);
end;

procedure TAutoROIForm.PreviewBtnClick(Sender: TObject);
var
   lXmm,lYmm,lZmm,lSqrRadius: single;
   lExcludeBlackIfZero,//lX,lY,lZ, //abba
   {lMaxROISz,}lEdge,lOriginPos,lROISz,lOriginIntensity,lVariance,lXdim, lYDim, lZDim: integer;
   lErodeCycles,lQTail,lQHead,lSliceSz,lQSz,lInc,lVolSz{,lX,lY,lZ}: integer;
   lROIConstrain,lReadFilteredData: boolean;
   lQra: LongIntP;
   lSourceBuffer,lBuff,lPreErodeBuff: ByteP;
const
	 kFillValue = -2;
Procedure IncQra(var lVal, lQSz: integer);
begin
	inc(lVal);
	if lVal >= lQSz then
	 lVal := 1;
end;
function UnsmoothedIntensity(lPixel: integer): integer; //1381
begin
	  if lReadFilteredData then
		 result := lBuff^[lPixel]
	  else
		  Result :=lSourceBuffer^[lPixel];
end;

 function MeanIntensity(lPixel: integer): integer;
 var lV: integer;
 begin
	  if lReadFilteredData then
		 result := lBuff^[lPixel]
	  else if ((lPixel-lSliceSz) > 0) and ((lPixel+lSliceSz) <= lVolSz) then begin
		 lV :=lSourceBuffer^[lPixel]+lSourceBuffer^[lPixel+1]+lSourceBuffer^[lPixel-1] //L/R
			+lSourceBuffer^[lPixel+lXdim]+lSourceBuffer^[lPixel-lXdim] //Anterior/Posterior
			+lSourceBuffer^[lPixel+lSliceSz]+lSourceBuffer^[lPixel-lSliceSz]; //Dorsal/Ventral
		 result := lV div 7;
	  end else result := lSourceBuffer^[lPixel];//1401 gImageBackupBuffer[lPixel]
 end;
 procedure Check(lPixel,lIntensity: integer);
 var lSmoothInten :integer;
 begin
   lSmoothInten := MeanIntensity(lPixel);
   if (lROIConstrain) and (gBGImg.VOIUndoVol^[lPixel] > 0) then //1410
	 //constrain
   else if (lBuff^[lPixel]<> 255) and (UnsmoothedIntensity(lPixel) > lExcludeBlackIfZero {1381}) and  (abs(lSmoothInten-lIntensity)<=lEdge) and(abs(lSmoothInten-lOriginIntensity)<=lVariance) {}then begin//add item
		incQra(lQHead,lQSz);
		inc(lROISz);
		lBuff^[lPixel] := 255;
		lQra^[lQHead] := lPixel;
   end;
 end;

PROCEDURE RetirePixel; //FIFO cleanup
function WithinRadius(lXs,lYs,lZs:integer): boolean;
begin
	 if (sqr((lXs-gOriginX)*lXmm)+sqr((lYs-gOriginY)*lYmm)+sqr((lZs-gOriginZ)*lZmm)) > lSqrRadius then
		result := false
	 else
		 result := true;
end;
VAR
   lVal,lXPos,lYPos,lZPos,lIntensity: integer;
BEGIN
   lVal := lQra^[lQTail];
   lXpos := lVal mod lXdim;
   if lXpos = 0 then lXPos := lXdim;

   lYpos := (1+((lVal-1) div lXdim)) mod lYDim;
   if lYPos = 0 then lYPos := lYdim;

   lZpos := ((lVal-1) div lSliceSz)+1;
   if lReadFilteredData then
	  lIntensity := 128
   else
	   lIntensity := lSourceBuffer^[lVal];//1401 gImageBackupBuffer[lVal];
   if (lXpos > 1) and WithinRadius(lXpos-1,lYpos,lZpos) then Check(lVal -1,lIntensity);//check to left
   if (lXPos < lXDim) and (WithinRadius(lXpos+1,lYpos,lZpos)) then Check(lVal + 1,lIntensity); //check to right
   if (lYpos > 1) and (WithinRadius(lXpos,lYpos-1,lZpos)) then Check(lVal -lXdim,lIntensity);//check previous line
   if (lYPos < lYDim) and (WithinRadius(lXpos,lYpos+1,lZpos)) then Check(lVal + lXdim,lIntensity); //check next line
   if (lZpos > 1) and (WithinRadius(lXpos,lYpos,lZpos-1)) then Check(lVal -lSliceSz,lIntensity);//check previous slice
   if (lZPos < lZDim) and (WithinRadius(lXpos,lYpos,lZpos+1)) then Check(lVal + lSliceSz,lIntensity); //check next slice
   incQra(lQTail,lQSz); //done with this pixel
END;

procedure FillStart (lPt: integer); {FIFO algorithm: keep memory VERY low}
var lI: integer;
begin
  for lI := 1 to lQsz do
	  lQra^[lI] := 0;
  lQHead := 0;
  lQTail := 1;
  lROISz := 0;
  Check(lPt,lOriginIntensity);
  RetirePixel;
  while ((lQHead+1) <> lQTail) do begin//complete until all voxels in buffer have been tested
		RetirePixel;
		if (lQHead = lQSz) and (lQTail = 1) then
		   exit; //break condition: avoids possible infinite loop where QTail is being incremented but QHead is stuck at maximum value
  end;
end;

function ROIOnEdge (lVal: integer): boolean;
BEGIN
   result := false;
   if lBuff^[lVal] <> 255 then exit; //not ROI - is not boundary
   //Find
   if ((lVal-lSliceSz) > 0) and ((lVal+lSliceSz) <= lVolSz) then begin
			if lBuff^[lVal+1] = 0 then result := true;
			if lBuff^[lVal-1] = 0 then result := true;
			if lBuff^[lVal+lXdim] = 0 then result := true;
			if lBuff^[lVal-lXdim] = 0 then result := true;
			if lBuff^[lVal+lSliceSz] = 0 then result := true;
			if lBuff^[lVal-lSliceSz] = 0 then result := true;
   end;
end;

function ZeroOnEdge (lVal: integer): boolean;
BEGIN
   result := false;
   if lBuff^[lVal] <> 0 then exit; //not ROI - is not boundary
   //Find
   if ((lVal-lSliceSz) > 0) and ((lVal+lSliceSz) <= lVolSz) then begin
			if lBuff^[lVal+1] = 255 then result := true;
			if lBuff^[lVal-1] = 255 then result := true;
			if lBuff^[lVal+lXdim] = 255 then result := true;
			if lBuff^[lVal-lXdim] = 255 then result := true;
			if lBuff^[lVal+lSliceSz] = 255 then result := true;
			if lBuff^[lVal-lSliceSz] = 255 then result := true;
   end;
end;

begin                                                       //alfa666
	 if (gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems<1) or (gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems<>gBGImg.VOIUndoVolItems) then exit;
	 //if gImageBackupSz <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
	 //UpdateBackupBuffer;
	 lXdim := gBGImg.ScrnDim[1];
	 lYDim := gBGImg.ScrnDim[2];
	 lZDim := gBGImg.ScrnDim[3];
	 if (gBGImg.Scrnmm[1] = 0) or (gBGImg.Scrnmm[2]=0) or (gBGImg.Scrnmm[3]=0) then begin
		 lXmm := 1;
		 lYmm := 1;
		 lZmm := 1;
	 end else begin
		 lXmm := gBGImg.Scrnmm[1];
		 lYmm := gBGImg.Scrnmm[2];
		 lZmm := gBGImg.Scrnmm[3];
	 end;
	 lSliceSz := lXdim * lYdim;
	 lVolSz := lSliceSz*lZdim;
	 //lMaxROISz := round(PctImg.Value/100 * lVolSz);
	 lOriginPos := gOriginX + ((gOriginY-1)*lXdim) + ((gOriginZ-1)*lSliceSz);
	 if (lOriginPos < 1) or (lVolSz <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems) or (lOriginPos > lVolSz) or (gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems <> gBGImg.VOIUndoVolItems) then
		exit;
         {$IFNDEF FPC}
	 lVariance := AutoROIForm.VarianceEdit.asinteger; //asinteger;
	 lEdge := AutoROIForm.EdgeEdit.asinteger;
         lSqrRadius := sqr(AutoROIForm.RadiusEdit.asinteger);
         {$ELSE}
	 lVariance := AutoROIForm.VarianceEdit.value; //asinteger;
	 lEdge := AutoROIForm.EdgeEdit.value;
         lSqrRadius := sqr(AutoROIForm.RadiusEdit.value);
         {$ENDIF}
	 if (lXDim < 4) or (lYDim < 4) or (lZDim < 4) or (lVolSz < 1)  then exit;
	 lSourceBuffer := gMRIcroOverlay[kBGOverlayNum].ScrnBuffer;//gBuffer;
	 //Next - START count cluster size
	 lQSz := (lVolSz div 4)+8;
	 GetMem(lQra,lQsz * sizeof(longint) );
	 //check positive clusters....
	 Getmem(lBuff,lVolSz);
	 FillChar(lBuff^,lVolSz, 0);
	 //Move(gImageBackupBuffer^,lBuff^,lVolSz);
	 if ExcludeBlackCheck.checked then //1381
	   lExcludeBlackIfZero := 0 //0
	 else
		 lExcludeBlackIfZero := -1;//impossible 8-bit value: do not use this feature
	 lOriginIntensity := lSourceBuffer^[lOriginPos]; //1401 gImageBackupBuffer[lOriginPos];
	 lReadFilteredData := false;
	 //ROIconstrainCheck.enabled := (gROIBupSz > 1); //1410: next 3 lines
	 ROIconstraint.enabled := (gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems > 1); //1410: next 3 lines
	 if (ROIconstraint.ItemIndex = 2) and (ROIconstraint.enabled) then
		lROIConstrain := true
	 else
		 lROIconstrain := false;
	 FillStart(lOriginPos);
	 lROIConstrain := false;//1410
	 //START: ERODE/DILATE CYCLES
{$IFNDEF FPC}
	 lErodeCycles :=  AutoROIForm.ErodeEdit.asinteger;
{$ELSE}
	 lErodeCycles :=  AutoROIForm.ErodeEdit.value;
{$ENDIF}
	 if lErodeCycles > 0 then begin
		Getmem(lPreErodeBuff,lVolSz);
		Move(lBuff^,lPreErodeBuff^,lVolSz);
		for lQHead := 1 to lErodeCycles do begin//ERODE
			for lInc := 1 to lVolSz do
				if ROIonEdge(lInc) then
				   lBuff^[lInc] :=254;
			for lInc := 1 to lVolSz do
				if lBuff^[lInc]=254 then
				   lBuff^[lInc] := 0; //erode
		end;//for ErodeCycles = ERODE
		//SET ALL VOXELS THAT HAVE SURVIVED EROSION TO 128, WE THEN GROW THE ORIGIN
		for lInc := 1 to lVolSz do
			if lBuff^[lInc] =255 then lBuff^[lInc] := 128;
		//NOW - ONLY PRESERVE STUFF CONNECTED TO ORIGIN
		lBuff^[lOriginPos] := 128;
		lOriginIntensity := 128;
		lVariance := 2;
		lEdge := 2;
		lReadFilteredData := true;
		FillStart(lOriginPos);
		//SWITCH OFF ALL UNCONNECTED BLOBS
		for lInc := 1 to lVolSz do
			if lBuff^[lInc] =128 then lBuff^[lInc] := 0;
		//for lInc := 1 to lVolSz do
		//    if lBuff[lInc] > 0 then showmessage(inttostr(lBuff[lInc]));// := 0;

		for lQHead := 1 to lErodeCycles do begin//DILATE
			for lInc := 1 to lVolSz do
				if (lPreErodeBuff^[lInc] = 255) and (ZeroonEdge(lInc)) then
				   lBuff^[lInc] :=254;
			for lInc := 1 to lVolSz do
				if lBuff^[lInc]=254 then
				   lBuff^[lInc] := 255; //erode
		end;//for ErodeCycles = DILATE
	  Freemem(lPreErodeBuff);
	  {}
	 end; //ERODE cycles > 0
	 //END: ERODE/DILATE
	 Freemem(lQra);
	 ROIconstraint.enabled := (gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems > 1); //1410: next 3 lines
	 if (ROIconstraint.ItemIndex = 1) and (ROIconstraint.enabled) then begin //delete ROI
		for lInc := 1 to gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems do //gROIBup
			if (lBuff^[lInc] = 255) then
			   lBuff^[lInc] := 0
			else
			   lBuff^[lInc] := gBGImg.VOIUndoVol^[lInc];
	 end else (*if true {alfa (gDynSz > 1) and (gROIBupsz > 1) {and (gImageBackupSz = gDynSz){} then begin
		for lInc := 1 to gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems do
			if lBuff[lInc] = 255 then
			else if gImageBackupBuffer[lInc] = 255 then
				 lBuff[lInc] := 255//255;
			else lBuff[lInc] := lSourceBuffer[lInc];

	 end else *)
	   for lInc := 1 to lVolSz do
		 if lBuff^[lInc] <> 255 then
			lBuff^[lInc] := gBGImg.VOIUndoVol^[lInc]
		 else
			lBuff^[lInc] := kVOI8bit;//1401 gImageBackupBuffer[lInc];
	 Move(lBuff^,gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^,lVolSz);
	 Freemem(lBuff);
	 //END check clusters
	 ImgForm.RefreshImagesTimer.Enabled := true;
end;

procedure TAutoROIForm.FormShow(Sender: TObject);
begin
EnsureVOIOpen;
CreateUndoVol;
	AutoROIForm.ModalResult := mrCancel;
	ROIconstraint.Enabled := (gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems > 1);
	OriginBtn.OnClick(sender);
	 //DeleteCheck.enabled := (gROIBupSz > 1);
	 //ROIConstrainCheck.enabled := (gROIBupSz > 1);
end;

procedure TAutoROIForm.FormCreate(Sender: TObject);
begin
 {$IFNDEF FPC}
	 ROIconstraint.SetItemIndex(0);//1410
 {$ELSE}
	 ROIconstraint.ItemIndex := (0);//1410
 {$ENDIF}
end;

procedure TAutoROIForm.FormHide(Sender: TObject);
begin
//	 if (AutoROIForm.ModalResult = mrCancel) and (gBGImg.VOIUndoVolItems > 1) and (gBGImg.VOIUndoVolItems = gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems) then
//		Move(gImageBackupBuffer^,gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^,gImageBackupSz);
	 if (AutoROIForm.ModalResult = mrCancel) then
		UndoVolVOI;
	 if not (AutoROIForm.ModalResult = mrCancel) then
		gBGImg.VOIchanged := true;
	 //if gImageBackupSz <> 0 then Freemem(gImageBackupBuffer);
	 //gImageBackupSz := 0;
	 ImgForm.Fill3DBtn.Down := false;
	 ImgForm.RefreshImagesTimer.Enabled := true;
end;

//Previous: create 3D ROI
//Below fill bubbles in 3D ROIS
//ROIcluster Follows
(***********************************************************88
************************************************************
**********************************************************)
procedure ROICluster (lXdim, lYDim, lZDim,lXOriginIn,lYOrigin,lZOrigin: integer; lDeleteNotFill: boolean);
var
  lVariability,lOrigin,lClusterInputValue,lClusterOutputValue, lClusterSz,lQTail,
  lXOrigin,lQHead,lSliceSz,lQSz,lInc,lVolSz: integer;
  lXInc,lYInc,lZInc,lSlicePos,lYPos,
  lMinX,lMaxX,lMinY,lMaxY,lMinZ,lMaxZ,
  lMinXBound,lMaxXBound,lMinYBound,lMaxYBound,lMinZBound,lMaxZBound: integer;
  lAtEdge: boolean;
  lROIBuf: bytep;
  lQra: LongIntP;
const
     kFillValue = -2;
Procedure IncQra(var lVal, lQSz: integer);
begin
    inc(lVal);
    if lVal >= lQSz then
     lVal := 1;
end;

 procedure Check(lPixel: integer);
 begin
    if (abs(lROIBuf^[lPixel] - lClusterInputValue)) <= lVariability then begin//add item
        incQra(lQHead,lQSz);
        inc(lClusterSz);
		lROIBuf^[lPixel] := lClusterOutputValue;
        lQra^[lQHead] := lPixel;
   end;
 end;

PROCEDURE RetirePixel; //FIFO cleanup
VAR
   lVal,lXPos,lYPos,lZPos: integer;
BEGIN
   lVal := lQra^[lQTail];
   lXpos := lVal mod lXdim;
   if lXpos = 0 then lXPos := lXdim;

   lYpos := (1+((lVal-1) div lXdim)) mod lYDim;
   if lYPos = 0 then lYPos := lYdim;

   lZpos := ((lVal-1) div lSliceSz)+1;

   if lXPos < lMinX then lMinX := lXPos;
   if lXPos > lMaxX then lMaxX := lXPos;
   if lXpos > lMinXBound then Check(lVal -1);//check to left
   if lXPos < lMaxXBound then Check(lVal + 1); //check to right

   if lYPos < lMinY then lMinY := lYPos;
   if lYPos > lMaxY then lMaxY := lYPos;
   if lYpos > lMinYBound then Check(lVal -lXdim);//check previous line
   if lYPos < lMaxYBound then Check(lVal + lXdim); //check next line

   if lZPos < lMinZ then lMinZ := lZPos;
   if lZPos > lMaxZ then lMaxZ := lZPos;
   if lZpos > lMinZBound then Check(lVal -lSliceSz);//check previous slice
   if lZPos < lMaxZBound then Check(lVal + lSliceSz); //check next slice

   incQra(lQTail,lQSz); //done with this pixel
END;

procedure FillStart (lPt: integer); {FIFO algorithm: keep memory VERY low}
var lI: integer;
begin
  //1414 follows
  for lI := 1 to lQsz do
      lQra^[lI] := 0;
  lQHead := 0;
  lQTail := 1;
  Check(lPt);
  RetirePixel;
  while ((lQHead+1) <> lQTail) do begin//complete until all voxels in buffer have been tested
        RetirePixel;
		if (lQHead = lQSz) and (lQTail = 1) then
           exit; //break condition: avoids possible infinite loop where QTail is being incremented but QHead is stuck at maximum value
  end;
end;

procedure SelectClusters (lInput,lOutput: integer);
begin
     lClusterSz := 0;
     lClusterInputValue := lInput;
     lClusterOutputValue := lOutput;
	 FillStart(lOrigin);
end;

function Lo (lVolumeEdge,lObjectEdge: integer): integer;
begin
    if lVolumeEdge > lObjectEdge then
       result := lObjectEdge
    else begin
        lAtEdge := true;
        result := lVolumeEdge;
    end;
end;

function Hi (lVolumeEdge,lObjectEdge: integer): integer;
begin
    if lVolumeEdge < lObjectEdge then
       result := lObjectEdge
    else begin
        lAtEdge := true;
        result := lVolumeEdge;
    end;
end;

begin
	 lXOrigin := lXOriginIn;
	 lVolSz := lXdim*lYdim*lZdim;
	 if gMRIcroOverlay[kVOIOverlayNum].ScrnBufferItems <> lVolSz then begin
		 showmessage('You need to draw or load a VOI in order to use the 3D bubble tool.');
		 exit;
	 end;
	 CreateUndoVol;
     lSliceSz := lXdim * lYdim;
     lMinX:=lXOrigin;
     lMaxX:=lXOrigin;
     lMinY:=lYOrigin;
     lMaxY:=lYOrigin;
     lMinZ:=lZOrigin;
     lMaxZ:=lZOrigin;
   lMinXBound := 1;
   lMaxXBound := lXDim;
   lMinYBound := 1;
   lMaxYBound := lYDim;
   lMinZBound := 1;
   lMaxZBound := lZDim;
     lOrigin := lXOrigin + ((lYOrigin-1)*lXdim)+((lZOrigin-1)*lSliceSz);
	 if (lOrigin > lVolSz) or (lXDim < 4) or (lYDim < 4) or (lZDim < 4) or (lVolSz < 1) {or (gROIBupSz <> lVolSz )} then exit;
	 if (gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^[lOrigin] = 0) then begin
        showmessage('You must click directly on a ROI to select it. The 3D ROI bubble tool will not work unless you choose the ROI you wish to fill/delete.');
        exit;
	 end;
     GetMem(lROIBuf, lVolSz);
	 for lInc := 1 to lVolSz do
		 if gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^[lInc] > 0 then//ROI
            lROIBuf^[lInc] := 1
         else
             lROIBuf^[lInc] := 0;
     //BEGIN: define selected ROI contiguous cluster
     lQSz := (lVolSz div 4)+8;
     GetMem(lQra,lQsz * sizeof(longint) );
     lVariability := 0; //only convert images that are exactly 1
     SelectClusters(1,255); //selected 3D ROI is 255, other ROI = 1, nonROI 0
     //END: define selected roi
     //BEGIN: either delete selected ROI, _OR_ fill bubbles in selected ROI
     if lDeleteNotFill then begin
	   for lInc := 1 to lVolSz do
		 if lROIBuf^[lInc] = 1 then    //alfa
			lROIBuf^[lInc] := gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^[lInc] //a different ROI
		 else
			lROIBuf^[lInc] := 0;//gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer[lInc]; //1402 selected ROI or non-ROI
	 end else begin //fill bubbles in selected ROI
		 //FindROIbounds;
   lMinXBound := Hi(1,lMinX-1);
   lMaxXBound := Lo(lXDim,lMaxX+1);
   lMinYBound := Hi(1,lMinY-1);
   lMaxYBound := Lo(lYDim,lMaxY+1);
   lMinZBound := Hi(1,lMinZ-1);
   lMaxZBound := Lo(lZDim,lMaxZ+1);
	 lOrigin := (lMinXBound) + ((lMinYBound-1)*lXdim)+((lMinZBound-1)*lSliceSz);
          lVariability := 2;//convert voxels that are either 0 or 1 to 1
          SelectClusters(1,128);
          //now bubbles trapped in volume are set to zero
          //we next need to distinguish bubbles from unmarked voxels outside the searched object boundary
          for lZInc := lMinZBound to lMaxZBound do begin
              lSlicePos := (lZInc-1) * lSliceSz;
              for lYInc := lMinYBound to lMaxYBound do begin
                  lYPos := (lYInc-1) * lXDim;
                  for lXInc := lMinXBound to lMaxXBound do begin
                      lInc :=  lXInc + lYPos + lSlicePos;
                      if lROIBuf^[lInc] = 0 then lROIBuf^[lInc] := 33;
                  end; //for X
			  end; //for Y
          end; //for Z

          for lInc := 1 to lVolSz do
			  if lROIBuf^[lInc] = 33 then
				lROIBuf^[lInc] := kVOI8bit //bubble in selected ROI
			  else
				lROIBuf^[lInc] := gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^[lInc];
	 end;
     Freemem(lQra);
     //BEGIN: CREATE 3D UNDO BUFFER
	 (*if (gDynSz > 1) and (gDynSz = gImageBackupSz) then begin
        if (gUndoBufSz > 0) then freemem(gUndoBuffer);
        gUndoBufSz := gDynSz;
        getmem(gUndoBuffer,gDynSz);
        Move(gImageBackupBuffer^,gUndoBuffer^,gImageBackupSz);
        gSaveUndoBuf := true;
	 end;   (**)
     //END: CREATE 3D UNDO BUFFER
     //BEGIN: mopping up: prepare data for viewing, report ROI change
	 Move(lROIBuf^,gMRIcroOverlay[kVOIOverlayNum].ScrnBuffer^,lVolSz);
	 Freemem(lROIBuf);  {}
	gBGImg.VOIchanged := true;
	 //END: mopping up
	ImgForm.RefreshImagesTimer.enabled := true;
end;  (**)

procedure TAutoROIForm.AutoROIBtnClick(Sender: TObject);
begin
	AutoROIForm.ModalResult := mrOK;
	AutoROIForm.close;
end;

procedure TAutoROIForm.CancelBtnClick(Sender: TObject);
begin
	 AutoROIForm.close;
end;

procedure TAutoROIForm.AutoROIchange(Sender: TObject);
begin
     if not AutoROIForm.visible then exit;
     Timer1.Enabled := true;
end;

procedure TAutoROIForm.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := false;
PreviewBtnClick(sender);
end;

procedure TAutoROIForm.FormDestroy(Sender: TObject);
begin
	 //if gImageBackupSz <> 0 then Freemem(gImageBackupBuffer);
     //gImageBackupSz := 0;
end;

  {$IFDEF FPC}
initialization
  {$I autoroi.lrs}
{$ENDIF}

end.