/// <summary>Evaluates whether a DICOM item is of type Other*</summary> /// <param name="item"></param> /// <returns>A boolean flag indicating whether the item is of the expected type, otherwise false</returns> private static bool IsOtherElement(DicomItem item) { var t = item.GetType(); return(t == typeof(DicomOtherByte) || t == typeof(DicomOtherDouble) || t == typeof(DicomOtherFloat) || t == typeof(DicomOtherLong) || t == typeof(DicomOtherWord) || t == typeof(DicomUnknown)); }
/// <string>Replaces the content of a UID with a random one</string> /// <param name="dataset">Reference to the dataset</param> /// <param name="item"></param> private void ReplaceUID(DicomDataset dataset, DicomItem item) { if (!(item is DicomElement)) { return; } string rep; DicomUID uid; var old = ((DicomElement)item).Get <string>(); if (ReplacedUIDs.ContainsKey(old)) { rep = ReplacedUIDs[old]; uid = new DicomUID(rep, "Anonymized UID", DicomUidType.Unknown); } else { uid = DicomUIDGenerator.GenerateDerivedFromUUID(); rep = uid.UID; ReplacedUIDs[old] = rep; } var newItem = new DicomUniqueIdentifier(item.Tag, uid); dataset.AddOrUpdate(newItem); }
/// <summary> /// Use reflection to get strongly-typed constructor info from <paramref name="item"/>. /// </summary> /// <param name="item">DICOM item for which to get constructor.</param> /// <param name="parameterTypes">Expected parameter types in the requested constructor.</param> /// <returns>Constructor info corresponding to <paramref name="item"/> and <paramref name="parameterTypes"/>.</returns> private static ConstructorInfo GetConstructor(DicomItem item, params Type[] parameterTypes) { return(item.GetType().GetTypeInfo().DeclaredConstructors.Single( ci => { var pars = ci.GetParameters().Select(par => par.ParameterType); return pars.SequenceEqual(parameterTypes); })); }
/// <summary>Blanks an item to value suitable for the concrete item type.</summary> /// <param name="dataset">Reference to the dataset</param> /// <param name="item">DICOM item subject to blanking.</param> /// <param name="nonZeroLength">Require that new value is non-zero length (dummy) value?</param> private static void BlankItem(DicomDataset dataset, DicomItem item, bool nonZeroLength) { var tag = item.Tag; if (item is DicomSequence) { dataset.AddOrUpdate <DicomDataset>(DicomVR.SQ, tag); return; } // Special date/time cases if (item is DicomDateTime) { dataset.AddOrUpdate(nonZeroLength ? new DicomDateTime(tag, DateTime.MinValue) : new DicomDateTime(tag, Array.Empty <string>())); return; } if (item is DicomDate) { dataset.AddOrUpdate(nonZeroLength ? new DicomDate(tag, DateTime.MinValue) : new DicomDate(tag, Array.Empty <string>())); return; } if (item is DicomTime) { dataset.AddOrUpdate(nonZeroLength ? new DicomTime(tag, DateTime.MinValue) : new DicomTime(tag, Array.Empty <string>())); return; } if (item is DicomStringElement stringElement) { dataset.AddOrUpdate(stringElement.ValueRepresentation, tag, string.Empty); return; } if (IsOtherElement(item)) // Replaces with an empty array { var ctor = GetConstructor(item, typeof(DicomTag), typeof(IByteBuffer)); var updated = (DicomItem)ctor.Invoke(new object[] { tag, EmptyBuffer.Value }); dataset.AddOrUpdate(updated); return; } var valueType = ElementValueType(item); // Replace with the default value if (valueType != null) { var ctor = GetConstructor(item, typeof(DicomTag), valueType); var updated = (DicomItem)ctor.Invoke(new[] { tag, Activator.CreateInstance(valueType) }); dataset.AddOrUpdate(updated); } }
/// <summary>Evaluates whether an element has a generic valueType</summary> /// <param name="item"></param> /// <returns>The data type if found, otherwise null</returns> private static Type ElementValueType(DicomItem item) { var t = item.GetType(); if (t.IsConstructedGenericType && t.GetGenericTypeDefinition() == typeof(DicomValueElement <>)) { return(t.GenericTypeArguments[0]); } return(null); }
/// <string>Replaces the content of an item.</string> /// <param name="dataset">Reference to the dataset</param> /// <param name="item">DICOM item for which the string value should be replaced.</param> /// <param name="newString">The replacement string.</param> private static void ReplaceString(DicomDataset dataset, DicomItem item, string newString) { dataset.AddOrUpdate(item.ValueRepresentation, item.Tag, newString); }