1
// ParasitemIA Zipped document format.
2 module ParasitemiaUI.PiaZ
6 open System.IO.Compression
15 let extension = ".piaz"
16 let filter = "PIA|*.piaz"
18 // Information associated to a document.
19 type JSONInformation =
25 // Information associated to each images.
26 type JSONSourceImage =
31 RBCRadius : float32
// The RBC Radius found by granulometry.
32 parameters
: ParasitemiaCore.Config.Parameters
33 dateLastAnalysis
: DateTime
36 healthyRBCBrightness
: float32
// 0 to 1.
37 infectedRBCBrightness
: float32
// 0 to 1.
43 images
: SourceImage list
46 let mainEntryName = "info.json"
47 let imageExtension = ".tiff"
48 let currentFileVersion = 2
51 /// Save a document in a give file path. The file may already exist.
53 /// <param name="filePath"></param>
54 /// <param name="data"></param>
55 /// <exception cref="System.IOException">If the file cannot be written</exception>
56 let save (filePath
: string) (data
: DocumentData) =
57 use file = ZipFile.Open (filePath
, ZipArchiveMode.Update)
59 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
63 let mainEntry = file.CreateEntry (mainEntryName, CompressionLevel.Fastest)
64 use mainEntryWriter = new StreamWriter (mainEntry.Open ())
65 mainEntryWriter.Write (JsonConvert.SerializeObject ({ patientID
= data
.patientID
; fileVersion
= currentFileVersion }))
67 // Write each images and the associated information.
68 for srcImg
in data
.images
do
69 let imgFilename = (string srcImg
.Num) + imageExtension
70 let imgEntry = file.CreateEntry (imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
71 srcImg
.Img.ToBitmap().Save (imgEntry.Open (), System.Drawing.Imaging.ImageFormat.Tiff)
73 let imgJSONEntry = file.CreateEntry (imgFilename + ".json", CompressionLevel.Fastest)
74 use imgJSONFileWriter = new StreamWriter (imgJSONEntry.Open ())
75 imgJSONFileWriter.Write (
76 JsonConvert.SerializeObject (
80 RBCRadius = srcImg
.Config.RBCRadius.Pixel
81 parameters
= srcImg
.Config.Parameters
82 dateLastAnalysis
= srcImg
.DateLastAnalysis
84 healthyRBCBrightness
= srcImg
.HealthyRBCBrightness
85 infectedRBCBrightness
= srcImg
.InfectedRBCBrightness
90 let updateDocumentData (fromVersion
: int) (toVersion
: int) (data
: DocumentData) : DocumentData =
91 for v
in fromVersion
+ 1 .. toVersion
do
93 | 1 -> // Version 0 -> 1 : set initial brightness for rbc.
94 data
.images
|> List.iter
(fun i
-> i
.HealthyRBCBrightness <- 1.f
; i
.InfectedRBCBrightness <- 1.f
)
99 /// Load document from a give file path.
101 /// <param name="filePath">Path to the PiaZ file</param>
102 /// <param name="defaultConfig"></param>
103 /// <exception cref="System.IOException">If the file cannot be read</exception>
104 let load (filePath
: string) (defaultConfig
: ParasitemiaCore.Config.Config) : DocumentData =
105 use file = ZipFile.Open (filePath
, ZipArchiveMode.Read)
107 let mainEntry = file.GetEntry (mainEntryName)
108 use mainEntryReader = new StreamReader (mainEntry.Open ())
109 let info = JsonConvert.DeserializeObject<JSONInformation> (mainEntryReader.ReadToEnd ())
111 updateDocumentData info.fileVersion
currentFileVersion
113 patientID
= info.patientID
116 let mutable imgNum = 0
117 for imgEntry in file.Entries do
118 if imgEntry.Name.EndsWith (imageExtension) then
119 use bitmap = new System.Drawing.Bitmap (imgEntry.Open (), false)
120 let img = bitmap.ToImage<Bgr, byte
> ()
122 let imgJSONEntry = file.GetEntry (imgEntry.Name + ".json")
123 use imgJSONFileReader = new StreamReader (imgJSONEntry.Open ())
124 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage> (imgJSONFileReader.ReadToEnd ())
126 let config = defaultConfig
.Copy ()
129 ParasitemiaCore.Config.defaultParameters
with
130 resolution
= imgInfo.parameters
.resolution
133 config.SetRBCRadius imgInfo.RBCRadius
135 SourceImage (imgNum, imgInfo.name
, config, imgInfo.dateLastAnalysis
, img, imgInfo.rbcs
, HealthyRBCBrightness = imgInfo.healthyRBCBrightness
, InfectedRBCBrightness = imgInfo.infectedRBCBrightness
)