ctkDICOMIndexer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.commontk.org/LICENSE
  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 <QSqlQuery>
  16. #include <QSqlRecord>
  17. #include <QVariant>
  18. #include <QDate>
  19. #include <QStringList>
  20. #include <QSet>
  21. #include <QFile>
  22. #include <QDirIterator>
  23. #include <QFileInfo>
  24. #include <QDebug>
  25. #include <QPixmap>
  26. // ctkDICOM includes
  27. #include "ctkDICOMIndexer.h"
  28. #include "ctkDICOMAbstractThumbnailGenerator.h"
  29. // DCMTK includes
  30. #ifndef WIN32
  31. #define HAVE_CONFIG_H
  32. #endif
  33. #include <dcmtk/dcmdata/dcfilefo.h>
  34. #include <dcmtk/dcmdata/dcfilefo.h>
  35. #include <dcmtk/dcmdata/dcdeftag.h>
  36. #include <dcmtk/dcmdata/dcdatset.h>
  37. #include <dcmtk/ofstd/ofcond.h>
  38. #include <dcmtk/ofstd/ofstring.h>
  39. #include <dcmtk/ofstd/ofstd.h> /* for class OFStandard */
  40. #include <dcmtk/dcmdata/dcddirif.h> /* for class DicomDirInterface */
  41. #include "dcmimage.h"
  42. #define MITK_ERROR std::cout
  43. #define MITK_INFO std::cout
  44. //------------------------------------------------------------------------------
  45. class ctkDICOMIndexerPrivate
  46. {
  47. public:
  48. ctkDICOMIndexerPrivate();
  49. ~ctkDICOMIndexerPrivate();
  50. ctkDICOMAbstractThumbnailGenerator* thumbnailGenerator;
  51. };
  52. //------------------------------------------------------------------------------
  53. // ctkDICOMIndexerPrivate methods
  54. //------------------------------------------------------------------------------
  55. ctkDICOMIndexerPrivate::ctkDICOMIndexerPrivate()
  56. {
  57. this->thumbnailGenerator = NULL;
  58. }
  59. //------------------------------------------------------------------------------
  60. ctkDICOMIndexerPrivate::~ctkDICOMIndexerPrivate()
  61. {
  62. }
  63. //------------------------------------------------------------------------------
  64. //------------------------------------------------------------------------------
  65. // ctkDICOMIndexer methods
  66. //------------------------------------------------------------------------------
  67. ctkDICOMIndexer::ctkDICOMIndexer():d_ptr(new ctkDICOMIndexerPrivate)
  68. {
  69. }
  70. //------------------------------------------------------------------------------
  71. ctkDICOMIndexer::~ctkDICOMIndexer()
  72. {
  73. }
  74. //------------------------------------------------------------------------------
  75. void ctkDICOMIndexer::addDirectory(ctkDICOMDatabase& database, const QString& directoryName,const QString& destinationDirectoryName, bool createHierarchy, bool createThumbnails)
  76. {
  77. Q_D(ctkDICOMIndexer);
  78. QSqlDatabase db = database.database();
  79. const std::string src_directory(directoryName.toStdString());
  80. OFList<OFString> originalDcmtkFileNames;
  81. OFList<OFString> dcmtkFileNames;
  82. OFStandard::searchDirectoryRecursively( src_directory.c_str(), originalDcmtkFileNames, "", "");
  83. // hack to reverse list of filenames (not neccessary when image loading works correctly)
  84. for ( OFListIterator(OFString) iter = originalDcmtkFileNames.begin(); iter != originalDcmtkFileNames.end(); ++iter )
  85. {
  86. dcmtkFileNames.push_front( *iter );
  87. }
  88. DcmFileFormat fileformat;
  89. DcmDataset *dataset;
  90. OFListIterator(OFString) iter = dcmtkFileNames.begin();
  91. OFListIterator(OFString) last = dcmtkFileNames.end();
  92. if(iter == last) return;
  93. QSqlQuery query(database.database());
  94. /// these are for optimizing the import of image sequences
  95. /// since most information are identical for all slices
  96. OFString lastPatientID = "";
  97. OFString lastPatientsName = "";
  98. OFString lastPatientsBirthDate = "";
  99. OFString lastStudyInstanceUID = "";
  100. OFString lastSeriesInstanceUID = "";
  101. int lastPatientUID = -1;
  102. /* iterate over all input filenames */
  103. while (iter != last)
  104. {
  105. std::string filename((*iter).c_str());
  106. QString qfilename(filename.c_str());
  107. /// first we check if the file is already in the database
  108. QSqlQuery fileExists(database.database());
  109. fileExists.prepare("SELECT InsertTimestamp FROM Images WHERE Filename == ?");
  110. fileExists.bindValue(0,qfilename);
  111. fileExists.exec();
  112. if (
  113. fileExists.next() &&
  114. QFileInfo(qfilename).lastModified() < QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate)
  115. )
  116. {
  117. MITK_INFO << "File " << filename << " already added.";
  118. continue;
  119. }
  120. MITK_INFO << filename << "\n";
  121. OFCondition status = fileformat.loadFile(filename.c_str());
  122. ++iter;
  123. dataset = fileformat.getDataset();
  124. if (!status.good())
  125. {
  126. MITK_ERROR << "Could not load " << filename << "\nDCMTK says: " << status.text();
  127. continue;
  128. }
  129. OFString patientsName, patientID, patientsBirthDate, patientsBirthTime, patientsSex,
  130. patientComments, patientsAge;
  131. OFString studyInstanceUID, studyID, studyDate, studyTime,
  132. accessionNumber, modalitiesInStudy, institutionName, performingPhysiciansName, referringPhysician, studyDescription;
  133. OFString seriesInstanceUID, seriesDate, seriesTime,
  134. seriesDescription, bodyPartExamined, frameOfReferenceUID,
  135. contrastAgent, scanningSequence;
  136. OFString instanceNumber, sopInstanceUID ;
  137. Sint32 seriesNumber = 0, acquisitionNumber = 0, echoNumber = 0, temporalPosition = 0;
  138. //The patient UID is a unique number within the database, generated by the sqlite autoincrement
  139. int patientUID = -1;
  140. //If the following fields can not be evaluated, cancel evaluation of the DICOM file
  141. if (!dataset->findAndGetOFString(DCM_PatientsName, patientsName).good())
  142. {
  143. MITK_ERROR << "Could not read DCM_PatientsName from " << filename;
  144. continue;
  145. }
  146. if (!dataset->findAndGetOFString(DCM_StudyInstanceUID, studyInstanceUID).good())
  147. {
  148. MITK_ERROR << "Could not read DCM_StudyInstanceUID from " << filename;
  149. continue;
  150. }
  151. if (!dataset->findAndGetOFString(DCM_SeriesInstanceUID, seriesInstanceUID).good())
  152. {
  153. MITK_ERROR << "Could not read DCM_SeriesInstanceUID from " << filename;
  154. continue;
  155. }
  156. if (!dataset->findAndGetOFString(DCM_SOPInstanceUID, sopInstanceUID).good())
  157. {
  158. MITK_ERROR << "Could not read DCM_SOPInstanceUID from " << filename;
  159. continue;
  160. }
  161. if (!dataset->findAndGetOFString(DCM_InstanceNumber, instanceNumber).good())
  162. {
  163. MITK_ERROR << "Could not read DCM_InstanceNumber from " << filename;
  164. continue;
  165. }
  166. dataset->findAndGetOFString(DCM_PatientID, patientID);
  167. dataset->findAndGetOFString(DCM_PatientsBirthDate, patientsBirthDate);
  168. dataset->findAndGetOFString(DCM_PatientsBirthTime, patientsBirthTime);
  169. dataset->findAndGetOFString(DCM_PatientsSex, patientsSex);
  170. dataset->findAndGetOFString(DCM_PatientsAge, patientsAge);
  171. dataset->findAndGetOFString(DCM_PatientComments, patientComments);
  172. dataset->findAndGetOFString(DCM_StudyID, studyID);
  173. dataset->findAndGetOFString(DCM_StudyDate, studyDate);
  174. dataset->findAndGetOFString(DCM_StudyTime, studyTime);
  175. dataset->findAndGetOFString(DCM_AccessionNumber, accessionNumber);
  176. dataset->findAndGetOFString(DCM_ModalitiesInStudy, modalitiesInStudy);
  177. dataset->findAndGetOFString(DCM_InstitutionName, institutionName);
  178. dataset->findAndGetOFString(DCM_PerformingPhysiciansName, performingPhysiciansName);
  179. dataset->findAndGetOFString(DCM_ReferringPhysiciansName, referringPhysician);
  180. dataset->findAndGetOFString(DCM_StudyDescription, studyDescription);
  181. dataset->findAndGetOFString(DCM_SeriesDate, seriesDate);
  182. dataset->findAndGetOFString(DCM_SeriesTime, seriesTime);
  183. dataset->findAndGetOFString(DCM_SeriesDescription, seriesDescription);
  184. dataset->findAndGetOFString(DCM_BodyPartExamined, bodyPartExamined);
  185. dataset->findAndGetOFString(DCM_FrameOfReferenceUID, frameOfReferenceUID);
  186. dataset->findAndGetOFString(DCM_ContrastBolusAgent, contrastAgent);
  187. dataset->findAndGetOFString(DCM_ScanningSequence, scanningSequence);
  188. dataset->findAndGetSint32(DCM_SeriesNumber, seriesNumber);
  189. dataset->findAndGetSint32(DCM_AcquisitionNumber, acquisitionNumber);
  190. dataset->findAndGetSint32(DCM_EchoNumbers, echoNumber);
  191. dataset->findAndGetSint32(DCM_TemporalPositionIdentifier, temporalPosition);
  192. MITK_INFO << "Adding new items to database:";
  193. MITK_INFO << "studyID: " << studyID;
  194. MITK_INFO << "seriesInstanceUID: " << seriesInstanceUID;
  195. MITK_INFO << "Patient's Name: " << patientsName;
  196. //-----------------------
  197. //Add Patient to Database
  198. //-----------------------
  199. //Speed up: Check if patient is the same as in last file; very probable, as all images belonging to a study have the same patient
  200. bool patientExists = false;
  201. if(lastPatientID.compare(patientID) || lastPatientsBirthDate.compare(patientsBirthDate) || lastPatientsName.compare(patientsName))
  202. {
  203. //Check if patient is already present in the db
  204. QSqlQuery check_exists_query(database.database());
  205. std::stringstream check_exists_query_string;
  206. check_exists_query_string << "SELECT * FROM Patients WHERE PatientID = '" << patientID << "'";
  207. check_exists_query.exec(check_exists_query_string.str().c_str());
  208. /// we check only patients with the same PatientID
  209. /// PatientID is not unique in DICOM, so we also compare Name and BirthDate
  210. /// and assume this is sufficient
  211. while (check_exists_query.next())
  212. {
  213. if (
  214. check_exists_query.record().value("PatientsName").toString() == patientsName.c_str() &&
  215. check_exists_query.record().value("PatientsBirthDate").toString() == patientsBirthDate.c_str()
  216. )
  217. {
  218. /// found it
  219. patientUID = check_exists_query.value(check_exists_query.record().indexOf("UID")).toInt();
  220. break;
  221. }
  222. }
  223. if(!patientExists)
  224. {
  225. std::stringstream query_string;
  226. query_string << "INSERT INTO Patients VALUES( NULL,'"
  227. << patientsName << "','"
  228. << patientID << "','"
  229. << patientsBirthDate << "','"
  230. << patientsBirthTime << "','"
  231. << patientsSex << "','"
  232. << patientsAge << "','"
  233. << patientComments << "')";
  234. query.exec(query_string.str().c_str());
  235. patientUID = query.lastInsertId().toInt();
  236. MITK_INFO << "New patient inserted: " << patientUID << "\n";
  237. }
  238. }
  239. else
  240. {
  241. patientUID = lastPatientUID;
  242. }
  243. /// keep this for the next image
  244. lastPatientUID = patientUID;
  245. lastPatientID = patientID;
  246. lastPatientsBirthDate = patientsBirthDate;
  247. lastPatientsName = patientsName;
  248. //---------------------
  249. //Add Study to Database
  250. //---------------------
  251. if(lastStudyInstanceUID.compare(studyInstanceUID))
  252. {
  253. QSqlQuery check_exists_query(database.database());
  254. std::stringstream check_exists_query_string;
  255. check_exists_query_string << "SELECT * FROM Studies WHERE StudyInstanceUID = '" << studyInstanceUID << "'";
  256. check_exists_query.exec(check_exists_query_string.str().c_str());
  257. if(!check_exists_query.next())
  258. {
  259. std::stringstream query_string;
  260. query_string << "INSERT INTO Studies VALUES('"
  261. << studyInstanceUID << "','"
  262. << patientUID << "','"
  263. << studyID << "','"
  264. << QDate::fromString(studyDate.c_str(), "yyyyMMdd").toString("yyyy-MM-dd").toStdString() << "','"
  265. << studyTime << "','"
  266. << accessionNumber << "','"
  267. << modalitiesInStudy << "','"
  268. << institutionName << "','"
  269. << referringPhysician << "','"
  270. << performingPhysiciansName << "','"
  271. << studyDescription << "')";
  272. query.exec(query_string.str().c_str());
  273. }
  274. }
  275. lastStudyInstanceUID = studyInstanceUID;
  276. //----------------------
  277. //Add Series to Database
  278. //----------------------
  279. if(lastSeriesInstanceUID.compare(seriesInstanceUID))
  280. {
  281. QSqlQuery check_exists_query(database.database());
  282. std::stringstream check_exists_query_string;
  283. check_exists_query_string << "SELECT * FROM Series WHERE SeriesInstanceUID = '" << seriesInstanceUID << "'";
  284. check_exists_query.exec(check_exists_query_string.str().c_str());
  285. if(!check_exists_query.next())
  286. {
  287. std::stringstream query_string;
  288. query_string << "INSERT INTO Series VALUES('"
  289. << seriesInstanceUID << "','"
  290. << studyInstanceUID << "','"
  291. << static_cast<int>(seriesNumber) << "','"
  292. << QDate::fromString(seriesDate.c_str(), "yyyyMMdd").toString("yyyy-MM-dd").toStdString() << "','"
  293. << seriesTime << "','"
  294. << seriesDescription << "','"
  295. << bodyPartExamined << "','"
  296. << frameOfReferenceUID << "','"
  297. << static_cast<int>(acquisitionNumber) << "','"
  298. << contrastAgent << "','"
  299. << scanningSequence << "','"
  300. << static_cast<int>(echoNumber) << "','"
  301. << static_cast<int>(temporalPosition) << "')";
  302. query.exec(query_string.str().c_str());
  303. }
  304. }
  305. lastSeriesInstanceUID = seriesInstanceUID;
  306. QString studySeriesDirectory = QString(studyInstanceUID.c_str()) + "/" + seriesInstanceUID.c_str();
  307. //----------------------------------
  308. //Move file to destination directory
  309. //----------------------------------
  310. if (!destinationDirectoryName.isEmpty())
  311. {
  312. QFile currentFile( qfilename );
  313. QDir destinationDir(destinationDirectoryName + "/dicom");
  314. QString destFileName = sopInstanceUID.c_str();
  315. if (createHierarchy)
  316. {
  317. destinationDir.mkpath(studySeriesDirectory);
  318. destFileName.prepend( destinationDir.absolutePath() + "/" + studySeriesDirectory + "/" );
  319. }
  320. currentFile.copy(destFileName);
  321. qfilename = destFileName;
  322. }
  323. if (createThumbnails)
  324. {
  325. if(d->thumbnailGenerator){
  326. QString thumbnailBaseDir = database.databaseDirectory() + "/thumbs/";
  327. QString thumbnailFilename = thumbnailBaseDir + "/" + database.pathForDataset(dataset) + ".png";
  328. QFileInfo thumbnailInfo(thumbnailFilename);
  329. if ( ! ( thumbnailInfo.exists() && thumbnailInfo.lastModified() < QFileInfo(qfilename).lastModified() ) )
  330. {
  331. QDir(thumbnailBaseDir).mkpath(studySeriesDirectory);
  332. DicomImage dcmtkImage(qfilename.toAscii());
  333. d->thumbnailGenerator->generateThumbnail(&dcmtkImage, thumbnailFilename);
  334. }
  335. }
  336. }
  337. // */
  338. //------------------------
  339. //Add Filename to Database
  340. //------------------------
  341. // std::stringstream relativeFilePath;
  342. // relativeFilePath << seriesInstanceUID.c_str() << "/" << currentFilePath.getFileName();
  343. QSqlQuery check_exists_query(database.database());
  344. std::stringstream check_exists_query_string;
  345. // check_exists_query_string << "SELECT * FROM Images WHERE Filename = '" << relativeFilePath.str() << "'";
  346. check_exists_query_string << "SELECT * FROM Images WHERE SOPInstanceUID = '" << sopInstanceUID << "'";
  347. check_exists_query.exec(check_exists_query_string.str().c_str());
  348. if(!check_exists_query.next())
  349. {
  350. std::stringstream query_string;
  351. //To save absolute path: destDirectoryPath.str()
  352. query_string << "INSERT INTO Images VALUES('"
  353. << sopInstanceUID << "','" << qfilename.toStdString() << "','" << seriesInstanceUID << "','" << QDateTime::currentDateTime().toString(Qt::ISODate).toStdString() << "')";
  354. query.exec(query_string.str().c_str());
  355. }
  356. }
  357. // db.commit();
  358. // db.close();
  359. }
  360. //------------------------------------------------------------------------------
  361. void ctkDICOMIndexer::refreshDatabase(ctkDICOMDatabase& database, const QString& directoryName)
  362. {
  363. /// get all filenames from the database
  364. QSqlQuery allFilesQuery(database.database());
  365. QStringList databaseFileNames;
  366. QStringList filesToRemove;
  367. allFilesQuery.exec("SELECT Filename from Images;");
  368. while (allFilesQuery.next())
  369. {
  370. QString fileName = allFilesQuery.value(0).toString();
  371. databaseFileNames.append(fileName);
  372. if (! QFile::exists(fileName) )
  373. {
  374. filesToRemove.append(fileName);
  375. }
  376. }
  377. QSet<QString> filesytemFiles;
  378. QDirIterator dirIt(directoryName);
  379. while (dirIt.hasNext())
  380. {
  381. filesytemFiles.insert(dirIt.next());
  382. }
  383. }
  384. //------------------------------------------------------------------------------
  385. void ctkDICOMIndexer::setThumbnailGenerator(ctkDICOMAbstractThumbnailGenerator *generator){
  386. Q_D(ctkDICOMIndexer);
  387. d->thumbnailGenerator = generator;
  388. }
  389. //------------------------------------------------------------------------------
  390. ctkDICOMAbstractThumbnailGenerator* ctkDICOMIndexer::thumbnailGenerator(){
  391. Q_D(ctkDICOMIndexer);
  392. return d->thumbnailGenerator;
  393. }