1
// ParasitemIA Zipped document format.
2 module ParasitemiaUI.PiaZ
7 open System.IO.Compression
10 open Emgu.CV.Structure
13 open Newtonsoft.Json.Converters
17 let extension = ".piaz"
18 let filter = "PIA|*.piaz"
20 // Information associated to a document.
21 type JSONInformation =
27 // Information associated to each images.
28 type JSONSourceImage =
33 RBCRadius : float32
// The RBC Radius found by granulometry.
34 parameters
: ParasitemiaCore.Config.Parameters
35 dateLastAnalysis
: DateTime
38 healthyRBCBrightness
: float32
// 0 to 1.
39 infectedRBCBrightness
: float32
// 0 to 1.
45 images
: SourceImage list
48 let mainEntryName = "info.json"
49 let imageExtension = ".tiff"
50 let currentFileVersion = 2
53 /// Save a document in a give file path. The file may already exist.
55 /// <param name="filePath"></param>
56 /// <param name="data"></param>
57 /// <exception cref="System.IOException">If the file cannot be written</exception>
58 let save (filePath
: string) (data
: DocumentData) =
59 use file = ZipFile.Open(filePath
, ZipArchiveMode.Update)
61 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
65 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
66 use mainEntryWriter = new StreamWriter(mainEntry.Open())
67 mainEntryWriter.Write(JsonConvert.SerializeObject({ patientID
= data
.patientID
; fileVersion
= currentFileVersion }))
69 // Write each images and the associated information.
70 for srcImg
in data
.images
do
71 let imgFilename = (string srcImg
.num
) + imageExtension
72 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
73 srcImg
.img
.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
75 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
76 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
77 imgJSONFileWriter.Write(
78 JsonConvert.SerializeObject(
82 RBCRadius = srcImg
.config
.RBCRadius.Pixel
83 parameters
= srcImg
.config
.Parameters
84 dateLastAnalysis
= srcImg
.dateLastAnalysis
86 healthyRBCBrightness
= srcImg
.healthyRBCBrightness
87 infectedRBCBrightness
= srcImg
.infectedRBCBrightness
92 let updateDocumentData (fromVersion
: int) (toVersion
: int) (data
: DocumentData) : DocumentData =
93 for v
in fromVersion
+ 1 .. toVersion
do
95 | 1 -> // Version 0 -> 1 : set initial brightness for rbc.
96 data
.images
|> List.iter
(fun i
-> i
.healthyRBCBrightness
<- 1.f
; i
.infectedRBCBrightness
<- 1.f
)
101 /// Load document from a give file path.
103 /// <param name="filePath"></param>
104 /// <exception cref="System.IOException">If the file cannot be read</exception>
105 let load (filePath
: string) (defaultConfig
: ParasitemiaCore.Config.Config) : DocumentData =
106 use file = ZipFile.Open(filePath
, ZipArchiveMode.Read)
108 let mainEntry = file.GetEntry(mainEntryName)
109 use mainEntryReader = new StreamReader(mainEntry.Open())
110 let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
112 updateDocumentData info.fileVersion
currentFileVersion
114 patientID
= info.patientID
117 let mutable imgNum = 0
118 for imgEntry in file.Entries do
119 if imgEntry.Name.EndsWith(imageExtension) then
120 use bitmap = new System.Drawing.Bitmap(imgEntry.Open(), false)
121 let img = new Image<Bgr, byte
>(bitmap)
123 let imgJSONEntry = file.GetEntry(imgEntry.Name + ".json")
124 use imgJSONFileReader = new StreamReader(imgJSONEntry.Open())
125 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgJSONFileReader.ReadToEnd())
127 let config = defaultConfig
.Copy()
130 ParasitemiaCore.Config.defaultParameters
with
131 resolution
= imgInfo.parameters
.resolution
134 config.SetRBCRadius imgInfo.RBCRadius
140 dateLastAnalysis
= imgInfo.dateLastAnalysis
143 healthyRBCBrightness
= imgInfo.healthyRBCBrightness
144 infectedRBCBrightness
= imgInfo.infectedRBCBrightness