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
HistogramControl.cs
1using Gtk;
2using System;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using AForge;
10
11using Cairo;
12using Color = AForge.Color;
13using Gdk;
14using System.IO;
15
16namespace BioGTK
17{
18 public class HistogramControl : Gtk.Window
19 {
20
21 #region Properties
22
24 private Builder _builder;
25
26#pragma warning disable 649
27 [Builder.Object]
28 private MenuItem setMin;
29 [Builder.Object]
30 private MenuItem setMax;
31 [Builder.Object]
32 private MenuItem setMinAll;
33 [Builder.Object]
34 private MenuItem setMaxAll;
35 [Builder.Object]
36 private DrawingArea view;
37 [Builder.Object]
38 private Menu menu;
39#pragma warning restore 649
40
41 #endregion
42
43 #region Constructors / Destructors
44 public static HistogramControl Create(Channel channel)
45 {
46 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/Histogram.glade", FileMode.Open));
47 return new HistogramControl(builder, builder.GetObject("histogram").Handle,channel);
48 }
49 protected HistogramControl(Builder builder, IntPtr handle, Channel channel) : base(handle)
50 {
51 _builder = builder;
52 this.channel = channel;
53 builder.Autoconnect(this);
54 Init();
55 }
56 #endregion
57
58 public Channel channel = null;
60 public void Init()
61 {
62 if (ImageView.SelectedImage.bitsPerPixel == 8)
63 {
64 graphMax = 255;
65 Bin = 1;
66 }
67 else
68 {
69 graphMax = ushort.MaxValue;
70 Bin = 1;
71 }
72 this.Drawn += HistogramControl_Drawn;
73 this.MotionNotifyEvent += HistogramControl_MotionNotifyEvent;
74 this.ButtonPressEvent += HistogramControl_ButtonPressEvent;
75 this.ScrollEvent += HistogramControl_ScrollEvent;
76 this.DeleteEvent += HistogramControl_DeleteEvent;
77 setMin.ButtonPressEvent += SetMin_ButtonPressEvent;
78 setMax.ButtonPressEvent += SetMax_ButtonPressEvent;
79 setMinAll.ButtonPressEvent += SetMinAll_ButtonPressEvent;
80 setMaxAll.ButtonPressEvent += SetMaxAll_ButtonPressEvent;
81 this.AddEvents((int)
82 (EventMask.ButtonPressMask
83 | EventMask.ButtonReleaseMask
84 | EventMask.KeyPressMask
85 | EventMask.PointerMotionMask | EventMask.ScrollMask));
86 }
87
95 private void HistogramControl_DeleteEvent(object o, DeleteEventArgs args)
96 {
97 args.RetVal = true;
98 Hide();
99 }
100
106 private void HistogramControl_ScrollEvent(object o, ScrollEventArgs args)
107 {
108 if(args.Event.Direction == Gdk.ScrollDirection.Up)
109 {
110 graphMax += 400;
111 }
112 else if (args.Event.Direction == Gdk.ScrollDirection.Down)
113 {
114 graphMax -= 400;
115 }
116 UpdateView();
117 }
118
124 private void SetMaxAll_ButtonPressEvent(object o, ButtonPressEventArgs args)
125 {
126 foreach (Channel c in ImageView.SelectedImage.Channels)
127 {
128 for (int i = 0; i < c.range.Length; i++)
129 {
130 c.range[i].Max = (int)MouseValX;
131 }
132 }
133 this.QueueDraw();
134 App.viewer.UpdateImage();
135 }
136
142 private void SetMinAll_ButtonPressEvent(object o, ButtonPressEventArgs args)
143 {
144 foreach (Channel c in ImageView.SelectedImage.Channels)
145 {
146 for (int i = 0; i < c.range.Length; i++)
147 {
148 c.range[i].Min = (int)MouseValX;
149 }
150 }
151 this.QueueDraw();
152 App.viewer.UpdateImage();
153 }
154
161 private void SetMax_ButtonPressEvent(object o, ButtonPressEventArgs args)
162 {
163 App.channelsTool.SelectedChannel.range[App.channelsTool.SelectedSample].Max = (int)MouseValX;
164 this.QueueDraw();
165 App.viewer.UpdateImage();
166 }
167
174 private void SetMin_ButtonPressEvent(object o, ButtonPressEventArgs args)
175 {
176 App.channelsTool.SelectedChannel.range[App.channelsTool.SelectedSample].Min = (int)MouseValX;
177 this.QueueDraw();
178 App.viewer.UpdateImage();
179 }
180
185 public void HistogramControl_ButtonPressEvent(object o, ButtonPressEventArgs args)
186 {
187 mouseX = (int)args.Event.X;
188 mouseY = (int)args.Event.Y;
189 this.QueueDraw();
190 if(args.Event.Button == 3)
191 menu.Popup();
192 }
193
199 public void HistogramControl_MotionNotifyEvent(object o, MotionNotifyEventArgs args)
200 {
201 mouseValX = (float)args.Event.X / fx;
202 mouseValY = (float)args.Event.Y / fy;
203 }
204
205 public Cairo.Color ToCairoColor(Color c)
206 {
207 return new Cairo.Color((double)c.R / 255.0, (double)c.G / 255.0, (double)c.B / 255.0, (double)c.A / 255.0);
208 }
209
214 private void HistogramControl_Drawn(object o, DrawnArgs args)
215 {
216 Cairo.Context g = args.Cr;
217 if (graphMax == 0)
218 graphMax = ushort.MaxValue;
219 g.SetSourceColor(ToCairoColor(Color.LightGray));
220 //g.Restore();
221 //g.Translate(-graphMin, 0);
222 g.LineWidth = 1;
223 string st = "";
224 fx = ((float)this.AllocatedWidth) / ((float)graphMax);
225 float maxmedian = 0;
226 int maxChan = 0;
227 for (int cc = 0; cc < ImageView.SelectedImage.Channels.Count; cc++)
228 {
229 for (int i = 0; i < ImageView.SelectedImage.Channels[cc].stats.Length; i++)
230 {
231 float f = ImageView.SelectedImage.Channels[cc].stats[i].StackMedian;
232 if (maxmedian < f)
233 {
234 maxmedian = f;
235 maxChan = cc;
236 }
237 }
238 }
239 fy = ((float)this.AllocatedHeight) / maxmedian;
240 for (int c = 0; c < ImageView.SelectedImage.Channels.Count; c++)
241 {
242 //We draw the mouse line
243
244 g.SetSourceColor(ToCairoColor(Color.Black));
245 g.MoveTo(mouseX, 0);
246 g.LineTo(mouseX, this.AllocatedHeight);
247 g.Stroke();
248 int gmax = graphMax;
249 int gmin = graphMin;
250 Channel channel = ImageView.SelectedImage.Channels[c];
251 for (int i = 0; i < channel.range.Length; i++)
252 {
253 Statistics stat;
254 if (ImageView.SelectedImage.RGBChannelCount == 1)
255 stat = channel.stats[0];
256 else
257 stat = channel.stats[i];
258 g.LineWidth = 1;// bin * fx;
259 Cairo.Color black = ToCairoColor(Color.FromArgb(35, 0, 0, 0));
260 Cairo.Color blackd = ToCairoColor(Color.FromArgb(150, 0, 0, 0));
261 Cairo.Color pen = new Cairo.Color();
262 Cairo.Color pend = new Cairo.Color();
263 int dark = 100;
264 int light = 75;
265
266
267 if (ImageView.SelectedImage.bitsPerPixel <= 8)
268 gmax = 255;
269
270 if (channel.Emission != 0)
271 {
272 Cairo.Color cc = ToCairoColor(SpectralColor(channel.Emission));
273 cc.A = 0.2;
274 pen = cc;
275 cc.A = 0.6;
276 pend = cc;
277 }
278 else
279 {
280 if (i == 0)
281 {
282 pen = ToCairoColor(Color.FromArgb(light, 255, 0, 0));
283 pend = ToCairoColor(Color.FromArgb(dark, 255, 0, 0));
284 }
285 else if (i == 1)
286 {
287 pen = ToCairoColor(Color.FromArgb(light, 0, 255, 0));
288 pend = ToCairoColor(Color.FromArgb(dark, 0, 255, 0));
289 }
290 else
291 {
292 pen = ToCairoColor(Color.FromArgb(light, 0, 0, 255));
293 pend = ToCairoColor(Color.FromArgb(dark, 0, 0, 255));
294 }
295 }
296
297 float sumbins = 0;
298 float sumbin = 0;
299 int binind = 0;
300 int bininds = 0;
301 PointF? prevs = null;
302 PointF? prev = null;
303 float f = (float)(gmax - gmin) / (float)this.AllocatedWidth;
304 g.SetSourceColor(pen);
305 for (float x = gmin; x < gmax; x+=f)
306 {
307 if (StackHistogram && c == ImageView.SelectedImage.Channels.Count - 1)
308 {
309 //Lets draw the stack histogram.
310 float val = (float)ImageView.SelectedImage.Statistics.StackValues[(int)x];
311 sumbin += val;
312 if (binind == bin)
313 {
314 float v = sumbin / binind;
315 float yy = this.AllocatedHeight - (fy * v);
316 if (prevs != null)
317 {
318 g.SetSourceColor(blackd);
319 g.MoveTo(prevs.Value.X, prevs.Value.Y);
320 g.LineTo(fx * x, yy);
321 g.Stroke();
322 }
323 g.SetSourceColor(black);
324 g.MoveTo(fx * x, this.AllocatedHeight);
325 g.LineTo(fx * x, yy);
326 g.Stroke();
327 prevs = new PointF(fx * x, yy);
328 binind = 0;
329 sumbin = 0;
330 }
331 }
332 //Lets draw the channel histogram on top of the stack histogram.
333 float rv = stat.StackValues[(int)x];
334 sumbins += rv;
335 if (bininds == bin)
336 {
337 g.SetSourceColor(pend);
338 g.MoveTo(fx * x, this.AllocatedHeight);
339 g.LineTo(fx * x, this.AllocatedHeight - (fy * (sumbins / bininds)));
340 g.Stroke();
341 if (prev != null)
342 {
343 g.MoveTo(prev.Value.X,prev.Value.Y);
344 g.LineTo(fx * x, this.AllocatedHeight - (fy * (sumbins / bininds)));
345 g.Stroke();
346 }
347 prev = new PointF(fx * x, this.AllocatedHeight - (fy * (sumbins / bininds)));
348 bininds = 0;
349 sumbins = 0;
350 }
351 binind++;
352 bininds++;
353
354 }
355 g.SetSourceColor(ToCairoColor(SpectralColor(channel.Emission)));
356 g.MoveTo((fx * channel.range[i].Max), 0);
357 g.LineTo((fx * channel.range[i].Max), this.AllocatedHeight);
358 g.Stroke();
359 g.MoveTo((fx * channel.range[i].Min), 0);
360 g.LineTo((fx * channel.range[i].Min), this.AllocatedHeight);
361 g.Stroke();
362 }
363
364 }
365
366 float tick = 6;
367 if (axisTicks)
368 {
369 g.SetSourceRGB(0, 0, 0);
370 if (axisNumbers)
371 {
372 if (graphMax <= 255)
373 {
374 for (float x = 0; x < graphMax; x += 50)
375 {
376 TextExtents ex = g.TextExtents(x.ToString());
377 g.MoveTo((fx * x) - (ex.Width / 2), tick + 10);
378 g.ShowText(x.ToString());
379 g.Stroke();
380 g.MoveTo((fx * x), 0);
381 g.LineTo((fx * x), tick);
382 g.Stroke();
383 }
384 }
385 else
386 {
387 for (float x = 0; x < graphMax; x += 2000)
388 {
389 g.MoveTo((fx * x), 0);
390 g.LineTo((fx * x), tick + 3);
391 g.Stroke();
392 }
393 for (float x = 0; x < graphMax; x += 1000)
394 {
395 TextExtents ex = g.TextExtents(x.ToString());
396 g.MoveTo((fx * x) - (ex.Width / 2), tick + 10);
397 g.ShowText(x.ToString());
398 g.Stroke();
399 g.MoveTo((fx * x), 0);
400 g.LineTo((fx * x), tick);
401 g.Stroke();
402 }
403 }
404 }
405 if (graphMax <= 16383)
406 for (float x = 0; x < graphMax; x += 100)
407 {
408 g.MoveTo((fx * x), 0);
409 g.LineTo((fx * x), tick);
410 g.Stroke();
411 }
412 if (graphMax <= 255)
413 {
414 for (float x = 0; x < graphMax; x += 50)
415 {
416 g.MoveTo((fx * x), 0);
417 g.LineTo((fx * x), 4);
418 g.Stroke();
419 }
420 for (float x = 0; x < graphMax; x += 10)
421 {
422 g.MoveTo((fx * x), 0);
423 g.LineTo((fx * x), 2);
424 g.Stroke();
425 }
426 }
427 }
428
429 if (ImageView.SelectedImage.RGBChannelCount == 3)
430 {
431 if (ImageView.SelectedImage.Channels.Count > 2)
432 {
433 st =
434 "(" + (mouseX / fx).ToString() +
435 ",R:" + ImageView.SelectedImage.Channels[2].stats[2].StackValues[(int)(mouseX / fx)].ToString() +
436 ",G:" + ImageView.SelectedImage.Channels[1].stats[1].StackValues[(int)(mouseX / fx)].ToString() +
437 ",B:" + ImageView.SelectedImage.Channels[0].stats[0].StackValues[(int)(mouseX / fx)].ToString() + ")";
438 }
439 else
440 {
441 st =
442 "(" + (mouseX / fx).ToString() +
443 ",R:" + ImageView.SelectedImage.Channels[0].stats[2].StackValues[(int)(mouseX / fx)].ToString() +
444 ",G:" + ImageView.SelectedImage.Channels[0].stats[1].StackValues[(int)(mouseX / fx)].ToString() +
445 ",B:" + ImageView.SelectedImage.Channels[0].stats[0].StackValues[(int)(mouseX / fx)].ToString() + ")";
446 }
447 }
448 else
449 {
450 int x = (int)(mouseX / fx);
451 if (ImageView.SelectedImage.bitsPerPixel < 8 && ImageView.SelectedImage.Channels[0].stats[0].StackValues.Length < 255)
452 st = "(" + (mouseX / fx).ToString() + "," + ImageView.SelectedImage.Channels[0].stats[0].StackValues[x].ToString() + ")";
453
454 }
455 g.MoveTo(mouseX, mouseY);
456 g.ShowText(st);
457 g.Stroke();
458 }
459
460 private float bin = 10;
461 public float Bin
462 {
463 get
464 {
465 return bin;
466 }
467 set
468 {
469 bin = value;
470 }
471 }
472 private int min = 0;
473 public float Min
474 {
475 get { return min; }
476 set
477 {
478 if(channel!=null)
479 channel.range[App.channelsTool.SelectedSample].Min = (int)value;
480 min = (int)value;
481 }
482 }
483 private int max = 0;
484 public float Max
485 {
486 get { return max; }
487 set
488 {
489 if (channel != null)
490 channel.range[App.channelsTool.SelectedSample].Max = (int)value;
491 max = (int)value;
492 }
493 }
494 private int graphMax = ushort.MaxValue;
495 public int GraphMax
496 {
497 get { return graphMax; }
498 set { graphMax = value; }
499 }
500 private int graphMin = 0;
501 public int GraphMin
502 {
503 get { return graphMin; }
504 set { graphMin = value; }
505 }
506 private bool stackHistogram = true;
507 public bool StackHistogram
508 {
509 get
510 {
511 return stackHistogram;
512 }
513 set
514 {
515 stackHistogram = value;
516 }
517 }
518 private int mouseX = 0;
519 private int mouseY = 0;
520 public int MouseX
521 {
522 get
523 {
524 return mouseX;
525 }
526 }
527 public int MouseY
528 {
529 get
530 {
531 return mouseX;
532 }
533 }
534 private float mouseValX = 0;
535 private float mouseValY = 0;
536 public float MouseValX
537 {
538 get
539 {
540 return mouseValX;
541 }
542 }
543 public float MouseValY
544 {
545 get
546 {
547 return mouseValY;
548 }
549 }
550
551 private bool axisNumbers = true;
552 public bool AxisNumbers
553 {
554 get { return axisNumbers;}
555 set { axisNumbers = value; }
556 }
557
558 private bool axisTicks = true;
559 public bool AxisTicks
560 {
561 get { return axisTicks; }
562 set { axisTicks = value; }
563 }
564
565 private float fx = 0;
566 private float fy = 0;
567 private AForge.Bitmap bm;
568
572 public void UpdateChannel(Channel c)
573 {
574 channel = c;
575 }
579 public void UpdateView()
580 {
581 view.QueueDraw();
582 }
583
589 Color SpectralColor(double l) // RGB <0,1> <- lambda l <400,700> [nm]
590 {
591 double t;
592 double r = 0;
593 double g = 0;
594 double b = 0;
595 if ((l >= 400.0) && (l < 410.0)) { t = (l - 400.0) / (410.0 - 400.0); r = +(0.33 * t) - (0.20 * t * t); }
596 else if ((l >= 410.0) && (l < 475.0)) { t = (l - 410.0) / (475.0 - 410.0); r = 0.14 - (0.13 * t * t); }
597 else if ((l >= 545.0) && (l < 595.0)) { t = (l - 545.0) / (595.0 - 545.0); r = +(1.98 * t) - (t * t); }
598 else if ((l >= 595.0) && (l < 650.0)) { t = (l - 595.0) / (650.0 - 595.0); r = 0.98 + (0.06 * t) - (0.40 * t * t); }
599 else if ((l >= 650.0) && (l < 700.0)) { t = (l - 650.0) / (700.0 - 650.0); r = 0.65 - (0.84 * t) + (0.20 * t * t); }
600 if ((l >= 415.0) && (l < 475.0)) { t = (l - 415.0) / (475.0 - 415.0); g = +(0.80 * t * t); }
601 else if ((l >= 475.0) && (l < 590.0)) { t = (l - 475.0) / (590.0 - 475.0); g = 0.8 + (0.76 * t) - (0.80 * t * t); }
602 else if ((l >= 585.0) && (l < 639.0)) { t = (l - 585.0) / (639.0 - 585.0); g = 0.84 - (0.84 * t); }
603 if ((l >= 400.0) && (l < 475.0)) { t = (l - 400.0) / (475.0 - 400.0); b = +(2.20 * t) - (1.50 * t * t); }
604 else if ((l >= 475.0) && (l < 560.0)) { t = (l - 475.0) / (560.0 - 475.0); b = 0.7 - (t) + (0.30 * t * t); }
605 r *= 255;
606 g *= 255;
607 b *= 255;
608 return Color.FromArgb(255, (int)r, (int)g, (int)b);
609 }
610 }
611}
void HistogramControl_ButtonPressEvent(object o, ButtonPressEventArgs args)
void Init()
This function initializes the histogram control.
void HistogramControl_MotionNotifyEvent(object o, MotionNotifyEventArgs args)
void UpdateChannel(Channel c)
Definition ML.cs:17