ctkDICOMImage.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.commontk.org/LICENSE
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =========================================================================*/
  15. // Qt includes
  16. #include <QDebug>
  17. #include <QString>
  18. // ctkDICOMCore includes
  19. #include "ctkDICOMImage.h"
  20. #include "ctkLogger.h"
  21. // DCMTK includes
  22. #include "dcmimage.h"
  23. #include <ofbmanip.h>
  24. static ctkLogger logger ( "org.commontk.dicom.DICOMImage" );
  25. struct Node;
  26. //------------------------------------------------------------------------------
  27. class ctkDICOMImagePrivate
  28. {
  29. Q_DECLARE_PUBLIC(ctkDICOMImage);
  30. protected:
  31. ctkDICOMImage* const q_ptr;
  32. public:
  33. ctkDICOMImagePrivate(ctkDICOMImage&);
  34. virtual ~ctkDICOMImagePrivate();
  35. ::DicomImage* DicomImage;
  36. };
  37. //------------------------------------------------------------------------------
  38. ctkDICOMImagePrivate::ctkDICOMImagePrivate(ctkDICOMImage& o):q_ptr(&o)
  39. {
  40. }
  41. //------------------------------------------------------------------------------
  42. ctkDICOMImagePrivate::~ctkDICOMImagePrivate()
  43. {
  44. }
  45. //------------------------------------------------------------------------------
  46. ctkDICOMImage::ctkDICOMImage(DicomImage* dicomImage, QObject* parentValue): d_ptr(new ctkDICOMImagePrivate(*this))
  47. {
  48. Q_UNUSED(parentValue);
  49. Q_D(ctkDICOMImage);
  50. d->DicomImage = dicomImage;
  51. // select first window by default
  52. d->DicomImage->setWindow(0);
  53. }
  54. //------------------------------------------------------------------------------
  55. ctkDICOMImage::~ctkDICOMImage()
  56. {
  57. }
  58. unsigned long ctkDICOMImage::frameCount() const
  59. {
  60. Q_D(const ctkDICOMImage);
  61. if (d->DicomImage)
  62. {
  63. return d->DicomImage->getFrameCount();
  64. }
  65. return 0;
  66. }
  67. DicomImage* ctkDICOMImage::getDicomImage() const
  68. {
  69. Q_D(const ctkDICOMImage);
  70. return d->DicomImage;
  71. }
  72. QImage ctkDICOMImage::getImage(int frame) const
  73. {
  74. Q_D(const ctkDICOMImage);
  75. // this way of converting the dicom image to a qpixmap was adopted from some code from
  76. // the DCMTK forum, posted by Joerg Riesmayer, see http://forum.dcmtk.org/viewtopic.php?t=120
  77. QImage image;
  78. if ((d->DicomImage != NULL) && (d->DicomImage->getStatus() == EIS_Normal))
  79. {
  80. /* get image extension */
  81. const unsigned long width = d->DicomImage->getWidth();
  82. const unsigned long height = d->DicomImage->getHeight();
  83. QString header = QString("P5 %1 %2 255\n").arg(width).arg(height);
  84. const unsigned long offset = header.length();
  85. const unsigned long length = width * height + offset;
  86. /* create output buffer for DicomImage class */
  87. QByteArray buffer;
  88. buffer.append(header);
  89. buffer.resize(length);
  90. /* copy PGM header to buffer */
  91. if (d->DicomImage->getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, frame))
  92. {
  93. if (!image.loadFromData( buffer ))
  94. {
  95. logger.error("QImage couldn't created");
  96. }
  97. }
  98. }
  99. return image;
  100. }