OGS
VtkVisHelper.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ** INCLUDES **
5#include "VtkVisHelper.h"
6
7#include <vtkImageData.h>
8#include <vtkPointData.h>
9#include <vtkSmartPointer.h>
10#include <vtkTexture.h>
11#include <vtkUnsignedCharArray.h>
12
13#include <QImage>
14
15vtkImageData* VtkVisHelper::QImageToVtkImageData(QImage& img)
16{
17 std::size_t imgWidth = img.width();
18 std::size_t imgHeight = img.height();
19 vtkSmartPointer<vtkUnsignedCharArray> data =
20 vtkSmartPointer<vtkUnsignedCharArray>::New();
21 data->SetNumberOfComponents(3);
22 data->SetNumberOfTuples(imgWidth * imgHeight);
23
24 for (std::size_t j = 0; j < imgHeight; j++)
25 {
26 for (std::size_t i = 0; i < imgWidth; i++)
27 {
28 QRgb pix = img.pixel(i, j);
29 const float color[3] = {static_cast<float>(qRed(pix)),
30 static_cast<float>(qGreen(pix)),
31 static_cast<float>(qBlue(pix))};
32 data->SetTuple(j * imgWidth + i, color);
33 }
34 }
35
36 vtkImageData* imgData = vtkImageData::New();
37 imgData->SetExtent(0, imgWidth - 1, 0, imgHeight - 1, 0, 0);
38 imgData->SetOrigin(0, 0, 0);
39 imgData->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
40 imgData->GetPointData()->SetScalars(data);
41
42 return imgData;
43}
44
45vtkTexture* VtkVisHelper::QImageToVtkTexture(QImage& img)
46{
47 vtkSmartPointer<vtkImageData> imgData = QImageToVtkImageData(img);
48
49 vtkTexture* texture = vtkTexture::New();
50 texture->InterpolateOff();
51 texture->RepeatOff();
52 // texture->EdgeClampOff();
53 // texture->SetBlendingMode(0);
54 texture->SetInputData(imgData);
55
56 return texture;
57}
static vtkImageData * QImageToVtkImageData(QImage &img)
Converts a QImage to vtkImageData.
static vtkTexture * QImageToVtkTexture(QImage &img)
Converts a QImage-object into a vtkTexture-object.