GUI (work in progress..)
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Pia.fs
index 432bad9..1821f26 100644 (file)
@@ -1,7 +1,7 @@
 // ParasitemIA file format.
 module Parasitemia.GUI.Pia
 
-open System.Drawing
+open System.Windows
 open System.IO
 open System.IO.Compression
 
@@ -28,10 +28,10 @@ type JSONSourceImage = JsonProvider<"""
                 "infected": true,
                 "addedManually": false,
                 "removed": false,
-                "posX" : 42,
-                "posY" : 42,
-                "width" : 10,
-                "height" : 10,
+                "posX" : 42.5,
+                "posY" : 42.5,
+                "width" : 10.5,
+                "height" : 10.5,
                 "stainArea" : 10
             }
         ]
@@ -46,20 +46,38 @@ type JSONMainInformation = JsonProvider<"""
 """>
 
 let mainFilename = "info.json"
+let imageExtension = ".tiff"
 
 let save (filePath: string) (data: FileData) =
     use file = ZipFile.Open(filePath, ZipArchiveMode.Update)
 
-    let mainJSON = JSONMainInformation.Root(data.patientID)
-
-    let mainFile =
-        match file.GetEntry(mainFilename) with
-        | null -> file.CreateEntry(mainFilename)
-        | entry -> entry
+    for e in List.ofSeq file.Entries do // 'ofSeq' to not iterate a collection currently modified.
+        e.Delete()
 
+    // Main JSON file.
+    let mainJSON = JSONMainInformation.Root(data.patientID)
+    let mainFile = file.CreateEntry(mainFilename, CompressionLevel.Fastest)
     use mainFileWriter = new StreamWriter(mainFile.Open())
     mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None)
 
+    // Write each images and the associated information.
+    for imgSrc in data.sources do
+        let imgFilename = (string imgSrc.num) + imageExtension
+        let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
+        imgSrc.img.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
+
+        let imgJSON =
+            JSONSourceImage.Root([| for rbc in imgSrc.rbcs ->
+                                        JSONSourceImage.Rbc(
+                                            rbc.num,
+                                            rbc.infected, rbc.addedManually, rbc.removed,
+                                            decimal rbc.center.X, decimal rbc.center.Y, decimal rbc.size.Width, decimal rbc.size.Height,
+                                            rbc.stainArea) |])
+
+        let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
+        use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
+        imgJSON.JsonValue.WriteTo(imgJSONFileWriter, JsonSaveOptions.None)
+
 
 let load (filePath: string) : FileData =
     use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
@@ -67,4 +85,19 @@ let load (filePath: string) : FileData =
     let mainFile = file.GetEntry(mainFilename)
     let mainJSON = JSONMainInformation.Load(mainFile.Open())
 
-    { sources = []; patientID = mainJSON.PatientId }
\ No newline at end of file
+    let sources = [
+        let mutable imgNum = 0
+        for imgEntry in file.Entries do
+            let filename = imgEntry.Name
+            if filename.EndsWith(imageExtension)
+            then
+                let img = new Image<Bgr, byte>(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap?
+                imgNum <- imgNum + 1
+                let imgJSONEntry = file.GetEntry(filename + ".json")
+                let imgJSON = JSONSourceImage.Load(imgJSONEntry.Open())
+                yield { num = imgNum; img = img; rbcs = [ for rbc in imgJSON.Rbcs ->
+                                                            { num = rbc.Num;
+                                                              infected = rbc.Infected; addedManually = rbc.AddedManually; removed = rbc.Removed;
+                                                              center = Point(float rbc.PosX, float rbc.PosY); size = Size(float rbc.Width, float rbc.Height);
+                                                              stainArea = rbc.StainArea } ] } ]
+    { sources = sources; patientID = mainJSON.PatientId }
\ No newline at end of file