ctkDICOMImage.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.apache.org/licenses/LICENSE-2.0.txt
  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 <dcmtk/dcmimgle/dcmimage.h>
  23. #include <dcmtk/ofstd/ofbmanip.h>
  24. static ctkLogger logger ( "org.commontk.dicom.DICOMImage" );
  25. struct Node;
  26. //------------------------------------------------------------------------------
  27. class ctkDICOMImagePrivate
  28. {
  29. Q_DECLARE_PUBLIC(ctkDICOMImage);
  30. public:
  31. ctkDICOMImagePrivate(ctkDICOMImage&);
  32. ::DicomImage* DicomImage;
  33. protected:
  34. ctkDICOMImage* const q_ptr;
  35. private:
  36. Q_DISABLE_COPY(ctkDICOMImagePrivate);
  37. };
  38. //------------------------------------------------------------------------------
  39. ctkDICOMImagePrivate::ctkDICOMImagePrivate(ctkDICOMImage& o):q_ptr(&o)
  40. {
  41. }
  42. //------------------------------------------------------------------------------
  43. ctkDICOMImage::ctkDICOMImage(DicomImage* dicomImage, QObject* parentValue)
  44. : d_ptr(new ctkDICOMImagePrivate(*this))
  45. {
  46. Q_UNUSED(parentValue);
  47. Q_D(ctkDICOMImage);
  48. d->DicomImage = dicomImage;
  49. if (d->DicomImage)
  50. // Select first window defined in image. If none, compute min/max window as best guess.
  51. // Only relevant for monochrome.
  52. if (d->DicomImage->isMonochrome())
  53. {
  54. if (d->DicomImage->getWindowCount() > 0)
  55. {
  56. d->DicomImage->setWindow(0);
  57. }
  58. else
  59. {
  60. d->DicomImage->setMinMaxWindow(OFTrue /* ignore extreme values */);
  61. }
  62. }
  63. }
  64. //------------------------------------------------------------------------------
  65. ctkDICOMImage::~ctkDICOMImage()
  66. {
  67. }
  68. //------------------------------------------------------------------------------
  69. unsigned long ctkDICOMImage::frameCount() const
  70. {
  71. Q_D(const ctkDICOMImage);
  72. if (d->DicomImage)
  73. {
  74. return d->DicomImage->getFrameCount();
  75. }
  76. return 0;
  77. }
  78. //------------------------------------------------------------------------------
  79. DicomImage* ctkDICOMImage::dicomImage() const
  80. {
  81. Q_D(const ctkDICOMImage);
  82. return d->DicomImage;
  83. }
  84. //------------------------------------------------------------------------------
  85. QImage ctkDICOMImage::frame(int frame) const
  86. {
  87. Q_D(const ctkDICOMImage);
  88. // this way of converting the dicom image to a qpixmap was adopted from some code from
  89. // the DCMTK forum, posted by Joerg Riesmayer, see http://forum.dcmtk.org/viewtopic.php?t=120
  90. QImage image;
  91. if ((d->DicomImage != NULL) && (d->DicomImage->getStatus() == EIS_Normal))
  92. {
  93. /* get image extension */
  94. const unsigned long width = d->DicomImage->getWidth();
  95. const unsigned long height = d->DicomImage->getHeight();
  96. QString header = QString("P5 %1 %2 255\n").arg(width).arg(height);
  97. const unsigned long offset = header.length();
  98. const unsigned long length = width * height + offset;
  99. /* create output buffer for DicomImage class */
  100. QByteArray buffer;
  101. buffer.append(header);
  102. buffer.resize(length);
  103. /* copy PGM header to buffer */
  104. if (d->DicomImage->getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, frame))
  105. {
  106. if (!image.loadFromData( buffer ))
  107. {
  108. logger.error("QImage couldn't created");
  109. }
  110. }
  111. }
  112. return image;
  113. }