/// <summary> /// Asynchronously read a DICOM file from stream. /// </summary> /// <param name="stream">Stream to read.</param> /// <param name="fallbackEncoding">Encoding to use if encoding cannot be obtained from DICOM file.</param> /// <param name="stop">Stop criterion in dataset.</param> /// <param name="readOption">The option how to deal with large DICOM tags like pixel data.</param> /// <param name="largeObjectSize">Custom limit of what are large values and what are not. If 0 is passend, then the default of 64k is used.</param> /// <returns>Awaitable <see cref="DicomFile"/> instance.</returns> public static async Task <DicomFile> OpenAsync(Stream stream, Encoding fallbackEncoding, Func <ParseState, bool> stop = null, FileReadOption readOption = FileReadOption.Default, int largeObjectSize = 0) { if (fallbackEncoding == null) { throw new ArgumentNullException(nameof(fallbackEncoding)); } var df = new DicomFile(); try { var source = StreamByteSourceFactory.Create(stream, readOption, largeObjectSize); using var unvalidated = new UnvalidatedScope(df.Dataset); var reader = new DicomFileReader(); var result = await reader.ReadAsync( source, new DicomDatasetReaderObserver(df.FileMetaInfo), new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding), stop).ConfigureAwait(false); HandleOpenError(df, result); df.IsPartial = result == DicomReaderResult.Stopped || result == DicomReaderResult.Suspended; df.Format = reader.FileFormat; df.Dataset.InternalTransferSyntax = reader.Syntax; return(df); } catch (Exception e) { throw new DicomFileException(df, e.Message, e); } }
/// <summary> /// Reads the specified file and returns a DicomFile object. Note that the values for large /// DICOM elements (e.g. PixelData) are read in "on demand" to conserve memory. Large DICOM elements /// are determined by their size in bytes - see the default value for this in the FileByteSource._largeObjectSize /// </summary> /// <param name="file">The file reference of the DICOM file</param> /// <param name="fallbackEncoding">Encoding to apply when attribute Specific Character Set is not available.</param> /// <param name="readOption">Option how to deal with large values, if they should be loaded directly into memory or lazy loaded on demand</param> /// <param name="largeObjectSize">Custom limit of what are large values and what are not. If 0 is passend, then the default of 64k is used.</param> /// <returns>DicomFile instance</returns> internal static DicomFile Open(IFileReference file, Encoding fallbackEncoding, FileReadOption readOption = FileReadOption.Default, int largeObjectSize = 0) { if (fallbackEncoding == null) { throw new ArgumentNullException(nameof(fallbackEncoding)); } var df = new DicomFile(); try { df.File = file; using var source = new FileByteSource(file, readOption, largeObjectSize); using var unvalidated = new UnvalidatedScope(df.Dataset); var reader = new DicomFileReader(); var result = reader.Read( source, new DicomDatasetReaderObserver(df.FileMetaInfo), new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding)); HandleOpenError(df, result); df.IsPartial = result == DicomReaderResult.Stopped || result == DicomReaderResult.Suspended; df.Format = reader.FileFormat; df.Dataset.InternalTransferSyntax = reader.Syntax; return(df); } catch (Exception e) { throw new DicomFileException(df, e.Message, e); } }
/// <summary> /// Initializes DICOM C-Store request to be sent to SCP. /// </summary> /// <param name="file">DICOM file to be sent</param> /// <param name="priority">Priority of request</param> public DicomCStoreRequest(DicomFile file, DicomPriority priority = DicomPriority.Medium) : base(DicomCommandField.CStoreRequest, file.Dataset.GetSingleValue <DicomUID>(DicomTag.SOPClassUID), priority) { File = file; Dataset = file.Dataset; // for potentially invalid UID values, we have to disable validation using var unvalidated = new UnvalidatedScope(Command); SOPInstanceUID = File.Dataset.GetSingleValue <DicomUID>(DicomTag.SOPInstanceUID); }
/// <inheritdoc /> public DicomFile Transcode(DicomFile file) { var f = new DicomFile(); using var scope = new UnvalidatedScope(f.Dataset); f.FileMetaInfo.Add(file.FileMetaInfo); f.FileMetaInfo.TransferSyntax = OutputSyntax; f.Dataset.InternalTransferSyntax = OutputSyntax; f.Dataset.Add(Transcode(file.Dataset)); return(f); }
/// <summary> /// Asynchronously reads the specified filename and returns a DicomFile object. Note that the values for large /// DICOM elements (e.g. PixelData) are read in "on demand" to conserve memory. Large DICOM elements /// are determined by their size in bytes - see the default value for this in the FileByteSource._largeObjectSize /// </summary> /// <param name="fileName">The filename of the DICOM file</param> /// <param name="fallbackEncoding">Encoding to apply when attribute Specific Character Set is not available.</param> /// <param name="stop">Stop criterion in dataset.</param> /// <returns>Awaitable <see cref="DicomFile"/> instance.</returns> public static async Task <DicomFile> OpenAsync(string fileName, Encoding fallbackEncoding, Func <ParseState, bool> stop = null, FileReadOption readOption = FileReadOption.Default) { if (fallbackEncoding == null) { throw new ArgumentNullException(nameof(fallbackEncoding)); } var df = new DicomFile(); try { df.File = IOManager.CreateFileReference(fileName); using (var unvalidated = new UnvalidatedScope(df.Dataset)) using (var source = new FileByteSource(df.File, readOption)) { var reader = new DicomFileReader(); var result = await reader.ReadAsync( source, new DicomDatasetReaderObserver(df.FileMetaInfo), new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding), stop).ConfigureAwait(false); if (result == DicomReaderResult.Processing) { throw new DicomFileException(df, $"Invalid read return 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; return(df); } } catch (Exception e) { throw new DicomFileException(df, e.Message, e); } }
/// <summary> /// Reads the specified file and returns a DicomFile object. Note that the values for large /// DICOM elements (e.g. PixelData) are read in "on demand" to conserve memory. Large DICOM elements /// are determined by their size in bytes - see the default value for this in the FileByteSource._largeObjectSize /// </summary> /// <param name="file">The file reference of the DICOM file</param> /// <param name="fallbackEncoding">Encoding to apply when attribute Specific Character Set is not available.</param> /// <returns>DicomFile instance</returns> internal static DicomFile Open(IFileReference file, Encoding fallbackEncoding, FileReadOption readOption = FileReadOption.Default) { if (fallbackEncoding == null) { throw new ArgumentNullException(nameof(fallbackEncoding)); } DicomFile df = new DicomFile(); try { df.File = file; using (var source = new FileByteSource(file, readOption)) using (var unvalidated = new UnvalidatedScope(df.Dataset)) { DicomFileReader reader = new DicomFileReader(); var result = reader.Read( source, new DicomDatasetReaderObserver(df.FileMetaInfo), new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding)); if (result == DicomReaderResult.Processing) { throw new DicomFileException(df, $"Invalid read return 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; return(df); } } catch (Exception e) { throw new DicomFileException(df, e.Message, e); } }
/// <summary> /// Read a DICOM file from stream. /// </summary> /// <param name="stream">Stream to read.</param> /// <param name="fallbackEncoding">Encoding to use if encoding cannot be obtained from DICOM file.</param> /// <param name="stop">Stop criterion in dataset.</param> /// <param name="readOption">The option how to deal with large DICOM tag like pixel data</param> /// <returns>Read <see cref="DicomFile"/>.</returns> public static DicomFile Open(Stream stream, Encoding fallbackEncoding, Func <ParseState, bool> stop = null, FileReadOption readOption = FileReadOption.Default) { if (fallbackEncoding == null) { throw new ArgumentNullException(nameof(fallbackEncoding)); } var df = new DicomFile(); try { var source = new StreamByteSource(stream, readOption); using (var unvalidated = new UnvalidatedScope(df.Dataset)) { var reader = new DicomFileReader(); var result = reader.Read( source, new DicomDatasetReaderObserver(df.FileMetaInfo), new DicomDatasetReaderObserver(df.Dataset, fallbackEncoding), stop); if (result == DicomReaderResult.Processing) { throw new DicomFileException(df, $"Invalid read return 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; return(df); } } catch (Exception e) { throw new DicomFileException(df, e.Message, e); } }