예제 #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();
                }
            }
        }
예제 #2
0
            private IByteSource Decompress(IByteSource source)
            {
                using (var compressed = new MemoryStream())
                {
                    // It is implicitly assumed that the rest of the byte source is compressed.
                    while (!source.IsEOF)
                    {
                        compressed.WriteByte(source.GetUInt8());
                    }

                    compressed.Seek(0, SeekOrigin.Begin);

                    using (var decompressor = new DeflateStream(compressed, CompressionMode.Decompress))
                    {
                        var decompressed = new MemoryStream();
                        decompressor.CopyTo(decompressed);
                        decompressed.Seek(0, SeekOrigin.Begin);
                        return(new StreamByteSource(decompressed));
                    }
                }
            }
예제 #3
0
        private static void Preprocess(
            IByteSource source,
            ref DicomFileFormat fileFormat,
            ref DicomTransferSyntax syntax)
        {
            // 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)
                {
                    fileFormat = @group == 0x0002
                                     ? DicomFileFormat.DICOM3NoPreamble
                                     : 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
                    syntax = syntax.Endian == Endian.Little
                                 ? DicomTransferSyntax.ImplicitVRLittleEndian
                                 : DicomTransferSyntax.ImplicitVRBigEndian;
                }

                source.Rewind();
            }while (fileFormat == DicomFileFormat.Unknown);

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

            // Adopt transfer syntax endianess to byte source.
            source.Endian = syntax.Endian;
        }
예제 #4
0
        private static void Preprocess(
            IByteSource source,
            ref DicomFileFormat fileFormat,
            ref DicomTransferSyntax syntax)
        {
            // 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)
                {
                    fileFormat = @group == 0x0002
                                     ? DicomFileFormat.DICOM3NoPreamble
                                     : 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
                    syntax = syntax.Endian == Endian.Little
                                 ? DicomTransferSyntax.ImplicitVRLittleEndian
                                 : DicomTransferSyntax.ImplicitVRBigEndian;
                }

                source.Rewind();
            }
            while (fileFormat == DicomFileFormat.Unknown);

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

            // Adopt transfer syntax endianess to byte source.
            source.Endian = syntax.Endian;
        }
예제 #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();
                }
            }
        }
예제 #6
0
            private IByteSource Decompress(IByteSource source)
            {
                using (var compressed = new MemoryStream())
                {
                    // It is implicitly assumed that the rest of the byte source is compressed.
                    while (!source.IsEOF)
                    {
                        compressed.WriteByte(source.GetUInt8());
                    }

                    compressed.Seek(0, SeekOrigin.Begin);

                    using (var decompressor = new DeflateStream(compressed, CompressionMode.Decompress))
                    {
                        var decompressed = new MemoryStream();
                        decompressor.CopyTo(decompressed);
                        decompressed.Seek(0, SeekOrigin.Begin);
                        return new StreamByteSource(decompressed);
                    }
                }
            }