GUI (work in progress..)
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Pia.fs
1 // ParasitemIA file format.
2 module Parasitemia.GUI.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 open Types
14
15 let extension = ".pia"
16 let filter = "PIA|*.pia"
17
18 type FileData = {
19 sources: SourceImage list
20 patientID: string }
21
22 // The json type associated to a source image.
23 type JSONSourceImage = JsonProvider<"""
24 {
25 "rbcs": [
26 {
27 "num": 1,
28 "infected": true,
29 "addedManually": false,
30 "removed": false,
31 "posX" : 42,
32 "posY" : 42,
33 "width" : 10,
34 "height" : 10,
35 "stainArea" : 10
36 }
37 ]
38 }
39 """>
40
41 // The json type associated to a file.
42 type JSONMainInformation = JsonProvider<"""
43 {
44 "patientID": "1234abcd"
45 }
46 """>
47
48 let mainFilename = "info.json"
49
50 let save (filePath: string) (data: FileData) =
51 use file = ZipFile.Open(filePath, ZipArchiveMode.Update)
52
53 let mainJSON = JSONMainInformation.Root(data.patientID)
54
55 let mainFile =
56 match file.GetEntry(mainFilename) with
57 | null -> file.CreateEntry(mainFilename)
58 | entry -> entry
59
60 use mainFileWriter = new StreamWriter(mainFile.Open())
61 mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None)
62
63
64 let load (filePath: string) : FileData =
65 use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
66
67 let mainFile = file.GetEntry(mainFilename)
68 let mainJSON = JSONMainInformation.Load(mainFile.Open())
69
70 { sources = []; patientID = mainJSON.PatientId }