public List <T> ReadValue(DicomStreamReader reader) { var items = new List <T>(); reader.ReadSequenceItems(_serializer.Deserialize, items.Add); return(items); }
/// <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); } }
internal void EnsureSingleValue(DicomStreamReader reader, int unitSize) { if (reader.ValueLength != unitSize) { throw new IOException($"Invalid single value length {reader.ValueLength} for VR {Name}"); } }
public T ReadSingleValue(DicomStreamReader reader) { EnsureSingleValue <T>(reader); var result = reader.DataReader.Read <T>(); reader.EndReadValue(); return(result); }
public DicomTag ReadSingleValue(DicomStreamReader reader) { EnsureSingleValue(reader, 4); var tag = DicomTag.ReadFrom(reader.DataReader); reader.EndReadValue(); return(tag); }
internal uint GetDefinedValueLength(DicomStreamReader reader) { if (reader.ValueLength == UndefinedLength) { throw new IOException($"Undefined value length not valid for VR {Name}"); } return(reader.ValueLength); }
protected T[] ReadArray(DicomStreamReader reader) { var count = GetValueCount <T>(reader); var array = new T[count]; reader.DataReader.Read <T>(array); reader.EndReadValue(); return(array); }
protected string ReadEntireValue(DicomStreamReader reader) { var valueLength = GetDefinedValueLength(reader); var bytes = reader.Input.ReadBytes(valueLength); reader.EndReadValue(); var encoding = reader.EncodedStringDecoder ?? StringDecoder.Default; return(encoding.Decode(this, TrimEnd(bytes))); }
internal T[] ReadAndConvertValues <T>(DicomStreamReader reader, Func <string, NumberFormatInfo, T> convert) { var stringValues = ReadValues(reader); var values = new T[stringValues.Length]; for (var i = 0; i < values.Length; i++) { values[i] = convert(stringValues[i], NumberFormatInfo.InvariantInfo); } return(values); }
public virtual T[] ReadValue(DicomStreamReader reader) { if (reader.ValueLength != UndefinedLength) { return(ReadArray(reader)); } else { throw new NotSupportedException($"Multi-chunk value reading not implemented by {this}"); } }
internal T2[] ReadAndConvertValues <T2>(DicomStreamReader reader, Func <T, T2> convert) { var values = ReadValues(reader); var convertedValues = new T2[values.Length]; for (var i = 0; i < values.Length; i++) { convertedValues[i] = convert(values[i]); } return(convertedValues); }
public DicomTag[] ReadValues(DicomStreamReader reader) { var count = GetValueCount(reader, 4); var array = new DicomTag[count]; for (var i = 0; i < count; i++) { array[i] = DicomTag.ReadFrom(reader.DataReader); } reader.EndReadValue(); return(array); }
internal int GetValueCount(DicomStreamReader reader, int unitSize) { Debug.Assert(unitSize > 1); // Prevent count overflow and/or bytes being read using this interface var valueLength = GetDefinedValueLength(reader); if ((valueLength % unitSize) != 0) { throw new IOException($"Invalid value length {valueLength} for VR {Name}"); } return(checked ((int)(valueLength / unitSize))); }
/// <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); } }
internal string ReadEntireValue(DicomStreamReader reader) { var valueLength = GetDefinedValueLength(reader); var bytes = reader.Input.ReadBytes(valueLength); var significantBytes = StripNonSignificantBytes(bytes); var value = Encoding.ASCII.GetString(significantBytes); if (reader.CurrentTag == DicomTag.SpecificCharacterSet) { reader.ApplySpecfificCharacterSet(value.Split('\\')); } reader.EndReadValue(); return(value); }
/// <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 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); } }
public override byte[] ReadValue(DicomStreamReader reader) { if (reader.ValueLength < uint.MaxValue) { var result = reader.Input.ReadBytes(reader.ValueLength); reader.EndReadValue(); return(result); } else { var chunks = new List <byte[]>(); while (reader.TryReadItemTagOfSequenceWithUndefinedLength()) { if (reader.ValueLength < uint.MaxValue) { var chunk = reader.Input.ReadBytes(reader.ValueLength); reader.EndReadValue(); chunks.Add(chunk); } else { throw new NotSupportedException("Cannot read chunks with undefined length"); } } var totalLength = chunks.Sum(chunk => chunk.LongLength); var byteArray = new byte[totalLength]; Span <byte> copyWindow = byteArray; foreach (var chunk in chunks) { chunk.CopyTo(copyWindow); copyWindow = copyWindow.Slice(chunk.Length); } return(byteArray); } }
static void Test(string relativePath) { try { using (var stream = OpenRead(relativePath)) { var input = new BufferedStreamReader(stream); if (DicomFileFormat.TryReadHeader(input, out DicomFileMetaInformation fileMetaInformation)) { Console.WriteLine($"SOPClassUID = {fileMetaInformation.MediaStorageSOPClassUID}"); Console.WriteLine($"TransferSyntax = {(DicomUID)fileMetaInformation.TransferSyntaxUID}"); Console.WriteLine(JsonSerializer.Serialize(fileMetaInformation, new JsonSerializerOptions { WriteIndented = true })); var xmlOutputPath = $"{stream.Name}.xml"; Console.WriteLine($"Writing dataset to {xmlOutputPath}"); var dataSet = new XElement("DicomDataSet"); dataSet.SetAttributeValue("Source", stream.Name); var dataSetReader = DicomStreamReader.Create(input, fileMetaInformation.TransferSyntaxUID); dataSetReader.ReadDataSet(dataSet, new DicomToXmlConverter()); dataSet.Save(xmlOutputPath); } else { Console.WriteLine($"Skipped"); } } } catch (Exception error) { Console.WriteLine($"ERROR: {error.Message}"); } }
/// <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); } } }
internal override object GetValue(DicomStreamReader reader) { throw new NotSupportedException(); }
internal override object GetValue(DicomStreamReader reader) => GetValue <DicomTag>(this, reader);
public T[] ReadValues(DicomStreamReader reader) => ReadArray(reader);
internal T2 ReadAndConvertSingleValue <T2>(DicomStreamReader reader, Func <T, T2> convert) { var value = ReadSingleValue(reader); return(convert(value)); }
byte IMultiValue <byte> .ReadSingleValue(DicomStreamReader reader) => ReadAndConvertSingleValue(reader, byte.Parse);
internal override object GetValue(DicomStreamReader reader) => ReadValue(reader);
public Array ReadValue(DicomStreamReader reader) { return((reader.ExplicitVR == DicomVR.OB) ? DicomVR.OB.ReadValue(reader) : DicomVR.OW.ReadValue(reader)); }
T[] IMultiValue <T> .ReadValues(DicomStreamReader reader) => ReadAndConvertValues(reader, Convert);
byte[] IMultiValue <byte> .ReadValues(DicomStreamReader reader) => ReadAndConvertValues(reader, byte.Parse);
T IMultiValue <T> .ReadSingleValue(DicomStreamReader reader) => ReadAndConvertSingleValue(reader, Convert);