1 #include <cpExtensions/Visualization/ImageBlender.h>
3 #include <vtkDataObject.h>
4 #include <vtkImageData.h>
5 #include <vtkImageIterator.h>
6 #include <vtkImageProgressIterator.h>
7 #include <vtkInformation.h>
8 #include <vtkInformationVector.h>
10 // -------------------------------------------------------------------------
11 cpExtensions::Visualization::ImageBlender::
12 Self* cpExtensions::Visualization::ImageBlender::
15 return( new Self( ) );
19 // -------------------------------------------------------------------------
20 cpExtensions::Visualization::ImageBlender::
24 this->SetNumberOfInputPorts( 1 );
27 // -------------------------------------------------------------------------
28 cpExtensions::Visualization::ImageBlender::
33 // -------------------------------------------------------------------------
34 int cpExtensions::Visualization::ImageBlender::
36 vtkInformation* request,
37 vtkInformationVector** inputVector,
38 vtkInformationVector* outputVector
41 if( this->GetNumberOfInputConnections( 0 ) == 0 )
44 vtkDataObject::SetPointDataActiveScalarInfo(
45 outputVector->GetInformationObject( 0 ),
52 // -------------------------------------------------------------------------
53 int cpExtensions::Visualization::ImageBlender::
55 vtkInformation* request,
56 vtkInformationVector** inputVector,
57 vtkInformationVector* outputVector
60 this->m_Ranges.clear( );
61 for( int i = 0; i < this->GetNumberOfInputPorts( ); ++i )
63 vtkInformationVector* portInfo = inputVector[ i ];
64 for( int j = 0; j < portInfo->GetNumberOfInformationObjects( ); ++j )
66 vtkInformation* info = portInfo->GetInformationObject( j );
67 vtkImageData* image = vtkImageData::SafeDownCast(
68 info->Get( vtkDataObject::DATA_OBJECT( ) )
73 image->GetScalarRange( r );
74 this->m_Ranges.push_back( r[ 0 ] );
75 this->m_Ranges.push_back( r[ 1 ] );
79 this->m_Ranges.push_back( double( 0 ) );
80 this->m_Ranges.push_back( double( 0 ) );
88 this->Superclass::RequestData( request, inputVector, outputVector )
92 // -------------------------------------------------------------------------
94 // This templated function executes the filter for any type of data.
96 void cpExtensions_Visualization_ImageBlender_Execute(
97 cpExtensions::Visualization::ImageBlender* self,
98 vtkImageData** inDatas, int numInputs,
99 const std::vector< double >& ranges, vtkImageData* outData,
100 int outExt[ 6 ], int id, T* really_not_used
103 vtkImageIterator< T > inItsFast[ 256 ];
105 vtkImageProgressIterator< T > outIt( outData, outExt, self, id );
106 vtkImageIterator< T >* inIts;
108 if( numInputs < 256 )
115 inIts = new vtkImageIterator< T >[ numInputs ];
116 inSI = new T*[ numInputs ];
120 // Loop through all input ImageData to initialize iterators
121 for( int i = 0; i < numInputs; ++i )
122 inIts[ i ].Initialize( inDatas[ i ], outExt );
124 // Loop through output pixels
125 while( !outIt.IsAtEnd( ) )
127 for( int j = 0; j < numInputs; ++j )
128 inSI[ j ] = inIts[ j ].BeginSpan( );
130 T* outSI = outIt.BeginSpan( );
131 T* outSIEnd = outIt.EndSpan( );
134 while( outSI != outSIEnd )
136 // Input 0 is ignored: it is just used to guarantee sizes all over
138 double vmax = double( 0 );
139 for( int k = 1; k < numInputs; ++k )
142 ( ( double( k ) * double( *inSI[ k ] ) ) - ranges[ k << 1 ] ) /
143 ( ranges[ ( k << 1 ) + 1 ] - ranges[ k << 1 ] );
144 vmax = ( vmax < v )? v: vmax;
147 *outSI = static_cast< T >( vmax );
149 for( int l = 0; l < numInputs; ++l )
153 for( int j = 0; j < numInputs; ++j )
154 inIts[ j ].NextSpan( );
159 if( numInputs >= 256)
167 // -------------------------------------------------------------------------
168 void cpExtensions::Visualization::ImageBlender::
170 vtkInformation* request,
171 vtkInformationVector** inputVector,
172 vtkInformationVector* outputVector,
173 vtkImageData*** inData, vtkImageData** outData,
174 int outExt[ 6 ], int id
177 if( inData[ 0 ][ 0 ] == NULL )
179 vtkErrorMacro( << "Input " << 0 << " must be specified." );
184 int numInputs = this->GetNumberOfInputConnections( 0 );
185 int scalarType = inData[ 0 ][ 0 ]->GetScalarType( );
186 int numComp = inData[ 0 ][ 0 ]->GetNumberOfScalarComponents( );
187 for( int i = 1; i < numInputs; ++i )
189 int otherType = inData[ 0 ][ i ]->GetScalarType( );
190 int otherComp = inData[ 0 ][ i ]->GetNumberOfScalarComponents( );
191 if( otherType != scalarType || otherComp != numComp )
195 "ThreadedRequestData: Input " << i
196 << " has " << otherComp << " components of type "
197 << otherType << ", but input 0 has " << numComp
198 << " components of type " << scalarType
209 cpExtensions_Visualization_ImageBlender_Execute(
210 this, inData[ 0 ], numInputs, this->m_Ranges,
211 outData[ 0 ], outExt, id, static_cast< VTK_TT* >( 0 )
216 vtkErrorMacro( << "Execute: Unknown ScalarType" );
221 // -------------------------------------------------------------------------
222 int cpExtensions::Visualization::ImageBlender::
223 FillInputPortInformation( int i, vtkInformation* info )
225 info->Set( vtkAlgorithm::INPUT_IS_REPEATABLE( ), 1 );
226 return( this->Superclass::FillInputPortInformation( i,info ) );