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);
		}
示例#2
0
        public void Load_UncompressedFile_LoadedTagFound()
        {
            var dict = new DicomDictionary();
            dict.Load(@".\Test Data\minimumdict.xml", DicomDictionaryFormat.XML);

            var expected = DicomVR.CS;
            var actual = dict[DicomTag.FileSetID].ValueRepresentations.Single();
            Assert.Equal(expected, actual);
        }
    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]);
    }
		public static string Generate(string vnamespace, string vclass, string vmethod, DicomDictionary dict) {
			StringBuilder output = new StringBuilder();

			output.AppendFormat("namespace {0} {{", vnamespace).AppendLine();
			output.AppendFormat("\tpublic partial class {0} {{", vclass).AppendLine();
			output.AppendFormat("\t\tpublic static void {0}(DicomDictionary dict) {{", vmethod).AppendLine();

			foreach (DicomDictionaryEntry entry in dict.OrderBy(entry => (uint)entry.Tag))
			{
				string vm = null;
				switch (entry.ValueMultiplicity.ToString()) {
					case "1": vm = "DicomVM.VM_1"; break;
					case "1-2": vm = "DicomVM.VM_1_2"; break;
					case "1-3": vm = "DicomVM.VM_1_3"; break;
					case "1-8": vm = "DicomVM.VM_1_8"; break;
					case "1-32": vm = "DicomVM.VM_1_32"; break;
					case "1-99": vm = "DicomVM.VM_1_99"; break;
					case "1-n": vm = "DicomVM.VM_1_n"; break;
					case "2": vm = "DicomVM.VM_2"; break;
					case "2-n": vm = "DicomVM.VM_2_n"; break;
					case "2-2n": vm = "DicomVM.VM_2_2n"; break;
					case "3": vm = "DicomVM.VM_3"; break;
					case "3-n": vm = "DicomVM.VM_3_n"; break;
					case "3-3n": vm = "DicomVM.VM_3_3n"; break;
					case "4": vm = "DicomVM.VM_4"; break;
					case "6": vm = "DicomVM.VM_6"; break;
					case "16": vm = "DicomVM.VM_16"; break;
					default:
						vm = String.Format("DicomVM.Parse(\"{0}\")", entry.ValueMultiplicity);
						break;
				}

				if (entry.MaskTag == null || entry.MaskTag.Mask == 0xffffffff) {
					output.AppendFormat("\t\t\tdict._entries.Add(DicomTag.{3}{6}, new DicomDictionaryEntry(DicomTag.{3}{6}, \"{2}\", \"{3}\", {4}, {5}",
						entry.Tag.ToString("g", null), entry.Tag.ToString("e", null), entry.Name, entry.Keyword, vm, entry.IsRetired ? "true" : "false", entry.IsRetired ? "RETIRED" : "");
				} else {
					output.AppendFormat("\t\t\tdict.Add(new DicomDictionaryEntry(DicomMaskedTag.Parse(\"{0}\",\"{1}\"), \"{2}\", \"{3}\", {4}, {5}",
						entry.MaskTag.ToString("g", null), entry.MaskTag.ToString("e", null), entry.Name, entry.Keyword, vm, entry.IsRetired ? "true" : "false");
				}

				foreach (DicomVR vr in entry.ValueRepresentations)
					output.AppendFormat(", DicomVR.{0}", vr.Code);

				output.AppendLine("));");
			}

			output.AppendLine("\t\t}");
			output.AppendLine("\t}");
			output.AppendLine("}");

			return output.ToString();
		}
    public void AddPrivateTagAndReadOut()
    {
      var dict = new DicomDictionary
      {
        new DicomDictionaryEntry(new DicomTag(0x0011, 0x0010), "Private Creator", "PrivateCreator", DicomVM.VM_1, false, DicomVR.LO)
      };

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

      var dictEntry = new DicomDictionaryEntry(new DicomTag(0x0011, 0x1010), "TestPrivTagName", "TestPrivTagKeyword", DicomVM.VM_1, false, DicomVR.DT);

      privDict.Add(dictEntry);

      Assert.True(dictEntry.Equals(dict[dictEntry.Tag.PrivateCreator][dictEntry.Tag]));
    }
示例#6
0
 private static void LoadInternalDictionaries()
 {
     if (_default == null)
     {
         _default = new DicomDictionary();
         _default.Add(new DicomDictionaryEntry(DicomMaskedTag.Parse("xxxx", "0000"), "Group Length", "GroupLength", DicomVM.VM_1, false, DicomVR.UL));
         LoadGeneratedDictionary_(_default);
         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);
         }
     }
 }
示例#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
        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]);
        }
示例#9
0
        public DicomDictionaryEntry this[DicomTag tag]
        {
            get
            {
                if (_private != null && tag.PrivateCreator != null)
                {
                    DicomDictionary pvt = null;
                    if (_private.TryGetValue(tag.PrivateCreator, out pvt))
                    {
                        return(pvt[tag]);
                    }
                }

                // special case for private creator tag
                if (tag.IsPrivate && tag.Element != 0x0000 && tag.Element <= 0x00ff)
                {
                    return(PrivateCreatorTag);
                }

                DicomDictionaryEntry entry = null;
                if (_entries.TryGetValue(tag, out entry))
                {
                    return(entry);
                }

                // this is faster than LINQ query
                lock (_maskedLock)
                {
                    foreach (var x in _masked)
                    {
                        if (x.MaskTag.IsMatch(tag))
                        {
                            return(x);
                        }
                    }
                }

                return(UnknownTag);
            }
        }
    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)));
    }
示例#11
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);
            }
        }
示例#12
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()));
                    }
                }
            }
        }
示例#13
0
 public DicomDictionaryReader(DicomDictionary dict, DicomDictionaryFormat format, Stream stream)
 {
     _dict   = dict;
     _format = format;
     _stream = stream;
 }
示例#14
0
 public void Throws_If_Already_Loaded()
 => _testDomain.DoCallBack(() =>
 {
     _ = DicomDictionary.EnsureDefaultDictionariesLoaded(false);
     Assert.Throws <DicomDataException>(() => DicomDictionary.Default = new DicomDictionary());
 });
示例#15
0
 public void Throws_If_EnsureLoaded_Called_Without_And_With_Private()
 => _testDomain.DoCallBack(() =>
 {
     DicomDictionary.EnsureDefaultDictionariesLoaded(false);
     Assert.Throws <DicomDataException>(() => DicomDictionary.EnsureDefaultDictionariesLoaded(true));
 });
示例#16
0
 public DicomDictionaryReader(DicomDictionary dict, DicomDictionaryFormat format, Stream stream)
 {
     _dict = dict;
     _format = format;
     _stream = stream;
 }
示例#17
0
 public void Constructor_NoExplicitLoading_TagsNotFound(DicomTag tag)
 {
     var dict = new DicomDictionary();
     var actual = dict[tag];
     Assert.Equal(DicomDictionary.UnknownTag, actual);
 }