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 = {
26 // Information associated to each images.
27 type JSONSourceImage = {
29 RBCRadius: float32
// The RBC Radius found by granulometry.
30 parameters
: ParasitemiaCore.Config.Parameters
31 dateLastAnalysis
: DateTime
34 healthyRBCBrightness
: float32
// 0 to 1.
35 infectedRBCBrightness
: float32
// 0 to 1.
40 images
: SourceImage list
43 let mainEntryName = "info.json"
44 let imageExtension = ".tiff"
45 let currentFileVersion = 1
48 /// Save a document in a give file path. The file may already exist.
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)
56 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
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 }))
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)
70 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
71 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
72 imgJSONFileWriter.Write(
73 JsonConvert.SerializeObject(
75 RBCRadius = srcImg
.config
.RBCRadius.Pixel
76 parameters
= srcImg
.config
.Parameters
77 dateLastAnalysis
= srcImg
.dateLastAnalysis
79 healthyRBCBrightness
= srcImg
.healthyRBCBrightness
80 infectedRBCBrightness
= srcImg
.infectedRBCBrightness
}))
82 let updateDocumentData (fromVersion
: int) (toVersion
: int) (data
: DocumentData) : DocumentData =
83 for v
in fromVersion
+ 1 .. toVersion
do
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
)
91 /// Load document from a give file path.
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)
98 let mainEntry = file.GetEntry(mainEntryName)
99 use mainEntryReader = new StreamReader(mainEntry.Open())
100 let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
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)
108 use bitmap = new System.Drawing.Bitmap(imgEntry.Open(), false)
109 let img = new Image<Bgr, byte
>(bitmap)
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
118 dateLastAnalysis
= imgInfo.dateLastAnalysis
121 healthyRBCBrightness
= imgInfo.healthyRBCBrightness
122 infectedRBCBrightness
= imgInfo.infectedRBCBrightness
} ] }