Esempio n. 1
0
        /// <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));
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        public override bool CanMatch(fo.DicomItem item)
        {
            //if SQ casting will fail and be null
            fo.DicomElement element = item as fo.DicomElement;


            if (element == null || element.Count == 0)
            {
                return(false);
            }

            string elementValue = element.ToString( );

            if (element.ValueRepresentation.Equals(fo.DicomVR.DA) ||
                element.ValueRepresentation.Equals(fo.DicomVR.DT) ||
                element.ValueRepresentation.Equals(fo.DicomVR.TM))
            {
                if (elementValue.Contains("-"))
                {
                    return(false);
                }
            }
            else
            {
                if (HasWildcardMatching(elementValue))
                {
                    return(false);
                }
            }

            return(base.CanMatch(element));
        }
Esempio n. 4
0
        /// <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, new string[0]));
                return;
            }
            if (item is DicomDate)
            {
                dataset.AddOrUpdate(nonZeroLength
                    ? new DicomDate(tag, DateTime.MinValue)
                    : new DicomDate(tag, new string[0]));
                return;
            }
            if (item is DicomTime)
            {
                dataset.AddOrUpdate(nonZeroLength
                    ? new DicomTime(tag, DateTime.MinValue)
                    : new DicomTime(tag, new string[0]));
                return;
            }

            var stringElement = item as DicomStringElement;

            if (stringElement != null)
            {
                dataset.AddOrUpdate(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);
            }
        }
Esempio n. 5
0
        public override bool CanMatch(fo.DicomItem element)
        {
            if (!element.ValueRepresentation.Equals(fo.DicomVR.SQ))
            {
                return(false);
            }

            return(base.CanMatch(element));
        }
Esempio n. 6
0
        public virtual bool CanMatch(fo.DicomItem element)
        {
            if (SupportedTags.Count > 0)
            {
                return(SupportedTags.Contains((uint)element.Tag));
            }

            return(true);
        }
Esempio n. 7
0
        public virtual bool IsSupported(fo.DicomItem element)
        {
            if (null != SupportedTags && SupportedTags.Count > 0)
            {
                return(SupportedTags.Contains((uint)element.Tag));
            }

            return(true);
        }
Esempio n. 8
0
        public override bool CanMatch(fo.DicomItem item)
        {
            fo.DicomElement element = item as fo.DicomElement;

            if (null == element || element.Count > 0)
            {
                return(false);
            }

            return(base.CanMatch(element));
        }
Esempio n. 9
0
        /// <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)
        {
#if PORTABLE || NETSTANDARD || NETFX_CORE
            return(item.GetType().GetTypeInfo().DeclaredConstructors.Single(
                       ci =>
            {
                var pars = ci.GetParameters().Select(par => par.ParameterType);
                return pars.SequenceEqual(parameterTypes);
            }));
#else
            return(item.GetType().GetConstructor(parameterTypes));
#endif
        }
Esempio n. 10
0
        public override void SetElement(fo.DicomItem element)
        {
            base.SetElement(element);

            if (element.ValueRepresentation == fo.DicomVR.DA)
            {
                DateElement = element;
            }

            if (element.ValueRepresentation == fo.DicomVR.TM)
            {
                TimeElement = element;
            }
        }
Esempio n. 11
0
        public override bool CanMatch(fo.DicomItem element)
        {
            //if ( IsRangeSupported && !DateTimeMatching.IsSupported(element.Tag.TagValue ) )
            //{
            //    return false ;
            //}

            if (!MatchVr(element))
            {
                return(false);
            }

            return(base.CanMatch(element));
        }
Esempio n. 12
0
        public override bool CanMatch(fo.DicomItem element)
        {
            if (_invliadVrs.Contains(element.ValueRepresentation))
            {
                return(false);
            }

            if (!HasWildcardMatching(element.ToString( )))
            {
                return(false);
            }

            return(base.CanMatch(element));
        }
Esempio n. 13
0
        public override bool CanMatch(fo.DicomItem element)
        {
            if (!element.ValueRepresentation.Equals(fo.DicomVR.UI))
            {
                return(false);
            }

            if (((fo.DicomElement)element).Count <= 1)
            {
                return(false);
            }

            return(base.CanMatch(element));
        }
Esempio n. 14
0
        public virtual void SetElement(fo.DicomItem element)
        {
            Elements.Add(element);

            if (KeyTag == 0)
            {
                KeyTag = (uint)element.Tag;
            }

            if (VR == null)
            {
                VR = element.ValueRepresentation;
            }
        }
Esempio n. 15
0
        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);
        }
Esempio n. 16
0
        public void Save_NewOptionalAttributes_SavedWhenExisting(DicomItem item)
        {
            var inFile = DicomFile.Open(@"Test Data\CT-MONO2-16-ankle");

            inFile.FileMetaInfo.Add(item);

            using (var saveStream = new MemoryStream())
            {
                inFile.Save(saveStream);
                saveStream.Seek(0, SeekOrigin.Begin);

                var file = DicomFile.Open(saveStream);
                Assert.True(file.FileMetaInfo.Contains(item.Tag));
            }
        }
Esempio n. 17
0
        /// <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 NET35
            if (t.IsGenericType && !t.ContainsGenericParameters && t.GetGenericTypeDefinition() == typeof(DicomValueElement <>))
            {
                return(t.GetGenericArguments()[0]);
            }
#else
            if (t.IsConstructedGenericType && t.GetGenericTypeDefinition() == typeof(DicomValueElement <>))
            {
                return(t.GenericTypeArguments[0]);
            }
#endif
            return(null);
        }
Esempio n. 18
0
        public override string[] GetValues()
        {
            if (Elements.Count == 0)
            {
                return(base.GetValues());
            }

            if (Elements.Count > 1)
            {
                if (VR == fo.DicomVR.TM || VR == fo.DicomVR.DA)
                {
                    List <string> values = new List <string> ( );

                    for (int index = 0; index < Elements.Count; index += 2)
                    {
                        fo.DicomItem dateElement = null;
                        fo.DicomItem timeElement = null;


                        for (int localIndex = index; localIndex < index + 2 && localIndex < Elements.Count; localIndex++)
                        {
                            var element = Elements[localIndex];

                            if (element.ValueRepresentation == fo.DicomVR.DA)
                            {
                                dateElement = element;
                            }
                            else if (element.ValueRepresentation == fo.DicomVR.TM)
                            {
                                timeElement = element;
                            }
                        }

                        if (null != dateElement)
                        {
                        }
                    }
                }
            }

            return(base.GetValues( ));
        }
Esempio n. 19
0
        protected virtual void WriteVR_Binary(fo.DicomItem item, JsonWriter writer)
        {
            fo.IO.Buffer.IByteBuffer buffer = GetItemBuffer(item);


            if (buffer is fo.IO.Buffer.IBulkDataUriByteBuffer)
            {
                WriteBinaryValue(writer, ((fo.IO.Buffer.IBulkDataUriByteBuffer)buffer).BulkDataUri,
                                 JsonConstants.ELEMENT_BULKDATA);
            }
            else
            {
                if (this.WriteInlineBinary)
                {
                    WriteBinaryValue(writer,
                                     System.Convert.ToBase64String(buffer.Data),
                                     JsonConstants.ELEMENT_INLINEBINARY);
                }
            }
        }
Esempio n. 20
0
        protected virtual void WriteVR_Binary(fo.DicomItem item, XmlWriter writer)
        {
            fo.IO.Buffer.IByteBuffer buffer = GetItemBuffer(item);

            if (buffer is fo.IO.Buffer.IBulkDataUriByteBuffer)
            {
                writer.WriteStartElement(Constants.ELEMENT_BULKDATA);
                //TODO: what about uuid? how is this represented in foDicom? fo.IO.Buffer.IBulkDataUUIDByteBuffer
                writer.WriteAttributeString(Constants.ATTRIBUTE_BULKDATAURI, ((fo.IO.Buffer.IBulkDataUriByteBuffer)buffer).BulkDataUri);
                writer.WriteEndElement( );
            }
            else
            {
                if (this.WriteInlineBinary)
                {
                    writer.WriteStartElement(Constants.ELEMENT_INLINEBINARY);
                    WriteStringValue(writer, buffer.Data);
                    writer.WriteEndElement( );
                }
            }
        }
        public virtual void ProcessElement(fo.DicomItem element)
        {
            for (int index = ProcessingList.Count - 1; index >= 0; index--)
            {
                IDicomDataParameter currentCondition = ProcessingList[index];


                if (currentCondition.IsSupported(element))
                {
                    currentCondition.SetElement(element);

                    if (!currentCondition.AllowExtraElement)
                    {
                        ProcessingList.RemoveAt(index);
                    }

                    return;
                }
            }

            foreach (var condition in ParametersTemplate)
            {
                if (condition.IsSupported(element))
                {
                    IDicomDataParameter dedicatedCondition = condition.CreateParameter( );


                    dedicatedCondition.SetElement(element);

                    InternalResult.Add((T)dedicatedCondition);

                    if (dedicatedCondition.AllowExtraElement)
                    {
                        ProcessingList.Add(dedicatedCondition);
                    }

                    return;
                }
            }
        }
        protected virtual IList <string> GetValues(IDicomDataParameter condition)
        {
            if (condition is RangeMatching)
            {
                RangeMatching rangeCondition = (RangeMatching)condition;
                fo.DicomItem  dateElement    = rangeCondition.DateElement;
                fo.DicomItem  timeElement    = rangeCondition.TimeElement;


                return(GetDateTimeValues((fo.DicomElement)dateElement, (fo.DicomElement)timeElement));
            }
            else if (condition.VR.Equals(fo.DicomVR.DA) || condition.VR.Equals(fo.DicomVR.DT))
            {
                fo.DicomElement dateElement = null;
                fo.DicomElement timeElement = null;

                foreach (var element in condition.Elements)
                {
                    if (element.ValueRepresentation.Equals(fo.DicomVR.DA))
                    {
                        dateElement = (fo.DicomElement)element;
                        continue;
                    }

                    if (element.ValueRepresentation.Equals(fo.DicomVR.TM))
                    {
                        timeElement = (fo.DicomElement)element;
                    }
                }

                return(GetDateTimeValues(dateElement, timeElement));
            }
            else
            {
                return(condition.GetValues( ));
            }
        }
Esempio n. 23
0
        protected virtual fo.IO.Buffer.IByteBuffer GetItemBuffer(fo.DicomItem item)
        {
            fo.IO.Buffer.IByteBuffer buffer;


            if (item is fo.DicomFragmentSequence)
            {
                var dicomfragmentSq = (fo.DicomFragmentSequence)item;
                var sb = new StringBuilder( );


                buffer = dicomfragmentSq.Fragments.Count == 1 ? dicomfragmentSq.Fragments[0] :
                         new fo.IO.Buffer.CompositeByteBuffer(dicomfragmentSq.Fragments.ToArray( ));
            }
            else
            {
                var dicomElement = (fo.DicomElement)item;


                buffer = dicomElement.Buffer;
            }

            return(buffer);
        }
        private void Walk(object state)
        {
            try {
                if (_items == null)
                {
                    _items = new Queue <DicomItem>();
                    BuildWalkQueue(_dataset);
                    _walker.OnBeginWalk(this, NextWalkItem);
                }

                DicomItem item = null;
                while (_items.Count > 0)
                {
                    item = _items.Peek();

                    if (item is DicomElement)
                    {
                        if (!_walker.OnElement(item as DicomElement))
                        {
                            return;
                        }
                    }
                    else if (item is DicomFragmentSequence)
                    {
                        if (!_walker.OnBeginFragment(item as DicomFragmentSequence))
                        {
                            return;
                        }
                    }
                    else if (item is DicomFragmentItem)
                    {
                        if (!_walker.OnFragmentItem((item as DicomFragmentItem).Buffer))
                        {
                            return;
                        }
                    }
                    else if (item is EndDicomFragment)
                    {
                        if (!_walker.OnEndFragment())
                        {
                            return;
                        }
                    }
                    else if (item is DicomSequence)
                    {
                        if (!_walker.OnBeginSequence(item as DicomSequence))
                        {
                            return;
                        }
                    }
                    else if (item is BeginDicomSequenceItem)
                    {
                        if (!_walker.OnBeginSequenceItem((item as BeginDicomSequenceItem).Dataset))
                        {
                            return;
                        }
                    }
                    else if (item is EndDicomSequenceItem)
                    {
                        if (!_walker.OnEndSequenceItem())
                        {
                            return;
                        }
                    }
                    else if (item is EndDicomSequence)
                    {
                        if (!_walker.OnEndSequence())
                        {
                            return;
                        }
                    }

                    _items.Dequeue();
                }

                _walker.OnEndWalk();

                _items = null;
                _async.Set();
            } catch (Exception e) {
                try {
                    _walker.OnEndWalk();
                } catch {
                }
                _exception = e;
                _items     = null;
                _async.Set();
            }
        }
Esempio n. 25
0
        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( );
        }
Esempio n. 26
0
        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( );
        }
Esempio n. 27
0
 /// <string>Replaces the content of an item.</string>
 /// <param name="dataset">Reference to the dataset</param>
 /// <param name="encoding">Identifies the character set should be used for encoding.</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, Encoding encoding, DicomItem item, string newString)
 {
     dataset.AddOrUpdate(item.Tag, encoding, newString);
 }
Esempio n. 28
0
 public override bool IsSupported(fo.DicomItem element)
 {
     return(CanMatch(element));
 }