ctkWidgetsUtils.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <QBuffer>
  16. #include <QImage>
  17. #include <QGLWidget>
  18. #include <QPainter>
  19. #include <QWidget>
  20. // ctkWidgets includes
  21. #include "ctkWidgetsUtils.h"
  22. //----------------------------------------------------------------------------
  23. QString ctk::base64HTMLImageTagSrc(const QImage& image)
  24. {
  25. QByteArray byteArray;
  26. QBuffer buffer(&byteArray);
  27. buffer.open(QIODevice::WriteOnly);
  28. image.save(&buffer, "PNG");
  29. return QString("data:image/png;base64,%1")
  30. .arg(QString(buffer.data().toBase64()));
  31. }
  32. //----------------------------------------------------------------------------
  33. QImage ctk::grabWidget(QWidget* widget, QRect rectangle)
  34. {
  35. if (!widget)
  36. {
  37. return QImage();
  38. }
  39. if (!rectangle.isValid())
  40. {
  41. rectangle = QRect(0,0,widget->width(),widget->height());
  42. }
  43. QPixmap widgetPixmap = QPixmap::grabWidget(widget, rectangle);
  44. QImage widgetImage = widgetPixmap.toImage();
  45. QPainter painter;
  46. painter.begin(&widgetImage);
  47. foreach(QGLWidget* glWidget, widget->findChildren<QGLWidget*>())
  48. {
  49. QRect subWidgetRect = QRect(glWidget->mapTo(widget, QPoint(0,0)), glWidget->size());
  50. if (!rectangle.intersects(subWidgetRect))
  51. {
  52. continue;
  53. }
  54. QImage subImage = glWidget->grabFrameBuffer();
  55. painter.drawImage(subWidgetRect, subImage);
  56. }
  57. painter.end();
  58. return widgetImage;
  59. }
  60. //----------------------------------------------------------------------------
  61. QImage ctk::kwIconToQImage(const unsigned char *data, int width, int height, int pixelSize, unsigned int bufferLength, int options)
  62. {
  63. Q_UNUSED(options); // not yet supported
  64. QByteArray imageData = QByteArray::fromRawData(
  65. reinterpret_cast<const char *>(data), bufferLength);
  66. unsigned int expectedLength = width * height * pixelSize;
  67. // Start with a zlib header ? If not, then it must be base64 encoded
  68. if (bufferLength != expectedLength &&
  69. static_cast<unsigned char>(imageData[0]) != 0x78 &&
  70. static_cast<unsigned char>(imageData[1]) != 0xDA)
  71. {
  72. imageData = QByteArray::fromBase64(imageData);
  73. bufferLength = imageData.size();
  74. }
  75. if (bufferLength != expectedLength &&
  76. static_cast<unsigned char>(imageData[0]) == 0x78 &&
  77. static_cast<unsigned char>(imageData[1]) == 0xDA)
  78. {
  79. imageData.prepend(static_cast<char>((expectedLength >> 0) & 0xFF));
  80. imageData.prepend(static_cast<char>((expectedLength >> 8) & 0xFF));
  81. imageData.prepend(static_cast<char>((expectedLength >> 16) & 0xFF));
  82. imageData.prepend(static_cast<char>((expectedLength >> 24) & 0xFF));
  83. imageData = qUncompress(imageData);
  84. }
  85. QImage image(reinterpret_cast<unsigned char*>(imageData.data()),
  86. width, height, width * pixelSize,
  87. pixelSize == 4 ? QImage::Format_ARGB32 : QImage::Format_RGB888);
  88. if (pixelSize == 4)
  89. {
  90. return image.rgbSwapped();
  91. }
  92. return image.copy();
  93. }