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 152
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkFFTWComplexToComplexFFTImageFilter_hxx
#define itkFFTWComplexToComplexFFTImageFilter_hxx
#include "itkIndent.h"
#include "itkMetaDataObject.h"
#include "itkImageRegionIterator.h"
#include "itkProgressReporter.h"
/*
*
* This code was contributed in the Insight Journal paper:
* "FFT Complex to Complex filters and helper classes"
* by Warfield S.
* https://www.insight-journal.org/browse/publication/128
*
*/
namespace itk
{
template <typename TInputImage, typename TOutputImage>
FFTWComplexToComplexFFTImageFilter<TInputImage, TOutputImage>::FFTWComplexToComplexFFTImageFilter()
#ifndef ITK_USE_CUFFTW
: m_PlanRigor(FFTWGlobalConfiguration::GetPlanRigor())
#endif
{
this->DynamicMultiThreadingOn();
}
template <typename TInputImage, typename TOutputImage>
void
FFTWComplexToComplexFFTImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateData()
{
// get pointers to the input and output
const InputImageType * input = this->GetInput();
OutputImageType * output = this->GetOutput();
if (!input || !output)
{
return;
}
// we don't have a nice progress to report, but at least this simple line
// reports the beginning and the end of the process
ProgressReporter progress(this, 0, 1);
// allocate output buffer memory
output->SetBufferedRegion(output->GetRequestedRegion());
output->Allocate();
const typename OutputImageType::SizeType & inputSize = input->GetLargestPossibleRegion().GetSize();
int transformDirection = 1;
if (this->GetTransformDirection() == Superclass::TransformDirectionEnum::INVERSE)
{
transformDirection = -1;
}
typename FFTWProxyType::PlanType plan;
auto * in = (typename FFTWProxyType::ComplexType *)input->GetBufferPointer();
auto * out = (typename FFTWProxyType::ComplexType *)output->GetBufferPointer();
int flags = m_PlanRigor;
if (!m_CanUseDestructiveAlgorithm)
{
// if the input is about to be destroyed, there is no need to force fftw
// to use an non destructive algorithm. If it is not released however,
// we must be careful to not destroy it.
flags = flags | FFTW_PRESERVE_INPUT;
}
int sizes[ImageDimension];
for (unsigned int i = 0; i < ImageDimension; ++i)
{
sizes[(ImageDimension - 1) - i] = inputSize[i];
}
plan =
FFTWProxyType::Plan_dft(ImageDimension, sizes, in, out, transformDirection, flags, this->GetNumberOfWorkUnits());
FFTWProxyType::Execute(plan);
FFTWProxyType::DestroyPlan(plan);
}
template <typename TInputImage, typename TOutputImage>
void
FFTWComplexToComplexFFTImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(
const OutputImageRegionType & outputRegionForThread)
{
// Normalize the output if backward transform
if (this->GetTransformDirection() == Superclass::TransformDirectionEnum::INVERSE)
{
using IteratorType = ImageRegionIterator<OutputImageType>;
SizeValueType totalOutputSize = this->GetOutput()->GetRequestedRegion().GetNumberOfPixels();
IteratorType it(this->GetOutput(), outputRegionForThread);
while (!it.IsAtEnd())
{
PixelType val = it.Value();
val /= totalOutputSize;
it.Set(val);
++it;
}
}
}
template <typename TInputImage, typename TOutputImage>
void
FFTWComplexToComplexFFTImageFilter<TInputImage, TOutputImage>::UpdateOutputData(DataObject * output)
{
// we need to catch that information now, because it is changed later
// during the pipeline execution, and thus can't be grabbed in
// GenerateData().
m_CanUseDestructiveAlgorithm = this->GetInput()->GetReleaseDataFlag();
Superclass::UpdateOutputData(output);
}
template <typename TInputImage, typename TOutputImage>
void
FFTWComplexToComplexFFTImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
#ifndef ITK_USE_CUFFTW
os << indent << "PlanRigor: " << FFTWGlobalConfiguration::GetPlanRigorName(m_PlanRigor) << " (" << m_PlanRigor << ')'
<< std::endl;
#endif
}
} // end namespace itk
#endif // _itkFFTWComplexToComplexFFTImageFilter_hxx
|