bb553b8442cc015198a567474913016e47f76b07
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 = {
25 // Information associated to each images.
26 type JSONSourceImage = {
30 RBCRadius: float32
// The RBC Radius found by granulometry.
31 parameters
: ParasitemiaCore.Config.Parameters
32 dateLastAnalysis
: DateTime
35 healthyRBCBrightness
: float32
// 0 to 1.
36 infectedRBCBrightness
: float32
} // 0 to 1.
40 images
: SourceImage list }
42 let mainEntryName = "info.json"
43 let imageExtension = ".tiff"
44 let currentFileVersion = 2
47 /// Save a document in a give file path. The file may already exist.
49 /// <param name="filePath"></param>
50 /// <param name="data"></param>
51 /// <exception cref="System.IOException">If the file cannot be written</exception>
52 let save (filePath
: string) (data
: DocumentData) =
53 use file = ZipFile.Open(filePath
, ZipArchiveMode.Update)
55 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
59 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
60 use mainEntryWriter = new StreamWriter(mainEntry.Open())
61 mainEntryWriter.Write(JsonConvert.SerializeObject({ patientID
= data
.patientID
; fileVersion
= currentFileVersion }))
63 // Write each images and the associated information.
64 for srcImg
in data
.images
do
65 let imgFilename = (string srcImg
.num
) + imageExtension
66 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
67 srcImg
.img
.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
69 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
70 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
71 imgJSONFileWriter.Write(
72 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) (defaultConfig
: ParasitemiaCore.Config.Config) : 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 imgJSONEntry = file.GetEntry(imgEntry.Name + ".json")
112 use imgJSONFileReader = new StreamReader(imgJSONEntry.Open())
113 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgJSONFileReader.ReadToEnd())
115 let config = defaultConfig
.Copy()
117 { ParasitemiaCore.Config.defaultParameters
with
118 resolution
= imgInfo.parameters
.resolution
}
120 config.SetRBCRadius imgInfo.RBCRadius
124 dateLastAnalysis
= imgInfo.dateLastAnalysis
127 healthyRBCBrightness
= imgInfo.healthyRBCBrightness
128 infectedRBCBrightness
= imgInfo.infectedRBCBrightness
} ] }