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