private void RetrievingFilesFromDirectory() { try { String indexFileName = initialDirectory + @"/index1.htm"; StringBuilder indexContent = new StringBuilder(); indexContent.Append("<center><font size='5' color='#0000ff'>"); if (utility.AnonymizationType) { indexContent.Append("Anonymized DCM files (Basic)</font></center>"); } else { indexContent.Append("Anonymized DCM files (Complete)</font></center>"); } indexContent.Append("<left><font color='black' size = '4'><br></br> Input Directory: " + inputDirectory); indexContent.Append("</font>"); indexContent.Append("</left>"); indexContent.Append("<br></br>"); indexContent.Append("<font size='3'>"); HLI.DataSet srcDataset = null; DicomFile dcmFile = null; allDCMFilesTemp = new ArrayList(); FileInfo mediaInputFileInfo = null; string mediaFileDir = inputDirectory; DirectoryInfo theDirectoryInfo = new DirectoryInfo(mediaFileDir); allDCMFilesTemp = utility.GetFilesRecursively(theDirectoryInfo); foreach (string fileName in allDCMFilesTemp) { mediaInputFileInfo = new FileInfo(fileName); counter++; dcmFile = new DicomFile(); dcmFile.Read(fileName, mainThread); // Get the Data set from the selected DCM file srcDataset = dcmFile.DataSet; indexContent.Append("<li><a href='" + mediaInputFileInfo.Name + "_" + counter.ToString() + ".html'>" + fileName.Substring(inputDirectory.Length) + "</a></li>"); HLI.DataSet annonymizedDataset = srcDataset.Clone(); utility.PatientAttributes(annonymizedDataset); for (int number = 0; number < annonymizedDataset.Count; number++) { HLI.Attribute attribute = annonymizedDataset[number]; if (attribute.VR == VR.SQ) { utility.SequenceAttribute_recursive(attribute); } else { utility.CacheAndRepairIdentifyingAttribute(attribute); utility.UpdateAnonymizedAttributes(attribute); } } //Save annonymized data to selected directory string savedFileName = ""; if (mediaInputFileInfo.Extension != "") { string srcFileNameWithoutExtn = mediaInputFileInfo.Name.Substring(0, (mediaInputFileInfo.Name.Length - 4)); savedFileName = outputDirectory + "\\" + srcFileNameWithoutExtn + "_an"; } else { savedFileName = outputDirectory + "\\" + mediaInputFileInfo.Name + "_an"; } dcmFile.DataSet = annonymizedDataset; dcmFile.Write(savedFileName); datasets = new AttributeCollections(); datasets.Add(srcDataset); datasets.Add(annonymizedDataset); //Cleanup temp file utility.cleanup(); resultFileName = mediaInputFileInfo.Name; CreateTableWithoutNav(); } StreamWriter writer = new StreamWriter(indexFileName); indexContent.Append("</font>"); indexContent.Append("<left><font color='black' size = '4'><br></br> Output Directory: " + outputDirectory); indexContent.Append("</font>"); indexContent.Append("</left>"); writer.Write(indexContent.ToString()); writer.Close(); dvtkWebBrowser.Navigate(indexFileName); } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
protected override void Execute() { try { WriteHtmlInformation("<br />"); WriteInformation(string.Format("Reading reference media file from {0}", DCMCompareForm.firstDCMFile)); WriteHtmlInformation("<br />"); // Read the DCM File DicomFile dcmFile = new DicomFile(); DataSet refDataset = new DataSet(); if ((DCMCompareForm.firstDCMFile.ToLower().IndexOf("dicomdir")) != -1) { // Read the DICOMDIR dataset refDataset.DvtkDataDataSet = Dvtk.DvtkDataHelper.ReadDataSetFromFile(DCMCompareForm.firstDCMFile); } else { dcmFile.Read(DCMCompareForm.firstDCMFile, this); refDataset = dcmFile.DataSet; } refDataset.UnVrDefinitionLookUpWhenReading = false; FileMetaInformation refFMI = dcmFile.FileMetaInformation; WriteInformation(string.Format("Reading source media file from {0}", DCMCompareForm.secondDCMFile)); WriteHtmlInformation("<br />"); DataSet srcDataset = new DataSet(); if ((DCMCompareForm.secondDCMFile.ToLower().IndexOf("dicomdir")) != -1) { // Read the DICOMDIR dataset srcDataset.DvtkDataDataSet = Dvtk.DvtkDataHelper.ReadDataSetFromFile(DCMCompareForm.secondDCMFile); } else { dcmFile.Read(DCMCompareForm.secondDCMFile, this); srcDataset = dcmFile.DataSet; } srcDataset.UnVrDefinitionLookUpWhenReading = false; FileMetaInformation srcFMI = dcmFile.FileMetaInformation; // Now get the list of filtered attribute if (DCMCompareForm.attributesTagList.Count != 0) { foreach (string filterAttr in DCMCompareForm.attributesTagList) { try { if (srcDataset.Exists(filterAttr)) { srcDataset.Delete(filterAttr); } if (refDataset.Exists(filterAttr)) { refDataset.Delete(filterAttr); } } catch (Exception exception) { MessageBox.Show(exception.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } DCMCompareForm.attributesTagList.Clear(); } WriteInformation(string.Format("Comparing the Media files - {0} and {1}", DCMCompareForm.firstDCMFile, DCMCompareForm.secondDCMFile)); WriteHtmlInformation("<br />"); StaticDicomCompare staticDicomCompare = new StaticDicomCompare(); //Determine the VR display based on Transfer syntax string srcTransferSyntax = ""; string refTransferSyntax = ""; if ((srcFMI != null) && srcFMI.Exists("0x00020010")) { // Get the Transfer syntax DvtkHighLevelInterface.Dicom.Other.Attribute tranferSyntaxAttr = srcFMI["0x00020010"]; srcTransferSyntax = tranferSyntaxAttr.Values[0]; } else { WriteHtmlInformation(string.Format("Couldn't retrieve the Transfer syntax from DCM File {0}", DCMCompareForm.secondDCMFile)); WriteHtmlInformation("<br />"); } if ((refFMI != null) && refFMI.Exists("0x00020010")) { // Get the Transfer syntax DvtkHighLevelInterface.Dicom.Other.Attribute tranferSyntaxAttr = refFMI["0x00020010"]; refTransferSyntax = tranferSyntaxAttr.Values[0]; } else { WriteHtmlInformation(string.Format("Couldn't retrieve the Transfer syntax from DCM File {0}", DCMCompareForm.firstDCMFile)); WriteHtmlInformation("<br />"); } FlagsDicomAttribute flags = FlagsDicomAttribute.Compare_values | FlagsDicomAttribute.Compare_present | FlagsDicomAttribute.Include_sequence_items; if ((srcTransferSyntax == "1.2.840.10008.1.2") || (refTransferSyntax == "1.2.840.10008.1.2")) { staticDicomCompare.DisplayAttributeVR = false; } else { flags |= FlagsDicomAttribute.Compare_VR; } //Check for group length attributes option if (DCMCompareForm.filterGroupLengthAttributes) { /*for( int i=0; i < srcDataset.Count; i++ ) * { * HLI.Attribute attribute = srcDataset[i]; * if(attribute.ElementNumber == 0x0000) * srcDataset.Delete(DCMCompareForm.TagString(attribute.GroupNumber,attribute.ElementNumber)); * } * * for( int i=0; i < refDataset.Count; i++ ) * { * HLI.Attribute attribute = refDataset[i]; * if(attribute.ElementNumber == 0x0000) * refDataset.Delete(DCMCompareForm.TagString(attribute.GroupNumber,attribute.ElementNumber)); * }*/ staticDicomCompare.DisplayGroupLength = false; } int differences = 0; if ((refFMI != null) && (srcFMI != null)) { AttributeCollections fmis = new AttributeCollections(); fmis.Add(refFMI); fmis.Add(srcFMI); StringCollection fmiDescriptions = new StringCollection(); fmiDescriptions.Add("Ref FMI"); fmiDescriptions.Add("Src FMI"); HLIStaticCompare.CompareResults fmiCompareResults = staticDicomCompare.CompareAttributeSets("FMI compare results", fmis, fmiDescriptions, flags); WriteHtmlInformation(fmiCompareResults.Table.ConvertToHtml()); differences += fmiCompareResults.DifferencesCount; NrOfValidationErrors += (uint)fmiCompareResults.DifferencesCount; } AttributeCollections datasets = new AttributeCollections(); datasets.Add(refDataset); datasets.Add(srcDataset); StringCollection dsDescriptions = new StringCollection(); dsDescriptions.Add("Ref Dataset"); dsDescriptions.Add("Src Dataset"); HLIStaticCompare.CompareResults dsCompareResults = staticDicomCompare.CompareAttributeSets("DataSet compare results", datasets, dsDescriptions, flags); WriteHtmlInformation(dsCompareResults.Table.ConvertToHtml()); differences += dsCompareResults.DifferencesCount; WriteHtmlInformation("<b>"); WriteInformation("Differences found: " + differences.ToString()); WriteHtmlInformation("</b><br />"); NrOfValidationErrors += (uint)dsCompareResults.DifferencesCount; } catch (Exception exception) { MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
private void AnonymizeDCMFile() { try { utility.AnonymizationType = anonymizationMode; CreateDICOMDIR.Enabled = true; OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = false; fileDialog.ReadOnlyChecked = true; fileDialog.Title = "Select DCM File"; fileDialog.Filter = "DCM files (*.dcm) |*.dcm|All files (*.*)|*.*"; // Show the file dialog. // If the user pressed the OK button... if (fileDialog.ShowDialog() == DialogResult.OK) { DicomFile dcmFile = new DicomFile(); dcmFile.Read(fileDialog.FileName, mainThread); // Get the Data set from the selected DCM file HLI.DataSet srcDataset = dcmFile.DataSet; SaveFileDialog saveDialog = new SaveFileDialog(); FileInfo saveFileInfo = null; saveDialog.Filter = "DCM files (*.dcm) |*.dcm|All files (*.*)|*.*"; if (saveDialog.ShowDialog() == DialogResult.OK) { HLI.DataSet annonymizedDataset = srcDataset.Clone(); utility.PatientAttributes(annonymizedDataset); for (int number = 0; number < annonymizedDataset.Count; number++) { HLI.Attribute attribute = annonymizedDataset[number]; if (attribute.VR == VR.SQ) { utility.SequenceAttribute_recursive(attribute); } else { utility.CacheAndRepairIdentifyingAttribute(attribute); utility.UpdateAnonymizedAttributes(attribute); } } datasets = new AttributeCollections(); datasets.Add(srcDataset); datasets.Add(annonymizedDataset); //Save annonymized data to selected file saveFileInfo = new FileInfo(saveDialog.FileName); dcmFile.DataSet = annonymizedDataset; dcmFile.Write(saveFileInfo.FullName); //Cleanup temp file utility.cleanup(); } else { return; } string htmlFileName = initialDirectory + "\\" + saveFileInfo.Name + ".html"; XmlTextWriter writer = new XmlTextWriter(htmlFileName, System.Text.Encoding.UTF8); writer.WriteStartElement("b"); writer.WriteRaw(@"<b><font size='3' color='#ff0000'>DCM File Anonymization Started...</font></b>"); writer.WriteEndElement(); writer.Close(); resultFileName = htmlFileName; System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(CreateTable)); dvtkWebBrowser.Navigate(htmlFileName); t.Start(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Compare the DICOM files in the two directories based on there being corresponding /// files in each directory with the same attribute value for the matching Tag. /// </summary> /// <param name="directory1">Full directory name of first directory.</param> /// <param name="directory2">Full directory name of second directory.</param> /// <param name="matchingTag">DICOM Tag value to match in a file in each directory.</param> /// <returns>Total number of differences between compared files.</returns> public int Compare(String directory1, String directory2, DvtkData.Dimse.Tag matchingTag) { int totalDifferences = 0; StreamWriter htmlOutput = null; try { // create the output file writer if necessary if (_htmlOutputFilename != String.Empty) { htmlOutput = new StreamWriter(_htmlOutputFilename); } // compare all files in directory 1 with corresponding files in directory 2 // - the files compared is based on them having the same attribute value for the // Tag given. DirectoryInfo directoryInfo = new DirectoryInfo(directory1); FileInfo[] fileInfo = directoryInfo.GetFiles(); for (int i = 0; i < fileInfo.Length; i++) { DataSet dataset1 = new DataSet(); String filename1 = fileInfo[i].FullName; dataset1.DvtkDataDataSet = Dvtk.DvtkDataHelper.ReadDataSetFromFile(filename1); // get the attribute value for the matching tag from this dataset String valueToMatch = GetAttributeValueFromDataset(dataset1, matchingTag); if (valueToMatch != String.Empty) { // try to find a dataset (file) containing the same value in directory 2 String filename2 = String.Empty; DataSet dataset2 = GetMatchingDatasetFromStoreDataDirectory(directory2, matchingTag, valueToMatch, out filename2); // if a dataset is returned that contains the matching attribute value go on to compare all the attributes // in both datasets with eachother if (dataset2 != null) { // get a new compare instance and set the comparison flags StaticDicomCompare staticDicomCompare = new StaticDicomCompare(); FlagsDicomAttribute flags = FlagsDicomAttribute.Compare_values | FlagsDicomAttribute.Compare_present | FlagsDicomAttribute.Include_sequence_items; if (_compareVr == false) { staticDicomCompare.DisplayAttributeVR = false; } else { flags |= FlagsDicomAttribute.Compare_VR; } staticDicomCompare.DisplayGroupLength = _displayGroupLength; dataset1.UnVrDefinitionLookUpWhenReading = _unVrDefinitionLookUpWhenReading; dataset2.UnVrDefinitionLookUpWhenReading = _unVrDefinitionLookUpWhenReading; AttributeCollections datasets = new AttributeCollections(); datasets.Add(dataset1); datasets.Add(dataset2); StringCollection datasetDescriptions = new StringCollection(); datasetDescriptions.Add(filename1); datasetDescriptions.Add(filename2); String title = String.Format("Comparison Results with matching using Tag with {0}", matchingTag.ToString()); DvtkHighLevelInterface.Common.Compare.CompareResults datasetCompareResults = staticDicomCompare.CompareAttributeSets(title, datasets, datasetDescriptions, flags); if (htmlOutput != null) { if (_includeDetailedResults == true) { htmlOutput.WriteLine(datasetCompareResults.Table.ConvertToHtml()); } else { htmlOutput.WriteLine("<br />"); String message = String.Format("Compared {0} with {1} - number of differences: {2}", filename1, filename2, datasetCompareResults.DifferencesCount); htmlOutput.WriteLine(message); htmlOutput.WriteLine("<br />"); } } // update the total differences counter totalDifferences += datasetCompareResults.DifferencesCount; } } } } catch (System.Exception) { } if (htmlOutput != null) { htmlOutput.Flush(); htmlOutput.Close(); } return(totalDifferences); }