-
Notifications
You must be signed in to change notification settings - Fork 0
/
unCorrectForBleaching.pas
510 lines (414 loc) · 16.3 KB
/
unCorrectForBleaching.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
unit unCorrectForBleaching;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Forms, Vcl.Graphics, Vcl.Controls, Vcl.StdCtrls, Vcl.ComCtrls,
Vcl.ExtCtrls, Vcl.Dialogs, unGlobUnit, EditNumerical;
type
TfrmCorrectForBleaching = class(TForm)
btScaleFile: TButton;
cbCorrectDuplicate: TCheckBox;
ProgrBar: TProgressBar;
lblMessage: TLabel;
lblImageCnt: TLabel;
pnScaleTo: TPanel;
rbScaleToMinimum: TRadioButton;
Label1: TLabel;
pnWhatToUse: TPanel;
Label2: TLabel;
rbUsePlot: TRadioButton;
rbUseFunction: TRadioButton;
pnFunction: TPanel;
Label3: TLabel;
rbMultiply: TRadioButton;
rbLinear: TRadioButton;
rbExponential: TRadioButton;
enMultiplyFactor: TEditNumerical;
enApplyFunctionFromToIm: TEditNumerical;
Label4: TLabel;
enValueImBeg: TEditNumerical;
enValueImEnd: TEditNumerical;
enTau1stExp: TEditNumerical;
enTau2ndExp: TEditNumerical;
enPerc1stExp: TEditNumerical;
enPerc2ndExp: TEditNumerical;
Label7: TLabel;
Label8: TLabel;
pnExpOrder: TPanel;
rb1stOrder: TRadioButton;
rb2ndOrder: TRadioButton;
rbScaleToMaximum: TRadioButton;
rbScaleToImage: TRadioButton;
rbScaleToValue: TRadioButton;
enScaleToImNr: TEditNumerical;
enScaleToValue: TEditNumerical;
Label5: TLabel;
Label6: TLabel;
Label9: TLabel;
BevelProgBar: TBevel;
Label10: TLabel;
lbInfoWhenFileInRAM: TLabel;
procedure btScaleFileClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure enPerc1stExpReturnPressed(Sender: TObject);
procedure enPerc2ndExpReturnPressed(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
procedure SetImageRange;
procedure SetLabels;
end;
var
frmCorrectForBleaching: TfrmCorrectForBleaching;
implementation
Uses unTimeSerAnal,DigiFilters, unInputDialog, unPlayBack, Main;
{$R *.dfm}
procedure TfrmCorrectForBleaching.btScaleFileClick(Sender: TObject);
{scales (corrects0 image file for changes in brightness,e.g. bleaching}
var
FhCor : THandle;
pFiltData : Pointer;
CntIm, CntROI, CntPix : Integer;
ImToStart,ImToEnd : Integer;
Slp, Offs : Double; //Slope & Offset for Linear Fuction
CorFileName : AnsiString;
im8bitBuffer : TDynByteArray;
im16bitBuffer : TDynWordArray;
ScaleCoeff : TDynDoubleArray; //Scale Coefficients Array
MinOrMaxVal : Double;
Perc1st,Perc2nd : Double;
InitVal : Double;
Tau1,Tau2 : Extended;
mrValue : Byte;
Proceed : LongBool;
DuplFname : Boolean;
procedure SetProgrBar(const MinLim,MaxLim,Pos : Integer);
begin
with ProgrBar do begin
Min := 0;
Max := High(Integer);
Position := Pos;
Min := MinLim;
Max := MaxLim;
end;
end;
Begin
If (MainImLoaded) and
(((rbUsePlot.Checked) and (TimeSerAnalDone) and not(RatioOn) and
(PLOTvar.TraceDivMinUsed) and Not(PLOTvar.AnalType = RATIO_TIME)
and (PLOTvar.BaseLineStatus = NO_BASELINE)) or (rbUseFunction.Checked))
Then
Begin
Proceed := True;
If (rbUsePlot.Checked) and (rbScaleToImage.Checked) and
(enScaleToImNr.NumberOne-1 < frmTimeSerPlot.dsXaxes.PosFrom) or
(enScaleToImNr.NumberOne-1 > frmTimeSerPlot.dsXaxes.PosTo) then
Begin
Proceed := False;
MessageDlg('Scale To Image ' + IntToStr(enScaleToImNr.NumberOne) + ' is Out of Range.'
,mtError,[mbOK],0);
End;
If Not(FileInRAM) and (Proceed) then Begin
//-------------- Copying File part ------------------------------------------//
If (cbCorrectDuplicate.Checked) and (ImageFileType = MULTI_IMAGE) then Begin
Suffix := '_BC';
CorFileName := Copy(ImFname,1, Length(ImFname) - Length(ExtractFileExt(ImFname)));
CorFileName := CorFileName + Suffix + ExtractFileExt(ImFname);
//Check if File Exsist the if so change the Suffix
DuplFname := FileExists(CorFileName);
Proceed := not(DuplFname);
btPressed := bEnter;
If (DuplFname) Then While (btPressed = bEnter) and not(Proceed) do Begin
if (frmInputDialog = nil) then Application.CreateForm(TfrmInputDialog, frmInputDialog);
Application.ProcessMessages;
If (btPressed = bEnter) then Begin
CorFileName := Copy(ImFname,1, Length(ImFname) - Length(ExtractFileExt(ImFname)));
CorFileName := CorFileName + Suffix + ExtractFileExt(ImFname);
DuplFname := FileExists(CorFileName);
Proceed := not(DuplFname);
End Else
If (btPressed = bOverWrite) then
Proceed := True
else
If (btPressed = bCancel) then
Proceed := False;
End; //If not(Proceed) Then While not(Proceed) do Begin
If (Proceed) and (btPressed = bEnter) then Begin
ProgrBar.Visible := True;
BevelProgBar.Visible := True;
SetProgrBar(0,ImToEnd-ImToStart,0);
lblMessage.Caption := 'Copying File';
Application.ProcessMessages;
Proceed := Boolean(CopyFileExA(pAnsiChar(ImFname),pAnsiChar(CorFileName),@CopyFileWithProgressBar,ProgrBar,pBool(False),0));
End;
if not(Proceed) and (btPressed <> bCancel) then begin //only if Copy did not Succeed
BevelProgBar.Visible := False;
ProgrBar.Visible := False;
MessageDlg('File Cannot be Copied',mtError,[mbOK],0);
lblMessage.Caption := '';
end;
{---------- End of Copying the File ----------}
End
Else
Begin
mrValue := MessageDlg('File will be Overwritten and Impossible to Recover!', mtConfirmation, [mbCancel,mbOK], 0, mbCancel);
if (mrValue = mrCancel) then Proceed := False else Proceed := True;
End;
End; // If Not(FileInRAM) and (Proceed) then Begin
//---------------------- End of Copying File -----------------------------------//
//------------- Choose from Plot or Function ----------------------------------//
If (Proceed) then Begin
If (rbUsePlot.Checked) then Begin //Using Plot Trace
ImToStart := frmTimeSerPlot.dsXaxes.PosFrom; //zero based = 1st image index = 0
ImToEnd := frmTimeSerPlot.dsXaxes.PosTo; //zero based
SetLength(ScaleCoeff,ImToEnd-ImToStart+1);
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := 0;
//summ the values from all the displayed traces
For CntROI := 0 to PLOTvar.NrROI-1 Do Begin
if (PLOTvar.AdrUsed[CntROI] > 0) then
for CntIm := 0 to High(ScaleCoeff) do
ScaleCoeff[CntIm] := ScaleCoeff[CntIm] + TSPlotData_BaseNotSubtr[CntROI,CntIm + (ImToStart-PLOTvar.ImBeg)] - Bkg;
End;
//find minimum or maximum value
if (rbScaleToMaximum.Checked) then begin
MinOrMaxVal := 0;
for CntIm := 0 to High(ScaleCoeff) do begin
if MinOrMaxVal < ScaleCoeff[CntIm] then MinOrMaxVal := ScaleCoeff[CntIm];
end;
end
else
if (rbScaleToMinimum.Checked) then begin
MinOrMaxVal := High(LongInt);
for CntIm := 0 to High(ScaleCoeff) do begin
if MinOrMaxVal > ScaleCoeff[CntIm] then MinOrMaxVal := ScaleCoeff[CntIm];
end;
end
else
if (rbScaleToImage.Checked) then begin
CntIm := (enScaleToImNr.NumberOne -1) - ImToStart;
MinOrMaxVal := ScaleCoeff[CntIm];
end
else
if (rbScaleToValue.Checked) then begin
MinOrMaxVal := enScaleToValue.NumberFloat;
end;
End
Else
If (rbUseFunction.Checked) then Begin
ImToStart := enApplyFunctionFromToIm.NumberOne - 1; //to be zero based = 1st image index = 0
ImToEnd := enApplyFunctionFromToIm.NumberTwo - 1; //to be zero based
SetLength(ScaleCoeff,ImToEnd-ImToStart+1);
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := 0;
//-- different choises --//
if (rbMultiply.Checked) then begin
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := enMultiplyFactor.NumberFloat;
end
else
if (rbLinear.Checked) then begin
Offs := enValueImBeg.NumberFloat - Bkg;
Slp := (enValueImEnd.NumberFloat - enValueImBeg.NumberFloat)/High(ScaleCoeff);
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := Offs + Slp*CntIm;
end
else
if (rbExponential.Checked) then begin //50*exp(-col(A)/10) + 50*exp(-col(A)/100)
Offs := enValueImEnd.NumberFloat - Bkg;
InitVal := enValueImBeg.NumberFloat - Bkg;
if (rb1stOrder.Checked) then begin
tau1 := enTau1stExp.NumberFloat;
Perc1st := InitVal - Offs;
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := Perc1st*exp(-(CntIm*MeanImTime)/tau1) + Offs;
end else
if (rb2ndOrder.Checked) then begin
tau1 := enTau1stExp.NumberFloat;
tau2 := enTau2ndExp.NumberFloat;
Perc1st := ((InitVal-Offs)*enPerc1stExp.NumberOne)/100;
Perc2nd := ((InitVal-Offs)*enPerc2ndExp.NumberOne)/100;
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := Perc1st*exp(-(CntIm*MeanImTime)/tau1) + Perc2nd*exp(-(CntIm*MeanImTime)/tau2) + Offs;
end;
end; //if (rbExponential.Checked)
If not(rbMultiply.Checked) then Begin
if (rbScaleToMinimum.Checked) then begin
if (ScaleCoeff[0] < ScaleCoeff[High(ScaleCoeff)]) then
MinOrMaxVal := ScaleCoeff[0] else
MinOrMaxVal := ScaleCoeff[High(ScaleCoeff)];
end else
if (rbScaleToMaximum.Checked) then begin
if (ScaleCoeff[0] > ScaleCoeff[High(ScaleCoeff)]) then
MinOrMaxVal := ScaleCoeff[0] else
MinOrMaxVal := ScaleCoeff[High(ScaleCoeff)];
end else
if (rbScaleToImage.Checked) then begin
CntIm := (enScaleToImNr.NumberOne -1) - ImToStart;
MinOrMaxVal := ScaleCoeff[CntIm];
end else
if (rbScaleToValue.Checked) then begin
MinOrMaxVal := enScaleToValue.NumberFloat;
end;
End; //If not(rbMultiply.Checked)
End; //If (rbUseFunction.Checked) then Begin
//generate coefficiens
If not((rbMultiply.Checked) and (rbUseFunction.Checked)) then Begin
for CntIm := 0 to High(ScaleCoeff) do ScaleCoeff[CntIm] := MinOrMaxVal/ScaleCoeff[CntIm];
End;
{Reads Every Image, Scales it and Writes it to the New "_COR" File}
// ---------- From Here is the Actual -- Correction --------------//
//Set Progress bar
BevelProgBar.Visible := True;
ProgrBar.Visible := True;
SetProgrBar(0,ImToEnd-ImToStart,0);
lblMessage.Caption := 'Scaling File';
Application.ProcessMessages;
//Prepares Output Buffers//
If (BitCount = 8) Then begin
SetLength(im8bitBuffer,imXsize*imYsize);
End Else
If (BitCount = 16) Then begin
SetLength(im16bitBuffer,imXsize*imYsize);
End;
If (FileInRAM) then Begin
//since the file will be modified the 1st 5 cannot be used
//this copies 1st 5 non filtered images for later
//and prepares pointer for the Move procedure
If (MovFType = NIKON_ND2_MOV) and (NrChannels = 2) then Begin
if (BitCount = 8) Then pFiltData := @TwoChByteArr[0]
else
if (BitCount = 16) Then pFiltData := @TwoChWordArr[0];
End
Else
Begin
if (BitCount = 8) Then pFiltData := @im8bitBuffer[0]
else
if (BitCount = 16) Then pFiltData := @im16bitBuffer[0];
End;
End; //If (FileInRAM) then Begin
//Prepares File Handles for Writing
If not(FileInRAM) then Begin
If (ImageFileType = MULTI_IMAGE) then begin
if (cbCorrectDuplicate.Checked) then
FhCor := FileOpen(CorFileName,fmOpenWrite)
else begin
FileClose(ImFile);
ImFile := FileOpen(ImFname,fmOpenReadWrite or fmShareDenyNone);
FhCor := ImFile;
end;
End
Else
If (ImageFileType = FILE_COLLECTION) then
SetAndPropagateFileHandles(fmOpenReadWrite);
End; //If not(FileInRAM) then Begin
//------------------------------- Starts Scalling ----------------------------//
For CntIm := 0 to High(ScaleCoeff) do Begin
ReadImage(CntIm + ImToStart,0,pImData);
If (BitCount = 8) Then begin
for CntPix := 0 to High(Im8bitBuffer) do begin
Im8bitBuffer[CntPix] := Round((Im8bitData[CntPix]-Bkg)*ScaleCoeff[CntIm]) + Bkg;
end
End Else
If (BitCount = 16) Then begin
for CntPix := 0 to High(Im16bitBuffer) do begin
Im16bitBuffer[CntPix] := Round((Im16bitData[CntPix]-Bkg)*ScaleCoeff[CntIm]) + Bkg;
end;
End;
if (MovFType = NIKON_ND2_MOV) and (NrChannels = 2) then
ReplaceCh1InND2File(im8bitBuffer,im16bitBuffer);
If (FileInRAM) then Begin
if (ImageFileType = MULTI_IMAGE) then
Move(pFiltData^,RawDataInRAM[0,ImOff[0,CntIm+ImToStart]],ImageBytes)
else
if (ImageFileType = FILE_COLLECTION) then
Move(pFiltData^,RawDataInRAM[CollFileNrInRAM[CntIm+ImToStart],ImOff[0,CntIm]],ImageBytes)
else
if (ImageFileType = SINGLE_IMAGE) then begin
if (ToSwap) then begin
for CntPix := 0 to High(im16bitBuffer) do
im16bitBuffer[CntPix] := Swap(im16bitBuffer[CntPix]);
end;
Move(pFiltData^,RawDataInRAM[0,(CntIm+ImToStart)*SizeOfFile+StripOffs[0]],ImageBytes);
end;
End
Else
WriteImage(FhCor,CntIm + ImToStart,im8bitBuffer,im16bitBuffer);
lblImageCnt.Caption := IntToStr(CntIm + 1 + ImToStart) + ' im';
ProgrBar.Position := CntIm;
Application.ProcessMessages;
End; //For CntIm := 0 to High(PlData) do Begin
//Reset Progress Bars and Labels
lblImageCnt.Caption := '';
lblMessage.Caption := '';
BevelProgBar.Visible := False;
ProgrBar.Visible := False;
ProgrBar.Position := 0;
Application.ProcessMessages;
//------------------------------- End Scalling ----------------------------//
//Restores File Handles to Read Only if Choise was to Overwrite Files
If not(FileInRAM) then Begin
If (ImageFileType = MULTI_IMAGE) then Begin
if (cbCorrectDuplicate.Checked) then
FileClose(FhCor)
else begin
FileClose(ImFile);
ImFile := FileOpen(ImFname,fmOpenRead or fmShareDenyNone);
end;
End Else
If (ImageFileType = FILE_COLLECTION) and not(FileInRAM)
then SetAndPropagateFileHandles(fmOpenRead);
End; //If not(FileInRAM) then Begin
//refresh to current Image
if not(FileInRAM) then ReadImage(CurIm,0,pImData)
else
if (FileInRAM) then frmPlayback.PlaybackScrollBarChange(Sender);
if (FileInRAM) then RAM_Usage.KindOfModMovie := BLEACH_COR;
End; //If (Proceed) then Begin
End Else //if Many of Conditions are fulfilled = Success = True
Begin
if (MainImLoaded) then
MessageDlg('Create up to 20 traces from ROI representing decay in fluorescence due to bleaching.' +
'Use Area type analysis without Normalization (Off). F/Fo should be Off.'
+ 'Trace smoothing can be used.'
,mtInformation,[mbOK],0);
End;
End;
procedure TfrmCorrectForBleaching.enPerc1stExpReturnPressed(Sender: TObject);
begin
enPerc2ndExp.NumberOne := 100 - enPerc1stExp.NumberOne;
end;
procedure TfrmCorrectForBleaching.enPerc2ndExpReturnPressed(Sender: TObject);
begin
enPerc1stExp.NumberOne := 100 - enPerc2ndExp.NumberOne;
end;
procedure TfrmCorrectForBleaching.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
frmCorrectForBleaching := nil;
end;
procedure TfrmCorrectForBleaching.FormCreate(Sender: TObject);
begin
if (MainImLoaded) then
SetImageRange
else
enApplyFunctionFromToIm.SetNumbersOneTwo(1,2); //for all functions
enMultiplyFactor.NumberFloat := 1.0;
enValueImBeg.NumberFloat := 1.0; //Value at First Image - for Linear & Exponential
enValueImEnd.NumberFloat := 1.0; //Value at Last Image - for Linear & Exponential
enTau1stExp.NumberFloat := 10.0; //tau1 sec
enTau2ndExp.NumberFloat := 100.0; //tau2 sec
enPerc1stExp.NumberOne := 50; //% contribution
enPerc2ndExp.NumberOne := 50; //% contribution
enScaleToImNr.NumberOne := 1; //Selects Image Number to Scale To
enScaleToValue.NumberFloat := 100.0; //Selects Value to Scale To
SetLabels;
end;
procedure TfrmCorrectForBleaching.SetImageRange;
begin
enApplyFunctionFromToIm.SetNumbersOneTwo(1,NrIm);
enApplyFunctionFromToIm.Max := NrIm;
end;
procedure TfrmCorrectForBleaching.SetLabels;
begin
cbCorrectDuplicate.Visible := not(FileInRAM);
lbInfoWhenFileInRAM.Visible := FileInRAM
end;
End.