module Parasitemia.GUI.Main open System.IO open System.Linq open System.Windows open System.Windows.Media open System.Windows.Markup open System.Windows.Shapes open System.Windows.Controls open System.Diagnostics open Microsoft.Win32 // For the common dialogs. open Emgu.CV.WPF open Config open Types let run (defaultConfig: Config) = let app = new Application() let mainWindow = Views.MainWindow() let ctrl (name: string): 'a = mainWindow.Root.FindName(name) :?> 'a // Utils.log <- (fun m -> log mainWindow m) let colorRBCHealthy = Brushes.YellowGreen let colorRBCInfected = Brushes.Red let state = State.State() let exit: MenuItem = ctrl "menuExit" let saveFile: MenuItem = ctrl "menuSave" let loadFile: MenuItem = ctrl "menuOpen" let newFile: MenuItem = ctrl "menuNew" let addSourceImage: MenuItem = ctrl "menuAddSourceImage" let txtPatient: TextBox = ctrl "txtPatient" let stackPreviews: StackPanel = ctrl "stackPreviews" let butStartAnalysis: Button = ctrl "butStartAnalysis" let scrollViewCurrentImage: ScrollViewer = ctrl "scrollViewCurrentImage" let borderCurrentImage: Border = ctrl "borderCurrentImage" let canvasCurrentImage: Canvas = ctrl "canvasCurrentImage" // Initializations. // Operations. let synchronizeState () = state.PatientID <- txtPatient.Text let setCurrentImage (srcImg: SourceImage) = state.CurrentImage <- Some srcImg // Highlight the preview. stackPreviews.Children |> Seq.cast |> Seq.iter (fun preview -> preview.border.BorderThickness <- Thickness(if preview.lblImageNumber.Content = box srcImg.num then 3. else 0.)) canvasCurrentImage.Height <- float srcImg.img.Height canvasCurrentImage.Width <- float srcImg.img.Width canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img)) // Remove all image canvas children and add the RBC. canvasCurrentImage.Children.Clear() for rbc in srcImg.rbcs do let rectangle = Rectangle( Height = rbc.size.Height, Width = rbc.size.Width, Stroke = (if rbc.infected then colorRBCInfected else colorRBCHealthy), StrokeThickness = 1., Fill = SolidColorBrush(Color.FromArgb(0uy, 0uy, 0uy, 0uy)), Tag = rbc, Opacity = if rbc.infected then 1. else 0.) Canvas.SetLeft(rectangle, rbc.center.X - rbc.size.Width / 2.) Canvas.SetTop(rectangle, rbc.center.Y - rbc.size.Height / 2.) canvasCurrentImage.Children.Add(rectangle) |> ignore rectangle.MouseEnter.AddHandler( fun obj args -> match obj with | :? Rectangle as r -> r.StrokeThickness <- 3. if not (r.Tag :?> RBC).infected then r.Opacity <- 1. | _ -> ()) rectangle.MouseLeave.AddHandler( fun obj args -> match obj with | :? Rectangle as r -> r.StrokeThickness <- 1. if not (r.Tag :?> RBC).infected then r.Opacity <- 0. | _ -> ()) let addPreview (srcImg: SourceImage) = let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.)) imgCtrl.lblImageNumber.Content <- srcImg.num let width = 200 let height = srcImg.img.Height * width / srcImg.img.Width imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic)) stackPreviews.Children.Add(imgCtrl) |> ignore imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args -> setCurrentImage (state.SourceImages |> Seq.find (fun i -> box i.num = (obj :?> Views.ImageSourcePreview).lblImageNumber.Content))) let updatePreviews () = stackPreviews.Children.Clear () if state.SourceImages.Count() > 0 then for srcImg in state.SourceImages do addPreview srcImg setCurrentImage (state.SourceImages.First()) let updateGUI () = txtPatient.Text <- state.PatientID updatePreviews () exit.Click.AddHandler(fun obj args -> mainWindow.Root.Close()) saveFile.Click.AddHandler(fun obj args -> synchronizeState () if state.FilePath = "" then let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension, Filter = PiaZ.filter); let res = dialog.ShowDialog() if res.HasValue && res.Value then state.FilePath <- dialog.FileName state.Save() else state.Save()) loadFile.Click.AddHandler(fun obj args -> // TODO: if current state not saved and not empty, ask to save it. let dialog = OpenFileDialog(Filter = PiaZ.filter) let res = dialog.ShowDialog() if res.HasValue && res.Value then state.FilePath <- dialog.FileName state.Load() updateGUI ()) newFile.Click.AddHandler(fun obj args -> // TODO: if current state not saved and not empty, ask to save it. state.Reset() updateGUI()) addSourceImage.Click.AddHandler(fun obj args -> let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff") let res = dialog.ShowDialog() if res.HasValue && res.Value then let srcImg = state.AddSourceImage(dialog.FileName) addPreview srcImg if state.SourceImages.Count() = 1 then setCurrentImage srcImg) butStartAnalysis.Click.AddHandler(fun obj args -> let results = ImageAnalysis.doMultipleAnalysis (state.SourceImages |> Seq.map (fun srcImg -> string srcImg.num, srcImg.img) |> Seq.toList) defaultConfig for id, cells in results do state.SetResult (int id) cells ) // Zoom on the current image. let adjustCurrentImageBorders (deltaX: float) (deltaY: float) = borderCurrentImage.BorderThickness <- Thickness( (scrollViewCurrentImage.ViewportWidth + deltaX) / 2., (scrollViewCurrentImage.ViewportHeight + deltaY) / 2., (scrollViewCurrentImage.ViewportWidth + deltaX) / 2., (scrollViewCurrentImage.ViewportHeight + deltaY) / 2.) canvasCurrentImage.SizeChanged.AddHandler(fun obj args -> let deltaX = args.NewSize.Width - args.PreviousSize.Width let deltaY = args.NewSize.Height - args.PreviousSize.Height if deltaX > 0.5 || deltaY > 0.5 then adjustCurrentImageBorders 0.0 0.0 // Center the view at the center of the image initialy. scrollViewCurrentImage.UpdateLayout() scrollViewCurrentImage.ScrollToHorizontalOffset(borderCurrentImage.ActualWidth / 2. - scrollViewCurrentImage.ViewportWidth / 2.) scrollViewCurrentImage.ScrollToVerticalOffset(borderCurrentImage.ActualHeight / 2. - scrollViewCurrentImage.ViewportHeight / 2.)) scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args -> let deltaX = args.NewSize.Width - args.PreviousSize.Width let deltaY = args.NewSize.Height - args.PreviousSize.Height adjustCurrentImageBorders deltaX deltaY scrollViewCurrentImage.ScrollToHorizontalOffset(scrollViewCurrentImage.HorizontalOffset + deltaX / 8.) scrollViewCurrentImage.ScrollToVerticalOffset(scrollViewCurrentImage.VerticalOffset + deltaY / 8.)) let mutable currentScale = 1. let mutable maxScale = 4. let mutable minScale = 0.25 let currentImageScaleTransform = ScaleTransform() canvasCurrentImage.LayoutTransform <- currentImageScaleTransform borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args -> let scaleFactor = if args.Delta > 0 then 2.0 else 0.5 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale then let previousScale = currentScale currentScale <- let newScale = currentScale * scaleFactor if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale let realScaleFactor = currentScale / previousScale let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - borderCurrentImage.BorderThickness.Left let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - borderCurrentImage.BorderThickness.Top //canvasCurrentImage.Margin <- Thickness(canvasCurrentImage.Margin.Top * realScaleFactor) currentImageScaleTransform.ScaleX <- currentScale currentImageScaleTransform.ScaleY <- currentScale scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left) scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top) args.Handled <- true) // Pan on the current image. let mutable scrollStartPosition = Point(0., 0.) let mutable scrollStartOffsetX = 0. let mutable scrollStartOffsetY = 0. borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args -> scrollStartPosition <- args.GetPosition(scrollViewCurrentImage) scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset borderCurrentImage.Cursor <- Input.Cursors.ScrollAll borderCurrentImage.CaptureMouse() |> ignore args.Handled <- true) borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args -> if borderCurrentImage.IsMouseCaptured then let position = args.GetPosition(scrollViewCurrentImage) let deltaX = scrollStartPosition.X - position.X let deltaY = scrollStartPosition.Y - position.Y scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX) scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY) args.Handled <- true) borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args -> if borderCurrentImage.IsMouseCaptured then borderCurrentImage.Cursor <- Input.Cursors.Arrow borderCurrentImage.ReleaseMouseCapture() args.Handled <- true) // display mainWindow img mainWindow.Root.Show() app.Run()