* Add area granulometry (not used for the moment)
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Pia.fs
1 // ParasitemIA file format.
2 module Pia
3
4 open System.Drawing
5 open System.IO
6 open System.IO.Compression
7
8 open FSharp.Data
9
10 open Emgu.CV
11 open Emgu.CV.Structure
12
13 let extension = ".pia"
14 let filter = "PIA|*.pia"
15
16 type RBC = {
17 infected: bool
18 addedManually: bool
19 removed: bool
20
21 center: Point
22 size: Size
23 stainArea: int }
24
25 type ImageSource = {
26 img: Image<Bgr, byte>
27 rbcs: RBC list }
28
29 type FileData = {
30 sources: ImageSource list
31 patientID: string }
32
33 // The json type associated to a source image.
34 type JSONSourceImage = JsonProvider<"""
35 {
36 "rbcs": [
37 { "infected": true, "addedManually": false, "removed": false, "posX" : 42, "posY" : 42, "width" : 10, "height" : 10, "stainArea" : 10 }
38 ]
39 }
40 """>
41
42 // The json type associated to a file.
43 type JSONMainInformation = JsonProvider<"""
44 {
45 "patientID": "1234abcd"
46 }
47 """>
48
49 let mainFilename = "info.json"
50
51 let save (filePath: string) (data: FileData) =
52 use file = ZipFile.Open(filePath, ZipArchiveMode.Update)
53
54 let mainJSON = JSONMainInformation.Root(data.patientID)
55
56 let mainFile =
57 match file.GetEntry(mainFilename) with
58 | null -> file.CreateEntry(mainFilename)
59 | entry -> entry
60
61 use mainFileWriter = new StreamWriter(mainFile.Open())
62 mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None)
63
64
65 let load (filePath: string) : FileData =
66 use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
67
68 let mainFile = file.GetEntry(mainFilename)
69 let mainJSON = JSONMainInformation.Load(mainFile.Open())
70
71 { sources = []; patientID = mainJSON.PatientId }