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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*=========================================================================
*
* 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 itkFlipImageFilter_hxx
#define itkFlipImageFilter_hxx
#include "itkImageScanlineIterator.h"
#include "itkTotalProgressReporter.h"
namespace itk
{
template <typename TImage>
FlipImageFilter<TImage>::FlipImageFilter()
{
m_FlipAxes.Fill(false);
this->DynamicMultiThreadingOn();
this->ThreaderUpdateProgressOff();
}
template <typename TImage>
void
FlipImageFilter<TImage>::GenerateOutputInformation()
{
// Call the superclass's implementation of this method
Superclass::GenerateOutputInformation();
// Get pointers to the input and output
InputImagePointer inputPtr = const_cast<TImage *>(this->GetInput());
OutputImagePointer outputPtr = this->GetOutput();
if (!inputPtr || !outputPtr)
{
return;
}
const typename TImage::DirectionType & inputDirection = inputPtr->GetDirection();
const typename TImage::SizeType & inputSize = inputPtr->GetLargestPossibleRegion().GetSize();
const typename TImage::IndexType & inputStartIndex = inputPtr->GetLargestPossibleRegion().GetIndex();
typename TImage::PointType outputOrigin;
typename TImage::IndexType newIndex = inputStartIndex;
typename TImage::DirectionType flipMatrix;
flipMatrix.SetIdentity();
// Need the coordinate of the pixel that will become the first pixel
// and need a matrix to model the flip
for (unsigned int j = 0; j < ImageDimension; ++j)
{
if (m_FlipAxes[j])
{
// If flipping the axis, then we need to know the last pixel in
// that dimension
newIndex[j] += (inputSize[j] - 1);
// What we really want is the index padded out past this point
// by the amount the start index is from [0,0,0] (because the
// output regions have the same index layout as the input
// regions)
newIndex[j] += inputStartIndex[j];
// Only flip the directions if we are NOT flipping about the
// origin (when flipping about the origin, the pixels are
// ordered in the same direction as the input directions. when
// NOT flipping about the origin, the pixels traverse space in
// the opposite direction. when flipping about the origin,
// increasing indices traverse space in the same direction as
// the original data.).
if (!m_FlipAboutOrigin)
{
flipMatrix[j][j] = -1.0;
}
}
}
inputPtr->TransformIndexToPhysicalPoint(newIndex, outputOrigin);
// Finally, flip about the origin if needed
if (m_FlipAboutOrigin)
{
for (unsigned int j = 0; j < ImageDimension; ++j)
{
if (m_FlipAxes[j])
{
outputOrigin[j] *= -1;
}
}
}
outputPtr->SetDirection(inputDirection * flipMatrix);
outputPtr->SetOrigin(outputOrigin);
}
template <typename TImage>
void
FlipImageFilter<TImage>::GenerateInputRequestedRegion()
{
// Call the superclass's implementation of this method
Superclass::GenerateInputRequestedRegion();
// Get pointers to the input and output
InputImagePointer inputPtr = const_cast<TImage *>(this->GetInput());
OutputImagePointer outputPtr = this->GetOutput();
if (!inputPtr || !outputPtr)
{
return;
}
const typename TImage::SizeType & outputRequestedSize = outputPtr->GetRequestedRegion().GetSize();
const typename TImage::IndexType & outputRequestedIndex = outputPtr->GetRequestedRegion().GetIndex();
const typename TImage::SizeType & outputLargestPossibleSize = outputPtr->GetLargestPossibleRegion().GetSize();
const typename TImage::IndexType & outputLargestPossibleIndex = outputPtr->GetLargestPossibleRegion().GetIndex();
IndexType inputRequestedIndex(outputRequestedIndex);
for (unsigned int j = 0; j < ImageDimension; ++j)
{
if (m_FlipAxes[j])
{
inputRequestedIndex[j] = 2 * outputLargestPossibleIndex[j] +
static_cast<IndexValueType>(outputLargestPossibleSize[j]) -
static_cast<IndexValueType>(outputRequestedSize[j]) - outputRequestedIndex[j];
}
}
typename TImage::RegionType inputRequestedRegion(inputRequestedIndex, outputRequestedSize);
inputPtr->SetRequestedRegion(inputRequestedRegion);
}
template <typename TImage>
void
FlipImageFilter<TImage>::DynamicThreadedGenerateData(const OutputImageRegionType & outputRegionForThread)
{
InputImageConstPointer inputPtr = this->GetInput();
OutputImagePointer outputPtr = this->GetOutput();
const typename TImage::SizeType & outputLargestPossibleSize = outputPtr->GetLargestPossibleRegion().GetSize();
const typename TImage::IndexType & outputLargestPossibleIndex = outputPtr->GetLargestPossibleRegion().GetIndex();
// Compute the input region the output region maps to
typename TImage::RegionType inputReginForThread(outputRegionForThread);
for (unsigned int j = 0; j < ImageDimension; ++j)
{
if (m_FlipAxes[j])
{
const IndexValueType idx =
2 * outputLargestPossibleIndex[j] + static_cast<IndexValueType>(outputLargestPossibleSize[j]) -
static_cast<IndexValueType>(outputRegionForThread.GetSize(j)) - outputRegionForThread.GetIndex(j);
inputReginForThread.SetIndex(j, idx);
}
}
// Setup region iterator
ImageScanlineConstIterator inputIter(inputPtr, inputReginForThread);
IndexValueType offset[ImageDimension];
for (unsigned int j = 0; j < ImageDimension; ++j)
{
if (m_FlipAxes[j])
{
offset[j] = 2 * outputLargestPossibleIndex[j] + static_cast<IndexValueType>(outputLargestPossibleSize[j]) - 1;
}
else
{
offset[j] = 0;
}
}
TotalProgressReporter progress(this, outputPtr->GetRequestedRegion().GetNumberOfPixels());
for (ImageScanlineIterator outputIt(outputPtr, outputRegionForThread); !outputIt.IsAtEnd(); outputIt.NextLine())
{
// Determine the index of the output line
const typename TImage::IndexType outputIndex = outputIt.GetIndex();
// Determine the input pixel location associated with the start of
// the line
typename TImage::IndexType inputIndex(outputIndex);
for (unsigned int j = 0; j < ImageDimension; ++j)
{
if (m_FlipAxes[j])
{
inputIndex[j] = -1 * outputIndex[j] + offset[j];
}
}
inputIter.SetIndex(inputIndex);
if (m_FlipAxes[0])
{
// Move the across the output scanline
while (!outputIt.IsAtEndOfLine())
{
// Copy the input pixel to the output
outputIt.Set(inputIter.Get());
++outputIt;
// Read the input scanline in reverse
--inputIter;
}
}
else
{
// Move the across the output scanline
while (!outputIt.IsAtEndOfLine())
{
// Copy the input pixel to the output
outputIt.Set(inputIter.Get());
++outputIt;
++inputIter;
}
}
progress.Completed(outputRegionForThread.GetSize()[0]);
}
}
template <typename TImage>
void
FlipImageFilter<TImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "FlipAxes: " << m_FlipAxes << std::endl;
os << indent << "FlipAboutOrigin: " << m_FlipAboutOrigin << std::endl;
}
} // namespace itk
#endif
|