PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/DICOM/DicomDictionary.cs

https://github.com/petnet/fo-dicom
C# | 162 lines | 139 code | 23 blank | 0 comment | 21 complexity | 68cf5a713ae6c6cc7372f0490552e0c7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace Dicom {
  11. public partial class DicomDictionary : IEnumerable<DicomDictionaryEntry> {
  12. #region Private Members
  13. private readonly static DicomDictionaryEntry UnknownTag = new DicomDictionaryEntry(DicomMaskedTag.Parse("xxxx","xxxx"), "Unknown", "Unknown", DicomVM.VM_1_n, false,
  14. DicomVR.UN, DicomVR.AE, DicomVR.AS, DicomVR.AT, DicomVR.CS, DicomVR.DA, DicomVR.DS, DicomVR.DT, DicomVR.FD, DicomVR.FL, DicomVR.IS, DicomVR.LO, DicomVR.LT, DicomVR.OB,
  15. DicomVR.OF, DicomVR.OW, DicomVR.PN, DicomVR.SH, DicomVR.SL, DicomVR.SQ, DicomVR.SS, DicomVR.ST, DicomVR.TM, DicomVR.UI, DicomVR.UL, DicomVR.US, DicomVR.UT);
  16. private readonly static DicomDictionaryEntry GroupLength = new DicomDictionaryEntry(DicomMaskedTag.Parse("xxxx","0000"), "Group Length", "GroupLength", DicomVM.VM_1, false, DicomVR.UL);
  17. private DicomPrivateCreator _privateCreator;
  18. private IDictionary<string, DicomPrivateCreator> _creators;
  19. private IDictionary<DicomPrivateCreator, DicomDictionary> _private;
  20. private IDictionary<DicomTag, DicomDictionaryEntry> _entries;
  21. private List<DicomDictionaryEntry> _masked;
  22. private bool _sortMasked;
  23. #endregion
  24. #region Constructors
  25. public DicomDictionary() {
  26. _creators = new Dictionary<string, DicomPrivateCreator>();
  27. _private = new Dictionary<DicomPrivateCreator, DicomDictionary>();
  28. _entries = new Dictionary<DicomTag, DicomDictionaryEntry>();
  29. _masked = new List<DicomDictionaryEntry>();
  30. }
  31. private DicomDictionary(DicomPrivateCreator creator) {
  32. _privateCreator = creator;
  33. _entries = new Dictionary<DicomTag, DicomDictionaryEntry>();
  34. _masked = new List<DicomDictionaryEntry>();
  35. }
  36. #endregion
  37. #region Properties
  38. private static object _lock = new object();
  39. private static DicomDictionary _default;
  40. public static DicomDictionary Default {
  41. get {
  42. lock (_lock) {
  43. if (_default == null) {
  44. _default = new DicomDictionary();
  45. try {
  46. var assembly = Assembly.GetCallingAssembly();
  47. var stream = assembly.GetManifestResourceStream("Dicom.Dictionaries.DICOM Dictionary.xml.gz");
  48. var gzip = new GZipStream(stream, CompressionMode.Decompress);
  49. var reader = new DicomDictionaryReader(_default, DicomDictionaryFormat.XML, gzip);
  50. reader.Process();
  51. } catch (Exception e) {
  52. throw new DicomDataException("Unable to load DICOM dictionary from resources.", e);
  53. }
  54. _default.Add(GroupLength);
  55. }
  56. return _default;
  57. }
  58. }
  59. set {
  60. lock (_lock)
  61. _default = value;
  62. }
  63. }
  64. public DicomDictionaryEntry this[DicomTag tag] {
  65. get {
  66. if (_private != null && tag.PrivateCreator != null) {
  67. DicomDictionary pvt = null;
  68. if (_private.TryGetValue(tag.PrivateCreator, out pvt))
  69. return pvt[tag];
  70. }
  71. DicomDictionaryEntry entry = null;
  72. if (_entries.TryGetValue(tag, out entry))
  73. return entry;
  74. entry = _masked.Where(x => x.MaskTag.IsMatch(tag)).FirstOrDefault();
  75. if (entry != null)
  76. return entry;
  77. return UnknownTag;
  78. }
  79. }
  80. public DicomDictionary this[DicomPrivateCreator creator] {
  81. get {
  82. DicomDictionary pvt = null;
  83. if (!_private.TryGetValue(creator, out pvt)) {
  84. pvt = new DicomDictionary(creator);
  85. _private.Add(creator, pvt);
  86. }
  87. return pvt;
  88. }
  89. }
  90. #endregion
  91. #region Public Methods
  92. public void Add(DicomDictionaryEntry entry) {
  93. if (_privateCreator != null) {
  94. entry.Tag = new DicomTag(entry.Tag.Group, entry.Tag.Element, _privateCreator);
  95. if (entry.MaskTag != null)
  96. entry.MaskTag.Tag = entry.Tag;
  97. }
  98. if (entry.MaskTag == null) {
  99. _entries.Add(entry.Tag, entry);
  100. } else {
  101. _masked.Add(entry);
  102. _sortMasked = true;
  103. }
  104. }
  105. public DicomPrivateCreator GetPrivateCreator(string creator) {
  106. DicomPrivateCreator pvt = null;
  107. if (!_creators.TryGetValue(creator, out pvt)) {
  108. pvt = new DicomPrivateCreator(creator);
  109. _creators.Add(creator, pvt);
  110. }
  111. return pvt;
  112. }
  113. public void Load(string file, DicomDictionaryFormat format) {
  114. using (var fs = File.OpenRead(file)) {
  115. Stream s = fs;
  116. if (file.EndsWith(".gz"))
  117. s = new GZipStream(s, CompressionMode.Decompress);
  118. DicomDictionaryReader reader = new DicomDictionaryReader(this, format, s);
  119. reader.Process();
  120. }
  121. }
  122. #endregion
  123. #region IEnumerable Members
  124. public IEnumerator<DicomDictionaryEntry> GetEnumerator() {
  125. List<DicomDictionaryEntry> items = new List<DicomDictionaryEntry>();
  126. items.AddRange(_entries.Values.OrderBy(x => x.Tag));
  127. if (_sortMasked) {
  128. _masked.Sort((a, b) => { return a.MaskTag.Mask.CompareTo(b.MaskTag.Mask); });
  129. _sortMasked = false;
  130. }
  131. items.AddRange(_masked);
  132. return items.GetEnumerator();
  133. }
  134. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  135. return GetEnumerator();
  136. }
  137. #endregion
  138. }
  139. }