예제 #1
0
        public List <T> ReadValue(DicomStreamReader reader)
        {
            var items = new List <T>();

            reader.ReadSequenceItems(_serializer.Deserialize, items.Add);
            return(items);
        }
예제 #2
0
        /// <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}");
     }
 }
예제 #4
0
        public T ReadSingleValue(DicomStreamReader reader)
        {
            EnsureSingleValue <T>(reader);
            var result = reader.DataReader.Read <T>();

            reader.EndReadValue();
            return(result);
        }
예제 #5
0
        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);
        }
예제 #8
0
        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);
        }
예제 #10
0
 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}");
     }
 }
예제 #11
0
        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);
        }
예제 #12
0
        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)));
        }
예제 #14
0
 /// <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);
     }
 }
예제 #15
0
        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);
        }
예제 #16
0
        /// <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);
        }
예제 #17
0
 /// <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);
     }
 }
예제 #18
0
        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);
            }
        }
예제 #19
0
        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}");
            }
        }
예제 #20
0
 /// <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);
         }
     }
 }
예제 #21
0
 internal override object GetValue(DicomStreamReader reader)
 {
     throw new NotSupportedException();
 }
예제 #22
0
 internal override object GetValue(DicomStreamReader reader) => GetValue <DicomTag>(this, reader);
예제 #23
0
 public T[] ReadValues(DicomStreamReader reader) => ReadArray(reader);
예제 #24
0
        internal T2 ReadAndConvertSingleValue <T2>(DicomStreamReader reader, Func <T, T2> convert)
        {
            var value = ReadSingleValue(reader);

            return(convert(value));
        }
예제 #25
0
 byte IMultiValue <byte> .ReadSingleValue(DicomStreamReader reader) => ReadAndConvertSingleValue(reader, byte.Parse);
예제 #26
0
 internal override object GetValue(DicomStreamReader reader) => ReadValue(reader);
예제 #27
0
 public Array ReadValue(DicomStreamReader reader)
 {
     return((reader.ExplicitVR == DicomVR.OB) ? DicomVR.OB.ReadValue(reader) : DicomVR.OW.ReadValue(reader));
 }
예제 #28
0
 T[] IMultiValue <T> .ReadValues(DicomStreamReader reader) => ReadAndConvertValues(reader, Convert);
예제 #29
0
 byte[] IMultiValue <byte> .ReadValues(DicomStreamReader reader) => ReadAndConvertValues(reader, byte.Parse);
예제 #30
0
 T IMultiValue <T> .ReadSingleValue(DicomStreamReader reader) => ReadAndConvertSingleValue(reader, Convert);