X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FGUI%2FPia.fs;h=1821f26fa28d203b128c06413a1015967102c706;hb=f4fdf61ef98b913129ddb771392c0bcb1475e6fb;hp=9ddd063c37d557903c6bf74412832f509e41bfd1;hpb=3ddaf64dc5ba6a7066a279ad75b9a1ee72194639;p=master-thesis.git diff --git a/Parasitemia/Parasitemia/GUI/Pia.fs b/Parasitemia/Parasitemia/GUI/Pia.fs index 9ddd063..1821f26 100644 --- a/Parasitemia/Parasitemia/GUI/Pia.fs +++ b/Parasitemia/Parasitemia/GUI/Pia.fs @@ -1,7 +1,7 @@ // ParasitemIA file format. -module Pia +module Parasitemia.GUI.Pia -open System.Drawing +open System.Windows open System.IO open System.IO.Compression @@ -10,31 +10,30 @@ open FSharp.Data open Emgu.CV open Emgu.CV.Structure +open Types + let extension = ".pia" let filter = "PIA|*.pia" -type RBC = { - infected: bool - addedManually: bool - removed: bool - - center: Point - size: Size - stainArea: int } - -type ImageSource = { - img: Image - rbcs: RBC list } - type FileData = { - sources: ImageSource list + sources: SourceImage list patientID: string } // The json type associated to a source image. type JSONSourceImage = JsonProvider<""" { "rbcs": [ - { "infected": true, "addedManually": false, "removed": false, "posX" : 42, "posY" : 42, "width" : 10, "height" : 10, "stainArea" : 10 } + { + "num": 1, + "infected": true, + "addedManually": false, + "removed": false, + "posX" : 42.5, + "posY" : 42.5, + "width" : 10.5, + "height" : 10.5, + "stainArea" : 10 + } ] } """> @@ -47,20 +46,38 @@ type JSONMainInformation = JsonProvider<""" """> let mainFilename = "info.json" +let imageExtension = ".tiff" let save (filePath: string) (data: FileData) = use file = ZipFile.Open(filePath, ZipArchiveMode.Update) - let mainJSON = JSONMainInformation.Root(data.patientID) - - let mainFile = - match file.GetEntry(mainFilename) with - | null -> file.CreateEntry(mainFilename) - | entry -> entry + for e in List.ofSeq file.Entries do // 'ofSeq' to not iterate a collection currently modified. + e.Delete() + // Main JSON file. + let mainJSON = JSONMainInformation.Root(data.patientID) + let mainFile = file.CreateEntry(mainFilename, CompressionLevel.Fastest) use mainFileWriter = new StreamWriter(mainFile.Open()) mainJSON.JsonValue.WriteTo(mainFileWriter, JsonSaveOptions.None) + // Write each images and the associated information. + for imgSrc in data.sources do + let imgFilename = (string imgSrc.num) + imageExtension + let imgEntry = file.CreateEntry(imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag. + imgSrc.img.ToBitmap().Save(imgEntry.Open(), System.Drawing.Imaging.ImageFormat.Tiff) + + let imgJSON = + JSONSourceImage.Root([| for rbc in imgSrc.rbcs -> + JSONSourceImage.Rbc( + rbc.num, + rbc.infected, rbc.addedManually, rbc.removed, + decimal rbc.center.X, decimal rbc.center.Y, decimal rbc.size.Width, decimal rbc.size.Height, + rbc.stainArea) |]) + + let imgJSONEntry = file.CreateEntry(imgFilename + ".json", CompressionLevel.Fastest) + use imgJSONFileWriter = new StreamWriter(imgJSONEntry.Open()) + imgJSON.JsonValue.WriteTo(imgJSONFileWriter, JsonSaveOptions.None) + let load (filePath: string) : FileData = use file = ZipFile.Open(filePath, ZipArchiveMode.Read) @@ -68,4 +85,19 @@ let load (filePath: string) : FileData = let mainFile = file.GetEntry(mainFilename) let mainJSON = JSONMainInformation.Load(mainFile.Open()) - { sources = []; patientID = mainJSON.PatientId } \ No newline at end of file + let sources = [ + let mutable imgNum = 0 + for imgEntry in file.Entries do + let filename = imgEntry.Name + if filename.EndsWith(imageExtension) + then + let img = new Image(new System.Drawing.Bitmap(imgEntry.Open(), false)) // FIXME: Should we dispose the bitmap? + imgNum <- imgNum + 1 + let imgJSONEntry = file.GetEntry(filename + ".json") + let imgJSON = JSONSourceImage.Load(imgJSONEntry.Open()) + yield { num = imgNum; img = img; rbcs = [ for rbc in imgJSON.Rbcs -> + { num = rbc.Num; + infected = rbc.Infected; addedManually = rbc.AddedManually; removed = rbc.Removed; + center = Point(float rbc.PosX, float rbc.PosY); size = Size(float rbc.Width, float rbc.Height); + stainArea = rbc.StainArea } ] } ] + { sources = sources; patientID = mainJSON.PatientId } \ No newline at end of file