public void OnFragmentItem_LargeBuffer_ReturnValueTrue() { var expected = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray(); var e = new ManualResetEventSlim(false); using (var stream = new MemoryStream()) { var target = new StreamByteTarget(stream); var writer = new DicomWriter( DicomTransferSyntax.ExplicitVRLittleEndian, new DicomWriteOptions { LargeObjectSize = 200 }, target); writer.OnBeginWalk(); Assert.True(writer.OnFragmentItem(new MemoryByteBuffer(expected))); e.Wait(100); var actual = new byte[expected.Length]; stream.Seek(8, SeekOrigin.Begin); stream.Read(actual, 0, actual.Length); Assert.Equal(expected, actual); } }
/// <summary> /// Write DICOM dataset. /// </summary> /// <param name="target">Byte target subject to writing.</param> /// <param name="syntax">Transfer syntax applicable to dataset.</param> /// <param name="dataset">Dataset.</param> /// <param name="options">Writer options.</param> private static async Task WriteDatasetAsync( IByteTarget target, DicomTransferSyntax syntax, DicomDataset dataset, DicomWriteOptions options) { dataset.OnBeforeSerializing(); UpdateDatasetGroupLengths(syntax, dataset, options); if (syntax.IsDeflate) { using (var uncompressed = new MemoryStream()) { var temp = new StreamByteTarget(uncompressed); await WalkDatasetAsync(temp, syntax, dataset, options).ConfigureAwait(false); uncompressed.Seek(0, SeekOrigin.Begin); using (var compressed = new MemoryStream()) { using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true)) { uncompressed.CopyTo(compressor); } target.Write(compressed.ToArray(), 0, (uint)compressed.Length); } } } else { await WalkDatasetAsync(target, syntax, dataset, options).ConfigureAwait(false); } }
/// <summary> /// Write DICOM dataset. /// </summary> /// <param name="target">Byte target subject to writing.</param> /// <param name="syntax">Transfer syntax applicable to dataset.</param> /// <param name="dataset">Dataset.</param> /// <param name="options">Writer options.</param> private static void WriteDataset( IByteTarget target, DicomTransferSyntax syntax, DicomDataset dataset, DicomWriteOptions options) { UpdateDatasetGroupLengths(syntax, dataset, options); if (syntax.IsDeflate) { #if NET35 throw new NotSupportedException("Deflated datasets not supported in Unity."); #else using (var uncompressed = new MemoryStream()) { var temp = new StreamByteTarget(uncompressed); WalkDataset(temp, syntax, dataset, options); uncompressed.Seek(0, SeekOrigin.Begin); using (var compressed = new MemoryStream()) using (var compressor = new DeflateStream(compressed, CompressionMode.Compress)) { uncompressed.CopyTo(compressor); target.Write(compressed.ToArray(), 0, (uint)compressed.Length); } } #endif } else { WalkDataset(target, syntax, dataset, options); } }
public void OnElement_SmallObject_ReturnValueTrue() { const string expected = "STEREOTACTIC"; var e = new ManualResetEventSlim(false); var element = new DicomCodeString(DicomTag.ApplicatorType, expected); using (var stream = new MemoryStream()) { var target = new StreamByteTarget(stream); var writer = new DicomWriter( DicomTransferSyntax.ExplicitVRLittleEndian, new DicomWriteOptions { LargeObjectSize = 14 }, target); writer.OnBeginWalk(); Assert.True(writer.OnElement(element)); e.Wait(100); stream.Seek(8, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var actual = reader.ReadToEnd().Trim(); Assert.Equal(expected, actual); } } }
/// <summary> /// Write DICOM dataset. /// </summary> /// <param name="target">Byte target subject to writing.</param> /// <param name="syntax">Transfer syntax applicable to dataset.</param> /// <param name="dataset">Dataset.</param> /// <param name="options">Writer options.</param> private static void WriteDataset( IByteTarget target, DicomTransferSyntax syntax, DicomDataset dataset, DicomWriteOptions options) { UpdateDatasetGroupLengths(syntax, dataset, options); if (syntax.IsDeflate) { using (var uncompressed = new MemoryStream()) { var temp = new StreamByteTarget(uncompressed); WalkDataset(temp, syntax, dataset, options); uncompressed.Seek(0, SeekOrigin.Begin); using (var compressed = new MemoryStream()) { using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true)) { uncompressed.CopyTo(compressor); } target.Write(compressed.ToArray(), 0, (uint)compressed.Length); } } } else { WalkDataset(target, syntax, dataset, options); } }
public void OnElement_SmallObject_ReturnValueTrue() { const string expected = "STEREOTACTIC"; var e = new ManualResetEventSlim(false); var element = new DicomCodeString(DicomTag.ApplicatorType, expected); using (var stream = new MemoryStream()) { var target = new StreamByteTarget(stream); var writer = new DicomWriter( DicomTransferSyntax.ExplicitVRLittleEndian, new DicomWriteOptions { LargeObjectSize = 14 }, target); writer.OnBeginWalk(); Assert.True(writer.OnElement(element)); e.Wait(100); stream.Seek(8, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var actual = reader.ReadToEnd().Trim(); Assert.Equal(expected, actual); } } }
/// <summary> /// Asynchronously save DICOM file to stream. /// </summary> /// <param name="stream">Stream on which to save DICOM file.</param> /// <param name="options">Options to apply during writing.</param> /// <returns>Awaitable task.</returns> public async Task SaveAsync(Stream stream, DicomWriteOptions options = null) { this.PreprocessFileMetaInformation(); this.OnSave(); var target = new StreamByteTarget(stream); var writer = new DicomFileWriter(options); await writer.WriteAsync(target, this.FileMetaInfo, this.Dataset).ConfigureAwait(false); }
/// <summary> /// Save DICOM file to stream. /// </summary> /// <param name="stream">Stream on which to save DICOM file.</param> /// <param name="options">Options to apply during writing.</param> public void Save(Stream stream, DicomWriteOptions options = null) { this.PreprocessFileMetaInformation(); this.OnSave(); var target = new StreamByteTarget(stream); var writer = new DicomFileWriter(options); writer.Write(target, this.FileMetaInfo, this.Dataset); }
private DicomDataset DeepClone_(DicomDataset dataset) { var ms = new MemoryStream(); var target = new StreamByteTarget(ms); var writer = new DicomWriter(DicomTransferSyntax.ImplicitVRLittleEndian, DicomWriteOptions.Default, target); var walker = new DicomDatasetWalker(dataset); walker.Walk(writer); var clone = new DicomDataset(); var reader = new DicomReader { IsExplicitVR = false }; var byteSource = new ByteBufferByteSource( new MemoryByteBuffer(ms.ToArray())); reader.Read(byteSource, new DicomDatasetReaderObserver(clone)); return(clone); }
/// <summary> /// Asynchronously save DICOM file to stream. /// </summary> /// <param name="stream">Stream on which to save DICOM file.</param> /// <returns>Awaitable task.</returns> public async Task SaveAsync(Stream stream) { if (this.Format == DicomFileFormat.ACRNEMA1 || this.Format == DicomFileFormat.ACRNEMA2) { throw new DicomFileException(this, "Unable to save ACR-NEMA file"); } if (this.Format == DicomFileFormat.DICOM3NoFileMetaInfo) { // create file meta information from dataset this.FileMetaInfo = new DicomFileMetaInformation(this.Dataset); } this.OnSave(); var target = new StreamByteTarget(stream); var writer = new DicomFileWriter(DicomWriteOptions.Default); await writer.WriteAsync(target, this.FileMetaInfo, this.Dataset).ConfigureAwait(false); }
public void Save(Stream stream) { if (Format == DicomFileFormat.ACRNEMA1 || Format == DicomFileFormat.ACRNEMA2) { throw new DicomFileException(this, "Unable to save ACR-NEMA file"); } if (Format == DicomFileFormat.DICOM3NoFileMetaInfo) { // create file meta information from dataset FileMetaInfo = new DicomFileMetaInformation(Dataset); } OnSave(); using (var target = new StreamByteTarget(stream)) { DicomFileWriter writer = new DicomFileWriter(DicomWriteOptions.Default); writer.Write(target, FileMetaInfo, Dataset); } }
public void OnFragmentItem_LargeBuffer_ReturnValueTrue() { var expected = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray(); var e = new ManualResetEventSlim(false); using (var stream = new MemoryStream()) { var target = new StreamByteTarget(stream); var writer = new DicomWriter( DicomTransferSyntax.ExplicitVRLittleEndian, new DicomWriteOptions { LargeObjectSize = 200 }, target); writer.OnBeginWalk(); Assert.True(writer.OnFragmentItem(new MemoryByteBuffer(expected))); e.Wait(100); var actual = new byte[expected.Length]; stream.Seek(8, SeekOrigin.Begin); stream.Read(actual, 0, actual.Length); Assert.Equal(expected, actual); } }
/// <summary> /// Write DICOM dataset. /// </summary> /// <param name="target">Byte target subject to writing.</param> /// <param name="syntax">Transfer syntax applicable to dataset.</param> /// <param name="dataset">Dataset.</param> /// <param name="options">Writer options.</param> private static async Task WriteDatasetAsync( IByteTarget target, DicomTransferSyntax syntax, DicomDataset dataset, DicomWriteOptions options) { UpdateDatasetGroupLengths(syntax, dataset, options); if (syntax.IsDeflate) { using (var uncompressed = new MemoryStream()) { var temp = new StreamByteTarget(uncompressed); await WalkDatasetAsync(temp, syntax, dataset, options).ConfigureAwait(false); uncompressed.Seek(0, SeekOrigin.Begin); using (var compressed = new MemoryStream()) using (var compressor = new DeflateStream(compressed, CompressionMode.Compress)) { uncompressed.CopyTo(compressor); target.Write(compressed.ToArray(), 0, (uint)compressed.Length); } } } else { await WalkDatasetAsync(target, syntax, dataset, options).ConfigureAwait(false); } }
/// <summary> /// Write DICOM dataset. /// </summary> /// <param name="target">Byte target subject to writing.</param> /// <param name="syntax">Transfer syntax applicable to dataset.</param> /// <param name="dataset">Dataset.</param> /// <param name="options">Writer options.</param> private static void WriteDataset( IByteTarget target, DicomTransferSyntax syntax, DicomDataset dataset, DicomWriteOptions options) { UpdateDatasetGroupLengths(syntax, dataset, options); if (syntax.IsDeflate) { #if NET35 throw new NotSupportedException("Deflated datasets not supported in Unity."); #else using (var uncompressed = new MemoryStream()) { var temp = new StreamByteTarget(uncompressed); WalkDataset(temp, syntax, dataset, options); uncompressed.Seek(0, SeekOrigin.Begin); using (var compressed = new MemoryStream()) using (var compressor = new DeflateStream(compressed, CompressionMode.Compress)) { uncompressed.CopyTo(compressor); target.Write(compressed.ToArray(), 0, (uint)compressed.Length); } } #endif } else { WalkDataset(target, syntax, dataset, options); } }