Esempio n. 1
0
        /// <summary>
        /// Save the <see cref="StudyXml"/> file for a study.
        /// </summary>
        /// <param name="studyXml">The <see cref="StudyXml"/> file to save.</param>
        /// <param name="fileCreated">flag set to true if the file was created</param>
        public void SaveStudyXml(StudyXml studyXml, out bool fileCreated)
        {
            var settings = new StudyXmlOutputSettings
            {
                IncludePrivateValues  = StudyXmlTagInclusion.IgnoreTag,
                IncludeUnknownTags    = StudyXmlTagInclusion.IgnoreTag,
                IncludeLargeTags      = StudyXmlTagInclusion.IncludeTagExclusion,
                MaxTagLength          = 2048,
                IncludeSourceFileName = true
            };

            var    doc        = studyXml.GetMemento(settings);
            string streamFile = GetStudyXmlPath();

            // allocate the random number generator here, in case we need it below
            var    rand          = new Random();
            string tmpStreamFile = streamFile + "_tmp";

            for (int i = 0; ; i++)
            {
                try
                {
                    if (File.Exists(tmpStreamFile))
                    {
                        FileUtils.Delete(tmpStreamFile);
                    }

                    using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(tmpStreamFile, FileMode.CreateNew))
                    {
                        StudyXmlIo.Write(doc, xmlStream);
                        xmlStream.Close();
                    }

                    File.Copy(tmpStreamFile, streamFile, true);
                    fileCreated = true;
                    FileUtils.Delete(tmpStreamFile);
                    return;
                }
                catch (IOException)
                {
                    if (i < 5)
                    {
                        Thread.Sleep(rand.Next(5, 50)); // Sleep 5-50 milliseconds
                        continue;
                    }

                    throw;
                }
            }
        }
Esempio n. 2
0
        public void TestExcludeBinaryTags()
        {
            List <DicomFile> images = SetupImages(3);

            images[2].DataSet[DicomTags.SpectroscopyData].Values = new float[6];

            StudyXml xml = new StudyXml();

            foreach (DicomFile file in images)
            {
                file.DataSet[DicomTags.RedPaletteColorLookupTableData].Values   = new byte[256];
                file.DataSet[DicomTags.GreenPaletteColorLookupTableData].Values = new byte[256];
                file.DataSet[DicomTags.BluePaletteColorLookupTableData].Values  = new byte[256];
                xml.AddFile(file);
            }

            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();

            settings.MaxTagLength = 100;
            XmlDocument doc = xml.GetMemento(settings);

            //SaveStudyXml(doc, @"C:\stewart\testxml.xml");
            List <InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);

            foreach (InstanceXmlDicomAttributeCollection dataSet in dataSets)
            {
                Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.RedPaletteColorLookupTableData));
                Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.GreenPaletteColorLookupTableData));
                Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.BluePaletteColorLookupTableData));
            }

            //This attribute has a short value, so it should not be excluded.
            Assert.IsFalse(dataSets[2].IsTagExcluded(DicomTags.SpectroscopyData));

            xml = new StudyXml();
            xml.SetMemento(doc);

            dataSets = GetInstanceXmlDataSets(xml);
            foreach (InstanceXmlDicomAttributeCollection dataSet in dataSets)
            {
                Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.RedPaletteColorLookupTableData));
                Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.GreenPaletteColorLookupTableData));
                Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.BluePaletteColorLookupTableData));
            }

            //This attribute has a short value, so it should not be excluded.
            Assert.IsFalse(dataSets[2].IsTagExcluded(DicomTags.SpectroscopyData));
        }
Esempio n. 3
0
        private void _buttonGenerateXml_Click(object sender, EventArgs e)
        {
            saveFileDialog.DefaultExt = "xml";
            saveFileDialog.ShowDialog();

            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();

            settings.IncludeSourceFileName = false;
            XmlDocument doc = _theStream.GetMemento(settings);

            Stream fileStream = saveFileDialog.OpenFile();

            StudyXmlIo.Write(doc, fileStream);

            fileStream.Close();
        }
        public InsertStudyXmlCommand(DicomFile file, StudyXml studyXml, StudyLocation storageLocation, bool writeFile)
            : base("Insert Study Xml", true)
        {
            _file                 = file;
            _studyXml             = studyXml;
            _studyStorageLocation = storageLocation;
            _writeFile            = writeFile;

            _settings = new StudyXmlOutputSettings
            {
                IncludePrivateValues  = StudyXmlTagInclusion.IgnoreTag,
                IncludeUnknownTags    = StudyXmlTagInclusion.IgnoreTag,
                IncludeLargeTags      = StudyXmlTagInclusion.IncludeTagExclusion,
                MaxTagLength          = 2048,
                IncludeSourceFileName = true
            };
        }
Esempio n. 5
0
        private void TestOverlappingTag(string[] values, bool setMaxTagLength)
        {
            List <DicomFile> images = SetupImages(values.Length);

            SetTestAttribute(images, values);

            StudyXml newStudyXml;
            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();

            if (setMaxTagLength)
            {
                settings.MaxTagLength = 1024;
            }

            List <InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(images, out newStudyXml, settings);

            ValidateSimpleDataSets(values, dataSets, settings);
        }
Esempio n. 6
0
		private void WriteStudyStream(string streamFile, StudyXml theStream)
		{
			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			settings.IncludeSourceFileName = false;

			XmlDocument doc = theStream.GetMemento(settings);

			if (File.Exists(streamFile))
				File.Delete(streamFile);

			using (Stream fileStream = new FileStream(streamFile, FileMode.CreateNew))
			{
				StudyXmlIo.Write(doc, fileStream);
				fileStream.Close();
			}

			return;
		}
        public void Create()
        {
            SelectFolderDialogCreationArgs args = new SelectFolderDialogCreationArgs();

            args.Path = _lastFolder ?? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            FileDialogResult result = base.Context.DesktopWindow.ShowSelectFolderDialogBox(args);

            if (result.Action == DialogBoxAction.Ok)
            {
                _lastFolder = result.FileName;

                StudyLoaderExtensionPoint xp = new StudyLoaderExtensionPoint();
                IStudyLoader loader          = (IStudyLoader)CollectionUtils.SelectFirst(xp.CreateExtensions(),
                                                                                         delegate(object extension) { return(((IStudyLoader)extension).Name == "DICOM_LOCAL"); });

                var selected = base.Context.SelectedStudy;

                loader.Start(new StudyLoaderArgs(selected.StudyInstanceUid, selected.Server, StudyLoaderOptions.Default));
                StudyXml xml = new StudyXml();
                Sop      sop;

                while (null != (sop = loader.LoadNextSop()))
                {
                    xml.AddFile(((ILocalSopDataSource)sop.DataSource).File);
                }

                StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
                settings.IncludePrivateValues  = StudyXmlTagInclusion.IgnoreTag;
                settings.IncludeUnknownTags    = StudyXmlTagInclusion.IgnoreTag;
                settings.MaxTagLength          = 100 * 1024;
                settings.IncludeSourceFileName = true;

                XmlDocument doc      = xml.GetMemento(settings);
                string      fileName = System.IO.Path.Combine(result.FileName, "studyxml.xml");

                XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8);
                writer.Formatting  = Formatting.Indented;
                writer.Indentation = 5;

                doc.Save(writer);
            }
        }
        private InstanceXml GetInstanceXml(StudyXmlOutputSettings outputSettings, out DicomFile real)
        {
            var xml = new StudyXml();

            real = new DicomFile();
            SetupMR(real.DataSet);
            real.MediaStorageSopClassUid = real.DataSet[DicomTags.SopClassUid].ToString();
            real.MetaInfo[DicomTags.SopClassUid].SetString(0, real.DataSet[DicomTags.SopClassUid].ToString());

            var bytes = new Byte[2048];

            real.DataSet[DicomTags.RedPaletteColorLookupTableData].Values = bytes;

            var privateTag = new DicomTag(0x00111301, "Private Tag", "PrivateTag", DicomVr.CSvr, false, 1, 1, false);

            real.DataSet[privateTag].SetString(0, "Private");

            var sequences = (DicomSequenceItem[])real.DataSet[DicomTags.RequestAttributesSequence].Values;
            var firstItem = sequences.First();

            firstItem[DicomTags.RedPaletteColorLookupTableData].Values = bytes;

            var attr         = real.DataSet[DicomTags.ReferencedStudySequence];
            var sequenceItem = new DicomSequenceItem();

            attr.AddSequenceItem(sequenceItem);
            sequenceItem[privateTag].SetString(0, "Private");

            xml.AddFile(real);

            var memento = xml.GetMemento(outputSettings ?? new StudyXmlOutputSettings
            {
                IncludeLargeTags     = StudyXmlTagInclusion.IncludeTagExclusion,
                IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag,
                MaxTagLength         = 1024
            });

            xml = new StudyXml();
            xml.SetMemento(memento);
            return(xml.First().First());
        }
Esempio n. 9
0
        private void WriteStudyStream(string streamFile, StudyXml theStream)
        {
            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();

            settings.IncludeSourceFileName = false;

            XmlDocument doc = theStream.GetMemento(settings);

            if (File.Exists(streamFile))
            {
                File.Delete(streamFile);
            }

            using (Stream fileStream = new FileStream(streamFile, FileMode.CreateNew))
            {
                StudyXmlIo.Write(doc, fileStream);
                fileStream.Close();
            }

            return;
        }
Esempio n. 10
0
        internal void Flush()
        {
            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();

            settings.IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag;
            settings.IncludeUnknownTags   = StudyXmlTagInclusion.IgnoreTag;
            settings.IncludeLargeTags     = StudyXmlTagInclusion.IncludeTagExclusion;
            settings.MaxTagLength         = 2048;

            settings.IncludeSourceFileName = true;

            //Ensure the existing stuff is loaded.
            LoadStudyXml(false);

            XmlDocument doc = _studyXml.GetMemento(settings);

            using (FileStream stream = GetFileStream(FileMode.Create, FileAccess.Write))
            {
                StudyXmlIo.Write(doc, stream);
                stream.Close();
            }
        }
Esempio n. 11
0
        public void TestEqualAfterSerialization()
        {
            foreach (string[] testSet in _overlappingTagTestSets)
            {
                List <DicomFile> images = SetupImages(testSet.Length);
                SetTestAttribute(images, testSet);

                StudyXml xml = new StudyXml();

                foreach (DicomFile file in images)
                {
                    xml.AddFile(file);
                }

                StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
                settings.MaxTagLength = 1024;
                XmlDocument doc = xml.GetMemento(settings);

                List <InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
                int i = 0;
                foreach (DicomFile file in images)
                {
                    ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
                }

                xml = new StudyXml();
                xml.SetMemento(doc);

                dataSets = GetInstanceXmlDataSets(xml);
                i        = 0;
                foreach (DicomFile file in images)
                {
                    ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
                }
            }
        }
        public void Test_Excluded_PrivateSequenceAttribute()
        {
            DicomFile real;
            var       outputSettings = new StudyXmlOutputSettings
            {
                //If this were enabled, it would work.
                //IncludePrivateValues = StudyXmlTagInclusion.IncludeTagExclusion
            };

            var instanceXml = GetInstanceXml(outputSettings, out real);

            using (var instance = new TestInstance(instanceXml, real))
            {
                Assert.AreEqual("MR", instance[DicomTags.Modality].ToString());
                Assert.AreEqual(false, instance._fullHeaderLoaded);

                var sequences = (DicomSequenceItem[])instance[DicomTags.ReferencedStudySequence].Values;
                Assert.AreEqual(true, instance._fullHeaderLoaded);

                var sequenceItem     = sequences.First();
                var privateAttribute = sequenceItem[0x00111301];
                Assert.AreEqual("Private", privateAttribute.ToString());
            }
        }
Esempio n. 13
0
		private void TestOverlappingSequence(SqId[] sequenceIds, bool setMaxTagLength)
		{
			List<DicomFile> images = SetupImagesWithVoiLutSequenceA(sequenceIds.Length);

			for (int i = 0; i < sequenceIds.Length; ++i)
			{
				DicomFile image = images[i];
				SqId sequenceId = sequenceIds[i];
				if (sequenceId == SqId.X)
					image.DataSet[DicomTags.VoiLutSequence] = null;
				else if (sequenceId == SqId.B)
					MakeVoiLutSequenceB(image.DataSet);
			}

			StudyXml newStudyXml;
			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			if (setMaxTagLength)
				settings.MaxTagLength = 1024;

			List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(images, out newStudyXml, settings);

			for (int i = 0; i < dataSets.Count; ++i)
			{
				InstanceXmlDicomAttributeCollection dataSet = dataSets[i];
				SqId sequenceId = sequenceIds[i];

				DicomAttribute sequence = dataSets[i][DicomTags.VoiLutSequence];
				DicomSequenceItem[] items = (DicomSequenceItem[])sequence.Values;

				if (sequenceId == SqId.X)
				{
					Assert.IsTrue(sequence.IsEmpty, "Tag should be empty.");
					Assert.IsFalse(dataSet.HasExcludedTags(true), "There should be no excluded tags.");
				}
				else
				{
					Assert.IsFalse(sequence.IsEmpty, "Tag should NOT be empty.");
					if (!setMaxTagLength)
					{
						Assert.AreEqual(sequence, images[i].DataSet[DicomTags.VoiLutSequence], "Tag doesn't match original.");
						Assert.IsFalse(dataSet.HasExcludedTags(true), "There should be no excluded tags.");
					}
					else
					{
						foreach (InstanceXmlDicomSequenceItem item in items)
							Assert.IsTrue(item.IsTagExcluded(DicomTags.LutData), "Tag should be excluded.");
					}

					if (sequenceId == SqId.B)
						Assert.AreEqual(items.Length, 2, "Sequence length should be 2.");
					else
						Assert.AreEqual(items.Length, 3, "Sequence length should be 3.");
				}
			}
		}
Esempio n. 14
0
		private void ValidateSimpleDataSets(string[] values, List<InstanceXmlDicomAttributeCollection> dataSets, StudyXmlOutputSettings settings)
		{
			if (values.Length != dataSets.Count)
				throw new ArgumentException("Test value and data set lists must have same number of values.");

			for (int i = 0; i < dataSets.Count; ++i)
			{
				string originalValue = values[i];
				InstanceXmlDicomAttributeCollection dataSet = dataSets[i];

				if (originalValue == null)
				{
					Assert.IsFalse(dataSet.IsTagExcluded(DicomTags.ImageComments), "Tag should NOT be excluded.");
					Assert.IsTrue(dataSet[DicomTags.ImageComments].IsEmpty, "Tag should be empty.");
				}
				else if (originalValue.Length > (int)settings.MaxTagLength)
				{
					Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.ImageComments), "Tag should be excluded.");
					Assert.AreNotEqual(dataSet[DicomTags.ImageComments].ToString(), originalValue, "Value doesn't match original.");
				}
				else
				{
					Assert.IsFalse(dataSet.IsTagExcluded(DicomTags.ImageComments), "Tag should not be excluded.");
					Assert.AreEqual(dataSet[DicomTags.ImageComments].ToString(), originalValue, "Value doesn't match original.");
				}
			}
		}
Esempio n. 15
0
		private void TestOverlappingTag(string[] values, bool setMaxTagLength)
		{
			List<DicomFile> images = SetupImages(values.Length);
			SetTestAttribute(images, values);

			StudyXml newStudyXml;
			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			if (setMaxTagLength)
				settings.MaxTagLength = 1024;

			List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(images, out newStudyXml, settings);

			ValidateSimpleDataSets(values, dataSets, settings);
		}
Esempio n. 16
0
		public void TestMultipleSerializations()
		{
			List<DicomFile> images = SetupImages(4);

			XmlDocument doc = null;
			StudyXml xml;
			int i;
			for(i = 0; i < images.Count; ++i)
			{
				xml = new StudyXml();

				if (doc != null)
					xml.SetMemento(doc);

				xml.AddFile(images[i]);
				StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
				doc = xml.GetMemento(settings);
			}

			xml = new StudyXml();
			xml.SetMemento(doc);
			List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
			i = 0;
			foreach (DicomFile file in images)
				ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
		}
Esempio n. 17
0
		public void TestExcludeBinaryTags()
		{
			List<DicomFile> images = SetupImages(3);
			images[2].DataSet[DicomTags.SpectroscopyData].Values = new float[6];

			StudyXml xml = new StudyXml();
			foreach (DicomFile file in images)
			{
				file.DataSet[DicomTags.RedPaletteColorLookupTableData].Values = new byte[256];
				file.DataSet[DicomTags.GreenPaletteColorLookupTableData].Values = new byte[256];
				file.DataSet[DicomTags.BluePaletteColorLookupTableData].Values = new byte[256];
				xml.AddFile(file);
			}

			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			settings.MaxTagLength = 100;
			XmlDocument doc = xml.GetMemento(settings);

			//SaveStudyXml(doc, @"C:\stewart\testxml.xml");
			List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
			foreach (InstanceXmlDicomAttributeCollection dataSet in dataSets)
			{
				Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.RedPaletteColorLookupTableData));
				Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.GreenPaletteColorLookupTableData));
				Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.BluePaletteColorLookupTableData));
			}

			//This attribute has a short value, so it should not be excluded.
			Assert.IsFalse(dataSets[2].IsTagExcluded(DicomTags.SpectroscopyData));

			xml = new StudyXml();
			xml.SetMemento(doc);

			dataSets = GetInstanceXmlDataSets(xml);
			foreach (InstanceXmlDicomAttributeCollection dataSet in dataSets)
			{
				Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.RedPaletteColorLookupTableData));
				Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.GreenPaletteColorLookupTableData));
				Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.BluePaletteColorLookupTableData));
			}

			//This attribute has a short value, so it should not be excluded.
			Assert.IsFalse(dataSets[2].IsTagExcluded(DicomTags.SpectroscopyData));
		}
Esempio n. 18
0
        private void TestOverlappingSequence(SqId[] sequenceIds, bool setMaxTagLength)
        {
            List <DicomFile> images = SetupImagesWithVoiLutSequenceA(sequenceIds.Length);

            for (int i = 0; i < sequenceIds.Length; ++i)
            {
                DicomFile image      = images[i];
                SqId      sequenceId = sequenceIds[i];
                if (sequenceId == SqId.X)
                {
                    image.DataSet[DicomTags.VoiLutSequence] = null;
                }
                else if (sequenceId == SqId.B)
                {
                    MakeVoiLutSequenceB(image.DataSet);
                }
            }

            StudyXml newStudyXml;
            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();

            if (setMaxTagLength)
            {
                settings.MaxTagLength = 1024;
            }

            List <InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(images, out newStudyXml, settings);

            for (int i = 0; i < dataSets.Count; ++i)
            {
                InstanceXmlDicomAttributeCollection dataSet = dataSets[i];
                SqId sequenceId = sequenceIds[i];

                DicomAttribute      sequence = dataSets[i][DicomTags.VoiLutSequence];
                DicomSequenceItem[] items    = (DicomSequenceItem[])sequence.Values;

                if (sequenceId == SqId.X)
                {
                    Assert.IsTrue(sequence.IsEmpty, "Tag should be empty.");
                    Assert.IsFalse(dataSet.HasExcludedTags(true), "There should be no excluded tags.");
                }
                else
                {
                    Assert.IsFalse(sequence.IsEmpty, "Tag should NOT be empty.");
                    if (!setMaxTagLength)
                    {
                        Assert.AreEqual(sequence, images[i].DataSet[DicomTags.VoiLutSequence], "Tag doesn't match original.");
                        Assert.IsFalse(dataSet.HasExcludedTags(true), "There should be no excluded tags.");
                    }
                    else
                    {
                        foreach (InstanceXmlDicomSequenceItem item in items)
                        {
                            Assert.IsTrue(item.IsTagExcluded(DicomTags.LutData), "Tag should be excluded.");
                        }
                    }

                    if (sequenceId == SqId.B)
                    {
                        Assert.AreEqual(items.Length, 2, "Sequence length should be 2.");
                    }
                    else
                    {
                        Assert.AreEqual(items.Length, 3, "Sequence length should be 3.");
                    }
                }
            }
        }
Esempio n. 19
0
		private static List<InstanceXmlDicomAttributeCollection> GetInstanceXmlDataSets(IEnumerable<DicomFile> images, out StudyXml newStudyXml, StudyXmlOutputSettings settings)
		{
			StudyXml xml = new StudyXml();
			foreach (DicomFile image in images)
				xml.AddFile(image);

			XmlDocument doc = xml.GetMemento(settings);
			//SaveStudyXml(doc, @"c:\stewart\LastStudyXml.xml");

			newStudyXml = new StudyXml();
			newStudyXml.SetMemento(doc);

			doc = newStudyXml.GetMemento(settings);
			//SaveStudyXml(doc, @"c:\stewart\LastStudyXml2.xml");

			return GetInstanceXmlDataSets(newStudyXml);
		}
Esempio n. 20
0
		public void TestExclusionsImmediatelyAfterSerialization()
		{
			//NOTE: previously, this test failed because the excluded tags were not added to the
			//xml collection until after it had been deserialized at least once from the xml.
			foreach (string[] testSet in _overlappingTagTestSets)
			{
				List<DicomFile> images = SetupImages(testSet.Length);
				SetTestAttribute(images, testSet);

				StudyXml xml = new StudyXml();

				foreach (DicomFile file in images)
					xml.AddFile(file);

				StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
				settings.MaxTagLength = 1024;
				XmlDocument doc = xml.GetMemento(settings);

				List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
				ValidateSimpleDataSets(testSet, dataSets, settings);

				//do a little extra validation, what the hay.
				xml = new StudyXml();
				xml.SetMemento(doc);

				dataSets = GetInstanceXmlDataSets(xml);
				ValidateSimpleDataSets(testSet, dataSets, settings);
			}
		}
Esempio n. 21
0
		public void TestEqualAfterSerialization()
		{
			foreach (string[] testSet in _overlappingTagTestSets)
			{
				List<DicomFile> images = SetupImages(testSet.Length);
				SetTestAttribute(images, testSet);

				StudyXml xml = new StudyXml();

				foreach (DicomFile file in images)
					xml.AddFile(file);

				StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
				settings.MaxTagLength = 1024;
				XmlDocument doc = xml.GetMemento(settings);

				List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
				int i = 0;
				foreach (DicomFile file in images)
					ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);

				xml = new StudyXml();
				xml.SetMemento(doc);

				dataSets = GetInstanceXmlDataSets(xml);
				i = 0;
				foreach (DicomFile file in images)
					ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
			}
		}
Esempio n. 22
0
        /// <summary>
        /// 刻录
        /// </summary>
        private void Writing()
        {
            List <IBurnMediaData> data         = new List <IBurnMediaData>();
            MediaFileSet          mediaFileSet = new MediaFileSet();

            mediaFileSet.Id = "Gold";
            List <MediaFileSetStudy> fileSetStudies = new List <MediaFileSetStudy>();
            string RootPath       = "CD";
            string studysPath     = "IMAGES";
            string studyitemPath  = "Study{0}";
            string seriesitemPath = "Series{0}";
            string filename       = "IM{0}";
            string xmlSavePath    = "studies";
            string XmlName        = "Study{0}.xml";
            int    study          = -1;
            int    series         = -1;
            int    Sopcount       = -1;

            string studyIndexName = "index.xml";

            string xmlPath = Path.Combine(this.StagingFolderPath, xmlSavePath);

            if (Directory.Exists(xmlPath))
            {
                Directory.Delete(xmlPath, true);
            }
            Directory.CreateDirectory(xmlPath);

            if (MediaWriterSettings.Default.PortablePath == null || !Directory.Exists(MediaWriterSettings.Default.PortablePath))
            {
                this.Host.ShowMessageBox("指定光盘浏览器目录不存在", MessageBoxActions.Ok);
                return;
            }
            FileDirectoryUtility.CopyFiles(MediaWriterSettings.Default.PortablePath, this.StagingFolderPath, true, true);
            RootPath = Path.Combine(this.StagingFolderPath, studysPath);
            DicomDirectory dicomDirectory = new DicomDirectory(VolumeName);

            dicomDirectory.SourceApplicationEntityTitle = VolumeName;
            dicomDirectory.FileSetId = "File Set Desc";
            try
            {
                foreach (IStudyTreeItem item in Tree.Items)
                {
                    StudyTreeItem treeitem = (StudyTreeItem)item;
                    if (!treeitem.Ischecked)
                    {
                        continue;
                    }
                    study++;
                    MediaFileSetStudy fileSet       = new MediaFileSetStudy();
                    string            tempstudypath = Path.Combine(RootPath, string.Format(studyitemPath, study));
                    StudyXml          studyXml      = new StudyXml();
                    foreach (ISeriesTreeItem seriss in treeitem.Tree.Items)
                    {
                        SeriesTreeItem seriesitem = (SeriesTreeItem)seriss;
                        if (!seriesitem.Ischecked)
                        {
                            continue;
                        }
                        series++;
                        string tempseriespath = Path.Combine(tempstudypath, string.Format(seriesitemPath, series));
                        if (Directory.Exists(tempseriespath))
                        {
                            Directory.Delete(tempseriespath, true);
                        }
                        Directory.CreateDirectory(tempseriespath);

                        foreach (var sop in seriesitem.Series.GetSopInstances())
                        {
                            if (LoadImageWorker.CancellationPending == true)
                            {
                                break;
                            }

                            if (File.Exists(sop.FilePath))
                            {
                                Sopcount++;
                                string temp = Path.Combine(tempseriespath, string.Format(filename, Sopcount));
                                File.Copy(sop.FilePath, temp);
                                int    start       = temp.IndexOf(studysPath);
                                string dirFilePath = temp.Substring(start);
                                dicomDirectory.AddFile(temp, dirFilePath);
                                string    fileName = string.Format("..\\{0}", dirFilePath);
                                DicomFile file     = new DicomFile(temp);
                                file.Load();
                                file.Filename = fileName;
                                studyXml.AddFile(file);
                                fileSet.UID = sop.StudyInstanceUid;
                            }
                        }
                    }
                    //保存studyXml
                    string xmlFileName = string.Format(XmlName, study);
                    string path        = Path.Combine(xmlPath, xmlFileName);
                    StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
                    settings.IncludeSourceFileName = true;
                    XmlDocument document = studyXml.GetMemento(settings);

                    // Create an XML declaration.
                    XmlDeclaration xmldecl;
                    xmldecl          = document.CreateXmlDeclaration("1.0", null, null);
                    xmldecl.Encoding = "UTF-8";
                    document.Save(path);

                    fileSet.Value = Path.Combine(xmlSavePath, xmlFileName);;
                    fileSetStudies.Add(fileSet);
                }

                if (!Directory.Exists(RootPath))
                {
                    throw new Exception(string.Format("复制检查图像到{0}目录出错", StagingFolderPath));
                }

                IBurnMediaData info = new BurnMediaData();
                info.Type = MediaType.Dir;
                info.Path = RootPath;
                data.Add(info);

                IBurnMediaData xml = new BurnMediaData();
                xml.Path = xmlPath;
                xml.Type = MediaType.Dir;
                data.Add(xml);

                string studyindexfilename = Path.Combine(this.StagingFolderPath, studyIndexName);
                if (File.Exists(studyindexfilename))
                {
                    File.Delete(studyindexfilename);
                }
                Stream stream = File.Open(studyindexfilename, FileMode.Create);
                mediaFileSet.StudyIndex = fileSetStudies.ToArray();
                XmlSerializer serializer = new XmlSerializer(typeof(MediaFileSet));
                serializer.Serialize(stream, mediaFileSet);
                IBurnMediaData studyindex = new BurnMediaData();
                studyindex.Path = studyindexfilename;
                studyindex.Type = MediaType.File;
                data.Add(studyindex);

                string dirfilename = Path.Combine(this.StagingFolderPath, "DICOMDIR");
                dicomDirectory.Save(dirfilename);
                info      = new BurnMediaData();
                info.Type = MediaType.File;
                info.Path = dirfilename;
                data.Add(info);
                DoBurn(data);
            }
            catch (COMException ex)
            {
                this.Host.ShowMessageBox(ImapiReturnValues.GetName(ex.ErrorCode), MessageBoxActions.Ok);
            }
            catch (Exception e)
            {
                this.Host.ShowMessageBox(e.Message, MessageBoxActions.Ok);
            }
            finally
            {
                this.IsWriting = false;
            }
        }
Esempio n. 23
0
        public void TestBaseInstanceTagsPastEnd()
        {
            //NOTE: previously, this test failed because, during xml serialization, only the
            //instance's own attributes were iterated over; if there were tags in the base instance
            //that went past the end of the individual instances, no 'EmptyAttributes' got added
            //to the xml and there would be extra attributes in the instances on deserialization.
            List <DicomFile> images    = SetupImages(2);
            DicomFile        smallFile = new DicomFile(null);

            images.Add(smallFile);
            base.SetupMetaInfo(smallFile);

            DicomAttributeCollection theSet = smallFile.DataSet;

            theSet[DicomTags.SpecificCharacterSet].SetStringValue("ISO_IR 100");
            theSet[DicomTags.ImageType].SetStringValue("ORIGINAL\\PRIMARY\\OTHER\\M\\FFE");
            theSet[DicomTags.InstanceCreationDate].SetStringValue("20070618");
            theSet[DicomTags.InstanceCreationTime].SetStringValue("133600");
            theSet[DicomTags.SopClassUid].SetStringValue(SopClass.MrImageStorageUid);
            theSet[DicomTags.SopInstanceUid].SetStringValue(DicomUid.GenerateUid().UID);
            theSet[DicomTags.StudyDate].SetStringValue("20070618");
            theSet[DicomTags.StudyTime].SetStringValue("133600");
            theSet[DicomTags.SeriesDate].SetStringValue("20070618");
            theSet[DicomTags.SeriesTime].SetStringValue("133700");
            theSet[DicomTags.AccessionNumber].SetStringValue("A1234");
            theSet[DicomTags.Modality].SetStringValue("MR");
            theSet[DicomTags.Manufacturer].SetStringValue("ClearCanvas");
            theSet[DicomTags.ManufacturersModelName].SetNullValue();
            theSet[DicomTags.InstitutionName].SetStringValue("Mount Sinai Hospital");
            theSet[DicomTags.ReferringPhysiciansName].SetStringValue("Last^First");
            theSet[DicomTags.StudyDescription].SetStringValue("HEART");
            theSet[DicomTags.SeriesDescription].SetStringValue("Heart 2D EPI BH TRA");

            theSet[DicomTags.StudyInstanceUid].SetStringValue(images[0].DataSet[DicomTags.StudyInstanceUid].ToString());
            theSet[DicomTags.SeriesInstanceUid].SetStringValue(images[0].DataSet[DicomTags.SeriesInstanceUid].ToString());
            theSet[DicomTags.SopInstanceUid].SetStringValue(DicomUid.GenerateUid().ToString());

            StudyXml xml = new StudyXml();

            foreach (DicomFile file in images)
            {
                xml.AddFile(file);
            }

            StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
            XmlDocument            doc      = xml.GetMemento(settings);

            List <InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
            int i = 0;

            foreach (DicomFile file in images)
            {
                ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
            }

            xml = new StudyXml();
            xml.SetMemento(doc);

            dataSets = GetInstanceXmlDataSets(xml);
            i        = 0;
            foreach (DicomFile file in images)
            {
                ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
            }
        }
Esempio n. 24
0
        private void ValidateSimpleDataSets(string[] values, List <InstanceXmlDicomAttributeCollection> dataSets, StudyXmlOutputSettings settings)
        {
            if (values.Length != dataSets.Count)
            {
                throw new ArgumentException("Test value and data set lists must have same number of values.");
            }

            for (int i = 0; i < dataSets.Count; ++i)
            {
                string originalValue = values[i];
                InstanceXmlDicomAttributeCollection dataSet = dataSets[i];

                if (originalValue == null)
                {
                    Assert.IsFalse(dataSet.IsTagExcluded(DicomTags.ImageComments), "Tag should NOT be excluded.");
                    Assert.IsTrue(dataSet[DicomTags.ImageComments].IsEmpty, "Tag should be empty.");
                }
                else if (originalValue.Length > (int)settings.MaxTagLength)
                {
                    Assert.IsTrue(dataSet.IsTagExcluded(DicomTags.ImageComments), "Tag should be excluded.");
                    Assert.AreNotEqual(dataSet[DicomTags.ImageComments].ToString(), originalValue, "Value doesn't match original.");
                }
                else
                {
                    Assert.IsFalse(dataSet.IsTagExcluded(DicomTags.ImageComments), "Tag should not be excluded.");
                    Assert.AreEqual(dataSet[DicomTags.ImageComments].ToString(), originalValue, "Value doesn't match original.");
                }
            }
        }
Esempio n. 25
0
        private void UpdateFilesystem()
        {
            Platform.Log(LogLevel.Info, "Updating filesystem...");
            StudyXml studyXml = _oldStudyLocation.LoadStudyXml();
            StudyXmlOutputSettings outputSettings = ImageServerCommonConfiguration.DefaultStudyXmlOutputSettings;

            StudyXml newStudyXml = new StudyXml();

            foreach (SeriesXml seriesXml in studyXml)
            {
                foreach (InstanceXml instanceXml in seriesXml)
                {
                    string path = Path.Combine(_oldStudyPath, seriesXml.SeriesInstanceUid);
                    path  = Path.Combine(path, instanceXml.SopInstanceUid);
                    path += ServerPlatform.DicomFileExtension;

                    if (!File.Exists(path))
                    {
                        Platform.Log(LogLevel.Info, "SOP {0} is referenced in study xml but does not exist. It will be removed");
                        continue; // file was removed but xml was not updated?
                    }

                    try
                    {
                        DicomFile file = new DicomFile(path);
                        file.Load();

                        InstanceInfo instance = new InstanceInfo
                        {
                            SeriesInstanceUid = file.DataSet[DicomTags.SeriesInstanceUid].GetString(0, String.Empty),
                            SopInstanceUid    = file.DataSet[DicomTags.SopInstanceUid].GetString(0, String.Empty)
                        };

                        UpdateDicomFile(file);

                        // Add into the temporary study xml
                        long fileSize = 0;
                        if (File.Exists(file.Filename))
                        {
                            FileInfo finfo = new FileInfo(file.Filename);
                            fileSize = finfo.Length;
                        }
                        newStudyXml.AddFile(file, fileSize, outputSettings);


                        _updatedSopList.Add(instance);
                        Platform.Log(ServerPlatform.InstanceLogLevel, "SOP {0} has been updated [{1} of {2}].", instance.SopInstanceUid, _updatedSopList.Count, _totalSopCount);
                    }
                    catch (Exception)
                    {
                        File.Delete(Path.Combine(_backupDir, instanceXml.SopInstanceUid) + ".bak"); //dont' need to restore this file
                        throw;
                    }
                }
            }

            // Log any study-level warnings
            if (_updatedSopList.Count != _totalSopCount)
            {
                Platform.Log(LogLevel.Warn, "Inconsistent data: expected {0} instances to be updated / Found {1}.", _totalSopCount, _updatedSopList.Count);
            }

            // update the header
            Platform.Log(LogLevel.Info, "Generating new study header...");
            string newStudyXmlPath  = Path.Combine(NewStudyPath, _newStudyInstanceUid + ".xml");
            string gzipStudyXmlPath = Path.Combine(NewStudyPath, _newStudyInstanceUid + ".xml.gz");

            using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(newStudyXmlPath, FileMode.Create),
                   gzipStream = FileStreamOpener.OpenForSoleUpdate(gzipStudyXmlPath, FileMode.Create))
            {
                StudyXmlIo.WriteXmlAndGzip(newStudyXml.GetMemento(outputSettings), xmlStream, gzipStream);
                xmlStream.Close();
                gzipStream.Close();
            }
        }
Esempio n. 26
0
		public void TestSameSequenceWithExclusionsMultipleSerializations()
		{
			List<DicomFile> images = SetupImagesWithVoiLutSequenceA(3);

			StudyXml newStudyXml;
			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			settings.MaxTagLength = 1024;

			List<DicomFile> first2Images = new List<DicomFile>();
			first2Images.AddRange(images.GetRange(0, 2));

			List<InstanceXmlDicomAttributeCollection> dataSets1 = GetInstanceXmlDataSets(first2Images, out newStudyXml, settings);
			for (int i = 0; i < first2Images.Count; ++i)
			{
				DicomAttributeSQ sequence = (DicomAttributeSQ)dataSets1[i][DicomTags.VoiLutSequence];
				for (int j = 0; j < sequence.Count; ++j)
					Assert.IsTrue(((InstanceXmlDicomSequenceItem)sequence[j]).ExcludedTags[0].TagValue == DicomTags.LutData);
			}

			Assert.AreEqual(dataSets1[0][DicomTags.VoiLutSequence], dataSets1[1][DicomTags.VoiLutSequence]);

			newStudyXml.AddFile(images[2]);
			XmlDocument document = newStudyXml.GetMemento(settings);
			//SaveStudyXml(document, @"c:\stewart\studyxml3.xml");

			newStudyXml = new StudyXml();
			newStudyXml.SetMemento(document);

			List<InstanceXmlDicomAttributeCollection> dataSets2 = GetInstanceXmlDataSets(newStudyXml);
			for (int i = 0; i < first2Images.Count; ++i)
			{
				DicomAttributeSQ sequence = (DicomAttributeSQ)dataSets2[i][DicomTags.VoiLutSequence];
				for (int j = 0; j < sequence.Count; ++j)
					Assert.IsTrue(((InstanceXmlDicomSequenceItem)sequence[j]).ExcludedTags[0].TagValue == DicomTags.LutData);
			}

			Assert.AreEqual(dataSets1[0][DicomTags.VoiLutSequence], dataSets2[0][DicomTags.VoiLutSequence]);
			Assert.AreEqual(dataSets1[1][DicomTags.VoiLutSequence], dataSets2[1][DicomTags.VoiLutSequence]);

			Assert.AreEqual(dataSets2[0][DicomTags.VoiLutSequence], dataSets2[1][DicomTags.VoiLutSequence]);
			Assert.AreEqual(dataSets2[0][DicomTags.VoiLutSequence], dataSets2[2][DicomTags.VoiLutSequence]);
		}
Esempio n. 27
0
		public void TestBaseInstanceExclusionAfterSerialization()
		{
			foreach (string[] testSet in _overlappingTagTestSets)
			{
				List<DicomFile> images = SetupImages(testSet.Length);
				SetTestAttribute(images, testSet);

				StudyXml xml = new StudyXml();

				StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
				xml = new StudyXml();

				xml.AddFile(images[0]);
				xml.AddFile(images[1]);

				XmlDocument doc = xml.GetMemento(settings);

				settings.MaxTagLength = 1024;

				xml.AddFile(images[2]); //re-add
				doc = xml.GetMemento(settings);

				xml = new StudyXml();
				xml.SetMemento(doc);
				doc = xml.GetMemento(settings);
				xml.AddFile(images[2]); //re-add
				doc = xml.GetMemento(settings);

				xml = new StudyXml();
				xml.SetMemento(doc);
				xml.AddFile(images[1]); //re-add
				doc = xml.GetMemento(settings);
			}
		}
Esempio n. 28
0
		public void TestBaseInstanceTagsPastEnd()
		{
			//NOTE: previously, this test failed because, during xml serialization, only the
			//instance's own attributes were iterated over; if there were tags in the base instance
			//that went past the end of the individual instances, no 'EmptyAttributes' got added
			//to the xml and there would be extra attributes in the instances on deserialization.
			List<DicomFile> images = SetupImages(2);
			DicomFile smallFile = new DicomFile(null);
			images.Add(smallFile);
			base.SetupMetaInfo(smallFile);

			DicomAttributeCollection theSet = smallFile.DataSet;
			theSet[DicomTags.SpecificCharacterSet].SetStringValue("ISO_IR 100");
			theSet[DicomTags.ImageType].SetStringValue("ORIGINAL\\PRIMARY\\OTHER\\M\\FFE");
			theSet[DicomTags.InstanceCreationDate].SetStringValue("20070618");
			theSet[DicomTags.InstanceCreationTime].SetStringValue("133600");
			theSet[DicomTags.SopClassUid].SetStringValue(SopClass.MrImageStorageUid);
			theSet[DicomTags.SopInstanceUid].SetStringValue(DicomUid.GenerateUid().UID);
			theSet[DicomTags.StudyDate].SetStringValue("20070618");
			theSet[DicomTags.StudyTime].SetStringValue("133600");
			theSet[DicomTags.SeriesDate].SetStringValue("20070618");
			theSet[DicomTags.SeriesTime].SetStringValue("133700");
			theSet[DicomTags.AccessionNumber].SetStringValue("A1234");
			theSet[DicomTags.Modality].SetStringValue("MR");
			theSet[DicomTags.Manufacturer].SetStringValue("ClearCanvas");
			theSet[DicomTags.ManufacturersModelName].SetNullValue();
			theSet[DicomTags.InstitutionName].SetStringValue("Mount Sinai Hospital");
			theSet[DicomTags.ReferringPhysiciansName].SetStringValue("Last^First");
			theSet[DicomTags.StudyDescription].SetStringValue("HEART");
			theSet[DicomTags.SeriesDescription].SetStringValue("Heart 2D EPI BH TRA");

			theSet[DicomTags.StudyInstanceUid].SetStringValue(images[0].DataSet[DicomTags.StudyInstanceUid].ToString());
			theSet[DicomTags.SeriesInstanceUid].SetStringValue(images[0].DataSet[DicomTags.SeriesInstanceUid].ToString());
			theSet[DicomTags.SopInstanceUid].SetStringValue(DicomUid.GenerateUid().ToString());

			StudyXml xml = new StudyXml();

			foreach (DicomFile file in images)
				xml.AddFile(file);

			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			XmlDocument doc = xml.GetMemento(settings);

			List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
			int i = 0;
			foreach (DicomFile file in images)
				ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);

			xml = new StudyXml();
			xml.SetMemento(doc);

			dataSets = GetInstanceXmlDataSets(xml);
			i = 0;
			foreach (DicomFile file in images)
				ValidateEqualExceptExclusions(file.DataSet, dataSets[i++], DicomTags.ImageComments, DicomTags.PixelData);
		}
Esempio n. 29
0
		public void TestExcludePrivateTags()
		{
			List<DicomFile> images = SetupImages(2);
			StudyXml xml = new StudyXml();
			DicomTag privateTag =
				new DicomTag(0x00210010, "Private Tag", "Private Tag", DicomVr.LTvr, false, 1, uint.MaxValue, false);
			
			foreach (DicomFile file in images)
			{
				file.DataSet[privateTag].SetStringValue("My Private Tag");
				xml.AddFile(file);
			}

			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
			XmlDocument doc = xml.GetMemento(settings);

			List<InstanceXmlDicomAttributeCollection> dataSets = GetInstanceXmlDataSets(xml);
			foreach (InstanceXmlDicomAttributeCollection dataSet in dataSets)
				Assert.IsFalse(dataSet.Contains(privateTag));

			xml = new StudyXml();
			xml.SetMemento(doc);

			dataSets = GetInstanceXmlDataSets(xml);
			foreach (InstanceXmlDicomAttributeCollection dataSet in dataSets)
				Assert.IsFalse(dataSet.Contains(privateTag));
		}
Esempio n. 30
0
        private static List <InstanceXmlDicomAttributeCollection> GetInstanceXmlDataSets(IEnumerable <DicomFile> images, out StudyXml newStudyXml, StudyXmlOutputSettings settings)
        {
            StudyXml xml = new StudyXml();

            foreach (DicomFile image in images)
            {
                xml.AddFile(image);
            }

            XmlDocument doc = xml.GetMemento(settings);

            //SaveStudyXml(doc, @"c:\stewart\LastStudyXml.xml");

            newStudyXml = new StudyXml();
            newStudyXml.SetMemento(doc);

            doc = newStudyXml.GetMemento(settings);
            //SaveStudyXml(doc, @"c:\stewart\LastStudyXml2.xml");

            return(GetInstanceXmlDataSets(newStudyXml));
        }