2using System.Collections;
3using System.Collections.Generic;
4using System.ComponentModel;
9using System.Threading.Tasks;
10using System.Windows.Forms;
12using AForge.Imaging.Filters;
14using AForge.Math.Geometry;
18using Rectangle = AForge.Rectangle;
19using Bitmap = AForge.Bitmap;
25 public partial class Tools : Form
27 public static bool applyToStack =
false;
29 public static bool rEnabled =
true;
30 public static bool gEnabled =
true;
31 public static bool bEnabled =
true;
32 private static ColorS drawColor =
new ColorS(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue);
33 private static ColorS eraseColor =
new ColorS(0, 0, 0);
34 private static int width = 1;
36 public static System.Drawing.Rectangle selectionRectangle;
37 public static Hashtable tools =
new Hashtable();
75 public static void Init()
79 foreach (
Tool.Type tool in (
Tool.Type[])Enum.GetValues(typeof(
Tool.Type)))
81 tools.Add(tool.ToString(),
new Tool(tool,
new ColorS(0, 0, 0)));
85 public List<AForge.Point> Points;
86 public ToolType toolType;
87 private RectangleD rect;
88 public RectangleD Rectangle
93 public AForge.RectangleF RectangleF
95 get {
return new AForge.RectangleF((
float)rect.X, (
float)rect.Y, (
float)rect.W, (
float)rect.H); }
99 public ColorS tolerance;
102 tolerance =
new ColorS(0, 0, 0);
107 tolerance =
new ColorS(0, 0, 0);
109 public Tool(Type t, ColorS col)
112 tolerance =
new ColorS(0, 0, 0);
114 public Tool(Type t, RectangleD r)
118 tolerance =
new ColorS(0, 0, 0);
120 public Tool(Type t,
string sc)
124 tolerance =
new ColorS(0, 0, 0);
126 public override string ToString()
128 return type.ToString();
131 public static Tool currentTool;
132 public static ColorS DrawColor
143 public static ColorS EraseColor
155 public static int StrokeWidth
164 App.tools.UpdateGUI();
168 public static RectangleD selectionRect;
173 InitializeComponent();
175 ColorS col =
new ColorS(ushort.MaxValue);
177 currentTool = GetTool(Tool.Type.move);
178 ROI.selectBoxSize = 1;
183 public static Tool GetTool(
string name)
185 return (Tool)tools[name];
187 public static Tool GetTool(Tool.Type typ)
189 return (Tool)tools[typ.ToString()];
191 public void UpdateOverlay()
195 public void UpdateView()
203 foreach (Control item
in this.Controls)
205 if (item.Tag !=
null)
206 if (item.Tag.ToString() ==
"tool")
207 item.BackColor = System.Drawing.Color.White;
211 private void UpdateGUI()
215 color1Box.BackColor = System.Drawing.Color.FromArgb((
int)((
float)DrawColor.R / (
float)ushort.MaxValue) * 255,(
int)((
float)DrawColor.G / (
float)ushort.MaxValue) * 255, (
int)((
float)DrawColor.B / (
float)ushort.MaxValue) * 255);
216 color2Box.BackColor = System.Drawing.Color.FromArgb((
int)(((
float)EraseColor.R / (
float)ushort.MaxValue) * 255), (
int)(((
float)EraseColor.G / (
float)ushort.MaxValue) * 255), (
int)(((
float)EraseColor.B / (
float)ushort.MaxValue) * 255));
218 widthBox.Value = width;
220 public static float selectBoxSize = 1;
221 ROI anno =
new ROI();
250 if (
App.viewer ==
null || currentTool ==
null ||
ImageView.SelectedImage ==
null)
255 p =
ImageView.SelectedImage.ToImageSpace(e);
256 if (currentTool.type ==
Tool.Type.line && buts == MouseButtons.Left)
258 if (anno.GetPointCount() == 0)
261 anno.type = ROI.Type.Line;
262 anno.AddPoint(
new PointD(e.X, e.Y));
263 anno.AddPoint(
new PointD(e.X, e.Y));
265 ImageView.SelectedImage.Annotations.Add(anno);
269 if (currentTool.type ==
Tool.Type.polygon && buts == MouseButtons.Left)
271 if (anno.GetPointCount() == 0)
274 anno.type = ROI.Type.Polygon;
275 anno.AddPoint(
new PointD(e.X, e.Y));
277 ImageView.SelectedImage.Annotations.Add(anno);
282 RectangleD d =
new RectangleD(e.X, e.Y, ROI.selectBoxSize, ROI.selectBoxSize);
283 if (d.IntersectsWith(anno.Points[0]))
290 anno.AddPoint(
new PointD(e.X, e.Y));
295 if (currentTool.type ==
Tool.Type.freeform && buts == MouseButtons.Left)
297 if (anno.GetPointCount() == 0)
300 anno.type = ROI.Type.Freeform;
301 anno.AddPoint(
new PointD(e.X, e.Y));
304 ImageView.SelectedImage.Annotations.Add(anno);
308 anno.AddPoint(
new PointD(e.X, e.Y));
312 if (currentTool.type ==
Tool.Type.rect && buts == MouseButtons.Left)
314 anno.type = ROI.Type.Rectangle;
315 anno.BoundingBox =
new RectangleD(e.X, e.Y, 1, 1);
317 ImageView.SelectedImage.Annotations.Add(anno);
320 if (currentTool.type ==
Tool.Type.ellipse && buts == MouseButtons.Left)
322 anno.type = ROI.Type.Ellipse;
323 anno.BoundingBox =
new RectangleD(e.X, e.Y, 1, 1);
325 ImageView.SelectedImage.Annotations.Add(anno);
328 if (currentTool.type ==
Tool.Type.delete && buts == MouseButtons.Left)
330 for (
int i = 0; i <
ImageView.SelectedImage.Annotations.Count; i++)
332 ROI an =
ImageView.SelectedImage.Annotations[i];
333 if (an.BoundingBox.IntersectsWith(
new RectangleD(e.X, e.Y, 1, 1)))
335 if (an.selectedPoints.Count == 0)
337 ImageView.SelectedImage.Annotations.Remove(an);
341 if (an.selectedPoints.Count == 1 && !(an.type == ROI.Type.Polygon || an.type == ROI.Type.Polyline || an.type == ROI.Type.Freeform))
343 ImageView.SelectedImage.Annotations.Remove(an);
348 if (an.type == ROI.Type.Polygon ||
349 an.type == ROI.Type.Polyline ||
350 an.type == ROI.Type.Freeform)
353 an.RemovePoints(an.selectedPoints.ToArray());
362 if (currentTool.type ==
Tool.Type.text && buts == MouseButtons.Left)
365 an.type = ROI.Type.Label;
366 an.AddPoint(
new PointD(e.X, e.Y));
369 if (ti.ShowDialog() != DialogResult.OK)
371 an.family = font.FontFamily.Name;
372 an.fontSize = ti.font.Size;
373 an.strokeColor = AForge.Color.FromArgb(ti.color.A, ti.color.R, ti.color.G, ti.color.B);
374 an.Text = ti.TextValue;
375 ImageView.SelectedImage.Annotations.Add(an);
378 if (buts == MouseButtons.Middle)
380 currentTool = GetTool(
Tool.Type.pan);
382 panPanel.BackColor = System.Drawing.Color.LightGray;
394 public void ToolUp(PointD e, MouseButtons buts)
396 PointD p =
new PointD();
399 p =
ImageView.SelectedImage.ToImageSpace(e);
400 if (
App.viewer ==
null || currentTool ==
null ||
ImageView.SelectedImage ==
null || anno ==
null)
403 if (currentTool.type ==
Tool.Type.point && buts == MouseButtons.Left)
406 an.AddPoint(
new PointD(e.X, e.Y));
407 an.type = ROI.Type.Point;
409 ImageView.SelectedImage.Annotations.Add(an);
412 if (currentTool.type ==
Tool.Type.line && anno.type == ROI.Type.Line && buts == MouseButtons.Left)
414 if (anno.GetPointCount() > 0)
416 anno.UpdatePoint(
new PointD(e.X, e.Y), 1);
421 if (currentTool.type ==
Tool.Type.rect && anno.type == ROI.Type.Rectangle && buts == MouseButtons.Left)
423 if (anno.GetPointCount() == 4)
429 if (currentTool.type ==
Tool.Type.ellipse && anno.type == ROI.Type.Ellipse && buts == MouseButtons.Left)
431 if (anno.GetPointCount() == 4)
437 if (currentTool.type ==
Tool.Type.freeform && anno.type == ROI.Type.Freeform && buts == MouseButtons.Left)
442 if (currentTool.type ==
Tool.Type.rectSel && buts == MouseButtons.Left)
446 RectangleD r = GetTool(
Tool.Type.rectSel).Rectangle;
447 foreach (ROI an
in App.viewer.AnnotationsRGB)
449 if (an.GetSelectBound(
App.viewer.
GetScale(),
App.viewer.
GetScale()).ToRectangleF().IntersectsWith(r.ToRectangleF()))
452 an.selectedPoints.Clear();
455 RectangleD[] sels = an.GetSelectBoxes(selectBoxSize);
456 for (
int i = 0; i < sels.Length; i++)
458 if (sels[i].IntersectsWith(r))
460 an.selectedPoints.Add(i);
467 Tools.GetTool(
Tools.
Tool.Type.rectSel).Rectangle =
new RectangleD(0, 0, 0, 0);
470 if (
Tools.currentTool.type ==
Tools.
Tool.Type.magic && buts == MouseButtons.Left)
473 AForge.RectangleF rr =
ImageView.SelectedImage.ToImageSpace(r);
476 if (
ImageView.SelectedImage.Buffers[0].RGBChannelsCount > 1)
480 bf.Crop(rr.ToRectangleInt());
481 Statistics[] sts = Statistics.FromBytes(bf);
482 Statistics st = sts[0];
484 if (magicSel.Numeric)
486 th = magicSel.Threshold;
489 if (magicSel.Index == 2)
490 th = (int)(st.Min + st.Mean);
492 if (magicSel.Index == 1)
497 if (bf.BitsPerPixel > 8)
508 BlobCounter blobCounter =
new BlobCounter(det);
509 blobCounter.FilterBlobs =
true;
510 blobCounter.MinWidth = 2;
511 blobCounter.MinHeight = 2;
512 blobCounter.CoupledSizeFiltering =
true;
513 Blob[] blobs = blobCounter.GetObjects(det,
true);
514 double px =
ImageView.SelectedImage.PhysicalSizeX;
515 double py =
ImageView.SelectedImage.PhysicalSizeY;
516 foreach (Blob blob
in blobs)
518 AForge.RectangleD rd =
new RectangleD(blob.Rectangle.X * px, blob.Rectangle.Y * py, blob.Rectangle.Width * px, blob.Rectangle.Height * py);
519 PointD loc =
new PointD(r.X + rd.X, r.Y + rd.Y);
520 ROI an = ROI.CreateRectangle(coord, loc.X, loc.Y, rd.W, rd.H);
521 ImageView.SelectedImage.Annotations.Add(an);
525 if (
Tools.currentTool.type ==
Tools.
Tool.Type.bucket && buts == MouseButtons.Left)
528 floodFiller.FillColor = DrawColor;
529 floodFiller.Tolerance = currentTool.tolerance;
530 floodFiller.Bitmap =
ImageView.SelectedImage.Buffers[
ImageView.SelectedImage.Coords[coord.C, coord.Z, coord.T]];
531 floodFiller.FloodFill(
new AForge.Point((
int)p.X, (
int)p.Y));
535 if (
Tools.currentTool.type ==
Tools.
Tool.Type.dropper && buts == MouseButtons.Left)
537 DrawColor =
ImageView.SelectedBuffer.GetPixel((
int)p.X, (
int)p.Y);
543 public void ToolMove(PointD e, MouseButtons buts)
545 if (
App.viewer ==
null)
549 PointD p =
ImageView.SelectedImage.ToImageSpace(e);
550 if (
Tools.currentTool.type ==
Tools.
Tool.Type.pan && (buts.HasFlag(MouseButtons.Left) || buts.HasFlag(MouseButtons.Middle)))
554 if (
App.viewer.MouseMoveInt.X != 0 ||
App.viewer.MouseMoveInt.Y != 0)
556 App.viewer.PyramidalOriginTransformed =
new PointD(
App.viewer.PyramidalOriginTransformed.X + (
ImageView.mouseDown.X - e.X),
App.viewer.PyramidalOriginTransformed.Y + (
ImageView.mouseDown.Y - e.Y));
563 App.viewer.Origin =
new PointD(
App.viewer.
Origin.X + pf.X,
App.viewer.
Origin.Y + pf.Y);
569 if (currentTool.type == Tool.Type.line &&
ImageView.down)
571 anno.UpdatePoint(
new PointD(e.X, e.Y), 1);
575 if (currentTool.type == Tool.Type.freeform && buts == MouseButtons.Left &&
ImageView.down)
577 if (anno.GetPointCount() == 0)
579 anno.type = ROI.Type.Freeform;
580 anno.AddPoint(
new PointD(e.X, e.Y));
583 ImageView.SelectedImage.Annotations.Add(anno);
587 anno.AddPoint(
new PointD(e.X, e.Y));
593 if (currentTool.type == Tool.Type.rect && anno.type == ROI.Type.Rectangle)
595 if (anno.GetPointCount() == 4)
597 anno.BoundingBox =
new RectangleD(anno.X, anno.Y, e.X - anno.X, e.Y - anno.Y);
602 if (currentTool.type == Tool.Type.ellipse && anno.type == ROI.Type.Ellipse)
604 if (anno.GetPointCount() == 4)
606 anno.BoundingBox =
new RectangleD(anno.X, anno.Y, e.X - anno.X, e.Y - anno.Y);
611 if (currentTool.type == Tool.Type.rectSel && buts == MouseButtons.Left &&
ImageView.down)
616 foreach (ROI an
in App.viewer.AnnotationsRGB)
621 an.selectedPoints.Clear();
624 RectangleD[] sels = an.GetSelectBoxes(selectBoxSize);
625 for (
int i = 0; i < sels.Length; i++)
627 if (sels[i].IntersectsWith(r))
629 an.selectedPoints.Add(i);
639 if (currentTool.type == Tool.Type.rectSel &&
ImageView.up)
641 Tools.GetTool(
Tools.
Tool.Type.rectSel).Rectangle =
new RectangleD(0, 0, 0, 0);
646 foreach (ROI an
in ImageView.selectedAnnotations)
650 if (an.selectedPoints.Count == 0)
652 ImageView.SelectedImage.Annotations.Remove(an);
656 if (an.type == ROI.Type.Polygon ||
657 an.type == ROI.Type.Polyline ||
658 an.type == ROI.Type.Freeform)
661 an.RemovePoints(an.selectedPoints.ToArray());
669 if (currentTool.type == Tool.Type.rectSel && buts.HasFlag(MouseButtons.Left))
674 List<ROI> rois =
ImageView.SelectedImage.Annotations;
675 foreach (ROI an
in rois)
677 if (an.GetSelectBound(ROI.selectBoxSize *
ImageView.SelectedImage.PhysicalSizeX, ROI.selectBoxSize *
ImageView.SelectedImage.PhysicalSizeY).IntersectsWith(r))
680 an.selectedPoints.Clear();
682 RectangleD[] sels = an.GetSelectBoxes();
683 for (
int i = 0; i < sels.Length; i++)
685 if (sels[i].IntersectsWith(r))
687 an.selectedPoints.Add(i);
703 if (buts == MouseButtons.Left && currentTool.type == Tool.Type.eraser)
707 g.FillEllipse(
new Rectangle((
int)p.X, (
int)p.Y, (
int)width, (
int)
Tools.StrokeWidth), pen.color);
718 private void movePanel_Click(
object sender, EventArgs e)
720 currentTool = GetTool(Tool.Type.move);
722 movePanel.BackColor = System.Drawing.Color.LightGray;
729 private void textPanel_Click(
object sender, EventArgs e)
731 currentTool = GetTool(Tool.Type.text);
733 textPanel.BackColor = System.Drawing.Color.LightGray;
744 private void textPanel_DoubleClick(
object sender, EventArgs e)
746 currentTool = GetTool(Tool.Type.text);
748 textPanel.BackColor = System.Drawing.Color.LightGray;
750 if (fontDialog.ShowDialog() != DialogResult.OK)
752 font = fontDialog.Font;
759 private void pointPanel_Click(
object sender, EventArgs e)
761 currentTool = GetTool(Tool.Type.point);
763 pointPanel.BackColor = System.Drawing.Color.LightGray;
770 private void linePanel_Click(
object sender, EventArgs e)
772 currentTool = GetTool(Tool.Type.line);
774 linePanel.BackColor = System.Drawing.Color.LightGray;
782 private void rectPanel_Click(
object sender, EventArgs e)
784 currentTool = GetTool(Tool.Type.rect);
786 rectPanel.BackColor = System.Drawing.Color.LightGray;
794 private void ellipsePanel_Click(
object sender, EventArgs e)
796 currentTool = GetTool(Tool.Type.ellipse);
798 ellipsePanel.BackColor = System.Drawing.Color.LightGray;
806 private void polyPanel_Click(
object sender, EventArgs e)
808 currentTool = GetTool(Tool.Type.polygon);
810 polyPanel.BackColor = System.Drawing.Color.LightGray;
818 private void deletePanel_Click(
object sender, EventArgs e)
820 currentTool = GetTool(Tool.Type.delete);
822 deletePanel.BackColor = System.Drawing.Color.LightGray;
829 private void freeformPanel_Click(
object sender, EventArgs e)
831 currentTool = GetTool(Tool.Type.freeform);
833 freeformPanel.BackColor = System.Drawing.Color.LightGray;
841 private void rectSelPanel_Click(
object sender, EventArgs e)
843 currentTool = GetTool(Tool.Type.rectSel);
845 rectSelPanel.BackColor = System.Drawing.Color.LightGray;
853 private void panPanel_Click(
object sender, EventArgs e)
855 currentTool = GetTool(Tool.Type.pan);
857 panPanel.BackColor = System.Drawing.Color.LightGray;
865 private void magicPanel_Click(
object sender, EventArgs e)
867 currentTool = GetTool(Tool.Type.magic);
869 magicPanel.BackColor = System.Drawing.Color.LightGray;
877 private void bucketPanel_Click(
object sender, EventArgs e)
879 currentTool = GetTool(Tool.Type.bucket);
881 bucketPanel.BackColor = System.Drawing.Color.LightGray;
889 private void pencilPanel_Click(
object sender, EventArgs e)
891 currentTool = GetTool(Tool.Type.pencil);
893 pencilPanel.BackColor = System.Drawing.Color.LightGray;
902 private void magicPanel_MouseDoubleClick(
object sender, MouseEventArgs e)
904 if (magicSel.ShowDialog() != DialogResult.OK)
912 private void Tools_FormClosing(
object sender, FormClosingEventArgs e)
925 private void pencilPanel_MouseDoubleClick(
object sender, MouseEventArgs e)
927 currentTool = GetTool(Tool.Type.pencil);
929 pencilPanel.BackColor = System.Drawing.Color.LightGray;
931 if (pt.ShowDialog() != DialogResult.OK)
933 Tools.StrokeWidth = pt.Pen.width;
934 DrawColor = pt.Pen.color;
946 private void bucketPanel_MouseDoubleClick(
object sender, MouseEventArgs e)
948 currentTool = GetTool(Tool.Type.bucket);
950 bucketPanel.BackColor = System.Drawing.Color.LightGray;
952 if (pt.ShowDialog() != DialogResult.OK)
954 Tools.StrokeWidth = pt.Pen.width;
955 DrawColor = pt.Pen.color;
956 currentTool.tolerance = pt.Tolerance;
965 private void dropperPanel_MouseClick(
object sender, MouseEventArgs e)
967 currentTool = GetTool(Tool.Type.dropper);
969 dropperPanel.BackColor = System.Drawing.Color.LightGray;
979 private void color1Box_MouseClick(
object sender, MouseEventArgs e)
982 if (t.ShowDialog() != DialogResult.OK)
995 private void color2Box_MouseClick(
object sender, MouseEventArgs e)
998 if (t.ShowDialog() != DialogResult.OK)
1000 EraseColor = t.Color;
1009 private void widthBox_ValueChanged(
object sender, EventArgs e)
1011 width = (int)widthBox.Value;
1020 private void eraserPanel_MouseClick(
object sender, MouseEventArgs e)
1022 currentTool = GetTool(Tool.Type.eraser);
1024 eraserPanel.BackColor = System.Drawing.Color.LightGray;
1031 private void switchBox_MouseClick(
object sender, MouseEventArgs e)
1033 ColorS s = DrawColor;
1034 DrawColor = eraseColor;
1043 private void Tools_Activated(
object sender, EventArgs e)
The base class that the flood fill algorithms inherit from. Implements the basic flood filler functio...
ImageView control for image stacks, pyramidal and whole-slide-images.
PointD Origin
A property of the class PointD. */.
async void UpdateImages(bool updatePyramidal=false)
Implements the QueueLinear flood fill algorithm using array-based pixel manipulation.
static State GetDown(PointD pf, MouseButtons mb)
static State GetUp(PointD pf, MouseButtons mb)
static void UpdateState(State s)
static bool GetKeyState(System.Windows.Forms.Keys vKey)