Example #1
0
        public new static DicomDirectory Open(string fileName)
        {
            var df = new DicomDirectory();

            // reset datasets
            df.FileMetaInfo.Clear();
            df.Dataset.Clear();

            try {
                df.File = new FileReference(fileName);

                using (var source = new FileByteSource(df.File)) {
                    DicomFileReader reader = new DicomFileReader();

                    var datasetObserver = new DicomDatasetReaderObserver(df.Dataset);
                    var dirObserver     = new DicomDirectoryReaderObserver(df.Dataset);

                    reader.Read(source,
                                new DicomDatasetReaderObserver(df.FileMetaInfo),
                                new DicomReaderMultiObserver(datasetObserver, dirObserver));

                    df.Format = reader.FileFormat;

                    df.Dataset.InternalTransferSyntax = reader.Syntax;

                    df._directoryRecordSequence = df.Dataset.Get <DicomSequence>(DicomTag.DirectoryRecordSequence);
                    df.RootDirectoryRecord      = dirObserver.BuildDirectoryRecords();

                    return(df);
                }
            } catch (Exception e) {
                throw new DicomFileException(df, e.Message, e);
            }
        }
Example #2
0
        private static DicomDirectory FinalizeDicomDirectoryLoad(
            DicomDirectory df,
            DicomFileReader reader,
            DicomDirectoryReaderObserver dirObserver,
            DicomReaderResult result)
        {
            if (result == DicomReaderResult.Processing)
            {
                throw new DicomFileException(df, "Invalid read return state: {state}", result);
            }
            if (result == DicomReaderResult.Error)
            {
                return(null);
            }
            df.IsPartial = result == DicomReaderResult.Stopped || result == DicomReaderResult.Suspended;

            df.Format = reader.FileFormat;

            df.Dataset.InternalTransferSyntax = reader.Syntax;

            df._directoryRecordSequence = df.Dataset.Get <DicomSequence>(DicomTag.DirectoryRecordSequence);
            df.RootDirectoryRecord      = dirObserver.BuildDirectoryRecords();

            return(df);
        }
Example #3
0
        public void Exact_Offsets_Should_All_Match()
        {
            var offsets = GenerateUniqueRandonmOffsets(155, 500000);
            var root    = new Container()
            {
                Sequence = new DicomDataset()
            };

            root.CreateChildren(offsets, 5, 3, 3, 2);

            var allChildren = root.AllContainer.Skip(1).ToArray();
            var sequence    = new DicomSequence(DicomTag.DirectoryRecordSequence);

            root.Sequence.AddOrUpdate(sequence);

            var byteSource = new TestByteSource();
            var observer   = new DicomDirectoryReaderObserver(root.Sequence);

            observer.OnBeginSequence(byteSource, DicomTag.DirectoryRecordSequence, 0);
            foreach (var container in allChildren)
            {
                sequence.Items.Add(container.Sequence);
                byteSource.Position = container.Offset + 8;
                observer.OnBeginSequenceItem(byteSource, 0);
                observer.OnEndSequenceItem();
            }
            observer.OnEndSequence();

            var rootRecord = new DicomDirectoryRecord
            {
                LowerLevelDirectoryRecord = observer.BuildDirectoryRecords(),
            };

            root.AssertRecord(rootRecord);
        }
Example #4
0
        /// <summary>
        /// Asynchronously read DICOM Directory.
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="fallbackEncoding">Encoding to apply if it cannot be identified from DICOM directory.</param>
        /// <param name="stop">Stop criterion in dataset.</param>
        /// <returns>Awaitable <see cref="DicomDirectory"/> instance.</returns>
        public static new async Task <DicomDirectory> OpenAsync(string fileName, Encoding fallbackEncoding, Func <ParseState, bool> stop = null)
        {
            if (fallbackEncoding == null)
            {
                throw new ArgumentNullException("fallbackEncoding");
            }
            var df = new DicomDirectory();

            // reset datasets
            df.FileMetaInfo.Clear();
            df.Dataset.Clear();

            try
            {
                df.File = IOManager.CreateFileReference(fileName);

                using (var source = new FileByteSource(df.File))
                {
                    var reader = new DicomFileReader();

                    var datasetObserver = new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding);
                    var dirObserver     = new DicomDirectoryReaderObserver(df.Dataset);

                    var result =
                        await
                        reader.ReadAsync(
                            source,
                            new DicomDatasetReaderObserver(df.FileMetaInfo),
                            new DicomReaderMultiObserver(datasetObserver, dirObserver),
                            stop).ConfigureAwait(false);

                    if (result == DicomReaderResult.Processing)
                    {
                        throw new DicomFileException(df, "Invalid read return state: {state}", result);
                    }
                    if (result == DicomReaderResult.Error)
                    {
                        return(null);
                    }
                    df.IsPartial = result == DicomReaderResult.Stopped || result == DicomReaderResult.Suspended;

                    df.Format = reader.FileFormat;

                    df.Dataset.InternalTransferSyntax = reader.Syntax;

                    df._directoryRecordSequence = df.Dataset.Get <DicomSequence>(DicomTag.DirectoryRecordSequence);
                    df.RootDirectoryRecord      = dirObserver.BuildDirectoryRecords();

                    return(df);
                }
            }
            catch (Exception e)
            {
                throw new DicomFileException(df, e.Message, e);
            }
        }
Example #5
0
        public void Duplicate_Offsets_Should_Not_Throw()
        {
            var offsets = new List <uint>(GenerateUniqueRandonmOffsets(155, 100000).Select(o => o * 5));

            for (int i = 0; i < offsets.Count - 2; i += 2)
            {
                offsets[i + 1] = offsets[i];
            }

            var root = new Container()
            {
                Sequence = new DicomDataset()
            };

            root.CreateChildren(offsets, 5, 3, 3, 2);

            var allChildren = root.AllContainer.Skip(1).ToArray();
            var sequence    = new DicomSequence(DicomTag.DirectoryRecordSequence);

            root.Sequence.AddOrUpdate(sequence);

            var byteSource = new TestByteSource();
            var observer   = new DicomDirectoryReaderObserver(root.Sequence);

            observer.OnBeginSequence(byteSource, DicomTag.DirectoryRecordSequence, 0);
            uint lastPosition = 0;

            foreach (var container in allChildren)
            {
                sequence.Items.Add(container.Sequence);
                var position = container.Offset + 8;
                if (position == lastPosition)
                {
                    position += 1;
                }
                lastPosition = position;

                byteSource.Position = position;
                observer.OnBeginSequenceItem(byteSource, 0);
                observer.OnEndSequenceItem();
            }
            observer.OnEndSequence();

            var rootRecord = new DicomDirectoryRecord
            {
                LowerLevelDirectoryRecord = observer.BuildDirectoryRecords(),
            };

            // Cannot assert all records when duplicate offsets exist.
            // root.AssertRecord(rootRecord);
        }