public DicomDictionaryReader(DicomDictionary dict, DicomDictionaryFormat format, Stream stream)
 {
     _dict   = dict;
     _format = format;
     _stream = stream;
 }
        private void ReadDictionaryXML()
        {
            DicomDictionary dict = _dict;

            var xdoc = XDocument.Load(_stream);

            IEnumerable <XElement> xdicts;

            if (xdoc.Root.Name == "dictionaries")
            {
                xdicts = xdoc.Root.Elements("dictionary");
            }
            else
            {
                XElement xdict = xdoc.Element("dictionary");
                if (xdict == null)
                {
                    throw new DicomDataException("Expected <dictionary> root node in DICOM dictionary.");
                }

                var dicts = new List <XElement>();
                dicts.Add(xdict);
                xdicts = dicts;
            }

            foreach (var xdict in xdicts)
            {
                XAttribute creator = xdict.Attribute("creator");
                if (creator != null && !string.IsNullOrEmpty(creator.Value))
                {
                    dict = _dict[_dict.GetPrivateCreator(creator.Value)];
                }
                else
                {
                    dict = _dict;
                }

                foreach (XElement xentry in xdict.Elements("tag"))
                {
                    string name = xentry.Value ?? "Unknown";

                    string keyword = string.Empty;
                    if (xentry.Attribute("keyword") != null)
                    {
                        keyword = xentry.Attribute("keyword").Value;
                    }

                    var        vrs = new List <DicomVR>();
                    XAttribute xvr = xentry.Attribute("vr");
                    if (xvr != null && !string.IsNullOrEmpty(xvr.Value))
                    {
                        string[] vra = xvr.Value.Split('_', '/', '\\', ',', '|');
                        foreach (string vr in vra)
                        {
                            vrs.Add(DicomVR.Parse(vr));
                        }
                    }
                    else
                    {
                        vrs.Add(DicomVR.NONE);
                    }

                    var vm = DicomVM.Parse(xentry.Attribute("vm").Value);

                    bool       retired  = false;
                    XAttribute xretired = xentry.Attribute("retired");
                    if (xretired != null && !string.IsNullOrEmpty(xretired.Value) && bool.Parse(xretired.Value))
                    {
                        retired = true;
                    }

                    string group   = xentry.Attribute("group").Value;
                    string element = xentry.Attribute("element").Value;
                    if (group.ToLower().Contains("x") || element.ToLower().Contains("x"))
                    {
                        var tag = DicomMaskedTag.Parse(group, element);
                        tag.Tag.PrivateCreator = dict.PrivateCreator;
                        dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray()));
                    }
                    else
                    {
                        var tag = DicomTag.Parse(group + "," + element);
                        tag.PrivateCreator = dict.PrivateCreator;
                        dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray()));
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Ensures the default DICOM dictionaries are loaded
        /// Safe to call multiple times but will throw an exception if inconsistent values for loadPrivateDictionary are provided over multiple calls
        /// </summary>
        /// <param name="loadPrivateDictionary">Leave null (default value) if unconcerned.  Set true to search for resource streams named "Dicom.Dictionaries.Private Dictionary.xml.gz" in referenced assemblies</param>
        /// <returns></returns>
        public static DicomDictionary EnsureDefaultDictionariesLoaded(bool?loadPrivateDictionary = null)
        {
            // short-circuit if already initialised (#151).
            if (_default != null)
            {
                if (loadPrivateDictionary.HasValue && _defaultIncludesPrivate != loadPrivateDictionary.Value)
                {
                    throw new DicomDataException("Default DICOM dictionary already loaded " +
                                                 (_defaultIncludesPrivate ? "with" : "without") +
                                                 "private dictionary and the current request to ensure the default dictionary is loaded requests that private dictionary " +
                                                 (loadPrivateDictionary.Value ? "is" : "is not") + " loaded");
                }
                return(_default);
            }

            lock (_lock)
            {
                if (_default == null)
                {
                    var dict = new DicomDictionary
                    {
                        new DicomDictionaryEntry(
                            DicomMaskedTag.Parse("xxxx", "0000"),
                            "Group Length",
                            "GroupLength",
                            DicomVM.VM_1,
                            false,
                            DicomVR.UL)
                    };
                    try
                    {
                        var assembly = typeof(DicomDictionary).GetTypeInfo().Assembly;
                        using var stream = assembly.GetManifestResourceStream("FellowOakDicom.Dictionaries.DICOMDictionary.xml.gz");
                        var gzip   = new GZipStream(stream, CompressionMode.Decompress);
                        var reader = new DicomDictionaryReader(dict, DicomDictionaryFormat.XML, gzip);
                        reader.Process();
                    }
                    catch (Exception e)
                    {
                        throw new DicomDataException(
                                  "Unable to load DICOM dictionary from resources.\n\n" + e.Message,
                                  e);
                    }
                    if (loadPrivateDictionary.GetValueOrDefault(true))
                    {
                        try
                        {
                            var assembly = typeof(DicomDictionary).GetTypeInfo().Assembly;
                            using var stream = assembly.GetManifestResourceStream("FellowOakDicom.Dictionaries.PrivateDictionary.xml.gz");
                            var gzip   = new GZipStream(stream, CompressionMode.Decompress);
                            var reader = new DicomDictionaryReader(dict, DicomDictionaryFormat.XML, gzip);
                            reader.Process();
                        }
                        catch (Exception e)
                        {
                            throw new DicomDataException(
                                      "Unable to load private dictionary from resources.\n\n" + e.Message,
                                      e);
                        }
                    }

                    _defaultIncludesPrivate = loadPrivateDictionary.GetValueOrDefault(true);
                    _default = dict;
                }
                else
                {
                    //ensure the race wasn't for two different "load private dictionary" states
                    if (loadPrivateDictionary.HasValue && _defaultIncludesPrivate != loadPrivateDictionary)
                    {
                        throw new DicomDataException("Default DICOM dictionary already loaded " +
                                                     (_defaultIncludesPrivate ? "with" : "without") +
                                                     "private dictionary and the current request to ensure the default dictionary is loaded requests that private dictionary " +
                                                     (loadPrivateDictionary.Value ? "is" : "is not") + " loaded");
                    }
                    return(_default);
                }

                //race is complete
                return(_default);
            }
        }