Exemplo n.º 1
0
        private static ParseResult Parse(
            IByteSource source,
            IDicomReaderObserver fileMetasetInfoObserver,
            IDicomReaderObserver datasetObserver,
            Func <ParseState, bool> stop)
        {
            if (!source.Require(132))
            {
                return(new ParseResult(DicomReaderResult.Error, DicomFileFormat.Unknown, null));
            }

            var fileFormat = DicomFileFormat.Unknown;
            var syntax     = DicomTransferSyntax.ExplicitVRLittleEndian;

            Preprocess(source, ref fileFormat, ref syntax);

            var result = DoParse(
                source,
                fileMetasetInfoObserver,
                datasetObserver,
                stop,
                ref syntax,
                ref fileFormat);

            return(new ParseResult(result, fileFormat, syntax));
        }
Exemplo n.º 2
0
		public IAsyncResult BeginRead(IByteSource source, IDicomReaderObserver observer, DicomTag stop, AsyncCallback callback, object state) {
			_stop = stop;
			_observer = observer;
			_result = DicomReaderResult.Processing;
			_exception = null;
			_async = new EventAsyncResult(callback, state);
			ThreadPool.QueueUserWorkItem(ParseProc, source);
			return _async;
		}
Exemplo n.º 3
0
		public IAsyncResult BeginRead(IByteSource source, IDicomReaderObserver fileMetaInfo, IDicomReaderObserver dataset, AsyncCallback callback, object state) {
			_result = DicomReaderResult.Processing;
			_source = source;
			_fmiObserver = fileMetaInfo;
			_dataObserver = dataset;
			_async = new EventAsyncResult(callback, state);
			ParsePreamble(source, null); // ThreadPool?
			return _async;
		}
Exemplo n.º 4
0
 public IAsyncResult BeginRead(IByteSource source, IDicomReaderObserver fileMetaInfo, IDicomReaderObserver dataset, AsyncCallback callback, object state)
 {
     _result       = DicomReaderResult.Processing;
     _source       = source;
     _fmiObserver  = fileMetaInfo;
     _dataObserver = dataset;
     _async        = new EventAsyncResult(callback, state);
     ParsePreamble(source, null);             // ThreadPool?
     return(_async);
 }
Exemplo n.º 5
0
 public static IAsyncResult BeginRead(
     this DicomReader @this,
     IByteSource source,
     IDicomReaderObserver observer,
     Func<ParseState, bool> stop,
     AsyncCallback callback,
     object state)
 {
     return AsyncFactory.ToBegin(@this.ReadAsync(source, observer, stop), callback, state);
 }
Exemplo n.º 6
0
 public static IAsyncResult BeginRead(
     this DicomReader @this,
     IByteSource source,
     IDicomReaderObserver observer,
     Func <ParseState, bool> stop,
     AsyncCallback callback,
     object state)
 {
     return(AsyncFactory.ToBegin(@this.ReadAsync(source, observer, stop), callback, state));
 }
Exemplo n.º 7
0
 public static IAsyncResult BeginRead(
     this DicomFileReader @this,
     IByteSource source,
     IDicomReaderObserver fileMetaInfo,
     IDicomReaderObserver dataset,
     AsyncCallback callback,
     object state)
 {
     return AsyncFactory.ToBegin(@this.ReadAsync(source, fileMetaInfo, dataset), callback, state);
 }
Exemplo n.º 8
0
 public static IAsyncResult BeginRead(
     this DicomFileReader @this,
     IByteSource source,
     IDicomReaderObserver fileMetaInfo,
     IDicomReaderObserver dataset,
     AsyncCallback callback,
     object state)
 {
     return(AsyncFactory.ToBegin(@this.ReadAsync(source, fileMetaInfo, dataset), callback, state));
 }
Exemplo n.º 9
0
 public IAsyncResult BeginRead(IByteSource source, IDicomReaderObserver observer, DicomTag stop, AsyncCallback callback, object state)
 {
     _stop      = stop;
     _observer  = observer;
     _result    = DicomReaderResult.Processing;
     _exception = null;
     _async     = new EventAsyncResult(callback, state);
     ThreadPool.QueueUserWorkItem(ParseProc, source);
     return(_async);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes an instance of <see cref="DicomReaderWorker"/>.
 /// </summary>
 internal DicomReaderWorker(
     IDicomReaderObserver observer,
     Func <ParseState, bool> stop,
     DicomDictionary dictionary,
     bool isExplicitVR,
     Dictionary <uint, string> @private)
 {
     this.observer     = observer;
     this.stop         = stop;
     this.dictionary   = dictionary;
     this.isExplicitVR = isExplicitVR;
     this._private     = @private;
     this.locker       = new object();
 }
Exemplo n.º 11
0
 /// <summary>
 /// Read DICOM file object.
 /// </summary>
 /// <param name="source">Byte source to read.</param>
 /// <param name="fileMetaInfo">Reader observer for file meta information.</param>
 /// <param name="dataset">Reader observer for dataset.</param>
 /// <param name="stop">Stop criterion in dataset.</param>
 /// <returns>Reader result.</returns>
 public DicomReaderResult Read(
     IByteSource source,
     IDicomReaderObserver fileMetaInfo,
     IDicomReaderObserver dataset,
     Func<ParseState, bool> stop = null)
 {
     var parse = Parse(source, fileMetaInfo, dataset, stop);
     lock (this.locker)
     {
         this.fileFormat = parse.Item2;
         this.syntax = parse.Item3;
     }
     return parse.Item1;
 }
Exemplo n.º 12
0
        /// <summary>
        /// Read DICOM file object.
        /// </summary>
        /// <param name="source">Byte source to read.</param>
        /// <param name="fileMetaInfo">Reader observer for file meta information.</param>
        /// <param name="dataset">Reader observer for dataset.</param>
        /// <param name="stop">Stop criterion in dataset.</param>
        /// <returns>Reader result.</returns>
        public DicomReaderResult Read(
            IByteSource source,
            IDicomReaderObserver fileMetaInfo,
            IDicomReaderObserver dataset,
            Func <ParseState, bool> stop = null)
        {
            var parse = Parse(source, fileMetaInfo, dataset, stop);

            lock (this.locker)
            {
                this.fileFormat = parse.Item2;
                this.syntax     = parse.Item3;
            }
            return(parse.Item1);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Asynchronously read DICOM file object.
        /// </summary>
        /// <param name="source">Byte source to read.</param>
        /// <param name="fileMetaInfo">Reader observer for file meta information.</param>
        /// <param name="dataset">Reader observer for dataset.</param>
        /// <param name="stop">Stop criterion in dataset.</param>
        /// <returns>Awaitable reader result.</returns>
        public async Task <DicomReaderResult> ReadAsync(
            IByteSource source,
            IDicomReaderObserver fileMetaInfo,
            IDicomReaderObserver dataset,
            Func <ParseState, bool> stop = null)
        {
            var parse = await ParseAsync(source, fileMetaInfo, dataset, stop).ConfigureAwait(false);

            lock (_locker)
            {
                FileFormat = parse.Item2;
                Syntax     = parse.Item3;
            }
            return(parse.Item1);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Read DICOM file object.
        /// </summary>
        /// <param name="source">Byte source to read.</param>
        /// <param name="fileMetaInfo">Reader observer for file meta information.</param>
        /// <param name="dataset">Reader observer for dataset.</param>
        /// <param name="stop">Stop criterion in dataset.</param>
        /// <returns>Reader result.</returns>
        public DicomReaderResult Read(
            IByteSource source,
            IDicomReaderObserver fileMetaInfo,
            IDicomReaderObserver dataset,
            Func <ParseState, bool> stop = null)
        {
            var parse = Parse(source, fileMetaInfo, dataset, stop);

            lock (_locker)
            {
                FileFormat = parse.Format;
                Syntax     = parse.Syntax;
            }

            return(parse.Result);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Initializes an instance of <see cref="DicomReaderWorker"/>.
 /// </summary>
 internal DicomReaderWorker(
     IDicomReaderObserver observer,
     Func <ParseState, bool> stop,
     DicomDictionary dictionary,
     bool isExplicitVR,
     bool isDeflated,
     Dictionary <uint, string> @private)
 {
     _observer     = observer;
     _stop         = stop;
     _dictionary   = dictionary;
     _isExplicitVR = isExplicitVR;
     _isDeflated   = isDeflated;
     _private      = @private;
     _locker       = new object();
 }
Exemplo n.º 16
0
        private static async Task <Tuple <DicomReaderResult, DicomFileFormat, DicomTransferSyntax> > ParseAsync(
            IByteSource source,
            IDicomReaderObserver fileMetasetInfoObserver,
            IDicomReaderObserver datasetObserver,
            Func <ParseState, bool> stop)
        {
            if (!source.Require(132))
            {
                return(Tuple.Create(DicomReaderResult.Error, DicomFileFormat.Unknown, (DicomTransferSyntax)null));
            }

            var fileFormat = DicomFileFormat.Unknown;
            var syntax     = DicomTransferSyntax.ExplicitVRLittleEndian;

            Preprocess(source, ref fileFormat, ref syntax);

            return
                (await
                 DoParseAsync(source, fileMetasetInfoObserver, datasetObserver, stop, syntax, fileFormat).ConfigureAwait(false));
        }
Exemplo n.º 17
0
 public DicomReaderResult Read(IByteSource source, IDicomReaderObserver fileMetaInfo, IDicomReaderObserver dataset)
 {
     return(EndRead(BeginRead(source, fileMetaInfo, dataset, null, null)));
 }
Exemplo n.º 18
0
        /// <summary>
        /// Perform DICOM reading of a byte source.
        /// </summary>
        /// <param name="source">Byte source to read.</param>
        /// <param name="observer">Reader observer.</param>
        /// <param name="stop">Criterion at which to stop.</param>
        /// <returns>Reader resulting status.</returns>
        public DicomReaderResult Read(IByteSource source, IDicomReaderObserver observer, Func <ParseState, bool> stop = null)
        {
            var worker = new DicomReaderWorker(observer, stop, this.Dictionary, this.IsExplicitVR, this._private);

            return(worker.DoWork(source));
        }
Exemplo n.º 19
0
		public DicomReaderResult Read(IByteSource source, IDicomReaderObserver fileMetaInfo, IDicomReaderObserver dataset) {
			return EndRead(BeginRead(source, fileMetaInfo, dataset, null, null));
		}
Exemplo n.º 20
0
		public DicomReaderResult Read(IByteSource source, IDicomReaderObserver observer, DicomTag stop = null) {
			return EndRead(BeginRead(source, observer, stop, null, null));
		}
Exemplo n.º 21
0
        private static async Task <Tuple <DicomReaderResult, DicomFileFormat, DicomTransferSyntax> > DoParseAsync(
            IByteSource source,
            IDicomReaderObserver fileMetasetInfoObserver,
            IDicomReaderObserver datasetObserver,
            Func <ParseState, bool> stop,
            DicomTransferSyntax syntax,
            DicomFileFormat fileFormat)
        {
            string code = null, uid = null;
            var    obs = new DicomReaderCallbackObserver();

            if (fileFormat != DicomFileFormat.DICOM3)
            {
                obs.Add(
                    DicomTag.RecognitionCodeRETIRED,
                    (sender, ea) =>
                {
                    try
                    {
                        code = Encoding.UTF8.GetString(ea.Data.Data, 0, ea.Data.Data.Length);
                    }
                    catch
                    {
                    }
                });
            }
            obs.Add(
                DicomTag.TransferSyntaxUID,
                (sender, ea) =>
            {
                try
                {
                    uid = Encoding.UTF8.GetString(ea.Data.Data, 0, ea.Data.Data.Length);
                }
                catch
                {
                }
            });

            var reader = new DicomReader {
                IsExplicitVR = syntax.IsExplicitVR, IsDeflated = false
            };

            DicomReaderResult result;

            if (fileFormat == DicomFileFormat.DICOM3NoFileMetaInfo)
            {
                result =
                    await
                    reader.ReadAsync(source, new DicomReaderMultiObserver(obs, datasetObserver), stop).ConfigureAwait(false);

                UpdateFileFormatAndSyntax(code, uid, ref fileFormat, ref syntax);
            }
            else
            {
                if (
                    await
                    reader.ReadAsync(
                        source,
                        new DicomReaderMultiObserver(obs, fileMetasetInfoObserver),
                        _FileMetaInfoStopCriterion).ConfigureAwait(false) != DicomReaderResult.Stopped)
                {
                    throw new DicomReaderException("DICOM File Meta Info ended prematurely");
                }

                UpdateFileFormatAndSyntax(code, uid, ref fileFormat, ref syntax);

                // rewind to last marker (start of previous tag)... ugly because
                // it requires knowledge of how the parser is implemented
                source.Rewind();

                source.Endian       = syntax.Endian;
                reader.IsExplicitVR = syntax.IsExplicitVR;
                reader.IsDeflated   = syntax.IsDeflate;
                result = await reader.ReadAsync(source, datasetObserver, stop).ConfigureAwait(false);
            }

            return(Tuple.Create(result, fileFormat, syntax));
        }
Exemplo n.º 22
0
        private static async Task<Tuple<DicomReaderResult, DicomFileFormat, DicomTransferSyntax>> DoParseAsync(
            IByteSource source,
            IDicomReaderObserver fileMetasetInfoObserver,
            IDicomReaderObserver datasetObserver,
            Func<ParseState, bool> stop,
            DicomTransferSyntax syntax,
            DicomFileFormat fileFormat)
        {
            string code = null, uid = null;
            var obs = new DicomReaderCallbackObserver();
            if (fileFormat != DicomFileFormat.DICOM3)
            {
                obs.Add(
                    DicomTag.RecognitionCodeRETIRED,
                    (sender, ea) =>
                        {
                            try
                            {
                                code = Encoding.UTF8.GetString(ea.Data.Data, 0, ea.Data.Data.Length);
                            }
                            catch
                            {
                            }
                        });
            }
            obs.Add(
                DicomTag.TransferSyntaxUID,
                (sender, ea) =>
                    {
                        try
                        {
                            uid = Encoding.UTF8.GetString(ea.Data.Data, 0, ea.Data.Data.Length);
                        }
                        catch
                        {
                        }
                    });

            var reader = new DicomReader { IsExplicitVR = syntax.IsExplicitVR, IsDeflated = false };

            DicomReaderResult result;
            if (fileFormat == DicomFileFormat.DICOM3NoFileMetaInfo)
            {
                result =
                    await
                    reader.ReadAsync(source, new DicomReaderMultiObserver(obs, datasetObserver), stop).ConfigureAwait(false);
                UpdateFileFormatAndSyntax(code, uid, ref fileFormat, ref syntax);
            }
            else
            {
                if (
                    await
                    reader.ReadAsync(
                        source,
                        new DicomReaderMultiObserver(obs, fileMetasetInfoObserver),
                        FileMetaInfoStopCriterion).ConfigureAwait(false) != DicomReaderResult.Stopped)
                {
                    throw new DicomReaderException("DICOM File Meta Info ended prematurely");
                }

                UpdateFileFormatAndSyntax(code, uid, ref fileFormat, ref syntax);

                // rewind to last marker (start of previous tag)... ugly because 
                // it requires knowledge of how the parser is implemented
                source.Rewind();

                source.Endian = syntax.Endian;
                reader.IsExplicitVR = syntax.IsExplicitVR;
                reader.IsDeflated = syntax.IsDeflate;
                result = await reader.ReadAsync(source, datasetObserver, stop).ConfigureAwait(false);
            }

            return Tuple.Create(result, fileFormat, syntax);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Asynchronously perform DICOM reading of a byte source.
        /// </summary>
        /// <param name="source">Byte source to read.</param>
        /// <param name="observer">Reader observer.</param>
        /// <param name="stop">Criterion at which to stop.</param>
        /// <returns>Awaitable reader resulting status.</returns>
        public Task <DicomReaderResult> ReadAsync(IByteSource source, IDicomReaderObserver observer, Func <ParseState, bool> stop = null)
        {
            var worker = new DicomReaderWorker(observer, stop, Dictionary, IsExplicitVR, IsDeflated, _private);

            return(worker.DoWorkAsync(source));
        }
Exemplo n.º 24
0
 /// <summary>
 /// Asynchronously perform DICOM reading of a byte source.
 /// </summary>
 /// <param name="source">Byte source to read.</param>
 /// <param name="observer">Reader observer.</param>
 /// <param name="stop">Criterion at which to stop.</param>
 /// <returns>Awaitable reader resulting status.</returns>
 public Task<DicomReaderResult> ReadAsync(IByteSource source, IDicomReaderObserver observer, Func<ParseState, bool> stop = null)
 {
     var worker = new DicomReaderWorker(observer, stop, this.Dictionary, this.IsExplicitVR, this._private);
     return worker.DoWorkAsync(source);
 }
Exemplo n.º 25
0
        private static ParseResult Parse(
            IByteSource source,
            IDicomReaderObserver fileMetasetInfoObserver,
            IDicomReaderObserver datasetObserver,
            Func<ParseState, bool> stop)
        {
            if (!source.Require(132))
            {
                return new ParseResult(DicomReaderResult.Error, DicomFileFormat.Unknown, null);
            }

            var fileFormat = DicomFileFormat.Unknown;
            var syntax = DicomTransferSyntax.ExplicitVRLittleEndian;

            Preprocess(source, ref fileFormat, ref syntax);

            var result = DoParse(
                source,
                fileMetasetInfoObserver,
                datasetObserver,
                stop,
                ref syntax,
                ref fileFormat);

            return new ParseResult(result, fileFormat, syntax);
        }
Exemplo n.º 26
0
 public DicomReaderResult Read(IByteSource source, IDicomReaderObserver observer, DicomTag stop = null)
 {
     return(EndRead(BeginRead(source, observer, stop, null, null)));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Asynchronously read DICOM file object.
 /// </summary>
 /// <param name="source">Byte source to read.</param>
 /// <param name="fileMetaInfo">Reader observer for file meta information.</param>
 /// <param name="dataset">Reader observer for dataset.</param>
 /// <param name="stop">Stop criterion in dataset.</param>
 /// <returns>Awaitable reader result.</returns>
 public async Task<DicomReaderResult> ReadAsync(
     IByteSource source,
     IDicomReaderObserver fileMetaInfo,
     IDicomReaderObserver dataset,
     Func<ParseState, bool> stop = null)
 {
     var parse = await ParseAsync(source, fileMetaInfo, dataset, stop).ConfigureAwait(false);
     lock (this.locker)
     {
         this.fileFormat = parse.Item2;
         this.syntax = parse.Item3;
     }
     return parse.Item1;
 }
Exemplo n.º 28
0
 /// <summary>
 /// Initializes an instance of <see cref="DicomReaderWorker"/>.
 /// </summary>
 internal DicomReaderWorker(
     IDicomReaderObserver observer,
     Func<ParseState, bool> stop,
     DicomDictionary dictionary,
     bool isExplicitVR,
     Dictionary<uint, string> @private)
 {
     this.observer = observer;
     this.stop = stop;
     this.dictionary = dictionary;
     this.isExplicitVR = isExplicitVR;
     this._private = @private;
     this.locker = new object();
 }
Exemplo n.º 29
0
        private static async Task<Tuple<DicomReaderResult, DicomFileFormat, DicomTransferSyntax>> ParseAsync(
            IByteSource source,
            IDicomReaderObserver fileMetasetInfoObserver,
            IDicomReaderObserver datasetObserver,
            Func<ParseState, bool> stop)
        {
            if (!source.Require(132))
            {
                return Tuple.Create(DicomReaderResult.Error, DicomFileFormat.Unknown, (DicomTransferSyntax)null);
            }

            var fileFormat = DicomFileFormat.Unknown;
            var syntax = DicomTransferSyntax.ExplicitVRLittleEndian;

            Preprocess(source, ref fileFormat, ref syntax);

            return
                await
                DoParseAsync(source, fileMetasetInfoObserver, datasetObserver, stop, syntax, fileFormat).ConfigureAwait(false);
        }