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 ) );
87 this->Superclass::RequestData( request, inputVector, outputVector );
90 // -------------------------------------------------------------------------
92 // This templated function executes the filter for any type of data.
94 void cpExtensions_Visualization_ImageBlender_Execute(
95 cpExtensions::Visualization::ImageBlender* self,
96 vtkImageData** inDatas, int numInputs,
97 const std::vector< double >& ranges, vtkImageData* outData,
98 int outExt[ 6 ], int id, T* really_not_used
101 vtkImageIterator< T > inItsFast[ 256 ];
103 vtkImageProgressIterator< T > outIt( outData, outExt, self, id );
104 vtkImageIterator< T >* inIts;
106 if( numInputs < 256 )
113 inIts = new vtkImageIterator< T >[ numInputs ];
114 inSI = new T*[ numInputs ];
118 // Loop through all input ImageData to initialize iterators
119 for( int i = 0; i < numInputs; ++i )
120 inIts[ i ].Initialize( inDatas[ i ], outExt );
122 // Loop through output pixels
123 while( !outIt.IsAtEnd( ) )
125 for( int j = 0; j < numInputs; ++j )
126 inSI[ j ] = inIts[ j ].BeginSpan( );
128 T* outSI = outIt.BeginSpan( );
129 T* outSIEnd = outIt.EndSpan( );
132 while( outSI != outSIEnd )
134 // Input 0 is ignored: it is just used to guarantee sizes all over
136 double vmax = double( 0 );
137 for( int k = 1; k < numInputs; ++k )
140 ( ( double( k ) * double( *inSI[ k ] ) ) - ranges[ k << 1 ] ) /
141 ( ranges[ ( k << 1 ) + 1 ] - ranges[ k << 1 ] );
142 vmax = ( vmax < v )? v: vmax;
145 *outSI = static_cast< T >( vmax );
147 for( int l = 0; l < numInputs; ++l )
151 for( int j = 0; j < numInputs; ++j )
152 inIts[ j ].NextSpan( );
157 if( numInputs >= 256)
165 // -------------------------------------------------------------------------
166 void cpExtensions::Visualization::ImageBlender::
168 vtkInformation* request,
169 vtkInformationVector** inputVector,
170 vtkInformationVector* outputVector,
171 vtkImageData*** inData, vtkImageData** outData,
172 int outExt[ 6 ], int id
175 if( inData[ 0 ][ 0 ] == NULL )
177 vtkErrorMacro( << "Input " << 0 << " must be specified." );
182 int numInputs = this->GetNumberOfInputConnections( 0 );
183 int scalarType = inData[ 0 ][ 0 ]->GetScalarType( );
184 int numComp = inData[ 0 ][ 0 ]->GetNumberOfScalarComponents( );
185 for( int i = 1; i < numInputs; ++i )
187 int otherType = inData[ 0 ][ i ]->GetScalarType( );
188 int otherComp = inData[ 0 ][ i ]->GetNumberOfScalarComponents( );
189 if( otherType != scalarType || otherComp != numComp )
193 "ThreadedRequestData: Input " << i
194 << " has " << otherComp << " components of type "
195 << otherType << ", but input 0 has " << numComp
196 << " components of type " << scalarType
207 cpExtensions_Visualization_ImageBlender_Execute(
208 this, inData[ 0 ], numInputs, this->m_Ranges,
209 outData[ 0 ], outExt, id, static_cast< VTK_TT* >( 0 )
214 vtkErrorMacro( << "Execute: Unknown ScalarType" );
219 // -------------------------------------------------------------------------
220 int cpExtensions::Visualization::ImageBlender::
221 FillInputPortInformation( int i, vtkInformation* info )
223 info->Set( vtkAlgorithm::INPUT_IS_REPEATABLE( ), 1 );
224 return( this->Superclass::FillInputPortInformation( i,info ) );