示例#1
0
        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);
            }
        }
示例#2
0
        /// <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);
            }
        }
示例#3
0
        /// <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);
            }
        }
示例#4
0
        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);
            }
        }
示例#6
0
        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);
                }
            }
        }
示例#7
0
        /// <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);
        }
示例#8
0
        /// <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);
        }
示例#9
0
        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);
        }
示例#10
0
        /// <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);
        }
示例#11
0
        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);
            }
        }
示例#12
0
        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);
            }
        }
示例#13
0
        /// <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);
            }
        }
示例#14
0
        /// <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);
            }
        }