Exemplo n.º 1
0
        private void ParsePreamble(IByteSource source, object state)
        {
            try {
                if (!source.Require(132, ParsePreamble, state))
                {
                    return;
                }

                _source.Skip(128);
                if (_source.GetUInt8() != 'D' ||
                    _source.GetUInt8() != 'I' ||
                    _source.GetUInt8() != 'C' ||
                    _source.GetUInt8() != 'M')
                {
                    throw new DicomReaderException("Invalid preamble found in DICOM file parser");
                }

                DicomReaderCallbackObserver obs = new DicomReaderCallbackObserver();
                obs.Add(DicomTag.TransferSyntaxUID, delegate(object sender, DicomReaderEventArgs ea) {
                    try {
                        string uid = Encoding.ASCII.GetString(ea.Data.Data);
                        _syntax    = DicomTransferSyntax.Parse(uid);
                    } catch {
                    }
                });

                _source.Endian       = _syntax.Endian;
                _reader.IsExplicitVR = _syntax.IsExplicitVR;
                _reader.BeginRead(_source, new DicomReaderMultiObserver(obs, _fmiObserver), FileMetaInfoStopTag, OnFileMetaInfoParseComplete, null);
            } catch (Exception e) {
                if (_exception == null)
                {
                    _exception = e;
                }
                _result = DicomReaderResult.Error;
            } finally {
                if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended)
                {
                    _async.Set();
                }
            }
        }
Exemplo n.º 2
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.º 3
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.º 4
0
		private void ParsePreamble(IByteSource source, object state) {
			try {
				if (!source.Require(132, ParsePreamble, state))
					return;

				// mark file origin
				_source.Mark();

				// test for DICM preamble
				_source.Skip(128);
				if (_source.GetUInt8() == 'D' &&
					_source.GetUInt8() == 'I' &&
					_source.GetUInt8() == 'C' &&
					_source.GetUInt8() == 'M')
					_fileFormat = DicomFileFormat.DICOM3;

				// test for incorrect syntax in file meta info
				 do {
					 if (_fileFormat == DicomFileFormat.DICOM3) {
						 // move milestone to after preamble
						 _source.Mark();
					 } else {
						 // rewind to origin milestone
						 _source.Rewind();
					 }

					// test for file meta info
					var group = _source.GetUInt16();

					if (group > 0x00ff) {
						_source.Endian = Endian.Big;
						_syntax = DicomTransferSyntax.ExplicitVRBigEndian;

						group = Endian.Swap(group);
					}

					if (group > 0x00ff) {
						// invalid starting tag
						_fileFormat = DicomFileFormat.Unknown;
						_source.Rewind();
						break;
					}

					if (_fileFormat == DicomFileFormat.Unknown) {
						if (group == 0x0002)
							_fileFormat = DicomFileFormat.DICOM3NoPreamble;
						else
							_fileFormat = DicomFileFormat.DICOM3NoFileMetaInfo;
					}

					var element = _source.GetUInt16();
					var tag = new DicomTag(group, element);

					// test for explicit VR
					var vrt = Encoding.UTF8.GetBytes(tag.DictionaryEntry.ValueRepresentations[0].Code);
					var vrs = _source.GetBytes(2);

					if (vrt[0] != vrs[0] || vrt[1] != vrs[1]) {
						// implicit VR
						if (_syntax.Endian == Endian.Little)
							_syntax = DicomTransferSyntax.ImplicitVRLittleEndian;
						else
							_syntax = DicomTransferSyntax.ImplicitVRBigEndian;
					}

					_source.Rewind();
				} while (_fileFormat == DicomFileFormat.Unknown);

				if (_fileFormat == DicomFileFormat.Unknown)
					throw new DicomReaderException("Attempted to read invalid DICOM file");

				var obs = new DicomReaderCallbackObserver();
				if (_fileFormat != DicomFileFormat.DICOM3) {
					obs.Add(DicomTag.RecognitionCodeRETIRED, (object sender, DicomReaderEventArgs ea) => {
						try {
							string code = Encoding.UTF8.GetString(ea.Data.Data, 0, ea.Data.Data.Length);
							if (code == "ACR-NEMA 1.0")
								_fileFormat = DicomFileFormat.ACRNEMA1;
							else if (code == "ACR-NEMA 2.0")
								_fileFormat = DicomFileFormat.ACRNEMA2;
						} catch {
						}
					});
				}
				obs.Add(DicomTag.TransferSyntaxUID, (object sender, DicomReaderEventArgs ea) => {
					try {
						string uid = Encoding.UTF8.GetString(ea.Data.Data, 0, ea.Data.Data.Length);
						_syntax = DicomTransferSyntax.Parse(uid);
					} catch {
					}
				});

				_source.Endian = _syntax.Endian;
				_reader.IsExplicitVR = _syntax.IsExplicitVR;

				if (_fileFormat == DicomFileFormat.DICOM3NoFileMetaInfo)
					_reader.BeginRead(_source, new DicomReaderMultiObserver(obs, _dataObserver), null, OnDatasetParseComplete, null);
				else
					_reader.BeginRead(_source, new DicomReaderMultiObserver(obs, _fmiObserver), FileMetaInfoStopTag, OnFileMetaInfoParseComplete, null);
			} catch (Exception e) {
				if (_exception == null)
					_exception = e;
				_result = DicomReaderResult.Error;
			} finally {
				if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) {
					_async.Set();
				}
			}
		}
Exemplo n.º 5
0
        private void ParsePreamble(IByteSource source, object state)
        {
            try {
                if (!source.Require(132, ParsePreamble, state))
                {
                    return;
                }

                // mark file origin
                _source.Mark();

                // test for DICM preamble
                _source.Skip(128);
                if (_source.GetUInt8() == 'D' &&
                    _source.GetUInt8() == 'I' &&
                    _source.GetUInt8() == 'C' &&
                    _source.GetUInt8() == 'M')
                {
                    _fileFormat = DicomFileFormat.DICOM3;
                }

                // test for incorrect syntax in file meta info
                do
                {
                    if (_fileFormat == DicomFileFormat.DICOM3)
                    {
                        // move milestone to after preamble
                        _source.Mark();
                    }
                    else
                    {
                        // rewind to origin milestone
                        _source.Rewind();
                    }

                    // test for file meta info
                    var group = _source.GetUInt16();

                    if (group > 0x00ff)
                    {
                        _source.Endian = Endian.Big;
                        _syntax        = DicomTransferSyntax.ExplicitVRBigEndian;

                        group = Endian.Swap(group);
                    }

                    if (group > 0x00ff)
                    {
                        // invalid starting tag
                        _fileFormat = DicomFileFormat.Unknown;
                        _source.Rewind();
                        break;
                    }

                    if (_fileFormat == DicomFileFormat.Unknown)
                    {
                        if (group == 0x0002)
                        {
                            _fileFormat = DicomFileFormat.DICOM3NoPreamble;
                        }
                        else
                        {
                            _fileFormat = DicomFileFormat.DICOM3NoFileMetaInfo;
                        }
                    }

                    var element = _source.GetUInt16();
                    var tag     = new DicomTag(group, element);

                    // test for explicit VR
                    var vrt = Encoding.ASCII.GetBytes(tag.DictionaryEntry.ValueRepresentations[0].Code);
                    var vrs = _source.GetBytes(2);

                    if (vrt[0] != vrs[0] || vrt[1] != vrs[1])
                    {
                        // implicit VR
                        if (_syntax.Endian == Endian.Little)
                        {
                            _syntax = DicomTransferSyntax.ImplicitVRLittleEndian;
                        }
                        else
                        {
                            _syntax = DicomTransferSyntax.ImplicitVRBigEndian;
                        }
                    }

                    _source.Rewind();
                } while (_fileFormat == DicomFileFormat.Unknown);

                if (_fileFormat == DicomFileFormat.Unknown)
                {
                    throw new DicomReaderException("Attempted to read invalid DICOM file");
                }

                var obs = new DicomReaderCallbackObserver();
                if (_fileFormat != DicomFileFormat.DICOM3)
                {
                    obs.Add(DicomTag.RecognitionCodeRETIRED, (object sender, DicomReaderEventArgs ea) => {
                        try {
                            string code = Encoding.ASCII.GetString(ea.Data.Data);
                            if (code == "ACR-NEMA 1.0")
                            {
                                _fileFormat = DicomFileFormat.ACRNEMA1;
                            }
                            else if (code == "ACR-NEMA 2.0")
                            {
                                _fileFormat = DicomFileFormat.ACRNEMA2;
                            }
                        } catch {
                        }
                    });
                }
                obs.Add(DicomTag.TransferSyntaxUID, (object sender, DicomReaderEventArgs ea) => {
                    try {
                        string uid = Encoding.ASCII.GetString(ea.Data.Data);
                        _syntax    = DicomTransferSyntax.Parse(uid);
                    } catch {
                    }
                });

                _source.Endian       = _syntax.Endian;
                _reader.IsExplicitVR = _syntax.IsExplicitVR;

                if (_fileFormat == DicomFileFormat.DICOM3NoFileMetaInfo)
                {
                    _reader.BeginRead(_source, new DicomReaderMultiObserver(obs, _dataObserver), null, OnDatasetParseComplete, null);
                }
                else
                {
                    _reader.BeginRead(_source, new DicomReaderMultiObserver(obs, _fmiObserver), FileMetaInfoStopTag, OnFileMetaInfoParseComplete, null);
                }
            } catch (Exception e) {
                if (_exception == null)
                {
                    _exception = e;
                }
                _result = DicomReaderResult.Error;
            } finally {
                if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended)
                {
                    _async.Set();
                }
            }
        }
Exemplo n.º 6
0
        private void ParsePreamble(IByteSource source, object state)
        {
            try {
                if (!source.Require(132, ParsePreamble, state))
                    return;

                _source.Skip(128);
                if (_source.GetUInt8() != 'D' ||
                    _source.GetUInt8() != 'I' ||
                    _source.GetUInt8() != 'C' ||
                    _source.GetUInt8() != 'M')
                    throw new DicomReaderException("Invalid preamble found in DICOM file parser");

                DicomReaderCallbackObserver obs = new DicomReaderCallbackObserver();
                obs.Add(DicomTag.TransferSyntaxUID, delegate(object sender, DicomReaderEventArgs ea) {
                    try {
                        string uid = Encoding.ASCII.GetString(ea.Data.Data);
                        _syntax = DicomTransferSyntax.Parse(uid);
                    } catch {
                    }
                });

                _source.Endian = _syntax.Endian;
                _reader.IsExplicitVR = _syntax.IsExplicitVR;
                _reader.BeginRead(_source, new DicomReaderMultiObserver(obs, _fmiObserver), FileMetaInfoStopTag, OnFileMetaInfoParseComplete, null);
            } catch (Exception e) {
                if (_exception == null)
                    _exception = e;
                _result = DicomReaderResult.Error;
            } finally {
                if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) {
                    _async.Set();
                }
            }
        }