소스 검색

Add scalarsToColorsImage and base64HTMLImageTagSrc

ctk::scalarsToColorsImage converts a vtkScalarsToColors into a
QImage
ctk::base64HTMLImageTagSrc converst a QImage into an
HTML/XML/CSS img tag src.
Julien Finet 14 년 전
부모
커밋
a55cc9674c

+ 2 - 0
Libs/Visualization/VTK/Widgets/CMakeLists.txt

@@ -29,6 +29,8 @@ SET(KIT_SRCS
   ctkVTKRenderView_p.h
   ctkVTKScalarBarWidget.cpp
   ctkVTKScalarBarWidget.h
+  ctkVTKScalarsToColorsUtils.cpp
+  ctkVTKScalarsToColorsUtils.h
   ctkVTKSliceView.cpp
   ctkVTKSliceView.h
   ctkVTKSliceView_p.h

+ 2 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/CMakeLists.txt

@@ -16,6 +16,7 @@ SET(TEST_SOURCES
   ctkTransferFunctionViewTest4.cpp
   ctkTransferFunctionViewTest5.cpp
   ctkVTKRenderViewTest1.cpp
+  ctkVTKScalarsToColorsUtilsTest1.cpp
   ctkVTKSliceViewTest1.cpp
   ctkVTKSurfaceMaterialPropertyWidgetTest1.cpp
   ctkVTKTextPropertyWidgetTest1.cpp
@@ -90,6 +91,7 @@ SIMPLE_TEST( ctkVTKDataSetModelTest1 )
 SIMPLE_TEST( ctkVTKMagnifyViewTest1 )
 SIMPLE_TEST( ctkVTKMatrixWidgetTest1 )
 SIMPLE_TEST( ctkVTKScalarBarWidgetTest1 )
+SIMPLE_TEST( ctkVTKScalarsToColorsUtilsTest1 )
 SIMPLE_TEST( ctkTransferFunctionBarsItemTest1 )
 SIMPLE_TEST( ctkTransferFunctionViewTest1 )
 SIMPLE_TEST( ctkTransferFunctionViewTest2 )

+ 67 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsUtilsTest1.cpp

@@ -0,0 +1,67 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QImage>
+#include <QLabel>
+#include <QTimer>
+
+// CTK includes
+#include "ctkVTKScalarsToColorsUtils.h"
+
+// VTK includes
+#include <vtkColorTransferFunction.h>
+#include <vtkSmartPointer.h>
+
+// STD includes
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkVTKScalarsToColorsUtilsTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  vtkSmartPointer<vtkColorTransferFunction> ctf =
+    vtkSmartPointer<vtkColorTransferFunction>::New();
+  ctf->AddRGBPoint(-0.5, 194./255,  78./255,  68./255);
+  ctf->AddRGBPoint(2.2, 194./255, 141./255,  68./255);
+  ctf->AddRGBPoint(3.3,  68./255, 194./255,  78./255);
+  ctf->AddRGBPoint(4.8,  68./255, 194./255, 141./255);
+  ctf->AddRGBPoint(5.9,  78./255,  68./255, 194./255);
+  ctf->AddRGBPoint(7.0, 141./255,  68./255, 194./255);
+  
+  QImage image = ctk::scalarsToColorsImage(ctf);
+
+  QLabel label;
+  label.setPixmap( QPixmap::fromImage(image));
+  label.show();
+
+  QLabel label2;
+  label2.setText( QString("<img src=\"%1\">: text")
+    .arg(ctk::base64HTMLImageTagSrc(image)));
+  label2.show();
+  
+  if (argc < 2 || QString(argv[1]) != "-I")
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+  return app.exec();
+}

+ 92 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsUtils.cpp

@@ -0,0 +1,92 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QBuffer>
+#include <QImage>
+#include <QStyle>
+
+// CTK includes
+#include "ctkLogger.h"
+#include "ctkVTKScalarsToColorsUtils.h"
+
+// VTK includes
+#include <vtkScalarsToColors.h>
+
+//----------------------------------------------------------------------------
+static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKScalarsToColorsUtils");
+//----------------------------------------------------------------------------
+
+//----------------------------------------------------------------------------
+QImage ctk::scalarsToColorsImage(vtkScalarsToColors* scalarsToColors, const QSize& size)
+{
+  if (!scalarsToColors ||
+      scalarsToColors->GetNumberOfAvailableColors() <= 0)
+    {
+    return QImage();
+    }
+  int width = size.width();
+  int height = size.height();
+  if (size.isEmpty())
+    {
+    width = height = qApp->style()->pixelMetric(QStyle::PM_LargeIconSize);
+    }
+  
+  double* values = new double[width];
+  const double* range = scalarsToColors->GetRange();
+  for (int i = 0; i < width; ++i)
+    {
+    values[i] = range[0] + i * (range[1] - range[0]) / (width - 1);
+    }
+
+  QImage transferFunctionImage(width, height, QImage::Format_RGB32);
+  unsigned char* colors = transferFunctionImage.bits();
+  // Map the first line
+  scalarsToColors->MapScalarsThroughTable2(
+    values, colors, VTK_DOUBLE, width, 1, VTK_RGBA);
+  delete [] values;
+  // Pixels are not correctly ordered, reorder them correctly
+  unsigned char* colorsPtr = colors;
+  QRgb* rgbPtr = reinterpret_cast<QRgb*>(colors);
+  for (int i = 0; i < width; ++i)
+    {
+    *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
+    colorsPtr += 4;
+    }
+  // Fill the other lines
+  for (int i = 1; i < height; ++i)
+    {
+    memcpy(colors + i*VTK_RGBA*width, colors, VTK_RGBA*width);
+    }
+
+  return transferFunctionImage;
+}
+
+//----------------------------------------------------------------------------
+QString ctk::base64HTMLImageTagSrc(const QImage& image)
+{
+  QByteArray byteArray;
+  QBuffer buffer(&byteArray);
+  buffer.open(QIODevice::WriteOnly);
+  image.save(&buffer, "PNG");
+  return QString("data:image/png;base64,%1")
+    .arg(QString(buffer.data().toBase64()));
+}

+ 45 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsUtils.h

@@ -0,0 +1,45 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkVTKScalarsToColorsUtils_h
+#define __ctkVTKScalarsToColorsUtils_h
+
+// CTK includes
+#include "ctkVisualizationVTKWidgetsExport.h"
+
+// VTK includes
+class vtkScalarsToColors;
+
+namespace ctk {
+///
+/// \ingroup vtkWidgets
+/// Convert a vtkScalarsToColors into a QImage
+/// If size is empty, it will use the large icon size of the application style
+QImage CTK_VISUALIZATION_VTK_WIDGETS_EXPORT scalarsToColorsImage(vtkScalarsToColors* scalarsToColors, const QSize& size = QSize());
+
+///
+/// \ingroup vtkWidgets
+/// Create a base 64 image tag. Can be used that way:
+/// QString("<img src=\"%1\">").arg(base64HTMLImageTagSrc(myImage);
+QString CTK_VISUALIZATION_VTK_WIDGETS_EXPORT base64HTMLImageTagSrc(const QImage& image);
+
+}
+
+#endif