private DicomReadStatus ParseSequenceItemDataset(DicomTransferSyntax syntax, long len, out DcmDataset dataset, DicomReadOptions options) { long pos = _stream.Position; dataset = new DcmDataset(pos, (uint)len, syntax); Stream stream = (len != UndefinedLength) ? new SegmentStream(_stream, _stream.Position, _len) : _stream; DicomStreamReader idsr = new DicomStreamReader(stream); idsr.Dataset = dataset; idsr.Encoding = _encoding; if (len != UndefinedLength) { idsr.PositionOffset = dataset.StreamPosition; } DicomReadStatus status = idsr.Read(null, options); if (status != DicomReadStatus.Success) { _stream.Seek(pos, SeekOrigin.Begin); dataset = null; } else { if (len == UndefinedLength) { // rewind delimitation item tag _stream.Seek(-4, SeekOrigin.Current); len = _stream.Position - pos; } _remain -= len; _bytes += len; _read += len; } return(status); }
public void CloseDataset() { DatasetStream = null; DatasetData = null; DatasetReader = null; if (DatasetFileStream != null) { DatasetFileStream.Dispose(); DatasetFileStream = null; } }
public void CloseCommand() { CommandData = null; CommandReader = null; }
private DicomReadStatus ParseSequenceItemDataset(DicomTransferSyntax syntax, long len, out DcmDataset dataset, DicomReadOptions options) { long pos = _stream.Position; dataset = new DcmDataset(pos, (uint)len, syntax); Stream stream = (len != UndefinedLength) ? new SegmentStream(_stream, _stream.Position, _len) : _stream; DicomStreamReader idsr = new DicomStreamReader(stream); idsr.Dataset = dataset; idsr.Encoding = _encoding; if (len != UndefinedLength) idsr.PositionOffset = dataset.StreamPosition; DicomReadStatus status = idsr.Read(null, options); if (status != DicomReadStatus.Success) { _stream.Seek(pos, SeekOrigin.Begin); dataset = null; } else { if (len == UndefinedLength) { // rewind delimitation item tag _stream.Seek(-4, SeekOrigin.Current); len = _stream.Position - pos; } _remain -= len; _bytes += len; _read += len; } return status; }
/// <summary> /// Gets file stream starting at DICOM dataset /// </summary> /// <param name="file">Filename</param> /// <returns>File stream</returns> public static FileStream GetDatasetStream(String file) { FileStream fs = File.OpenRead(file); fs.Seek(128, SeekOrigin.Begin); CheckFileHeader(fs); DicomStreamReader dsr = new DicomStreamReader(fs); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; if (dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly) == DicomReadStatus.Success && fs.Position < fs.Length) { return fs; } fs.Close(); return null; }
/// <summary> /// Gets the file meta information from a DICOM file /// </summary> /// <param name="file">Filename</param> /// <returns>File meta information</returns> public static DcmFileMetaInfo LoadFileMetaInfo(String file) { using (FileStream fs = File.OpenRead(file)) { fs.Seek(128, SeekOrigin.Begin); CheckFileHeader(fs); DicomStreamReader dsr = new DicomStreamReader(fs); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly); fs.Close(); return metainfo; } }
/// <summary> /// Loads a dicom file, stopping at a certain tag /// </summary> /// <param name="file">Filename</param> /// <param name="stopTag">Tag to stop parsing at</param> /// <param name="options">DICOM read options</param> public DicomReadStatus Load(String file, DicomTag stopTag, DicomReadOptions options) { using (FileStream fs = File.OpenRead(file)) { fs.Seek(128, SeekOrigin.Begin); CheckFileHeader(fs); DicomStreamReader dsr = new DicomStreamReader(fs); _metainfo = new DcmFileMetaInfo(); dsr.Dataset = _metainfo; dsr.Read(DcmFileMetaInfo.StopTag, options | DicomReadOptions.FileMetaInfoOnly); if (_metainfo.TransferSyntax.IsDeflate) { MemoryStream ms = StreamUtility.Deflate(fs, false); dsr = new DicomStreamReader(ms); } _dataset = new DcmDataset(_metainfo.TransferSyntax); dsr.Dataset = _dataset; DicomReadStatus status = dsr.Read(stopTag, options); fs.Close(); return status; } }
/// <summary> /// Gets the file meta information from a DICOM file /// </summary> /// <param name="file">Filename</param> /// <param name="useIsoStore">Load file from isolated storage</param> /// <returns>File meta information</returns> public static DcmFileMetaInfo LoadFileMetaInfo(String file, bool useIsoStore = false) { if (useIsoStore) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var fs = store.OpenFile(file, FileMode.Open, FileAccess.Read)) { fs.Seek(128, SeekOrigin.Begin); if (!CheckFileHeader(fs)) return null; DicomStreamReader dsr = new DicomStreamReader(fs); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly); fs.Close(); return metainfo; } } } else { using (var fs = File.OpenRead(file)) { fs.Seek(128, SeekOrigin.Begin); CheckFileHeader(fs); DicomStreamReader dsr = new DicomStreamReader(fs); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly); fs.Close(); return metainfo; } } }
/// <summary> /// Gets file stream starting at DICOM dataset /// </summary> /// <param name="file">Filename</param> /// <param name="useIsoStore">Get dataset from isolated store</param> /// <returns>File stream</returns> public static FileStream GetDatasetStream(String file, bool useIsoStore = false) { if (useIsoStore) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { var fs = store.OpenFile(file, FileMode.Open, FileAccess.Read); fs.Seek(128, SeekOrigin.Begin); CheckFileHeader(fs); DicomStreamReader dsr = new DicomStreamReader(fs); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; if ( dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly) == DicomReadStatus.Success && fs.Position < fs.Length) { return fs; } fs.Close(); return null; } } else { FileStream fs = File.OpenRead(file); fs.Seek(128, SeekOrigin.Begin); CheckFileHeader(fs); DicomStreamReader dsr = new DicomStreamReader(fs); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; if (dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly) == DicomReadStatus.Success && fs.Position < fs.Length) { return fs; } fs.Close(); return null; } }
/// <summary> /// Loads a dicom file from a stream, stopping at a certain tag /// </summary> /// <param name="stream">Stream containing DICOM file</param> /// <param name="stopTag">Tag to stop parsing at</param> /// <param name="options">DICOM read options</param> public DicomReadStatus Load(Stream stream, DicomTag stopTag, DicomReadOptions options) { stream.Seek(128, SeekOrigin.Begin); CheckFileHeader(stream); DicomStreamReader dsr = new DicomStreamReader(stream); _metainfo = new DcmFileMetaInfo(); dsr.Dataset = _metainfo; dsr.Read(DcmFileMetaInfo.StopTag, options | DicomReadOptions.FileMetaInfoOnly); if (_metainfo.TransferSyntax.IsDeflate) { MemoryStream ms = StreamUtility.Deflate(stream, false); dsr = new DicomStreamReader(ms); } _dataset = new DcmDataset(_metainfo.TransferSyntax); dsr.Dataset = _dataset; DicomReadStatus status = dsr.Read(stopTag, options); return status; }
/// <summary> /// Loads the meta information from a DICOM file in a stream. /// Note that the caller is expected to dispose of the stream after usage. /// </summary> /// <param name="stream">The stream fontaining the DICOM file</param> /// <returns>File meta information</returns> public static DcmFileMetaInfo LoadFileMetaInfo(Stream stream) { stream.Seek(128, SeekOrigin.Begin); CheckFileHeader(stream); DicomStreamReader dsr = new DicomStreamReader(stream); DcmFileMetaInfo metainfo = new DcmFileMetaInfo(); dsr.Dataset = metainfo; dsr.Read(DcmFileMetaInfo.StopTag, DicomReadOptions.Default | DicomReadOptions.FileMetaInfoOnly); return metainfo; }