Save imported image in the same format (WIP)
[master-thesis.git] / Parasitemia / ParasitemiaUI / SourceImage.fs
index 7f5c245..8dd20df 100644 (file)
@@ -1,6 +1,7 @@
 namespace ParasitemiaUI
 
 open System
+open System.IO
 open System.Windows.Media
 
 open Emgu.CV
@@ -8,12 +9,36 @@ open Emgu.CV.Structure
 
 open Types
 
-type SourceImage (num : int, name : string, config : ParasitemiaCore.Config.Config, dateLastAnalysis : DateTime, img : Image<Bgr, byte>, rbcs : RBC list) =
+type ImageData =
+    | FromMemory of Image<Bgr, byte>
+    | 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 = img
+    let img =
+        match imgData with
+        | FromMemory i -> i
+        | FromFile path -> new Image<Bgr, byte> (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
@@ -39,12 +64,18 @@ type SourceImage (num : int, name : string, config : ParasitemiaCore.Config.Conf
 
     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 =