BioGTK  6.5.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
ChannelsTool.cs
1using AForge;
2using Gtk;
3using System;
4using System.Collections.Generic;
5using System.IO;
6
7
8namespace BioGTK
9{
10 public class ChannelsTool : Gtk.Window
11 {
12 #region Properties
13
15 private Builder _builder;
16
17#pragma warning disable 649
18 [Builder.Object]
19 private Button applyBut;
20 [Builder.Object]
21 private Button resetBut;
22 [Builder.Object]
23 private ComboBox channelsBox;
24 [Builder.Object]
25 private SpinButton sampleBox;
26 [Builder.Object]
27 private ComboBox maxUintBox;
28 [Builder.Object]
29 private SpinButton minBox;
30 [Builder.Object]
31 private SpinButton maxBox;
32 [Builder.Object]
33 private Button setMinAllBut;
34 [Builder.Object]
35 private Button setMaxAllBut;
36 [Builder.Object]
37 private Entry fluorBox;
38 [Builder.Object]
39 private SpinButton emissionBox;
40 [Builder.Object]
41 private SpinButton excitationBox;
42 [Builder.Object]
43 private SpinButton binBox;
44 [Builder.Object]
45 private SpinButton graphMinBox;
46 [Builder.Object]
47 private SpinButton graphMax;
48 [Builder.Object]
49 private ComboBox maxUintBox2;
50 [Builder.Object]
51 private CheckButton meanStackBox;
52#pragma warning restore 649
53
54 #endregion
55
56 #region Constructors / Destructors
57 public static ChannelsTool Create()
58 {
59 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/ChannelsTool.glade", FileMode.Open));
60 return new ChannelsTool(builder, builder.GetObject("chanTool").Handle);
61 }
62 /* Creating a new window and populating it with the values from the channels. */
63 protected ChannelsTool(Builder builder, IntPtr handle) : base(handle)
64 {
65 _builder = builder;
66 builder.Autoconnect(this);
67 App.channelsTool = this;
68 var store = new ListStore(typeof(string));
69 foreach (Channel item in Channels)
70 {
71 // Add items to the ListStore
72 store.AppendValues(item.ToString());
73 channelsBox.Active = 0;
74 }
75 channelsBox.Model = store;
76 // Set the text column to display
77 var renderer = new CellRendererText();
78 channelsBox.PackStart(renderer, false);
79 channelsBox.AddAttribute(renderer, "text", 0);
80
81 UpdateGUI();
82
83 minBox.Value = (int)Channels[0].stats[0].StackMin;
84 maxBox.Value = (int)Channels[0].stats[0].StackMax;
85 binBox.Value = 1;
86 binBox.Adjustment = new Adjustment(1, 1, 255, 1, 1, 1);
87 hist = HistogramControl.Create(Channels[0]);
88 hist.GraphMax = (int)maxBox.Value;
89 graphMax.Value = (int)maxBox.Value;
90 hist.Show();
91
92 minBox.ButtonPressEvent += minBox_ValueChanged;
93 //meanStackBox.Clicked += MeanStackBox_Clicked;
94 setMinAllBut.ButtonPressEvent += setMinAllBut_Click;
95 setMaxAllBut.ButtonPressEvent += setMaxAllBut_Click;
96 minBox.ValueChanged+= minBox_ValueChanged;
97 maxBox.ValueChanged+= maxBox_ValueChanged;
98 fluorBox.Changed += fluorBox_TextChanged;
99 emissionBox.ValueChanged += emissionBox_ValueChanged;
100 excitationBox.ValueChanged += excitationBox_ValueChanged;
101 binBox.ValueChanged += binBox_ValueChanged;
102 graphMinBox.ValueChanged += minGraphBox_ValueChanged;
103 graphMax.ValueChanged += maxGraphBox_ValueChanged;
104 applyBut.ButtonPressEvent += applyBut_Click;
105 resetBut.ButtonPressEvent += ResetButton_ButtonPressEvent;
106 this.ButtonPressEvent += ChannelsTool_MouseDown;
107 this.FocusActivated += ChannelsTool_Activated;
108 channelsBox.Changed += ChannelsBox_Changed;
109 sampleBox.Changed += SampleBox_Changed;
110 meanStackBox.Clicked += MeanStackBox_Clicked;
111
112 maxUintBox.Changed += MaxUintBox_Changed;
113 maxUintBox2.Changed += MaxUintBox2_Changed;
114
115 var st = new ListStore(typeof(string));
116 st.AppendValues(ushort.MaxValue.ToString());
117 st.AppendValues(16383);
118 st.AppendValues(4096);
119 st.AppendValues(1023);
120 st.AppendValues(byte.MaxValue.ToString());
121 maxUintBox.Model = st;
122 maxUintBox2.Model = st;
123 // Set the text column to display
124 var rend = new CellRendererText();
125 maxUintBox.PackStart(rend, false);
126 maxUintBox.AddAttribute(rend, "text", 0);
127 var rend2 = new CellRendererText();
128 maxUintBox2.PackStart(rend2, false);
129 maxUintBox2.AddAttribute(rend2, "text", 0);
130 ShowAll();
131 App.ApplyStyles(this);
132 }
133
138 private void SampleBox_Changed(object sender, EventArgs e)
139 {
140 UpdateValues();
141 }
142
148 private void MeanStackBox_Clicked(object sender, EventArgs e)
149 {
150 hist.StackHistogram = meanStackBox.Active;
151 }
152
158 private void MaxUintBox2_Changed(object sender, EventArgs e)
159 {
160 int selectedIndex = maxUintBox2.Active;
161 TreeIter iter;
162 if (maxUintBox2.Model.GetIterFromString(out iter, selectedIndex.ToString()))
163 {
164 string selectedValue = (string)maxUintBox2.Model.GetValue(iter, 0);
165 // Your code to handle the selected value
166 int i = int.Parse(selectedValue);
167 graphMax.Value = i;
168 }
169 }
170
176 private void MaxUintBox_Changed(object sender, EventArgs e)
177 {
178 int selectedIndex = maxUintBox.Active;
179 TreeIter iter;
180 if (maxUintBox.Model.GetIterFromString(out iter, selectedIndex.ToString()))
181 {
182 string selectedValue = (string)maxUintBox.Model.GetValue(iter, 0);
183 // Your code to handle the selected value
184 int i = int.Parse(selectedValue);
185 maxBox.Value = i;
186 }
187 }
188
193 private void ChannelsBox_Changed(object sender, EventArgs e)
194 {
195 UpdateValues();
196 }
197
201 private void UpdateValues()
202 {
203 if (channelsBox.Active == -1)
204 return;
205 sampleBox.Adjustment.Upper = SelectedChannel.range.Length - 1;
206 if (minBox.Adjustment.Upper < SelectedChannel.range[(int)sampleBox.Value].Min || maxBox.Adjustment.Upper < SelectedChannel.range[(int)sampleBox.Value].Max)
207 {
208 minBox.Value = 0;
209 maxBox.Value = ushort.MaxValue;
210 }
211 else
212 {
213 minBox.Value = Channels[channelsBox.Active].range[(int)sampleBox.Value].Min;
214 maxBox.Value = Channels[channelsBox.Active].range[(int)sampleBox.Value].Max;
215 }
216 if (hist != null)
217 {
218 //hist.Statistics = Channels[channelsBox.Active].statistics;
219 hist.UpdateChannel(SelectedChannel);
220 hist.UpdateView();
221 }
222 float max = float.MinValue;
223 float min = float.MaxValue;
224 foreach (Channel c in Channels)
225 {
226 for (int i = 0; i < c.SamplesPerPixel; i++)
227 {
228 if (max < c.range[i].Max)
229 max = c.range[i].Max;
230 if (min > c.range[i].Min)
231 min = c.range[i].Min;
232 }
233 }
234 graphMax.Value = max;
235 graphMinBox.Value = min;
236 fluorBox.Text = SelectedChannel.Fluor;
237 emissionBox.Value = SelectedChannel.Emission;
238 excitationBox.Value = SelectedChannel.Excitation;
239 }
240
242 private void UpdateGUI()
243 {
244 if (SelectedChannel.BitsPerPixel > 8)
245 {
246 minBox.Adjustment.Upper = ushort.MaxValue;
247 minBox.Adjustment.StepIncrement = 1;
248 minBox.Adjustment.PageIncrement = 10;
249 maxBox.Adjustment.Upper = ushort.MaxValue;
250 maxBox.Adjustment.StepIncrement = 1;
251 maxBox.Adjustment.PageIncrement = 10;
252 graphMax.Adjustment.Upper = ushort.MaxValue;
253 graphMax.Adjustment.StepIncrement = 1;
254 graphMax.Adjustment.PageIncrement = 10;
255 graphMinBox.Adjustment.Upper = ushort.MaxValue;
256 graphMinBox.Adjustment.StepIncrement = 1;
257 graphMinBox.Adjustment.PageIncrement = 10;
258 }
259 else
260 {
261 minBox.Adjustment.Upper = byte.MaxValue;
262 minBox.Adjustment.StepIncrement = 1;
263 minBox.Adjustment.PageIncrement = 10;
264 maxBox.Adjustment.Upper = byte.MaxValue;
265 maxBox.Adjustment.StepIncrement = 1;
266 maxBox.Adjustment.PageIncrement = 10;
267 graphMax.Adjustment.Upper = byte.MaxValue;
268 graphMax.Adjustment.StepIncrement = 1;
269 graphMax.Adjustment.PageIncrement = 10;
270 graphMinBox.Adjustment.Upper = byte.MaxValue;
271 graphMinBox.Adjustment.StepIncrement = 1;
272 graphMinBox.Adjustment.PageIncrement = 10;
273 }
274 minBox.Value = SelectedChannel.stats[(int)sampleBox.Value].Min;
275 minBox.Value = SelectedChannel.stats[(int)sampleBox.Value].Max;
276 emissionBox.Value =
277
278 emissionBox.Adjustment.Upper = 1000;
279 emissionBox.Adjustment.StepIncrement = 1;
280 emissionBox.Adjustment.PageIncrement = 10;
281 excitationBox.Adjustment.Upper = 1000;
282 excitationBox.Adjustment.StepIncrement = 1;
283 excitationBox.Adjustment.PageIncrement = 10;
284 binBox.Adjustment.Upper = 1000;
285 binBox.Adjustment.StepIncrement = 1;
286 binBox.Adjustment.PageIncrement = 10;
287 sampleBox.Adjustment.Upper = SelectedChannel.SamplesPerPixel-1;
288 sampleBox.Adjustment.StepIncrement = 1;
289 sampleBox.Adjustment.PageIncrement = 1;
290 }
291
298 private void ResetButton_ButtonPressEvent(object o, ButtonPressEventArgs args)
299 {
300 if (ImageView.SelectedImage.bitsPerPixel > 8)
301 ImageView.SelectedImage.StackThreshold(true);
302 else
303 ImageView.SelectedImage.StackThreshold(false);
304 App.viewer.UpdateView();
305 }
306 /*
307 private void View_ScrollEvent(object o, ScrollEventArgs args)
308 {
309 int i = 100;
310 if (maxGraphBox.Value < 255)
311 i = 10;
312 if (e.Delta > 0)
313 {
314 if (graphMax.Value + i < graphMax.Adjustment.Upper)
315 {
316 graphMax.Value += i;
317 graphMax.QueueDraw();
318 return;
319 }
320 }
321 else
322 if (graphMax.Value - i > graphMax.Adjustment.Upper)
323 {
324 graphMax.Value -= i;
325 graphMax.QueueDraw();
326 return;
327 }
328 }
329 */
330 #endregion
331
332 private HistogramControl hist = null;
333
334 public List<Channel> Channels
335 {
336 get
337 {
338 return ImageView.SelectedImage.Channels;
339 }
340 }
341 public Channel SelectedChannel
342 {
343 get
344 {
345 if (channelsBox.Active != -1)
346 return Channels[channelsBox.Active];
347 else
348 return Channels[0];
349 }
350 }
351 public int SelectedSample
352 {
353 get
354 {
355 return (int)sampleBox.Value;
356 }
357 set
358 {
359 sampleBox.Value = value;
360 }
361 }
363 public void UpdateItems()
364 {
365 channelsBox.Model = null;
366 var store = new ListStore(typeof(string));
367 // Add items to the ListStore
368 foreach (Channel item in Channels)
369 {
370 store.AppendValues(item.Name);
371 }
372 // Set the model for the ComboBox
373 channelsBox.Model = store;
374 // Set the text column to display
375 var renderer = new CellRendererText();
376 channelsBox.PackStart(renderer, false);
377 channelsBox.AddAttribute(renderer, "text", 0);
378
379 }
380
388 private void minBox_ValueChanged(object sender, EventArgs e)
389 {
390 if (channelsBox.Active == -1)
391 return;
392 if (hist != null)
393 {
394 hist.UpdateChannel(Channels[channelsBox.Active]);
395 hist.UpdateView();
396 hist.Min = (int)minBox.Value;
397 }
398 SelectedChannel.range[(int)sampleBox.Value].Min = (int)minBox.Value;
399 App.viewer.UpdateImage();
400 App.viewer.UpdateView();
401 }
409 private void maxBox_ValueChanged(object sender, EventArgs e)
410 {
411 if (channelsBox.Active == -1)
412 return;
413 if (hist != null)
414 {
415 hist.UpdateChannel(Channels[channelsBox.Active]);
416 hist.QueueDraw();
417 hist.Max = (int)maxBox.Value;
418 }
419 SelectedChannel.range[(int)sampleBox.Value].Min = (int)minBox.Value;
420 App.viewer.UpdateImage();
421 App.viewer.UpdateView();
422 }
428 private void maxUintBox_ActiveChanged(object sender, EventArgs e)
429 {
430 TreeIter it;
431 maxUintBox.GetActiveIter(out it);
432 var selectedItem = (string)maxUintBox.Model.GetValue(it, 0);
433
434 int i = int.Parse((string)selectedItem, System.Globalization.CultureInfo.InvariantCulture);
435 if(i<=maxBox.Adjustment.Upper)
436 maxBox.Value = i;
437
438 }
439
445 private void setMaxAllBut_Click(object o, ButtonPressEventArgs args)
446 {
447 foreach (Channel c in Channels)
448 {
449 for (int i = 0; i < c.range.Length; i++)
450 {
451 c.range[i].Max = (int)maxBox.Value;
452 }
453 }
454 App.viewer.UpdateView();
455 }
456
462 private void setMinAllBut_Click(object o, ButtonPressEventArgs args)
463 {
464 foreach (Channel c in Channels)
465 {
466 for (int i = 0; i < c.range.Length; i++)
467 {
468 c.range[i].Min = (int)minBox.Value;
469 }
470 }
471 App.viewer.UpdateView();
472 }
473
480 private void ChannelsTool_Activated(object? sender, EventArgs e)
481 {
482 sampleBox.Adjustment.Upper = SelectedChannel.SamplesPerPixel - 1;
483 for (int i = 0; i < SelectedChannel.range.Length; i++)
484 {
485 SelectedChannel.range[i].Min = (int)minBox.Value;
486 }
487 minBox.Value = SelectedChannel.range[(int)sampleBox.Value].Min;
488 maxBox.Value = SelectedChannel.range[(int)sampleBox.Value].Max;
489 UpdateItems();
490 hist.UpdateView();
491
492 }
493
499 private void maxUintBox2_ActiveChanged(object sender, EventArgs e)
500 {
501 TreeIter it;
502 maxUintBox.GetActiveIter(out it);
503 var selectedItem = (string)maxUintBox.Model.GetValue(it, 0);
504 int i = int.Parse((string)selectedItem, System.Globalization.CultureInfo.InvariantCulture);
505 if(i <= graphMax.Adjustment.Upper)
506 graphMax.Value = i;
507 if (i == 255)
508 {
509 graphMax.Value = 255;
510 binBox.Value = 1;
511 }
512 }
513
519 private void minGraphBox_ValueChanged(object sender, EventArgs e)
520 {
521 if (hist != null)
522 {
523 hist.GraphMin = (int)graphMinBox.Value;
524 hist.UpdateView();
525 }
526 }
527
533 private void maxGraphBox_ValueChanged(object sender, EventArgs e)
534 {
535 if (hist != null)
536 {
537 hist.GraphMax = (int)graphMax.Value;
538 hist.UpdateView();
539 }
540 }
546 private void binBox_ValueChanged(object sender, EventArgs e)
547 {
548 if (hist != null)
549 {
550 hist.Bin = (int)binBox.Value;
551 hist.UpdateView();
552 }
553 }
554
559 private void ChannelsTool_MouseDown(object sender, ButtonPressEventArgs e)
560 {
561
562 }
569 private void setMinToolStripMenuItem_Click(object o, ButtonPressEventArgs args)
570 {
571 minBox.Value = (int)hist.MouseValX;
572 }
573
580 private void setMaxToolStripMenuItem_Click(object o, ButtonPressEventArgs args)
581 {
582 maxBox.Value = (int)hist.MouseValX;
583 }
584
589 private void applyBut_Click(object o, ButtonPressEventArgs args)
590 {
591 ImageView.SelectedImage.Bake(ImageView.SelectedImage.RChannel.RangeR, ImageView.SelectedImage.GChannel.RangeG, ImageView.SelectedImage.BChannel.RangeB);
592 }
593
601 private void minToolStripMenuItem_Click(object o, ButtonPressEventArgs args)
602 {
603 minBox.Value = SelectedChannel.stats[channelsBox.Active].StackMin;
604 }
605
612 private void maxToolStripMenuItem_Click(object o, ButtonPressEventArgs args)
613 {
614 minBox.Value = SelectedChannel.stats[channelsBox.Active].StackMax;
615 }
616
624 private void medianToolStripMenuItem_Click(object o, ButtonPressEventArgs args)
625 {
626 minBox.Value = SelectedChannel.stats[channelsBox.Active].StackMedian;
627 }
628
635 private void meanToolStripMenuItem_Click(object o, ButtonPressEventArgs args)
636 {
637 minBox.Value = SelectedChannel.stats[channelsBox.Active].StackMean;
638 }
639
646 private void toolStripMenuItem1_Click(object o, ButtonPressEventArgs args)
647 {
648 maxBox.Value = SelectedChannel.stats[channelsBox.Active].StackMin;
649 }
650
657 private void toolStripMenuItem2_Click(object o, ButtonPressEventArgs args)
658 {
659 maxBox.Value = SelectedChannel.stats[channelsBox.Active].StackMax;
660 }
667 private void toolStripMenuItem3_Click(object o, ButtonPressEventArgs args)
668 {
669 maxBox.Value = SelectedChannel.stats[channelsBox.Active].StackMedian;
670 }
677 private void toolStripMenuItem4_Click(object o, ButtonPressEventArgs args)
678 {
679 maxBox.Value = SelectedChannel.stats[channelsBox.Active].StackMean;
680 }
681
687 private void updateBut_Click(object o, ButtonPressEventArgs args)
688 {
689 if (ImageView.SelectedImage.bitsPerPixel > 8)
690 {
691 ImageView.SelectedImage.StackThreshold(true);
692 if (ImageView.SelectedImage.RGBChannelCount == 1)
693 {
694 maxBox.Value = ImageView.SelectedImage.Channels[channelsBox.Active].stats[0].StackMax;
695 minBox.Value = ImageView.SelectedImage.Channels[channelsBox.Active].stats[0].StackMin;
696 }
697 else
698 {
699 maxBox.Value = ImageView.SelectedImage.Channels[channelsBox.Active].stats[channelsBox.Active].StackMax;
700 minBox.Value = ImageView.SelectedImage.Channels[channelsBox.Active].stats[channelsBox.Active].StackMin;
701 }
702 }
703 else
704 {
705 ImageView.SelectedImage.StackThreshold(false);
706 }
707 App.viewer.UpdateImage();
708 }
709
715 private void sampleBox_ValueChanged(object sender, EventArgs e)
716 {
717 if (channelsBox.Active == -1)
718 channelsBox.Active = 0;
719 UpdateValues();
720 if (hist != null)
721 {
722 //hist.Statistics = Channels[channelsBox.Active].statistics;
723 hist.UpdateChannel(SelectedChannel);
724 hist.QueueDraw();
725 }
726 App.viewer.UpdateView();
727 }
728
734 private void excitationBox_ValueChanged(object sender, EventArgs e)
735 {
736 SelectedChannel.Excitation = (int)excitationBox.Value;
737 }
738
744 private void emissionBox_ValueChanged(object sender, EventArgs e)
745 {
746 SelectedChannel.Emission = (int)emissionBox.Value;
747 }
748
754 private void fluorBox_TextChanged(object sender, EventArgs e)
755 {
756 SelectedChannel.Fluor = fluorBox.Text;
757 }
758 }
759}
void UpdateItems()
It takes the list of channels and adds them to the ComboBox.
void UpdateChannel(Channel c)
Definition ML.cs:17