namespace ParasitemiaUI open System open System.IO open System.Windows.Media open Emgu.CV open Emgu.CV.Structure open Types type ImageData = | FromMemory of Image | FromFile of string // This file will be stored in a temporary directory until the image is saved in a piaz file. type SourceImage (num : int, name : string, originalName : string, config : ParasitemiaCore.Config.Config, dateLastAnalysis : DateTime, imgData : ImageData, rbcs : RBC list) = let mutable num = num let mutable name = name let mutable config = config let mutable dateLastAnalysis = dateLastAnalysis // UTC. let img = match imgData with | FromMemory i -> i | FromFile path -> new Image (path) let mutable tempFile : string option = match imgData with | FromMemory _ -> None | FromFile path -> let tmpDirname = Guid.NewGuid () let filename = Path.GetFileName path // Copy it to a temporary directory. let tmpDir = Path.Combine (Path.GetTempPath (), string tmpDirname) Directory.CreateDirectory tmpDir |> ignore let tmpPath = Path.Combine (tmpDir, filename) File.Copy (path, tmpPath) Some tmpPath let mutable rbcs = rbcs let mutable healthyRBCBrightness = 1.f let mutable infectedRBCBrightness = 1.f let mutable averageRBCSize = 1. let healthyRBColor = Color.FromRgb (255uy, 255uy, 0uy) // Yellow-green. let infectedRBColor = Color.FromRgb (255uy, 0uy, 40uy) // Red with a bit of blue. let updateAverageRBCSize () = if List.isEmpty rbcs |> not then averageRBCSize <- rbcs |> List.collect (fun rbc -> [ rbc.size.Width; rbc.size.Height ]) |> List.average do updateAverageRBCSize () member this.Num with get () = num and set value = num <- value member this.RomanNum = Utils.toRomanNumber this.Num member this.Name with get () = name and set value = name <- value member this.OriginalName = originalName member this.Config = config member this.DateLastAnalysis with get () = dateLastAnalysis and set value = dateLastAnalysis <- value member this.Img = img member this.TempFile with get () = tempFile and set value = tempFile <- value // TODO: remove temp file if set to None member this.RBCs with get () = rbcs and set value = rbcs <- value updateAverageRBCSize () member this.ImageParasitemia : int * int = List.length rbcs, rbcs |> List.fold (fun nbInfected rbc -> if rbc.infected then nbInfected + 1 else nbInfected) 0 member this.ImageNbManuallyChangedRBC (setAsInfected : bool) : int * int = List.length rbcs, rbcs |> List.fold (fun nb rbc -> if rbc.setManually && rbc.infected = setAsInfected then nb + 1 else nb) 0 member this.ImageNbManuallyChangedRBCStr (setAsInfected : bool) : string = Utils.percentText (this.ImageNbManuallyChangedRBC setAsInfected) member this.ImageManuallyChangedRBC (setAsInfected : bool) : int seq = query { for rbc in rbcs do where (rbc.setManually && rbc.infected = setAsInfected) select rbc.num } member this.ImageManuallyChangedRBCStr (setAsInfected : bool) : string = let listStr = Utils.listAsStr <| this.ImageManuallyChangedRBC setAsInfected if listStr = "" then "" else "[" + listStr + "]" member this.HealthyRBCBrightness with get () = healthyRBCBrightness and set value = healthyRBCBrightness <- value member this.InfectedRBCBrightness with get () = infectedRBCBrightness and set value = infectedRBCBrightness <- value member this.HealthyRBCColor : SolidColorBrush = let mutable color = healthyRBColor * healthyRBCBrightness color.A <- 255uy SolidColorBrush (color) member this.InfectedRBCColor : SolidColorBrush = let mutable color = infectedRBColor * infectedRBCBrightness color.A <- 255uy SolidColorBrush (color) member this.AverageRBCSize = averageRBCSize