/// <summary> /// Load the Information Model by reading all the .DCM and .RAW files /// present in the given directory. The data read is normalised into the /// Information Model. /// </summary> /// <param name="dataDirectory">Source data directory containing the .DCm and .RAW files.</param> public override void LoadInformationModel(System.String dataDirectory) { DataDirectory = dataDirectory; DirectoryInfo directoryInfo = new DirectoryInfo(DataDirectory); foreach (FileInfo fileInfo in directoryInfo.GetFiles()) { if ((fileInfo.Extension.ToLower().Equals(".dcm")) || (fileInfo.Extension.ToLower().Equals(".raw")) || (fileInfo.Extension == "") || (fileInfo.Extension == null)) { try { // read the DCM file Dvtk.Sessions.ScriptSession dvtkScriptSession = new Dvtk.Sessions.ScriptSession(); String tempPath = Path.GetTempPath(); dvtkScriptSession.StorageMode = Dvtk.Sessions.StorageMode.AsMedia; dvtkScriptSession.DataDirectory = tempPath; dvtkScriptSession.ResultsRootDirectory = tempPath; DvtkData.Media.DicomFile dvtkDataDicomFile = dvtkScriptSession.ReadFile(fileInfo.FullName); // Add DICOM file to Information Model - but do not re-save in file AddToInformationModel(dvtkDataDicomFile, false); } catch (Exception) { //Invalid DICOM File - will be skiped from QR information model. } } } }
/// <summary> /// Writes a Dicom file. /// </summary> /// <remarks> /// The group length attribute (0002,0000) will be set with the correct value /// automatically before writing to file begins. /// </remarks> /// <exception cref="HliException"> /// Writing to the file fails. /// </exception> /// <param name="fullFileName">The full file name.</param> /// <example> /// <b>VB .NET</b> /// <code> /// ' Example: Read a specified DICOM file and /// 'write it to back to a DICOM file /// /// Dim myDicomFile As DvtkHighLevelInterface.Dicom.Files.DicomFile /// /// If File.Exists("c:\somefile.dcm") Then /// Try /// myDicomFile.Read("c:\Somefile.dcm") /// Catch ex As DvtkHighLevelInterface.Common.Other.HliException /// ' Error reading the file, Maybe the file format is wrong? /// End Try /// 'here is where you can manipulate the file /// 'write the dataset to a file /// myDicomFile.Write("c:\newfile.dcm") /// End If /// </code> /// </example> public void Write(String fullFileName) { if (FileMetaInformation.TransferSyntax == "") { FileMetaInformation.TransferSyntax = "1.2.840.10008.1.2.1"; } Dvtk.Sessions.ScriptSession dvtkScriptSession = CreateEmptyDvtkScriptSession(); if (this.AddGroupLength) { dvtkScriptSession.AddGroupLength = true; } DvtkData.Media.DicomFile dvtkDataDicomFile = new DvtkData.Media.DicomFile(); dvtkDataDicomFile.DataSet = this.dataSet.DvtkDataDataSet; dvtkDataDicomFile.FileMetaInformation = this.fileMetaInformation.DvtkDataFileMetaInformation; dvtkDataDicomFile.FileHead = this.fileMetaInformation.DvtkDataFileHead; if (!dvtkScriptSession.WriteFile(dvtkDataDicomFile, fullFileName)) { throw new HliException("Error while writing file \"" + fullFileName + "\"."); } }
public void Read(String fullFileName, params String[] definitionFilesFullName) { DvtkData.Media.DicomFile dvtkDataDicomFile = null; if (!File.Exists(fullFileName)) { throw new HliException("Dicom file or raw file \"" + fullFileName + "\" not found."); } String tempPath = Path.GetTempPath(); Dvtk.Sessions.ScriptSession dvtkScriptSession = new Dvtk.Sessions.ScriptSession(); dvtkScriptSession.StorageMode = Dvtk.Sessions.StorageMode.AsMedia; dvtkScriptSession.DataDirectory = tempPath; dvtkScriptSession.ResultsRootDirectory = tempPath; foreach (String definitionFileFullName in definitionFilesFullName) { dvtkScriptSession.DefinitionManagement.LoadDefinitionFile(definitionFileFullName); } dvtkDataDicomFile = dvtkScriptSession.ReadFile(fullFileName); this.dataSet.DvtkDataDataSet = dvtkDataDicomFile.DataSet; this.FileMetaInformation.DvtkDataFileMetaInformation = dvtkDataDicomFile.FileMetaInformation; this.dvtkDataFileHead = dvtkDataDicomFile.FileHead; }
public bool Write(String fullFileName) { Dvtk.Sessions.ScriptSession dvtkScriptSession = new Dvtk.Sessions.ScriptSession(); DvtkData.Media.DicomFile dvtkDataDicomFile = new DvtkData.Media.DicomFile(); dvtkDataDicomFile.DataSet = this.dataSet.DvtkDataDataSet; dvtkDataDicomFile.FileMetaInformation = this.fileMetaInformation.DvtkDataFileMetaInformation; dvtkDataDicomFile.FileHead = this.dvtkDataFileHead; return(dvtkScriptSession.WriteFile(dvtkDataDicomFile, fullFileName)); }
public void Read(String fullFileName, DicomThread dicomThread) { DvtkData.Media.DicomFile dvtkDataDicomFile = null; if (!File.Exists(fullFileName)) { throw new HliException("Dicom file or raw file \"" + fullFileName + "\" not found."); } dvtkDataDicomFile = dicomThread.DvtkScriptSession.ReadFile(fullFileName); this.dataSet.DvtkDataDataSet = dvtkDataDicomFile.DataSet; this.fileMetaInformation.DvtkDataFileMetaInformation = dvtkDataDicomFile.FileMetaInformation; this.dvtkDataFileHead = dvtkDataDicomFile.FileHead; }
/// <summary> /// Reads a file. /// </summary> /// <remarks> /// A new FileMetaInformation and DataSet object will be created inside this object. They will be /// filled with the content of the specified file. /// The FileMetaInformation and DataSet object previously used will not change (they will not be /// used anymore by this object) and can still be used outside this object if needed. /// /// Also see properties UnVrDefinitionLookUpWhenReading and StoreOBOFOWValuesWhenReading. /// /// If something goes wrong while reading the file, an exception is thrown. /// </remarks> /// <param name="fullFileName">The full file name.</param> /// <param name="dvtkScriptSession">The dvtk ScriptSession, from which the definition files to use for determining the attribute names are used.</param> private void Read(String fullFileName, Dvtk.Sessions.ScriptSession dvtkScriptSession) { // Create new FileMetaInformation and DataSet (depending on setting) objects. if (this.createNewDataSetObjectWhenReading) { this.dataSet = new DataSet(); } // Throw an excpetion if the file doesn't exist. if (!File.Exists(fullFileName)) { throw new HliException("File \"" + fullFileName + "\" not found."); } // Read the DicomFile using the supplied DicomThread. bool originalUnVrDefinitionLookUp = dvtkScriptSession.UnVrDefinitionLookUp; Dvtk.Sessions.StorageMode originalDvtkStorageMode = dvtkScriptSession.StorageMode; dvtkScriptSession.UnVrDefinitionLookUp = this.unVrDefinitionLookUpWhenReading; if (this.storeOBOFOWValuesWhenReading) { dvtkScriptSession.StorageMode = Dvtk.Sessions.StorageMode.AsDataSet; } else { dvtkScriptSession.StorageMode = Dvtk.Sessions.StorageMode.NoStorage; } DvtkData.Media.DicomFile dvtkDataDicomFile = dvtkScriptSession.ReadFile(fullFileName); if (dvtkDataDicomFile == null) { throw new HliException("Error while reading file \"" + fullFileName + "\"."); } else { this.dataSet.DvtkDataDataSet = dvtkDataDicomFile.DataSet; this.FileMetaInformation = new FileMetaInformation(dvtkDataDicomFile.FileMetaInformation, dvtkDataDicomFile.FileHead); } dvtkScriptSession.UnVrDefinitionLookUp = originalUnVrDefinitionLookUp; dvtkScriptSession.StorageMode = originalDvtkStorageMode; }
/// <summary> /// Store the DICOM file into a DICOM Media File in the current Information Model directory. /// The file contents might be later used for retrieval.The transfer syntax is also passed so that the TS specified /// in the dcm file is used while moving the file to a Storage SCP /// </summary> /// <param name="dicomFile">The DICOM file to store</param> protected void StoreDicomFile(DvtkData.Media.DicomFile dicomFile) { System.String iom = System.String.Empty; if (Name.Equals("PatientRootInformationModel")) { iom = "PR"; } else if (Name.Equals("StudyRootInformationModel")) { iom = "SR"; } else if (Name.Equals("PatientStudyOnlyInformationModel")) { iom = "PS"; } // generate a filename System.String filename; filename = System.String.Format("{0}\\DVTK_IOM_TMP_{1}_{2:00000000}.DCM", DataDirectory, iom, _fileIndex++); DvtkData.Media.DicomFile dicomMediaFile = new DvtkData.Media.DicomFile(); // set up the file head DvtkData.Media.FileHead FileHead = new DvtkData.Media.FileHead(); // add the Transfer Syntax UID //DvtkData.Dul.TransferSyntax transferSyntax = new DvtkData.Dul.TransferSyntax(DvtkData.Dul.TransferSyntax.Explicit_VR_Little_Endian.UID); FileHead.TransferSyntax = dicomFile.FileHead.TransferSyntax; // set up the file meta information DvtkData.Media.FileMetaInformation fileMetaInformation = new DvtkData.Media.FileMetaInformation(); // add the FMI version fileMetaInformation.AddAttribute(Tag.FILE_META_INFORMATION_VERSION.GroupNumber, Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2); // add the SOP Class UID System.String sopClassUid = ""; DvtkData.Dimse.Attribute attribute = dicomFile.DataSet.GetAttribute(DvtkData.Dimse.Tag.SOP_CLASS_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopClassUid = uniqueIdentifier.Values[0]; } } fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber, Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, sopClassUid); // add the SOP Instance UID System.String sopInstanceUid = ""; attribute = dicomFile.DataSet.GetAttribute(DvtkData.Dimse.Tag.SOP_INSTANCE_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopInstanceUid = uniqueIdentifier.Values[0]; } } fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber, Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, sopInstanceUid); // add the Transfer Syntax UID fileMetaInformation.AddAttribute(Tag.TRANSFER_SYNTAX_UID.GroupNumber, Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, dicomFile.FileHead.TransferSyntax); // add the Implemenation Class UID DvtkData.Dimse.Attribute implentationUID = dicomFile.FileMetaInformation.GetAttribute(0x00020012); fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_CLASS_UID.GroupNumber, Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, implentationUID); // add the Implementation Version Name DvtkData.Dimse.Attribute implementationVersion = dicomFile.FileMetaInformation.GetAttribute(0x00020013); fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber, Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, implementationVersion); // set up the dicomMediaFile contents dicomMediaFile.FileHead = FileHead; dicomMediaFile.FileMetaInformation = fileMetaInformation; dicomMediaFile.DataSet = dicomFile.DataSet; // write the dicomMediaFile to file Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, filename); // save the filename in the dataset dicomFile.DataSet.Filename = filename; }
public abstract void AddToInformationModel(DvtkData.Media.DicomFile dicomFile, bool storeFile);
void SaveFileAsDefinedTS(string transferSyntax) { if (DCMFile == "") { richTextBoxLog.AppendText("There is no Media file to save, please select Media file.\n"); return; } try { SaveFileDialog saveAsDlg = new SaveFileDialog(); saveAsDlg.Filter = "DCM files (*.dcm) |*.dcm | All files (*.*)|*.*"; FileInfo dcmFileInfo = new FileInfo(DCMFile); saveAsDlg.InitialDirectory = dcmFileInfo.DirectoryName; saveAsDlg.FileName = dcmFileInfo.Name; saveAsDlg.Title = "Save DICOM file..."; if (saveAsDlg.ShowDialog() == DialogResult.OK) { richTextBoxLog.AppendText("Saving the modified Media file.\n"); _NewDCMFileName = saveAsDlg.FileName.Trim(); if (DCMFile != _NewDCMFileName) _IsSavedInNewDCMFile = true; DvtkData.Media.DicomFile dicomMediaFile = new DvtkData.Media.DicomFile(); // set up the file head DvtkData.Media.FileHead fileHead = new DvtkData.Media.FileHead(); // add the Transfer Syntax UID DvtkData.Dul.TransferSyntax tSyntax = new DvtkData.Dul.TransferSyntax(transferSyntax); fileHead.TransferSyntax = tSyntax; // set up the file meta information DvtkData.Media.FileMetaInformation fileMetaInformation = new DvtkData.Media.FileMetaInformation(); // add the FMI version fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.FILE_META_INFORMATION_VERSION.GroupNumber, DvtkData.Dimse.Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2); // add the SOP Class UID fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber, DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, _FileMetaInfo.MediaStorageSOPClassUID); // add the SOP Instance UID fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber, DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, _FileMetaInfo.MediaStorageSOPInstanceUID); // add the Transfer Syntax UID fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID.GroupNumber, DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, transferSyntax); // add the Implemenation Class UID string implClassUID = ""; if(_FileMetaInfo.Exists("0x00020012")) { // Get the Transfer syntax HLI.Attribute implClassUIDAttr = _FileMetaInfo["0x00020012"]; implClassUID = implClassUIDAttr.Values[0]; } fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.IMPLEMENTATION_CLASS_UID.GroupNumber, DvtkData.Dimse.Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, implClassUID); // add the Implementation Version Name string implClassVersion = ""; if(_FileMetaInfo.Exists("0x00020013")) { // Get the Transfer syntax HLI.Attribute implClassVersionAttr = _FileMetaInfo["0x00020013"]; implClassVersion = implClassVersionAttr.Values[0]; } fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber, DvtkData.Dimse.Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, implClassVersion); // set up the dicomMediaFile contents dicomMediaFile.FileHead = fileHead; dicomMediaFile.FileMetaInformation = fileMetaInformation; dicomMediaFile.DataSet = _DCMdataset.DvtkDataDataSet; // write the dicomMediaFile to file Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, _NewDCMFileName); string theLogText = string.Format("Dataset {0} saved successfully with transfer syntax: {1}.\n\n", _NewDCMFileName, transferSyntax); richTextBoxLog.AppendText(theLogText); richTextBoxLog.ScrollToCaret(); richTextBoxLog.Focus(); //Cleanup the FMI & Dataset IsDCMFileModified = false; _IsAttributeGroupLengthDefined = false; _DCMdataset = null; _FileMetaInfo = null; if (_IsSavedInNewDCMFile) { FileInfo newDcmFileInfo = new FileInfo(_NewDCMFileName); _IsNewDCMFileLoaded = true; this.dirListBox.Path = newDcmFileInfo.DirectoryName; this.fileListBox.Path = this.dirListBox.Path; this.fileListBox.SelectedItem = newDcmFileInfo.Name; this.dirListBox.Refresh(); this.fileListBox.Refresh(); //Load the new Media file LoadDCMFile(_NewDCMFileName); } else { _IsNewDCMFileLoaded = false; this.dirListBox.Path = dcmFileInfo.DirectoryName; this.fileListBox.Path = this.dirListBox.Path; this.fileListBox.SelectedItem = dcmFileInfo.Name; //Load the new Media file LoadDCMFile(DCMFile); } UpdateTitleBarText(); } } catch (Exception exception) { string theErrorText; theErrorText = string.Format("Media file {0} could not be saved:\n{1}\n\n", _NewDCMFileName, exception.Message); richTextBoxLog.AppendText(theErrorText); richTextBoxLog.ScrollToCaret(); richTextBoxLog.Focus(); IsDCMFileModified = false; UpdateTitleBarText(); } }
/// <summary> /// Add the given dataset present in a Dicom File to the Information Model. The data is normalised into the Information Model. /// </summary> /// <param name="dicomFile">The dicom File containing the dataset to be added.</param> /// <param name="storeFile">Boolean indicating whether the dataset should be stored or not.</param> public override void AddToInformationModel(DvtkData.Media.DicomFile dicomFile, bool storeFile) { // STUDY level PatientStudyInformationEntity patientStudyInformationEntity = null; this.IsDataStored = storeFile; // check if the patient/study IE is already in the studyRootList foreach (PatientStudyInformationEntity lPatientStudyInformationEntity in Root) { if (lPatientStudyInformationEntity.IsUniqueTagFoundIn(dicomFile.DataSet)) { patientStudyInformationEntity = lPatientStudyInformationEntity; patientStudyInformationEntity.CheckForSpecialTags(dicomFile.DataSet); break; } } // patient/study IE is not already in the studyRootList if (patientStudyInformationEntity == null) { // create a new patient/study IE from the dataset and add to the studyRootList patientStudyInformationEntity = new PatientStudyInformationEntity(); patientStudyInformationEntity.CopyFrom(dicomFile.DataSet); Root.Add(patientStudyInformationEntity); } // SERIES level SeriesInformationEntity seriesInformationEntity = null; // check if the series IE is already in the study IE children foreach (SeriesInformationEntity lSeriesInformationEntity in patientStudyInformationEntity.Children) { if (lSeriesInformationEntity.IsUniqueTagFoundIn(dicomFile.DataSet)) { seriesInformationEntity = lSeriesInformationEntity; seriesInformationEntity.CheckForSpecialTags(dicomFile.DataSet); break; } } // series IE is not already in the study IE children if (seriesInformationEntity == null) { // create a new series IE from the dataset and add to the study IE children seriesInformationEntity = new SeriesInformationEntity(); seriesInformationEntity.CopyFrom(dicomFile.DataSet); patientStudyInformationEntity.AddChild(seriesInformationEntity); } // IMAGE (Instance) level InstanceInformationEntity instanceInformationEntity = null; // check if the instance IE is already in the series IE children foreach (InstanceInformationEntity lInstanceInformationEntity in seriesInformationEntity.Children) { if (lInstanceInformationEntity.IsUniqueTagFoundIn(dicomFile.DataSet)) { instanceInformationEntity = lInstanceInformationEntity; instanceInformationEntity.CheckForSpecialTags(dicomFile.DataSet); break; } } // instance IE is not already in the series IE children if (instanceInformationEntity == null) { // Store the dicom File as a DCM file if requested. if (storeFile == true) { StoreDicomFile(dicomFile); } // create a new instance IE from the dataset and add to the series IE children instanceInformationEntity = new InstanceInformationEntity(dicomFile.DataSet.Filename); instanceInformationEntity.CopyFrom(dicomFile.DataSet); seriesInformationEntity.AddChild(instanceInformationEntity); } patientStudyInformationEntity.CheckForSpecialTags(dicomFile.DataSet); seriesInformationEntity.CheckForSpecialTags(dicomFile.DataSet); instanceInformationEntity.CheckForSpecialTags(dicomFile.DataSet); }
public virtual void AddToInformationModel(DvtkData.Media.DicomFile dicomFile, bool storeFile) { }
public bool Write(String fullFileName) { Dvtk.Sessions.ScriptSession dvtkScriptSession = new Dvtk.Sessions.ScriptSession(); DvtkData.Media.DicomFile dvtkDataDicomFile = new DvtkData.Media.DicomFile(); dvtkDataDicomFile.DataSet = this.dataSet.DvtkDataDataSet; dvtkDataDicomFile.FileMetaInformation = this.fileMetaInformation.DvtkDataFileMetaInformation; dvtkDataDicomFile.FileHead = this.dvtkDataFileHead; return dvtkScriptSession.WriteFile(dvtkDataDicomFile, fullFileName); }