Exemplo n.º 1
0
        /// <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;
        }