module Parasitemia.GUI.Main open System 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) (fileToOpen: string option) = let app = new Application() let mainWindow = Views.MainWindow() let ctrl (name: string): 'a = mainWindow.Root.FindName(name) :?> 'a let colorRBCHealthy = Brushes.YellowGreen let colorRBCInfected = Brushes.Red let state = State.State() let mutable currentScale = 1. let mutable displayHealthy = false let menuExit: MenuItem = ctrl "menuExit" let menuSaveFile: MenuItem = ctrl "menuSave" let menuLoadFile: MenuItem = ctrl "menuOpen" let menuNewFile: MenuItem = ctrl "menuNew" let menuAddSourceImage: MenuItem = ctrl "menuAddSourceImage" let menuAnalysis: MenuItem = ctrl "menuAnalysis" let menuStartAnalysis: MenuItem = ctrl "menuStartAnalysis" let menuView: MenuItem = ctrl "menuView" let menuHightlightRBC: MenuItem = ctrl "menuHightlightRBC" let menuAbout: MenuItem = ctrl "menuAbout" let txtPatient: TextBox = ctrl "txtPatient" let txtGlobalParasitemia: TextBox = ctrl "txtGlobalParasitemia" let stackPreviews: StackPanel = ctrl "stackPreviews" let scrollViewCurrentImage: ScrollViewer = ctrl "scrollViewCurrentImage" let borderCurrentImage: Border = ctrl "borderCurrentImage" let canvasCurrentImage: Canvas = ctrl "canvasCurrentImage" let txtImageInformation: TextBlock = ctrl "txtImageInformation" let scrollRBC: ScrollViewer = ctrl "scrollRBC" let stackRBC: StackPanel = ctrl "stackRBC" // Initializations. menuHightlightRBC.IsChecked <- displayHealthy // Utils. let extractRBCPreview (img: Emgu.CV.Image) (rbc: RBC) : Emgu.CV.Image = let rbcWidth = rbc.size.Width let rbcHeight = rbc.size.Height let x = rbc.center.X - rbcWidth / 2. |> Utils.roundInt let y = rbc.center.Y - rbcHeight / 2. |> Utils.roundInt let w = Utils.roundInt rbcWidth let h = Utils.roundInt rbcHeight img.GetSubRect(System.Drawing.Rectangle(System.Drawing.Point((if x < 0 then 0 else x), (if y < 0 then 0 else y)), System.Drawing.Size((if x + w >= img.Width then img.Width - x else w), (if y + h >= img.Height then img.Height - y else h)))) let setRBCFrameStyle (rbc: RBC) (frame: Views.RBCFrame) = frame.Opacity <- if displayHealthy || rbc.setManually || rbc.infected then 1. else 0. let color = if rbc.infected then colorRBCInfected else colorRBCHealthy frame.manuallyAdded.Visibility <- if rbc.setManually then Visibility.Visible else Visibility.Hidden frame.manuallyAdded.Fill <- color frame.border.Stroke <- color let RBCFrameFromExisting (rbc: RBC) (frame: Views.RBCFrame) : Views.RBCFrame = frame.Visibility <- Visibility.Visible frame.Height <- rbc.size.Height frame.Width <- rbc.size.Width frame.Tag <- rbc setRBCFrameStyle rbc frame frame.border.StrokeThickness <- 1. frame.txtRBCNumber.Text <- rbc.num.ToString() frame let highlightRBCFrame (frame: Views.RBCFrame) (highlight: bool) = let rbc = frame.Tag :?> RBC if highlight then frame.border.StrokeThickness <- 3. if not rbc.infected && not rbc.setManually && not displayHealthy then frame.Opacity <- 1. else frame.border.StrokeThickness <- 1. if not rbc.infected && not rbc.setManually && not displayHealthy then frame.Opacity <- 0. let zoomToRBC (rbc: RBC) = scrollViewCurrentImage.ScrollToHorizontalOffset(rbc.center.X * currentScale - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left) scrollViewCurrentImage.ScrollToVerticalOffset(rbc.center.Y * currentScale - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top) let parasitemiaText (nbTotal: int, nbInfected: int) : string = if nbTotal = 0 then "" else let percent = 100. * (float nbInfected) / (float nbTotal) sprintf "%.1f %% (%d / %d)" percent nbInfected nbTotal let updateCurrentImageInformation () = match state.CurrentImage with | Some srcImg -> let parasitemiaStr = parasitemiaText (state.ImageParasitemia srcImg) txtImageInformation.Inlines.Clear() txtImageInformation.Inlines.Add(Documents.Run("Parasitemia: ", FontWeight = FontWeights.Bold)) txtImageInformation.Inlines.Add(parasitemiaStr) txtImageInformation.Inlines.Add(Documents.LineBreak()) txtImageInformation.Inlines.Add(Documents.Run("Average erytrocyte diameter: ", FontWeight = FontWeights.Bold)) txtImageInformation.Inlines.Add(Documents.Run(srcImg.config.RBCRadius.ToString())) txtImageInformation.Inlines.Add(Documents.LineBreak()) txtImageInformation.Inlines.Add(Documents.Run("Last analysis: ", FontWeight = FontWeights.Bold)) txtImageInformation.Inlines.Add(Documents.Run(if srcImg.dateLastAnalysis.Ticks = 0L then "" else srcImg.dateLastAnalysis.ToLocalTime().ToString())) | _ -> () let updateGlobalParasitemia () = txtGlobalParasitemia.Text <- parasitemiaText state.GlobalParasitemia let updateViewportPreview () = for preview in stackPreviews.Children |> Seq.cast do let srcImg = preview.Tag :?> SourceImage if Some srcImg = state.CurrentImage then preview.viewport.Visibility <- Visibility.Visible let canvasWidth = canvasCurrentImage.ActualWidth * currentScale let canvasHeight = canvasCurrentImage.ActualHeight * currentScale let previewWidth = (preview.ActualWidth - preview.BorderThickness.Left - preview.BorderThickness.Right) let previewHeight = (preview.ActualHeight - preview.BorderThickness.Top - preview.BorderThickness.Bottom) let marginLeft = previewWidth * (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Left) / canvasWidth - 2. let marginRight = previewWidth * (canvasWidth - (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Right) - scrollViewCurrentImage.ViewportWidth) / canvasWidth - 2. let marginTop = previewHeight * (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Top) / canvasHeight - 2. let marginBottom = previewHeight * (canvasHeight - (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Bottom) - scrollViewCurrentImage.ViewportHeight) / canvasHeight - 2. preview.viewport.Margin <- Thickness( marginLeft, marginTop, marginRight, marginBottom) else preview.viewport.Visibility <- Visibility.Hidden let rec setAsInfected (rbc: RBC) (infected: bool) = state.SetAsInfected rbc infected canvasCurrentImage.Children |> Seq.cast |> Seq.iter (fun frame -> if (frame.Tag :?> RBC) = rbc then setRBCFrameStyle rbc frame) updateRBCFramesPreview () updateCurrentImageInformation () updateGlobalParasitemia () and RBCFrame (rbc: RBC) : Views.RBCFrame = let frame = RBCFrameFromExisting rbc (Views.RBCFrame()) frame.SetValue(Panel.ZIndexProperty, Int32.MaxValue - rbc.num) // To be sure the frame.menuRBCSetAsHealthy.Click.AddHandler(fun obj args -> setAsInfected (frame.Tag :?> RBC) false) frame.menuRBCSetAsInfected.Click.AddHandler(fun obj args -> setAsInfected (frame.Tag :?> RBC) true) frame.ContextMenuOpening.AddHandler( fun obj args -> if (frame.Tag :?> RBC).infected then frame.menuRBCSetAsHealthy.Visibility <- Visibility.Visible frame.menuRBCSetAsInfected.Visibility <- Visibility.Collapsed else frame.menuRBCSetAsHealthy.Visibility <- Visibility.Collapsed frame.menuRBCSetAsInfected.Visibility <- Visibility.Visible) frame.ContextMenuClosing.AddHandler(fun obj args -> if not frame.IsMouseOver then highlightRBCFrame frame false ) frame.MouseEnter.AddHandler(fun obj args -> highlightRBCFrame frame true) frame.MouseLeave.AddHandler(fun obj args -> if not frame.grid.ContextMenu.IsOpen then highlightRBCFrame frame false) frame and updateRBCFramesPreview () = match state.CurrentImage with | Some srcImg -> let mutable currentPreview = 0 for rbc in srcImg.rbcs |> List.filter (fun rbc -> displayHealthy || rbc.infected) do let previewInfected = if currentPreview < stackRBC.Children.Count then RBCFrameFromExisting rbc (stackRBC.Children.[currentPreview] :?> Views.RBCFrame) else let f = RBCFrame rbc f.MouseLeftButtonUp.AddHandler(fun obj args -> zoomToRBC (f.Tag :?> RBC)) stackRBC.Children.Add(f) |> ignore f currentPreview <- currentPreview + 1 previewInfected.Height <- stackRBC.ActualHeight previewInfected.Width <- stackRBC.ActualHeight * rbc.size.Width / rbc.size.Height previewInfected.border.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img rbc)) stackRBC.Children.RemoveRange(currentPreview, stackRBC.Children.Count - currentPreview) | _ -> () updateViewportPreview () let updateRBCFramesCurrent () = match state.CurrentImage with | Some srcImg -> let mutable currentCanvas = 0 for rbc in srcImg.rbcs do let frame = if currentCanvas < canvasCurrentImage.Children.Count then RBCFrameFromExisting rbc (canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame) else let f = RBCFrame rbc f.Root.Opacity <- 0.7 canvasCurrentImage.Children.Add(f) |> ignore f currentCanvas <- currentCanvas + 1 Canvas.SetLeft(frame, rbc.center.X - rbc.size.Width / 2.) Canvas.SetTop(frame, rbc.center.Y - rbc.size.Height / 2.) for i in currentCanvas .. canvasCurrentImage.Children.Count - 1 do canvasCurrentImage.Children.[i].Visibility <- Visibility.Hidden | _ -> () let saveCurrentDocument () = 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() // Ask the use to save the current document if neccessary. let askSaveCurrent () = if state.AlteredSinceLastSave then match MessageBox.Show("Would you like to save the current document?", "Saving the current document", MessageBoxButton.YesNo, MessageBoxImage.Question) with | MessageBoxResult.Yes -> saveCurrentDocument () | _ -> () let updateCurrentImage () = match state.CurrentImage with | Some srcImg -> // Highlight the preview. stackPreviews.Children |> Seq.cast |> Seq.iter (fun preview -> preview.border.BorderThickness <- Thickness(if preview.Tag = (srcImg :> Object) then 3. else 0.)) canvasCurrentImage.Height <- float srcImg.img.Height canvasCurrentImage.Width <- float srcImg.img.Width canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img)) updateRBCFramesCurrent () updateRBCFramesPreview () updateCurrentImageInformation () | None -> stackRBC.Children.Clear() canvasCurrentImage.Children.Clear() canvasCurrentImage.Background <- Brushes.Black let setCurrentImage (srcImg: SourceImage) = if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg then state.CurrentImage <- Some srcImg updateCurrentImage () let addPreview (srcImg: SourceImage) = let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.)) imgCtrl.menuRemoveImage.Click.AddHandler(fun obj args -> stackPreviews.Children.Remove(imgCtrl) let srcImg = imgCtrl.Tag :?> SourceImage let currentRemoved = Some srcImg = state.CurrentImage state.RemoveSourceImage srcImg if currentRemoved then updateCurrentImage() stackPreviews.Children |> Seq.cast |> Seq.iter (fun imgPreview -> imgPreview.txtImageNumber.Text <- (imgPreview.Tag :?> SourceImage).num.ToString())) imgCtrl.Tag <- srcImg imgCtrl.txtImageNumber.Text <- srcImg.num.ToString() 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 // Zoom to a mouse position into the control 'imgCtrl'. let zoomTo (mousePos: Point) = let canvasW = canvasCurrentImage.ActualWidth * currentScale let canvasH = canvasCurrentImage.ActualHeight * currentScale let centerX = (mousePos.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW let centerY = (mousePos.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH scrollViewCurrentImage.ScrollToHorizontalOffset(centerX - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left) scrollViewCurrentImage.ScrollToVerticalOffset(centerY - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top) imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args -> setCurrentImage (state.SourceImages |> Seq.find (fun srcImg -> (srcImg :> Object) = imgCtrl.Tag)) imgCtrl.UpdateLayout() zoomTo (args.GetPosition(imgCtrl)) imgCtrl.CaptureMouse() |> ignore) imgCtrl.MouseMove.AddHandler(fun obj args -> if imgCtrl.IsMouseCaptured then zoomTo (args.GetPosition(imgCtrl))) imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args -> if imgCtrl.IsMouseCaptured then imgCtrl.ReleaseMouseCapture()) let updatePreviews () = stackPreviews.Children.Clear () for srcImg in state.SourceImages do addPreview srcImg updateCurrentImage () let updateGUI () = txtPatient.Text <- state.PatientID updatePreviews () updateGlobalParasitemia () let loadFile (filepath: string) = askSaveCurrent () state.FilePath <- filepath state.Load() updateGUI () txtPatient.LostFocus.AddHandler(fun obj args -> state.PatientID <- txtPatient.Text) menuExit.Click.AddHandler(fun obj args -> askSaveCurrent () mainWindow.Root.Close()) menuSaveFile.Click.AddHandler(fun obj args -> saveCurrentDocument ()) menuLoadFile.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 loadFile dialog.FileName) menuNewFile.Click.AddHandler(fun obj args -> askSaveCurrent () state.Reset() updateGUI()) menuAddSourceImage.Click.AddHandler(fun obj args -> let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff", Multiselect = true) let res = dialog.ShowDialog() if res.HasValue && res.Value then let noSourceImage = state.SourceImages.Count() = 0 for filename in dialog.FileNames do let srcImg = state.AddSourceImage filename defaultConfig addPreview srcImg updateGlobalParasitemia () if noSourceImage then updateCurrentImage ()) menuAnalysis.SubmenuOpened.AddHandler(fun obj args -> menuStartAnalysis.IsEnabled <- state.SourceImages.Count() > 0) menuStartAnalysis.Click.AddHandler(fun obj args -> if Analysis.showWindow mainWindow.Root state then updateGlobalParasitemia () updateCurrentImage ()) menuHightlightRBC.Click.AddHandler(fun obj args -> displayHealthy <- menuHightlightRBC.IsChecked updateRBCFramesPreview () updateRBCFramesCurrent ()) menuAbout.Click.AddHandler(fun obj args -> About.showWindow mainWindow.Root) // 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 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 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) // Viewport preview. scrollViewCurrentImage.ScrollChanged.AddHandler(fun obj args -> updateViewportPreview ()) mainWindow.Root.Show() match fileToOpen with | Some filepath -> loadFile filepath | None -> () app.Run()