/// <summary> /// Try to get VR for given string value. /// </summary> /// <param name="vr">String representation of VR</param> /// <param name="result">VR code</param> /// <returns>true if VR was successfully parsed, false otherwise</returns> public static bool TryParse(string vr, out DicomVR result) { bool valid; result = TryParse(vr, out valid); return(valid); }
private void ReadInlineBinary ( fo.DicomTag tag, fo.DicomVR vr, JsonTextReader reader, fo.DicomDataset dataset, int level ) { if (reader.Read( )) { fo.IO.Buffer.MemoryByteBuffer buffer = null; byte[] data = new byte[0]; string base64 = (string)reader.Value; if (!string.IsNullOrEmpty(base64)) { buffer = new fo.IO.Buffer.MemoryByteBuffer(System.Convert.FromBase64String(base64)); } if (tag == fo.DicomTag.PixelData && level == 0) { dataset.AddOrUpdatePixelData(vr, buffer, fo.DicomTransferSyntax.Parse(TransferSyntaxUID)); } else { dataset.AddOrUpdate <fo.IO.Buffer.IByteBuffer> (vr, tag, buffer); } } }
private void ReadBulkData ( fo.DicomTag tag, fo.DicomVR vr, JsonTextReader reader, fo.DicomDataset dataset, int level ) { fo.IO.Buffer.BulkDataUriByteBuffer data = null; if (reader.Read( )) { string uri = (string)reader.Value; if (!string.IsNullOrEmpty(uri)) { data = new fo.IO.Buffer.BulkDataUriByteBuffer(uri); } if (tag == fo.DicomTag.PixelData && level == 0) { dataset.AddOrUpdatePixelData(vr, data, fo.DicomTransferSyntax.Parse(TransferSyntaxUID)); } else { dataset.AddOrUpdate <fo.IO.Buffer.IByteBuffer> (vr, tag, data); } } }
protected virtual void WriteVR_Default(fo.DicomDataset ds, fo.DicomElement element, XmlWriter writer) { fo.DicomVR dicomVr = element.ValueRepresentation; for (int index = 0; index < element.Count; index++) { writer.WriteStartElement(Constants.ATTRIBUTE_VALUE_NAME); WriteNumberAttrib(writer, index); if (dicomVr.Equals(fo.DicomVR.AT)) { var atElement = ds.Get <fo.DicomElement> (element.Tag, null); var tagValue = atElement.Get <fo.DicomTag> ( ); string stringValue = tagValue.ToString("J", null); writer.WriteString(stringValue); } else { writer.WriteString(GetTrimmedString(ds.Get <string> (element.Tag, index, string.Empty))); } writer.WriteEndElement( ); } }
/// <summary> /// Get VR for given string value. /// </summary> /// <param name="vr">String representation of VR</param> /// <returns>VR</returns> public static DicomVR Parse(string vr) { DicomVR result = TryParse(vr, out bool valid); if (!valid) { throw new DicomDataException($"Unknown VR: '{vr}'"); } return(result); }
/// <summary> /// Get VR for given string value. /// </summary> /// <param name="vr">String representation of VR</param> /// <returns>VR</returns> public static DicomVR Parse(string vr) { bool valid; DicomVR result = TryParse(vr, out valid); if (!valid) { throw new DicomDataException(string.Format("Unknown VR: '{0}'", vr)); } return(result); }
/// <summary> /// Add or update the image pixel data element in the dataset /// </summary> /// <param name="vr">DICOM vr of the image pixel. For a PixelData element this value should be either DicomVR.OB or DicomVR.OW DICOM VR.</param> /// <param name="pixelData">An <see cref="IByteBuffer"/> that holds the image pixel data </param> /// <param name="transferSyntax">A DicomTransferSyntax object of the <paramref name="pixelData"/> parameter. /// If parameter is not provided (null), then the default TransferSyntax "ExplicitVRLittleEndian" will be applied to the dataset</param> /// <remarks>Use this method whenever you are attaching an external image pixel data to the dataset and provide the proper TransferSyntax</remarks> /// <returns>The dataset instance.</returns> public DicomDataset AddOrUpdatePixelData(DicomVR vr, IByteBuffer pixelData, DicomTransferSyntax transferSyntax = null) { this.AddOrUpdate(vr, DicomTag.PixelData, pixelData); if (null != transferSyntax) { InternalTransferSyntax = transferSyntax; } return(this); }
protected virtual void ReadDefaultVr ( fo.DicomTag tag, fo.DicomVR vr, JsonTextReader reader, fo.DicomDataset dataset, int level ) { //if VR was the first property we already read the right thing above, //otherwise we'll got it from the dictionary. Could it be defined after the value? switch (vr.Code) { case fo.DicomVRCode.SQ: { ReadVr_SQ(reader, tag, dataset, level); } break; case fo.DicomVRCode.PN: { ReadVr_PN(reader, tag, dataset); } break; default: { List <string> values = new List <string> ( ); while (reader.Read( ) && reader.TokenType == JsonToken.StartArray) { while (reader.Read( ) && reader.TokenType != JsonToken.EndArray) { values.Add(System.Convert.ToString(reader.Value)); } break; } if (tag == fo.DicomTag.TransferSyntaxUID) { TransferSyntaxUID = values.FirstOrDefault( ); } dataset.AddOrUpdate <string> (vr, tag, values.ToArray( )); } break; } }
private bool MatchVr(fo.DicomItem element) { fo.DicomVR elementVr = element.ValueRepresentation; if (!elementVr.Equals(fo.DicomVR.DA) && !elementVr.Equals(fo.DicomVR.TM) && !elementVr.Equals(fo.DicomVR.DT)) { return(false); } if (HasWildcardMatching(element.ToString( ))) { return(false); } return(true); }
protected virtual void WriteVR_Default ( fo.DicomDataset ds, fo.DicomElement element, JsonWriter writer, fo.DicomVR dicomVr ) { writer.WritePropertyName(JsonConstants.ValueField); writer.WriteStartArray( ); for (int index = 0; index < element.Count; index++) { string stringValue = GetTrimmedString(element.Get <string> (index)); if (_numberBasedVrs.Contains(element.ValueRepresentation.Code)) { //parse with the greatest type that can handle //need to do that to remove the ' ' around the string if (_decimalBasedVrs.Contains(element.ValueRepresentation.Code)) { writer.WriteValue(double.Parse(stringValue, System.Globalization.NumberStyles.Any)); } else { writer.WriteValue(long.Parse(stringValue, System.Globalization.NumberStyles.Number)); } } else { writer.WriteValue(stringValue); } } writer.WriteEndArray( ); }
/// <summary> /// Add single DICOM item given by <paramref name="tag"/> and <paramref name="values"/>. /// </summary> /// <typeparam name="T">Type of added values.</typeparam> /// <param name="tag">DICOM tag of the added item.</param> /// <param name="values">Values of the added item.</param> /// <param name="allowUpdate">True if existing tag can be updated, false if method should throw when trying to add already existing tag.</param> /// <returns>The dataset instance.</returns> private DicomDataset DoAdd <T>(DicomTag tag, IList <T> values, bool allowUpdate) { var entry = DicomDictionary.Default[tag]; if (entry == null) { throw new DicomDataException( "Tag {0} not found in DICOM dictionary. Only dictionary tags may be added implicitly to the dataset.", tag); } DicomVR vr = null; if (values != null) { vr = entry.ValueRepresentations.FirstOrDefault(x => x.ValueType == typeof(T)); } if (vr == null) { vr = entry.ValueRepresentations.First(); } return(DoAdd(vr, tag, values, allowUpdate)); }
/// <summary> /// Add single DICOM item given by <paramref name="vr"/>, <paramref name="tag"/> and <paramref name="values"/>. /// </summary> /// <typeparam name="T">Type of added values.</typeparam> /// <param name="vr">DICOM vr of the added item. Use when setting a private element.</param> /// <param name="tag">DICOM tag of the added item.</param> /// <param name="values">Values of the added item.</param> /// <param name="allowUpdate">True if existing tag can be updated, false if method should throw when trying to add already existing tag.</param> /// <remarks>No validation is performed on the <paramref name="vr"/> matching the element <paramref name="tag"/> /// This method is useful when adding a private tag and need to explicitly set the VR of the created element. /// </remarks> /// <returns>The dataset instance.</returns> private DicomDataset DoAdd <T>(DicomVR vr, DicomTag tag, IList <T> values, bool allowUpdate) { if (vr == DicomVR.AE) { if (values == null) { return(DoAdd(new DicomApplicationEntity(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomApplicationEntity(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.AS) { if (values == null) { return(DoAdd(new DicomAgeString(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomAgeString(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.AT) { if (values == null) { return(DoAdd(new DicomAttributeTag(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(DicomTag)) { return(DoAdd(new DicomAttributeTag(tag, values.Cast <DicomTag>().ToArray()), allowUpdate)); } IEnumerable <DicomTag> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, DicomTag.Parse, out parsedValues)) { return(DoAdd(new DicomAttributeTag(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.CS) { if (values == null) { return(DoAdd(new DicomCodeString(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomCodeString(tag, values.Cast <string>().ToArray()), allowUpdate)); } if (typeof(T).GetTypeInfo().IsEnum) { return(DoAdd(new DicomCodeString(tag, values.Select(x => x.ToString()).ToArray()), allowUpdate)); } } if (vr == DicomVR.DA) { if (values == null) { return(DoAdd(new DicomDate(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(DateTime)) { return(DoAdd(new DicomDate(tag, values.Cast <DateTime>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(DicomDateRange)) { return (DoAdd(new DicomDate(tag, values.Cast <DicomDateRange>().FirstOrDefault() ?? new DicomDateRange()), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomDate(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.DS) { if (values == null) { return(DoAdd(new DicomDecimalString(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(decimal)) { return(DoAdd(new DicomDecimalString(tag, values.Cast <decimal>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomDecimalString(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.DT) { if (values == null) { return(DoAdd(new DicomDateTime(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(DateTime)) { return(DoAdd(new DicomDateTime(tag, values.Cast <DateTime>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(DicomDateRange)) { return (DoAdd( new DicomDateTime( tag, values.Cast <DicomDateRange>().FirstOrDefault() ?? new DicomDateRange()), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomDateTime(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.FD) { if (values == null) { return(DoAdd(new DicomFloatingPointDouble(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(double)) { return(DoAdd(new DicomFloatingPointDouble(tag, values.Cast <double>().ToArray()), allowUpdate)); } IEnumerable <double> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, double.Parse, out parsedValues)) { return(DoAdd(new DicomFloatingPointDouble(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.FL) { if (values == null) { return(DoAdd(new DicomFloatingPointSingle(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(float)) { return(DoAdd(new DicomFloatingPointSingle(tag, values.Cast <float>().ToArray()), allowUpdate)); } IEnumerable <float> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, float.Parse, out parsedValues)) { return(DoAdd(new DicomFloatingPointSingle(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.IS) { if (values == null) { return(DoAdd(new DicomIntegerString(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(int)) { return(DoAdd(new DicomIntegerString(tag, values.Cast <int>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomIntegerString(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.LO) { if (values == null) { return(DoAdd(new DicomLongString(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomLongString(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.LT) { if (values == null) { return(DoAdd(new DicomLongText(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomLongText(tag, values.Cast <string>().First()), allowUpdate)); } } if (vr == DicomVR.OB) { if (values == null) { return(DoAdd(new DicomOtherByte(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(byte)) { return(DoAdd(new DicomOtherByte(tag, values.Cast <byte>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(IByteBuffer) && values.Count == 1) { return(DoAdd(new DicomOtherByte(tag, (IByteBuffer)values[0]), allowUpdate)); } } if (vr == DicomVR.OD) { if (values == null) { return(DoAdd(new DicomOtherDouble(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(double)) { return(DoAdd(new DicomOtherDouble(tag, values.Cast <double>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(IByteBuffer) && values.Count == 1) { return(DoAdd(new DicomOtherDouble(tag, (IByteBuffer)values[0]), allowUpdate)); } } if (vr == DicomVR.OF) { if (values == null) { return(DoAdd(new DicomOtherFloat(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(float)) { return(DoAdd(new DicomOtherFloat(tag, values.Cast <float>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(IByteBuffer) && values.Count == 1) { return(DoAdd(new DicomOtherFloat(tag, (IByteBuffer)values[0]), allowUpdate)); } } if (vr == DicomVR.OL) { if (values == null) { return(DoAdd(new DicomOtherLong(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(uint)) { return(DoAdd(new DicomOtherLong(tag, values.Cast <uint>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(IByteBuffer) && values.Count == 1) { return(DoAdd(new DicomOtherLong(tag, (IByteBuffer)values[0]), allowUpdate)); } } if (vr == DicomVR.OW) { if (values == null) { return(DoAdd(new DicomOtherWord(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(ushort)) { return(DoAdd(new DicomOtherWord(tag, values.Cast <ushort>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(IByteBuffer) && values.Count == 1) { return(DoAdd(new DicomOtherWord(tag, (IByteBuffer)values[0]), allowUpdate)); } } if (vr == DicomVR.PN) { if (values == null) { return(DoAdd(new DicomPersonName(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomPersonName(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.SH) { if (values == null) { return(DoAdd(new DicomShortString(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomShortString(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.SL) { if (values == null) { return(DoAdd(new DicomSignedLong(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(int)) { return(DoAdd(new DicomSignedLong(tag, values.Cast <int>().ToArray()), allowUpdate)); } IEnumerable <int> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, int.Parse, out parsedValues)) { return(DoAdd(new DicomSignedLong(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.SQ) { if (values == null) { return(DoAdd(new DicomSequence(tag), allowUpdate)); } if (typeof(T) == typeof(DicomContentItem)) { return(DoAdd(new DicomSequence(tag, values.Cast <DicomContentItem>().Select(x => x.Dataset).ToArray()), allowUpdate)); } if (typeof(T) == typeof(DicomDataset) || typeof(T) == typeof(DicomCodeItem) || typeof(T) == typeof(DicomMeasuredValue) || typeof(T) == typeof(DicomReferencedSOP)) { return(DoAdd(new DicomSequence(tag, values.Cast <DicomDataset>().ToArray()), allowUpdate)); } } if (vr == DicomVR.SS) { if (values == null) { return(DoAdd(new DicomSignedShort(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(short)) { return(DoAdd(new DicomSignedShort(tag, values.Cast <short>().ToArray()), allowUpdate)); } IEnumerable <short> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, short.Parse, out parsedValues)) { return(DoAdd(new DicomSignedShort(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.ST) { if (values == null) { return(DoAdd(new DicomShortText(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomShortText(tag, values.Cast <string>().First()), allowUpdate)); } } if (vr == DicomVR.TM) { if (values == null) { return(DoAdd(new DicomTime(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(DateTime)) { return(DoAdd(new DicomTime(tag, values.Cast <DateTime>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(DicomDateRange)) { return (DoAdd(new DicomTime(tag, values.Cast <DicomDateRange>().FirstOrDefault() ?? new DicomDateRange()), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomTime(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.UC) { if (values == null) { return(DoAdd(new DicomUnlimitedCharacters(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomUnlimitedCharacters(tag, values.Cast <string>().ToArray()), allowUpdate)); } } if (vr == DicomVR.UI) { if (values == null) { return(DoAdd(new DicomUniqueIdentifier(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomUniqueIdentifier(tag, values.Cast <string>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(DicomUID)) { return(DoAdd(new DicomUniqueIdentifier(tag, values.Cast <DicomUID>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(DicomTransferSyntax)) { return(DoAdd(new DicomUniqueIdentifier(tag, values.Cast <DicomTransferSyntax>().ToArray()), allowUpdate)); } } if (vr == DicomVR.UL) { if (values == null) { return(DoAdd(new DicomUnsignedLong(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(uint)) { return(DoAdd(new DicomUnsignedLong(tag, values.Cast <uint>().ToArray()), allowUpdate)); } IEnumerable <uint> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, uint.Parse, out parsedValues)) { return(DoAdd(new DicomUnsignedLong(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.UN) { if (values == null) { return(DoAdd(new DicomUnknown(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(byte)) { return(DoAdd(new DicomUnknown(tag, values.Cast <byte>().ToArray()), allowUpdate)); } if (typeof(T) == typeof(IByteBuffer) && values.Count == 1) { return(DoAdd(new DicomUnknown(tag, (IByteBuffer)values[0]), allowUpdate)); } } if (vr == DicomVR.UR) { if (values == null) { return(DoAdd(new DicomUniversalResource(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomUniversalResource(tag, values.Cast <string>().First()), allowUpdate)); } } if (vr == DicomVR.US) { if (values == null) { return(DoAdd(new DicomUnsignedShort(tag, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(ushort)) { return(DoAdd(new DicomUnsignedShort(tag, values.Cast <ushort>().ToArray()), allowUpdate)); } IEnumerable <ushort> parsedValues; if (ParseVrValueFromString(values, tag.DictionaryEntry.ValueMultiplicity, ushort.Parse, out parsedValues)) { return(DoAdd(new DicomUnsignedShort(tag, parsedValues.ToArray()), allowUpdate)); } } if (vr == DicomVR.UT) { if (values == null) { return(DoAdd(new DicomUnlimitedText(tag, DicomEncoding.Default, EmptyBuffer.Value), allowUpdate)); } if (typeof(T) == typeof(string)) { return(DoAdd(new DicomUnlimitedText(tag, values.Cast <string>().First()), allowUpdate)); } } throw new InvalidOperationException( $"Unable to create DICOM element of type {vr.Code} with values of type {typeof(T)}"); }
/// <summary> /// Add or update a single DICOM item given by <paramref name="vr"/>, <paramref name="tag"/> and <paramref name="values"/>. /// </summary> /// <typeparam name="T">Type of added values.</typeparam> /// <param name="vr">DICOM vr of the added item. Use when setting a private element.</param> /// <param name="tag">DICOM tag of the added item.</param> /// <param name="values">Values of the added item.</param> /// <remarks>No validation is performed on the <paramref name="vr"/> matching the element <paramref name="tag"/> /// This method is useful when adding a private tag and need to explicitly set the VR of the created element. /// </remarks> /// <returns>The dataset instance.</returns> public DicomDataset AddOrUpdate <T>(DicomVR vr, DicomTag tag, params T[] values) { return(DoAdd(vr, tag, values, true)); }
protected virtual void WriteDicomAttribute ( fo.DicomDataset ds, fo.DicomItem element, XmlWriter writer ) { //group length element must not be written if (null == element || element.Tag.Element == 0x0000) { return; } fo.DicomVR dicomVr = element.ValueRepresentation; writer.WriteStartElement(Constants.ATTRIBUTE_NAME); writer.WriteAttributeString(Constants.ATTRIBUTE_KEYWORD, element.Tag.DictionaryEntry.Keyword); writer.WriteAttributeString(Constants.ATTRIBUTE_TAG, element.Tag.ToString("J", null)); writer.WriteAttributeString(Constants.ATTRIBUTE_VR, element.ValueRepresentation.Code.ToUpper( )); if (element.Tag.IsPrivate && null != element.Tag.PrivateCreator) { writer.WriteAttributeString(Constants.ATTRIBUTE_PRIVATE_CREATOR, element.Tag.PrivateCreator.Creator); } switch (element.ValueRepresentation.Code) { case fo.DicomVRCode.SQ: { WriteVR_SQ((fo.DicomSequence)element, writer); } break; case fo.DicomVRCode.PN: { WriteVR_PN((fo.DicomElement)element, writer); } break; case fo.DicomVRCode.OB: case fo.DicomVRCode.OD: case fo.DicomVRCode.OF: case fo.DicomVRCode.OW: case fo.DicomVRCode.OL: case fo.DicomVRCode.UN: { WriteVR_Binary(element, writer); } break; default: { WriteVR_Default(ds, (fo.DicomElement)element, writer); } break; } writer.WriteEndElement( ); }
private void ReadDictionaryXML() { DicomDictionary dict = _dict; XDocument xdoc = XDocument.Load(_stream); IEnumerable <XElement> xdicts; if (xdoc.Root.Name == "dictionaries") { xdicts = xdoc.Root.Elements("dictionary"); } else { XElement xdict = xdoc.Element("dictionary"); if (xdict == null) { throw new DicomDataException("Expected <dictionary> root node in DICOM dictionary."); } List <XElement> dicts = new List <XElement>(); dicts.Add(xdict); xdicts = dicts; } foreach (var xdict in xdicts) { XAttribute creator = xdict.Attribute("creator"); if (creator != null && !String.IsNullOrEmpty(creator.Value)) { dict = _dict[_dict.GetPrivateCreator(creator.Value)]; } else { dict = _dict; } foreach (XElement xentry in xdict.Elements("tag")) { string name = xentry.Value ?? "Unknown"; string keyword = String.Empty; if (xentry.Attribute("keyword") != null) { keyword = xentry.Attribute("keyword").Value; } List <DicomVR> vrs = new List <DicomVR>(); XAttribute xvr = xentry.Attribute("vr"); if (xvr != null && !String.IsNullOrEmpty(xvr.Value)) { string[] vra = xvr.Value.Split('_', '/', '\\', ',', '|'); foreach (string vr in vra) { vrs.Add(DicomVR.Parse(vr)); } } else { vrs.Add(DicomVR.NONE); } DicomVM vm = DicomVM.Parse(xentry.Attribute("vm").Value); bool retired = false; XAttribute xretired = xentry.Attribute("retired"); if (xretired != null && !String.IsNullOrEmpty(xretired.Value) && Boolean.Parse(xretired.Value)) { retired = true; } string group = xentry.Attribute("group").Value; string element = xentry.Attribute("element").Value; if (group.ToLower().Contains("x") || element.ToLower().Contains("x")) { DicomMaskedTag tag = DicomMaskedTag.Parse(group, element); tag.Tag.PrivateCreator = dict.PrivateCreator; dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray())); } else { DicomTag tag = DicomTag.Parse(group + "," + element); tag.PrivateCreator = dict.PrivateCreator; dict.Add(new DicomDictionaryEntry(tag, name, keyword, vm, retired, vrs.ToArray())); } } } }
/// <summary> /// Add single DICOM item given by <paramref name="vr"/>, <paramref name="tag"/> and <paramref name="values"/>. /// </summary> /// <typeparam name="T">Type of added values.</typeparam> /// <param name="vr">DICOM vr of the added item. Use when setting a private element.</param> /// <param name="tag">DICOM tag of the added item.</param> /// <param name="values">Values of the added item.</param> /// <remarks>No validation is performed on the <paramref name="vr"/> matching the element <paramref name="tag"/> /// This method is useful when adding a private tag and need to explicitly set the VR of the created element. /// </remarks> /// <returns>The dataset instance.</returns> /// <exception cref="ArgumentException">If tag already exists in dataset.</exception> public DicomDataset Add <T>(DicomVR vr, DicomTag tag, params T[] values) { return(DoAdd(vr, tag, values, false)); }
/// <summary> /// Try to get VR for given string value. /// </summary> /// <param name="vr">String representation of VR</param> /// <param name="result">VR code</param> /// <returns>true if VR was successfully parsed, false otherwise</returns> public static bool TryParse(string vr, out DicomVR result) { bool valid; result = TryParse(vr, out valid); return valid; }
public DicomValidationException(string content, DicomVR vr, string message) : base(message) { Content = content; VR = vr; }
public static bool IsBinaryVR(fo.DicomVR dicomVr) { return(dicomVr == fo.DicomVR.OB || dicomVr == fo.DicomVR.OD || dicomVr == fo.DicomVR.OF || dicomVr == fo.DicomVR.OW || dicomVr == fo.DicomVR.OL || dicomVr == fo.DicomVR.UN); }
public void Parse_OF_ReturnsVRObject() { var actual = DicomVR.Parse("OF"); Assert.IsType <DicomVR>(actual); }
public DicomDataset Add <T>(DicomTag tag, params T[] values) { var entry = DicomDictionary.Default[tag]; if (entry == null) { throw new DicomDataException("Tag {0} not found in DICOM dictionary. Only dictionary tags may be added implicitly to the dataset.", tag); } DicomVR vr = null; if (values != null) { vr = entry.ValueRepresentations.FirstOrDefault(x => x.ValueType == typeof(T)); } if (vr == null) { vr = entry.ValueRepresentations.First(); } if (vr == DicomVR.AE) { if (values == null) { return(Add(new DicomApplicationEntity(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomApplicationEntity(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.AS) { if (values == null) { return(Add(new DicomAgeString(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomAgeString(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.AT) { if (values == null) { return(Add(new DicomAttributeTag(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(DicomTag)) { return(Add(new DicomAttributeTag(tag, values.Cast <DicomTag>().ToArray()))); } } if (vr == DicomVR.CS) { if (values == null) { return(Add(new DicomCodeString(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomCodeString(tag, values.Cast <string>().ToArray()))); } if (typeof(T).IsEnum) { return(Add(new DicomCodeString(tag, values.Select(x => x.ToString()).ToArray()))); } } if (vr == DicomVR.DA) { if (values == null) { return(Add(new DicomDate(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(DateTime)) { return(Add(new DicomDate(tag, values.Cast <DateTime>().ToArray()))); } if (typeof(T) == typeof(DicomDateRange)) { return(Add(new DicomDate(tag, values.Cast <DicomDateRange>().FirstOrDefault() ?? new DicomDateRange()))); } if (typeof(T) == typeof(string)) { return(Add(new DicomDate(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.DS) { if (values == null) { return(Add(new DicomDecimalString(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(decimal)) { return(Add(new DicomDecimalString(tag, values.Cast <decimal>().ToArray()))); } if (typeof(T) == typeof(string)) { return(Add(new DicomDecimalString(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.DT) { if (values == null) { return(Add(new DicomDateTime(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(DateTime)) { return(Add(new DicomDateTime(tag, values.Cast <DateTime>().ToArray()))); } if (typeof(T) == typeof(DicomDateRange)) { return(Add(new DicomDateTime(tag, values.Cast <DicomDateRange>().FirstOrDefault() ?? new DicomDateRange()))); } if (typeof(T) == typeof(string)) { return(Add(new DicomDateTime(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.FD) { if (values == null) { return(Add(new DicomFloatingPointDouble(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(double)) { return(Add(new DicomFloatingPointDouble(tag, values.Cast <double>().ToArray()))); } } if (vr == DicomVR.FL) { if (values == null) { return(Add(new DicomFloatingPointSingle(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(float)) { return(Add(new DicomFloatingPointSingle(tag, values.Cast <float>().ToArray()))); } } if (vr == DicomVR.IS) { if (values == null) { return(Add(new DicomIntegerString(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(int)) { return(Add(new DicomIntegerString(tag, values.Cast <int>().ToArray()))); } if (typeof(T) == typeof(string)) { return(Add(new DicomIntegerString(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.LO) { if (values == null) { return(Add(new DicomLongString(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomLongString(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.LT) { if (values == null) { return(Add(new DicomLongText(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomLongText(tag, values.Cast <string>().First()))); } } if (vr == DicomVR.OB) { if (values == null) { return(Add(new DicomOtherByte(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(byte)) { return(Add(new DicomOtherByte(tag, values.Cast <byte>().ToArray()))); } } if (vr == DicomVR.OD) { if (values == null) { return(Add(new DicomOtherDouble(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(double)) { return(Add(new DicomOtherDouble(tag, values.Cast <double>().ToArray()))); } } if (vr == DicomVR.OF) { if (values == null) { return(Add(new DicomOtherFloat(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(float)) { return(Add(new DicomOtherFloat(tag, values.Cast <float>().ToArray()))); } } if (vr == DicomVR.OW) { if (values == null) { return(Add(new DicomOtherWord(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(ushort)) { return(Add(new DicomOtherWord(tag, values.Cast <ushort>().ToArray()))); } } if (vr == DicomVR.PN) { if (values == null) { return(Add(new DicomPersonName(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomPersonName(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.SH) { if (values == null) { return(Add(new DicomShortString(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomShortString(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.SL) { if (values == null) { return(Add(new DicomSignedLong(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(int)) { return(Add(new DicomSignedLong(tag, values.Cast <int>().ToArray()))); } } if (vr == DicomVR.SQ) { if (values == null) { return(Add(new DicomSequence(tag))); } if (typeof(T) == typeof(DicomContentItem)) { return(Add(new DicomSequence(tag, values.Cast <DicomContentItem>().Select(x => x.Dataset).ToArray()))); } if (typeof(T) == typeof(DicomDataset) || typeof(T) == typeof(DicomCodeItem) || typeof(T) == typeof(DicomMeasuredValue) || typeof(T) == typeof(DicomReferencedSOP)) { return(Add(new DicomSequence(tag, values.Cast <DicomDataset>().ToArray()))); } } if (vr == DicomVR.SS) { if (values == null) { return(Add(new DicomSignedShort(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(short)) { return(Add(new DicomSignedShort(tag, values.Cast <short>().ToArray()))); } } if (vr == DicomVR.ST) { if (values == null) { return(Add(new DicomShortText(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomShortText(tag, values.Cast <string>().First()))); } } if (vr == DicomVR.TM) { if (values == null) { return(Add(new DicomTime(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(DateTime)) { return(Add(new DicomTime(tag, values.Cast <DateTime>().ToArray()))); } if (typeof(T) == typeof(DicomDateRange)) { return(Add(new DicomTime(tag, values.Cast <DicomDateRange>().FirstOrDefault() ?? new DicomDateRange()))); } if (typeof(T) == typeof(string)) { return(Add(new DicomTime(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.UC) { if (values == null) { return(Add(new DicomUnlimitedCharacters(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomUnlimitedCharacters(tag, values.Cast <string>().ToArray()))); } } if (vr == DicomVR.UI) { if (values == null) { return(Add(new DicomUniqueIdentifier(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomUniqueIdentifier(tag, values.Cast <string>().ToArray()))); } if (typeof(T) == typeof(DicomUID)) { return(Add(new DicomUniqueIdentifier(tag, values.Cast <DicomUID>().ToArray()))); } if (typeof(T) == typeof(DicomTransferSyntax)) { return(Add(new DicomUniqueIdentifier(tag, values.Cast <DicomTransferSyntax>().ToArray()))); } } if (vr == DicomVR.UL) { if (values == null) { return(Add(new DicomUnsignedLong(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(uint)) { return(Add(new DicomUnsignedLong(tag, values.Cast <uint>().ToArray()))); } } if (vr == DicomVR.UN) { if (values == null) { return(Add(new DicomUnknown(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(byte)) { return(Add(new DicomUnknown(tag, values.Cast <byte>().ToArray()))); } } if (vr == DicomVR.UR) { if (values == null) { return(Add(new DicomUniversalResource(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomUniversalResource(tag, values.Cast <string>().First()))); } } if (vr == DicomVR.US) { if (values == null) { return(Add(new DicomUnsignedShort(tag, EmptyBuffer.Value))); } if (typeof(T) == typeof(ushort)) { return(Add(new DicomUnsignedShort(tag, values.Cast <ushort>().ToArray()))); } } if (vr == DicomVR.UT) { if (values == null) { return(Add(new DicomUnlimitedText(tag, DicomEncoding.Default, EmptyBuffer.Value))); } if (typeof(T) == typeof(string)) { return(Add(new DicomUnlimitedText(tag, values.Cast <string>().First()))); } } throw new InvalidOperationException(String.Format("Unable to create DICOM element of type {0} with values of type {1}", vr.Code, typeof(T).ToString())); }
private void ReadElement ( fo.DicomDataset ds, XElement element, fo.DicomTag tag, fo.DicomVR dicomVr, int level ) { if (dicomVr == fo.DicomVR.PN) { string personNameValue = ""; foreach (var personNameElementValue in element.Elements( ).OrderBy(n => n.Attribute(Constants.ATTRIBUTE_NUMBER))) { foreach (var personNameComponent in personNameElementValue.Elements( )) { if (personNameComponent.Name == Utilities.PersonNameComponents.PN_COMP_ALPHABETIC || personNameComponent.Name == Utilities.PersonNameComponents.PN_COMP_IDEOGRAPHIC || personNameComponent.Name == Utilities.PersonNameComponents.PN_COMP_PHONETIC) { personNameValue = UpdatePersonName(personNameValue, personNameComponent, Utilities.PersonNameParts.PN_Family); personNameValue = UpdatePersonName(personNameValue, personNameComponent, Utilities.PersonNameParts.PN_Given); personNameValue = UpdatePersonName(personNameValue, personNameComponent, Utilities.PersonNameParts.PN_Midlle); personNameValue = UpdatePersonName(personNameValue, personNameComponent, Utilities.PersonNameParts.PN_Prefix); personNameValue = UpdatePersonName(personNameValue, personNameComponent, Utilities.PersonNameParts.PN_Suffix, true); personNameValue = personNameValue.TrimEnd('^'); // extra cleanup personNameValue += "="; } } personNameValue = personNameValue.TrimEnd('='); personNameValue += "\\"; } personNameValue = personNameValue.TrimEnd('\\'); ds.AddOrUpdate <string> (dicomVr, tag, personNameValue); } else if (Utilities.IsBinaryVR(dicomVr)) { var dataElement = element.Elements( ).OfType <XElement> ( ).FirstOrDefault( ); if (null != dataElement) { fo.IO.Buffer.IByteBuffer data; if (dataElement.Name == Constants.ELEMENT_BULKDATA) { string uri = dataElement.Attribute(Constants.ATTRIBUTE_BULKDATAURI).Value; data = new fo.IO.Buffer.BulkDataUriByteBuffer(uri); } else { var base64 = System.Convert.FromBase64String(dataElement.Value); data = new fo.IO.Buffer.MemoryByteBuffer(base64); } if (tag == fo.DicomTag.PixelData && level == 0) { ds.AddOrUpdatePixelData(dicomVr, data, TransferSyntax); } else { ds.AddOrUpdate <fo.IO.Buffer.IByteBuffer> (dicomVr, tag, data); } } } else { var values = ReadValue(element); if (tag == fo.DicomTag.TransferSyntaxUID) { TransferSyntax = fo.DicomTransferSyntax.Parse(values.FirstOrDefault( )); } ds.AddOrUpdate <string> (dicomVr, tag, values.ToArray( )); } }
protected virtual void WriteDicomItem ( fo.DicomDataset ds, fo.DicomItem element, JsonWriter writer ) { //group length element must not be written if (null == element || element.Tag.Element == 0x0000) { return; } fo.DicomVR dicomVr = element.ValueRepresentation; writer.WritePropertyName(element.Tag.Group.ToString("X4") + element.Tag.Element.ToString("X4")); writer.WriteStartObject( ); //writer.WritePropertyName ( "temp" ); //writer.WriteValue ( element.Tag.DictionaryEntry.Keyword ); writer.WritePropertyName("vr"); writer.WriteValue(element.ValueRepresentation.Code); switch (element.ValueRepresentation.Code) { case fo.DicomVRCode.SQ: { WriteVR_SQ((fo.DicomSequence)element, writer); } break; case fo.DicomVRCode.PN: { WriteVR_PN((fo.DicomElement)element, writer); } break; case fo.DicomVRCode.OB: case fo.DicomVRCode.OD: case fo.DicomVRCode.OF: case fo.DicomVRCode.OW: case fo.DicomVRCode.OL: case fo.DicomVRCode.UN: { WriteVR_Binary(element, writer); } break; default: { WriteVR_Default(ds, (fo.DicomElement)element, writer, dicomVr); } break; } writer.WriteEndObject( ); }