25#pragma warning disable 649
28 private Gtk.DrawingArea view;
29#pragma warning restore 649
30 private AForge.Color selectColor;
31 private Builder _builder;
38 Builder builder =
new Builder(
new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"/" +
"Glade/Tools.glade", FileMode.Open));
39 return new Tools(builder, builder.GetObject(
"tools").Handle);
42 protected Tools(Builder builder, IntPtr handle) : base(handle)
44 selectColor = AForge.Color.FromArgb(0, 0, 150, 255);
45 AForge.Color white = AForge.Color.FromArgb(255, 255, 255, 255);
47 builder.Autoconnect(
this);
50 ColorS col =
new ColorS(ushort.MaxValue);
52 currentTool =
GetTool(Tool.Type.move.ToString());
53 floodFiller =
new QueueLinearFloodFiller(floodFiller);
55 App.ApplyStyles(
this);
57 public static bool applyToStack =
false;
58 public static ColorTool colorTool;
59 public static bool rEnabled =
true;
60 public static bool gEnabled =
true;
61 public static bool bEnabled =
true;
62 public static ColorS drawColor =
new ColorS(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue);
63 public static ColorS eraseColor =
new ColorS(0, 0, 0);
64 public static ColorS tolerance =
new ColorS(0, 0, 0);
66 private static int width = 5;
67 private static ROI roi;
68 public static ROI selectedROI
72 List<ROI> rs =
new List<ROI>();
73 foreach (var item
in ImageView.SelectedImage.Annotations)
87 public static Rectangle selectionRectangle;
88 public static Dictionary<string, Tool> tools =
new Dictionary<string, Tool>();
89 private static AbstractFloodFiller floodFiller =
null;
90 static List<ROI> rois =
new List<ROI>();
92 public static List<ROI> selectedROIs
97 for (
int i = 0; i < Images.images.Count; i++)
99 for (
int r = 0; r < Images.images[i].Annotations.Count; r++)
101 if (Images.images[i].Annotations[r].Selected)
102 rois.Add(Images.images[i].Annotations[r]);
110 rois.AddRange(value);
115 if (ImageView.SelectedImage.isPyramidal)
116 roi.serie = App.viewer.Level;
118 roi.serie = ImageView.SelectedImage.series;
119 roi.coord = App.viewer.GetCoordinate();
120 ImageView.SelectedImage.Annotations.Add(roi);
126 this.ButtonPressEvent += Tools_ButtonPressEvent;
128 view.Drawn += View_Drawn;
129 view.DeleteEvent += View_DeleteEvent;
131 private void View_DeleteEvent(
object o, DeleteEventArgs args)
137 static int gridW = 2;
138 static int gridH = 11;
140 static List<Rectangle> rects =
new List<Rectangle>();
145 private void View_Drawn(
object o, DrawnArgs e)
151 foreach (Tool item
in tools.Values)
153 if (item.image !=
null)
156 Pixbuf pf = item.image.ScaleSimple(w, h, InterpType.Bilinear);
159 e.Cr.SetSourceRGB(item.color.Rf, item.color.Gf, item.color.Bf);
160 e.Cr.Rectangle(x * w, y * h, w, h);
163 Gdk.CairoHelper.SetSourcePixbuf(e.Cr, pf, w * x, h * y);
170 e.Cr.SetSourceRGB(item.color.Rf, item.color.Gf, item.color.Bf);
171 e.Cr.Rectangle(x * w, y * h, w, h);
193 private void Tools_ButtonPressEvent(
object o, ButtonPressEventArgs args)
195 Gdk.Rectangle r =
new Gdk.Rectangle((
int)args.Event.X, (
int)args.Event.Y, 1, 1);
197 foreach (Tool t
in tools.Values)
199 if (t.bounds.IntersectsWith(
new AForge.PointD(args.Event.X, args.Event.Y)))
212 if (tool.type == Tool.Type.color1)
214 if (tool.type == Tool.Type.color2)
216 if (tool.type == Tool.Type.switchColors)
220 public static bool colorOne =
true;
224 private void Color1Click()
226 if (App.color !=
null)
227 if (App.color.Visible)
230 App.color = ColorTool.Create(
false);
234 private void Color2Click()
236 if (App.color !=
null)
237 if (App.color.Visible)
240 App.color = ColorTool.Create(
true);
244 private void SwitchClick()
246 ColorS s = DrawColor;
247 DrawColor = EraseColor;
250 private void UpdateView()
252 App.viewer.UpdateView();
256 private void SyncPointsFromBoundingBox(ROI roi)
258 if (roi ==
null)
return;
259 RectangleD r = roi.BoundingBox;
261 double left = Math.Min(r.X, r.X + r.W);
262 double top = Math.Min(r.Y, r.Y + r.H);
263 double right = Math.Max(r.X, r.X + r.W);
264 double bottom = Math.Max(r.Y, r.Y + r.H);
267 roi.BoundingBox =
new RectangleD(left, top, right - left, bottom - top);
270 if (roi.Points ==
null) roi.Points =
new List<PointD>();
272 if (roi.Points.Count < 4)
275 roi.Points.Add(
new PointD(left, top));
276 roi.Points.Add(
new PointD(right, top));
277 roi.Points.Add(
new PointD(right, bottom));
278 roi.Points.Add(
new PointD(left, bottom));
283 roi.UpdatePoint(
new PointD(left, top), 0);
284 roi.UpdatePoint(
new PointD(right, top), 1);
285 roi.UpdatePoint(
new PointD(right, bottom), 2);
286 roi.UpdatePoint(
new PointD(left, bottom), 3);
291 private void SyncBoundingBoxFromPoints(ROI roi)
293 if (roi ==
null || roi.Points ==
null || roi.Points.Count == 0)
return;
295 double minx =
double.MaxValue, miny =
double.MaxValue;
296 double maxx =
double.MinValue, maxy =
double.MinValue;
298 foreach (var p
in roi.Points)
300 if (p.X < minx) minx = p.X;
301 if (p.Y < miny) miny = p.Y;
302 if (p.X > maxx) maxx = p.X;
303 if (p.Y > maxy) maxy = p.Y;
306 roi.BoundingBox =
new RectangleD(minx, miny, maxx - minx, maxy - miny);
313 public void ToolDown(PointD e, ButtonPressEventArgs buts)
315 if (App.viewer ==
null || currentTool ==
null || ImageView.SelectedImage ==
null)
318 Plugins.MouseDown(ImageView.SelectedImage, e, buts);
319 Scripting.UpdateState(Scripting.State.GetDown(e, buts.Event.Button));
321 switch (currentTool.type)
324 ToolDown_Pan(e, buts);
326 case Tool.Type.magic:
327 ToolDown_Magic(e, buts);
329 case Tool.Type.point:
330 ToolDown_Point(e, buts);
333 ToolDown_Move(e, buts);
336 ToolDown_Line(e, buts);
339 ToolDown_Rectangle(e, buts);
341 case Tool.Type.ellipse:
342 ToolDown_Ellipse(e, buts);
344 case Tool.Type.polygon:
345 ToolDown_Polygon(e, buts);
348 ToolDown_Text(e, buts);
350 case Tool.Type.delete:
351 ToolDown_Delete(e, buts);
353 case Tool.Type.freeform:
354 ToolDown_Freeform(e, buts);
361 public void ToolUp(PointD e, ButtonReleaseEventArgs buts)
363 if (App.viewer ==
null || currentTool ==
null || ImageView.SelectedImage ==
null)
366 Plugins.MouseUp(ImageView.SelectedImage, e, buts);
367 Scripting.UpdateState(Scripting.State.GetUp(e, buts.Event.Button));
369 switch (currentTool.type)
375 ToolUp_Move(e, buts);
377 case Tool.Type.magic:
378 ToolUp_Magic(e, buts);
381 ToolUp_Line(e, buts);
384 ToolUp_Rectangle(e, buts);
386 case Tool.Type.ellipse:
387 ToolUp_Ellipse(e, buts);
389 case Tool.Type.polygon:
390 ToolUp_Polygon(e, buts);
395 case Tool.Type.delete:
398 case Tool.Type.freeform:
399 ToolUp_Freeform(e, buts);
401 case Tool.Type.bucket:
402 ToolUp_Bucket(e, buts);
404 case Tool.Type.dropper:
405 ToolUp_Dropper(e, buts);
412 public void ToolMove(PointD e, MotionNotifyEventArgs buts)
414 if (App.viewer ==
null)
417 Plugins.MouseMove(ImageView.SelectedImage, e, buts);
420 if (buts.Event.State.HasFlag(ModifierType.Button1Mask))
421 Scripting.UpdateState(Scripting.State.GetMove(e, 1));
422 else if (buts.Event.State.HasFlag(ModifierType.Button2Mask))
423 Scripting.UpdateState(Scripting.State.GetMove(e, 2));
424 else if (buts.Event.State.HasFlag(ModifierType.Button3Mask))
425 Scripting.UpdateState(Scripting.State.GetMove(e, 3));
426 else if (buts.Event.State.HasFlag(ModifierType.Button4Mask))
427 Scripting.UpdateState(Scripting.State.GetMove(e, 4));
428 else if (buts.Event.State.HasFlag(ModifierType.Button5Mask))
429 Scripting.UpdateState(Scripting.State.GetMove(e, 5));
431 Scripting.UpdateState(Scripting.State.GetMove(e, 0));
433 switch (currentTool.type)
436 ToolMove_Pan(e, buts);
439 ToolMove_Move(e, buts);
441 case Tool.Type.magic:
442 ToolMove_Magic(e, buts);
445 ToolMove_Line(e, buts);
448 ToolMove_Rectangle(e, buts);
450 case Tool.Type.ellipse:
451 ToolMove_Ellipse(e, buts);
453 case Tool.Type.polygon:
454 ToolMove_Polygon(e, buts);
459 case Tool.Type.freeform:
460 ToolMove_Freeform(e, buts);
462 case Tool.Type.brush:
463 ToolMove_Brush(e, buts);
465 case Tool.Type.eraser:
466 ToolMove_Eraser(e, buts);
476 public void ToolDown_Move(PointD e, ButtonPressEventArgs buts)
478 if (currentTool.type != Tool.Type.move || buts.Event.Button != 1)
481 App.viewer.MouseDown = e;
482 bool ctrl = buts.Event.State.HasFlag(ModifierType.ControlMask);
487 foreach (var ann
in ImageView.SelectedImage.Annotations)
489 if (ann ==
null || !ann.Selected)
continue;
491 var boxes = ann.GetSelectBoxes(ROI.selectBoxSize * ImageView.SelectedImage.PhysicalSizeX);
492 if (boxes ==
null)
continue;
494 for (
int i = 0; i < boxes.Length; i++)
496 if (boxes[i].Contains(ann.coord, e))
501 if (ann.selectedPoints.Contains(i)) ann.selectedPoints.Remove(i);
502 else ann.selectedPoints.Add(i);
506 ann.selectedPoints.Clear();
507 ann.selectedPoints.Add(i);
516 foreach (var ann
in ImageView.SelectedImage.Annotations)
518 if (ann !=
null && ann.Selected && ann.BoundingBox.Contains(ann.coord, e))
527 foreach (var ann
in ImageView.SelectedImage.Annotations)
529 if (ann !=
null && ann.BoundingBox.Contains(ann.coord, e))
533 foreach (var other
in ImageView.SelectedImage.Annotations)
537 other.Selected =
false;
538 other.selectedPoints?.Clear();
544 ann.selectedPoints?.Clear();
554 foreach (var ann
in ImageView.SelectedImage.Annotations)
558 ann.Selected =
false;
559 ann.selectedPoints?.Clear();
565 Tools.GetTool(Tools.Tool.Type.move).Rectangle =
new RectangleD(e.X, e.Y, 0, 0);
568 public void ToolMove_Move(PointD e, MotionNotifyEventArgs buts)
570 if (currentTool.type != Tool.Type.move ||
571 !buts.Event.State.HasFlag(ModifierType.Button1Mask))
575 if (selectedROI !=
null && selectedROI.Selected && selectedROI.selectedPoints !=
null)
577 PointD delta =
new PointD(e.X - App.viewer.MouseDown.X, e.Y - App.viewer.MouseDown.Y);
580 if (selectedROI.type == ROI.Type.Rectangle || selectedROI.type == ROI.Type.Ellipse)
583 if (selectedROI.selectedPoints.Count == 1)
585 int pointIndex = selectedROI.selectedPoints[0];
586 RectangleD rect = selectedROI.BoundingBox;
589 double left = rect.X;
591 double right = rect.X + rect.W;
592 double bottom = rect.Y + rect.H;
617 RectangleD newRect =
new RectangleD(
618 Math.Min(left, right),
619 Math.Min(top, bottom),
620 Math.Abs(right - left),
621 Math.Abs(bottom - top)
624 selectedROI.BoundingBox = newRect;
627 SyncPointsFromBoundingBox(selectedROI);
628 selectedROI.UpdateBoundingBox();
630 App.viewer.MouseDown =
new PointD(e.X, e.Y);
631 App.viewer.UpdateView();
637 for (
int i = 0; i < selectedROI.Points.Count; i++)
639 selectedROI.UpdatePoint(
640 new PointD(selectedROI.Points[i].X + delta.X,
641 selectedROI.Points[i].Y + delta.Y), i);
645 SyncBoundingBoxFromPoints(selectedROI);
646 selectedROI.UpdateBoundingBox();
648 App.viewer.MouseDown =
new PointD(e.X, e.Y);
649 App.viewer.UpdateView();
656 if (selectedROI.selectedPoints.Count == 0)
659 for (
int i = 0; i < selectedROI.Points.Count; i++)
661 selectedROI.UpdatePoint(
662 new PointD(selectedROI.Points[i].X + delta.X,
663 selectedROI.Points[i].Y + delta.Y), i);
669 foreach (
int pointIndex
in selectedROI.selectedPoints.ToArray())
671 if (pointIndex >= 0 && pointIndex < selectedROI.Points.Count)
673 PointD pt = selectedROI.Points[pointIndex];
674 selectedROI.UpdatePoint(
675 new PointD(pt.X + delta.X, pt.Y + delta.Y), pointIndex);
681 SyncBoundingBoxFromPoints(selectedROI);
682 selectedROI.UpdateBoundingBox();
684 App.viewer.MouseDown =
new PointD(e.X, e.Y);
685 App.viewer.UpdateView();
690 else if (selectedROI !=
null && selectedROI.Selected)
692 PointD delta =
new PointD(e.X - App.viewer.MouseDown.X, e.Y - App.viewer.MouseDown.Y);
694 for (
int i = 0; i < selectedROI.Points.Count; i++)
696 selectedROI.UpdatePoint(
697 new PointD(selectedROI.Points[i].X + delta.X,
698 selectedROI.Points[i].Y + delta.Y), i);
701 SyncBoundingBoxFromPoints(selectedROI);
702 selectedROI.UpdateBoundingBox();
704 App.viewer.MouseDown =
new PointD(e.X, e.Y);
705 App.viewer.UpdateView();
711 PointD d =
new PointD(e.X - App.viewer.MouseDown.X, e.Y - App.viewer.MouseDown.Y);
712 RectangleD selRect =
new RectangleD(
713 Math.Min(App.viewer.MouseDown.X, e.X),
714 Math.Min(App.viewer.MouseDown.Y, e.Y),
715 Math.Abs(d.X), Math.Abs(d.Y));
717 Tools.GetTool(Tools.Tool.Type.move).Rectangle = selRect;
719 bool controlHeld = buts.Event.State.HasFlag(ModifierType.ControlMask);
722 foreach (ROI an
in ImageView.SelectedImage.Annotations)
724 if (an ==
null)
continue;
726 RectangleD selBound = an.GetSelectBound(
727 ROI.selectBoxSize * ImageView.SelectedImage.PhysicalSizeX,
728 ROI.selectBoxSize * ImageView.SelectedImage.PhysicalSizeY);
730 if (selBound.IntersectsWith(selRect))
735 an.selectedPoints?.Clear();
738 RectangleD[] sels = an.GetSelectBoxes(
739 ROI.selectBoxSize * ImageView.SelectedImage.PhysicalSizeX);
742 for (
int i = 0; i < sels.Length; i++)
744 if (sels[i].IntersectsWith(selRect))
746 if (an.selectedPoints ==
null) an.selectedPoints =
new List<int>();
747 if (!an.selectedPoints.Contains(i))
748 an.selectedPoints.Add(i);
753 else if (!controlHeld)
756 an.selectedPoints?.Clear();
760 if (selectedROI !=
null)
761 selectedROI.UpdateBoundingBox();
763 App.viewer.UpdateView();
766 public void ToolUp_Move(PointD e, ButtonReleaseEventArgs buts)
768 if (currentTool.type != Tool.Type.move || buts.Event.Button != 1)
772 Tools.GetTool(Tools.Tool.Type.move).Rectangle =
new RectangleD(0, 0, 0, 0);
773 selectedROI?.UpdateBoundingBox();
774 App.viewer.UpdateView();
781 public void ToolDown_Point(PointD e, ButtonPressEventArgs buts)
783 if (currentTool.type != Tool.Type.point || buts.Event.Button != 1)
787 an.type = ROI.Type.Point;
789 an.Points.Add(
new PointD(e.X, e.Y));
790 an.coord = App.viewer.GetCoordinate();
791 an.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
801 public void ToolDown_Line(PointD e, ButtonPressEventArgs buts)
803 if (currentTool.type != Tool.Type.line || buts.Event.Button != 1)
806 selectedROI =
new ROI();
807 selectedROI.type = ROI.Type.Line;
808 selectedROI.AddPoint(
new PointD(e.X, e.Y));
809 selectedROI.AddPoint(
new PointD(e.X + ImageView.SelectedImage.PhysicalSizeX * ROI.selectBoxSize,
810 e.Y + ImageView.SelectedImage.PhysicalSizeX * ROI.selectBoxSize));
811 selectedROI.coord = App.viewer.GetCoordinate();
812 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
816 public void ToolMove_Line(PointD e, MotionNotifyEventArgs buts)
818 if (currentTool.type != Tool.Type.line ||
819 !buts.Event.State.HasFlag(ModifierType.Button1Mask))
822 if (selectedROI !=
null)
824 if (selectedROI.selectedPoints.Count == 1)
826 selectedROI.UpdatePoint(
new PointD(e.X, e.Y), selectedROI.selectedPoints[0]);
827 selectedROI.UpdateBoundingBox();
832 selectedROI.UpdatePoint(
new PointD(e.X, e.Y), 1);
833 selectedROI.UpdateBoundingBox();
839 public void ToolUp_Line(PointD e, ButtonReleaseEventArgs buts)
841 if (currentTool.type != Tool.Type.line || buts.Event.Button != 1)
844 if (selectedROI !=
null && selectedROI.GetPointCount() >= 2)
846 selectedROI.UpdatePoint(
new PointD(e.X, e.Y), 1);
847 selectedROI.UpdateBoundingBox();
853 public void ToolDown_Rectangle(PointD e, ButtonPressEventArgs buts)
855 if (currentTool.type != Tool.Type.rect || buts.Event.Button != 1)
858 selectedROI =
new ROI();
859 selectedROI.type = ROI.Type.Rectangle;
860 selectedROI.BoundingBox =
new RectangleD(e.X, e.Y, 0, 0);
861 selectedROI.coord = App.viewer.GetCoordinate();
862 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
866 public void ToolMove_Rectangle(PointD e, MotionNotifyEventArgs buts)
868 if (currentTool.type != Tool.Type.rect ||
869 !buts.Event.State.HasFlag(ModifierType.Button1Mask))
872 if (selectedROI !=
null && selectedROI.type == ROI.Type.Rectangle)
874 selectedROI.BoundingBox = RectangleDExtensions.FromCorners(
875 new PointD(selectedROI.BoundingBox.X, selectedROI.BoundingBox.Y),
876 new PointD(e.X, e.Y));
881 public void ToolUp_Rectangle(PointD e, ButtonReleaseEventArgs buts)
883 if (currentTool.type != Tool.Type.rect || buts.Event.Button != 1)
886 if (selectedROI !=
null && selectedROI.type == ROI.Type.Rectangle)
889 selectedROI.BoundingBox = RectangleDExtensions.FromCorners(
890 new PointD(selectedROI.BoundingBox.X, selectedROI.BoundingBox.Y),
891 new PointD(e.X, e.Y));
894 selectedROI.Validate();
903 public void ToolDown_Ellipse(PointD e, ButtonPressEventArgs buts)
905 if (currentTool.type != Tool.Type.ellipse || buts.Event.Button != 1)
908 selectedROI =
new ROI();
909 selectedROI.type = ROI.Type.Ellipse;
910 selectedROI.BoundingBox =
new RectangleD(e.X, e.Y, 0, 0);
911 selectedROI.coord = App.viewer.GetCoordinate();
912 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
916 public void ToolMove_Ellipse(PointD e, MotionNotifyEventArgs buts)
918 if (currentTool.type != Tool.Type.ellipse ||
919 !buts.Event.State.HasFlag(ModifierType.Button1Mask))
922 if (selectedROI !=
null && selectedROI.type == ROI.Type.Ellipse)
924 selectedROI.BoundingBox = RectangleDExtensions.FromCorners(
925 new PointD(selectedROI.BoundingBox.X, selectedROI.BoundingBox.Y),
926 new PointD(e.X, e.Y));
931 public void ToolUp_Ellipse(PointD e, ButtonReleaseEventArgs buts)
933 if (currentTool.type != Tool.Type.ellipse || buts.Event.Button != 1)
936 if (selectedROI !=
null && selectedROI.type == ROI.Type.Ellipse)
939 selectedROI.BoundingBox = RectangleDExtensions.FromCorners(
940 new PointD(selectedROI.BoundingBox.X, selectedROI.BoundingBox.Y),
941 new PointD(e.X, e.Y));
944 selectedROI.Validate();
953 public void ToolDown_Polygon(PointD e, ButtonPressEventArgs buts)
955 if (currentTool.type != Tool.Type.polygon || buts.Event.Button != 1)
958 if (selectedROI ==
null || selectedROI.type != ROI.Type.Polygon)
961 selectedROI =
new ROI();
962 selectedROI.type = ROI.Type.Polygon;
963 selectedROI.closed =
false;
964 selectedROI.AddPoint(
new PointD(e.X, e.Y));
965 selectedROI.coord = App.viewer.GetCoordinate();
966 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
970 else if (!selectedROI.closed)
973 if (selectedROI.Points.Count >= 3)
975 PointD firstPoint = selectedROI.Points[0];
976 double distance = Math.Sqrt(Math.Pow(e.X - firstPoint.X, 2) + Math.Pow(e.Y - firstPoint.Y, 2));
977 double threshold = 10.0;
979 if (distance < threshold)
982 selectedROI.closed =
true;
983 selectedROI.Selected =
false;
984 selectedROI.UpdateBoundingBox();
985 selectedROI.Validate();
995 selectedROI.AddPoint(
new PointD(e.X, e.Y));
996 selectedROI.UpdateBoundingBox();
1000 private void ToolMove_Polygon(PointD e, MotionNotifyEventArgs buts)
1002 if (currentTool.type != Tool.Type.polygon)
1006 if (selectedROI !=
null && selectedROI.type == ROI.Type.Polygon && !selectedROI.closed)
1013 private void ToolUp_Polygon(PointD e, ButtonReleaseEventArgs buts)
1022 public void ToolDown_Freeform(PointD e, ButtonPressEventArgs buts)
1024 if (currentTool.type != Tool.Type.freeform || buts.Event.Button != 1)
1027 if (selectedROI ==
null || selectedROI.type != ROI.Type.Polygon)
1030 selectedROI =
new ROI();
1031 selectedROI.type = ROI.Type.Polygon;
1032 selectedROI.closed =
false;
1033 selectedROI.AddPoint(
new PointD(e.X, e.Y));
1034 selectedROI.coord = App.viewer.GetCoordinate();
1035 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
1036 AddROI(selectedROI);
1039 else if (!selectedROI.closed)
1042 if (selectedROI.Points.Count >= 3)
1044 PointD firstPoint = selectedROI.Points[0];
1045 double distance = Math.Sqrt(Math.Pow(e.X - firstPoint.X, 2) + Math.Pow(e.Y - firstPoint.Y, 2));
1046 double threshold = 10.0;
1048 if (distance < threshold)
1051 selectedROI.closed =
true;
1052 selectedROI.Selected =
false;
1053 selectedROI.UpdateBoundingBox();
1054 selectedROI.Validate();
1064 selectedROI.AddPoint(
new PointD(e.X, e.Y));
1065 selectedROI.UpdateBoundingBox();
1069 public void ToolMove_Freeform(PointD e, MotionNotifyEventArgs buts)
1071 if (selectedROI !=
null)
1072 if (currentTool.type == Tool.Type.freeform && buts.Event.State.HasFlag(ModifierType.Button1Mask))
1074 selectedROI.AddPoint(
new PointD(e.X, e.Y));
1075 selectedROI.coord = App.viewer.GetCoordinate();
1076 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
1080 public void ToolUp_Freeform(PointD e, ButtonReleaseEventArgs buts)
1082 if (currentTool.type != Tool.Type.freeform || buts.Event.Button != 1)
1085 if (selectedROI !=
null)
1087 selectedROI.UpdateBoundingBox();
1096 public void ToolDown_Text(PointD e, ButtonPressEventArgs buts)
1098 if (currentTool.type != Tool.Type.text || buts.Event.Button != 1)
1101 selectedROI =
new ROI();
1102 selectedROI.type = ROI.Type.Label;
1103 selectedROI.AddPoint(
new PointD(e.X, e.Y));
1104 selectedROI.coord = App.viewer.GetCoordinate();
1105 selectedROI.serie = ImageView.SelectedImage.isPyramidal ? App.viewer.Level : ImageView.SelectedImage.series;
1112 AddROI(selectedROI);
1113 selectedROI.UpdateBoundingBox();
1120 public void ToolDown_Delete(PointD e, ButtonPressEventArgs buts)
1122 if (currentTool.type != Tool.Type.delete || buts.Event.Button != 1)
1125 for (
int i = ImageView.SelectedImage.Annotations.Count - 1; i >= 0; i--)
1127 ROI an = ImageView.SelectedImage.Annotations[i];
1128 if (an ==
null)
continue;
1130 if (an.BoundingBox.IntersectsWith(
new RectangleD(e.X, e.Y, 1, 1)))
1132 if (an.selectedPoints ==
null || an.selectedPoints.Count == 0)
1135 ImageView.SelectedImage.Annotations.RemoveAt(i);
1138 else if (an.selectedPoints.Count == 1 &&
1139 !(an.type == ROI.Type.Polygon ||
1140 an.type == ROI.Type.Polyline ||
1141 an.type == ROI.Type.Freeform))
1144 ImageView.SelectedImage.Annotations.RemoveAt(i);
1150 if (an.type == ROI.Type.Polygon ||
1151 an.type == ROI.Type.Polyline ||
1152 an.type == ROI.Type.Freeform)
1155 an.RemovePoints(an.selectedPoints.ToArray());
1156 an.UpdateBoundingBox();
1168 public void ToolDown_Brush(PointD e, ButtonPressEventArgs buts)
1170 if (currentTool.type != Tool.Type.brush || buts.Event.Button != 1)
1173 PointD ip = ImageView.SelectedImage.ToImageSpace(e);
1174 if (ImageView.SelectedImage.isPyramidal)
1175 ip =
new PointD(e.X, e.Y);
1177 Bio.Graphics.Graphics g =
Bio.Graphics.Graphics.FromImage(ImageView.SelectedBuffer);
1178 g.pen =
new Bio.Graphics.Pen(DrawColor, (
int)StrokeWidth, ImageView.SelectedImage.bitsPerPixel);
1179 g.FillEllipse(
new Rectangle((
int)ip.X, (
int)ip.Y, (
int)StrokeWidth, (
int)StrokeWidth), g.pen.color);
1181 App.viewer.UpdateImage();
1182 App.viewer.UpdateView();
1185 public void ToolMove_Brush(PointD e, MotionNotifyEventArgs buts)
1187 if (currentTool.type != Tool.Type.brush ||
1188 !buts.Event.State.HasFlag(ModifierType.Button1Mask))
1191 PointD ip = ImageView.SelectedImage.ToImageSpace(e);
1192 if (ImageView.SelectedImage.isPyramidal)
1193 ip =
new PointD(e.X, e.Y);
1195 Bio.Graphics.Graphics g =
Bio.Graphics.Graphics.FromImage(ImageView.SelectedBuffer);
1196 g.pen =
new Bio.Graphics.Pen(DrawColor, (
int)StrokeWidth, ImageView.SelectedImage.bitsPerPixel);
1197 g.FillEllipse(
new Rectangle((
int)ip.X, (
int)ip.Y, (
int)StrokeWidth, (
int)StrokeWidth), g.pen.color);
1199 App.viewer.UpdateImages();
1200 App.viewer.UpdateView();
1206 public void ToolDown_Eraser(PointD e, ButtonPressEventArgs buts)
1208 if (currentTool.type != Tool.Type.eraser || buts.Event.Button != 1)
1211 PointD ip = ImageView.SelectedImage.ToImageSpace(e);
1212 if (ImageView.SelectedImage.isPyramidal)
1213 ip =
new PointD(e.X, e.Y);
1215 Bio.Graphics.Graphics g =
Bio.Graphics.Graphics.FromImage(ImageView.SelectedBuffer);
1216 Bio.Graphics.Pen pen =
new Bio.Graphics.Pen(EraseColor, (
int)StrokeWidth, ImageView.SelectedBuffer.BitsPerPixel);
1217 g.FillEllipse(
new Rectangle((
int)ip.X, (
int)ip.Y, (
int)StrokeWidth, (
int)StrokeWidth), pen.color);
1219 App.viewer.UpdateImages();
1220 App.viewer.UpdateView();
1223 public void ToolMove_Eraser(PointD e, MotionNotifyEventArgs buts)
1225 if (currentTool.type != Tool.Type.eraser ||
1226 !buts.Event.State.HasFlag(ModifierType.Button1Mask))
1229 PointD ip = ImageView.SelectedImage.ToImageSpace(e);
1230 if (ImageView.SelectedImage.isPyramidal)
1231 ip =
new PointD(e.X, e.Y);
1233 Bio.Graphics.Graphics g =
Bio.Graphics.Graphics.FromImage(ImageView.SelectedBuffer);
1234 Bio.Graphics.Pen pen =
new Bio.Graphics.Pen(EraseColor, (
int)StrokeWidth, ImageView.SelectedBuffer.BitsPerPixel);
1235 g.FillEllipse(
new Rectangle((
int)ip.X, (
int)ip.Y, (
int)StrokeWidth, (
int)StrokeWidth), pen.color);
1237 App.viewer.UpdateImages();
1238 App.viewer.UpdateView();
1244 public void ToolUp_Bucket(PointD e, ButtonReleaseEventArgs buts)
1246 if (currentTool.type != Tool.Type.bucket || buts.Event.Button != 1)
1249 PointD ip = ImageView.SelectedImage.ToImageSpace(e);
1251 if (ip.X >= ImageView.SelectedImage.SizeX || ip.Y >= ImageView.SelectedImage.SizeY)
1254 floodFiller.FillColor = DrawColor;
1255 floodFiller.Tolerance = tolerance;
1256 floodFiller.Bitmap = ImageView.SelectedBuffer;
1257 floodFiller.FloodFill(
new AForge.Point((
int)ip.X, (
int)ip.Y));
1259 App.viewer.UpdateImages();
1260 App.viewer.UpdateView();
1266 public void ToolUp_Dropper(PointD e, ButtonReleaseEventArgs buts)
1268 if (currentTool.type != Tool.Type.dropper || buts.Event.Button != 1)
1271 PointD ip = ImageView.SelectedImage.ToImageSpace(e);
1273 if (ip.X >= 0 && ip.Y >= 0 && ip.X < ImageView.SelectedImage.SizeX && ip.Y < ImageView.SelectedImage.SizeY)
1275 DrawColor = ImageView.SelectedBuffer.GetPixel((
int)ip.X, (
int)ip.Y);
1276 App.viewer.UpdateView();
1286 private bool isPanning;
1287 private double panStartX;
1288 private double panStartY;
1290 private PointD initialPanOrigin;
1291 private double initialPanScale;
1294 private PointD panVelocity;
1295 private DateTime lastMoveTime;
1298 private const double PanFriction = 50.0;
1299 private const double VelocityEpsilon = 0.0001;
1302 private double dpiScale = 1.0;
1305 private bool kineticActive =
true;
1308 public static double PanSpeed = 0.75;
1312 public void ToolDown_Pan(PointD e, ButtonPressEventArgs buts)
1314 if (buts.Event.Button != 1 && buts.Event.Button != 2)
1317 currentTool =
GetTool(Tool.Type.pan);
1318 panStartX = buts.Event.X;
1319 panStartY = buts.Event.Y;
1320 initialPanScale = App.viewer.Resolution;
1322 if (ImageView.SelectedImage.isPyramidal)
1324 initialPanOrigin =
new PointD(
1325 App.viewer.PyramidalOrigin.X,
1326 App.viewer.PyramidalOrigin.Y);
1330 initialPanOrigin =
new PointD(
1331 App.viewer.Origin.X,
1332 App.viewer.Origin.Y);
1339 public void ToolMove_Pan(PointD e, MotionNotifyEventArgs buts)
1341 if (currentTool.type != Tool.Type.pan)
1344 if (!buts.Event.State.HasFlag(ModifierType.Button1Mask) &&
1345 !buts.Event.State.HasFlag(ModifierType.Button2Mask))
1348 double dx = buts.Event.X - panStartX;
1349 double dy = buts.Event.Y - panStartY;
1352 double deltaX = (dx / initialPanScale) * PanSpeed;
1353 double deltaY = (dy / initialPanScale) * PanSpeed;
1355 if (ImageView.SelectedImage.isPyramidal)
1357 App.viewer.PyramidalOrigin =
new PointD(
1358 initialPanOrigin.X - deltaX,
1359 initialPanOrigin.Y - deltaY);
1360 App.viewer.UpdateView(
true);
1364 App.viewer.Origin =
new PointD(
1365 initialPanOrigin.X - deltaX,
1366 initialPanOrigin.Y - deltaY);
1368 App.viewer.UpdateView();
1376 public void ToolUp_Pan(PointD e, ButtonReleaseEventArgs buts)
1378 if (currentTool.type == Tool.Type.pan &&
1379 (buts.Event.Button == 1 || buts.Event.Button == 2))
1381 currentTool =
GetTool(Tool.Type.move);
1384 private void StartKineticLoop()
1386 DateTime last = DateTime.UtcNow;
1388 GLib.Timeout.Add(16, () =>
1393 DateTime now = DateTime.UtcNow;
1394 double dt = (now - last).TotalSeconds;
1397 double speed = Math.Sqrt(
1398 panVelocity.X * panVelocity.X +
1399 panVelocity.Y * panVelocity.Y);
1401 if (speed < VelocityEpsilon)
1403 kineticActive =
false;
1408 double decel = PanFriction * dt;
1409 double scale = Math.Max(0, (speed - decel) / speed);
1411 panVelocity =
new PointD(
1412 panVelocity.X * scale,
1413 panVelocity.Y * scale);
1415 PointD origin = GetOrigin();
1416 origin =
new PointD(
1417 origin.X + panVelocity.X * dt,
1418 origin.Y + panVelocity.Y * dt);
1420 origin = ClampOrigin(origin);
1426 private PointD ClampOrigin(PointD origin)
1428 var img = ImageView.SelectedImage;
1429 var res0 = img.Resolutions[0];
1431 double viewW = App.viewer.ImageViewWidth / App.viewer.Resolution;
1432 double viewH = App.viewer.ImageViewHeight / App.viewer.Resolution;
1436 double maxX = res0.SizeX - viewW;
1437 double maxY = res0.SizeY - viewH;
1440 Math.Clamp(origin.X, minX, Math.Abs(maxX)),
1441 Math.Clamp(origin.Y, minY, Math.Abs(maxY)));
1443 private PointD GetOrigin()
1445 return ImageView.SelectedImage.isPyramidal
1446 ? App.viewer.PyramidalOrigin
1447 : App.viewer.Origin;
1450 private void SetOrigin(PointD p)
1452 if (ImageView.SelectedImage.isPyramidal)
1453 App.viewer.PyramidalOrigin = p;
1455 App.viewer.Origin = p;
1461 public void ToolDown_Magic(PointD e, ButtonPressEventArgs buts)
1463 if (selectedROI ==
null)
1465 selectedROI =
new ROI();
1466 selectedROI.type = ROI.Type.Freeform;
1467 selectedROI.AddPoint(
new PointD(e.X, e.Y));
1468 selectedROI.coord = App.viewer.GetCoordinate();
1469 if (ImageView.SelectedImage.isPyramidal)
1470 selectedROI.serie = App.viewer.Level;
1472 selectedROI.serie = ImageView.SelectedImage.series;
1473 AddROI(selectedROI);
1477 selectedROI.AddPoint(
new PointD(e.X, e.Y));
1481 public void ToolMove_Magic(PointD e, MotionNotifyEventArgs buts)
1484 PointD d =
new PointD(e.X - App.viewer.MouseDown.X, e.Y - App.viewer.MouseDown.Y);
1485 Tools.GetTool(Tools.Tool.Type.move).Rectangle =
new RectangleD(App.viewer.MouseDown.X, App.viewer.MouseDown.Y, d.X, d.Y);
1489 public void ToolUp_Magic(PointD e, ButtonReleaseEventArgs buts)
1491 if (!buts.Event.State.HasFlag(ModifierType.Button1Mask))
1494 RectangleD rectangle =
new RectangleD(
1495 App.viewer.MouseDown.X,
1496 App.viewer.MouseDown.Y,
1497 Math.Abs(App.viewer.MouseUp.X - App.viewer.MouseDown.X),
1498 Math.Abs(App.viewer.MouseUp.Y - App.viewer.MouseDown.Y)
1502 AForge.RectangleF rectInImageSpace = ImageView.SelectedImage.ToImageSpace(rectangle);
1503 ZCT coord = App.viewer.GetCoordinate();
1507 if (ImageView.SelectedImage.Buffers[0].RGBChannelsCount > 1)
1509 bitmap = ImageView.SelectedImage.GetFiltered(
1511 new IntRange((
int)ImageView.SelectedBuffer.Stats[0].Min, (
int)ImageView.SelectedBuffer.Stats[0].Max),
1512 new IntRange((
int)ImageView.SelectedBuffer.Stats[1].Min, (
int)ImageView.SelectedBuffer.Stats[1].Max),
1513 new IntRange((
int)ImageView.SelectedBuffer.Stats[2].Min, (
int)ImageView.SelectedBuffer.Stats[2].Max)
1518 bitmap = ImageView.SelectedImage.GetFiltered(
1520 new IntRange((
int)ImageView.SelectedBuffer.Stats[0].Min, (
int)ImageView.SelectedBuffer.Stats[0].Max),
1521 new IntRange((
int)ImageView.SelectedBuffer.Stats[0].Min, (
int)ImageView.SelectedBuffer.Stats[0].Max),
1522 new IntRange((
int)ImageView.SelectedBuffer.Stats[0].Min, (
int)ImageView.SelectedBuffer.Stats[0].Max)
1527 bitmap.Crop(rectInImageSpace.ToRectangleInt());
1529 Statistics[] st = Statistics.FromBytes(bitmap.Bytes, bitmap.SizeX, bitmap.Height, bitmap.RGBChannelsCount, bitmap.BitsPerPixel, bitmap.Stride, bitmap.PixelFormat);
1530 int threshold = magicSelect.Numeric ? magicSelect.Threshold :
CalculateThreshold(magicSelect.Index, st[0]);
1531 BlobCounter blobCounter =
new BlobCounter();
1532 OtsuThreshold th =
new OtsuThreshold();
1534 th.ApplyInPlace(bitmap);
1535 blobCounter.FilterBlobs =
true;
1536 blobCounter.MinWidth = 2;
1537 blobCounter.MinHeight = 2;
1538 blobCounter.ProcessImage(bitmap);
1540 Blob[] blobs = blobCounter.GetObjectsInformation();
1541 double pixelSizeX = ImageView.SelectedImage.PhysicalSizeX;
1542 double pixelSizeY = ImageView.SelectedImage.PhysicalSizeY;
1544 foreach (Blob blob
in blobs)
1546 AForge.RectangleD blobRectangle =
new AForge.RectangleD(
1547 blob.Rectangle.X * pixelSizeX,
1548 blob.Rectangle.Y * pixelSizeY,
1549 blob.Rectangle.Width * pixelSizeX,
1550 blob.Rectangle.Height * pixelSizeY
1553 PointD location =
new PointD(rectangle.X + blobRectangle.X, rectangle.Y + blobRectangle.Y);
1555 ROI annotation = ROI.CreateRectangle(coord, location.X, location.Y, blobRectangle.W, blobRectangle.H);
1556 ImageView.SelectedImage.Annotations.Add(annotation);
1558 App.viewer.UpdateView(
true);
1564 static int gridW = 2;
1565 static int gridH = 11;
1567 public enum ToolType
1602 string s = System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"/";
1604 if (tools.Count == 0)
1606 int x = 0;
int y = 0;
1607 foreach (
Tool.Type t in (
Tool.Type[])Enum.GetValues(typeof(
Tool.Type)))
1609 if (t != Type.script)
1610 if (t != Type.color1 && t != Type.color2)
1612 Tool tool =
new Tool(t,
new ColorS(0, 0, 0));
1613 tool.bounds =
new Rectangle(x * w, y * h, w, h);
1614 tool.image =
new Pixbuf(s +
"Images/" + tool.type.ToString() +
".png");
1615 tools.Add(tool.ToString(), tool);
1619 Tool tool =
new Tool(t,
new ColorS(0, 0, 0));
1620 tool.bounds =
new Rectangle(x * w, y * h, w, h);
1621 tools.Add(tool.ToString(), tool);
1624 if (x >
Tools.gridW - 1)
1631 DrawColor =
new ColorS(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue);
1633 public List<AForge.Point> Points;
1634 public ToolType toolType;
1635 public bool selected;
1636 public Pixbuf image;
1637 public Rectangle bounds;
1638 private RectangleD rect;
1639 public RectangleD Rectangle
1641 get {
return rect; }
1642 set { rect = value; }
1644 public RectangleF RectangleF
1646 get {
return new RectangleF((
float)rect.X, (
float)rect.Y, (
float)rect.W, (
float)rect.H); }
1648 public string script;
1650 public ColorS tolerance;
1651 public ColorS color;
1654 tolerance =
new ColorS(0, 0, 0);
1659 tolerance =
new ColorS(0, 0, 0);
1661 public Tool(Type t, ColorS col)
1665 tolerance =
new ColorS(0, 0, 0);
1667 public Tool(Type t, RectangleD r)
1671 tolerance =
new ColorS(0, 0, 0);
1673 public Tool(Type t,
string sc)
1677 tolerance =
new ColorS(0, 0, 0);
1679 public override string ToString()
1681 return type.ToString();
1686 public static Tool currentTool;
1688 public static ColorS DrawColor
1697 tools[Tool.Type.color1.ToString()].color = drawColor;
1701 public static ColorS EraseColor
1710 tools[Tool.Type.color2.ToString()].color = eraseColor;
1715 public static int StrokeWidth
1727 public bool IsPanning {
get;
set; }
1729 public static RectangleD selectionRect;
1730 public static TextInput ti =
null;
1740 if (tools.ContainsKey(name))
1741 return (
Tool)tools[name];
1742 else return tools[
Tool.Type.move.ToString()];
1744 public static Tool
GetTool(Tool.Type t)
1746 if (tools.ContainsKey(t.ToString()))
1747 return (Tool)tools[t.ToString()];
1748 else return tools.FirstOrDefault().Value;
1757 2 => (int)(stat.Min + stat.Mean),
1758 1 => (int)stat.Median,