22359e9ab38517a9e65d614b238ebfdb69b1f734
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 parameters
: Config.Parameters
29 dateLastAnalysis
: DateTime
35 images
: SourceImage list
38 let mainEntryName = "info.json"
39 let imageExtension = ".tiff"
41 let save (filePath
: string) (data
: FileData) =
42 use file = ZipFile.Open(filePath
, ZipArchiveMode.Update)
44 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
48 let mainEntry = file.CreateEntry(mainEntryName, CompressionLevel.Fastest)
49 use mainEntryWriter = new StreamWriter(mainEntry.Open())
50 mainEntryWriter.Write(JsonConvert.SerializeObject({ JSONInformation.patientID
= data
.patientID
}))
52 // Write each images and the associated information.
53 for srcImg
in data
.images
do
54 let imgFilename = (string srcImg
.num
) + imageExtension
55 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
56 srcImg
.img
.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
58 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
59 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
60 imgJSONFileWriter.Write(JsonConvert.SerializeObject({ num
= srcImg
.num
; parameters
= srcImg
.config
.Parameters; dateLastAnalysis
= srcImg
.dateLastAnalysis
; rbcs
= srcImg
.rbcs
}))
63 let load (filePath
: string) : FileData =
64 use file = ZipFile.Open(filePath
, ZipArchiveMode.Read)
66 let mainEntry = file.GetEntry(mainEntryName)
67 use mainEntryReader = new StreamReader(mainEntry.Open())
68 let info = JsonConvert.DeserializeObject<JSONInformation>(mainEntryReader.ReadToEnd())
70 { patientID
= info.patientID
71 images
= [ let mutable imgNum = 0
72 for imgEntry in file.Entries do
73 if imgEntry.Name.EndsWith(imageExtension)
75 let img = new Image<Bgr, byte
>(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap?
77 let imgEntry = file.GetEntry(imgEntry.Name + ".json")
78 use imgEntryFileReader = new StreamReader(imgEntry.Open())
79 let imgInfo = JsonConvert.DeserializeObject<JSONSourceImage>(imgEntryFileReader.ReadToEnd())
81 config
= Config.Config(imgInfo.parameters
)
82 dateLastAnalysis
= imgInfo.dateLastAnalysis
84 rbcs
= imgInfo.rbcs
} ] }