OTB  10.0.0
Orfeo Toolbox
otbCastImageFilter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkCastImageFilter_hxx
19 #define itkCastImageFilter_hxx
20 
21 #include "otbCastImageFilter.h"
22 #include "itkProgressReporter.h"
23 #include "itkImageAlgorithm.h"
24 
25 namespace otb
26 {
27 
28 
29 template< typename TInputImage, typename TOutputImage >
32 {
33  this->SetNumberOfRequiredInputs(1);
34  this->InPlaceOff();
35  this->DynamicMultiThreadingOn();
36 }
37 
38 template< typename TInputImage, typename TOutputImage >
39 void
42 {
43  if ( this->GetInPlace() && this->CanRunInPlace() )
44  {
45  // nothing to do, so avoid iterating over all the pixels
46  // for nothing! Allocate the output, generate a fake progress and exit
47  this->AllocateOutputs();
48  itk::ProgressReporter progress(this, 0, 1);
49  return;
50  }
51  //else do normal Before+Threaded+After
52  Superclass::GenerateData();
53 }
54 
55 
56 template< typename TInputImage, typename TOutputImage >
57 void
60  {
61  // do not call the superclass' implementation of this method since
62  // this filter allows the input the output to be of different dimensions
63 
64  // get pointers to the input and output
65  TOutputImage *outputPtr = this->GetOutput();
66  const TInputImage *inputPtr = this->GetInput();
67 
68  if ( !outputPtr || !inputPtr )
69  {
70  return;
71  }
72 
73  // Set the output image largest possible region. Use a RegionCopier
74  // so that the input and output images can have different dimensions.
75  OutputImageRegionType outputLargestPossibleRegion;
76  this->CallCopyInputRegionToOutputRegion( outputLargestPossibleRegion,
77  inputPtr->GetLargestPossibleRegion() );
78  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
79 
80  itk::ImageToImageFilterDetail::ImageInformationCopier<Superclass::OutputImageDimension,
81  Superclass::InputImageDimension>
82  informationCopier;
83  informationCopier(outputPtr, inputPtr);
84 
85  }
86 
87 
88 template< typename TInputImage, typename TOutputImage >
89 void
92 {
93  DynamicThreadedGenerateDataDispatched<InputPixelType>(outputRegionForThread,
94  std::is_convertible<InputPixelType, OutputPixelType>());
95 }
96 
97 template< typename TInputImage, typename TOutputImage >
98 template<typename TInputPixelType>
99 void
101 ::DynamicThreadedGenerateDataDispatched(const OutputImageRegionType & outputRegionForThread, std::true_type)
102 {
103  const TInputImage *inputPtr = this->GetInput();
104  TOutputImage *outputPtr = this->GetOutput(0);
105 
106  // Define the portion of the input to walk for this thread, using
107  // the CallCopyOutputRegionToInputRegion method allows for the input
108  // and output images to be different dimensions
109  typename TInputImage::RegionType inputRegionForThread;
110 
111  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
112 
113  itk::ImageAlgorithm::Copy( inputPtr, outputPtr, inputRegionForThread, outputRegionForThread );
114 }
115 
116 
117 template< typename TInputImage, typename TOutputImage >
118 template<typename TInputPixelType>
119 void
121 ::DynamicThreadedGenerateDataDispatched(const OutputImageRegionType & outputRegionForThread, std::false_type)
122 {
123  // Implementation for non-implicit convertible pixels which are
124  // itk-array-like.
125 
126  //static_assert( OutputPixelType::Dimension == InputPixelType::Dimension, "Vector dimensions are required to match!" );
127  static_assert( std::is_convertible<typename InputPixelType::ValueType, typename OutputPixelType::ValueType>::value, "Component types are required to be convertible." );
128 
129  const typename OutputImageRegionType::SizeType &regionSize = outputRegionForThread.GetSize();
130 
131  if( regionSize[0] == 0 )
132  {
133  return;
134  }
135  const TInputImage *inputPtr = this->GetInput();
136  TOutputImage *outputPtr = this->GetOutput(0);
137 
138  // Define the portion of the input to walk for this thread, using
139  // the CallCopyOutputRegionToInputRegion method allows for the input
140  // and output images to be different dimensions
141  typename TInputImage::RegionType inputRegionForThread;
142 
143  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
144 
145  // Define the iterators
146  itk::ImageScanlineConstIterator< TInputImage > inputIt(inputPtr, inputRegionForThread);
147  itk::ImageScanlineIterator< TOutputImage > outputIt(outputPtr, outputRegionForThread);
148 
149  inputIt.GoToBegin();
150  outputIt.GoToBegin();
151  OutputPixelType value;
152 
153  while ( !inputIt.IsAtEnd() )
154  {
155  while ( !inputIt.IsAtEndOfLine() )
156  {
157  const InputPixelType &inputPixel = inputIt.Get();
158  for ( unsigned int k = 0; k < OutputPixelType::Dimension; k++ )
159  {
160  value[k] = static_cast< typename OutputPixelType::ValueType >( inputPixel[k] );
161  }
162  outputIt.Set( value );
163 
164  ++inputIt;
165  ++outputIt;
166  }
167  inputIt.NextLine();
168  outputIt.NextLine();
169  }
170 
171 }
172 
173 } // end namespace otb
174 
175 #endif
void DynamicThreadedGenerateDataDispatched(const OutputImageRegionType &outputRegionForThread, std::true_type isConvertible)
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
typename Superclass::OutputImageRegionType OutputImageRegionType
typename TInputImage::PixelType InputPixelType
typename TOutputImage::PixelType OutputPixelType
void GenerateOutputInformation() override
void GenerateData() override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.