ctkVTKWidgetsUtils.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QImage>
  17. #include <QPainter>
  18. #include <QStyle>
  19. #include <QWidget>
  20. // ctkWidgets includes
  21. #include "ctkVTKWidgetsUtils.h"
  22. #include "ctkWidgetsUtils.h"
  23. // VTK includes
  24. #if CTK_USE_QVTKOPENGLWIDGET
  25. #include <QVTKOpenGLWidget.h>
  26. #else
  27. #include <QVTKWidget.h>
  28. #endif
  29. #include <vtkImageData.h>
  30. #include <vtkPiecewiseFunction.h>
  31. #include <vtkScalarsToColors.h>
  32. #include <vtkVersion.h>
  33. //----------------------------------------------------------------------------
  34. QImage ctk::grabVTKWidget(QWidget* widget, QRect rectangle)
  35. {
  36. if (!widget)
  37. {
  38. return QImage();
  39. }
  40. if (!rectangle.isValid())
  41. {
  42. rectangle = QRect(0,0,widget->width(),widget->height());
  43. }
  44. QImage widgetImage = ctk::grabWidget(widget, rectangle);
  45. QPainter painter;
  46. painter.begin(&widgetImage);
  47. #if CTK_USE_QVTKOPENGLWIDGET
  48. foreach(QVTKOpenGLWidget* vtkWidget, widget->findChildren<QVTKOpenGLWidget*>())
  49. #else
  50. foreach(QVTKWidget* vtkWidget, widget->findChildren<QVTKWidget*>())
  51. #endif
  52. {
  53. if (!vtkWidget->isVisible())
  54. {
  55. continue;
  56. }
  57. QRect subWidgetRect = QRect(vtkWidget->mapTo(widget, QPoint(0,0)), vtkWidget->size());
  58. if (!rectangle.intersects(subWidgetRect))
  59. {
  60. continue;
  61. }
  62. #if CTK_USE_QVTKOPENGLWIDGET
  63. QImage subImage = vtkWidget->grabFramebuffer();
  64. #else
  65. vtkImageData* imageData = vtkWidget->cachedImage();
  66. /// \todo retrieve just the rectangle.intersected(
  67. QImage subImage = ctk::vtkImageDataToQImage(imageData);
  68. #endif
  69. painter.drawImage(subWidgetRect, subImage);
  70. }
  71. painter.end();
  72. return widgetImage;
  73. }
  74. //----------------------------------------------------------------------------
  75. QImage ctk::vtkImageDataToQImage(vtkImageData* imageData)
  76. {
  77. if (!imageData)
  78. {
  79. return QImage();
  80. }
  81. #if VTK_MAJOR_VERSION <= 5
  82. imageData->Update();
  83. #endif
  84. /// \todo retrieve just the UpdateExtent
  85. int width = imageData->GetDimensions()[0];
  86. int height = imageData->GetDimensions()[1];
  87. QImage image(width, height, QImage::Format_RGB32);
  88. QRgb* rgbPtr = reinterpret_cast<QRgb*>(image.bits()) +
  89. width * (height-1);
  90. unsigned char* colorsPtr = reinterpret_cast<unsigned char*>(
  91. imageData->GetScalarPointer());
  92. // mirror vertically
  93. for(int row = 0; row < height; ++row)
  94. {
  95. for (int col = 0; col < width; ++col)
  96. {
  97. // Swap rgb
  98. *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
  99. colorsPtr += 3;
  100. }
  101. rgbPtr -= width * 2;
  102. }
  103. return image;
  104. }
  105. //----------------------------------------------------------------------------
  106. QImage ctk::scalarsToColorsImage(vtkScalarsToColors* scalarsToColors,
  107. const QSize& size)
  108. {
  109. if (!scalarsToColors ||
  110. scalarsToColors->GetNumberOfAvailableColors() <= 0)
  111. {
  112. return QImage();
  113. }
  114. int width = size.width();
  115. int height = size.height();
  116. if (size.isEmpty())
  117. {
  118. width = height = qApp->style()->pixelMetric(QStyle::PM_LargeIconSize);
  119. }
  120. double* values = new double[width];
  121. const double* range = scalarsToColors->GetRange();
  122. for (int i = 0; i < width; ++i)
  123. {
  124. values[i] = range[0] + i * (range[1] - range[0]) / (width - 1);
  125. }
  126. QImage transferFunctionImage(width, height, QImage::Format_RGB32);
  127. unsigned char* colors = transferFunctionImage.bits();
  128. // Map the first line
  129. scalarsToColors->MapScalarsThroughTable2(
  130. values, colors, VTK_DOUBLE, width, 1, VTK_RGBA);
  131. delete[] values;
  132. // Pixels are not correctly ordered, reorder them correctly
  133. unsigned char* colorsPtr = colors;
  134. QRgb* rgbPtr = reinterpret_cast<QRgb*>(colors);
  135. for (int i = 0; i < width; ++i)
  136. {
  137. *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
  138. colorsPtr += 4;
  139. }
  140. // Fill the other lines
  141. for (int i = 1; i < height; ++i)
  142. {
  143. memcpy(colors + i*VTK_RGBA*width, colors, VTK_RGBA*width);
  144. }
  145. return transferFunctionImage;
  146. }