Add an option to change the brightness of the highlight box color (healthy/infected...
[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
26 // Information associated to each images.
27 type JSONSourceImage = {
28 num: int
29 RBCRadius: float32 // The RBC Radius found by granulometry.
30 parameters: ParasitemiaCore.Config.Parameters
31 dateLastAnalysis: DateTime
32 rbcs: RBC List
33
34 healthyRBCBrightness: float32 // 0 to 1.
35 infectedRBCBrightness: float32 // 0 to 1.
36 }
37
38 type DocumentData = {
39 patientID: string
40 images: SourceImage list
41 }
42
43 let mainEntryName = "info.json"
44 let imageExtension = ".tiff"
45 let currentFileVersion = 1
46
47 /// <summary>
48 /// Save a document in a give file path. The file may already exist.
49 /// </summary>
50 /// <param name="filePath"></param>
51 /// <param name="data"></param>
52 /// <exception cref="System.IOException">If the file cannot be written</exception>
53 let save (filePath: string) (data: DocumentData) =
54 use file = ZipFile.Open(filePath, ZipArchiveMode.Update)
55
56 for e in List.ofSeq file.Entries do // 'ofSeq' to not iterate a collection currently modified.
57 e.Delete()
58
59 // Main JSON file.
60 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
61 use mainEntryWriter = new StreamWriter(mainEntry.Open())
62 mainEntryWriter.Write(JsonConvert.SerializeObject({ patientID = data.patientID; fileVersion = currentFileVersion }))
63
64 // Write each images and the associated information.
65 for srcImg in data.images do
66 let imgFilename = (string srcImg.num) + imageExtension
67 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
68 srcImg.img.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
69
70 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
71 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
72 imgJSONFileWriter.Write(
73 JsonConvert.SerializeObject(
74 { num = srcImg.num
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) : 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 imgEntry = file.GetEntry(imgEntry.Name + ".json")
112 use imgEntryFileReader = new StreamReader(imgEntry.Open())
113 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgEntryFileReader.ReadToEnd())
114 let config = ParasitemiaCore.Config.Config(imgInfo.parameters)
115 config.SetRBCRadius imgInfo.RBCRadius
116 yield { num = imgNum
117 config = config
118 dateLastAnalysis = imgInfo.dateLastAnalysis
119 img = img
120 rbcs = imgInfo.rbcs
121 healthyRBCBrightness = imgInfo.healthyRBCBrightness
122 infectedRBCBrightness = imgInfo.infectedRBCBrightness } ] }