* Add area granulometry (not used for the moment)
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Pia.fs
diff --git a/Parasitemia/Parasitemia/GUI/Pia.fs b/Parasitemia/Parasitemia/GUI/Pia.fs
new file mode 100644 (file)
index 0000000..9ddd063
--- /dev/null
@@ -0,0 +1,71 @@
+// ParasitemIA file format.
+module Pia
+
+open System.Drawing
+open System.IO
+open System.IO.Compression
+
+open FSharp.Data
+
+open Emgu.CV
+open Emgu.CV.Structure
+
+let extension = ".pia"
+let filter = "PIA|*.pia"
+
+type RBC = {
+    infected: bool
+    addedManually: bool
+    removed: bool
+
+    center: Point
+    size: Size
+    stainArea: int }
+
+type ImageSource = {
+    img: Image<Bgr, byte>
+    rbcs: RBC list }
+
+type FileData = {
+    sources: ImageSource list
+    patientID: string }
+
+// The json type associated to a source image.
+type JSONSourceImage = JsonProvider<"""
+    {
+        "rbcs": [
+            { "infected": true, "addedManually": false, "removed": false, "posX" : 42, "posY" : 42, "width" : 10, "height" : 10, "stainArea" : 10 }
+        ]
+    }
+""">
+
+// The json type associated to a file.
+type JSONMainInformation = JsonProvider<"""
+    {
+        "patientID": "1234abcd"
+    }
+""">
+
+let mainFilename = "info.json"
+
+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
+
+    use mainFileWriter = new StreamWriter(mainFile.Open())
+    mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None)
+
+
+let load (filePath: string) : FileData =
+    use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
+
+    let mainFile = file.GetEntry(mainFilename)
+    let mainJSON = JSONMainInformation.Load(mainFile.Open())
+
+    { sources = []; patientID = mainJSON.PatientId }
\ No newline at end of file