Save imported image in the same format (WIP)
[master-thesis.git] / Parasitemia / ParasitemiaUI / SourceImage.fs
1 namespace ParasitemiaUI
2
3 open System
4 open System.IO
5 open System.Windows.Media
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Types
11
12 type ImageData =
13 | FromMemory of Image<Bgr, byte>
14 | FromFile of string // This file will be stored in a temporary directory until the image is saved in a piaz file.
15
16 type SourceImage (num : int, name : string, originalName : string, config : ParasitemiaCore.Config.Config, dateLastAnalysis : DateTime, imgData : ImageData, rbcs : RBC list) =
17 let mutable num = num
18 let mutable name = name
19 let mutable config = config
20 let mutable dateLastAnalysis = dateLastAnalysis // UTC.
21 let img =
22 match imgData with
23 | FromMemory i -> i
24 | FromFile path -> new Image<Bgr, byte> (path)
25
26 let mutable tempFile : string option =
27 match imgData with
28 | FromMemory _ -> None
29 | FromFile path ->
30 let tmpDirname = Guid.NewGuid ()
31 let filename = Path.GetFileName path
32
33 // Copy it to a temporary directory.
34 let tmpDir = Path.Combine (Path.GetTempPath (), string tmpDirname)
35 Directory.CreateDirectory tmpDir |> ignore
36 let tmpPath = Path.Combine (tmpDir, filename)
37
38 File.Copy (path, tmpPath)
39
40 Some tmpPath
41
42 let mutable rbcs = rbcs
43 let mutable healthyRBCBrightness = 1.f
44 let mutable infectedRBCBrightness = 1.f
45
46 let mutable averageRBCSize = 1.
47
48 let healthyRBColor = Color.FromRgb (255uy, 255uy, 0uy) // Yellow-green.
49 let infectedRBColor = Color.FromRgb (255uy, 0uy, 40uy) // Red with a bit of blue.
50
51 let updateAverageRBCSize () =
52 if List.isEmpty rbcs |> not then
53 averageRBCSize <-
54 rbcs
55 |> List.collect (fun rbc -> [ rbc.size.Width; rbc.size.Height ])
56 |> List.average
57
58 do
59 updateAverageRBCSize ()
60
61 member this.Num with get () = num and set value = num <- value
62
63 member this.RomanNum = Utils.toRomanNumber this.Num
64
65 member this.Name with get () = name and set value = name <- value
66
67 member this.OriginalName = originalName
68
69 member this.Config = config
70
71 member this.DateLastAnalysis with get () = dateLastAnalysis and set value = dateLastAnalysis <- value
72
73 member this.Img = img
74
75 member this.TempFile
76 with get () = tempFile
77 and set value = tempFile <- value // TODO: remove temp file if set to None
78
79 member this.RBCs
80 with get () = rbcs
81 and set value =
82 rbcs <- value
83 updateAverageRBCSize ()
84
85 member this.ImageParasitemia : int * int =
86 List.length rbcs,
87 rbcs |> List.fold (fun nbInfected rbc -> if rbc.infected then nbInfected + 1 else nbInfected) 0
88
89 member this.ImageNbManuallyChangedRBC (setAsInfected : bool) : int * int =
90 List.length rbcs,
91 rbcs |> List.fold (fun nb rbc -> if rbc.setManually && rbc.infected = setAsInfected then nb + 1 else nb) 0
92
93 member this.ImageNbManuallyChangedRBCStr (setAsInfected : bool) : string =
94 Utils.percentText (this.ImageNbManuallyChangedRBC setAsInfected)
95
96 member this.ImageManuallyChangedRBC (setAsInfected : bool) : int seq =
97 query {
98 for rbc in rbcs do
99 where (rbc.setManually && rbc.infected = setAsInfected)
100 select rbc.num
101 }
102
103 member this.ImageManuallyChangedRBCStr (setAsInfected : bool) : string =
104 let listStr = Utils.listAsStr <| this.ImageManuallyChangedRBC setAsInfected
105 if listStr = "" then
106 ""
107 else
108 "[" + listStr + "]"
109
110 member this.HealthyRBCBrightness with get () = healthyRBCBrightness and set value = healthyRBCBrightness <- value
111 member this.InfectedRBCBrightness with get () = infectedRBCBrightness and set value = infectedRBCBrightness <- value
112
113 member this.HealthyRBCColor : SolidColorBrush =
114 let mutable color = healthyRBColor * healthyRBCBrightness
115 color.A <- 255uy
116 SolidColorBrush (color)
117
118 member this.InfectedRBCColor : SolidColorBrush =
119 let mutable color = infectedRBColor * infectedRBCBrightness
120 color.A <- 255uy
121 SolidColorBrush (color)
122
123 member this.AverageRBCSize = averageRBCSize