+// 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