ctkVTKWidgetsUtils.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <QImage>
  16. #include <QPainter>
  17. #include <QWidget>
  18. // ctkWidgets includes
  19. #include "ctkVTKWidgetsUtils.h"
  20. #include "ctkWidgetsUtils.h"
  21. // VTK includes
  22. #include <QVTKWidget.h>
  23. #include <vtkImageData.h>
  24. #include <vtkVersion.h>
  25. //----------------------------------------------------------------------------
  26. QImage ctk::grabVTKWidget(QWidget* widget, QRect rectangle)
  27. {
  28. if (!widget)
  29. {
  30. return QImage();
  31. }
  32. if (!rectangle.isValid())
  33. {
  34. rectangle = QRect(0,0,widget->width(),widget->height());
  35. }
  36. QImage widgetImage = ctk::grabWidget(widget, rectangle);
  37. QPainter painter;
  38. painter.begin(&widgetImage);
  39. foreach(QVTKWidget* vtkWidget, widget->findChildren<QVTKWidget*>())
  40. {
  41. if (!vtkWidget->isVisible())
  42. {
  43. continue;
  44. }
  45. QRect subWidgetRect = QRect(vtkWidget->mapTo(widget, QPoint(0,0)), vtkWidget->size());
  46. if (!rectangle.intersects(subWidgetRect))
  47. {
  48. continue;
  49. }
  50. vtkImageData* imageData = vtkWidget->cachedImage();
  51. /// \todo retrieve just the rectangle.intersected(
  52. QImage subImage = ctk::vtkImageDataToQImage(imageData);
  53. painter.drawImage(subWidgetRect, subImage);
  54. }
  55. painter.end();
  56. return widgetImage;
  57. }
  58. //----------------------------------------------------------------------------
  59. QImage ctk::vtkImageDataToQImage(vtkImageData* imageData)
  60. {
  61. if (!imageData)
  62. {
  63. return QImage();
  64. }
  65. #if VTK_MAJOR_VERSION <= 5
  66. imageData->Update();
  67. #endif
  68. /// \todo retrieve just the UpdateExtent
  69. int width = imageData->GetDimensions()[0];
  70. int height = imageData->GetDimensions()[1];
  71. QImage image(width, height, QImage::Format_RGB32);
  72. QRgb* rgbPtr = reinterpret_cast<QRgb*>(image.bits()) +
  73. width * (height-1);
  74. unsigned char* colorsPtr = reinterpret_cast<unsigned char*>(
  75. imageData->GetScalarPointer());
  76. // mirror vertically
  77. for(int row = 0; row < height; ++row)
  78. {
  79. for (int col = 0; col < width; ++col)
  80. {
  81. // Swap rgb
  82. *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
  83. colorsPtr += 3;
  84. }
  85. rgbPtr -= width * 2;
  86. }
  87. return image;
  88. }