The images to be analyzed can be selected.
[master-thesis.git] / Parasitemia / Parasitemia / GUI / PiaZ.fs
index e330a2d..22359e9 100644 (file)
@@ -6,48 +6,36 @@ open System.Windows
 open System.IO
 open System.IO.Compression
 
-open FSharp.Data
-
 open Emgu.CV
 open Emgu.CV.Structure
 
+open Newtonsoft.Json
+open Newtonsoft.Json.Converters
+
 open Types
 
 let extension = ".piaz"
 let filter = "PIA|*.piaz"
 
+// Information associated to a document.
+type JSONInformation = {
+    patientID: string
+}
+
+// Information associated to each images.
+type JSONSourceImage = {
+    num: int
+    parameters: Config.Parameters
+    dateLastAnalysis: DateTime
+    rbcs: RBC List
+}
+
 type FileData = {
-    sources: SourceImage list
-    patientID: string }
-
-// The json type associated to a source image.
-type JSONSourceImage = JsonProvider<"""
-    {
-        "RBCRadius" : 32.5,
-        "dateLastAnalysis" : 1.5,
-        "rbcs": [
-            {
-                "num": 1,
-                "infected": true,
-                "setManually": false,
-                "posX" : 42.5,
-                "posY" : 42.5,
-                "width" : 10.5,
-                "height" : 10.5,
-                "stainArea" : 10
-            }
-        ]
-    }
-""">
-
-// The json type associated to a file.
-type JSONMainInformation = JsonProvider<"""
-    {
-        "patientID": "1234abcd"
-    }
-""">
-
-let mainFilename = "info.json"
+    patientID: string
+    images: SourceImage list
+}
+
+let mainEntryName = "info.json"
 let imageExtension = ".tiff"
 
 let save (filePath: string) (data: FileData) =
@@ -57,55 +45,40 @@ let save (filePath: string) (data: FileData) =
         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)
+    let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
+    use mainEntryWriter = new StreamWriter(mainEntry.Open())
+    mainEntryWriter.Write(JsonConvert.SerializeObject({ JSONInformation.patientID = data.patientID }))
 
     // Write each images and the associated information.
-    for imgSrc in data.sources do
-        let imgFilename = (string imgSrc.num) + imageExtension
+    for srcImg in data.images do
+        let imgFilename = (string srcImg.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(decimal imgSrc.rbcRadius,
-                                 decimal <| imgSrc.dateLastAnalysis.ToFileTimeUtc(),
-                                 [| for rbc in imgSrc.rbcs ->
-                                       JSONSourceImage.Rbc(
-                                            rbc.num,
-                                            rbc.infected, rbc.setManually,
-                                            decimal rbc.center.X, decimal rbc.center.Y, decimal rbc.size.Width, decimal rbc.size.Height,
-                                            rbc.infectedArea) |])
+        srcImg.img.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
 
         let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
         use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
-        imgJSON.JsonValue.WriteTo(imgJSONFileWriter, JsonSaveOptions.None)
+        imgJSONFileWriter.Write(JsonConvert.SerializeObject({ num = srcImg.num; parameters = srcImg.config.Parameters; dateLastAnalysis = srcImg.dateLastAnalysis; rbcs = srcImg.rbcs }))
 
 
 let load (filePath: string) : FileData =
     use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
 
-    let mainFile = file.GetEntry(mainFilename)
-    let mainJSON = JSONMainInformation.Load(mainFile.Open())
-
-    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
-                        rbcRadius = float imgJSON.RbcRadius
-                        dateLastAnalysis = DateTime.FromFileTimeUtc(int64 imgJSON.DateLastAnalysis)
-                        img = img
-                        rbcs = [ for rbc in imgJSON.Rbcs ->
-                                    { num = rbc.Num;
-                                      infected = rbc.Infected; setManually = rbc.SetManually;
-                                      center = Point(float rbc.PosX, float rbc.PosY); size = Size(float rbc.Width, float rbc.Height);
-                                      infectedArea = rbc.StainArea } ] } ]
-    { sources = sources; patientID = mainJSON.PatientId }
\ No newline at end of file
+    let mainEntry = file.GetEntry(mainEntryName)
+    use mainEntryReader = new StreamReader(mainEntry.Open())
+    let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
+
+    { patientID = info.patientID
+      images = [ let mutable imgNum = 0
+                 for imgEntry in file.Entries do
+                    if imgEntry.Name.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 imgEntry = file.GetEntry(imgEntry.Name + ".json")
+                        use imgEntryFileReader = new StreamReader(imgEntry.Open())
+                        let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgEntryFileReader.ReadToEnd())
+                        yield { num = imgNum
+                                config = Config.Config(imgInfo.parameters)
+                                dateLastAnalysis = imgInfo.dateLastAnalysis
+                                img = img
+                                rbcs = imgInfo.rbcs } ] }
\ No newline at end of file