Example #1
0
        public void PrivateAttributeTest()
        {
            DicomFile file = new DicomFile("LittleEndianPrivateReadFileTest.dcm");

            DicomAttributeCollection dataSet = file.DataSet;

            SetupMR(dataSet);
            dataSet[DicomTags.StudyDescription].SetNullValue();

            List <DicomTag> tagList = new List <DicomTag>();

            SetupPrivateGroups(tagList, dataSet);

            SetupMetaInfo(file);


            // Little Endian Tests
            file.TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;

            DicomReadOptions readOptions = DicomReadOptions.Default;

            // Use ExplicitLengthSequence to force SQ attributes to UN VR when they're read back in
            Assert.IsTrue(file.Save(DicomWriteOptions.ExplicitLengthSequence));

            DicomFile newFile = new DicomFile(file.Filename);

            newFile.Load(readOptions);

            Assert.IsTrue(newFile.DataSet[DicomTags.StudyDescription].IsNull);

            Assert.AreNotEqual(file.DataSet.Equals(newFile.DataSet), true);

            foreach (DicomTag tag in tagList)
            {
                DicomAttributeUN unAttrib = newFile.DataSet[tag] as DicomAttributeUN;
                Assert.IsNotNull(unAttrib, String.Format("UN VR Attribute is not null for tag {0}", tag));

                ByteBuffer bb = unAttrib.GetByteBuffer(TransferSyntax.ImplicitVrLittleEndian,
                                                       newFile.DataSet[DicomTags.SpecificCharacterSet].ToString());
                Assert.IsNotNull(bb, String.Format("ByteBuffer not null for tag: {0}", tag));

                DicomAttribute validAttrib = tag.VR.CreateDicomAttribute(tag, bb);
                Assert.IsNotNull(validAttrib);

                Assert.IsTrue(validAttrib.Equals(file.DataSet[tag]), String.Format("Attributes equal for tag {0}", tag));
            }
        }
Example #2
0
        /// <summary>
        /// Compare at the IMAGE level if a query matches the data in an <see cref="InstanceXml"/> file.
        /// </summary>
        /// <param name="queryMessage"></param>
        /// <param name="matchTagList"></param>
        /// <param name="instanceStream"></param>
        /// <returns></returns>
        private static bool CompareInstanceMatch(DicomAttributeCollection queryMessage, IEnumerable <uint> matchTagList,
                                                 InstanceXml instanceStream)
        {
            foreach (uint tag in matchTagList)
            {
                if (!instanceStream.Collection.Contains(tag))
                {
                    continue;
                }

                DicomAttribute sourceAttrib = queryMessage[tag];
                DicomAttribute matchAttrib  = instanceStream.Collection[tag];

                if (sourceAttrib.Tag.VR.Equals(DicomVr.SQvr))
                {
                    continue; // TODO
                }
                if (sourceAttrib.IsNull)
                {
                    continue;
                }

                string sourceString = sourceAttrib.ToString();
                if (sourceString.Contains("*") || sourceString.Contains("?"))
                {
                    sourceString = sourceString.Replace("*", "[\x21-\x7E]");
                    sourceString = sourceString.Replace("?", ".");
                    if (!Regex.IsMatch(matchAttrib.ToString(), sourceString))
                    {
                        return(false);
                    }
                }
                else if (!sourceAttrib.Equals(matchAttrib))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #3
0
        private void ValidateEqualExceptExclusions(DicomAttributeCollection collection1, DicomAttributeCollection collection2, params uint[] ignoreTags)
        {
            IEnumerator <DicomAttribute> iterator1 = collection1.GetEnumerator();
            IEnumerator <DicomAttribute> iterator2 = collection2.GetEnumerator();

            while (iterator1.MoveNext())
            {
                bool ignored = false;
                foreach (uint ignoreTag in ignoreTags)
                {
                    if (ignoreTag == iterator1.Current.Tag.TagValue)
                    {
                        ignored = true;
                        break;
                    }
                }

                if (ignored)
                {
                    continue;
                }

                DicomAttribute attribute = iterator1.Current;

                if (collection1 is InstanceXmlDicomAttributeCollection)
                {
                    if (((InstanceXmlDicomAttributeCollection)collection1).ExcludedTags.Contains(attribute.Tag))
                    {
                        continue;
                    }
                }

                if (collection2 is InstanceXmlDicomAttributeCollection)
                {
                    if (((InstanceXmlDicomAttributeCollection)collection2).ExcludedTags.Contains(attribute.Tag))
                    {
                        continue;
                    }
                }

                while (true)
                {
                    if (!iterator2.MoveNext())
                    {
                        throw new Exception("Tag exists in collection 1, but not collection 2.");
                    }
                    else if (iterator2.Current.Tag == iterator1.Current.Tag)
                    {
                        break;
                    }
                    else
                    {
                        ignored = false;
                        foreach (uint ignoreTag in ignoreTags)
                        {
                            if (ignoreTag == iterator2.Current.Tag.TagValue)
                            {
                                ignored = true;
                                break;
                            }
                        }

                        if (ignored)
                        {
                            continue;
                        }
                        else if (!iterator2.Current.IsEmpty)
                        {
                            throw new Exception("Tag exists in collection 2, but not collection 1.");
                        }
                    }
                }

                DicomAttribute attribute2 = iterator2.Current;
                if (attribute2.IsEmpty && attribute.IsEmpty)
                {
                    continue;
                }

                if (!attribute.Equals(attribute2))
                {
                    throw new Exception("Attributes should be equal.");
                }
            }

            while (iterator2.MoveNext())
            {
                bool ignored = false;
                foreach (uint ignoreTag in ignoreTags)
                {
                    if (ignoreTag == iterator2.Current.Tag.TagValue)
                    {
                        ignored = true;
                        break;
                    }
                }

                if (ignored)
                {
                    continue;
                }
                else if (!iterator2.Current.IsEmpty)
                {
                    throw new Exception("Tag exists in collection 2, but not collection 1.");
                }
            }
        }
Example #4
0
        public void PrivateAttributeTest()
        {
            DicomFile file = new DicomFile("LittleEndianPrivateReadFileTest.dcm");

            DicomAttributeCollection dataSet = file.DataSet;

            SetupMR(dataSet);

            DicomTag privateCreatorTag = new DicomTag(0x00090010, "PrivateCreator", "PrivateCreator", DicomVr.LOvr, false, 1, 1,
                                                      false);

            dataSet[privateCreatorTag].SetStringValue("ClearCanvasGroup9");

            DicomTag privateTagFL = new DicomTag(0x00091020, "PrivateFL", "PrivateFL", DicomVr.FLvr, true, 1, 10, false);
            DicomTag privateTagLO = new DicomTag(0x00091021, "PrivateLO", "PrivateLO", DicomVr.LOvr, true, 1, 10, false);
            DicomTag privateTagAE = new DicomTag(0x00091022, "PrivateAE", "PrivateAE", DicomVr.AEvr, true, 1, 10, false);
            DicomTag privateTagAS = new DicomTag(0x00091023, "PrivateAS", "PrivateAS", DicomVr.ASvr, true, 1, 10, false);
            DicomTag privateTagAT = new DicomTag(0x00091024, "PrivateAT", "PrivateAT", DicomVr.ATvr, true, 1, 10, false);
            DicomTag privateTagCS = new DicomTag(0x00091025, "PrivateCS", "PrivateCS", DicomVr.CSvr, true, 1, 10, false);
            DicomTag privateTagDA = new DicomTag(0x00091026, "PrivateDA", "PrivateDA", DicomVr.DAvr, true, 1, 10, false);
            DicomTag privateTagDS = new DicomTag(0x00091027, "PrivateDS", "PrivateDS", DicomVr.DSvr, true, 1, 10, false);
            DicomTag privateTagDT = new DicomTag(0x00091028, "PrivateDT", "PrivateDT", DicomVr.DTvr, true, 1, 10, false);
            DicomTag privateTagFD = new DicomTag(0x00091029, "PrivateFD", "PrivateFD", DicomVr.FDvr, true, 1, 10, false);
            DicomTag privateTagIS = new DicomTag(0x00091030, "PrivateIS", "PrivateIS", DicomVr.ISvr, true, 1, 10, false);
            DicomTag privateTagLT = new DicomTag(0x00091031, "PrivateLT", "PrivateLT", DicomVr.LTvr, true, 1, 1, false);
            DicomTag privateTagOB = new DicomTag(0x00091032, "PrivateOB", "PrivateOB", DicomVr.OBvr, true, 1, 1, false);
            DicomTag privateTagOF = new DicomTag(0x00091033, "PrivateOF", "PrivateOF", DicomVr.OFvr, true, 1, 1, false);
            DicomTag privateTagOW = new DicomTag(0x00091034, "PrivateOW", "PrivateOW", DicomVr.OWvr, true, 1, 1, false);
            DicomTag privateTagPN = new DicomTag(0x00091035, "PrivatePN", "PrivatePN", DicomVr.PNvr, true, 1, 10, false);
            DicomTag privateTagSH = new DicomTag(0x00091036, "PrivateSH", "PrivateSH", DicomVr.SHvr, true, 1, 10, false);
            DicomTag privateTagSL = new DicomTag(0x00091037, "PrivateSL", "PrivateSL", DicomVr.SLvr, true, 1, 10, false);
            DicomTag privateTagSQ = new DicomTag(0x00091038, "PrivateSQ", "PrivateSQ", DicomVr.SQvr, true, 1, 10, false);
            DicomTag privateTagSS = new DicomTag(0x00091039, "PrivateSS", "PrivateSS", DicomVr.SSvr, true, 1, 10, false);
            DicomTag privateTagST = new DicomTag(0x00091040, "PrivateST", "PrivateST", DicomVr.STvr, true, 1, 10, false);
            DicomTag privateTagTM = new DicomTag(0x00091041, "PrivateTM", "PrivateTM", DicomVr.TMvr, true, 1, 10, false);
            DicomTag privateTagUI = new DicomTag(0x00091042, "PrivateUI", "PrivateUI", DicomVr.UIvr, true, 1, 10, false);
            DicomTag privateTagUL = new DicomTag(0x00091043, "PrivateUI", "PrivateUI", DicomVr.ULvr, true, 1, 10, false);
            DicomTag privateTagUS = new DicomTag(0x00091044, "PrivateUS", "PrivateUS", DicomVr.USvr, true, 1, 10, false);
            DicomTag privateTagUT = new DicomTag(0x00091045, "PrivateUT", "PrivateUT", DicomVr.UTvr, true, 1, 1, false);

            List <DicomTag> tagList = new List <DicomTag>();


            dataSet[privateTagFL].AppendFloat32(1.1f);
            dataSet[privateTagFL].AppendFloat32(1.1123132f);
            tagList.Add(privateTagFL);
            dataSet[privateTagLO].AppendString("Test");
            dataSet[privateTagLO].AppendString("Test Me 2");
            tagList.Add(privateTagLO);
            dataSet[privateTagAE].AppendString("TESTAE1");
            dataSet[privateTagAE].AppendString("TESTAE2");
            tagList.Add(privateTagAE);
            dataSet[privateTagAS].AppendString("003Y");
            dataSet[privateTagAS].AppendString("003D");
            tagList.Add(privateTagAS);
            dataSet[privateTagAT].AppendUInt32(DicomTags.ZoomFactor);
            dataSet[privateTagAT].AppendUInt32(DicomTags.PhotometricInterpretation);
            tagList.Add(privateTagAT);
            dataSet[privateTagCS].AppendString("CODE1");
            dataSet[privateTagCS].AppendString("CODE2");
            tagList.Add(privateTagCS);
            dataSet[privateTagDA].AppendDateTime(DateTime.Now);
            dataSet[privateTagDA].AppendDateTime(DateTime.Now);
            tagList.Add(privateTagDA);
            dataSet[privateTagDS].AppendFloat64(1.12351234124124f);
            dataSet[privateTagDS].AppendFloat64(-12312312312.1231f);
            tagList.Add(privateTagDS);
            dataSet[privateTagDT].AppendDateTime(DateTime.Now);
            dataSet[privateTagDT].AppendDateTime(DateTime.Now);
            tagList.Add(privateTagDT);
            dataSet[privateTagFD].AppendFloat64(1.112312d);
            dataSet[privateTagFD].AppendFloat64(-11123.13211d);
            tagList.Add(privateTagFD);
            dataSet[privateTagIS].AppendString("123456789");
            dataSet[privateTagIS].AppendString("123456789876");
            tagList.Add(privateTagIS);
            dataSet[privateTagLT].SetStringValue("Now is the time for all good men to come to the aide of their country.");
            tagList.Add(privateTagLT);
            dataSet[privateTagPN].AppendString("Last^First^Middle^Post");
            dataSet[privateTagPN].AppendString("W^Steven^R^Test");
            tagList.Add(privateTagPN);
            dataSet[privateTagST].SetStringValue("Now is the time for all good men to come to the aide of their country.");
            tagList.Add(privateTagST);
            dataSet[privateTagSH].AppendString("Short text 1");
            dataSet[privateTagSH].AppendString("Short text 2");
            dataSet[privateTagSH].AppendString("Short text 3");
            dataSet[privateTagSH].AppendString("Short text 4");
            tagList.Add(privateTagSH);
            dataSet[privateTagSL].AppendInt32(1024);
            dataSet[privateTagSL].AppendInt32(2048);
            dataSet[privateTagSL].AppendInt32(4096);
            dataSet[privateTagSL].AppendInt32(8192);
            dataSet[privateTagSL].AppendInt32(-1024);
            dataSet[privateTagSL].AppendInt32(-2048);
            dataSet[privateTagSL].AppendInt32(-4096);
            dataSet[privateTagSL].AppendInt32(-8192);
            tagList.Add(privateTagSL);
            dataSet[privateTagSS].AppendInt16(-1024);
            dataSet[privateTagSS].AppendInt16(512);
            dataSet[privateTagSS].AppendInt16(-256);
            dataSet[privateTagSS].AppendInt16(128);
            tagList.Add(privateTagSS);
            dataSet[privateTagTM].AppendDateTime(DateTime.Now);
            dataSet[privateTagTM].AppendDateTime(DateTime.Now);
            tagList.Add(privateTagTM);
            dataSet[privateTagUI].AppendUid(DicomUid.GenerateUid());
            dataSet[privateTagUI].AppendUid(DicomUid.GenerateUid());
            dataSet[privateTagUI].AppendUid(DicomUid.GenerateUid());
            dataSet[privateTagUI].AppendUid(DicomUid.GenerateUid());
            dataSet[privateTagUI].AppendUid(DicomUid.GenerateUid());
            dataSet[privateTagUI].AppendUid(DicomUid.GenerateUid());
            tagList.Add(privateTagUI);
            dataSet[privateTagUL].AppendUInt32(128);
            dataSet[privateTagUL].AppendUInt32(1024);
            dataSet[privateTagUL].AppendUInt32(16384);
            dataSet[privateTagUL].AppendUInt32(123123123);
            tagList.Add(privateTagUL);
            dataSet[privateTagUS].AppendUInt16(128);
            dataSet[privateTagUS].AppendUInt16(64);
            dataSet[privateTagUS].AppendUInt16(256);
            tagList.Add(privateTagUS);
            dataSet[privateTagUT].SetStringValue("A man, a plan, a canal, panama.");
            tagList.Add(privateTagUT);

            dataSet[privateTagOB].Values = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
            tagList.Add(privateTagOB);

            dataSet[privateTagOW].Values = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
            tagList.Add(privateTagOW);

            dataSet[privateTagOF].Values = new[] { 1.1111f, 2.222f, 3.3333f, 44444.444f, 5555.555f, 66666.66666f, 123123.123123f };
            tagList.Add(privateTagOF);


            DicomSequenceItem item = new DicomSequenceItem();

            item[DicomTags.PhotometricInterpretation].AppendString("MONOCHROME1");
            item[DicomTags.Rows].AppendUInt16(256);
            item[DicomTags.Columns].AppendUInt16(256);
            item[DicomTags.BitsAllocated].AppendUInt16(8);
            item[DicomTags.BitsStored].AppendUInt16(8);
            item[DicomTags.HighBit].AppendUInt16(7);
            dataSet[privateTagSQ].AddSequenceItem(item);
            // SQ Attribute UN parsing does not work right now, don't add to the list.
            //tagList.Add(privateTagSQ);

            SetupMetaInfo(file);

            dataSet[DicomTags.StudyDescription].SetNullValue();

            // Little Endian Tests
            file.TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;

            DicomReadOptions readOptions = DicomReadOptions.Default;

            // Use ExplicitLengthSequence to force SQ attributes to UN VR when they're ready back in
            bool result = file.Save(DicomWriteOptions.ExplicitLengthSequence);

            Assert.AreEqual(result, true);

            DicomFile newFile = new DicomFile(file.Filename);

            newFile.Load(readOptions);

            Assert.IsTrue(newFile.DataSet[DicomTags.StudyDescription].IsNull);

            Assert.AreNotEqual(file.DataSet.Equals(newFile.DataSet), true);

            foreach (DicomTag tag in tagList)
            {
                DicomAttributeUN unAttrib = newFile.DataSet[tag] as DicomAttributeUN;
                Assert.IsNotNull(unAttrib, String.Format("UN VR Attribute is not null for tag {0}", tag));

                ByteBuffer bb = unAttrib.GetByteBuffer(TransferSyntax.ImplicitVrLittleEndian,
                                                       newFile.DataSet[DicomTags.SpecificCharacterSet].ToString());
                Assert.IsNotNull(bb, String.Format("ByteBuffer not null for tag: {0}", tag));

                DicomAttribute validAttrib = tag.VR.CreateDicomAttribute(tag, bb);
                Assert.IsNotNull(validAttrib);

                Assert.IsTrue(validAttrib.Equals(file.DataSet[tag]), String.Format("Attributes equal for tag {0}", tag));
            }
        }