OGS
VtkVisHelper.cpp
Go to the documentation of this file.
1
15// ** INCLUDES **
16#include "VtkVisHelper.h"
17
18#include <vtkImageData.h>
19#include <vtkPointData.h>
20#include <vtkSmartPointer.h>
21#include <vtkTexture.h>
22#include <vtkUnsignedCharArray.h>
23
24#include <QImage>
25
26vtkImageData* VtkVisHelper::QImageToVtkImageData(QImage& img)
27{
28 std::size_t imgWidth = img.width();
29 std::size_t imgHeight = img.height();
30 vtkSmartPointer<vtkUnsignedCharArray> data =
31 vtkSmartPointer<vtkUnsignedCharArray>::New();
32 data->SetNumberOfComponents(3);
33 data->SetNumberOfTuples(imgWidth * imgHeight);
34
35 for (std::size_t j = 0; j < imgHeight; j++)
36 {
37 for (std::size_t i = 0; i < imgWidth; i++)
38 {
39 QRgb pix = img.pixel(i, j);
40 const float color[3] = {static_cast<float>(qRed(pix)),
41 static_cast<float>(qGreen(pix)),
42 static_cast<float>(qBlue(pix))};
43 data->SetTuple(j * imgWidth + i, color);
44 }
45 }
46
47 vtkImageData* imgData = vtkImageData::New();
48 imgData->SetExtent(0, imgWidth - 1, 0, imgHeight - 1, 0, 0);
49 imgData->SetOrigin(0, 0, 0);
50 imgData->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
51 imgData->GetPointData()->SetScalars(data);
52
53 return imgData;
54}
55
56vtkTexture* VtkVisHelper::QImageToVtkTexture(QImage& img)
57{
58 vtkSmartPointer<vtkImageData> imgData = QImageToVtkImageData(img);
59
60 vtkTexture* texture = vtkTexture::New();
61 texture->InterpolateOff();
62 texture->RepeatOff();
63 // texture->EdgeClampOff();
64 // texture->SetBlendingMode(0);
65 texture->SetInputData(imgData);
66
67 return texture;
68}
Definition of the VtkVisHelper class.
static vtkImageData * QImageToVtkImageData(QImage &img)
Converts a QImage to vtkImageData.
static vtkTexture * QImageToVtkTexture(QImage &img)
Converts a QImage-object into a vtkTexture-object.