PageRenderTime 274ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/DICOM/DicomElement.cs

https://github.com/petnet/fo-dicom
C# | 1023 lines | 984 code | 31 blank | 8 comment | 34 complexity | 635c753c615c192ebefae9baf9b09c4c 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.Runtime.Serialization;
  6. using System.Text;
  7. using Dicom.IO;
  8. using Dicom.IO.Buffer;
  9. using Dicom.Imaging.Mathematics;
  10. namespace Dicom {
  11. public abstract class DicomElement : DicomItem {
  12. protected DicomElement(DicomTag tag, IByteBuffer data) : base(tag) {
  13. this.Buffer = data;
  14. }
  15. /// <summary>Gets the number of values that the DICOM element contains.</summary>
  16. /// <value>Number of value items</value>
  17. public abstract int Count {
  18. get;
  19. }
  20. public IByteBuffer Buffer {
  21. get;
  22. protected set;
  23. }
  24. public uint Length {
  25. get {
  26. if (Buffer != null)
  27. return Buffer.Size;
  28. return 0;
  29. }
  30. }
  31. public abstract T Get<T>(int item = -1);
  32. }
  33. /// <summary>
  34. /// Base class for a DICOM string element.
  35. /// </summary>
  36. /// <seealso cref="DicomPersonName"/>
  37. public abstract class DicomStringElement : DicomElement {
  38. protected DicomStringElement(DicomTag tag, string value) : this(tag, DicomEncoding.Default, value) {
  39. }
  40. protected DicomStringElement(DicomTag tag, Encoding encoding, string value) : base(tag, EmptyBuffer.Value) {
  41. Encoding = encoding;
  42. Buffer = ByteConverter.ToByteBuffer(value ?? String.Empty, encoding, ValueRepresentation.PaddingValue);
  43. }
  44. protected DicomStringElement(DicomTag tag, Encoding encoding, IByteBuffer buffer) : base(tag, buffer) {
  45. Encoding = encoding;
  46. }
  47. public Encoding Encoding {
  48. get;
  49. protected set;
  50. }
  51. /// <summary>Gets the number of values that the DICOM element contains.</summary>
  52. /// <value>Number of value items</value>
  53. public override int Count {
  54. get { return 1; }
  55. }
  56. private string _value = null;
  57. protected string StringValue {
  58. get {
  59. if (_value == null && Buffer != null)
  60. _value = Encoding.GetString(Buffer.Data).TrimEnd((char)ValueRepresentation.PaddingValue);
  61. return _value;
  62. }
  63. }
  64. public override T Get<T>(int item = -1) {
  65. if (typeof(T) == typeof(string) || typeof(T) == typeof(object))
  66. return (T)((object)StringValue);
  67. if (typeof(T) == typeof(string[]) || typeof(T) == typeof(object[]))
  68. return (T)(object)(new string[] { StringValue });
  69. if (typeof(T).IsSubclassOf(typeof(DicomParseable)))
  70. return (T)DicomParseable.Parse<T>(StringValue);
  71. if (typeof(T).IsEnum)
  72. return (T)Enum.Parse(typeof(T), StringValue, true);
  73. throw new InvalidCastException("Unable to convert DICOM " + ValueRepresentation.Code + " value to '" + typeof(T).Name + "'");
  74. }
  75. }
  76. public abstract class DicomMultiStringElement : DicomStringElement {
  77. protected DicomMultiStringElement(DicomTag tag, params string[] values) : base(tag, DicomEncoding.Default, String.Join("\\", values)) {
  78. }
  79. protected DicomMultiStringElement(DicomTag tag, Encoding encoding, params string[] values) : base(tag, encoding, String.Join("\\", values)) {
  80. }
  81. protected DicomMultiStringElement(DicomTag tag, Encoding encoding, IByteBuffer buffer) : base(tag, encoding, buffer) {
  82. }
  83. private int _count = -1;
  84. public override int Count {
  85. get {
  86. if (_count == -1) {
  87. if (String.IsNullOrEmpty(StringValue))
  88. _count = 0;
  89. else
  90. _count = StringValue.Split('\\').Count();
  91. }
  92. return _count;
  93. }
  94. }
  95. private string[] _values = null;
  96. public override T Get<T>(int item = -1) {
  97. if (_values == null) {
  98. if (String.IsNullOrEmpty(StringValue))
  99. _values = new string[0];
  100. else
  101. _values = StringValue.Split('\\');
  102. }
  103. if (typeof(T) == typeof(string) || typeof(T) == typeof(object)) {
  104. if (item == -1)
  105. return (T)((object)StringValue);
  106. return (T)((object)_values[item]);
  107. }
  108. if (typeof(T) == typeof(string[]) || typeof(T) == typeof(object[]))
  109. return (T)(object)_values;
  110. if (typeof(T).IsSubclassOf(typeof(DicomParseable)))
  111. return (T)DicomParseable.Parse<T>(_values[item]);
  112. if (typeof(T).IsEnum)
  113. return (T)Enum.Parse(typeof(T), _values[item]);
  114. throw new InvalidCastException("Unable to convert DICOM " + ValueRepresentation.Code + " value to '" + typeof(T).Name + "'");
  115. }
  116. }
  117. public abstract class DicomDateElement : DicomMultiStringElement {
  118. protected DicomDateElement(DicomTag tag, string[] dateFormats, params DateTime[] values) : base(tag, DicomEncoding.Default, values.Select(x => x.ToString(dateFormats[0])).ToArray()) {
  119. DateFormats = dateFormats;
  120. }
  121. protected DicomDateElement(DicomTag tag, string[] dateFormats, DicomDateRange range) : base(tag, DicomEncoding.Default, range.ToString(dateFormats[0])) {
  122. DateFormats = dateFormats;
  123. }
  124. protected DicomDateElement(DicomTag tag, string[] dateFormats, params string[] values) : base(tag, DicomEncoding.Default, String.Join("\\", values)) {
  125. DateFormats = dateFormats;
  126. }
  127. protected DicomDateElement(DicomTag tag, string[] dateFormats, IByteBuffer buffer) : base(tag, DicomEncoding.Default, buffer) {
  128. DateFormats = dateFormats;
  129. }
  130. protected string[] DateFormats {
  131. get;
  132. private set;
  133. }
  134. private DateTime[] _values = null;
  135. public override T Get<T>(int item = -1) {
  136. if (typeof(T) == typeof(DicomDateRange)) {
  137. string[] vals = base.Get<string>(item).Split('-');
  138. var range = new DicomDateRange();
  139. if (vals.Length >= 2) {
  140. if (!String.IsNullOrEmpty(vals[0]))
  141. range.Minimum = DateTime.ParseExact(vals[0], DateFormats, CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault);
  142. if (!String.IsNullOrEmpty(vals[1]))
  143. range.Maximum = DateTime.ParseExact(vals[1], DateFormats, CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault);
  144. } else if (vals.Length == 1) {
  145. range.Minimum = DateTime.ParseExact(vals[0], DateFormats, CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault);
  146. range.Maximum = range.Minimum.AddDays(1).AddMilliseconds(-1);
  147. }
  148. return (T)(object)range;
  149. }
  150. if (_values == null) {
  151. string[] vals = base.Get<string[]>();
  152. if (vals.Length == 1 && String.IsNullOrEmpty(vals[0]))
  153. _values = new DateTime[0];
  154. else {
  155. _values = new DateTime[vals.Length];
  156. for (int i = 0; i < vals.Length; i++)
  157. _values[i] = DateTime.ParseExact(vals[i], DateFormats, CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault);
  158. }
  159. }
  160. if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(object)) {
  161. if (item < 0 || item >= Count)
  162. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  163. return (T)((object)_values[item]);
  164. }
  165. if (typeof(T) == typeof(DateTime[]) || typeof(T) == typeof(object[])) {
  166. return (T)(object)_values;
  167. }
  168. return base.Get<T>(item);
  169. }
  170. }
  171. public abstract class DicomValueElement<Tv> : DicomElement where Tv: struct {
  172. protected DicomValueElement(DicomTag tag, params Tv[] values) : this(tag, ByteConverter.ToByteBuffer<Tv>(values)) {
  173. }
  174. protected DicomValueElement(DicomTag tag, IByteBuffer data) : base(tag, data) {
  175. }
  176. public override int Count {
  177. get { return (int)Buffer.Size / ValueRepresentation.UnitSize; }
  178. }
  179. #region Public Members
  180. public override T Get<T>(int item = 0) {
  181. if (typeof(T) == typeof(Tv)) {
  182. if (item < 0 || item >= Count)
  183. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  184. return ByteConverter.Get<T>(Buffer, item);
  185. }
  186. if (typeof(T) == typeof(Tv[])) {
  187. // Is there a way to avoid this cast?
  188. return (T)(object)ByteConverter.ToArray<Tv>(Buffer);
  189. }
  190. if (typeof(T) == typeof(string)) {
  191. if (item < 0 || item >= Count)
  192. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  193. return (T)(object)ByteConverter.Get<Tv>(Buffer, item).ToString();
  194. }
  195. if (typeof(T) == typeof(string[])) {
  196. return (T)(object)ByteConverter.ToArray<Tv>(Buffer).Select(x => x.ToString()).ToArray();
  197. }
  198. if (typeof(T).IsEnum) {
  199. if (item < 0 || item >= Count)
  200. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  201. var s = ByteConverter.Get<Tv>(Buffer, item).ToString();
  202. return (T)Enum.Parse(typeof(T), s);
  203. }
  204. if (typeof(T).IsValueType) {
  205. if (item < 0 || item >= Count)
  206. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  207. return (T)Convert.ChangeType(ByteConverter.Get<Tv>(Buffer, item), typeof(T));
  208. }
  209. throw new InvalidCastException("Unable to convert DICOM " + ValueRepresentation.Code + " value to '" + typeof(T).Name + "'");
  210. }
  211. #endregion
  212. }
  213. /// <summary>Application Entity (AE)</summary>
  214. public class DicomApplicationEntity : DicomMultiStringElement {
  215. #region Public Constructors
  216. public DicomApplicationEntity(DicomTag tag, params string[] values) : base(tag, DicomEncoding.Default, values) {
  217. }
  218. public DicomApplicationEntity(DicomTag tag, IByteBuffer data) : base(tag, DicomEncoding.Default, data) {
  219. }
  220. #endregion
  221. #region Public Properties
  222. public override DicomVR ValueRepresentation {
  223. get { return DicomVR.AE; }
  224. }
  225. #endregion
  226. }
  227. /// <summary>Age String (AS)</summary>
  228. public class DicomAgeString : DicomMultiStringElement {
  229. #region Public Constructors
  230. public DicomAgeString(DicomTag tag, params string[] values) : base(tag, DicomEncoding.Default, values) {
  231. }
  232. public DicomAgeString(DicomTag tag, IByteBuffer data) : base(tag, DicomEncoding.Default, data) {
  233. }
  234. #endregion
  235. #region Public Properties
  236. public override DicomVR ValueRepresentation {
  237. get { return DicomVR.AS; }
  238. }
  239. #endregion
  240. }
  241. /// <summary>Attribute Tag (AT)</summary>
  242. public class DicomAttributeTag : DicomElement {
  243. #region Public Constructors
  244. public DicomAttributeTag(DicomTag tag, params DicomTag[] values) : base(tag, EmptyBuffer.Value) {
  245. Values = values;
  246. }
  247. public DicomAttributeTag(DicomTag tag, IByteBuffer data) : base(tag, data) {
  248. }
  249. #endregion
  250. #region Public Properties
  251. public override int Count {
  252. get { return (int)Buffer.Size / 4; }
  253. }
  254. public override DicomVR ValueRepresentation {
  255. get { return DicomVR.AT; }
  256. }
  257. private DicomTag[] _values;
  258. public IEnumerable<DicomTag> Values {
  259. get {
  260. if (_values == null) {
  261. var values = new List<DicomTag>();
  262. var parts = ByteBufferEnumerator<ushort>.Create(Buffer).ToArray();
  263. for (int i = 0; i < parts.Length; i += 2) {
  264. var group = parts[i + 0];
  265. var element = parts[i + 1];
  266. values.Add(new DicomTag(group, element));
  267. }
  268. _values = values.ToArray();
  269. }
  270. return _values;
  271. }
  272. private set {
  273. _values = value.ToArray();
  274. int length = _values.Length * 4;
  275. byte[] buffer = new byte[length];
  276. for (int i = 0; i < _values.Length; i++) {
  277. var bytes = BitConverter.GetBytes(_values[i].Group);
  278. Array.Copy(bytes, buffer, i * 4);
  279. bytes = BitConverter.GetBytes(_values[i].Element);
  280. Array.Copy(bytes, buffer, i * 4 + 2);
  281. }
  282. Buffer = new MemoryByteBuffer(buffer);
  283. }
  284. }
  285. #endregion
  286. #region Public Members
  287. public override T Get<T>(int item = 0) {
  288. var tags = Values.ToArray();
  289. if (typeof(T) == typeof(DicomTag))
  290. return (T)(object)tags[item];
  291. if (typeof(T) == typeof(DicomTag[]))
  292. return (T)(object)tags;
  293. if (typeof(T) == typeof(string))
  294. return (T)(object)tags[item].ToString();
  295. if (typeof(T) == typeof(string[]))
  296. return (T)(object)tags.Select(x => x.ToString()).ToArray();
  297. throw new InvalidCastException("Unable to convert DICOM " + ValueRepresentation.Code + " value to '" + typeof(T).Name + "'");
  298. }
  299. #endregion
  300. }
  301. /// <summary>Code String (CS)</summary>
  302. public class DicomCodeString : DicomMultiStringElement {
  303. #region Public Constructors
  304. public DicomCodeString(DicomTag tag, params string[] values) : base(tag, DicomEncoding.Default, values) {
  305. }
  306. public DicomCodeString(DicomTag tag, IByteBuffer data) : base (tag, DicomEncoding.Default, data) {
  307. }
  308. #endregion
  309. #region Public Properties
  310. public override DicomVR ValueRepresentation {
  311. get { return DicomVR.CS; }
  312. }
  313. #endregion
  314. }
  315. /// <summary>Date (DA)</summary>
  316. public class DicomDate : DicomDateElement {
  317. #region Public Constructors
  318. public DicomDate(DicomTag tag, params DateTime[] values) : base(tag, PrivateDateFormats, values) {
  319. }
  320. public DicomDate(DicomTag tag, DicomDateRange range) : base(tag, PrivateDateFormats, range) {
  321. }
  322. public DicomDate(DicomTag tag, params string[] values) : base(tag, PrivateDateFormats, values) {
  323. }
  324. public DicomDate(DicomTag tag, IByteBuffer data) : base (tag, PrivateDateFormats, data) {
  325. }
  326. #endregion
  327. #region Public Properties
  328. public override DicomVR ValueRepresentation {
  329. get { return DicomVR.DA; }
  330. }
  331. private static string[] _formats;
  332. private static string[] PrivateDateFormats {
  333. get {
  334. if (_formats == null) {
  335. _formats = new string[6];
  336. _formats[0] = "yyyyMMdd";
  337. _formats[1] = "yyyy.MM.dd";
  338. _formats[2] = "yyyy/MM/dd";
  339. _formats[3] = "yyyy";
  340. _formats[4] = "yyyyMM";
  341. _formats[5] = "yyyy.MM";
  342. }
  343. return _formats;
  344. }
  345. }
  346. #endregion
  347. }
  348. /// <summary>Decimal String (DS)</summary>
  349. public class DicomDecimalString : DicomMultiStringElement {
  350. #region Public Constructors
  351. public DicomDecimalString(DicomTag tag, params decimal[] values) : base(tag, DicomEncoding.Default, values.Select(x => x.ToString()).ToArray()) {
  352. }
  353. public DicomDecimalString(DicomTag tag, params string[] values) : base(tag, DicomEncoding.Default, values) {
  354. }
  355. public DicomDecimalString(DicomTag tag, IByteBuffer data) : base (tag, DicomEncoding.Default, data) {
  356. }
  357. #endregion
  358. #region Public Properties
  359. public override DicomVR ValueRepresentation {
  360. get { return DicomVR.DS; }
  361. }
  362. #endregion
  363. #region Public Members
  364. private decimal[] _values;
  365. public override T Get<T>(int item = -1) {
  366. if (_values == null) {
  367. _values = base.Get<string[]>().Select(x => decimal.Parse(x, NumberStyles.Any)).ToArray();
  368. }
  369. if (typeof(T) == typeof(decimal) || typeof(T) == typeof(object)) {
  370. return (T)(object)_values[item];
  371. }
  372. if (typeof(T) == typeof(decimal[]) || typeof(T) == typeof(object[])) {
  373. return (T)(object)_values;
  374. }
  375. if (typeof(T) == typeof(double))
  376. return (T)(object)Convert.ToDouble(_values[item]);
  377. if (typeof(T) == typeof(double[]))
  378. return (T)(object)_values.Select(x => Convert.ToDouble(x)).ToArray();
  379. return base.Get<T>(item);
  380. }
  381. #endregion
  382. }
  383. /// <summary>Date Time (DT)</summary>
  384. public class DicomDateTime : DicomDateElement {
  385. #region Public Constructors
  386. public DicomDateTime(DicomTag tag, params DateTime[] values) : base(tag, PrivateDateFormats, values) {
  387. }
  388. public DicomDateTime(DicomTag tag, DicomDateRange range) : base(tag, PrivateDateFormats, range) {
  389. }
  390. public DicomDateTime(DicomTag tag, params string[] values) : base(tag, PrivateDateFormats, values) {
  391. }
  392. public DicomDateTime(DicomTag tag, IByteBuffer data) : base (tag, PrivateDateFormats, data) {
  393. }
  394. #endregion
  395. #region Public Properties
  396. public override DicomVR ValueRepresentation {
  397. get { return DicomVR.DT; }
  398. }
  399. private static string[] _formats;
  400. private static string[] PrivateDateFormats {
  401. get {
  402. if (_formats == null) {
  403. _formats = new string[8];
  404. _formats[0] = "yyyyMMddHHmmss.fff";
  405. _formats[1] = "yyyyMMddHHmmss.ff";
  406. _formats[2] = "yyyyMMddHHmmss.f";
  407. _formats[3] = "yyyyMMddHHmmss";
  408. _formats[4] = "yyyyMMddHHmm";
  409. _formats[5] = "yyyyMMdd";
  410. _formats[6] = "yyyy.MM.dd";
  411. _formats[7] = "yyyy/MM/dd";
  412. }
  413. return _formats;
  414. }
  415. }
  416. #endregion
  417. }
  418. /// <summary>Floating Point Double (FD)</summary>
  419. public class DicomFloatingPointDouble : DicomValueElement<double> {
  420. #region Public Constructors
  421. public DicomFloatingPointDouble(DicomTag tag, params double[] values) : base(tag, values) {
  422. }
  423. public DicomFloatingPointDouble(DicomTag tag, IByteBuffer data) : base (tag, data) {
  424. }
  425. #endregion
  426. #region Public Properties
  427. public override DicomVR ValueRepresentation {
  428. get { return DicomVR.FD; }
  429. }
  430. #endregion
  431. }
  432. /// <summary>Floating Point Single (FL)</summary>
  433. public class DicomFloatingPointSingle : DicomValueElement<float> {
  434. #region Public Constructors
  435. public DicomFloatingPointSingle(DicomTag tag, params float[] values) : base(tag, values) {
  436. }
  437. public DicomFloatingPointSingle(DicomTag tag, IByteBuffer data) : base (tag, data) {
  438. }
  439. #endregion
  440. #region Public Properties
  441. public override DicomVR ValueRepresentation {
  442. get { return DicomVR.FL; }
  443. }
  444. #endregion
  445. }
  446. /// <summary>Integer String (IS)</summary>
  447. public class DicomIntegerString : DicomMultiStringElement {
  448. #region Public Constructors
  449. public DicomIntegerString(DicomTag tag, params int[] values) : base(tag, DicomEncoding.Default, values.Select(x => x.ToString()).ToArray()) {
  450. }
  451. public DicomIntegerString(DicomTag tag, params string[] values) : base(tag, DicomEncoding.Default, values) {
  452. }
  453. public DicomIntegerString(DicomTag tag, IByteBuffer data) : base (tag, DicomEncoding.Default, data) {
  454. }
  455. #endregion
  456. #region Public Properties
  457. public override DicomVR ValueRepresentation {
  458. get { return DicomVR.IS; }
  459. }
  460. #endregion
  461. #region Public Members
  462. private int[] _values;
  463. public override T Get<T>(int item = -1) {
  464. if (_values == null) {
  465. _values = base.Get<string[]>().Select(x => int.Parse(x)).ToArray();
  466. }
  467. if (typeof(T) == typeof(int) || typeof(T) == typeof(object)) {
  468. return (T)(object)_values[item];
  469. }
  470. if (typeof(T) == typeof(int[]) || typeof(T) == typeof(object[])) {
  471. return (T)(object)_values;
  472. }
  473. if (typeof(T).IsEnum) {
  474. if (item < 0 || item >= Count)
  475. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  476. return (T)(object)_values[item];
  477. }
  478. if (typeof(T).IsValueType) {
  479. if (item < 0 || item >= Count)
  480. throw new ArgumentOutOfRangeException("item", "Index is outside the range of available value items");
  481. return (T)Convert.ChangeType(_values[item], typeof(T));
  482. }
  483. return base.Get<T>(item);
  484. }
  485. #endregion
  486. }
  487. /// <summary>Long String (LO)</summary>
  488. public class DicomLongString : DicomStringElement {
  489. #region Public Constructors
  490. public DicomLongString(DicomTag tag, string value) : base(tag, value) {
  491. }
  492. public DicomLongString(DicomTag tag, Encoding encoding, IByteBuffer data) : base (tag, encoding, data) {
  493. }
  494. #endregion
  495. #region Public Properties
  496. public override DicomVR ValueRepresentation {
  497. get { return DicomVR.LO; }
  498. }
  499. #endregion
  500. }
  501. /// <summary>Long Text (LT)</summary>
  502. public class DicomLongText : DicomStringElement {
  503. #region Public Constructors
  504. public DicomLongText(DicomTag tag, string value) : base(tag, value) {
  505. }
  506. public DicomLongText(DicomTag tag, Encoding encoding, IByteBuffer data) : base(tag, encoding, data) {
  507. }
  508. #endregion
  509. #region Public Properties
  510. public override DicomVR ValueRepresentation {
  511. get { return DicomVR.LT; }
  512. }
  513. #endregion
  514. }
  515. /// <summary>Other Byte (OB)</summary>
  516. public class DicomOtherByte : DicomValueElement<byte> {
  517. #region Public Constructors
  518. public DicomOtherByte(DicomTag tag, params byte[] values) : base(tag, values) {
  519. }
  520. public DicomOtherByte(DicomTag tag, IByteBuffer data) : base (tag, data) {
  521. }
  522. #endregion
  523. #region Public Properties
  524. public override DicomVR ValueRepresentation {
  525. get { return DicomVR.OB; }
  526. }
  527. #endregion
  528. #region Public Methods
  529. public override T Get<T>(int item = -1) {
  530. if (!typeof(T).IsArray && item == -1)
  531. item = 0;
  532. if (typeof(T) == typeof(short))
  533. return (T)(object)ByteBufferEnumerator<short>.Create(Buffer).ToArray().GetValue(item);
  534. if (typeof(T) == typeof(short[]))
  535. return (T)(object)ByteBufferEnumerator<short>.Create(Buffer).ToArray();
  536. if (typeof(T) == typeof(ushort))
  537. return (T)(object)ByteBufferEnumerator<ushort>.Create(Buffer).ToArray().GetValue(item);
  538. if (typeof(T) == typeof(ushort[]))
  539. return (T)(object)ByteBufferEnumerator<ushort>.Create(Buffer).ToArray();
  540. if (typeof(T) == typeof(int))
  541. return (T)(object)ByteBufferEnumerator<int>.Create(Buffer).ToArray().GetValue(item);
  542. if (typeof(T) == typeof(int[]))
  543. return (T)(object)ByteBufferEnumerator<int>.Create(Buffer).ToArray();
  544. if (typeof(T) == typeof(uint))
  545. return (T)(object)ByteBufferEnumerator<uint>.Create(Buffer).ToArray().GetValue(item);
  546. if (typeof(T) == typeof(uint[]))
  547. return (T)(object)ByteBufferEnumerator<uint>.Create(Buffer).ToArray();
  548. if (typeof(T) == typeof(float))
  549. return (T)(object)ByteBufferEnumerator<float>.Create(Buffer).ToArray().GetValue(item);
  550. if (typeof(T) == typeof(float[]))
  551. return (T)(object)ByteBufferEnumerator<float>.Create(Buffer).ToArray();
  552. if (typeof(T) == typeof(double))
  553. return (T)(object)ByteBufferEnumerator<double>.Create(Buffer).ToArray().GetValue(item);
  554. if (typeof(T) == typeof(double[]))
  555. return (T)(object)ByteBufferEnumerator<double>.Create(Buffer).ToArray();
  556. return base.Get<T>(item);
  557. }
  558. #endregion
  559. }
  560. /// <summary>Other Word (OW)</summary>
  561. public class DicomOtherWord : DicomValueElement<ushort> {
  562. #region Public Constructors
  563. public DicomOtherWord(DicomTag tag, params ushort[] values) : base(tag, values) {
  564. }
  565. public DicomOtherWord(DicomTag tag, IByteBuffer data) : base (tag, data) {
  566. }
  567. #endregion
  568. #region Public Properties
  569. public override DicomVR ValueRepresentation {
  570. get { return DicomVR.OW; }
  571. }
  572. #endregion
  573. }
  574. /// <summary>Other Float (OF)</summary>
  575. public class DicomOtherFloat : DicomValueElement<float> {
  576. #region Public Constructors
  577. public DicomOtherFloat(DicomTag tag, params float[] values) : base(tag, values) {
  578. }
  579. public DicomOtherFloat(DicomTag tag, IByteBuffer data) : base (tag, data) {
  580. }
  581. #endregion
  582. #region Public Properties
  583. public override DicomVR ValueRepresentation {
  584. get { return DicomVR.OF; }
  585. }
  586. #endregion
  587. }
  588. /// <summary>Person Name (PN)</summary>
  589. public sealed class DicomPersonName : DicomStringElement {
  590. #region Public Constructors
  591. public DicomPersonName(DicomTag tag, string value) : base(tag, value) {
  592. }
  593. public DicomPersonName(DicomTag tag, string Last, string Middle, string First, string Prefix = null, string Suffix = null) : base(tag, null) {
  594. throw new NotImplementedException();
  595. }
  596. public DicomPersonName(DicomTag tag, Encoding encoding, IByteBuffer data) : base (tag, encoding, data) {
  597. }
  598. #endregion
  599. #region Public Properties
  600. public override DicomVR ValueRepresentation {
  601. get { return DicomVR.PN; }
  602. }
  603. public string Last {
  604. get;
  605. set;
  606. }
  607. public string Middle {
  608. get;
  609. set;
  610. }
  611. public string First {
  612. get;
  613. set;
  614. }
  615. #endregion
  616. }
  617. /// <summary>Short String (SH)</summary>
  618. public class DicomShortString : DicomStringElement {
  619. #region Public Constructors
  620. public DicomShortString(DicomTag tag, string value) : base(tag, value) {
  621. }
  622. public DicomShortString(DicomTag tag, Encoding encoding, IByteBuffer data) : base (tag, encoding, data) {
  623. }
  624. #endregion
  625. #region Public Properties
  626. public override DicomVR ValueRepresentation {
  627. get { return DicomVR.SH; }
  628. }
  629. #endregion
  630. }
  631. /// <summary>Signed Long (SL)</summary>
  632. public class DicomSignedLong : DicomValueElement<int> {
  633. #region Public Constructors
  634. public DicomSignedLong(DicomTag tag, params int[] values) : base(tag, values) {
  635. }
  636. public DicomSignedLong(DicomTag tag, IByteBuffer data) : base (tag, data) {
  637. }
  638. #endregion
  639. #region Public Properties
  640. public override DicomVR ValueRepresentation {
  641. get { return DicomVR.SL; }
  642. }
  643. #endregion
  644. }
  645. /// <summary>Signed Short (SS)</summary>
  646. public class DicomSignedShort : DicomValueElement<short> {
  647. #region Public Constructors
  648. public DicomSignedShort(DicomTag tag, params short[] values) : base(tag, values) {
  649. }
  650. public DicomSignedShort(DicomTag tag, IByteBuffer data) : base (tag, data) {
  651. }
  652. #endregion
  653. #region Public Properties
  654. public override DicomVR ValueRepresentation {
  655. get { return DicomVR.SS; }
  656. }
  657. #endregion
  658. }
  659. /// <summary>Short Text (ST)</summary>
  660. public class DicomShortText : DicomStringElement {
  661. #region Public Constructors
  662. public DicomShortText(DicomTag tag, string value) : base(tag, value) {
  663. }
  664. public DicomShortText(DicomTag tag, Encoding encoding, IByteBuffer data) : base(tag, encoding, data) {
  665. }
  666. #endregion
  667. #region Public Properties
  668. public override DicomVR ValueRepresentation {
  669. get { return DicomVR.ST; }
  670. }
  671. #endregion
  672. }
  673. /// <summary>Time (TM)</summary>
  674. public class DicomTime : DicomDateElement {
  675. #region Public Constructors
  676. public DicomTime(DicomTag tag, params DateTime[] values) : base(tag, PrivateDateFormats, values) {
  677. }
  678. public DicomTime(DicomTag tag, DicomDateRange range) : base(tag, PrivateDateFormats, range) {
  679. }
  680. public DicomTime(DicomTag tag, params string[] values) : base(tag, PrivateDateFormats, values) {
  681. }
  682. public DicomTime(DicomTag tag, IByteBuffer data) : base (tag, PrivateDateFormats, data) {
  683. }
  684. #endregion
  685. #region Public Properties
  686. public override DicomVR ValueRepresentation {
  687. get { return DicomVR.TM; }
  688. }
  689. private static string[] _formats;
  690. private static string[] PrivateDateFormats {
  691. get {
  692. if (_formats == null) {
  693. _formats = new string[37];
  694. _formats[0] = "HHmmss";
  695. _formats[1] = "HH";
  696. _formats[2] = "HHmm";
  697. _formats[3] = "HHmmssf";
  698. _formats[4] = "HHmmssff";
  699. _formats[5] = "HHmmssfff";
  700. _formats[6] = "HHmmssffff";
  701. _formats[7] = "HHmmssfffff";
  702. _formats[8] = "HHmmssffffff";
  703. _formats[9] = "HHmmss.f";
  704. _formats[10] = "HHmmss.ff";
  705. _formats[11] = "HHmmss.fff";
  706. _formats[12] = "HHmmss.ffff";
  707. _formats[13] = "HHmmss.fffff";
  708. _formats[14] = "HHmmss.ffffff";
  709. _formats[15] = "HH.mm";
  710. _formats[16] = "HH.mm.ss";
  711. _formats[17] = "HH.mm.ss.f";
  712. _formats[18] = "HH.mm.ss.ff";
  713. _formats[19] = "HH.mm.ss.fff";
  714. _formats[20] = "HH.mm.ss.ffff";
  715. _formats[21] = "HH.mm.ss.fffff";
  716. _formats[22] = "HH.mm.ss.ffffff";
  717. _formats[23] = "HH:mm";
  718. _formats[24] = "HH:mm:ss";
  719. _formats[25] = "HH:mm:ss:f";
  720. _formats[26] = "HH:mm:ss:ff";
  721. _formats[27] = "HH:mm:ss:fff";
  722. _formats[28] = "HH:mm:ss:ffff";
  723. _formats[29] = "HH:mm:ss:fffff";
  724. _formats[30] = "HH:mm:ss:ffffff";
  725. _formats[25] = "HH:mm:ss.f";
  726. _formats[26] = "HH:mm:ss.ff";
  727. _formats[27] = "HH:mm:ss.fff";
  728. _formats[28] = "HH:mm:ss.ffff";
  729. _formats[29] = "HH:mm:ss.fffff";
  730. _formats[30] = "HH:mm:ss.ffffff";
  731. }
  732. return _formats;
  733. }
  734. }
  735. #endregion
  736. }
  737. /// <summary>Unique Identifier (UI)</summary>
  738. public class DicomUniqueIdentifier : DicomMultiStringElement {
  739. #region Public Constructors
  740. public DicomUniqueIdentifier(DicomTag tag, params DicomUID[] values) : base(tag, values.Select(x => x.UID).ToArray()) {
  741. }
  742. public DicomUniqueIdentifier(DicomTag tag, params DicomTransferSyntax[] values) : base(tag, values.Select(x => x.UID.UID).ToArray()) {
  743. }
  744. public DicomUniqueIdentifier(DicomTag tag, IByteBuffer data) : base(tag, DicomEncoding.Default, data) {
  745. }
  746. #endregion
  747. #region Public Properties
  748. public override DicomVR ValueRepresentation {
  749. get { return DicomVR.UI; }
  750. }
  751. #endregion
  752. #region Public Members
  753. private DicomUID[] _values;
  754. public override T Get<T>(int item = -1) {
  755. if (_values == null) {
  756. _values = base.Get<string[]>().Select(x => DicomUID.Parse(x)).ToArray();
  757. }
  758. if (typeof(T) == typeof(DicomTransferSyntax)) {
  759. return (T)(object)DicomTransferSyntax.Lookup(_values[item]);
  760. }
  761. if (typeof(T) == typeof(DicomTransferSyntax[])) {
  762. return (T)(object)_values.Select(x => DicomTransferSyntax.Lookup(x)).ToArray();
  763. }
  764. if (typeof(T) == typeof(DicomUID) || typeof(T) == typeof(object)) {
  765. return (T)(object)_values[item];
  766. }
  767. if (typeof(T) == typeof(DicomUID[]) || typeof(T) == typeof(object[])) {
  768. return (T)(object)_values;
  769. }
  770. return base.Get<T>(item);
  771. }
  772. #endregion
  773. }
  774. /// <summary>Unsigned Long (UL)</summary>
  775. public class DicomUnsignedLong : DicomValueElement<uint> {
  776. #region Public Constructors
  777. public DicomUnsignedLong(DicomTag tag, params uint[] values) : base(tag, values) {
  778. }
  779. public DicomUnsignedLong(DicomTag tag, IByteBuffer data) : base(tag, data) {
  780. }
  781. #endregion
  782. #region Public Properties
  783. public override DicomVR ValueRepresentation {
  784. get { return DicomVR.UL; }
  785. }
  786. #endregion
  787. }
  788. /// <summary>Unknown (UN)</summary>
  789. public class DicomUnknown : DicomOtherByte {
  790. #region Public Constructors
  791. public DicomUnknown(DicomTag tag, params byte[] values) : base(tag, values) {
  792. }
  793. public DicomUnknown(DicomTag tag, IByteBuffer data) : base (tag, data) {
  794. }
  795. #endregion
  796. #region Public Properties
  797. public override DicomVR ValueRepresentation {
  798. get { return DicomVR.UN; }
  799. }
  800. #endregion
  801. }
  802. /// <summary>Unsigned Short (US)</summary>
  803. public class DicomUnsignedShort : DicomValueElement<ushort> {
  804. #region Public Constructors
  805. public DicomUnsignedShort(DicomTag tag, params ushort[] values) : base(tag, values) {
  806. }
  807. public DicomUnsignedShort(DicomTag tag, IByteBuffer data) : base (tag, data) {
  808. }
  809. #endregion
  810. #region Public Properties
  811. public override DicomVR ValueRepresentation {
  812. get { return DicomVR.US; }
  813. }
  814. #endregion
  815. }
  816. /// <summary>Unlimited Text (UT)</summary>
  817. public class DicomUnlimitedText : DicomStringElement {
  818. #region Public Constructors
  819. public DicomUnlimitedText(DicomTag tag, string value) : base(tag, value) {
  820. }
  821. public DicomUnlimitedText(DicomTag tag, Encoding encoding, string value) : base(tag, encoding, value) {
  822. }
  823. public DicomUnlimitedText(DicomTag tag, Encoding encoding, IByteBuffer data) : base(tag, encoding, data) {
  824. }
  825. #endregion
  826. #region Public Properties
  827. public override DicomVR ValueRepresentation {
  828. get { return DicomVR.UT; }
  829. }
  830. #endregion
  831. }
  832. }