예제 #1
0
 private static void LoadInternalDictionaries()
 {
     lock (_lock) {
         if (_default == null)
         {
             _default = new DicomDictionary();
             _default.Add(new DicomDictionaryEntry(DicomMaskedTag.Parse("xxxx", "0000"), "Group Length", "GroupLength", DicomVM.VM_1, false, DicomVR.UL));
             try {
                 var assembly = Assembly.GetExecutingAssembly();
                 var stream   = assembly.GetManifestResourceStream("Dicom.Dictionaries.DICOM Dictionary.xml.gz");
                 var gzip     = new GZipStream(stream, CompressionMode.Decompress);
                 var reader   = new DicomDictionaryReader(_default, DicomDictionaryFormat.XML, gzip);
                 reader.Process();
             } catch (Exception e) {
                 throw new DicomDataException("Unable to load DICOM dictionary from resources.\n\n" + e.Message, e);
             }
             try {
                 var assembly = Assembly.GetExecutingAssembly();
                 var stream   = assembly.GetManifestResourceStream("Dicom.Dictionaries.Private Dictionary.xml.gz");
                 var gzip     = new GZipStream(stream, CompressionMode.Decompress);
                 var reader   = new DicomDictionaryReader(_default, DicomDictionaryFormat.XML, gzip);
                 reader.Process();
             } catch (Exception e) {
                 throw new DicomDataException("Unable to load private dictionary from resources.\n\n" + e.Message, e);
             }
         }
     }
 }
예제 #2
0
        public void Add_PrivateTag_GetsCorrectVR()
        {
            var privCreatorDictEntry = new DicomDictionaryEntry(
                new DicomTag(0x0011, 0x0010),
                "Private Creator",
                "PrivateCreator",
                DicomVM.VM_1,
                false,
                DicomVR.LO);

            DicomDictionary.Default.Add(privCreatorDictEntry);

            DicomPrivateCreator privateCreator1 = DicomDictionary.Default.GetPrivateCreator("TESTCREATOR1");
            DicomDictionary     privDict1       = DicomDictionary.Default[privateCreator1];

            var dictEntry = new DicomDictionaryEntry(
                DicomMaskedTag.Parse("0011", "xx10"),
                "TestPrivTagName",
                "TestPrivTagKeyword",
                DicomVM.VM_1,
                false,
                DicomVR.CS);

            privDict1.Add(dictEntry);

            var ds = new DicomDataset
            {
                { dictEntry.Tag, "VAL1" }
            };

            Assert.Equal(DicomVR.CS, ds.Get <DicomVR>(ds.GetPrivateTag(dictEntry.Tag)));
        }
		static void Main(string[] args)
		{

			if (args.Length != 1 ||
				!File.Exists(Path.Combine(args[0], "part06.xml")) ||
				!File.Exists(Path.Combine(args[0], "part07.xml")) ||
				!File.Exists(Path.Combine(args[0], "part16.xml")))
			{
				System.Console.WriteLine("Syntax: {0} <xml-base-path>", System.AppDomain.CurrentDomain.FriendlyName);
				System.Console.WriteLine();
				System.Console.WriteLine("e.g: {0} C:\\temp", System.AppDomain.CurrentDomain.FriendlyName);
				System.Console.WriteLine();
				System.Console.WriteLine("Cannot find required XML files (part06.xml, part07.xml and part16.xml). Try downloading them using the attached script \"GetDocBooks.ps1\".");
				System.Environment.Exit(1);
			}

			string basepath = args[0];

			var dd = Part6Parser.ParseDataDictionaries(Path.Combine(basepath, "part06.xml"), Path.Combine(basepath, "part07.xml"));
			var uids = Part6Parser.ParseUIDTables(Path.Combine(basepath, "part06.xml"), Path.Combine(basepath, "part16.xml"));

			var dicomDict = new DicomDictionary();
			foreach (var ddentry in dd)
			{
				dicomDict.Add(ddentry);
			}

			var a = Generators.DicomDictionaryGenerator.Generate("Dicom", "DicomDictionary", "LoadGeneratedDictionary_", dicomDict);
			var b = Generators.DicomTagGenerator.Generate("Dicom", "DicomTag", dicomDict);
			var c = Generators.DicomUIDGenerator.Emit(uids);

			File.WriteAllText("DicomDictionaryGenerated.cs", a, Encoding.UTF8);
			File.WriteAllText("DicomTagGenerated.cs", b, Encoding.UTF8);
			File.WriteAllText("DicomUIDGenerated.cs", c, Encoding.UTF8);
		}
예제 #4
0
    public void AddTagAndReadOut()
    {
      var dict = new DicomDictionary();

      var tag = new DicomTag(0x0010, 0x0020);
      var dictEntry = new DicomDictionaryEntry(tag, "TestTagName", "TestTagKeyword", DicomVM.VM_1, false, DicomVR.DT);

      dict.Add(dictEntry);

      Assert.Equal(dictEntry, dict[tag]);
    }
예제 #5
0
        public void Enumerate_DictionaryEntriesWithPrivateTags_ContainsAllExpectedEntries()
        {
            var dict = new DicomDictionary();

            var tag1       = new DicomTag(0x0010, 0x0020);
            var dictEntry1 = new DicomDictionaryEntry(
                tag1,
                "TestPublicTagName",
                "TestPublicTagKeyword",
                DicomVM.VM_1,
                false,
                DicomVR.DT);
            var privCreatorDictEntry = new DicomDictionaryEntry(
                new DicomTag(0x0011, 0x0010),
                "Private Creator",
                "PrivateCreator",
                DicomVM.VM_1,
                false,
                DicomVR.LO);

            dict.Add(privCreatorDictEntry);

            DicomPrivateCreator privateCreator = dict.GetPrivateCreator("TESTCREATOR");
            DicomDictionary     privDict       = dict[privateCreator];

            var dictEntry2 = new DicomDictionaryEntry(
                DicomMaskedTag.Parse("0011", "xx10"),
                "TestPrivTagName",
                "TestPrivTagKeyword",
                DicomVM.VM_1,
                false,
                DicomVR.DT);

            privDict.Add(dictEntry2);
            dict.Add(dictEntry1);

            Assert.Contains(dictEntry1, dict);
            Assert.Contains(privCreatorDictEntry, dict);
            Assert.Contains(dictEntry2, dict[dictEntry2.Tag.PrivateCreator]);
        }
예제 #6
0
    public void EnumerateBothPublicAndPrivateEntries()
    {
      var dict = new DicomDictionary();

      var tag1 = new DicomTag(0x0010, 0x0020);
      var dictEntry1 = new DicomDictionaryEntry(tag1, "TestPublicTagName", "TestPublicTagKeyword", DicomVM.VM_1, false, DicomVR.DT);
      var privCreatorDictEntry = new DicomDictionaryEntry(new DicomTag(0x0011, 0x0010), "Private Creator", "PrivateCreator", DicomVM.VM_1, false, DicomVR.LO);
      dict.Add(privCreatorDictEntry);

      DicomPrivateCreator privateCreator = dict.GetPrivateCreator("TESTCREATOR");
      DicomDictionary privDict = dict[privateCreator];

      var dictEntry2 = new DicomDictionaryEntry(DicomMaskedTag.Parse("0011", "xx10"), "TestPrivTagName", "TestPrivTagKeyword", DicomVM.VM_1, false, DicomVR.DT);

      privDict.Add(dictEntry2);
      dict.Add(dictEntry1);

      Assert.True(dict.Contains(dictEntry1));
      Assert.True(dict.Contains(privCreatorDictEntry));
      Assert.True(dict[dictEntry2.Tag.PrivateCreator].Contains(dictEntry2));
      Assert.True(dict.PrivateCreators.Any(pc => dict[pc].Contains(dictEntry2)));
    }
예제 #7
0
        public void Add_PrivateTag_ShouldBeAddedWithCorrectVR()
        {
            var privCreatorDictEntry = new DicomDictionaryEntry(new DicomTag(0x0011, 0x0010), "Private Creator", "PrivateCreator", DicomVM.VM_1, false, DicomVR.LO);

            DicomDictionary.Default.Add(privCreatorDictEntry);

            DicomPrivateCreator privateCreator1 = DicomDictionary.Default.GetPrivateCreator("TESTCREATOR1");
            DicomDictionary     privDict1       = DicomDictionary.Default[privateCreator1];

            var dictEntry = new DicomDictionaryEntry(DicomMaskedTag.Parse("0011", "xx10"), "TestPrivTagName", "TestPrivTagKeyword", DicomVM.VM_1, false, DicomVR.CS);

            privDict1.Add(dictEntry);

            var ds = new DicomDataset();

            ds.Add(dictEntry.Tag, "VAL1");
            Assert.Equal(DicomVR.CS, ds.GetDicomItem <DicomItem>(dictEntry.Tag).ValueRepresentation);
        }
예제 #8
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();
                    dict.Add(
                        new DicomDictionaryEntry(
                            DicomMaskedTag.Parse("xxxx", "0000"),
                            "Group Length",
                            "GroupLength",
                            DicomVM.VM_1,
                            false,
                            DicomVR.UL));
                    try
                    {
#if NET35 || HOLOLENS
                        using (
                            var stream =
                                new MemoryStream(
                                    UnityEngine.Resources.Load <UnityEngine.TextAsset>("DICOM Dictionary").bytes))
                        {
                            var reader = new DicomDictionaryReader(dict, DicomDictionaryFormat.XML, stream);
                            reader.Process();
                        }
#else
                        var assembly = typeof(DicomDictionary).GetTypeInfo().Assembly;
                        using (
                            var stream = assembly.GetManifestResourceStream(
                                "Dicom.Dictionaries.DICOMDictionary.xml.gz"))
                        {
                            var gzip   = new GZipStream(stream, CompressionMode.Decompress);
                            var reader = new DicomDictionaryReader(dict, DicomDictionaryFormat.XML, gzip);
                            reader.Process();
                        }
#endif
                    }
                    catch (Exception e)
                    {
                        throw new DicomDataException(
                                  "Unable to load DICOM dictionary from resources.\n\n" + e.Message,
                                  e);
                    }
                    if (loadPrivateDictionary.GetValueOrDefault(true))
                    {
                        try
                        {
#if NET35 || HOLOLENS
                            using (
                                var stream =
                                    new MemoryStream(
                                        UnityEngine.Resources.Load <UnityEngine.TextAsset>("Private Dictionary").bytes))
                            {
                                var reader = new DicomDictionaryReader(dict, DicomDictionaryFormat.XML, stream);
                                reader.Process();
                            }
#else
                            var assembly = typeof(DicomDictionary).GetTypeInfo().Assembly;
                            using (
                                var stream =
                                    assembly.GetManifestResourceStream("Dicom.Dictionaries.PrivateDictionary.xml.gz"))
                            {
                                var gzip   = new GZipStream(stream, CompressionMode.Decompress);
                                var reader = new DicomDictionaryReader(dict, DicomDictionaryFormat.XML, gzip);
                                reader.Process();
                            }
#endif
                        }
                        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);
            }
        }
예제 #9
0
        private void ReadDictionaryXML()
        {
            DicomDictionary dict = _dict;

            XDocument 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.");
                }

                List <XElement> 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;
                    }

                    List <DicomVR> 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);
                    }

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

                    bool       retired  = false;
                    XAttribute xretired = xentry.Attribute("retired");
                    if (xretired != null && !String.IsNullOrEmpty(xretired.Value) && Boolean.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"))
                    {
                        DicomMaskedTag tag = DicomMaskedTag.Parse(group, element);
                        tag.Tag.PrivateCreator = dict.PrivateCreator;
                        dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray()));
                    }
                    else
                    {
                        DicomTag tag = DicomTag.Parse(group + "," + element);
                        tag.PrivateCreator = dict.PrivateCreator;
                        dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray()));
                    }
                }
            }
        }