Ejemplo n.º 1
10
        private void AddItem(DicomItem i, ListView lv, Color c)
        {
            ListViewItem lvi = null;

            if (i != null)
            {
                var tag = GetTagName(i.Tag);
                lvi = lv.Items.Add(tag);
                lvi.SubItems.Add(i.ValueRepresentation.Code);
                if (i is DicomElement)
                {
                    var e = i as DicomElement;
                    lvi.SubItems.Add(e.Length.ToString());
                    string value = "<large value not displayed>";
                    if (e.Length <= 2048) value = String.Join("\\", e.Get<string[]>());
                    lvi.SubItems.Add(value);
                }
                else
                {
                    lvi.SubItems.Add("-");
                    lvi.SubItems.Add(String.Empty);
                }
                lvi.Tag = i;
            }
            else
            {
                lvi = lv.Items.Add(String.Empty);
                lvi.SubItems.Add(String.Empty);
                lvi.SubItems.Add(String.Empty);
                lvi.SubItems.Add(String.Empty);
            }

            lvi.UseItemStyleForSubItems = true;
            lvi.BackColor = c;
        }
Ejemplo n.º 2
4
        public uint Calculate(DicomItem item)
        {
            // skip group lengths if not writing to file
            if (!_options.KeepGroupLengths && item.Tag.Element == 0x0000) return 0;

            uint length = 0;
            length += 4; // tag

            if (_syntax.IsExplicitVR)
            {
                length += 2; // vr
                if (item.ValueRepresentation.Is16bitLength)
                {
                    length += 2; // length
                }
                else
                {
                    length += 2; // reserved
                    length += 4; // length
                }
            }
            else
            {
                length += 4; // length
            }

            if (item is DicomElement)
            {
                length += (uint)(item as DicomElement).Buffer.Size;
            }
            else if (item is DicomFragmentSequence)
            {
                DicomFragmentSequence sq = item as DicomFragmentSequence;
                // fragment item (offset table)
                length += 4; // tag
                length += 4; // length
                length += (uint)(sq.OffsetTable.Count / 4);

                foreach (IByteBuffer fragment in sq)
                {
                    // fragment item
                    length += 4; // tag
                    length += 4; // length
                    length += fragment.Size;
                }

                // sequence delimitation item
                length += 4; // tag
                length += 4; // length
            }
            else if (item is DicomSequence)
            {
                DicomSequence sq = item as DicomSequence;
                length += Calculate(sq);
            }

            return length;
        }
Ejemplo n.º 3
3
        private void AddItem(DicomItem i1, DicomItem i2)
        {
            if (i1 is DicomSequence || i2 is DicomSequence)
            {
                CompareSequences(i1 as DicomSequence, i2 as DicomSequence);
                return;
            }

            if (i2 == null)
            {
                AddItem(i1, lvFile1, Green);
                AddItem(i2, lvFile2, Gray);
                return;
            }

            if (i1 == null)
            {
                AddItem(i1, lvFile1, Gray);
                AddItem(i2, lvFile2, Green);
                return;
            }

            if (i1 is DicomElement && i2 is DicomElement)
            {
                var e1 = i1 as DicomElement;
                var e2 = i2 as DicomElement;

                var c = None;
                if (!cbIgnoreVR.Checked && e1.ValueRepresentation != e2.ValueRepresentation) c = Red;
                else if (!Compare(e1.Buffer.Data, e2.Buffer.Data)) c = Red;

                if (cbIgnoreGroupLengths.Checked && e1.Tag.Element == 0x0000) c = Yellow;

                if (cbIgnoreUIDs.Checked && e1.ValueRepresentation == DicomVR.UI)
                {
                    var uid = (i1 as DicomElement).Get<DicomUID>(0);
                    if (uid != null && (uid.Type == DicomUidType.SOPInstance || uid.Type == DicomUidType.Unknown)) c = Yellow;
                }

                if (cbIgnorePixelData.Checked && i1.Tag == DicomTag.PixelData) c = Yellow;

                AddItem(i1, lvFile1, c);
                AddItem(i2, lvFile2, c);
                return;
            }

            if (i1 is DicomFragmentSequence || i2 is DicomFragmentSequence)
            {
                CompareFragments(i1, i2);
                return;
            }

            while (i1 is DicomElement || i2 is DicomElement)
            {
                AddItem(i1, lvFile1, Red);
                AddItem(i2, lvFile2, Red);
                return;
            }

            AddItem(i1, lvFile1, Yellow);
            AddItem(i2, lvFile2, Yellow);
        }
Ejemplo n.º 4
2
        private void CompareFragments(DicomItem i1, DicomItem i2)
        {
            DicomFragmentSequence s1 = null;
            DicomFragmentSequence s2 = null;

            bool pixel = cbIgnorePixelData.Checked && i1.Tag == DicomTag.PixelData;

            if (i1 == null)
            {
                AddItem(i1, lvFile1, Gray);
                AddItem(i2, lvFile2, Green);
                s2 = i2 as DicomFragmentSequence;
            }
            else if (i2 == null)
            {
                AddItem(i1, lvFile1, Green);
                AddItem(i2, lvFile2, Gray);
                s1 = i1 as DicomFragmentSequence;
            }
            else if (!(i1 is DicomFragmentSequence))
            {
                AddItem(i1, lvFile1, pixel ? Yellow : Red);
                AddItem(i2, lvFile2, pixel ? Yellow : Red);
                s2 = i2 as DicomFragmentSequence;
            }
            else if (!(i2 is DicomFragmentSequence))
            {
                AddItem(i1, lvFile1, pixel ? Yellow : Red);
                AddItem(i2, lvFile2, pixel ? Yellow : Red);
                s1 = i1 as DicomFragmentSequence;
            }
            else
            {
                AddItem(i1, lvFile1, pixel ? Yellow : None);
                AddItem(i2, lvFile2, pixel ? Yellow : None);
                s1 = i1 as DicomFragmentSequence;
                s2 = i2 as DicomFragmentSequence;
            }

            Level++;

            if (s1 == null)
            {
                AddItem(String.Empty, UInt32.MaxValue, String.Empty, lvFile1, Gray);
                AddItem(
                    _indent + "Offset Table",
                    (uint)s2.OffsetTable.Count * 4,
                    String.Format("@entries={0}", s2.OffsetTable.Count),
                    lvFile2,
                    pixel ? Yellow : Red);
            }
            else if (s2 == null)
            {
                AddItem(
                    _indent + "Offset Table",
                    (uint)s1.OffsetTable.Count * 4,
                    String.Format("@entries={0}", s1.OffsetTable.Count),
                    lvFile1,
                    pixel ? Yellow : Red);
                AddItem(String.Empty, UInt32.MaxValue, String.Empty, lvFile2, Gray);
            }
            else
            {
                Color c = None;
                if (s1.OffsetTable.Count != s2.OffsetTable.Count) c = Red;
                else
                {
                    for (int i = 0; i < s1.OffsetTable.Count; i++)
                    {
                        if (s1.OffsetTable[i] != s2.OffsetTable[i])
                        {
                            c = Red;
                            break;
                        }
                    }
                }
                AddItem(
                    _indent + "Offset Table",
                    (uint)s2.OffsetTable.Count * 4,
                    String.Format("@entries={0}", s1.OffsetTable.Count),
                    lvFile1,
                    pixel ? Yellow : c);
                AddItem(
                    _indent + "Offset Table",
                    (uint)s2.OffsetTable.Count * 4,
                    String.Format("@entries={0}", s2.OffsetTable.Count),
                    lvFile2,
                    pixel ? Yellow : c);
            }

            int count = 0;
            if (s1 != null) count = s1.Fragments.Count;
            if (s2 != null && s2.Fragments.Count > count) count = s2.Fragments.Count;

            string name = _indent + "Fragment";

            for (int i = 0; i < count; i++)
            {
                IByteBuffer b1 = null;
                if (s1 != null && i < s1.Fragments.Count) b1 = s1.Fragments[i];

                IByteBuffer b2 = null;
                if (s2 != null && i < s2.Fragments.Count) b2 = s2.Fragments[i];

                if (b1 == null)
                {
                    AddItem(String.Empty, UInt32.MaxValue, String.Empty, lvFile1, Gray);
                    AddItem(name, b2.Size, String.Empty, lvFile2, pixel ? Yellow : Red);
                    continue;
                }
                else if (b2 == null)
                {
                    AddItem(name, b1.Size, String.Empty, lvFile1, pixel ? Yellow : Red);
                    AddItem(String.Empty, UInt32.MaxValue, String.Empty, lvFile2, Gray);
                    continue;
                }

                Color c = None;
                if (pixel) c = Yellow;
                else if (!Compare(b1.Data, b2.Data)) c = Red;

                AddItem(name, b1.Size, String.Empty, lvFile1, c);
                AddItem(name, b2.Size, String.Empty, lvFile2, c);
            }

            Level--;
        }
Ejemplo n.º 5
0
 public static void InferOutput(DicomItem input, DicomItem output, AnonExample example)
 {
     if (output == null)
     {
         example.Output.Add("<removed>");
     }
     else if (input == output)
     {
         example.Output.Add("<retained>");
     }
     else if (output as DicomElement != null)
     {
         if (output as DicomDate != null)
         {
             var v = (output as DicomDate).Get <DateTime>();
             example.Output.Add(v.ToShortDateString());
         }
         else if (output as DicomTime != null)
         {
             var v = (output as DicomTime).Get <DateTime>();
             example.Output.Add(v.TimeOfDay.ToString());
         }
         else
         {
             var v = (output as DicomElement).Get <string>();
             example.Output.Add(v == "" ? "<empty>" : v);
         }
     }
 }
        public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context = null)
        {
            EnsureArg.IsNotNull(dicomDataset, nameof(dicomDataset));
            EnsureArg.IsNotNull(item, nameof(item));

            var redactedValues = new List <string>();

            if (item.ValueRepresentation == DicomVR.AS)
            {
                var values = ((DicomAgeString)item).Get <string[]>();
                foreach (var value in values)
                {
                    var result = _redactFunction.Redact(DicomUtility.ParseAge(value));
                    if (result != null)
                    {
                        redactedValues.Add(DicomUtility.GenerateAgeString(result));
                    }
                }
            }
            else if (item.ValueRepresentation == DicomVR.DA)
            {
                var values = DicomUtility.ParseDicomDate((DicomDate)item);
                foreach (var value in values)
                {
                    var result = _redactFunction.Redact(value);
                    if (result != null)
                    {
                        redactedValues.Add(DicomUtility.GenerateDicomDateString((DateTimeOffset)result));
                    }
                }
            }
            else if (item.ValueRepresentation == DicomVR.DT)
            {
                DateTimeObject[] values = DicomUtility.ParseDicomDateTime((DicomDateTime)item);
                foreach (var value in values)
                {
                    DateTimeObject result = _redactFunction.Redact(value);
                    if (result != null)
                    {
                        redactedValues.Add(DicomUtility.GenerateDicomDateTimeString(result));
                    }
                }
            }
            else if (item.ValueRepresentation == DicomVR.SQ)
            {
                dicomDataset.AddOrUpdate <DicomDataset>(DicomVR.SQ, item.Tag);
                return;
            }

            if (redactedValues.Count != 0)
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, redactedValues.ToArray());
            }
            else
            {
                dicomDataset.AddOrUpdate <string>(item.ValueRepresentation, item.Tag, values: null);
            }

            _logger.LogDebug($"The value of DICOM item '{item}' is redacted.");
        }
Ejemplo n.º 7
0
        public override bool CanMatch(DicomItem item)
        {
            //if SQ casting will fail and be null
            DicomElement element = item as DicomElement;


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

            string elementValue = element.ToString( );

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

            return(base.CanMatch(element));
        }
        private static void WriteJsonOther(JsonWriter writer, DicomItem item)
        {
            // DicomFragmentSequence - Only for pixel data
            if (!(item is DicomElement))
            {
                return;
            }

            var elem = (DicomElement)item;

            if (!DicomTypeTranslater.SerializeBinaryData && DicomTypeTranslater.DicomVrBlacklist.Contains(elem.ValueRepresentation))
            {
                return;
            }

            if (elem.Buffer is IBulkDataUriByteBuffer buffer)
            {
                writer.WritePropertyName(BLK_URI_PROPERTY_NAME);
                writer.WriteValue(buffer.BulkDataUri);
            }
            else if (elem.Count != 0)
            {
                writer.WritePropertyName(INL_BIN_PROPERTY_NAME);
                writer.WriteValue(Convert.ToBase64String(elem.Buffer.Data));
            }
        }
Ejemplo n.º 9
0
        public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context)
        {
            var mask = new string('*', this._maskedLength);

            if (item is DicomStringElement)
            {
                var values = ((DicomStringElement)item).Get <string[]>().Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Length > _maskedLength?mask + x[this._maskedLength..] : mask);
Ejemplo n.º 10
0
 private static bool ValueEquals(DicomItem a, DicomItem b)
 {
     if (a == null || b == null)
     {
         return(a == b);
     }
     else if (a == b)
     {
         return(true);
     }
     else if (a.ValueRepresentation != b.ValueRepresentation || (uint)a.Tag != (uint)b.Tag)
     {
         return(false);
     }
     else if (a is DicomElement elementA)
     {
         if (!(b is DicomElement elementB))
         {
             return(false);
         }
         else if (b is DicomDecimalString decimalStringB &&
                  a is DicomDecimalString decimalStringA)
         {
             return(decimalStringA.Get <decimal[]>().SequenceEqual(decimalStringB.Get <decimal[]>()));
         }
Ejemplo n.º 11
0
 internal DElement(DicomItem elementRef, DicomDataset rootEleList)
 {
     IsRef        = true;
     _element     = elementRef;
     _rootEleList = rootEleList;
     FillSequenceFromElementRef(rootEleList);
 }
Ejemplo n.º 12
0
        public static void InferOutput(DicomItem input, DicomItem output, AnonExample example)
        {
            example = example ?? throw new ArgumentNullException(nameof(example));

            if (output == null)
            {
                example.Output.Add("<removed>");
            }
            else if (input == output)
            {
                example.Output.Add("<retained>");
            }
            else if (output is DicomElement)
            {
                if (output is DicomDate)
                {
                    var v = (output as DicomDate).Get <DateTime>();
                    example.Output.Add(v.ToShortDateString());
                }
                else if (output is DicomTime)
                {
                    var v = (output as DicomTime).Get <DateTime>();
                    example.Output.Add(v.TimeOfDay.ToString());
                }
                else
                {
                    var v = (output as DicomElement).Get <string>();
                    example.Output.Add(string.IsNullOrEmpty(v) ? "<empty>" : v);
                }
            }
        }
Ejemplo n.º 13
0
        public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context = null)
        {
            EnsureArg.IsNotNull(dicomDataset, nameof(dicomDataset));
            EnsureArg.IsNotNull(item, nameof(item));

            if (item.ValueRepresentation == DicomVR.AS)
            {
                var values = ((DicomAgeString)item).Get <string[]>().Select(DicomUtility.ParseAge).Select(x => _perturbFunction.Perturb(x));
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Select(DicomUtility.GenerateAgeString).Where(x => x != null).ToArray());
            }
            else
            {
                if (!_numericValueTypeMapping.ContainsKey(item.ValueRepresentation))
                {
                    throw new AnonymizerOperationException(DicomAnonymizationErrorCode.UnsupportedAnonymizationMethod, $"Perturb is not supported for {item.ValueRepresentation}.");
                }

                var elementType = _numericElementTypeMapping[item.ValueRepresentation];
                var valueType   = _numericValueTypeMapping[item.ValueRepresentation];

                // Get numeric value using reflection.
                var valueObj = elementType.GetMethod("Get").MakeGenericMethod(valueType).Invoke(item, new object[] { -1 });
                PerturbNumericValue(dicomDataset, item, valueObj as Array);
            }

            _logger.LogDebug($"The value of DICOM item '{item}' is perturbed.");
        }
Ejemplo n.º 14
0
 public void Dispose()
 {
     if (_element != null)
     {
         _element = null;
     }
 }
Ejemplo n.º 15
0
        public DicomItem ReplaceUID(DicomDataset oldds, List <TagOrIndex> path, DicomItem item)
        {
            var element = (item as DicomElement);

            if (element == null)
            {
                throw new InvalidOperationException($"ReplaceUID can not handle type: {item.GetType()}");
            }

            if (element.ValueRepresentation != DicomVR.UI)
            {
                throw new ArgumentOutOfRangeException($"Tag: {element.Tag} is marked as U but VR is: {element.ValueRepresentation}");
            }

            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;
            }

            return(new DicomUniqueIdentifier(element.Tag, uid));
        }
Ejemplo n.º 16
0
        public virtual bool IsSupported(DicomItem element)
        {
            if (null != SupportedTags && SupportedTags.Count > 0)
            {
                return(SupportedTags.Contains((uint)element.Tag));
            }

            return(true);
        }
Ejemplo n.º 17
0
        public virtual bool CanMatch(DicomItem element)
        {
            if (SupportedTags.Count > 0)
            {
                return(SupportedTags.Contains((uint)element.Tag));
            }

            return(true);
        }
        public bool IsSupported(DicomItem item)
        {
            if (item is DicomOtherByte || item is DicomSequence || item is DicomFragmentSequence)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 19
0
        public bool IsSupported(DicomItem item)
        {
            if (item is DicomStringElement)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 20
0
        public override bool CanMatch(DicomItem element)
        {
            if (!element.ValueRepresentation.Equals(DicomVR.SQ))
            {
                return(false);
            }

            return(base.CanMatch(element));
        }
        public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context = null)
        {
            EnsureArg.IsNotNull(dicomDataset, nameof(dicomDataset));
            EnsureArg.IsNotNull(item, nameof(item));

            dicomDataset.Remove(item.Tag);

            _logger.LogDebug($"The DICOM item '{item}' is removed.");
        }
Ejemplo n.º 22
0
 private static bool ValueEquals(DicomItem a, DicomItem b)
 {
     if (a == null || b == null)
     {
         return(a == b);
     }
     else if (a == b)
     {
         return(true);
     }
     else if (a.ValueRepresentation != b.ValueRepresentation || (uint)a.Tag != (uint)b.Tag)
     {
         return(false);
     }
     else if (a is DicomElement)
     {
         if (b is DicomElement == false)
         {
             return(false);
         }
         else if (b is DicomStringElement && a is DicomDecimalString)
         {
             return(((DicomDecimalString)a).Get <decimal[]>().SequenceEqual(((DicomDecimalString)b).Get <decimal[]>()));
         }
         else
         {
             return(ValueEquals(((DicomElement)a).Buffer, ((DicomElement)b).Buffer));
         }
     }
     else if (a is DicomSequence)
     {
         if (b is DicomSequence == false)
         {
             return(false);
         }
         else
         {
             return(((DicomSequence)a).Items.Zip(((DicomSequence)b).Items, ValueEquals).All(x => x));
         }
     }
     else if (a is DicomFragmentSequence)
     {
         if (b is DicomFragmentSequence == false)
         {
             return(false);
         }
         else
         {
             return(((DicomFragmentSequence)a).Fragments.Zip(((DicomFragmentSequence)b).Fragments, ValueEquals).All(x => x));
         }
     }
     else
     {
         return(a.Equals(b));
     }
 }
Ejemplo n.º 23
0
        public uint Calculate(DicomItem item)
        {
            uint length = 0;

            length += 4;             // tag

            if (_syntax.IsExplicitVR)
            {
                length += 2;                 // vr
                if (item.ValueRepresentation.Is16bitLength)
                {
                    length += 2;                     // length
                }
                else
                {
                    length += 2;                     // reserved
                    length += 4;                     // length
                }
            }
            else
            {
                length += 4;                 // length
            }

            if (item is DicomElement)
            {
                length += (uint)(item as DicomElement).Buffer.Size;
            }
            else if (item is DicomFragmentSequence)
            {
                DicomFragmentSequence sq = item as DicomFragmentSequence;
                // fragment item (offset table)
                length += 4;                 // tag
                length += 4;                 // length
                length += (uint)(sq.OffsetTable.Count / 4);

                foreach (IByteBuffer fragment in sq)
                {
                    // fragment item
                    length += 4;                     // tag
                    length += 4;                     // length
                    length += fragment.Size;
                }

                // sequence delimitation item
                length += 4;                 // tag
                length += 4;                 // length
            }
            else if (item is DicomSequence)
            {
                DicomSequence sq = item as DicomSequence;
                length += Calculate(sq);
            }

            return(length);
        }
Ejemplo n.º 24
0
        public DElement(int groupNumber, int elementNumber, DVR vr, string value)
        {
            IsRef = false;

            DicomVR  dicomVR  = DHelper.ConvertToDicomVR(vr);
            DicomTag dicomTag = new DicomTag((ushort)groupNumber, (ushort)elementNumber);

            _element = CreateEmptyElement(dicomTag, dicomVR);
            Value    = value;
        }
Ejemplo n.º 25
0
        public DElement(int tag, DVR vr, DValueType type)
        {
            IsRef = false;

            DicomVR  dicomVR  = DHelper.ConvertToDicomVR(vr);
            DicomTag dicomTag = DHelper.Int2DicomTag(tag);

            _element = CreateEmptyElement(dicomTag, dicomVR);

            Type = type;
        }
Ejemplo n.º 26
0
 private static void WriteDicomAttribute(StringBuilder xmlOutput, DicomItem item)
 {
     if (item.Tag.IsPrivate && item.Tag.PrivateCreator != null)
     {
         xmlOutput.AppendLine($@"<DicomAttribute tag=""{item.Tag.Group:X4}{item.Tag.Element:X4}"" vr=""{item.ValueRepresentation.Code}"" keyword=""{ item.Tag.DictionaryEntry.Keyword}"" privateCreator=""{item.Tag.PrivateCreator.Creator}"">");
     }
     else
     {
         xmlOutput.AppendLine($@"<DicomAttribute tag=""{item.Tag.Group:X4}{item.Tag.Element:X4}"" vr=""{item.ValueRepresentation.Code}"" keyword=""{item.Tag.DictionaryEntry.Keyword}"">");
     }
 }
Ejemplo n.º 27
0
        public override bool CanMatch(DicomItem item)
        {
            DicomElement element = item as DicomElement;

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

            return(base.CanMatch(element));
        }
Ejemplo n.º 28
0
        public void Process(DicomDataset dicomDataset, DicomItem item, Dictionary <string, object> settings = null)
        {
            // var values = Utility.SplitValues(Encoding.UTF8.GetString(((DicomElement)item).Buffer.Data));

            var redactedValues = new List <string>()
            {
            };

            if (item.ValueRepresentation == DicomVR.AS)
            {
                var values = ((DicomAgeString)item).Get <string[]>();
                foreach (var value in values)
                {
                    var result = Utility.AgeToString(RedactFunction.RedactAge(Utility.ParseAge(value)));
                    if (result != null)
                    {
                        redactedValues.Add(result);
                    }
                }
            }

            if (item.ValueRepresentation == DicomVR.DA)
            {
                var values = ((DicomDate)item).Get <string[]>();
                foreach (var value in values)
                {
                    var result = RedactFunction.RedactDateTime(Utility.ParseDicomDate(value));
                    if (result != null)
                    {
                        redactedValues.Add(Utility.GenerateDicomDateString((DateTimeOffset)result));
                    }
                }
            }

            if (item.ValueRepresentation == DicomVR.DT)
            {
                var values = ((DicomDateTime)item).Get <string[]>();
                foreach (var value in values)
                {
                    var result = RedactFunction.RedactDateTime(Utility.ParseDicomDateTime(value));
                    if (result != null)
                    {
                        redactedValues.Add(Utility.GenerateDicomDateTimeString(result));
                    }
                }
            }

            dicomDataset.Remove(item.Tag);

            if (redactedValues.Count() != 0)
            {
                dicomDataset.AddOrUpdate(item.Tag, redactedValues.ToArray());
            }
        }
Ejemplo n.º 29
0
        private void setTag(int tag)
        {
            DicomTag dicomTag = DicomTag.Parse(DHelper.Int2HexString(tag));

            if (_element != null)
            {
                DicomVR vr = _element.ValueRepresentation;
                //string val = Value;
                _element = CreateEmptyElement(dicomTag, vr);
                //Value = val;
            }
        }
Ejemplo n.º 30
0
        public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context)
        {
            EnsureArg.IsNotNull(dicomDataset, nameof(dicomDataset));
            EnsureArg.IsNotNull(item, nameof(item));
            EnsureArg.IsNotNull(context, nameof(context));

            _dateShiftFunction.SetDateShiftPrefix(_dateShiftScope switch
            {
                DateShiftScope.StudyInstance => context.StudyInstanceUID ?? string.Empty,
                DateShiftScope.SeriesInstance => context.SeriesInstanceUID ?? string.Empty,
                DateShiftScope.SopInstance => context.SopInstanceUID ?? string.Empty,
                _ => string.Empty,
            });
Ejemplo n.º 31
0
        private void PerturbNumericValue(DicomDataset dicomDataset, DicomItem item, Array values)
        {
            if (values == null || values.Length == 0)
            {
                return;
            }

            var valueType = values.GetValue(0).GetType();

            if (valueType == typeof(decimal))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <decimal>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(double))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <double>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(float))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <float>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(int))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <int>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(uint))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <uint>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(short))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <short>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(ushort))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <ushort>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(long))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <long>().Select(_perturbFunction.Perturb).ToArray());
            }
            else if (valueType == typeof(ulong))
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <ulong>().Select(_perturbFunction.Perturb).ToArray());
            }
            else
            {
                dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.Cast <string>().Select(_perturbFunction.Perturb).ToArray());
            }
        }
Ejemplo n.º 32
0
        public virtual void SetElement(DicomItem element)
        {
            Elements.Add(element);

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

            if (VR == null)
            {
                VR = element.ValueRepresentation;
            }
        }
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var dataset = new DicomDataset();

            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new JsonReaderException("Malformed DICOM json");
            }

            reader.Read();

            while (reader.TokenType == JsonToken.PropertyName)
            {
                DicomTag tag = ParseTag((string)reader.Value);

                reader.Read();

                DicomItem item = ReadJsonDicomItem(tag, reader, serializer);

                dataset.Add(item);

                reader.Read();
            }

            // Ensure all private tags have a reference to their Private Creator tag
            foreach (DicomItem item in dataset)
            {
                if (item.Tag.IsPrivate && ((item.Tag.Element & 0xff00) != 0))
                {
                    var privateCreatorTag = new DicomTag(item.Tag.Group, (ushort)(item.Tag.Element >> 8));

                    if (dataset.Contains(privateCreatorTag))
                    {
                        item.Tag.PrivateCreator = new DicomPrivateCreator(dataset.GetSingleValue <string>(privateCreatorTag));
                    }
                }
            }

            if (reader.TokenType != JsonToken.EndObject)
            {
                throw new JsonReaderException("Malformed DICOM json");
            }

            return(dataset);
        }
Ejemplo n.º 34
0
 public static void ValidateDicomItem(this DicomItem dicomItem)
 {
     try
     {
         dicomItem.Validate();
     }
     catch (DicomValidationException ex)
     {
         throw new DatasetValidationException(
                   FailureReasonCodes.ValidationFailure,
                   ex.Message,
                   ex);
     }
 }
 private static bool ValueEquals(DicomItem a, DicomItem b)
 {
   if (a == null || b == null)
     return a == b;
   else if (a == b)
     return true;
   else if (a.ValueRepresentation != b.ValueRepresentation || (uint)a.Tag != (uint)b.Tag)
     return false;
   else if (a is DicomElement)
   {
     if (b is DicomElement == false)
       return false;
     else if (b is DicomStringElement && a is DicomDecimalString)
       return ((DicomDecimalString)a).Get<decimal[]>().SequenceEqual(((DicomDecimalString)b).Get<decimal[]>());
     else
       return ValueEquals(((DicomElement)a).Buffer, ((DicomElement)b).Buffer);
   }
   else if (a is DicomSequence)
   {
     if (b is DicomSequence == false)
       return false;
     else
       return ((DicomSequence)a).Items.Zip(((DicomSequence)b).Items, (x, y) => ValueEquals(x, y)).All(x => x);
   }
   else if (a is DicomFragmentSequence)
   {
     if (b is DicomFragmentSequence == false)
       return false;
     else
       return ((DicomFragmentSequence)a).Fragments.Zip(((DicomFragmentSequence)b).Fragments, (x, y) => ValueEquals(x, y)).All(x => x);
   }
   else
     return a.Equals(b);
 }