forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkQImageToImageSource.cxx
151 lines (126 loc) · 4.66 KB
/
vtkQImageToImageSource.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*=========================================================================
Program: Visualization Toolkit
Module: vtkQImageToImageSource.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkQImageToImageSource.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkUnsignedCharArray.h"
#include "vtkPointData.h"
#include <QApplication>
#include <QImage>
#include <cmath>
#include "vtkSmartPointer.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
vtkStandardNewMacro(vtkQImageToImageSource);
//----------------------------------------------------------------------------
vtkQImageToImageSource::vtkQImageToImageSource()
{
this->QtImage = 0;
this->SetNumberOfInputPorts(0);
this->DataExtent[0] = 0;
this->DataExtent[1] = 0;
this->DataExtent[2] = 0;
this->DataExtent[3] = 0;
this->DataExtent[4] = 0;
this->DataExtent[5] = 0;
}
//----------------------------------------------------------------------------
int vtkQImageToImageSource::RequestData( vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
if(!QApplication::instance())
{
vtkErrorMacro("You must initialize QApplication before using this filter.");
return 0;
}
// get the info objects
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkImageData *output = vtkImageData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if( this->QtImage == 0 )
{
vtkErrorMacro( "Qt Image was not set." );
return 0;
}
QImage newImage = this->QtImage->convertToFormat( QImage::Format_ARGB32 );
QSize size = newImage.size();
int width = size.width();
int height = size.height();
const unsigned char* data2 = newImage.bits();
unsigned char* data = new unsigned char[4*width*height];
memcpy( data, data2, 4*width*height );
output->SetExtent(this->DataExtent);
output->AllocateScalars(VTK_UNSIGNED_CHAR, 4);
vtkUnsignedCharArray* array = vtkArrayDownCast<vtkUnsignedCharArray>( output->GetPointData()->GetScalars() );
int i, j;
unsigned char temp[4];
for( i = 0; i < height/2; i++ )
{
for( j = 0; j < width; j++ )
{
int bottom_address = ((((height-1)-i)*width)+j);
int top_address = (i*width)+j;
temp[0] = data[(4*bottom_address)+0];
temp[1] = data[(4*bottom_address)+1];
temp[2] = data[(4*bottom_address)+2];
temp[3] = data[(4*bottom_address)+3];
data[(4*bottom_address)+2] = data[(4*top_address)+0]; //B
data[(4*bottom_address)+1] = data[(4*top_address)+1]; //G
data[(4*bottom_address)+0] = data[(4*top_address)+2]; //R
data[(4*bottom_address)+3] = data[(4*top_address)+3]; //A
data[(4*top_address)+2] = temp[0]; //B
data[(4*top_address)+1] = temp[1]; //G
data[(4*top_address)+0] = temp[2]; //R
data[(4*top_address)+3] = temp[3]; //A
}
}
if( height%2 )
{
for( j = 0; j < width; j++ )
{
int address = (i*width)+j;
unsigned char temp1 = data[(4*address)+0];
data[(4*address)+0] = data[(4*address)+2];
data[(4*address)+2] = temp1;
}
}
array->SetVoidArray( data, 4*width*height, 0, vtkUnsignedCharArray::VTK_DATA_ARRAY_DELETE );
return 1;
}
//----------------------------------------------------------------------------
int vtkQImageToImageSource::RequestInformation (
vtkInformation * vtkNotUsed(request),
vtkInformationVector ** vtkNotUsed( inputVector ),
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation* outInfo = outputVector->GetInformationObject(0);
if( this->QtImage == 0 )
{
vtkErrorMacro( "Qt Image was not set." );
return 0;
}
QSize size = this->QtImage->size();
this->DataExtent[1] = size.width() - 1;
this->DataExtent[3] = size.height() - 1;
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
this->DataExtent,6);
return 1;
}
//----------------------------------------------------------------------------
void vtkQImageToImageSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}