22359e9ab38517a9e65d614b238ebfdb69b1f734
[master-thesis.git] / Parasitemia / Parasitemia / GUI / PiaZ.fs
1 // ParasitemIA Zipped file format.
2 module Parasitemia.GUI.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 }
24
25 // Information associated to each images.
26 type JSONSourceImage = {
27 num: int
28 parameters: Config.Parameters
29 dateLastAnalysis: DateTime
30 rbcs: RBC List
31 }
32
33 type FileData = {
34 patientID: string
35 images: SourceImage list
36 }
37
38 let mainEntryName = "info.json"
39 let imageExtension = ".tiff"
40
41 let save (filePath: string) (data: FileData) =
42 use file = ZipFile.Open(filePath, ZipArchiveMode.Update)
43
44 for e in List.ofSeq file.Entries do // 'ofSeq' to not iterate a collection currently modified.
45 e.Delete()
46
47 // Main JSON file.
48 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
49 use mainEntryWriter = new StreamWriter(mainEntry.Open())
50 mainEntryWriter.Write(JsonConvert.SerializeObject({ JSONInformation.patientID = data.patientID }))
51
52 // Write each images and the associated information.
53 for srcImg in data.images do
54 let imgFilename = (string srcImg.num) + imageExtension
55 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
56 srcImg.img.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
57
58 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
59 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
60 imgJSONFileWriter.Write(JsonConvert.SerializeObject({ num = srcImg.num; parameters = srcImg.config.Parameters; dateLastAnalysis = srcImg.dateLastAnalysis; rbcs = srcImg.rbcs }))
61
62
63 let load (filePath: string) : FileData =
64 use file = ZipFile.Open(filePath, ZipArchiveMode.Read)
65
66 let mainEntry = file.GetEntry(mainEntryName)
67 use mainEntryReader = new StreamReader(mainEntry.Open())
68 let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
69
70 { patientID = info.patientID
71 images = [ let mutable imgNum = 0
72 for imgEntry in file.Entries do
73 if imgEntry.Name.EndsWith(imageExtension)
74 then
75 let img = new Image<Bgr, byte>(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap?
76 imgNum <- imgNum + 1
77 let imgEntry = file.GetEntry(imgEntry.Name + ".json")
78 use imgEntryFileReader = new StreamReader(imgEntry.Open())
79 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgEntryFileReader.ReadToEnd())
80 yield { num = imgNum
81 config = Config.Config(imgInfo.parameters)
82 dateLastAnalysis = imgInfo.dateLastAnalysis
83 img = img
84 rbcs = imgInfo.rbcs } ] }