d551b8d720ca7b6e0656c7fe2dd78ce003ddf7ab
1
// ParasitemIA Zipped file format.
2 module Parasitemia.GUI.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 = {
28 RBCRadius: float32
// The RBC Radius found by granulometry.
29 parameters
: Config.Parameters
30 dateLastAnalysis
: DateTime
36 images
: SourceImage list
39 let mainEntryName = "info.json"
40 let imageExtension = ".tiff"
42 let save (filePath
: string) (data
: FileData) =
43 use file = ZipFile.Open(filePath
, ZipArchiveMode.Update)
45 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
49 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
50 use mainEntryWriter = new StreamWriter(mainEntry.Open())
51 mainEntryWriter.Write(JsonConvert.SerializeObject({ JSONInformation.patientID
= data
.patientID
}))
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)
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
}))
64 let load (filePath
: string) : FileData =
65 use file = ZipFile.Open(filePath
, ZipArchiveMode.Read)
67 let mainEntry = file.GetEntry(mainEntryName)
68 use mainEntryReader = new StreamReader(mainEntry.Open())
69 let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
71 { patientID
= info.patientID
72 images
= [ let mutable imgNum = 0
73 for imgEntry in file.Entries do
74 if imgEntry.Name.EndsWith(imageExtension)
76 let img = new Image<Bgr, byte
>(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap?
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
85 dateLastAnalysis
= imgInfo.dateLastAnalysis
87 rbcs
= imgInfo.rbcs
} ] }