1 #include <cpExtensions/Algorithms/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::Algorithms::ImageBlender::
12 Self* cpExtensions::Algorithms::ImageBlender::
15 return( new Self( ) );
18 // -------------------------------------------------------------------------
19 unsigned int cpExtensions::Algorithms::ImageBlender::
22 unsigned int np = this->GetNumberOfInputPorts( );
24 for( unsigned int p = 0; p < np; ++p )
25 ni += this->GetNumberOfInputConnections( p );
29 // -------------------------------------------------------------------------
30 cpExtensions::Algorithms::ImageBlender::
34 this->SetNumberOfInputPorts( 1 );
37 // -------------------------------------------------------------------------
38 cpExtensions::Algorithms::ImageBlender::
43 // -------------------------------------------------------------------------
44 int cpExtensions::Algorithms::ImageBlender::
46 vtkInformation* request,
47 vtkInformationVector** inputVector,
48 vtkInformationVector* outputVector
51 if( this->GetNumberOfInputConnections( 0 ) == 0 )
54 vtkDataObject::SetPointDataActiveScalarInfo(
55 outputVector->GetInformationObject( 0 ),
62 // -------------------------------------------------------------------------
63 int cpExtensions::Algorithms::ImageBlender::
65 vtkInformation* request,
66 vtkInformationVector** inputVector,
67 vtkInformationVector* outputVector
70 this->m_Ranges.clear( );
71 for( int i = 0; i < this->GetNumberOfInputPorts( ); ++i )
73 vtkInformationVector* portInfo = inputVector[ i ];
74 for( int j = 0; j < portInfo->GetNumberOfInformationObjects( ); ++j )
76 vtkInformation* info = portInfo->GetInformationObject( j );
77 vtkImageData* image = vtkImageData::SafeDownCast(
78 info->Get( vtkDataObject::DATA_OBJECT( ) )
83 image->GetScalarRange( r );
84 this->m_Ranges.push_back( r[ 0 ] );
85 this->m_Ranges.push_back( r[ 1 ] );
89 this->m_Ranges.push_back( double( 0 ) );
90 this->m_Ranges.push_back( double( 0 ) );
98 this->Superclass::RequestData( request, inputVector, outputVector )
102 // -------------------------------------------------------------------------
104 // This templated function executes the filter for any type of data.
106 void cpExtensions_Algorithms_ImageBlender_Execute(
107 cpExtensions::Algorithms::ImageBlender* self,
108 vtkImageData** inDatas, int numInputs,
109 const std::vector< double >& ranges, vtkImageData* outData,
110 int outExt[ 6 ], int id, T* really_not_used
113 vtkImageIterator< T > inItsFast[ 256 ];
115 vtkImageProgressIterator< T > outIt( outData, outExt, self, id );
116 vtkImageIterator< T >* inIts;
118 if( numInputs < 256 )
125 inIts = new vtkImageIterator< T >[ numInputs ];
126 inSI = new T*[ numInputs ];
130 // Loop through all input ImageData to initialize iterators
131 for( int i = 0; i < numInputs; ++i )
132 inIts[ i ].Initialize( inDatas[ i ], outExt );
134 // Loop through output pixels
135 while( !outIt.IsAtEnd( ) )
137 for( int j = 0; j < numInputs; ++j )
138 inSI[ j ] = inIts[ j ].BeginSpan( );
140 T* outSI = outIt.BeginSpan( );
141 T* outSIEnd = outIt.EndSpan( );
144 while( outSI != outSIEnd )
146 // Input 0 is ignored: it is just used to guarantee sizes all over
149 for( int k = 0; k < numInputs; ++k )
150 if( double( *inSI[ k ] ) > ranges[ k << 1 ] && vmax <= k )
152 *outSI = static_cast< T >( vmax );
154 for( int l = 0; l < numInputs; ++l )
158 for( int j = 0; j < numInputs; ++j )
159 inIts[ j ].NextSpan( );
164 if( numInputs >= 256)
172 // -------------------------------------------------------------------------
173 void cpExtensions::Algorithms::ImageBlender::
175 vtkInformation* request,
176 vtkInformationVector** inputVector,
177 vtkInformationVector* outputVector,
178 vtkImageData*** inData, vtkImageData** outData,
179 int outExt[ 6 ], int id
182 if( inData[ 0 ][ 0 ] == NULL )
184 vtkErrorMacro( << "Input " << 0 << " must be specified." );
189 int numInputs = this->GetNumberOfInputConnections( 0 );
190 int scalarType = inData[ 0 ][ 0 ]->GetScalarType( );
191 int numComp = inData[ 0 ][ 0 ]->GetNumberOfScalarComponents( );
192 for( int i = 0; i < numInputs; ++i )
194 int otherType = inData[ 0 ][ i ]->GetScalarType( );
195 int otherComp = inData[ 0 ][ i ]->GetNumberOfScalarComponents( );
196 if( otherType != scalarType || otherComp != numComp )
200 "ThreadedRequestData: Input " << i
201 << " has " << otherComp << " components of type "
202 << otherType << ", but input 0 has " << numComp
203 << " components of type " << scalarType
214 cpExtensions_Algorithms_ImageBlender_Execute(
215 this, inData[ 0 ], numInputs, this->m_Ranges,
216 outData[ 0 ], outExt, id, static_cast< VTK_TT* >( 0 )
221 vtkErrorMacro( << "Execute: Unknown ScalarType" );
226 // -------------------------------------------------------------------------
227 int cpExtensions::Algorithms::ImageBlender::
228 FillInputPortInformation( int i, vtkInformation* info )
230 info->Set( vtkAlgorithm::INPUT_IS_REPEATABLE( ), 1 );
231 return( this->Superclass::FillInputPortInformation( i,info ) );