1
// ParasitemIA Zipped file format.
2 module Parasitemia.GUI.PiaZ
7 open System.IO.Compression
12 open Emgu.CV.Structure
16 let extension = ".piaz"
17 let filter = "PIA|*.piaz"
20 sources
: SourceImage list
23 // The json type associated to a source image.
24 type JSONSourceImage = JsonProvider<"""
27 "dateLastAnalysis
" : 1.5,
43 // The json type associated to a file.
44 type JSONMainInformation = JsonProvider<"""
46 "patientID
": "1234abcd"
50 let mainFilename = "info.json"
51 let imageExtension = ".tiff"
53 let save (filePath
: string) (data
: FileData) =
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 mainJSON = JSONMainInformation.Root(data
.patientID
)
61 let mainFile = file.CreateEntry(mainFilename, CompressionLevel.Fastest)
62 use mainFileWriter = new StreamWriter(mainFile.Open())
63 mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None)
65 // Write each images and the associated information.
66 for imgSrc
in data
.sources
do
67 let imgFilename = (string imgSrc
.num
) + imageExtension
68 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
69 imgSrc
.img
.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
72 JSONSourceImage.Root(decimal
imgSrc.rbcRadius
,
73 decimal
<| imgSrc.dateLastAnalysis
.ToFileTimeUtc(),
74 [| for rbc
in imgSrc.rbcs
->
77 rbc
.infected
, rbc
.setManually
,
78 decimal
rbc.center
.X, decimal
rbc.center
.Y, decimal
rbc.size
.Width, decimal
rbc.size
.Height,
81 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
82 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
83 imgJSON.JsonValue.WriteTo(imgJSONFileWriter, JsonSaveOptions.None)
86 let load (filePath
: string) : FileData =
87 use file = ZipFile.Open(filePath
, ZipArchiveMode.Read)
89 let mainFile = file.GetEntry(mainFilename)
90 let mainJSON = JSONMainInformation.Load(mainFile.Open())
93 let mutable imgNum = 0
94 for imgEntry in file.Entries do
95 let filename = imgEntry.Name
96 if filename.EndsWith(imageExtension)
98 let img = new Image<Bgr, byte
>(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap?
100 let imgJSONEntry = file.GetEntry(filename + ".json")
101 let imgJSON = JSONSourceImage.Load(imgJSONEntry.Open())
103 rbcRadius
= float imgJSON.RbcRadius
104 dateLastAnalysis
= DateTime.FromFileTimeUtc(int64
imgJSON.DateLastAnalysis)
106 rbcs
= [ for rbc in imgJSON.Rbcs ->
108 infected
= rbc.Infected; setManually
= rbc.SetManually;
109 center
= Point(float rbc.PosX, float rbc.PosY); size
= Size(float rbc.Width, float rbc.Height);
110 infectedArea
= rbc.StainArea } ] } ]
111 { sources = sources; patientID
= mainJSON.PatientId }