PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/DICOM/DicomDictionaryReader.cs

https://github.com/petnet/fo-dicom
C# | 91 lines | 75 code | 16 blank | 0 comment | 22 complexity | ca738d6bd4bdac556955088dbe67c8e0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. namespace Dicom {
  9. public enum DicomDictionaryFormat {
  10. XML
  11. }
  12. public class DicomDictionaryReader {
  13. private DicomDictionary _dict;
  14. private DicomDictionaryFormat _format;
  15. private Stream _stream;
  16. public DicomDictionaryReader(DicomDictionary dict, DicomDictionaryFormat format, Stream stream) {
  17. _dict = dict;
  18. _format = format;
  19. _stream = stream;
  20. }
  21. public void Process() {
  22. if (_format == DicomDictionaryFormat.XML)
  23. ReadDictionaryXML();
  24. }
  25. private void ReadDictionaryXML() {
  26. DicomDictionary dict = _dict;
  27. XDocument xdoc = XDocument.Load(_stream);
  28. IEnumerable<XElement> xdicts;
  29. if (xdoc.Root.Name == "dictionaries") {
  30. xdicts = xdoc.Root.Elements("dictionary");
  31. } else {
  32. XElement xdict = xdoc.Element("dictionary");
  33. if (xdict == null)
  34. throw new DicomDataException("Expected <dictionary> root node in DICOM dictionary.");
  35. List<XElement> dicts = new List<XElement>();
  36. dicts.Add(xdict);
  37. xdicts = dicts;
  38. }
  39. foreach (var xdict in xdicts) {
  40. XAttribute creator = xdict.Attribute("creator");
  41. if (creator != null && !String.IsNullOrEmpty(creator.Value)) {
  42. dict = dict[dict.GetPrivateCreator(creator.Value)];
  43. }
  44. foreach (XElement xentry in xdict.Elements("tag")) {
  45. string name = xentry.Value;
  46. if (xentry.Attribute("keyword") == null)
  47. continue;
  48. string keyword = xentry.Attribute("keyword").Value;
  49. List<DicomVR> vrs = new List<DicomVR>();
  50. XAttribute xvr = xentry.Attribute("vr");
  51. if (xvr != null && !String.IsNullOrEmpty(xvr.Value)) {
  52. string[] vra = xvr.Value.Split('_', '/', '\\', ',', '|');
  53. foreach (string vr in vra)
  54. vrs.Add(DicomVR.Parse(vr));
  55. } else
  56. vrs.Add(DicomVR.NONE);
  57. DicomVM vm = DicomVM.Parse(xentry.Attribute("vm").Value);
  58. bool retired = false;
  59. XAttribute xretired = xentry.Attribute("retired");
  60. if (xretired != null && !String.IsNullOrEmpty(xretired.Value) && Boolean.Parse(xretired.Value))
  61. retired = true;
  62. string group = xentry.Attribute("group").Value;
  63. string element = xentry.Attribute("element").Value;
  64. if (group.ToLower().Contains('x') || element.ToLower().Contains('x')) {
  65. DicomMaskedTag tag = DicomMaskedTag.Parse(group, element);
  66. dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray()));
  67. } else {
  68. DicomTag tag = DicomTag.Parse(group + "," + element);
  69. dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray()));
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }