/// <summary> /// Creates a new, decompressed volume from the specified data /// </summary> /// <param name="metadata">Holds additional information about a volume</param> /// <param name="stream">A stream to the slice data</param> /// <param name="options">Codec settings</param> /// <param name="progress">A progress indicator, which reports the current slice number.</param> /// <param name="ct"></param> /// <exception cref="VolumeException">Error during encoding</exception> public static Volume CreateCompressed(VolumeMetadata metadata, Stream stream, VolumeCompressionOptions options, IProgress <VolumeSliceDefinition> progress = null, CancellationToken ct = default(CancellationToken)) { var compressedData = CompressStream(stream, Direction.Z, metadata, options, progress, ct); var dirctionMap = new DirectionMap { [Direction.Z] = compressedData }; return(new CompressedVolume(metadata, options, dirctionMap)); }
internal CompressedVolume(VolumeMetadata metadata, VolumeCompressionOptions options, DirectionMap compressedData) : base(metadata) { if (compressedData[Direction.Z] == null) { throw new NotSupportedException(Resources.GetResource <Volume>("CompressedDataMissing_ErrorText")); } _CompressedData = compressedData; CompressionOptions = options; }
/// <summary> /// Compresses the volume with the specified compression options. /// </summary> /// <param name="options"></param> /// <param name="multiDirection"></param> /// <param name="progress">A progress indicator, which reports the current slice number.</param> /// <param name="ct"></param> /// <exception cref="VolumeException">Error during encoding</exception> public CompressedVolume Compress(VolumeCompressionOptions options, bool multiDirection = false, IProgress <VolumeSliceDefinition> progress = null, CancellationToken ct = default(CancellationToken)) { var directionMap = new DirectionMap { [Direction.Z] = CompressDirection(Direction.Z, options, progress, ct) }; if (multiDirection) { directionMap[Direction.X] = CompressDirection(Direction.X, options, progress, ct); directionMap[Direction.Y] = CompressDirection(Direction.Y, options, progress, ct); } return(new CompressedVolume(Metadata, options, directionMap)); }
/// <summary> /// Loads volume data from the specified data stream. /// </summary> /// <param name="stream">The stream.</param> /// <returns></returns> /// <exception cref="VolumeException">Error during decoding</exception> /// <exception cref="NotSupportedException">The volume has no compressed data</exception> /// <exception cref="InvalidOperationException">One or more entries are missing in the archive.</exception> public static CompressedVolume Load(Stream stream) { if (!stream.CanSeek) { stream = new MemoryStream(stream.StreamToArray()); } VolumeMetadata metaData; DirectionMap directionMap = new DirectionMap(); VolumeCompressionOptions compressionOptions; using (var archive = new ZipArchive(stream)) { #region Metadata var metaDataEntry = archive.GetEntry("Metadata.xml"); if (metaDataEntry == null) { throw new InvalidOperationException(Resources.FormatResource <Volume>("InvalidFormatMissingFile_ErrorText", "Metadata.xml")); } using (var entryStream = metaDataEntry.Open()) { metaData = VolumeMetadata.Deserialize(entryStream); if (metaData.FileVersion > FileVersion) { throw new InvalidOperationException(Resources.FormatResource <Volume>("FileVersionError_Text", metaData.FileVersion, FileVersion)); } } #endregion #region CompressionOptions var compressionOptionsEntry = archive.GetEntry("CompressionOptions.xml"); if (compressionOptionsEntry != null) { using (var entryStream = compressionOptionsEntry.Open()) { compressionOptions = VolumeCompressionOptions.Deserialize(entryStream); } } else { compressionOptions = null; } #endregion #region Voxels var dataEntry = archive.GetEntry("Voxels.dat"); if (dataEntry == null) { dataEntry = archive.GetEntry("VoxelsZ.dat"); if (dataEntry == null) { throw new InvalidOperationException(Resources.FormatResource <Volume>("InvalidFormatMissingFile_ErrorText", "Voxels.dat")); } using (var entryStream = dataEntry.Open()) { directionMap[Direction.Z] = entryStream.StreamToArray(); } dataEntry = archive.GetEntry("VoxelsY.dat"); if (dataEntry != null) { using (var entryStream = dataEntry.Open()) { directionMap[Direction.Y] = entryStream.StreamToArray(); } } dataEntry = archive.GetEntry("VoxelsX.dat"); if (dataEntry != null) { using (var entryStream = dataEntry.Open()) { directionMap[Direction.X] = entryStream.StreamToArray(); } } } else { using (var entryStream = dataEntry.Open()) { directionMap[Direction.Z] = entryStream.StreamToArray(); } } #endregion } return(new CompressedVolume(metaData, compressionOptions, directionMap)); }