ctkDICOMDatabaseTest6.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDir>
  17. // ctkDICOMCore includes
  18. #include "ctkDICOMDatabase.h"
  19. // STD includes
  20. #include <iostream>
  21. #include <cstdlib>
  22. int ctkDICOMDatabaseTest6( int argc, char * argv [] )
  23. {
  24. QCoreApplication app(argc, argv);
  25. if (argc < 2)
  26. {
  27. std::cerr << "ctkDICOMDatabaseTest6: missing dicom filePath argument";
  28. std::cerr << std::endl;
  29. return EXIT_FAILURE;
  30. }
  31. QString dicomFilePath(argv[1]);
  32. ctkDICOMDatabase database;
  33. QDir databaseDirectory = QDir::temp();
  34. databaseDirectory.remove("ctkDICOMDatabase.sql");
  35. databaseDirectory.remove("ctkDICOMTagCache.sql");
  36. QFileInfo databaseFile(databaseDirectory, QString("database.test"));
  37. database.openDatabase(databaseFile.absoluteFilePath());
  38. bool res = database.initializeDatabase();
  39. if (!res)
  40. {
  41. std::cerr << "ctkDICOMDatabase::initializeDatabase() failed." << std::endl;
  42. return EXIT_FAILURE;
  43. }
  44. //
  45. // Basic test:
  46. // - insert the file specified on the command line
  47. // - ask for name and descriptions and compare to known results
  48. //
  49. QString instanceUID("1.2.840.113619.2.135.3596.6358736.4843.1115808177.83");
  50. //
  51. // Test the pre load values feature of the database
  52. //
  53. QString preInsertDescription = database.descriptionForSeries(instanceUID);
  54. if (!preInsertDescription.isEmpty())
  55. {
  56. std::cerr
  57. << "ctkDICOMDatabase: db should return empty string for unknown "
  58. << " instance series description, instead got: "
  59. << preInsertDescription.toStdString() << std::endl;
  60. return EXIT_FAILURE;
  61. }
  62. database.insert(dicomFilePath, false, false);
  63. QString filePath = database.fileForInstance(instanceUID);
  64. std::cerr << "Instance file " << filePath.toStdString() << std::endl;
  65. // check for descriptions
  66. QHash<QString,QString> descriptions (database.descriptionsForFile(filePath));
  67. std::cout << "\tPatient Name: "
  68. << descriptions["PatientsName"].toStdString()
  69. << "\n\tStudy Desciption: "
  70. << descriptions["StudyDescription"].toStdString()
  71. << "\n\tSeries Desciption: "
  72. << descriptions["SeriesDescription"].toStdString()
  73. << std::endl;
  74. // check for known series description
  75. QString knownSeriesDescription("3D Cor T1 FAST IR-prepped GRE");
  76. QString seriesUID = database.seriesForFile(filePath);
  77. QString seriesDescription = database.descriptionForSeries(seriesUID);
  78. if (seriesDescription != knownSeriesDescription)
  79. {
  80. std::cerr << "ctkDICOMDatabase: database should return series description of '"
  81. << knownSeriesDescription.toStdString()
  82. << "', instead returned '" << seriesDescription.toStdString()
  83. << "'\n\tinstanceUID = "
  84. << instanceUID.toStdString()
  85. << "\n\tseriesUID = "
  86. << seriesUID.toStdString()
  87. << std::endl;
  88. return EXIT_FAILURE;
  89. }
  90. // get the study and patient uids
  91. QString patientUID, studyUID;
  92. studyUID = database.studyForSeries(seriesUID);
  93. patientUID = database.patientForStudy(studyUID);
  94. // check for empty study description
  95. QString studyDescription = database.descriptionForStudy(studyUID);
  96. if (!studyDescription.isEmpty())
  97. {
  98. std::cerr << "ctkDICOMDatabase: database should return empty study"
  99. << " description for studyUID of "
  100. << studyUID.toStdString() << ", instead returned '"
  101. << studyDescription.toStdString() << "'"
  102. << std::endl;
  103. return EXIT_FAILURE;
  104. }
  105. // check for known patient name
  106. QString knownPatientName("Facial Expression");
  107. QString patientName = database.nameForPatient(patientUID);
  108. if (patientName != knownPatientName)
  109. {
  110. std::cerr << "ctkDICOMDatabase: database should return known patient name '"
  111. << knownPatientName.toStdString()
  112. << "' for patient UID of "
  113. << patientUID.toStdString() << ", instead returned '"
  114. << patientName.toStdString() << "'"
  115. << std::endl;
  116. return EXIT_FAILURE;
  117. }
  118. database.closeDatabase();
  119. std::cerr << "Database is in " << databaseDirectory.path().toStdString() << std::endl;
  120. return EXIT_SUCCESS;
  121. }