ctkDICOMThumbnailGenerator.cpp 4.3 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. // ctkDICOMCore includes
  16. #include "ctkDICOMThumbnailGenerator.h"
  17. #include "ctkLogger.h"
  18. // Qt includes
  19. #include <QImage>
  20. // DCMTK includes
  21. #include "dcmtk/dcmimgle/dcmimage.h"
  22. static ctkLogger logger ( "org.commontk.dicom.DICOMThumbnailGenerator" );
  23. struct Node;
  24. //------------------------------------------------------------------------------
  25. class ctkDICOMThumbnailGeneratorPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkDICOMThumbnailGenerator);
  28. public:
  29. ctkDICOMThumbnailGeneratorPrivate(ctkDICOMThumbnailGenerator&);
  30. virtual ~ctkDICOMThumbnailGeneratorPrivate();
  31. protected:
  32. ctkDICOMThumbnailGenerator* const q_ptr;
  33. private:
  34. Q_DISABLE_COPY( ctkDICOMThumbnailGeneratorPrivate );
  35. };
  36. //------------------------------------------------------------------------------
  37. ctkDICOMThumbnailGeneratorPrivate::ctkDICOMThumbnailGeneratorPrivate(ctkDICOMThumbnailGenerator& o):q_ptr(&o)
  38. {
  39. }
  40. //------------------------------------------------------------------------------
  41. ctkDICOMThumbnailGeneratorPrivate::~ctkDICOMThumbnailGeneratorPrivate()
  42. {
  43. }
  44. //------------------------------------------------------------------------------
  45. ctkDICOMThumbnailGenerator::ctkDICOMThumbnailGenerator(QObject* parentValue)
  46. : d_ptr(new ctkDICOMThumbnailGeneratorPrivate(*this))
  47. {
  48. Q_UNUSED(parentValue);
  49. }
  50. //------------------------------------------------------------------------------
  51. ctkDICOMThumbnailGenerator::~ctkDICOMThumbnailGenerator()
  52. {
  53. }
  54. //------------------------------------------------------------------------------
  55. bool ctkDICOMThumbnailGenerator::generateThumbnail(DicomImage *dcmImage, const QString &path){
  56. QImage image;
  57. // Check whether we have a valid image
  58. EI_Status result = dcmImage->getStatus();
  59. if (result != EIS_Normal)
  60. {
  61. logger.error(QString("Rendering of DICOM image failed for thumbnail failed: ") + DicomImage::getString(result));
  62. return false;
  63. }
  64. // Select first window defined in image. If none, compute min/max window as best guess.
  65. // Only relevant for monochrome.
  66. if (dcmImage->isMonochrome())
  67. {
  68. if (dcmImage->getWindowCount() > 0)
  69. {
  70. dcmImage->setWindow(0);
  71. }
  72. else
  73. {
  74. dcmImage->setMinMaxWindow(OFTrue /* ignore extreme values */);
  75. }
  76. }
  77. /* get image extension and prepare image header */
  78. const unsigned long width = dcmImage->getWidth();
  79. const unsigned long height = dcmImage->getHeight();
  80. unsigned long offset = 0;
  81. unsigned long length = 0;
  82. QString header;
  83. if (dcmImage->isMonochrome())
  84. {
  85. // write PGM header (binary monochrome image format)
  86. header = QString("P5 %1 %2 255\n").arg(width).arg(height);
  87. offset = header.length();
  88. length = width * height + offset;
  89. }
  90. else
  91. {
  92. // write PPM header (binary color image format)
  93. header = QString("P6 %1 %2 255\n").arg(width).arg(height);
  94. offset = header.length();
  95. length = width * height * 3 /* RGB */ + offset;
  96. }
  97. /* create output buffer for DicomImage class */
  98. QByteArray buffer;
  99. /* copy header to output buffer and resize it for pixel data */
  100. buffer.append(header);
  101. buffer.resize(length);
  102. /* render pixel data to buffer */
  103. if (dcmImage->getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, 0))
  104. {
  105. if (!image.loadFromData( buffer ))
  106. {
  107. logger.error("QImage couldn't created");
  108. return false;
  109. }
  110. }
  111. image.scaled(128,128,Qt::KeepAspectRatio).save(path,"PNG");
  112. return true;
  113. }