Exemplo n.º 1
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 Task WriteDatasetAsync(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            UpdateDatasetGroupLengths(syntax, dataset, options);

            var writer = new DicomWriter(syntax, options, target);
            var walker = new DicomDatasetWalker(dataset);

            return(walker.WalkAsync(writer));
        }
Exemplo n.º 2
0
        ////不支持.net4
        ///// <summary>
        ///// Write DICOM file preamble.
        ///// </summary>
        ///// <param name="target">Byte target subject to writing.</param>
        //private static Task WritePreambleAsync(IByteTarget target)
        //{
        //    var preamble = new byte[132];
        //    preamble[128] = (byte)'D';
        //    preamble[129] = (byte)'I';
        //    preamble[130] = (byte)'C';
        //    preamble[131] = (byte)'M';

        //    return target.WriteAsync(preamble, 0, 132);
        //}

        /// <summary>
        /// Write DICOM file meta information.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="fileMetaInfo">File meta information.</param>
        /// <param name="options">Writer options.</param>
        private static void WriteFileMetaInfo(
            IByteTarget target,
            DicomDataset fileMetaInfo,
            DicomWriteOptions options)
        {
            // recalculate FMI group length as required by standard
            fileMetaInfo.RecalculateGroupLengths();

            var writer = new DicomWriter(DicomTransferSyntax.ExplicitVRLittleEndian, options, target);
            var walker = new DicomDatasetWalker(fileMetaInfo);

            walker.Walk(writer);
        }
Exemplo n.º 3
0
        ////不支持.net4
        ///// <summary>
        ///// Write DICOM file meta information.
        ///// </summary>
        ///// <param name="target">Byte target subject to writing.</param>
        ///// <param name="fileMetaInfo">File meta information.</param>
        ///// <param name="options">Writer options.</param>
        //private static Task WriteFileMetaInfoAsync(
        //    IByteTarget target,
        //    DicomDataset fileMetaInfo,
        //    DicomWriteOptions options)
        //{
        //    // recalculate FMI group length as required by standard
        //    fileMetaInfo.RecalculateGroupLengths();

        //    var writer = new DicomWriter(DicomTransferSyntax.ExplicitVRLittleEndian, options, target);
        //    var walker = new DicomDatasetWalker(fileMetaInfo);
        //    return walker.WalkAsync(writer);
        //}

        /// <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);

            var writer = new DicomWriter(syntax, options, target);
            var walker = new DicomDatasetWalker(dataset);

            walker.Walk(writer);
        }
Exemplo n.º 4
0
        ////不支持.net4
        ///// <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 Task WriteDatasetAsync(
        //    IByteTarget target,
        //    DicomTransferSyntax syntax,
        //    DicomDataset dataset,
        //    DicomWriteOptions options)
        //{
        //    UpdateDatasetGroupLengths(syntax, dataset, options);

        //    var writer = new DicomWriter(syntax, options, target);
        //    var walker = new DicomDatasetWalker(dataset);
        //    return walker.WalkAsync(writer);
        //}

        /// <summary>
        /// If necessary, update dataset syntax and group lengths.
        /// </summary>
        /// <param name="syntax">Transfer syntax.</param>
        /// <param name="dataset">DICOM dataset.</param>
        /// <param name="options">Writer options.</param>
        private static void UpdateDatasetGroupLengths(
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            if (options.KeepGroupLengths)
            {
                // update transfer syntax and recalculate existing group lengths
                dataset.InternalTransferSyntax = syntax;
                dataset.RecalculateGroupLengths(false);
            }
            else
            {
                // remove group lengths as suggested in PS 3.5 7.2
                //
                //	2. It is recommended that Group Length elements be removed during storage or transfer
                //	   in order to avoid the risk of inconsistencies arising during coercion of data
                //	   element values and changes in transfer syntax.
                dataset.RemoveGroupLengths();
            }
        }
Exemplo n.º 5
0
        public bool OnBeginSequence(DicomSequence sequence)
        {
            uint length = UndefinedLength;

            if (_options.ExplicitLengthSequences || sequence.Tag.IsPrivate)
            {
                DicomWriteOptions options = _options;
                if (sequence.Tag.IsPrivate)
                {
                    options = new DicomWriteOptions(options);
                    options.ExplicitLengthSequences     = false;
                    options.ExplicitLengthSequenceItems = false;
                }

                DicomWriteLengthCalculator calc = new DicomWriteLengthCalculator(_syntax, options);
                length = calc.Calculate(sequence);
            }

            _sequences.Push(sequence);

            WriteTagHeader(sequence.Tag, DicomVR.SQ, length);
            return(true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Write DICOM file meta information.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="fileMetaInfo">File meta information.</param>
        /// <param name="options">Writer options.</param>
        private static void WriteFileMetaInfo(
            IByteTarget target,
            DicomDataset fileMetaInfo,
            DicomWriteOptions options)
        {
            // recalculate FMI group length as required by standard
            fileMetaInfo.RecalculateGroupLengths();

            var writer = new DicomWriter(DicomTransferSyntax.ExplicitVRLittleEndian, options, target);
            var walker = new DicomDatasetWalker(fileMetaInfo);
            walker.Walk(writer);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes an instance of <see cref="DicomWriter"/>.
 /// </summary>
 /// <param name="syntax">Writer transfer syntax.</param>
 /// <param name="options">Writer options.</param>
 /// <param name="target">Target to which to write the DICOM object.</param>
 public DicomWriter(DicomTransferSyntax syntax, DicomWriteOptions options, IByteTarget target)
 {
     _syntax  = syntax;
     _options = options ?? DicomWriteOptions.Default;
     _target  = target;
 }
Exemplo n.º 8
0
 public DicomFileWriter(DicomWriteOptions options)
 {
     _options = options;
 }
Exemplo n.º 9
0
 public DicomWriteOptions(DicomWriteOptions options)
 {
     ExplicitLengthSequences     = options.ExplicitLengthSequences;
     ExplicitLengthSequenceItems = options.ExplicitLengthSequenceItems;
     LargeObjectSize             = options.LargeObjectSize;
 }
Exemplo n.º 10
0
 public DicomWriteLengthCalculator(DicomTransferSyntax syntax, DicomWriteOptions options)
 {
     _syntax  = syntax;
     _options = options;
 }
Exemplo n.º 11
0
 private static void WalkDataset(
     IByteTarget target,
     DicomTransferSyntax syntax,
     DicomDataset dataset,
     DicomWriteOptions options)
 {
     var writer = new DicomWriter(syntax, options, target);
     var walker = new DicomDatasetWalker(dataset);
     walker.Walk(writer);
 }
Exemplo n.º 12
0
 public DicomWriter(DicomTransferSyntax syntax, DicomWriteOptions options, IByteTarget target)
 {
     _syntax = syntax;
     _options = options ?? DicomWriteOptions.Default;
     _target = target;
 }
Exemplo n.º 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);
            }
        }
 /// <summary>
 /// Initializes an instance of a <see cref="DicomFileWriter"/>.
 /// </summary>
 /// <param name="options">Writer options.</param>
 public DicomFileWriter(DicomWriteOptions options)
 {
     this.options = options ?? DicomWriteOptions.Default;
 }
Exemplo n.º 15
0
 /// <summary>
 /// If necessary, update dataset syntax and group lengths.
 /// </summary>
 /// <param name="syntax">Transfer syntax.</param>
 /// <param name="dataset">DICOM dataset.</param>
 /// <param name="options">Writer options.</param>
 private static void UpdateDatasetGroupLengths(
     DicomTransferSyntax syntax,
     DicomDataset dataset,
     DicomWriteOptions options)
 {
     if (options.KeepGroupLengths)
     {
         // update transfer syntax and recalculate existing group lengths
         dataset.InternalTransferSyntax = syntax;
         dataset.RecalculateGroupLengths(false);
     }
     else
     {
         // remove group lengths as suggested in PS 3.5 7.2
         //
         //	2. It is recommended that Group Length elements be removed during storage or transfer 
         //	   in order to avoid the risk of inconsistencies arising during coercion of data 
         //	   element values and changes in transfer syntax.
         dataset.RemoveGroupLengths();
     }
 }
Exemplo n.º 16
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 Task WriteDatasetAsync(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            UpdateDatasetGroupLengths(syntax, dataset, options);

            var writer = new DicomWriter(syntax, options, target);
            var walker = new DicomDatasetWalker(dataset);
            return walker.WalkAsync(writer);
        }
Exemplo n.º 17
0
		public DicomWriteOptions(DicomWriteOptions options) {
			ExplicitLengthSequences = options.ExplicitLengthSequences;
			ExplicitLengthSequenceItems = options.ExplicitLengthSequenceItems;
			KeepGroupLengths = options.KeepGroupLengths;
			LargeObjectSize = options.LargeObjectSize;
		}
Exemplo n.º 18
0
 /// <summary>
 /// Initializes an instance of a <see cref="DicomFileWriter"/>.
 /// </summary>
 /// <param name="options">Writer options.</param>
 public DicomFileWriter(DicomWriteOptions options)
 {
     this.options = options;
 }
Exemplo n.º 19
0
 public DicomWriter(DicomTransferSyntax syntax, DicomWriteOptions options, IByteTarget target)
 {
     _syntax  = syntax;
     _options = new DicomWriteOptions();
     _target  = target;
 }
		public DicomWriteLengthCalculator(DicomTransferSyntax syntax, DicomWriteOptions options) {
			_syntax = syntax;
			_options = options;
		}
Exemplo n.º 21
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);
            }
        }