-
Notifications
You must be signed in to change notification settings - Fork 5
/
three_B.java
1694 lines (1374 loc) · 40.8 KB
/
three_B.java
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
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;
import ij.plugin.filter.PlugInFilter;
import ij.*;
import ij.io.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.InterruptedException;
import java.lang.System;
import java.lang.Math.*;
import java.lang.reflect.*;
//Please accept my apologies for poor Java code.
//This is my first ever Java program.
class ThreeBGlobalConstants
{
public static int critical_iterations=200;
};
///Utility class to hold a pair of strings
///
///@ingroup gPlugin
class SPair
{
public String a, b;
}
///Utility calss to hold a number of handy static functions.
///@ingroup gPlugin
class Util
{
///Read a file into a string. It simply sidcards errors
///because if the file is missing from the JAR archive,
///then extreme badness has happened and I have no idea how to
///recover.
///@param in File to be read
///@returns file contents in a string
static String read(Reader in)
{
try {
final char[] buffer = new char[100];
StringBuilder out = new StringBuilder();
int read;
do
{
read = in.read(buffer, 0, buffer.length);
if (read>0)
out.append(buffer, 0, read);
} while (read>=0);
return out.toString();
}
catch(java.io.IOException err)
{
return "";
//What do we do here?
}
}
///The a file name for saving and complete path using ImageJ's file open dialog.
///Kep re-querying user if the file will be overwritten. Windows already provides
///the query built in.
///@ingroup gPlugin
///@param t Initial file name
///@returns filename and full path
static SPair getFileName(String t)
{
//Get a filename to save as, with appropriate warnings for
//overwriting files.
String fname, fullname;
while(true)
{
SaveDialog save = new SaveDialog("Save 3B output", t, ".txt");
fname = save.getFileName();
fullname = save.getDirectory() + File.separator + fname;
if(fname == null)
break;
File test = new File(fullname);
//Windows' open dialog seems to do overwrite confirmation automatically,
//so there is no need to do it here.
if(!ij.IJ.isWindows() && test.exists())
{
GenericDialog g = new GenericDialog("Overwrite file?");
g.addMessage("The file \"" + fname + "\" already exists. Continue and overwrite?");
g.enableYesNoCancel("Yes", "No");
g.showDialog();
if(g.wasOKed())
break;
else if(g.wasCanceled())
{
fname = null;
break;
}
}
else
break;
}
SPair r = new SPair();
r.a = fname;
r.b = fullname;
return r;
}
}
//ImageJ plugins must have an _ in the name. lolwut?
///ImageJ plugin class
///@ingroup gPlugin
public class three_B implements PlugInFilter {
ImagePlus window;
ByteProcessor mask;
String arg;
public int setup(String arg_, ImagePlus img) {
window = img;
arg=arg_;
return ROI_REQUIRED + SUPPORTS_MASKING + STACK_REQUIRED +NO_CHANGES + DOES_16 + DOES_32 + DOES_8G;
}
public void run(ImageProcessor ip) {
//Load the config file contents
Reader cfgstream = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("multispot5.cfg"));
String cfg = Util.read(cfgstream);
try{
cfgstream.close();
}
catch(IOException close_err){
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Error reading config file.");
return;
}
//Some basic error checking if reading of the config file from
//the JAR archive fails.
if(cfg == "")
{
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Error reading config file.");
return;
}
//The image from getMask() is only the size of the ROI
//We need it to be congruent with the original image, in order
//to work with the C++ code.
mask = new ByteProcessor(ip.getWidth(), ip.getHeight());
int x = ip.getRoi().x;
int y = ip.getRoi().y;
//Rectangular selections do not have a mask set, so we get
//a null pointer exception when we try to copy.
//So, we have to manually set the pixels, rather than just
//copy them
try{
mask.copyBits(ip.getMask(), x, y, Blitter.COPY);
}
catch(NullPointerException e)
{
for(int r=0; r < ip.getRoi().height; r++)
for(int c=0; c < ip.getRoi().width; c++)
mask.set(c+x, r+y, 255);
}
//Count the number of set pixels in the mask. This is used to
//warn the user if the number is not within a reasonable range.
int count=0;
for(int r=0; r < mask.getHeight(); r++)
for(int c=0; c < mask.getWidth(); c++)
if(mask.get(c, r) != 0)
count ++;
//The non-config file parameters are the range of frames to operate on
//and the pixel size in nm (the config works in terms of FWHM in pixels).
//These have to be sepficied whether the basic or advanced dialog is used.
int firstfr;
int lastfr;
double pixel_size_in_nm;
ImageStack s = window.getStack();
if(arg.equals("advanced"))
{
AdvancedDialog ad = new AdvancedDialog(cfg, s.getSize());
if(!ad.wasOKed())
return;
cfg = ad.getTextArea1().getText();
/*pixel_size_in_nm = ad.getPixelSize();
firstfr = ad.getFirstFrame();
lastfr = ad.getLastFrame();*/
pixel_size_in_nm = ad.getNextNumber();
firstfr = (int)ad.getNextNumber();
lastfr = (int)ad.getNextNumber();
}
else
{
ThreeBDialog gd = new ThreeBDialog(count, mask.getWidth()*mask.getHeight(), s.getSize());
gd.showDialog();
if(!gd.wasOKed())
return;
/*final double fwhm = gd.getFWHM();
pixel_size_in_nm = gd.getPixelSize();
final int initial_spots = gd.getSpots();
firstfr = gd.getFirstFrame();
lastfr = gd.getLastFrame();*/
//We have to use getNextNumber, otherwise macro recording does not work.
final double fwhm = gd.getNextNumber();
pixel_size_in_nm = gd.getNextNumber();
final int initial_spots = (int)gd.getNextNumber();
firstfr = (int)gd.getNextNumber();
lastfr = (int)gd.getNextNumber();
//Compute the parameters of the log-normal prior such that the mode
//matches the size of the spots.
final double sigma = (fwhm / pixel_size_in_nm) / (2*Math.sqrt(2*Math.log(2)));
final double blur_sigma=0.1;
//s = exp(mu-sig^2)
//ln s = mu - sig^2
//mu = ln s + sig^2
final double blur_mu = Math.log(sigma) + blur_sigma*blur_sigma;
//Initialized from the current time.
Random rng = new Random();
//
//Append stuff to the config file now. This will be parsed later in C++.
cfg = cfg + "placement.uniform.num_spots=" + Integer.toString(initial_spots) + "\n"
+ "blur.mu=" + Double.toString(blur_mu) + "\n"
+ "blur.sigma=" + Double.toString(blur_sigma) + "\n"
+ "seed=" + Integer.toString(rng.nextInt(16777216)) + "\n";
}
//Acquire a filename to save moderately safely.
SPair f = Util.getFileName(window.getTitle());
final String fname = f.a;
final String fullname = f.b;
if(fname!= null)
{
//Create the 3B runner and the control panel, then execute the control panel in the
//GUI thread.
final Rectangle roi = ip.getRoi();
final double pixel_size_in_nm_ = pixel_size_in_nm;
final ThreeBRunner tbr = new ThreeBRunner(mask, s, cfg, fullname, firstfr, lastfr);
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
new EControlPanel(roi, pixel_size_in_nm_, fname, tbr);
}
}
);
}
}
};
///Control panel which basically presents the .cfg file in a large edit box
///along with additional necessary things (frame range and pixel size). Also
///make the user acknowledge that they might be getting into trouble.
///This is uglier than I would like.
///@ingroup gPlugin
class AdvancedDialog extends GenericDialog implements DialogListener
{
boolean ok=true;
int nframes;
AdvancedDialog(String cfg, int nframes_)
{
super("3B Analysis");
nframes=nframes_;
addMessage("Advanced configuration");
addMessage(" ");
addCheckbox("I understand 3B enough to be editing the text below", false);
addTextAreas(cfg, null, 40, 100);
addNumericField("Pixel size (nm / pixel)", 100., 1, 10, "nm");
addNumericField("First frame", 0., 0, 10, "");
addNumericField("Last frame", nframes-1., 0, 10, "");
addDialogListener(this);
dialogItemChanged(null, null);
showDialog();
}
public boolean dialogItemChanged(GenericDialog gd, java.awt.AWTEvent e)
{
boolean v = ((Checkbox)(getCheckboxes().get(0))).getState();
if(v != ok)
{
ok=v;
if(ok)
{
getTextArea1().setEditable(true);
((Label)getMessage()).setText("Warning: strange behaviour may result!");
getPixelSizeField().setEditable(true);
getFirstFrameField().setEditable(true);
getLastFrameField().setEditable(true);
}
else
{
getTextArea1().setEditable(false);
((Label)getMessage()).setText(" ");
getPixelSizeField().setEditable(false);
getFirstFrameField().setEditable(false);
getLastFrameField().setEditable(false);
}
}
//Clamp the frames
int first = getFirstFrame();
int last = getLastFrame();
int nfirst = Math.max(0, Math.min(nframes-1, first));
int nlast = Math.max(nfirst, Math.min(nframes-1, last));
if(first != nfirst)
getFirstFrameField().setText(Integer.toString(nfirst));
if(last != nlast)
getLastFrameField().setText(Integer.toString(nlast));
return ok;
}
int parseInt(String s)
{
try
{
return Integer.parseInt(s);
}
catch(Exception e)
{
return 0;
}
}
public double parseDouble(String s)
{
try
{
return Double.parseDouble(s);
}
catch(Exception e)
{
return 0;
}
}
TextField getPixelSizeField()
{
return (TextField)(getNumericFields().get(0));
}
TextField getFirstFrameField()
{
return (TextField)(getNumericFields().get(1));
}
TextField getLastFrameField()
{
return (TextField)(getNumericFields().get(2));
}
double getPixelSize()
{
return parseDouble(getPixelSizeField().getText());
}
int getFirstFrame()
{
return parseInt(getFirstFrameField().getText());
}
int getLastFrame()
{
return parseInt(getLastFrameField().getText());
}
}
///Dialog box for starting 3B
///The dialog highlights bad things in red.
///@ingroup gPlugin
//Not pretty. Should I use dialogItemChanged rather than textValueChanged???
class ThreeBDialog extends GenericDialog
{
int count_, npix, nframes;
Color c, bg;
ThreeBDialog(int count, int npix_, int nframes_)
{
super("3B Analysis");
count_ = count;
npix = npix_;
nframes= nframes_;
addNumericField("Microscope FWHM", 250.0, 1, 10, "nm");
addNumericField("Pixel size", 100., 1, 10, "nm");
addNumericField("Initial number of spots", Math.round(count / 10.), 0, 10, "spots");
addNumericField("First frame", 0., 0, 10, "");
addNumericField("Last frame", nframes-1., 0, 10, "");
addTextAreas("",null, 8,30);
getTextArea1().setEditable(false);
c = getFWHMField().getBackground(); //Get the default background colour
bg = getBackground(); //Dialog background color
getTextArea1().removeTextListener(this); //To prevent event thrashing when we write messages
//Process initial warnings
textValueChanged(null);
}
//Try to parse a double and return 0 for an empty string.
public double parseDouble(String s)
{
try
{
return Double.parseDouble(s);
}
catch(Exception e)
{
return 0;
}
}
int parseInt(String s)
{
try
{
return Integer.parseInt(s);
}
catch(Exception e)
{
return 0;
}
}
TextField getFWHMField()
{
return (TextField)(getNumericFields().get(0));
}
TextField getPixelSizeField()
{
return (TextField)(getNumericFields().get(1));
}
TextField getSpotsField()
{
return (TextField)(getNumericFields().get(2));
}
TextField getFirstFrameField()
{
return (TextField)(getNumericFields().get(3));
}
TextField getLastFrameField()
{
return (TextField)(getNumericFields().get(4));
}
double getFWHM()
{
return parseDouble(getFWHMField().getText());
}
double getPixelSize()
{
return parseDouble(getPixelSizeField().getText());
}
int getSpots()
{
return parseInt(getSpotsField().getText());
}
int getFirstFrame()
{
return parseInt(getFirstFrameField().getText());
}
int getLastFrame()
{
return parseInt(getLastFrameField().getText());
}
int getCount()
{
return count_;
}
public void textValueChanged(TextEvent e)
{
boolean long_run=false;
String err = "";
// 012345678901234567890123456789012345678901234567890
if(getCount() > 1000)
{
long_run=true;
err = err + "Warning: large area selected.\n3B will run very slowly.\n";
}
if(npix < 2500)
err = err + "Warning: image is very small. Fitting may be bad because\nimage noise cannot be accurately estimated.\n";
if(getSpots() > 500)
{
err = err + "Warning: large number of spots.\n3B will run very slowly.\n";
getSpotsField().setBackground(Color.RED);
}
else
getSpotsField().setBackground(c);
if(getFWHM() < 200)
{
err = err + "Warning: unrealistically small\nmicsoscope resolution.\n";
getFWHMField().setBackground(Color.RED);
}
else if(getFWHM() > 350)
{
err = err + "Warning: 3B will not work well with\na poorly focussed microscope.\n";
getFWHMField().setBackground(Color.RED);
}
else
getFWHMField().setBackground(c);
if(getPixelSize() < 70)
{
getPixelSizeField().setBackground(Color.RED);
err = err + "Warning: Very small pixels specified.\nAre you sure?\n";
}
else if(getPixelSize() > 180)
{
getPixelSizeField().setBackground(Color.RED);
err = err + "Warning: 3B will not work well if the camera\nresolution is too poor.\n";
}
else
getPixelSizeField().setBackground(c);
//Clamp the frames
int first = getFirstFrame();
int last = getLastFrame();
int nfirst = Math.max(0, Math.min(nframes-1, first));
int nlast = Math.max(nfirst, Math.min(nframes-1, last));
if(first != nfirst)
getFirstFrameField().setText(Integer.toString(nfirst));
if(last != nlast)
getLastFrameField().setText(Integer.toString(nlast));
if(last -first + 1 > 500)
{
getFirstFrameField().setBackground(Color.RED);
getLastFrameField().setBackground(Color.RED);
err = err + "Warning: large number of frames specified.\n3B will run very slowly and may be inaccurate.\nFewer than 500 frames is strongly recommended.\n200--300 is generally most suitable.\n";
long_run = true;
}
if(last -first + 1 < 150)
{
getFirstFrameField().setBackground(Color.RED);
getLastFrameField().setBackground(Color.RED);
err = err + "Warning: small number of frames specified.\n3B may be inaccurate.\nAt least 150 frames is recommended.\n";
}
else
{
getFirstFrameField().setBackground(c);
getLastFrameField().setBackground(c);
}
if(!long_run && (last -first + 1)*getCount() > 200000)
{
err = err + "Warning: large amount of data specified.\n"+
"3B will run very slowly.\n"+
"Reduce the area and/or number of frames.\n"+
"We recommend: \n" +
"Number of frames*area in pixels < 200,000.";
getFirstFrameField().setBackground(Color.RED);
getLastFrameField().setBackground(Color.RED);
}
if(!err.equals(""))
getTextArea1().setBackground(Color.RED);
else
getTextArea1().setBackground(bg);
getTextArea1().setText(err);
repaint();
}
}
///Basic spot class, simply contains coordinates.
///@ingroup gPlugin
class Spot
{
double x, y;
Spot()
{
}
Spot(double xx, double yy)
{
x=xx;
y=yy;
}
}
///Listener class which triggers a complete redraw. Since all redraws are
///pretty much equal, there is no need to distinguish them.
///@ingroup gPlugin
class SomethingChanges implements ChangeListener
{
private EControlPanel c;
public SomethingChanges(EControlPanel c_)
{
c = c_;
}
public void stateChanged(ChangeEvent e)
{
c.send_update_canvas_event();
}
}
///This class makes a floating point slider bar with an edit box next to
///it for more precision. Also has a reciprocal option for inverse, reciprocal scaling./
///@ingroup gPlugin
//That's not at all bodged in in an unpleasant way.
class FloatSliderWithBox extends JPanel {
private JSlider slider;
private JTextField number;
private JLabel label;
private GridBagConstraints completePanelConstraints_;
private int steps=1000000;
private double min, max;
private String text;
private String units;
private String format = "%8.3f";
private double value;
private boolean reciprocal;
public FloatSliderWithBox(String text_, double min_, double max_, double value_, int cols, boolean rec_)
{
super( new GridBagLayout() );
reciprocal = rec_;
min=min_;
max=max_;
text=text_;
value = value_;
if(reciprocal)
{
min=1/max_;
max=1/min_;
}
slider = new JSlider(0, steps);
label = new JLabel();
number = new JTextField(cols);
//Assemble into a panel
completePanelConstraints_ = new GridBagConstraints();
completePanelConstraints_.fill = GridBagConstraints.HORIZONTAL;
completePanelConstraints_.gridx = 0;
completePanelConstraints_.gridy = 0;
completePanelConstraints_.weightx=1;
this.add( slider, completePanelConstraints_ );
completePanelConstraints_.gridx = 2;
completePanelConstraints_.gridy = 0;
completePanelConstraints_.weightx=0;
this.add(label, completePanelConstraints_ );
completePanelConstraints_.gridx = 1;
completePanelConstraints_.gridy = 0;
completePanelConstraints_.weightx=0;
//Add some space to the left to move away from slider slightly
//And some more to the bottom to help with the way they are displayed
completePanelConstraints_.insets = new Insets(0,5,10,0);
this.add( number, completePanelConstraints_ );
this.setBorder(BorderFactory.createTitledBorder(text));
slider.addChangeListener(new SliderChanged(this));
number.addActionListener(new TextChanged(this));
setValue(value);
}
public FloatSliderWithBox setUnits(String s)
{
units = s;
setValue(value);
return this;
}
public FloatSliderWithBox setFormat(String s)
{
format = s;
setValue(value);
return this;
}
public void addChangeListener(ChangeListener changeListener){
slider.addChangeListener( changeListener );
return;
}
void setValue(double v)
{
value = v;
if(reciprocal)
slider.setValue((int)Math.round(steps * (1/value-min)/(max-min)));
else
slider.setValue((int)Math.round(steps * (value-min)/(max-min)));
number.setText(String.format(format, value));
label.setText(units);
}
double getValue()
{
return value;
}
public double get_value_from_slider()
{
if(reciprocal)
return 1/((slider.getValue() * 1.0 / steps) * (max - min) + min);
else
return (slider.getValue() * 1.0 / steps) * (max - min) + min;
}
public double get_value_from_text()
{
return Double.parseDouble(number.getText());
}
class SliderChanged implements ChangeListener
{
FloatSliderWithBox f;
SliderChanged(FloatSliderWithBox f_)
{
f = f_;
}
public void stateChanged(ChangeEvent e)
{
f.setValue(f.get_value_from_slider());
}
}
class TextChanged implements ActionListener
{
FloatSliderWithBox f;
TextChanged(FloatSliderWithBox f_)
{
f = f_;
}
public void actionPerformed(ActionEvent e)
{
f.setValue(f.get_value_from_text());
}
}
}
///Close button issues a window close event. Actual closing logic is then
///done in the close event handler.
///@ingroup gPlugin
class CloseButtonListener implements ActionListener
{
private JFrame f;
public CloseButtonListener(JFrame fr)
{
f = fr;
}
public void actionPerformed(ActionEvent e)
{
//Voodoo from Stack Overflow
WindowEvent wev = new WindowEvent(f, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
}
///Stop 3B thread
///@ingroup gPlugin
class StopButtonListener implements ActionListener
{
private EControlPanel t;
public StopButtonListener(EControlPanel t_)
{
t = t_;
}
public void actionPerformed(ActionEvent e)
{
t.issue_stop();
}
}
///Listener for the export button.
///@ingroup gPlugin
class ExportButtonListener implements ActionListener
{
private EControlPanel c;
public ExportButtonListener(EControlPanel c_)
{
c = c_;
}
public void actionPerformed(ActionEvent e)
{
c.export_reconstruction_as_ij();
}
}
class Reconstruction
{
///Compute a brand new reconstruction.
static FloatProcessor reconstruct(Rectangle roi, double zoom, double reconstruction_blur_fwhm, double pixel_size_in_nm, ArrayList<Spot> pts)
{
//Reconstruct an image which is based around the ROI in size
int xoff = (int)roi.x;
int yoff = (int)roi.y;
//Apparently combining a 1x1 rectangle at 0,0 and 24,24 gives a total width of... 24.
//lol.
int xsize = (int)Math.round((roi.width + 1) * zoom);
int ysize = (int)Math.round((roi.height + 1) * zoom);
//New blank image set to zero
FloatProcessor reconstructed = new FloatProcessor(xsize, ysize);
for(int i=0; i < pts.size(); i++)
{
//Increment the count
int xc = (int)Math.floor((pts.get(i).x-xoff) * zoom + 0.5);
int yc = (int)Math.floor((pts.get(i).y-yoff) * zoom + 0.5);
float p = reconstructed.getPixelValue(xc, yc);
reconstructed.putPixelValue(xc, yc, p+1);
}
double blur_sigma = reconstruction_blur_fwhm / (2 * Math.sqrt(2 * Math.log(2))) * zoom / pixel_size_in_nm;
(new GaussianBlur()).blurGaussian(reconstructed, blur_sigma, blur_sigma, 0.005);
/*
//Make an image
int size =xsize;
//New image
FloatProcessor reconstructed = new FloatProcessor(size, size);
for(int y=0; y < size; y++)
for(int x=0; x < size; x++)
if(x % 20 < 10 != y % 20 < 10)
reconstructed.putPixelValue(x, y, x * 255.0 / size);
*/
return reconstructed;
}
}
///Control panel for running 3B plugin and providing interactive update.
///@ingroup gPlugin
class EControlPanel extends JFrame implements WindowListener
{
private ImagePlus linear_reconstruction; //Reconstructed image
private ImageCanvas canvas;
private JButton stopButton, exportButton, closeButton;
private Color exportButtonColor;
private JLabel status, time_msg;
private FloatSliderWithBox blur_fwhm;
private FloatSliderWithBox reconstructed_pixel_size;
private JPanel complete;
private JPanel buttons;
private JScrollPane scroll;
private ThreeBRunner tbr;
private ArrayList<Spot> pts;
private JLabel image_label;
private ImageIcon icon;
private Rectangle roi;
private double zoom=0.01; //Sets initial zoom small, so the ImageCanvas will always be bigger than its initial size
//otherwise it always puts up the zooming rectangle :(
private double reconstruction_blur_fwhm=.5;
private double pixel_size_in_nm;
private String filename;
//Number of iterations. Used to colourize export button and provide warnings.
//Architecture is now getting quite messy.
private int iterations = 0;
void set_reconstruction_blur_fwhm(double r)
{
blur_fwhm.setValue(r);
}
void set_reconstructed_pixel_size(double z)
{
reconstructed_pixel_size.setValue(z);
}
EControlPanel(Rectangle roi_, double ps_, String filename_, ThreeBRunner tbr_)
{
//Constract superclass
super(filename_);
tbr = tbr_;
filename=filename_;
roi = roi_;
pixel_size_in_nm = ps_;
pts = new ArrayList<Spot>();
//Now generate the dialog box
complete = new JPanel(new GridBagLayout());
//Create the image viewer
linear_reconstruction = new ImagePlus();
linear_reconstruction.setProcessor(reconstruct());
// canvas = new ImageCanvas(linear_reconstruction);