ctkDICOMItemView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include <iostream>
  15. // DCMTK includes
  16. #include <dcmtk/dcmimgle/dcmimage.h>
  17. #include <dcmtk/dcmimage/diregist.h> /* Include color image support */
  18. // CTK includes
  19. #include "ctkLogger.h"
  20. #include "ctkQImageView.h"
  21. // ctkDICOMCore includes
  22. #include "ctkDICOMFilterProxyModel.h"
  23. #include "ctkDICOMModel.h"
  24. // ctkDICOMWidgets includex
  25. #include "ctkDICOMItemView.h"
  26. // Qt includes
  27. #include <QDebug>
  28. #include <QDir>
  29. #include <QFile>
  30. #include <QHBoxLayout>
  31. #include <QKeyEvent>
  32. #include <QLabel>
  33. #include <QMouseEvent>
  34. #include <QPainter>
  35. #include <QResizeEvent>
  36. static ctkLogger logger("org.commontk.DICOM.Widgets.ctkDICOMItemView");
  37. //--------------------------------------------------------------------------
  38. class ctkDICOMItemViewPrivate
  39. {
  40. Q_DECLARE_PUBLIC( ctkDICOMItemView );
  41. public:
  42. ctkDICOMItemViewPrivate( ctkDICOMItemView& object );
  43. QString DatabaseDirectory;
  44. QModelIndex CurrentImageIndex;
  45. QPoint OldMousePos;
  46. double DicomIntensityLevel;
  47. double DicomIntensityWindow;
  48. bool AutoWindowLevel;
  49. void init();
  50. void setImage(const QModelIndex& imageIndex, bool defaultIntensity = true);
  51. void onPatientModelSelected(const QModelIndex& index);
  52. void onStudyModelSelected(const QModelIndex& index);
  53. void onSeriesModelSelected(const QModelIndex& index);
  54. void onImageModelSelected(const QModelIndex& index);
  55. protected:
  56. ctkDICOMItemView* const q_ptr;
  57. private:
  58. Q_DISABLE_COPY( ctkDICOMItemViewPrivate );
  59. };
  60. //--------------------------------------------------------------------------
  61. ctkDICOMItemViewPrivate::ctkDICOMItemViewPrivate(
  62. ctkDICOMItemView& object )
  63. : q_ptr( & object )
  64. {
  65. }
  66. //--------------------------------------------------------------------------
  67. void ctkDICOMItemViewPrivate::init()
  68. {
  69. Q_Q( ctkDICOMItemView );
  70. q->setMouseTracking(true);
  71. this->DicomIntensityLevel = 0;
  72. this->DicomIntensityWindow = 0;
  73. this->AutoWindowLevel = true;
  74. /*
  75. this->Window->setParent(q);
  76. QHBoxLayout* layout = new QHBoxLayout(q);
  77. layout->addWidget(this->Window);
  78. layout->setContentsMargins(0,0,0,0);
  79. q->setLayout(layout);
  80. */
  81. }
  82. // -------------------------------------------------------------------------
  83. void ctkDICOMItemViewPrivate::setImage(const QModelIndex &imageIndex, bool defaultIntensity){
  84. Q_Q(ctkDICOMItemView);
  85. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(imageIndex.model()));
  86. if(model){
  87. QModelIndex seriesIndex = imageIndex.parent();
  88. QModelIndex studyIndex = seriesIndex.parent();
  89. QString dicomPath = this->DatabaseDirectory;
  90. dicomPath.append("/dicom/").append(model->data(studyIndex ,ctkDICOMModel::UIDRole).toString());
  91. dicomPath.append("/").append(model->data(seriesIndex ,ctkDICOMModel::UIDRole).toString());
  92. dicomPath.append("/").append(model->data(imageIndex ,ctkDICOMModel::UIDRole).toString());
  93. if (QFile(dicomPath).exists()){
  94. DicomImage dcmImage( QDir::toNativeSeparators(dicomPath).toStdString().c_str() );
  95. q->clearImages();
  96. q->addImage(dcmImage, defaultIntensity);
  97. this->CurrentImageIndex = imageIndex;
  98. q->emitImageDisplayedSignal(imageIndex.row(), model->rowCount(seriesIndex));
  99. }else{
  100. q->clearImages();
  101. }
  102. }
  103. }
  104. // -------------------------------------------------------------------------
  105. void ctkDICOMItemViewPrivate::onPatientModelSelected(const QModelIndex &index){
  106. Q_Q(ctkDICOMItemView);
  107. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  108. if(model){
  109. QModelIndex patientIndex = index;
  110. model->fetchMore(patientIndex);
  111. QModelIndex studyIndex = patientIndex.child(0,0);
  112. model->fetchMore(studyIndex);
  113. QModelIndex seriesIndex = studyIndex.child(0,0);
  114. model->fetchMore(seriesIndex);
  115. int imageCount = model->rowCount(seriesIndex);
  116. QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
  117. this->setImage(imageIndex);
  118. }else{
  119. q->clearImages();
  120. }
  121. }
  122. // -------------------------------------------------------------------------
  123. void ctkDICOMItemViewPrivate::onStudyModelSelected(const QModelIndex &index){
  124. Q_Q(ctkDICOMItemView);
  125. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  126. if(model){
  127. QModelIndex studyIndex = index;
  128. model->fetchMore(studyIndex);
  129. QModelIndex seriesIndex = studyIndex.child(0,0);
  130. model->fetchMore(seriesIndex);
  131. int imageCount = model->rowCount(seriesIndex);
  132. QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
  133. this->setImage(imageIndex);
  134. }else{
  135. q->clearImages();
  136. }
  137. }
  138. // -------------------------------------------------------------------------
  139. void ctkDICOMItemViewPrivate::onSeriesModelSelected(const QModelIndex &index){
  140. Q_Q(ctkDICOMItemView);
  141. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  142. if(model){
  143. QModelIndex seriesIndex = index;
  144. model->fetchMore(seriesIndex);
  145. int imageCount = model->rowCount(seriesIndex);
  146. QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
  147. this->setImage(imageIndex);
  148. }else{
  149. q->clearImages();
  150. }
  151. }
  152. // -------------------------------------------------------------------------
  153. void ctkDICOMItemViewPrivate::onImageModelSelected(const QModelIndex &index){
  154. Q_Q(ctkDICOMItemView);
  155. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  156. if(model){
  157. QModelIndex imageIndex = index;
  158. if(index.parent() == this->CurrentImageIndex.parent()){
  159. this->setImage(imageIndex, false);
  160. }else{
  161. this->setImage(imageIndex, true);
  162. }
  163. }else{
  164. q->clearImages();
  165. }
  166. }
  167. // -------------------------------------------------------------------------
  168. ctkDICOMItemView::ctkDICOMItemView( QWidget* _parent )
  169. : Superclass( _parent ),
  170. d_ptr( new ctkDICOMItemViewPrivate( *this ) )
  171. {
  172. Q_D( ctkDICOMItemView );
  173. d->init();
  174. }
  175. // -------------------------------------------------------------------------
  176. ctkDICOMItemView::ctkDICOMItemView(
  177. ctkDICOMItemViewPrivate& pvt,
  178. QWidget* _parent)
  179. : Superclass(_parent), d_ptr(&pvt)
  180. {
  181. Q_D(ctkDICOMItemView);
  182. d->init();
  183. }
  184. // -------------------------------------------------------------------------
  185. ctkDICOMItemView::~ctkDICOMItemView()
  186. {
  187. }
  188. // -------------------------------------------------------------------------
  189. void ctkDICOMItemView::setDatabaseDirectory(const QString &directory){
  190. Q_D(ctkDICOMItemView);
  191. d->DatabaseDirectory = directory;
  192. }
  193. // -------------------------------------------------------------------------
  194. QModelIndex ctkDICOMItemView::currentImageIndex(){
  195. Q_D(ctkDICOMItemView);
  196. return d->CurrentImageIndex;
  197. }
  198. // -------------------------------------------------------------------------
  199. void ctkDICOMItemView::addImage( const QImage & image )
  200. {
  201. Superclass::addImage( image );
  202. }
  203. // -------------------------------------------------------------------------
  204. void ctkDICOMItemView::addImage( DicomImage & dcmImage, bool defaultIntensity )
  205. {
  206. Q_D(ctkDICOMItemView);
  207. QImage image;
  208. // Check whether we have a valid image
  209. EI_Status result = dcmImage.getStatus();
  210. if (result != EIS_Normal)
  211. {
  212. logger.error(QString("Rendering of DICOM image failed for thumbnail failed: ") + DicomImage::getString(result));
  213. return;
  214. }
  215. // Select first window defined in image. If none, compute min/max window as best guess.
  216. // Only relevant for monochrome
  217. if (d->AutoWindowLevel)
  218. {
  219. if (dcmImage.isMonochrome())
  220. {
  221. if (defaultIntensity && dcmImage.getWindowCount() > 0)
  222. {
  223. dcmImage.setWindow(0);
  224. }
  225. else
  226. {
  227. dcmImage.setMinMaxWindow(OFTrue /* ignore extreme values */);
  228. dcmImage.getWindow(d->DicomIntensityLevel, d->DicomIntensityWindow);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. dcmImage.setWindow(d->DicomIntensityLevel, d->DicomIntensityWindow);
  235. }
  236. /* get image extension and prepare image header */
  237. const unsigned long width = dcmImage.getWidth();
  238. const unsigned long height = dcmImage.getHeight();
  239. unsigned long offset = 0;
  240. unsigned long length = 0;
  241. QString header;
  242. if (dcmImage.isMonochrome())
  243. {
  244. // write PGM header (binary monochrome image format)
  245. header = QString("P5 %1 %2 255\n").arg(width).arg(height);
  246. offset = header.length();
  247. length = width * height + offset;
  248. }
  249. else
  250. {
  251. // write PPM header (binary color image format)
  252. header = QString("P6 %1 %2 255\n").arg(width).arg(height);
  253. offset = header.length();
  254. length = width * height * 3 /* RGB */ + offset;
  255. }
  256. /* create output buffer for DicomImage class */
  257. QByteArray buffer;
  258. /* copy header to output buffer and resize it for pixel data */
  259. buffer.append(header);
  260. buffer.resize(length);
  261. /* render pixel data to buffer */
  262. if (dcmImage.getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, 0))
  263. {
  264. if (!image.loadFromData( buffer ))
  265. {
  266. logger.error("QImage couldn't created");
  267. }
  268. }
  269. this->addImage(image);
  270. }
  271. // -------------------------------------------------------------------------
  272. void ctkDICOMItemView::update( bool zoomChanged,
  273. bool sizeChanged )
  274. {
  275. Superclass::update( zoomChanged, sizeChanged );
  276. }
  277. // -------------------------------------------------------------------------
  278. void ctkDICOMItemView::mousePressEvent(QMouseEvent* event){
  279. Q_D(ctkDICOMItemView);
  280. event->accept();
  281. d->OldMousePos = event->pos();
  282. }
  283. // -------------------------------------------------------------------------
  284. void ctkDICOMItemView::mouseMoveEvent(QMouseEvent* event){
  285. Q_D(ctkDICOMItemView);
  286. if(event->buttons() == Qt::RightButton){
  287. event->accept();
  288. QPoint nowPos = event->pos();
  289. if(nowPos.y() > d->OldMousePos.y()){
  290. emit requestNextImage();
  291. d->OldMousePos = event->pos();
  292. }else if(nowPos.y() < d->OldMousePos.y()){
  293. emit requestPreviousImage();
  294. d->OldMousePos = event->pos();
  295. }
  296. }else if(event->buttons() == Qt::MidButton){
  297. event->accept();
  298. QPoint nowPos = event->pos();
  299. this->setZoom(this->zoom() - (nowPos.y()-d->OldMousePos.y())/100.0);
  300. d->OldMousePos = event->pos();
  301. }else if(event->buttons() == Qt::LeftButton){
  302. event->accept();
  303. QPoint nowPos = event->pos();
  304. d->DicomIntensityWindow += (5*(nowPos.x()-d->OldMousePos.x()));
  305. d->DicomIntensityLevel -= (5*(nowPos.y()-d->OldMousePos.y()));
  306. d->AutoWindowLevel = false;
  307. d->setImage(d->CurrentImageIndex, false);
  308. d->OldMousePos = event->pos();
  309. }
  310. Superclass::mouseMoveEvent(event);
  311. }
  312. // -------------------------------------------------------------------------
  313. void ctkDICOMItemView::onModelSelected(const QModelIndex &index){
  314. Q_D(ctkDICOMItemView);
  315. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(index.model()));
  316. if(model){
  317. QModelIndex index0 = index.sibling(index.row(), 0);
  318. if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::PatientType) ){
  319. d->onPatientModelSelected(index0);
  320. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::StudyType) ){
  321. d->onStudyModelSelected(index0);
  322. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::SeriesType) ){
  323. d->onSeriesModelSelected(index0);
  324. }else if ( model->data(index0,ctkDICOMModel::TypeRole) == static_cast<int>(ctkDICOMModel::ImageType) ){
  325. d->onImageModelSelected(index0);
  326. }
  327. }
  328. }
  329. // -------------------------------------------------------------------------
  330. void ctkDICOMItemView::displayImage(int imageIndex){
  331. Q_D(ctkDICOMItemView);
  332. if(d->CurrentImageIndex.isValid())
  333. {
  334. QModelIndex seriesIndex = d->CurrentImageIndex.parent();
  335. ctkDICOMModel* model = const_cast<ctkDICOMModel*>(qobject_cast<const ctkDICOMModel*>(seriesIndex.model()));
  336. if(model)
  337. {
  338. if(imageIndex >= 0 && imageIndex < model->rowCount(seriesIndex))
  339. {
  340. this->onModelSelected(model->index(imageIndex, 0, seriesIndex));
  341. }
  342. else
  343. {
  344. logger.debug("out of index");
  345. }
  346. }
  347. }
  348. }
  349. // -------------------------------------------------------------------------
  350. void ctkDICOMItemView::emitImageDisplayedSignal(int imageID, int count){
  351. emit imageDisplayed(imageID, count);
  352. }