ctkDICOMThumbnailGenerator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // ctkDICOMCore includes
  16. #include "ctkDICOMThumbnailGenerator.h"
  17. #include "ctkLogger.h"
  18. // Qt includes
  19. #include <QImage>
  20. // DCMTK includes
  21. #include "dcmimage.h"
  22. static ctkLogger logger ( "org.commontk.dicom.DICOMThumbnailGenerator" );
  23. struct Node;
  24. //------------------------------------------------------------------------------
  25. class ctkDICOMThumbnailGeneratorPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkDICOMThumbnailGenerator);
  28. protected:
  29. ctkDICOMThumbnailGenerator* const q_ptr;
  30. public:
  31. ctkDICOMThumbnailGeneratorPrivate(ctkDICOMThumbnailGenerator&);
  32. virtual ~ctkDICOMThumbnailGeneratorPrivate();
  33. };
  34. //------------------------------------------------------------------------------
  35. ctkDICOMThumbnailGeneratorPrivate::ctkDICOMThumbnailGeneratorPrivate(ctkDICOMThumbnailGenerator& o):q_ptr(&o)
  36. {
  37. }
  38. //------------------------------------------------------------------------------
  39. ctkDICOMThumbnailGeneratorPrivate::~ctkDICOMThumbnailGeneratorPrivate()
  40. {
  41. }
  42. //------------------------------------------------------------------------------
  43. ctkDICOMThumbnailGenerator::ctkDICOMThumbnailGenerator(QObject* parentValue)
  44. : d_ptr(new ctkDICOMThumbnailGeneratorPrivate(*this))
  45. {
  46. }
  47. //------------------------------------------------------------------------------
  48. ctkDICOMThumbnailGenerator::~ctkDICOMThumbnailGenerator()
  49. {
  50. }
  51. bool ctkDICOMThumbnailGenerator::generateThumbnail(DicomImage *dcmImage, const QString &path){
  52. QImage image;
  53. if ((dcmImage->getStatus() == EIS_Normal)){
  54. dcmImage->setWindow(0);
  55. /* get image extension */
  56. const unsigned long width = dcmImage->getWidth();
  57. const unsigned long height = dcmImage->getHeight();
  58. QString header = QString("P5 %1 %2 255\n").arg(width).arg(height);
  59. const unsigned long offset = header.length();
  60. const unsigned long length = width * height + offset;
  61. /* create output buffer for DicomImage class */
  62. QByteArray buffer;
  63. buffer.append(header);
  64. buffer.resize(length);
  65. /* copy PGM header to buffer */
  66. if (dcmImage->getOutputData(static_cast<void *>(buffer.data() + offset), length - offset, 8, 0)){
  67. if (!image.loadFromData( buffer )){
  68. logger.error("QImage couldn't created");
  69. return false;
  70. }
  71. }
  72. }
  73. image.scaled(128,128,Qt::KeepAspectRatio).save(path,"PNG");
  74. return true;
  75. }