Exemplo n.º 1
0
        public void AddResult(DicomDataset ds, Exception ex)
        {
            var referencedInstance = GetReferencedInstsance(ds);
            var failedSeq          = new fo.DicomSequence(fo.DicomTag.FailedSOPSequence);
            var item = new fo.DicomDataset( );


            referencedInstance.Merge(item);

            _dataset.AddOrUpdate(failedSeq);
            failedSeq.Items.Add(item);

            item.AddOrUpdate <UInt16> (fo.DicomTag.FailureReason, 272);  //TODO: for now 272 == "0110 - Processing failure", must map proper result code from org. exception

            item.AddOrUpdate <string> (fo.DicomTag.RetrieveURI, ex.Message);

            if (_successAdded)
            {
                HttpStatus = HttpStatusCode.Accepted;
            }
            else
            {
                HttpStatus = HttpStatusCode.Conflict;  //should figure out the true reason from a wrapped exception code
            }

            _failureAdded = true;
        }
Exemplo n.º 2
0
        private fo.DicomDataset GetReferencedInstsance(fo.DicomDataset ds)
        {
            var classUID = ds.Get <fo.DicomElement> (fo.DicomTag.SOPClassUID, null);
            var sopUID   = ds.Get <fo.DicomElement> (fo.DicomTag.SOPInstanceUID, null);
            var dataset  = new fo.DicomDataset( );


            dataset.AddOrUpdate(classUID);
            dataset.AddOrUpdate(sopUID);

            return(dataset);
        }
Exemplo n.º 3
0
        public fo.DicomDataset GetResponseContent( )
        {
            string studyUrl = "";

            if (null != StudyId)
            {
                studyUrl = UrlProvider.GetStudyUrl(StudyId);
            }

            _dataset.AddOrUpdate <string>(fo.DicomTag.RetrieveURI, studyUrl);

            return(_dataset);
        }
Exemplo n.º 4
0
        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);
                }
            }
        }
Exemplo n.º 5
0
        public void AddResult(DicomDataset ds)
        {
            var referencedInstance = GetReferencedInstsance(ds);
            var referencedSeq      = new fo.DicomSequence(fo.DicomTag.ReferencedInstanceSequence);
            var item = new fo.DicomDataset( );


            referencedInstance.Merge(item);

            _dataset.AddOrUpdate(referencedSeq);
            referencedSeq.Items.Add(item);

            item.AddOrUpdate <string> (fo.DicomTag.RetrieveURI, UrlProvider.GetInstanceUrl(new ObjectId(ds)));


            if (_failureAdded)
            {
                HttpStatus = HttpStatusCode.Accepted;
            }
            else
            {
                HttpStatus = HttpStatusCode.OK;
            }

            _successAdded = true;
        }
Exemplo n.º 6
0
        private void CreateSequence(List <string> elements, int currentElementIndex, fo.DicomDataset dicomRequest, string value)
        {
            uint tag      = uint.Parse(elements[currentElementIndex], System.Globalization.NumberStyles.HexNumber);//TODO: need to handle the case of keywords
            var  dicEntry = fo.DicomDictionary.Default[tag];

            fo.DicomSequence sequence;
            fo.DicomDataset  item;

            dicomRequest.AddOrUpdate(new fo.DicomSequence(dicEntry.Tag));
            sequence = dicomRequest.Get <fo.DicomSequence>(dicEntry.Tag);


            item = new fo.DicomDataset( );

            sequence.Items.Add(item);


            for (int index = (currentElementIndex + 1); index < elements.Count; index++)
            {
                tag = uint.Parse(elements[index], System.Globalization.NumberStyles.HexNumber);

                dicEntry = fo.DicomDictionary.Default[tag];

                if (dicEntry.ValueRepresentations.Contains(fo.DicomVR.SQ))
                {
                    CreateSequence(elements, index, item, value);

                    break;
                }
                else
                {
                    item.AddOrUpdate <string> (tag, value);
                }
            }
        }
Exemplo n.º 7
0
        protected virtual void ReadVr_SQ(JsonTextReader reader, fo.DicomTag tag, fo.DicomDataset dataset, int level)
        {
            fo.DicomSequence seq = new fo.DicomSequence(tag, new fo.DicomDataset[0]);


            if (reader.Value as string == JsonConstants.ValueField)
            {
                while (reader.Read( ) && reader.TokenType == JsonToken.StartArray)
                {
                    while (reader.Read( ) && reader.TokenType != JsonToken.EndArray)
                    {
                        fo.DicomDataset itemDs = new fo.DicomDataset( );

                        ReadChildren(reader, itemDs, ++level);

                        --level;

                        seq.Items.Add(itemDs);
                    }

                    break;
                }
            }

            dataset.AddOrUpdate(seq);
        }
Exemplo n.º 8
0
 public static void Merge(this fo.DicomDataset source, fo.DicomDataset destination)
 {
     foreach (var element in source)
     {
         destination.AddOrUpdate(element);
     }
 }
Exemplo n.º 9
0
        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);
                }
            }
        }
Exemplo n.º 10
0
        private void ReadSequence
        (
            fo.DicomDataset ds,
            XElement element,
            fo.DicomTag tag,
            int level
        )
        {
            fo.DicomSequence seq = new fo.DicomSequence(tag, new fo.DicomDataset[0]);


            foreach (var item in  element.Elements(Constants.ATTRIBUTE_ITEM_NAME))
            {
                fo.DicomDataset itemDs = new fo.DicomDataset( );

                level++;

                ReadChildren(itemDs, item, level);

                level--;

                seq.Items.Add(itemDs);
            }

            ds.AddOrUpdate(seq);
        }
Exemplo n.º 11
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_src))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _dst);
         dataset.AddOrUpdate(_dst, dataset.GetDicomItem <DicomElement>(_src).Buffer);
     }
 }
Exemplo n.º 12
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (_match.Match(dataset))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _match.Tag);
         dataset.AddOrUpdate(_match.Tag, _value);
     }
 }
Exemplo n.º 13
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag) && dataset.Get <string>(_tag, -1, String.Empty) == _match)
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         dataset.AddOrUpdate(_tag, _value);
     }
 }
Exemplo n.º 14
0
        private void TestAddElementToDatasetAsString <T>(DicomElement element, T[] testValues)
        {
            DicomDataset ds = new DicomDataset();

            string[] stringValues;


            if (typeof(T) == typeof(string))
            {
                stringValues = testValues.Cast <string>().ToArray();
            }
            else
            {
                stringValues = testValues.Select(x => x.ToString()).ToArray();
            }


            ds.AddOrUpdate(element.Tag, stringValues);


            for (int index = 0; index < element.Count; index++)
            {
                string val;

                val = GetStringValue(element, ds, index);

                Assert.Equal(stringValues[index], val);
            }

            if (element.Tag.DictionaryEntry.ValueMultiplicity.Maximum > 1)
            {
                var stringValue = string.Join("\\", testValues);

                ds.AddOrUpdate(element.Tag, stringValue);

                for (int index = 0; index < element.Count; index++)
                {
                    string val;

                    val = GetStringValue(element, ds, index);

                    Assert.Equal(stringValues[index], val);
                }
            }
        }
Exemplo n.º 15
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var value = dataset.GetString(_tag);
         dataset.AddOrUpdate(_tag, value.ToLower());
     }
 }
Exemplo n.º 16
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var value = dataset.Get <string>(_tag, -1, String.Empty);
         dataset.AddOrUpdate(_tag, value + _append);
     }
 }
Exemplo n.º 17
0
        public void DicomValidation_ValidateUID()
        {
            var ds       = new DicomDataset();
            var validUid = "1.2.315.6666.0.0.0.8965.19187632.1";

            ds.Add(DicomTag.StudyInstanceUID, validUid);
            Assert.Equal(validUid, ds.GetSingleValue <string>(DicomTag.StudyInstanceUID));

            var tooLongUid = validUid + "." + validUid;
            var ex         = Assert.ThrowsAny <DicomValidationException>(() => ds.AddOrUpdate(DicomTag.StudyInstanceUID, tooLongUid));

            Assert.Contains("length", ex.Message);

            var leadingZeroUid = validUid + ".03";
            var ex2            = Assert.ThrowsAny <DicomValidationException>(() => ds.AddOrUpdate(DicomTag.SeriesInstanceUID, leadingZeroUid));

            Assert.Contains("leading zero", ex2.Message);
        }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var value = dataset.GetString(_tag);
         value = Regex.Replace(value, _pattern, _replacement);
         dataset.AddOrUpdate(_tag, value);
     }
 }
Exemplo n.º 20
0
 /// <summary>
 /// Copies tags to the destination dataset.
 /// </summary>
 /// <param name="destination">Destination Dataset</param>
 /// <param name="tags">Tags to copy</param>
 /// <returns>Current Dataset</returns>
 public DicomDataset CopyTo(DicomDataset destination, params DicomTag[] tags)
 {
     if (destination != null)
     {
         foreach (var tag in tags)
         {
             destination.AddOrUpdate(Get <DicomItem>(tag));
         }
     }
     return(this);
 }
Exemplo n.º 21
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var      value = dataset.GetString(_tag);
         string[] parts = value.Split(_seperators);
         value = String.Format(_format, parts);
         dataset.AddOrUpdate(_tag, value);
     }
 }
Exemplo n.º 22
0
        public void AddOrUpdate_NonDefaultEncodedStringElement_StringIsPreserved()
        {
            var          encoding = Encoding.GetEncoding("SHIFT_JIS");
            var          tag      = DicomTag.AdditionalPatientHistory;
            const string expected = "YamadaTarou山田太郎ヤマダタロウ";

            var dataset = new DicomDataset();

            dataset.AddOrUpdate(tag, encoding, expected);

            var actual = encoding.GetString(dataset.GetDicomItem <DicomElement>(tag).Buffer.Data);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 23
0
        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;
            }
        }
Exemplo n.º 24
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var value = dataset.GetString(_tag);
         if (_totalLength < 0)
         {
             value = value.PadLeft(-_totalLength, _paddingChar);
         }
         else
         {
             value = value.PadRight(_totalLength, _paddingChar);
         }
         dataset.AddOrUpdate(_tag, value);
     }
 }
Exemplo n.º 25
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var      value = dataset.GetString(_tag);
         string[] parts = value.Split('\\');
         for (int i = 0; i < parts.Length; i++)
         {
             if (parts[i].Length > _length)
             {
                 parts[i] = parts[i].Substring(0, _length);
             }
         }
         value = String.Join("\\", parts);
         dataset.AddOrUpdate(_tag, value);
     }
 }
Exemplo n.º 26
0
        /// <summary>
        /// Regenerate all UIDs in a DICOM dataset.
        /// </summary>
        /// <param name="dataset">Dataset in which UIDs should be regenerated.</param>
        public void RegenerateAll(DicomDataset dataset)
        {
            foreach (var ui in dataset.Where(x => x.ValueRepresentation == DicomVR.UI).ToArray())
            {
                var uid = dataset.Get <DicomUID>(ui.Tag);
                if (uid.Type == DicomUidType.SOPInstance || uid.Type == DicomUidType.Unknown)
                {
                    dataset.AddOrUpdate(ui.Tag, this.Generate(uid));
                }
            }

            foreach (var sq in dataset.Where(x => x.ValueRepresentation == DicomVR.SQ).Cast <DicomSequence>().ToArray())
            {
                foreach (var item in sq)
                {
                    this.RegenerateAll(item);
                }
            }
        }
Exemplo n.º 27
0
 public void Transform(DicomDataset dataset, DicomDataset modifiedAttributesSequenceItem = null)
 {
     if (dataset.Contains(_tag))
     {
         dataset.CopyTo(modifiedAttributesSequenceItem, _tag);
         var value = dataset.GetString(_tag);
         if (_position == DicomTrimPosition.Both)
         {
             if (_trim != null)
             {
                 value = value.Trim(_trim);
             }
             else
             {
                 value = value.Trim();
             }
         }
         else if (_position == DicomTrimPosition.Start)
         {
             if (_trim != null)
             {
                 value = value.TrimStart(_trim);
             }
             else
             {
                 value = value.TrimStart();
             }
         }
         else
         {
             if (_trim != null)
             {
                 value = value.TrimEnd(_trim);
             }
             else
             {
                 value = value.TrimEnd();
             }
         }
         dataset.AddOrUpdate(_tag, value);
     }
 }
Exemplo n.º 28
0
        public void Construction_sets_validation_to_false_copy_constructor(bool validate, string sopInstanceUid, bool expectedError)
        {
            DicomFileMetaInformation existing, metaInfo;
            var dataset = new DicomDataset {
                ValidateItems = validate
            };

            if (expectedError)
            {
                // we test the dataset as well here
                Assert.Throws <DicomValidationException>(() => dataset.Add(
                                                             new DicomUniqueIdentifier(DicomTag.SOPClassUID, DicomUID.SecondaryCaptureImageStorage),
                                                             new DicomUniqueIdentifier(DicomTag.SOPInstanceUID, sopInstanceUid)));

                // such that we can construct it
                dataset.ValidateItems = false;
                dataset.AddOrUpdate(
                    new DicomUniqueIdentifier(DicomTag.SOPClassUID, DicomUID.SecondaryCaptureImageStorage),
                    new DicomUniqueIdentifier(DicomTag.SOPInstanceUID, sopInstanceUid));
                dataset.ValidateItems = validate;

                Assert.Throws <DicomValidationException>(() => new DicomFileMetaInformation(dataset));

                return;
            }
            else
            {
                dataset.Add(
                    new DicomUniqueIdentifier(DicomTag.SOPClassUID, DicomUID.SecondaryCaptureImageStorage),
                    new DicomUniqueIdentifier(DicomTag.SOPInstanceUID, sopInstanceUid));
                existing = new DicomFileMetaInformation(dataset);
            }

            metaInfo = new DicomFileMetaInformation(existing);

            Assert.Equal(validate, metaInfo.ValidateItems);
            Assert.Equal(validate, metaInfo.AutoValidate);
            Assert.Equal(sopInstanceUid, metaInfo.MediaStorageSOPInstanceUID.UID);
        }
Exemplo n.º 29
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);
        }
Exemplo n.º 30
0
        protected virtual void ReadVr_PN(JsonTextReader reader, fo.DicomTag tag, fo.DicomDataset dataset)
        {
            List <string> pnNames = new List <string> ( );


            while (reader.Read( ) && reader.TokenType == JsonToken.StartArray)
            {
                PersonNameValue personName = new PersonNameValue( );


                //keep reading until reach end of array
                while (reader.Read( ) && reader.TokenType != JsonToken.EndArray)
                {
                    PersonNameReader pnReader = new PersonNameReader( );


                    while (reader.Read( ) && reader.TokenType == JsonToken.PropertyName)
                    {
                        string componentName = (string)reader.Value;
                        string component     = "";


                        if (reader.Read( ))
                        {
                            component = (string)reader.Value;
                        }

                        pnReader.Add(componentName, component);
                    }

                    personName.Add(pnReader);
                }

                dataset.AddOrUpdate <string> (tag, personName.ToString( ));

                break;
            }
        }