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);
        }
        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);
        }
Esempio n. 3
0
		public new static IAsyncResult BeginOpen(string fileName, AsyncCallback callback, object state) {
			var df = new DicomDirectory();

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

			df.File = new FileReference(fileName);

			FileByteSource source = new FileByteSource(df.File);

			EventAsyncResult result = new EventAsyncResult(callback, state);

			DicomFileReader reader = new DicomFileReader();

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

			reader.BeginRead(source,
				new DicomDatasetReaderObserver(df.FileMetaInfo),
				new DicomReaderMultiObserver(datasetObserver, dirObserver),
				OnReadComplete, new Tuple<DicomFileReader, DicomDirectory, DicomDirectoryReaderObserver, EventAsyncResult>(reader, df, dirObserver, result));

			return result;
		}
Esempio n. 4
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);
			}
		}
Esempio n. 5
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;
        }
Esempio n. 6
0
        /// <summary>
        /// Asynchronously read DICOM Directory from stream.
        /// </summary>
        /// <param name="stream">Stream to read.</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(Stream stream, Encoding fallbackEncoding, Func<ParseState, bool> stop = null)
        {
            if (fallbackEncoding == null)
            {
                throw new ArgumentNullException(nameof(fallbackEncoding));
            }
            var df = new DicomDirectory();

            try
            {
                var source = new StreamByteSource(stream);

                var reader = new DicomFileReader();
                var dirObserver = new DicomDirectoryReaderObserver(df.Dataset);

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

                return FinalizeDicomDirectoryLoad(df, reader, dirObserver, result);
            }
            catch (Exception e)
            {
                throw new DicomFileException(df, e.Message, e);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 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><see cref="DicomDirectory"/> instance.</returns>
        public static new DicomDirectory Open(string fileName, Encoding fallbackEncoding, Func<ParseState, bool> stop = null)
        {
            if (fallbackEncoding == null)
            {
                throw new ArgumentNullException(nameof(fallbackEncoding));
            }
            var df = new DicomDirectory();

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

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

                    var result = reader.Read(
                        source,
                        new DicomDatasetReaderObserver(df.FileMetaInfo),
                        new DicomReaderMultiObserver(
                            new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding),
                            dirObserver),
                        stop);

                    return FinalizeDicomDirectoryLoad(df, reader, dirObserver, result);
                }
            }
            catch (Exception e)
            {
                throw new DicomFileException(df, e.Message, e);
            }
        }
Esempio n. 8
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);
            }
        }