X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FParasitemiaUI%2FPiaZ.fs;fp=Parasitemia%2FParasitemiaUI%2FPiaZ.fs;h=8498c64c0a3b7e7243c2947aade5e817ccb006b4;hp=18e9d6a4afaeae837d305c56ec993f1195faedab;hb=24bfd2ea10b5945100168ad5a1b2545e43d05569;hpb=c3370c187e36ca3c73cf3600f19783f958097e29 diff --git a/Parasitemia/ParasitemiaUI/PiaZ.fs b/Parasitemia/ParasitemiaUI/PiaZ.fs index 18e9d6a..8498c64 100644 --- a/Parasitemia/ParasitemiaUI/PiaZ.fs +++ b/Parasitemia/ParasitemiaUI/PiaZ.fs @@ -43,9 +43,10 @@ type DocumentData = images : SourceImage list } -let mainEntryName = "info.json" -let imageExtension = ".tiff" -let currentFileVersion = 2 +let MAIN_ENTRY_NAME = "info.json" +let DEFAULT_IMAGE_EXTENSION = ".tiff" +let JSON_EXTENSION = ".json" +let CURRENT_FILE_VERSION = 3 /// /// Save a document in a give file path. The file may already exist. @@ -56,21 +57,31 @@ let currentFileVersion = 2 let save (filePath : string) (data : DocumentData) = use file = ZipFile.Open (filePath, ZipArchiveMode.Update) + // We only delete JSON files and removed images. for e in List.ofSeq file.Entries do // 'ofSeq' to not iterate a collection currently modified. - e.Delete () + if Path.GetExtension e.Name = JSON_EXTENSION || data.images |> List.exists (fun img -> img.OriginalName = e.Name) |> not then + e.Delete () // Main JSON file. - let mainEntry = file.CreateEntry (mainEntryName, CompressionLevel.Fastest) + let mainEntry = file.CreateEntry (MAIN_ENTRY_NAME, CompressionLevel.Fastest) use mainEntryWriter = new StreamWriter (mainEntry.Open ()) - mainEntryWriter.Write (JsonConvert.SerializeObject ({ patientID = data.patientID; fileVersion = currentFileVersion })) + mainEntryWriter.Write (JsonConvert.SerializeObject ({ patientID = data.patientID; fileVersion = CURRENT_FILE_VERSION })) - // Write each images and the associated information. + // Write each images and the associated information as a JSON file. for srcImg in data.images do - let imgFilename = (string srcImg.Num) + imageExtension - let imgEntry = file.CreateEntry (imgFilename, CompressionLevel.NoCompression) // FIXME: It seems a compression is applied to this file despite of the 'NoCompression' flag. - srcImg.Img.ToBitmap().Save (imgEntry.Open (), System.Drawing.Imaging.ImageFormat.Tiff) + match srcImg.TempFile with + | Some imgTempFile -> + let imgEntry = file.CreateEntry (srcImg.OriginalName, CompressionLevel.NoCompression) + (File.Open (imgTempFile, FileMode.Open, FileAccess.Read)).CopyTo (imgEntry.Open ()) + srcImg.TempFile <- None - let imgJSONEntry = file.CreateEntry (imgFilename + ".json", CompressionLevel.Fastest) + | None -> () + + //let imgFilename = (string srcImg.Num) + DEFAULT_IMAGE_EXTENSION + //let imgEntry = file.CreateEntry (imgFilename, CompressionLevel.NoCompression) + //srcImg.Img.ToBitmap().Save (imgEntry.Open (), System.Drawing.Imaging.ImageFormat.Tiff) + + let imgJSONEntry = file.CreateEntry (srcImg.OriginalName + JSON_EXTENSION, CompressionLevel.Fastest) use imgJSONFileWriter = new StreamWriter (imgJSONEntry.Open ()) imgJSONFileWriter.Write ( JsonConvert.SerializeObject ( @@ -95,33 +106,38 @@ let updateDocumentData (fromVersion : int) (toVersion : int) (data : DocumentDat | _ -> () data +exception VersionFileNewerException of int + /// /// Load document from a give file path. /// /// Path to the PiaZ file /// /// If the file cannot be read +/// If the file version is newer than the current supported version let load (filePath : string) (defaultConfig : ParasitemiaCore.Config.Config) : DocumentData = use file = ZipFile.Open (filePath, ZipArchiveMode.Read) - let mainEntry = file.GetEntry (mainEntryName) + let mainEntry = file.GetEntry (MAIN_ENTRY_NAME) use mainEntryReader = new StreamReader (mainEntry.Open ()) let info = JsonConvert.DeserializeObject (mainEntryReader.ReadToEnd ()) - updateDocumentData info.fileVersion currentFileVersion + if info.fileVersion > CURRENT_FILE_VERSION then + raise <| VersionFileNewerException info.fileVersion + + updateDocumentData info.fileVersion CURRENT_FILE_VERSION { patientID = info.patientID images = [ - let mutable imgNum = 0 for imgEntry in file.Entries do - if imgEntry.Name.EndsWith (imageExtension) then + if imgEntry.Name.EndsWith JSON_EXTENSION |> not then use bitmap = new System.Drawing.Bitmap (imgEntry.Open (), false) let img = bitmap.ToImage () - imgNum <- imgNum + 1 - let imgJSONEntry = file.GetEntry (imgEntry.Name + ".json") + let imgJSONEntry = file.GetEntry (imgEntry.Name + JSON_EXTENSION) use imgJSONFileReader = new StreamReader (imgJSONEntry.Open ()) let imgInfo = JsonConvert.DeserializeObject (imgJSONFileReader.ReadToEnd ()) + let imgNum = imgInfo.num let config = defaultConfig.Copy () config.Parameters <- @@ -132,6 +148,7 @@ let load (filePath : string) (defaultConfig : ParasitemiaCore.Config.Config) : D config.SetRBCRadius imgInfo.RBCRadius - SourceImage (imgNum, imgInfo.name, config, imgInfo.dateLastAnalysis, img, imgInfo.rbcs, HealthyRBCBrightness = imgInfo.healthyRBCBrightness, InfectedRBCBrightness = imgInfo.infectedRBCBrightness) + SourceImage (imgNum, imgInfo.name, imgEntry.Name, config, imgInfo.dateLastAnalysis, FromMemory img, imgInfo.rbcs, HealthyRBCBrightness = imgInfo.healthyRBCBrightness, InfectedRBCBrightness = imgInfo.infectedRBCBrightness) ] + |> List.sortBy (fun image -> image.Num) } \ No newline at end of file