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