예제 #1
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);
            }
        }
예제 #2
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);
            }
        }
예제 #3
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));
            }
        }
		/// <summary>
		/// Load the first instance from the first series of the StudyXml file for a study.
		/// </summary>
		/// <param name="location">The storage location of the study.</param>
		/// <returns></returns>
		protected static DicomFile LoadInstance(StudyStorageLocation location)
		{
			string studyXml = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

			if (!File.Exists(studyXml))
			{
				return null;
			}

			FileStream stream = FileStreamOpener.OpenForRead(studyXml, FileMode.Open);
			var theDoc = new XmlDocument();
			StudyXmlIo.Read(theDoc, stream);
			stream.Close();
			stream.Dispose();
			var xml = new StudyXml();
			xml.SetMemento(theDoc);
            
			IEnumerator<SeriesXml> seriesEnumerator = xml.GetEnumerator();
			if (seriesEnumerator.MoveNext())
			{
				SeriesXml seriesXml = seriesEnumerator.Current;
				IEnumerator<InstanceXml> instanceEnumerator = seriesXml.GetEnumerator();
				if (instanceEnumerator.MoveNext())
				{
					InstanceXml instance = instanceEnumerator.Current;
					var file = new DicomFile("file.dcm",new DicomAttributeCollection(), instance.Collection)
						{TransferSyntax = instance.TransferSyntax};
					return file;
				}
			}

			return null;
		}
예제 #5
0
        public void TestSopClass()
        {
            List <DicomFile> images = SetupImages(4);

            string seriesUid = images[0].DataSet[DicomTags.SeriesInstanceUid].ToString();

            images[0].MediaStorageSopClassUid = SopClass.EnhancedCtImageStorageUid;
            images[1].MediaStorageSopClassUid = SopClass.EnhancedMrImageStorageUid;
            images[2].MediaStorageSopClassUid = SopClass.EnhancedSrStorageUid;
            images[3].MediaStorageSopClassUid = SopClass.EnhancedXaImageStorageUid;

            StudyXml xml = new StudyXml();

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

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

            Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedCtImageStorageUid);
            Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedMrImageStorageUid);
            Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedSrStorageUid);
            Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedXaImageStorageUid);

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

            Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedCtImageStorageUid);
            Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedMrImageStorageUid);
            Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedSrStorageUid);
            Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedXaImageStorageUid);
        }
예제 #6
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);
            }
        }
예제 #7
0
        /// <summary>
        /// Gets the <see cref="StudyXml"/> if it exists in this storage location.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns></returns>
        public StudyXml LoadStudyXml()
        {
            // TODO: Use FileStreamOpener instead
            // Can't do it until we break the dependency of ImageServer.Common on Model
            if (_studyXml == null)
            {
                lock (SyncRoot)
                {
                    try
                    {
                        Stream xmlStream = Open(GetStudyXmlPath());
                        if (xmlStream != null)
                        {
                            var theMemento = new StudyXmlMemento();
                            using (xmlStream)
                            {
                                StudyXmlIo.Read(theMemento, xmlStream);
                                xmlStream.Close();
                            }

                            _studyXml = new StudyXml();
                            _studyXml.SetMemento(theMemento);
                        }
                    }
                    catch (Exception)
                    { }
                }
            }


            return(_studyXml);
        }
예제 #8
0
        public void TestTransferSyntax()
        {
            List <DicomFile> images = SetupImages(4);

            string seriesUid = images[0].DataSet[DicomTags.SeriesInstanceUid].ToString();

            images[0].TransferSyntax = TransferSyntax.Jpeg2000ImageCompression;
            images[1].TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
            images[2].TransferSyntax = TransferSyntax.ExplicitVrBigEndian;
            images[3].TransferSyntax = TransferSyntax.Jpeg2000ImageCompressionLosslessOnly;

            StudyXml xml = new StudyXml();

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

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

            Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompression);
            Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrLittleEndian);
            Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrBigEndian);
            Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);

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

            Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompression);
            Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrLittleEndian);
            Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrBigEndian);
            Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
        }
예제 #9
0
		public void TestSopClass()
		{
			List<DicomFile> images = SetupImages(4);

			string seriesUid = images[0].DataSet[DicomTags.SeriesInstanceUid].ToString();

			images[0].MediaStorageSopClassUid = SopClass.EnhancedCtImageStorageUid;
			images[1].MediaStorageSopClassUid = SopClass.EnhancedMrImageStorageUid;
			images[2].MediaStorageSopClassUid = SopClass.EnhancedSrStorageUid;
			images[3].MediaStorageSopClassUid = SopClass.EnhancedXaImageStorageUid;
		
			StudyXml xml = new StudyXml();
			foreach (DicomFile file in images)
				xml.AddFile(file);
	
			XmlDocument doc = xml.GetMemento(new StudyXmlOutputSettings());

			Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedCtImageStorageUid);
			Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedMrImageStorageUid);
			Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedSrStorageUid);
			Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedXaImageStorageUid);

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

			Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedCtImageStorageUid);
			Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedMrImageStorageUid);
			Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedSrStorageUid);
			Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].SopClass.Uid, SopClass.EnhancedXaImageStorageUid);
		}
예제 #10
0
        private void LoadStudyXml(bool throwIfNotExists)
        {
            if (_studyXml == null)
            {
                if (StudyXmlUri == null)
                {
                    throw new DataStoreException("The study xml location must be set.");
                }

                XmlDocument doc = new XmlDocument();
                _studyXml = new StudyXml(StudyInstanceUid);

                if (File.Exists(StudyXmlUri.LocalDiskPath))
                {
                    using (FileStream stream = GetFileStream(FileMode.Open, FileAccess.Read))
                    {
                        StudyXmlIo.Read(doc, stream);
                        _studyXml.SetMemento(doc);
                    }
                }
                else if (throwIfNotExists)
                {
                    throw new FileNotFoundException("The study xml file could not be found", StudyXmlUri.LocalDiskPath);
                }
            }
        }
예제 #11
0
		/// <summary>
		/// Apply the rules.
		/// </summary>
		/// <remarks>
		/// When rules are applied, we are simply adding new <see cref="ServerDatabaseCommand"/> instances
		/// for the rules to the currently executing <see cref="ServerCommandProcessor"/>.  They will be
		/// executed after all other rules have been executed.
		/// </remarks>
		protected override void OnExecute(CommandProcessor theProcessor)
		{
			string studyXmlFile = Path.Combine(_directory, String.Format("{0}.xml", _studyInstanceUid));
			StudyXml theXml = new StudyXml(_studyInstanceUid);

			if (File.Exists(studyXmlFile))
			{
				using (Stream fileStream = FileStreamOpener.OpenForRead(studyXmlFile, FileMode.Open))
				{
					var theMemento = new StudyXmlMemento();

					StudyXmlIo.Read(theMemento, fileStream);

					theXml.SetMemento(theMemento);

					fileStream.Close();
				}
			}
			else
			{
				string errorMsg = String.Format("Unable to load study XML file of restored study: {0}", studyXmlFile);

				Platform.Log(LogLevel.Error, errorMsg);
				throw new ApplicationException(errorMsg);
			}

			DicomFile defaultFile = null;
			bool rulesExecuted = false;
			foreach (SeriesXml seriesXml in theXml)
			{
				foreach (InstanceXml instanceXml in seriesXml)
				{
					// Skip non-image objects
					if (instanceXml.SopClass.Equals(SopClass.KeyObjectSelectionDocumentStorage)
					    || instanceXml.SopClass.Equals(SopClass.GrayscaleSoftcopyPresentationStateStorageSopClass)
					    || instanceXml.SopClass.Equals(SopClass.BlendingSoftcopyPresentationStateStorageSopClass)
					    || instanceXml.SopClass.Equals(SopClass.ColorSoftcopyPresentationStateStorageSopClass))
					{
						// Save the first one encountered, just in case the whole study is non-image objects.
						if (defaultFile == null)
							defaultFile = new DicomFile("test", new DicomAttributeCollection(), instanceXml.Collection);
						continue;
					}

					DicomFile file = new DicomFile("test", new DicomAttributeCollection(), instanceXml.Collection);
					_context.Message = file;
					_engine.Execute(_context);
					rulesExecuted = true;
					break;
				}
				if (rulesExecuted) break;
			}

			if (!rulesExecuted && defaultFile != null)
			{
				_context.Message = defaultFile;
				_engine.Execute(_context);
			}
		}
예제 #12
0
        /// <summary>
        /// Get a list of paths to the first image in each series within the study being processed.
        /// </summary>
        /// <returns></returns>
        private List <string> GetFirstInstanceInEachStudySeries()
        {
            var fileList = new List <string>();

            if (_studyXml == null)
            {
                string studyXml = _location.GetStudyXmlPath();

                if (!File.Exists(studyXml))
                {
                    return(fileList);
                }

                _studyXml = new StudyXml();

                using (FileStream stream = FileStreamOpener.OpenForRead(studyXml, FileMode.Open))
                {
                    var theDoc = new XmlDocument();
                    StudyXmlIo.Read(theDoc, stream);
                    stream.Close();
                    _studyXml.SetMemento(theDoc);
                }
            }

            // Note, we try and force ourselves to have an uncompressed
            // image, if one exists.  That way the rules will be reapplied on the object
            // if necessary for compression.
            foreach (SeriesXml seriesXml in _studyXml)
            {
                InstanceXml saveInstance = null;

                foreach (InstanceXml instance in seriesXml)
                {
                    if (instance.TransferSyntax.Encapsulated)
                    {
                        if (saveInstance == null)
                        {
                            saveInstance = instance;
                        }
                    }
                    else
                    {
                        saveInstance = instance;
                        break;
                    }
                }

                if (saveInstance != null)
                {
                    string path = Path.Combine(_location.GetStudyPath(), seriesXml.SeriesInstanceUid);
                    path = Path.Combine(path, saveInstance.SopInstanceUid + ServerPlatform.DicomFileExtension);
                    fileList.Add(path);
                }
            }

            return(fileList);
        }
예제 #13
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]);
        }
예제 #14
0
        /// <summary>
        /// Load the StudyXml file.
        /// </summary>
        /// <param name="studyXmlFile"></param>
        public void LoadStudyXml(string studyXmlFile)
        {
            using (Stream fileStream = FileStreamOpener.OpenForRead(studyXmlFile, FileMode.Open))
            {
                var theMemento = new StudyXmlMemento();

                StudyXmlIo.Read(theMemento, fileStream);

                _studyXml = new StudyXml(_storageLocation.StudyInstanceUid);
                _studyXml.SetMemento(theMemento);

                fileStream.Close();
            }
        }
        private static StudyXml GetStudyXml(StudyStorageLocation storageLocation)
        {
            StudyXml studyXml     = new StudyXml();
            string   studyXmlPath = Path.Combine(storageLocation.GetStudyPath(), storageLocation.StudyInstanceUid + ".xml");

            using (Stream stream = FileStreamOpener.OpenForRead(studyXmlPath, FileMode.Open))
            {
                var theMemento = new StudyXmlMemento();
                StudyXmlIo.Read(theMemento, stream);
                studyXml.SetMemento(theMemento);
                stream.Close();
            }
            return(studyXml);
        }
예제 #16
0
		/// <summary>
		/// Load the StudyXml file.
		/// </summary>
		/// <param name="studyXmlFile"></param>
		public void LoadStudyXml(string studyXmlFile)
		{
			using (Stream fileStream = FileStreamOpener.OpenForRead(studyXmlFile, FileMode.Open))
			{
				XmlDocument theDoc = new XmlDocument();

				StudyXmlIo.Read(theDoc, fileStream);

				_studyXml = new StudyXml(_storageLocation.StudyInstanceUid);
				_studyXml.SetMemento(theDoc);

				fileStream.Close();
			}
		}
예제 #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));
        }
예제 #18
0
        private void _buttonLoadGzipXml_Click(object sender, EventArgs e)
        {
            openFileDialog.DefaultExt = "gzip";
            openFileDialog.ShowDialog();

            Stream fileStream = openFileDialog.OpenFile();

            XmlDocument theDoc = new XmlDocument();

            StudyXmlIo.ReadGzip(theDoc, fileStream);

            fileStream.Close();

            _theStream = new StudyXml();

            _theStream.SetMemento(theDoc);
        }
예제 #19
0
파일: BaseScp.cs 프로젝트: hksonngan/Xian
        /// <summary>
        /// Helper method to load a <see cref="StudyXml"/> instance for a given study location.
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public static StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            StudyXml theXml = new StudyXml();

            if (File.Exists(streamFile))
            {
                // allocate the random number generator here, in case we need it below
                Random rand = new Random();

                // Go into a retry loop, to handle if the study is being processed right now
                for (int i = 0; ; i++)
                {
                    try
                    {
                        using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                        {
                            XmlDocument theDoc = new XmlDocument();

                            StudyXmlIo.Read(theDoc, fileStream);

                            theXml.SetMemento(theDoc);

                            fileStream.Close();

                            return(theXml);
                        }
                    }
                    catch (IOException)
                    {
                        if (i < 5)
                        {
                            Thread.Sleep(rand.Next(5, 50)); // Sleep 5-50 milliseconds
                            continue;
                        }

                        throw;
                    }
                }
            }

            return(theXml);
        }
        /// <summary>
        /// Gets the study header (via <see cref="GetStudyHeader"/>), unzips the gzipped stream, and returns it as <see cref="StudyXml"/>.
        /// </summary>
        /// <param name="callingAETitle">AE title of the local application.</param>
        /// <param name="parameters">Input parameters.</param>
        /// <returns></returns>
        public StudyXml GetStudyXml(string callingAETitle, HeaderStreamingParameters parameters)
        {
            XmlDocument doc;

            using (var stream = GetStudyHeader(callingAETitle, parameters))
            {
                using (var gzStream = new GZipStream(stream, CompressionMode.Decompress))
                {
                    doc = new XmlDocument();
                    doc.Load(gzStream);
                    gzStream.Close();
                }
            }

            var studyXml = new StudyXml();

            studyXml.SetMemento(doc);
            return(studyXml);
        }
        /// <summary>
        /// Gets the study header (via <see cref="GetStudyHeader"/>), unzips the gzipped stream, and returns it as <see cref="StudyXml"/>.
        /// </summary>
        /// <param name="callingAETitle">AE title of the local application.</param>
        /// <param name="parameters">Input parameters.</param>
        /// <returns></returns>
        public StudyXml GetStudyXml(string callingAETitle, HeaderStreamingParameters parameters)
        {
            StudyXmlMemento theMemento;

            using (var stream = GetStudyHeader(callingAETitle, parameters))
            {
                using (var gzStream = new GZipStream(stream, CompressionMode.Decompress))
                {
                    theMemento = new StudyXmlMemento();
                    StudyXmlIo.Read(theMemento, gzStream);
                    gzStream.Close();
                }
            }

            var studyXml = new StudyXml();

            studyXml.SetMemento(theMemento);
            return(studyXml);
        }
예제 #22
0
		/// <summary>
		/// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
		/// </summary>
		/// <param name="location">The location a study is stored.</param>
		/// <returns>The <see cref="StudyXml"/> instance for <paramref name="location"/></returns>
		private StudyXml LoadStudyStream(string location)
		{
			StudyXml theXml = new StudyXml();

			if (File.Exists(location))
			{
				using (Stream fileStream = new FileStream(location, FileMode.Open))
				{
					XmlDocument theDoc = new XmlDocument();

					StudyXmlIo.Read(theDoc, fileStream);

					theXml.SetMemento(theDoc);

					fileStream.Close();
				}
			}

			return theXml;
		}
        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());
        }
예제 #24
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));
        }
예제 #25
0
		/// <summary>
		/// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
		/// </summary>
		/// <returns>The <see cref="StudyXml"/> instance for the study</returns>
		private StudyXml LoadStudyXml()
		{
			StudyXml theXml = new StudyXml();

			String streamFile = Path.Combine(_rootPath, _studyInstanceUid + ".xml");
			if (File.Exists(streamFile))
			{
				using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
				{
					XmlDocument theDoc = new XmlDocument();

					StudyXmlIo.Read(theDoc, fileStream);

					theXml.SetMemento(theDoc);

					fileStream.Close();
				}
			}

			return theXml;
		}
예제 #26
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
        /// </summary>
        /// <param name="location">The location a study is stored.</param>
        /// <returns>The <see cref="StudyXml"/> instance for <paramref name="location"/></returns>
        private StudyXml LoadStudyStream(string location)
        {
            StudyXml theXml = new StudyXml();

            if (File.Exists(location))
            {
                using (Stream fileStream = new FileStream(location, FileMode.Open))
                {
                    XmlDocument theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }


            return(theXml);
        }
예제 #27
0
        public StudyXml LoadStudyXml(bool refresh)
        {
            lock (SyncRoot)
            {
                Stream xmlStream = Open(GetStudyXmlPath());
                if (xmlStream != null)
                {
                    var theMemento = new StudyXmlMemento();
                    using (xmlStream)
                    {
                        StudyXmlIo.Read(theMemento, xmlStream);
                        xmlStream.Close();
                    }

                    _studyXml = new StudyXml();
                    _studyXml.SetMemento(theMemento);
                }
            }

            return(_studyXml);
        }
예제 #28
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for the <see cref="StudyLocation"/>
        /// </summary>
        /// <returns>The <see cref="StudyXml"/> instance</returns>
        public StudyXml LoadStudyXml()
        {
            var theXml = new StudyXml();

            string streamFile = GetStudyXmlPath();

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }
            return(theXml);
        }
예제 #29
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
        /// </summary>
        /// <param name="location">The location a study is stored.</param>
        /// <returns>The <see cref="StudyXml"/> instance for <paramref name="location"/></returns>
        protected virtual StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            StudyXml theXml = new StudyXml();

            String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    XmlDocument theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }

            return(theXml);
        }
예제 #30
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
        /// </summary>
        /// <returns>The <see cref="StudyXml"/> instance for the study</returns>
        private StudyXml LoadStudyXml()
        {
            var theXml = new StudyXml();

            String streamFile = Path.Combine(_rootPath, _studyInstanceUid + ".xml");

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theMemento = new StudyXmlMemento();

                    StudyXmlIo.Read(theMemento, fileStream);

                    theXml.SetMemento(theMemento);

                    fileStream.Close();
                }
            }

            return(theXml);
        }
예제 #31
0
        /// <summary>
        /// Load the first instance from the first series of the StudyXml file for a study.
        /// </summary>
        /// <param name="location">The storage location of the study.</param>
        /// <returns></returns>
        protected static DicomFile LoadInstance(StudyStorageLocation location)
        {
            string studyXml = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            if (!File.Exists(studyXml))
            {
                return(null);
            }

            FileStream stream     = FileStreamOpener.OpenForRead(studyXml, FileMode.Open);
            var        theMemento = new StudyXmlMemento();

            StudyXmlIo.Read(theMemento, stream);
            stream.Close();
            stream.Dispose();
            var xml = new StudyXml();

            xml.SetMemento(theMemento);

            IEnumerator <SeriesXml> seriesEnumerator = xml.GetEnumerator();

            if (seriesEnumerator.MoveNext())
            {
                SeriesXml seriesXml = seriesEnumerator.Current;
                IEnumerator <InstanceXml> instanceEnumerator = seriesXml.GetEnumerator();
                if (instanceEnumerator.MoveNext())
                {
                    InstanceXml instance = instanceEnumerator.Current;
                    var         file     = new DicomFile("file.dcm", new DicomAttributeCollection(), instance.Collection)
                    {
                        TransferSyntax = instance.TransferSyntax
                    };
                    return(file);
                }
            }

            return(null);
        }
예제 #32
0
        protected static StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            // This method should be combined with StudyStorageLocation.LoadStudyXml()
            StudyXml theXml = new StudyXml(location.StudyInstanceUid);

            String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theMemento = new StudyXmlMemento();

                    StudyXmlIo.Read(theMemento, fileStream);

                    theXml.SetMemento(theMemento);

                    fileStream.Close();
                }
            }

            return(theXml);
        }
예제 #33
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);
                }
            }
        }
예제 #34
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);
		}
예제 #35
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));
		}
예제 #36
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));
		}
예제 #37
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);
		}
예제 #38
0
		public void TestTransferSyntax()
		{
			List<DicomFile> images = SetupImages(4);

			string seriesUid = images[0].DataSet[DicomTags.SeriesInstanceUid].ToString();

			images[0].TransferSyntax = TransferSyntax.Jpeg2000ImageCompression;
			images[1].TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
			images[2].TransferSyntax = TransferSyntax.ExplicitVrBigEndian;
			images[3].TransferSyntax = TransferSyntax.Jpeg2000ImageCompressionLosslessOnly;

			StudyXml xml = new StudyXml();
			foreach (DicomFile file in images)
				xml.AddFile(file);

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

			Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompression);
			Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrLittleEndian);
			Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrBigEndian);
			Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);

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

			Assert.AreEqual(xml[seriesUid][images[0].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompression);
			Assert.AreEqual(xml[seriesUid][images[1].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrLittleEndian);
			Assert.AreEqual(xml[seriesUid][images[2].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.ExplicitVrBigEndian);
			Assert.AreEqual(xml[seriesUid][images[3].DataSet[DicomTags.SopInstanceUid]].TransferSyntax, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
		}
예제 #39
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);
			}
		}
예제 #40
0
        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();
        }
예제 #41
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);
            }
        }
		/// <summary>
		/// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
		/// </summary>
		/// <param name="location">The location a study is stored.</param>
		/// <returns>The <see cref="StudyXml"/> instance for <paramref name="location"/></returns>
		protected virtual StudyXml LoadStudyXml(StudyStorageLocation location)
		{
			StudyXml theXml = new StudyXml();

			String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");
			if (File.Exists(streamFile))
			{
				using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
				{
					var theMemento = new StudyXmlMemento();

					StudyXmlIo.Read(theMemento, fileStream);

					theXml.SetMemento(theMemento);

					fileStream.Close();
				}
			}

			return theXml;
		}
예제 #43
0
        /// <summary>
        /// Apply the rules.
        /// </summary>
        /// <remarks>
        /// When rules are applied, we are simply adding new <see cref="ServerDatabaseCommand"/> instances
        /// for the rules to the currently executing <see cref="ServerCommandProcessor"/>.  They will be
        /// executed after all other rules have been executed.
        /// </remarks>
        protected override void OnExecute(CommandProcessor theProcessor)
        {
            string   studyXmlFile = Path.Combine(_directory, String.Format("{0}.xml", _studyInstanceUid));
            StudyXml theXml       = new StudyXml(_studyInstanceUid);

            if (File.Exists(studyXmlFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(studyXmlFile, FileMode.Open))
                {
                    XmlDocument theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }
            else
            {
                string errorMsg = String.Format("Unable to load study XML file of restored study: {0}", studyXmlFile);

                Platform.Log(LogLevel.Error, errorMsg);
                throw new ApplicationException(errorMsg);
            }

            DicomFile defaultFile   = null;
            bool      rulesExecuted = false;

            foreach (SeriesXml seriesXml in theXml)
            {
                foreach (InstanceXml instanceXml in seriesXml)
                {
                    // Skip non-image objects
                    if (instanceXml.SopClass.Equals(SopClass.KeyObjectSelectionDocumentStorage) ||
                        instanceXml.SopClass.Equals(SopClass.GrayscaleSoftcopyPresentationStateStorageSopClass) ||
                        instanceXml.SopClass.Equals(SopClass.BlendingSoftcopyPresentationStateStorageSopClass) ||
                        instanceXml.SopClass.Equals(SopClass.ColorSoftcopyPresentationStateStorageSopClass))
                    {
                        // Save the first one encountered, just in case the whole study is non-image objects.
                        if (defaultFile == null)
                        {
                            defaultFile = new DicomFile("test", new DicomAttributeCollection(), instanceXml.Collection);
                        }
                        continue;
                    }

                    DicomFile file = new DicomFile("test", new DicomAttributeCollection(), instanceXml.Collection);
                    _context.Message = file;
                    _engine.Execute(_context);
                    rulesExecuted = true;
                    break;
                }
                if (rulesExecuted)
                {
                    break;
                }
            }

            if (!rulesExecuted && defaultFile != null)
            {
                _context.Message = defaultFile;
                _engine.Execute(_context);
            }
        }
        private void PopulateSeries(string studyInstanceUid)
        {
            LogTextPanel.Text = "";
            StatisticsLog.Text = "";
            HeaderStreamingServiceClient proxy = null;
           
            try
            {
                proxy = new HeaderStreamingServiceClient();
                HeaderStreamingParameters parms = new HeaderStreamingParameters();
                parms.StudyInstanceUID = studyInstanceUid;
                parms.ServerAETitle = ServerAE.Text;
                parms.ReferenceID = Guid.NewGuid().ToString();

                TimeSpanStatistics servicecall = new TimeSpanStatistics();
                servicecall.Start();
                Stream stream = proxy.GetStudyHeader(AETitle.Text, parms);
                
                servicecall.End();


                var decompression = new TimeSpanStatistics();
                decompression.Start();
                
                //GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress);
                var theMemento = new StudyXmlMemento();
                StudyXmlIo.ReadGzip(theMemento, stream);
                //doc.Load(gzipStream);

                decompression.End();
                

                var settings = new XmlWriterSettings();
                //settings.Indent = true;
                settings.NewLineOnAttributes = false;
                settings.OmitXmlDeclaration = true;
                settings.Encoding = Encoding.UTF8;
                StringWriter sw = new StringWriter();
                XmlWriter writer = XmlWriter.Create(sw, settings);
                theMemento.Document.WriteTo(writer);
                writer.Flush();
                Log(sw.ToString());

                TimeSpanStatistics loading = new TimeSpanStatistics();
                loading.Start();
                
                StudyXml xml = new StudyXml();
                xml.SetMemento(theMemento);
                loading.End();

                int sopCounter = 0;
                SeriesTree.Nodes.Clear();
                foreach(SeriesXml series in xml)
                {
                    TreeNode seriesNode = new TreeNode(series.SeriesInstanceUid);
                    SeriesTree.Nodes.Add(seriesNode);
                    foreach(InstanceXml instance in series)
                    {
                        TreeNode instanceNode = new TreeNode(instance.SopInstanceUid);
                        seriesNode.Nodes.Add(instanceNode);
                        sopCounter++;
                    }
                }
                
               

                StatisticsLog.Text="";
                StatisticsLog.Text += String.Format("\r\nHeader Size (Decompressed): {0} KB", sw.ToString().Length / 1024);
                
                StatisticsLog.Text += String.Format("\r\nWCF Service call  : {0} ms", servicecall.Value.TotalMilliseconds);
                StatisticsLog.Text += String.Format("\r\nDecompression    : {0} ms", decompression.Value.TotalMilliseconds);
                StatisticsLog.Text += String.Format("\r\nLoading StudyXml : {0} ms", loading.Value.TotalMilliseconds);
                

                SeriesLabel.Text = String.Format("Series : {0} \tInstances: {1}", SeriesTree.Nodes.Count, sopCounter);

                stream.Close();

            }
            catch(FaultException<StudyIsInUseFault> ex)
            {
                timer1.Stop();
                MessageBox.Show(String.Format("StudyIsInUseFault received:{0}\n\nState={1}" ,
                            ex.Message, ex.Detail.StudyState));
            }
            catch (FaultException<StudyIsNearlineFault> ex)
            {
                timer1.Stop();
                MessageBox.Show("StudyIsNearlineFault received:\n" + ex.Message);
                
            }
            catch (FaultException<StudyNotFoundFault> ex)
            {
                timer1.Stop();
                MessageBox.Show("StudyNotFoundFault received:\n" + ex.Message);
                
            }
            catch (Exception ex)
            {
                timer1.Stop();
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Close();
            }

        }
예제 #45
0
		/// <summary>
		/// Get a list of paths to the first image in each series within the study being processed.
		/// </summary>
		/// <returns></returns>
		private List<string> GetFirstInstanceInEachStudySeries()
		{
			var fileList = new List<string>();

			if (_studyXml == null)
			{
			    string studyXml = _location.GetStudyXmlPath();

				if (!File.Exists(studyXml))
				{
					return fileList;
				}

				_studyXml = new StudyXml();

				using (FileStream stream = FileStreamOpener.OpenForRead(studyXml, FileMode.Open))
				{
					var theMemento = new StudyXmlMemento();
					StudyXmlIo.Read(theMemento, stream);
					stream.Close();
					_studyXml.SetMemento(theMemento);
				}
			}

			// Note, we try and force ourselves to have an uncompressed 
			// image, if one exists.  That way the rules will be reapplied on the object
			// if necessary for compression.
			foreach (SeriesXml seriesXml in _studyXml)
			{
				InstanceXml saveInstance = null;

				foreach (InstanceXml instance in seriesXml)
				{
					if (instance.TransferSyntax.Encapsulated)
					{
						if (saveInstance == null)
							saveInstance = instance;
					}
					else
					{
						saveInstance = instance;
						break;
					}
				}

				if (saveInstance != null)
				{
					string path = Path.Combine(_location.GetStudyPath(), seriesXml.SeriesInstanceUid);
					path = Path.Combine(path, saveInstance.SopInstanceUid + ServerPlatform.DicomFileExtension);
					fileList.Add(path);
				}
			}

			return fileList;
		}
예제 #46
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
        /// </summary>
        /// <param name="location">The location a study is stored.</param>
        /// <returns>The <see cref="StudyXml"/> instance for <paramref name="location"/></returns>
        protected virtual StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            StudyXml theXml = new StudyXml();

            StudyXmlLoadTime.Add(
                delegate
                    {
                        String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");
                        if (File.Exists(streamFile))
                        {
                            using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                            {
                                XmlDocument theDoc = new XmlDocument();

                                StudyXmlIo.Read(theDoc, fileStream);

                                theXml.SetMemento(theDoc);

                                fileStream.Close();
                            }
                        }
                    }
                );

           return theXml;
        }
예제 #47
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]);
		}
예제 #48
0
        private void _buttonLoadGzipXml_Click(object sender, EventArgs e)
        {
            openFileDialog.DefaultExt = "gzip";
            openFileDialog.ShowDialog();

            Stream fileStream = openFileDialog.OpenFile();

            XmlDocument theDoc = new XmlDocument();

            StudyXmlIo.ReadGzip(theDoc, fileStream);

            fileStream.Close();

            _theStream = new StudyXml();

            _theStream.SetMemento(theDoc);
        }
예제 #49
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);
		}
예제 #50
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);
			}
		}
예제 #51
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);
			}
		}
예제 #52
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for the <see cref="StudyLocation"/>
        /// </summary>
        /// <returns>The <see cref="StudyXml"/> instance</returns>
        public StudyXml LoadStudyXml()
        {
            var theXml = new StudyXml();

            string streamFile = GetStudyXmlPath();
            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }
            return theXml;
        }