2using System.Collections.Generic;
3using System.ComponentModel;
8using System.Threading.Tasks;
9using System.Windows.Forms;
11using Bitmap = System.Drawing.Bitmap;
12using Color = System.Drawing.Color;
13using PointF = System.Drawing.PointF;
14using Size = System.Drawing.Size;
15using SizeF = System.Drawing.SizeF;
16using RectangleF = System.Drawing.RectangleF;
17using RectangleD = AForge.RectangleD;
18using Rectangle = System.Drawing.Rectangle;
19using Point = System.Drawing.Point;
22 public partial class HistogramControl : UserControl
24 public HistogramControl(Channel c)
27 this.Dock = DockStyle.Fill;
28 InitializeComponent();
29 if (c.BitsPerPixel == 8)
36 graphMax = ushort.MaxValue;
41 private Channel channel =
null;
42 private float bin = 10;
43 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
56 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
63 channel.range[App.channelsTool.SelectedSample].Min = (int)value;
68 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
75 channel.range[App.channelsTool.SelectedSample].Max = (int)value;
79 private int graphMax = ushort.MaxValue;
80 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
83 get {
return graphMax; }
84 set { graphMax = value; }
86 private int graphMin = 0;
87 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
90 get {
return graphMin; }
91 set { graphMin = value; }
93 private bool stackHistogram =
true;
94 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
95 public bool StackHistogram
99 return stackHistogram;
103 stackHistogram = value;
106 private int mouseX = 0;
107 private int mouseY = 0;
122 private float mouseValX = 0;
123 private float mouseValY = 0;
124 public float MouseValX
131 public float MouseValY
139 private bool axisNumbers =
true;
140 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
141 public bool AxisNumbers
143 get {
return axisNumbers;}
144 set { axisNumbers = value; }
147 private bool axisTicks =
true;
148 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
149 public bool AxisTicks
151 get {
return axisTicks; }
152 set { axisTicks = value; }
155 private float fx = 0;
156 private float fy = 0;
158 private System.Drawing.Graphics g;
176 private void HistogramControl_Paint(
object sender, PaintEventArgs e)
179 graphMax = ushort.MaxValue;
180 g.Clear(Color.LightGray);
182 g.TranslateTransform(-graphMin, 0);
184 fx = ((float)this.Width) / ((
float)graphMax);
187 for (
int cc = 0; cc <
ImageView.SelectedImage.Channels.Count; cc++)
189 for (
int i = 0; i <
ImageView.SelectedImage.Channels[cc].stats.Length; i++)
191 float f =
ImageView.SelectedImage.Channels[cc].stats[i].StackMedian;
199 fy = ((float)this.Height) / maxmedian;
200 for (
int c = 0; c < ImageView.SelectedImage.Channels.Count; c++)
202 Channel channel = ImageView.SelectedImage.Channels[c];
203 for (
int i = 0; i < channel.range.Length; i++)
206 if (ImageView.SelectedImage.RGBChannelCount == 1)
207 stat = channel.stats[0];
209 stat = channel.stats[i];
211 Pen black =
new Pen(Color.FromArgb(35, 0, 0, 0), bin * fx);
212 Pen blackd =
new Pen(Color.FromArgb(150, 0, 0, 0), bin * fx);
217 if (channel.Emission != 0)
219 pen =
new Pen(SpectralColor(channel.Emission), bin * fx);
220 pen.Color = Color.FromArgb(light, pen.Color);
221 pend =
new Pen(SpectralColor(channel.Emission), bin * fx);
222 pend.Color = Color.FromArgb(dark, pen.Color);
228 pen =
new Pen(Color.FromArgb(light, 255, 0, 0), bin * fx);
229 pend =
new Pen(Color.FromArgb(dark, 255, 0, 0), bin * fx);
233 pen =
new Pen(Color.FromArgb(light, 0, 255, 0), bin * fx);
234 pend =
new Pen(Color.FromArgb(dark, 0, 255, 0), bin * fx);
238 pen =
new Pen(Color.FromArgb(light, 0, 0, 255), bin * fx);
239 pend =
new Pen(Color.FromArgb(dark, 0, 0, 255), bin * fx);
243 g.DrawLine(Pens.Black,
new PointF(mouseX, 0),
new PointF(mouseX, this.Height));
245 if (App.Image.bitsPerPixel <= 8)
253 PointF? prevs =
null;
255 for (
float x = 0; x < gmax; x++)
257 if (StackHistogram && c == ImageView.SelectedImage.Channels.Count - 1)
260 float val = (float)ImageView.SelectedImage.Statistics.StackValues[(
int)x];
264 float v = sumbin / binind;
265 float yy = this.Height - (fy * v);
268 g.DrawLine(blackd, prevs.Value,
new PointF(fx * x, yy));
270 g.DrawLine(black,
new PointF(fx * x, this.Height),
new PointF(fx * x, yy));
271 prevs =
new PointF(fx * x, yy);
277 float rv = stat.StackValues[(int)x];
281 g.DrawLine(pen,
new PointF(fx * x, this.Height),
new PointF(fx * x, this.Height - (fy * (sumbins / bininds))));
284 g.DrawLine(pend, prev.Value,
new PointF(fx * x, this.Height - (fy * (sumbins / bininds))));
286 prev =
new PointF(fx * x, this.Height - (fy * (sumbins / bininds)));
295 g.DrawLine(pend,
new PointF((fx * channel.range[i].Max), 0),
new PointF((fx * channel.range[i].Max),
this.Height));
296 g.DrawLine(pend,
new PointF(fx * channel.range[i].Min, 0),
new PointF(fx * channel.range[i].Min,
this.Height));
311 for (
float x = 0; x < graphMax; x += 2000)
313 SizeF s = g.MeasureString(x.ToString(), SystemFonts.DefaultFont);
314 g.DrawString(x.ToString(), SystemFonts.DefaultFont, Brushes.Black, (fx * x) - (s.Width/2), tick + 6);
315 g.DrawLine(Pens.Black,
new PointF((fx * x), 0),
new PointF((fx * x), tick + 3));
317 for (
float x = 0; x < graphMax; x += 1000)
319 g.DrawLine(Pens.Black,
new PointF((fx * x), 0),
new PointF((fx * x), tick));
322 if (graphMax <= 16383)
323 for (
float x = 0; x < graphMax; x += 100)
325 g.DrawLine(Pens.Black,
new PointF((fx * x), 0),
new PointF((fx * x), tick));
329 for (
float x = 0; x < graphMax; x += 50)
331 g.DrawLine(Pens.Black,
new PointF((fx * x), 0),
new PointF((fx * x), 4));
333 for (
float x = 0; x < graphMax; x += 10)
335 g.DrawLine(Pens.Black,
new PointF((fx * x), 0),
new PointF((fx * x), 2));
340 if (ImageView.SelectedImage.RGBChannelCount == 3)
342 if (ImageView.SelectedImage.Channels.Count > 2)
345 "(" + (mouseX / fx).ToString() +
346 ",R:" + ImageView.SelectedImage.Channels[2].stats[2].StackValues[(int)(mouseX / fx)].ToString() +
347 ",G:" + ImageView.SelectedImage.Channels[1].stats[1].StackValues[(int)(mouseX / fx)].ToString() +
348 ",B:" + ImageView.SelectedImage.Channels[0].stats[0].StackValues[(int)(mouseX / fx)].ToString() +
")";
353 "(" + (mouseX / fx).ToString() +
354 ",R:" + ImageView.SelectedImage.Channels[0].stats[2].StackValues[(int)(mouseX / fx)].ToString() +
355 ",G:" + ImageView.SelectedImage.Channels[0].stats[1].StackValues[(int)(mouseX / fx)].ToString() +
356 ",B:" + ImageView.SelectedImage.Channels[0].stats[0].StackValues[(int)(mouseX / fx)].ToString() +
")";
361 int x = (int)(mouseX / fx);
362 if (ImageView.SelectedImage.bitsPerPixel < 8 && ImageView.SelectedImage.Channels[0].stats[0].StackValues.Length < 255)
363 st =
"(" + (mouseX / fx).ToString() +
"," + ImageView.SelectedImage.Channels[0].stats[0].StackValues[x].ToString() +
")";
366 SizeF sf = g.MeasureString(st, SystemFonts.DefaultFont);
367 g.DrawString(st, SystemFonts.DefaultFont, Brushes.Black, mouseX, mouseY + sf.Height);
369 e.Graphics.DrawImage(bm, 0, 0);
376 private void HistogramControl_MouseDown(
object sender, MouseEventArgs e)
388 private void HistogramControl_MouseMove(
object sender, MouseEventArgs e)
390 mouseValX = e.X / fx;
391 mouseValY = e.Y / fy;
398 private void HistogramControl_SizeChanged(
object sender, EventArgs e)
406 private void InitGraphics()
408 if (Width == 0 || Height == 0)
410 bm =
new Bitmap(Width, Height);
413 g = System.Drawing.Graphics.FromImage(bm);
420 private void copyViewToClipboardToolStripMenuItem_Click(
object sender, EventArgs e)
422 Clipboard.SetImage(bm);
430 private void setMaxToolStripMenuItem_Click(
object sender, EventArgs e)
432 App.channelsTool.SelectedChannel.range[App.channelsTool.SelectedSample].Max = (int)MouseValX;
434 App.viewer.UpdateImage();
442 private void setMinToolStripMenuItem_Click(
object sender, EventArgs e)
444 App.channelsTool.SelectedChannel.range[App.channelsTool.SelectedSample].Min = (int)MouseValX;
446 App.viewer.UpdateImage();
453 private void setMaxAllToolStripMenuItem_Click(
object sender, EventArgs e)
455 foreach (Channel c
in ImageView.SelectedImage.Channels)
457 for (
int i = 0; i < c.range.Length; i++)
459 c.range[i].Max = (int)MouseValX;
468 private void setMinAllToolStripMenuItem_Click(
object sender, EventArgs e)
470 foreach (Channel c
in ImageView.SelectedImage.Channels)
472 for (
int i = 0; i < c.range.Length; i++)
474 c.range[i].Min = (int)MouseValX;
483 Color SpectralColor(
double l)
489 if ((l >= 400.0) && (l < 410.0)) { t = (l - 400.0) / (410.0 - 400.0); r = +(0.33 * t) - (0.20 * t * t); }
490 else if ((l >= 410.0) && (l < 475.0)) { t = (l - 410.0) / (475.0 - 410.0); r = 0.14 - (0.13 * t * t); }
491 else if ((l >= 545.0) && (l < 595.0)) { t = (l - 545.0) / (595.0 - 545.0); r = +(1.98 * t) - (t * t); }
492 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); }
493 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); }
494 if ((l >= 415.0) && (l < 475.0)) { t = (l - 415.0) / (475.0 - 415.0); g = +(0.80 * t * t); }
495 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); }
496 else if ((l >= 585.0) && (l < 639.0)) { t = (l - 585.0) / (639.0 - 585.0); g = 0.84 - (0.84 * t); }
497 if ((l >= 400.0) && (l < 475.0)) { t = (l - 400.0) / (475.0 - 400.0); b = +(2.20 * t) - (1.50 * t * t); }
498 else if ((l >= 475.0) && (l < 560.0)) { t = (l - 475.0) / (560.0 - 475.0); b = 0.7 - (t) + (0.30 * t * t); }
502 return Color.FromArgb(255, (
int)r, (
int)g, (
int)b);
void UpdateChannel(Channel c)
ImageView control for image stacks, pyramidal and whole-slide-images.