bb553b8442cc015198a567474913016e47f76b07
[master-thesis.git] / Parasitemia / ParasitemiaUI / PiaZ.fs
1 // ParasitemIA Zipped document format.
2 module ParasitemiaUI.PiaZ
3
4 open System
5 open System.Windows
6 open System.IO
7 open System.IO.Compression
8
9 open Emgu.CV
10 open Emgu.CV.Structure
11
12 open Newtonsoft.Json
13 open Newtonsoft.Json.Converters
14
15 open Types
16
17 let extension = ".piaz"
18 let filter = "PIA|*.piaz"
19
20 // Information associated to a document.
21 type JSONInformation = {
22 patientID: string
23 fileVersion: int }
24
25 // Information associated to each images.
26 type JSONSourceImage = {
27 num: int
28 name: string
29
30 RBCRadius: float32 // The RBC Radius found by granulometry.
31 parameters: ParasitemiaCore.Config.Parameters
32 dateLastAnalysis: DateTime
33 rbcs: RBC List
34
35 healthyRBCBrightness: float32 // 0 to 1.
36 infectedRBCBrightness: float32 } // 0 to 1.
37
38 type DocumentData = {
39 patientID: string
40 images: SourceImage list }
41
42 let mainEntryName = "info.json"
43 let imageExtension = ".tiff"
44 let currentFileVersion = 2
45
46 /// <summary>
47 /// Save a document in a give file path. The file may already exist.
48 /// </summary>
49 /// <param name="filePath"></param>
50 /// <param name="data"></param>
51 /// <exception cref="System.IOException">If the file cannot be written</exception>
52 let save (filePath: string) (data: DocumentData) =
53 use file = ZipFile.Open(filePath, ZipArchiveMode.Update)
54
55 for e in List.ofSeq file.Entries do // 'ofSeq' to not iterate a collection currently modified.
56 e.Delete()
57
58 // Main JSON file.
59 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
60 use mainEntryWriter = new StreamWriter(mainEntry.Open())
61 mainEntryWriter.Write(JsonConvert.SerializeObject({ patientID = data.patientID; fileVersion = currentFileVersion }))
62
63 // Write each images and the associated information.
64 for srcImg in data.images do
65 let imgFilename = (string srcImg.num) + imageExtension
66 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
67 srcImg.img.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
68
69 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
70 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
71 imgJSONFileWriter.Write(
72 JsonConvert.SerializeObject(
73 { num = srcImg.num
74 name = srcImg.name
75 RBCRadius = srcImg.config.RBCRadius.Pixel
76 parameters = srcImg.config.Parameters
77 dateLastAnalysis = srcImg.dateLastAnalysis
78 rbcs = srcImg.rbcs
79 healthyRBCBrightness = srcImg.healthyRBCBrightness
80 infectedRBCBrightness = srcImg.infectedRBCBrightness }))
81
82 let updateDocumentData (fromVersion: int) (toVersion: int) (data: DocumentData) : DocumentData =
83 for v in fromVersion + 1 .. toVersion do
84 match v with
85 | 1 -> // Version 0 -> 1 : set initial brightness for rbc.
86 data.images |> List.iter (fun i -> i.healthyRBCBrightness <- 1.f; i.infectedRBCBrightness <- 1.f)
87 | _ -> ()
88 data
89
90 /// <summary>
91 /// Load document from a give file path.
92 /// </summary>
93 /// <param name="filePath"></param>
94 /// <exception cref="System.IOException">If the file cannot be read</exception>
95 let load (filePath: string) (defaultConfig: ParasitemiaCore.Config.Config) : DocumentData =
96 use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
97
98 let mainEntry = file.GetEntry(mainEntryName)
99 use mainEntryReader = new StreamReader(mainEntry.Open())
100 let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
101
102 updateDocumentData info.fileVersion currentFileVersion
103 { patientID = info.patientID
104 images = [ let mutable imgNum = 0
105 for imgEntry in file.Entries do
106 if imgEntry.Name.EndsWith(imageExtension)
107 then
108 use bitmap = new System.Drawing.Bitmap(imgEntry.Open(), false)
109 let img = new Image<Bgr, byte>(bitmap)
110 imgNum <- imgNum + 1
111 let imgJSONEntry = file.GetEntry(imgEntry.Name + ".json")
112 use imgJSONFileReader = new StreamReader(imgJSONEntry.Open())
113 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgJSONFileReader.ReadToEnd())
114
115 let config = defaultConfig.Copy()
116 config.Parameters <-
117 { ParasitemiaCore.Config.defaultParameters with
118 resolution = imgInfo.parameters.resolution }
119
120 config.SetRBCRadius imgInfo.RBCRadius
121 yield { num = imgNum
122 name = imgInfo.name
123 config = config
124 dateLastAnalysis = imgInfo.dateLastAnalysis
125 img = img
126 rbcs = imgInfo.rbcs
127 healthyRBCBrightness = imgInfo.healthyRBCBrightness
128 infectedRBCBrightness = imgInfo.infectedRBCBrightness } ] }