Ejemplo n.º 1
0
        public T Get <T>(DicomTag tag, int n, T defaultValue)
        {
            DicomItem item = null;

            if (!_items.TryGetValue(tag, out item))
            {
                return(defaultValue);
            }

            if (typeof(T) == typeof(DicomItem))
            {
                return((T)(object)item);
            }

            if (typeof(T).IsSubclassOf(typeof(DicomItem)))
            {
                return((T)(object)item);
            }

            if (item.GetType().IsSubclassOf(typeof(DicomElement)))
            {
                DicomElement element = (DicomElement)item;
                if (n >= element.Count)
                {
                    return(defaultValue);
                }
                return((T)(object)element.Get <T>(n));
            }

            throw new DicomDataException("Unable to get a value type of {0} from DICOM item of type {1}", typeof(T), item.GetType());
        }
Ejemplo n.º 2
0
        protected virtual void WriteVR_PN(fo.DicomElement element, XmlWriter writer)
        {
            for (int index = 0; index < element.Count; index++)
            {
                writer.WriteStartElement(Constants.PN_PERSON_NAME);
                WriteNumberAttrib(writer, index);

                var pnComponents = GetTrimmedString(element.Get <string> ( )).Split('=');

                for (int compIndex = 0; (compIndex < pnComponents.Length) && (compIndex < 3); compIndex++)
                {
                    writer.WriteStartElement(Utilities.PersonNameComponents.PN_Components[compIndex]);

                    fo.DicomPersonName pn = new fo.DicomPersonName(element.Tag, pnComponents[compIndex]);

                    writer.WriteElementString(Utilities.PersonNameParts.PN_Family, pn.Last);
                    writer.WriteElementString(Utilities.PersonNameParts.PN_Given, pn.First);
                    writer.WriteElementString(Utilities.PersonNameParts.PN_Midlle, pn.Middle);
                    writer.WriteElementString(Utilities.PersonNameParts.PN_Prefix, pn.Prefix);
                    writer.WriteElementString(Utilities.PersonNameParts.PN_Suffix, pn.Suffix);

                    writer.WriteEndElement( );
                }
                writer.WriteEndElement( );
            }
        }
Ejemplo n.º 3
0
            public bool OnElement(DicomElement element)
            {
                if (element.Tag.Equals(DicomTag.NumberOfContourPoints))
                {
                    this.numberOfCountourPoints = element.Get <int>();
                }
                if (element.Tag.Equals(DicomTag.FinalCumulativeMetersetWeight))
                {
                    this.maxFinalCumulativeMetersetWeight = Math.Max(
                        element.Get <double>(),
                        this.maxFinalCumulativeMetersetWeight);
                }

                var success = !element.Tag.Equals(DicomTag.AcquisitionDate);

                return(success);
            }
Ejemplo n.º 4
0
        protected virtual void WriteVR_PN(fo.DicomElement element, JsonWriter writer)
        {
            writer.WritePropertyName(JsonConstants.ValueField);
            writer.WriteStartArray(                          );

            for (int index = 0; index < element.Count; index++)
            {
                writer.WriteStartObject( );

                var pnComponents = GetTrimmedString(element.Get <string> ( )).Split('=');

                for (int compIndex = 0; (compIndex < pnComponents.Length) && (compIndex < 3); compIndex++)
                {
                    writer.WritePropertyName(Utilities.PersonNameComponents.PN_Components[compIndex]);
                    writer.WriteValue(GetTrimmedString(pnComponents[compIndex]));
                    writer.WriteEndObject(                                                         );
                }
            }

            writer.WriteEndArray( );
        }
Ejemplo n.º 5
0
        /// <string>Replaces the content of a UID with a random one</string>
        /// <param name="dataset">Reference to the dataset</param>
        /// <param name="element">The element to be altered</param>
        private void ReplaceUID(DicomDataset dataset, DicomElement element)
        {
            string   rep;
            DicomUID uid;
            var      old = element.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(element.Tag, uid);

            dataset.AddOrUpdate(newItem);
        }
Ejemplo n.º 6
0
        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( );
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the item or element value of the specified <paramref name="tag"/>.
        /// </summary>
        /// <typeparam name="T">Type of the return value.</typeparam>
        /// <param name="tag">Requested DICOM tag.</param>
        /// <param name="n">Item index (for multi-valued elements).</param>
        /// <param name="useDefault">Indicates whether to use default value (true) or throw (false) if <paramref name="tag"/> is not contained in dataset.</param>
        /// <param name="defaultValue">Default value to apply if <paramref name="tag"/> is not contained in dataset and <paramref name="useDefault"/> is true.</param>
        /// <returns>Item or element value corresponding to <paramref name="tag"/>.</returns>
        private T Get <T>(DicomTag tag, int n, bool useDefault, T defaultValue)
        {
            DicomItem item = null;

            if (!_items.TryGetValue(tag, out item))
            {
                if (useDefault)
                {
                    return(defaultValue);
                }
                throw new DicomDataException("Tag: {0} not found in dataset", tag);
            }

            if (typeof(T) == typeof(DicomItem))
            {
                return((T)(object)item);
            }

            if (typeof(T).GetTypeInfo().IsSubclassOf(typeof(DicomItem)))
            {
                return((T)(object)item);
            }

            if (typeof(T) == typeof(DicomVR))
            {
                return((T)(object)item.ValueRepresentation);
            }

            if (item.GetType().GetTypeInfo().IsSubclassOf(typeof(DicomElement)))
            {
                DicomElement element = (DicomElement)item;

                if (typeof(IByteBuffer).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo()))
                {
                    return((T)(object)element.Buffer);
                }

                if (typeof(T) == typeof(byte[]))
                {
                    return((T)(object)element.Buffer.Data);
                }

                if (!typeof(T).GetTypeInfo().IsArray&& (n >= element.Count || element.Count == 0))
                {
                    if (useDefault)
                    {
                        return(defaultValue);
                    }
                    throw new DicomDataException("Element empty or index: {0} exceeds element count: {1}", n, element.Count);
                }

                return(element.Get <T>(n));
            }

            if (item.GetType() == typeof(DicomSequence))
            {
                if (typeof(T) == typeof(DicomCodeItem))
                {
                    return((T)(object)new DicomCodeItem((DicomSequence)item));
                }

                if (typeof(T) == typeof(DicomMeasuredValue))
                {
                    return((T)(object)new DicomMeasuredValue((DicomSequence)item));
                }

                if (typeof(T) == typeof(DicomReferencedSOP))
                {
                    return((T)(object)new DicomReferencedSOP((DicomSequence)item));
                }
            }

            throw new DicomDataException(
                      "Unable to get a value type of {0} from DICOM item of type {1}",
                      typeof(T),
                      item.GetType());
        }
Ejemplo n.º 8
0
        public T Get <T>(DicomTag tag, int n, T defaultValue)
        {
            DicomItem item = null;

            if (!_items.TryGetValue(tag, out item))
            {
                return(defaultValue);
            }

            if (typeof(T) == typeof(DicomItem))
            {
                return((T)(object)item);
            }

            if (typeof(T).IsSubclassOf(typeof(DicomItem)))
            {
                return((T)(object)item);
            }

            if (typeof(T) == typeof(DicomVR))
            {
                return((T)(object)item.ValueRepresentation);
            }

            if (item.GetType().IsSubclassOf(typeof(DicomElement)))
            {
                DicomElement element = (DicomElement)item;

                if (typeof(IByteBuffer).IsAssignableFrom(typeof(T)))
                {
                    return((T)(object)element.Buffer);
                }

                if (typeof(T) == typeof(byte[]))
                {
                    return((T)(object)element.Buffer.Data);
                }

                if (n >= element.Count || element.Count == 0)
                {
                    return(defaultValue);
                }

                return((T)(object)element.Get <T>(n));
            }

            if (item.GetType() == typeof(DicomSequence))
            {
                if (typeof(T) == typeof(DicomCodeItem))
                {
                    return((T)(object)new DicomCodeItem((DicomSequence)item));
                }

                if (typeof(T) == typeof(DicomMeasuredValue))
                {
                    return((T)(object)new DicomMeasuredValue((DicomSequence)item));
                }

                if (typeof(T) == typeof(DicomReferencedSOP))
                {
                    return((T)(object)new DicomReferencedSOP((DicomSequence)item));
                }
            }

            throw new DicomDataException("Unable to get a value type of {0} from DICOM item of type {1}", typeof(T), item.GetType());
        }
        private IList <string> GetDateTimeValues(fo.DicomElement dateElement, fo.DicomElement timeElement)
        {
            List <string> values          = new List <string> ( );
            int           dateValuesCount = dateElement == null ? 0 : (int)dateElement.Count;
            int           timeValuesCount = timeElement == null ? 0 : (int)timeElement.Count;
            int           dateTimeIndex   = 0;

            for (; dateTimeIndex < dateValuesCount || dateTimeIndex < timeValuesCount; dateTimeIndex++)
            {
                string dateString = null;
                string timeString = null;

                if (dateTimeIndex < dateValuesCount)
                {
                    dateString = dateElement == null || dateElement.Count == 0 ? null : dateElement.Get <string>(0); //TODO: test - original code returns "" as default
                }

                if (dateTimeIndex < dateValuesCount)
                {
                    timeString = timeElement == null || timeElement.Count == 0 ? null : timeElement.Get <string>(0); //TODO: test - original code returns "" as default
                }

                values.AddRange(GetDateTimeValues(dateString, timeString));
            }

            return(values);
        }