public static Dictionary <uint, Element> ReadFile(BinaryReader reader) { var state = new ReaderState() { UseImplicitVr = false, IsImplicitVr = false, Reader = reader, }; reader.ReadBytes(128); var magicBytes = reader.ReadChars(4); var magicText = new string(magicBytes); if (magicText != "DICM") { Logger.Error("Fileformat not recognized!"); return(new Dictionary <uint, Element>()); } var dicomData = new Dictionary <uint, Element>(); while (reader.BaseStream.Position < reader.BaseStream.Length) { var groupId = reader.ReadUInt16(); var elementId = reader.ReadUInt16(); var combinedId = ElementTag.GetCombinedId(groupId, elementId); Logger.Debug("(0x{0:x4}, 0x{1:x4})", groupId, elementId); var element = ReadElement(state, groupId, elementId); if (element.Tag != null && element.Tag.VR != ValueTypes.Separator) { dicomData.Add(combinedId, element); } } return(dicomData); }
private static Element ReadElement(ReaderState state, ushort groupId, ushort elementId) { var combinedId = ElementTag.GetCombinedId(groupId, elementId); state.IsImplicitVr = state.UseImplicitVr && (groupId != 0x0002); if (!state.IsImplicitVr && groupId != 0xfffe) // Sequence items are always implicit - hence 0xfffe. { state.ExplicitVr = new string(state.Reader.ReadChars(2)); } // Always use Implicit VR if the tag is recognized in the dictionary. var vr = ValueTypes.Unknown; if (Dictionary.ContainsKey(combinedId)) { var elementTag = Dictionary[combinedId]; vr = elementTag.VR; Logger.Debug(elementTag.Name); } var value = new Value[0]; switch (vr) { case ValueTypes.String: value = ReadString(state); break; case ValueTypes.DoubleString: value = ReadDoubleString(state); break; case ValueTypes.UShort: value = ReadUShort(state); break; case ValueTypes.Bytes: value = ReadBytes(state); break; case ValueTypes.Item: value = ReadItem(state); break; case ValueTypes.Separator: value = ReadSeparator(state); break; default: ReadUnknown(state); break; } // TransferSyntaxUID - See if implicit is specified. We hate implicit because shit cannot always be understood! state.UseImplicitVr = (combinedId == ReverseDictionary["TransferSyntaxUID"]) && (value[0].Text == "1.2.840.10008.1.2"); var tag = Dictionary.ContainsKey(combinedId) ? Dictionary[combinedId] : null; return(new Element() { GroupId = groupId, ElementId = elementId, Value = value, Tag = tag }); }