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

/DICOM/DicomMaskedTag.cs

https://github.com/petnet/fo-dicom
C# | 146 lines | 123 code | 23 blank | 0 comment | 24 complexity | e3ccdf59fd8d5029d0be2f99ceac24ad MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Dicom {
  7. public sealed class DicomMaskedTag : IFormattable {
  8. public const uint FullMask = 0xffffffff;
  9. public DicomMaskedTag(DicomTag tag) {
  10. Tag = tag;
  11. Mask = FullMask;
  12. }
  13. private DicomMaskedTag() {
  14. }
  15. private DicomTag _tag = null;
  16. public DicomTag Tag {
  17. get {
  18. if (_tag == null)
  19. _tag = new DicomTag(Group, Element);
  20. return _tag;
  21. }
  22. set {
  23. _tag = value;
  24. Card = ((uint)Group << 16) | (uint)Element;
  25. }
  26. }
  27. public ushort Group {
  28. get { return Tag.Group; }
  29. }
  30. public ushort Element {
  31. get { return Tag.Element; }
  32. }
  33. public uint Card {
  34. get;
  35. private set;
  36. }
  37. public uint Mask {
  38. get;
  39. private set;
  40. }
  41. public override string ToString() {
  42. return ToString("G", null);
  43. }
  44. public string ToString(string format, IFormatProvider formatProvider) {
  45. if (formatProvider != null) {
  46. ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
  47. if (fmt != null)
  48. return fmt.Format(format, this, formatProvider);
  49. }
  50. switch (format) {
  51. case "g": {
  52. string s = Group.ToString("x4");
  53. string x = String.Empty;
  54. x += ((Mask & 0xf0000000) != 0) ? s[0] : 'x';
  55. x += ((Mask & 0x0f000000) != 0) ? s[1] : 'x';
  56. x += ((Mask & 0x00f00000) != 0) ? s[2] : 'x';
  57. x += ((Mask & 0x000f0000) != 0) ? s[3] : 'x';
  58. return x;
  59. }
  60. case "e": {
  61. string s = Element.ToString("x4");
  62. string x = String.Empty;
  63. x += ((Mask & 0x0000f000) != 0) ? s[0] : 'x';
  64. x += ((Mask & 0x00000f00) != 0) ? s[1] : 'x';
  65. x += ((Mask & 0x000000f0) != 0) ? s[2] : 'x';
  66. x += ((Mask & 0x0000000f) != 0) ? s[3] : 'x';
  67. return x;
  68. }
  69. case "G":
  70. default: {
  71. return String.Format("({0},{1})", this.ToString("g", null), this.ToString("e", null));
  72. }
  73. }
  74. }
  75. public bool IsMatch(DicomTag tag) {
  76. return Card == ((((uint)tag.Group << 16) | (uint)tag.Element) & Mask);
  77. }
  78. public static DicomMaskedTag Parse(string s) {
  79. try {
  80. if (s.Length < 8)
  81. throw new ArgumentOutOfRangeException("s", "Expected a string of 8 or more characters");
  82. int pos = 0;
  83. if (s[pos] == '(')
  84. pos++;
  85. int idx = s.IndexOf(',');
  86. if (idx == -1)
  87. idx = pos + 4;
  88. string group = s.Substring(pos, idx - pos);
  89. pos = idx + 1;
  90. string element = null;
  91. if (s[s.Length - 1] == ')')
  92. element = s.Substring(pos, s.Length - pos - 1);
  93. else
  94. element = s.Substring(pos);
  95. return Parse(group, element);
  96. } catch (Exception e) {
  97. if (e is DicomDataException)
  98. throw;
  99. else
  100. throw new DicomDataException("Error parsing masked DICOM tag ['" + s + "']", e);
  101. }
  102. }
  103. public static DicomMaskedTag Parse(string group, string element) {
  104. try {
  105. DicomMaskedTag tag = new DicomMaskedTag();
  106. ushort g = ushort.Parse(group.ToLower().Replace('x', '0'), NumberStyles.HexNumber);
  107. ushort e = ushort.Parse(element.ToLower().Replace('x', '0'), NumberStyles.HexNumber);
  108. tag.Tag = new DicomTag(g, e);
  109. string mask = group + element;
  110. mask = mask.Replace('0', 'f').Replace('1', 'f').Replace('2', 'f')
  111. .Replace('3', 'f').Replace('4', 'f').Replace('5', 'f')
  112. .Replace('6', 'f').Replace('7', 'f').Replace('8', 'f')
  113. .Replace('9', 'f').Replace('a', 'f').Replace('b', 'f')
  114. .Replace('c', 'f').Replace('d', 'f').Replace('e', 'f')
  115. .Replace('f', 'f').Replace('x', '0');
  116. tag.Mask = uint.Parse(mask, NumberStyles.HexNumber);
  117. return tag;
  118. } catch (Exception e) {
  119. throw new DicomDataException("Error parsing masked DICOM tag [group:'" + group + "', element:'" + element +"']", e);
  120. }
  121. }
  122. }
  123. }