PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DICOM/DicomDataset.cs

https://github.com/petnet/fo-dicom
C# | 295 lines | 220 code | 52 blank | 23 comment | 139 complexity | 026ff6c695ecc7a7842cd684e1a77cf9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Concurrent;
  4. using System.Linq;
  5. using System.Text;
  6. using Dicom.Log;
  7. namespace Dicom {
  8. public class DicomDataset : IEnumerable<DicomItem> {
  9. private IDictionary<DicomTag, DicomItem> _items;
  10. public DicomDataset() {
  11. _items = new SortedList<DicomTag, DicomItem>();
  12. InternalTransferSyntax = DicomTransferSyntax.ExplicitVRLittleEndian;
  13. }
  14. public DicomDataset(params DicomItem[] items) : this() {
  15. foreach (DicomItem item in items)
  16. _items[item.Tag] = item;
  17. }
  18. public DicomDataset(IEnumerable<DicomItem> items) : this() {
  19. foreach (DicomItem item in items)
  20. _items[item.Tag] = item;
  21. }
  22. public DicomTransferSyntax InternalTransferSyntax {
  23. get;
  24. internal set;
  25. }
  26. public bool Exists(DicomTag tag) {
  27. return _items.ContainsKey(tag);
  28. }
  29. public T Get<T>(DicomTag tag, int n=0) {
  30. return Get<T>(tag, n, default(T));
  31. }
  32. public T Get<T>(DicomTag tag, T defaultValue) {
  33. return Get<T>(tag, 0, defaultValue);
  34. }
  35. public T Get<T>(DicomTag tag, int n, T defaultValue) {
  36. DicomItem item = null;
  37. if (!_items.TryGetValue(tag, out item))
  38. return defaultValue;
  39. if (typeof(T) == typeof(DicomItem))
  40. return (T)(object)item;
  41. if (typeof(T).IsSubclassOf(typeof(DicomItem))) {
  42. return (T)(object)item;
  43. }
  44. if (item.GetType().IsSubclassOf(typeof(DicomElement))) {
  45. DicomElement element = (DicomElement)item;
  46. if (n >= element.Count)
  47. return defaultValue;
  48. return (T)(object)element.Get<T>(n);
  49. }
  50. throw new DicomDataException("Unable to get a value type of {0} from DICOM item of type {1}", typeof(T), item.GetType());
  51. }
  52. public DicomDataset Add(params DicomItem[] items) {
  53. foreach (DicomItem item in items)
  54. _items[item.Tag] = item;
  55. return this;
  56. }
  57. public DicomDataset Add(IEnumerable<DicomItem> items) {
  58. foreach (DicomItem item in items)
  59. _items[item.Tag] = item;
  60. return this;
  61. }
  62. public DicomDataset Add<T>(DicomTag tag, params T[] values) {
  63. if (values == null)
  64. return this;
  65. var entry = DicomDictionary.Default[tag];
  66. if (entry == null)
  67. throw new DicomDataException("Tag {0} not found in DICOM dictionary", tag);
  68. var vr = entry.ValueRepresentations.FirstOrDefault(x => x.ValueType == typeof(T));
  69. if (vr == null)
  70. vr = entry.ValueRepresentations.First();
  71. if (vr == DicomVR.AE) {
  72. if (typeof(T) == typeof(string))
  73. return Add(new DicomApplicationEntity(tag, values.Cast<string>().ToArray()));
  74. }
  75. if (vr == DicomVR.AS) {
  76. if (typeof(T) == typeof(string))
  77. return Add(new DicomAgeString(tag, values.Cast<string>().ToArray()));
  78. }
  79. if (vr == DicomVR.AT) {
  80. if (typeof(T) == typeof(DicomTag))
  81. return Add(new DicomAttributeTag(tag, values.Cast<DicomTag>().ToArray()));
  82. }
  83. if (vr == DicomVR.CS) {
  84. if (typeof(T) == typeof(string))
  85. return Add(new DicomCodeString(tag, values.Cast<string>().ToArray()));
  86. if (typeof(T).IsEnum)
  87. return Add(new DicomCodeString(tag, values.Select(x => x.ToString()).ToArray()));
  88. }
  89. if (vr == DicomVR.DA) {
  90. if (typeof(T) == typeof(DateTime))
  91. return Add(new DicomDate(tag, values.Cast<DateTime>().ToArray()));
  92. if (typeof(T) == typeof(DicomDateRange))
  93. return Add(new DicomDate(tag, values.Cast<DicomDateRange>().First()));
  94. if (typeof(T) == typeof(string))
  95. return Add(new DicomDate(tag, values.Cast<string>().ToArray()));
  96. }
  97. if (vr == DicomVR.DS) {
  98. if (typeof(T) == typeof(decimal))
  99. return Add(new DicomDecimalString(tag, values.Cast<decimal>().ToArray()));
  100. if (typeof(T) == typeof(string))
  101. return Add(new DicomDecimalString(tag, values.Cast<string>().ToArray()));
  102. }
  103. if (vr == DicomVR.DT) {
  104. if (typeof(T) == typeof(DateTime))
  105. return Add(new DicomDateTime(tag, values.Cast<DateTime>().ToArray()));
  106. if (typeof(T) == typeof(DicomDateRange))
  107. return Add(new DicomDateTime(tag, values.Cast<DicomDateRange>().First()));
  108. if (typeof(T) == typeof(string))
  109. return Add(new DicomDateTime(tag, values.Cast<string>().ToArray()));
  110. }
  111. if (vr == DicomVR.FD) {
  112. if (typeof(T) == typeof(double))
  113. return Add(new DicomFloatingPointDouble(tag, values.Cast<double>().ToArray()));
  114. }
  115. if (vr == DicomVR.FL) {
  116. if (typeof(T) == typeof(float))
  117. return Add(new DicomFloatingPointSingle(tag, values.Cast<float>().ToArray()));
  118. }
  119. if (vr == DicomVR.IS) {
  120. if (typeof(T) == typeof(int))
  121. return Add(new DicomIntegerString(tag, values.Cast<int>().ToArray()));
  122. if (typeof(T) == typeof(string))
  123. return Add(new DicomIntegerString(tag, values.Cast<string>().ToArray()));
  124. }
  125. if (vr == DicomVR.LO) {
  126. if (typeof(T) == typeof(string))
  127. return Add(new DicomLongString(tag, values.Cast<string>().First()));
  128. }
  129. if (vr == DicomVR.LT) {
  130. if (typeof(T) == typeof(string))
  131. return Add(new DicomLongText(tag, values.Cast<string>().First()));
  132. }
  133. if (vr == DicomVR.OB) {
  134. if (typeof(T) == typeof(byte))
  135. return Add(new DicomOtherByte(tag, values.Cast<byte>().ToArray()));
  136. }
  137. if (vr == DicomVR.OF) {
  138. if (typeof(T) == typeof(float))
  139. return Add(new DicomOtherFloat(tag, values.Cast<float>().ToArray()));
  140. }
  141. if (vr == DicomVR.OW) {
  142. if (typeof(T) == typeof(ushort))
  143. return Add(new DicomOtherWord(tag, values.Cast<ushort>().ToArray()));
  144. }
  145. if (vr == DicomVR.PN) {
  146. if (typeof(T) == typeof(string))
  147. return Add(new DicomPersonName(tag, values.Cast<string>().First()));
  148. }
  149. if (vr == DicomVR.SH) {
  150. if (typeof(T) == typeof(string))
  151. return Add(new DicomShortString(tag, values.Cast<string>().First()));
  152. }
  153. if (vr == DicomVR.SL) {
  154. if (typeof(T) == typeof(int))
  155. return Add(new DicomSignedLong(tag, values.Cast<int>().ToArray()));
  156. }
  157. if (vr == DicomVR.SS) {
  158. if (typeof(T) == typeof(short))
  159. return Add(new DicomSignedShort(tag, values.Cast<short>().ToArray()));
  160. }
  161. if (vr == DicomVR.ST) {
  162. if (typeof(T) == typeof(string))
  163. return Add(new DicomShortText(tag, values.Cast<string>().First()));
  164. }
  165. if (vr == DicomVR.TM) {
  166. if (typeof(T) == typeof(DateTime))
  167. return Add(new DicomTime(tag, values.Cast<DateTime>().ToArray()));
  168. if (typeof(T) == typeof(DicomDateRange))
  169. return Add(new DicomTime(tag, values.Cast<DicomDateRange>().First()));
  170. if (typeof(T) == typeof(string))
  171. return Add(new DicomTime(tag, values.Cast<string>().ToArray()));
  172. }
  173. if (vr == DicomVR.UI) {
  174. if (typeof(T) == typeof(DicomUID))
  175. return Add(new DicomUniqueIdentifier(tag, values.Cast<DicomUID>().ToArray()));
  176. if (typeof(T) == typeof(DicomTransferSyntax))
  177. return Add(new DicomUniqueIdentifier(tag, values.Cast<DicomTransferSyntax>().ToArray()));
  178. }
  179. if (vr == DicomVR.UL) {
  180. if (typeof(T) == typeof(uint))
  181. return Add(new DicomUnsignedLong(tag, values.Cast<uint>().ToArray()));
  182. }
  183. if (vr == DicomVR.UN) {
  184. if (typeof(T) == typeof(byte))
  185. return Add(new DicomUnknown(tag, values.Cast<byte>().ToArray()));
  186. }
  187. if (vr == DicomVR.US) {
  188. if (typeof(T) == typeof(ushort))
  189. return Add(new DicomUnsignedShort(tag, values.Cast<ushort>().ToArray()));
  190. }
  191. if (vr == DicomVR.UT) {
  192. if (typeof(T) == typeof(string))
  193. return Add(new DicomUnlimitedText(tag, values.Cast<string>().First()));
  194. }
  195. throw new InvalidOperationException(String.Format("Unable to create DICOM element of type {0} with values of type {1}", vr.Code, typeof(T).ToString()));
  196. }
  197. public bool Contains(DicomTag tag) {
  198. return _items.ContainsKey(tag);
  199. }
  200. /// <summary>
  201. /// Removes items for specified tags.
  202. /// </summary>
  203. /// <param name="tags">DICOM tags to remove</param>
  204. /// <returns>Current Dataset</returns>
  205. public DicomDataset Remove(params DicomTag[] tags) {
  206. foreach (DicomTag tag in tags)
  207. _items.Remove(tag);
  208. return this;
  209. }
  210. /// <summary>
  211. /// Removes items where the selector function returns true.
  212. /// </summary>
  213. /// <param name="selector">Selector function</param>
  214. /// <returns>Current Dataset</returns>
  215. public DicomDataset Remove(Func<DicomItem, bool> selector) {
  216. foreach (DicomItem item in _items.Values.Where(selector).ToArray())
  217. _items.Remove(item.Tag);
  218. return this;
  219. }
  220. /// <summary>
  221. /// Enumerates all DICOM items.
  222. /// </summary>
  223. /// <returns>Enumeration of DICOM items</returns>
  224. public IEnumerator<DicomItem> GetEnumerator() {
  225. return _items.Values.GetEnumerator();
  226. }
  227. /// <summary>
  228. /// Enumerates all DICOM items.
  229. /// </summary>
  230. /// <returns>Enumeration of DICOM items</returns>
  231. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  232. return _items.Values.GetEnumerator();
  233. }
  234. /// <summary>
  235. /// Enumerates DICOM items for specified group.
  236. /// </summary>
  237. /// <param name="group">Group</param>
  238. /// <returns>Enumeration of DICOM items</returns>
  239. public IEnumerable<DicomItem> GetGroup(ushort group) {
  240. return _items.Values.Where(x => x.Tag.Group == group && x.Tag.Element != 0x0000);
  241. }
  242. }
  243. }