a373e53454009df41393e793e80ce47c7cca1c58
1
// ParasitemIA Zipped file format.
2 module Parasitemia.GUI.PiaZ
6 open System.IO.Compression
11 open Emgu.CV.Structure
15 let extension = ".piaz"
16 let filter = "PIA|*.piaz"
19 sources
: SourceImage list
22 // The json type associated to a source image.
23 type JSONSourceImage = JsonProvider<"""
40 // The json type associated to a file.
41 type JSONMainInformation = JsonProvider<"""
43 "patientID
": "1234abcd"
47 let mainFilename = "info.json"
48 let imageExtension = ".tiff"
50 let save (filePath
: string) (data
: FileData) =
51 use file = ZipFile.Open(filePath
, ZipArchiveMode.Update)
53 for e
in List.ofSeq
file.Entries do // 'ofSeq' to not iterate a collection currently modified.
57 let mainJSON = JSONMainInformation.Root(data
.patientID
)
58 let mainFile = file.CreateEntry(mainFilename, CompressionLevel.Fastest)
59 use mainFileWriter = new StreamWriter(mainFile.Open())
60 mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None)
62 // Write each images and the associated information.
63 for imgSrc
in data
.sources
do
64 let imgFilename = (string imgSrc
.num
) + imageExtension
65 let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag.
66 imgSrc
.img
.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff)
69 JSONSourceImage.Root([| for rbc
in imgSrc
.rbcs
->
72 rbc
.infected
, rbc
.setManually
,
73 decimal
rbc.center
.X, decimal
rbc.center
.Y, decimal
rbc.size
.Width, decimal
rbc.size
.Height,
76 let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest)
77 use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open())
78 imgJSON.JsonValue.WriteTo(imgJSONFileWriter, JsonSaveOptions.None)
81 let load (filePath
: string) : FileData =
82 use file = ZipFile.Open(filePath
, ZipArchiveMode.Read)
84 let mainFile = file.GetEntry(mainFilename)
85 let mainJSON = JSONMainInformation.Load(mainFile.Open())
88 let mutable imgNum = 0
89 for imgEntry in file.Entries do
90 let filename = imgEntry.Name
91 if filename.EndsWith(imageExtension)
93 let img = new Image<Bgr, byte
>(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap?
95 let imgJSONEntry = file.GetEntry(filename + ".json")
96 let imgJSON = JSONSourceImage.Load(imgJSONEntry.Open())
97 yield
{ num
= imgNum; img = img; rbcs
= [ for rbc in imgJSON.Rbcs ->
99 infected
= rbc.Infected; setManually
= rbc.SetManually;
100 center
= Point(float rbc.PosX, float rbc.PosY); size
= Size(float rbc.Width, float rbc.Height);
101 infectedArea
= rbc.StainArea } ] } ]
102 { sources = sources; patientID
= mainJSON.PatientId }