예제 #1
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);
            }
        }
예제 #2
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);
     }
 }
예제 #3
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);
        }
예제 #4
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);
     }
 }
예제 #5
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);
         }
     }
 }
예제 #6
0
        private void LoadCore(Stream stream, DicomStreamOpener streamOpener, DicomTag stopTag, DicomReadOptions options)
        {
            // TODO CR (24 Jan 2014): DICOM stream read only uses tag value, so the real implementation should be the uint overload!
            if (stopTag == null)
            {
                stopTag = new DicomTag(0xFFFFFFFF, "Bogus Tag", "BogusTag", DicomVr.NONE, false, 1, 1, false);
            }

            DicomStreamReader dsr;

            var iStream = stream ?? streamOpener.Open();

            if (iStream.CanSeek)
            {
                iStream.Seek(128, SeekOrigin.Begin);
                if (!FileHasPart10Header(iStream))
                {
                    if (!Flags.IsSet(options, DicomReadOptions.ReadNonPart10Files))
                    {
                        throw new DicomException(String.Format("File is not part 10 format file: {0}", Filename));
                    }

                    iStream.Seek(0, SeekOrigin.Begin);
                    dsr = new DicomStreamReader(iStream)
                    {
                        StreamOpener   = streamOpener,
                        TransferSyntax = TransferSyntax.ImplicitVrLittleEndian,
                        Dataset        = DataSet
                    };
                    DicomReadStatus stat = dsr.Read(stopTag, options);
                    if (stat != DicomReadStatus.Success)
                    {
                        Platform.Log(LogLevel.Error, "Unexpected error when reading file: {0}", Filename);
                        throw new DicomException("Unexpected read error with file: " + Filename);
                    }

                    TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;
                    if (DataSet.Contains(DicomTags.SopClassUid))
                    {
                        MediaStorageSopClassUid = DataSet[DicomTags.SopClassUid].ToString();
                    }
                    if (DataSet.Contains(DicomTags.SopInstanceUid))
                    {
                        MediaStorageSopInstanceUid = DataSet[DicomTags.SopInstanceUid].ToString();
                    }

                    Loaded = true;
                    return;
                }
            }
            else
            {
                // TODO CR (04 Apr 2014): this code here is almost identical to the seekable stream above, except that we use the 4CC wrapper
                // we can combine these two when we trust that the wrapper works in all cases
                iStream = FourCcReadStream.Create(iStream);

                // Read the 128 byte header first, then check for DICM
                iStream.SeekEx(128, SeekOrigin.Begin);

                if (!FileHasPart10Header(iStream))
                {
                    if (!Flags.IsSet(options, DicomReadOptions.ReadNonPart10Files))
                    {
                        throw new DicomException(String.Format("File is not part 10 format file: {0}", Filename));
                    }

                    iStream.Seek(0, SeekOrigin.Begin);
                    dsr = new DicomStreamReader(iStream)
                    {
                        StreamOpener   = streamOpener,
                        TransferSyntax = TransferSyntax.ImplicitVrLittleEndian,
                        Dataset        = DataSet
                    };
                    DicomReadStatus stat = dsr.Read(stopTag, options);
                    if (stat != DicomReadStatus.Success)
                    {
                        Platform.Log(LogLevel.Error, "Unexpected error when reading file: {0}", Filename);
                        throw new DicomException("Unexpected read error with file: " + Filename);
                    }

                    TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;
                    if (DataSet.Contains(DicomTags.SopClassUid))
                    {
                        MediaStorageSopClassUid = DataSet[DicomTags.SopClassUid].ToString();
                    }
                    if (DataSet.Contains(DicomTags.SopInstanceUid))
                    {
                        MediaStorageSopInstanceUid = DataSet[DicomTags.SopInstanceUid].ToString();
                    }

                    Loaded = true;
                    return;
                }
            }

            dsr = new DicomStreamReader(iStream)
            {
                TransferSyntax = TransferSyntax.ExplicitVrLittleEndian,
                StreamOpener   = streamOpener,
                Dataset        = MetaInfo
            };

            DicomReadStatus readStat =
                dsr.Read(new DicomTag(0x0002FFFF, "Bogus Tag", "BogusTag", DicomVr.UNvr, false, 1, 1, false), options);

            if (readStat != DicomReadStatus.Success)
            {
                Platform.Log(LogLevel.Error, "Unexpected error when reading file Meta info for file: {0}", Filename);
                throw new DicomException("Unexpected failure reading file Meta info for file: " + Filename);
            }

            MetaInfoFileLength = dsr.EndGroupTwo + 128 + 4;

            dsr.Dataset        = DataSet;
            dsr.TransferSyntax = TransferSyntax;
            readStat           = dsr.Read(stopTag, options);
            if (readStat != DicomReadStatus.Success)
            {
                Platform.Log(LogLevel.Error, "Unexpected error ({0}) when reading file at offset {2}: {1}", readStat, Filename, dsr.BytesRead);
                throw new DicomException("Unexpected failure (" + readStat + ") reading file at offset " + dsr.BytesRead + ": " + Filename);
            }

            Loaded = true;
        }
예제 #7
0
        /// <summary>
        /// Load a DICOM file from an input stream.
        /// </summary>
        /// <remarks>
        /// Note:  If the file does not contain DICM encoded in it, and
        /// <see cref="Stream.CanSeek"/> is true for <paramref name="iStream"/>,
        /// the routine will assume the file is not a Part 10 format file, and is
        /// instead encoded as just a DataSet with the transfer syntax set to
        /// Implicit VR Little Endian.
        /// </remarks>
        /// <param name="iStream">The input stream to read from.</param>
        /// <param name="stopTag">The dicom tag to stop the reading at.</param>
        /// <param name="options">The dicom read options to consider.</param>
        public void Load(Stream iStream, DicomTag stopTag, DicomReadOptions options)
        {
            if (iStream == null)
            {
                throw new ArgumentNullException("iStream");
            }

            if (stopTag == null)
            {
                stopTag = new DicomTag(0xFFFFFFFF, "Bogus Tag", "BogusTag", DicomVr.NONE, false, 1, 1, false);
            }

            DicomStreamReader dsr;

            if (iStream.CanSeek)
            {
                iStream.Seek(128, SeekOrigin.Begin);
                if (!FileHasPart10Header(iStream))
                {
                    if (!Flags.IsSet(options, DicomReadOptions.ReadNonPart10Files))
                    {
                        throw new DicomException(String.Format("File is not part 10 format file: {0}", Filename));
                    }

                    iStream.Seek(0, SeekOrigin.Begin);
                    dsr = new DicomStreamReader(iStream)
                    {
                        Filename       = Filename,
                        TransferSyntax = TransferSyntax.ImplicitVrLittleEndian,
                        Dataset        = DataSet
                    };
                    DicomReadStatus stat = dsr.Read(stopTag, options);
                    if (stat != DicomReadStatus.Success)
                    {
                        Platform.Log(LogLevel.Error, "Unexpected error when reading file: {0}", Filename);
                        throw new DicomException("Unexpected read error with file: " + Filename);
                    }

                    TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;
                    if (DataSet.Contains(DicomTags.SopClassUid))
                    {
                        MediaStorageSopClassUid = DataSet[DicomTags.SopClassUid].ToString();
                    }
                    if (DataSet.Contains(DicomTags.SopInstanceUid))
                    {
                        MediaStorageSopInstanceUid = DataSet[DicomTags.SopInstanceUid].ToString();
                    }
                    return;
                }
            }
            else
            {
                // Read the 128 byte header first, then check for DICM
                iStream.Read(new byte[128], 0, 128);

                if (!FileHasPart10Header(iStream))
                {
                    Platform.Log(LogLevel.Error, "Reading DICOM file from stream, file does not have part 10 format header.");
                    throw new DicomException("File being read from stream is not a part 10 format file");
                }
            }

            dsr = new DicomStreamReader(iStream)
            {
                TransferSyntax = TransferSyntax.ExplicitVrLittleEndian,
                Filename       = Filename,
                Dataset        = MetaInfo
            };

            DicomReadStatus readStat =
                dsr.Read(new DicomTag(0x0002FFFF, "Bogus Tag", "BogusTag", DicomVr.UNvr, false, 1, 1, false), options);

            if (readStat != DicomReadStatus.Success)
            {
                Platform.Log(LogLevel.Error, "Unexpected error when reading file Meta info for file: {0}", Filename);
                throw new DicomException("Unexpected failure reading file Meta info for file: " + Filename);
            }
            dsr.Dataset        = DataSet;
            dsr.TransferSyntax = TransferSyntax;
            readStat           = dsr.Read(stopTag, options);
            if (readStat != DicomReadStatus.Success)
            {
                Platform.Log(LogLevel.Error, "Unexpected error ({0}) when reading file at offset {2}: {1}", readStat, Filename, dsr.BytesRead);
                throw new DicomException("Unexpected failure (" + readStat + ") reading file at offset " + dsr.BytesRead + ": " + Filename);
            }
        }