21 #ifndef otbTileImageFilter_hxx
22 #define otbTileImageFilter_hxx
25 #include "itkImageRegionIterator.h"
29 template <
class TImage>
34 template <
class TImage>
39 template <
class TImage>
42 Superclass::PrintSelf(os, indent);
43 os << indent <<
"Layout: " << m_Layout << std::endl;
46 template <
class TImage>
50 Superclass::GenerateOutputInformation();
53 unsigned int numberOfImages = m_Layout[0] * m_Layout[1];
56 if (numberOfImages != this->GetNumberOfInputs())
58 itkExceptionMacro(<<
"Layout has " << numberOfImages <<
" tiles, but only " << this->GetNumberOfInputs() <<
" inputs are found.");
61 typename ImageType::SpacingType spacing = this->GetInput()->GetSignedSpacing();
62 unsigned int nbComp = this->GetInput()->GetNumberOfComponentsPerPixel();
64 m_ColumnsSizes.clear();
68 for (
unsigned int col = 0; col < m_Layout[0]; ++col)
70 for (
unsigned int row = 0; row < m_Layout[1]; ++row)
73 const ImageType* currentTile = this->GetInput(col + row * m_Layout[0]);
74 typename ImageType::SizeType currentSize = currentTile->GetLargestPossibleRegion().GetSize();
79 m_RowsSizes.push_back(currentSize[1]);
83 m_ColumnsSizes.push_back(currentSize[0]);
87 if (currentSize[1] != m_RowsSizes[row] || currentSize[0] != m_ColumnsSizes[col])
89 itkExceptionMacro(<<
"Inconsistent sizes in layout detected!");
92 if (spacing != currentTile->GetSignedSpacing())
94 itkExceptionMacro(<<
"Inconsistent spacings in layout detected!");
97 if (nbComp != currentTile->GetNumberOfComponentsPerPixel())
99 itkExceptionMacro(<<
"Inconsistent number of components in layout detected!");
105 typename ImageType::SizeType totalSize;
108 for (
unsigned int i = 0; i < m_Layout[0]; ++i)
110 totalSize[0] += m_ColumnsSizes[i];
113 for (
unsigned int i = 0; i < m_Layout[1]; ++i)
115 totalSize[1] += m_RowsSizes[i];
119 typename ImageType::RegionType outRegion;
120 outRegion.SetSize(totalSize);
124 ImageType* outputPtr = this->GetOutput();
127 outputPtr->CopyInformation(this->GetInput());
130 outputPtr->SetLargestPossibleRegion(outRegion);
133 template <
class TImage>
137 RegionType outRegion = this->GetOutput()->GetRequestedRegion();
140 unsigned int numberOfImages = m_Layout[0] * m_Layout[1];
146 for (
unsigned int i = 0; i < numberOfImages; ++i)
150 RegionType inRegion = OutputRegionToInputRegion(i, outRegion);
155 inputTile->SetRequestedRegion(inRegion);
159 template <
class TImage>
163 ImageType* outputPtr = this->GetOutput();
166 unsigned int numberOfImages = m_Layout[0] * m_Layout[1];
168 for (
unsigned int i = 0; i < numberOfImages; ++i)
170 const ImageType* inputTile = this->GetInput(i);
172 RegionType inRegion = OutputRegionToInputRegion(i, outputRegionForThread);
173 RegionType outRegion = InputRegionToOutputRegion(i, inRegion);
177 if (inRegion.GetNumberOfPixels() > 0)
183 itk::ImageRegionConstIterator<ImageType> inIt(inputTile, inRegion);
184 itk::ImageRegionIterator<ImageType> outIt(outputPtr, outRegion);
189 while (!inIt.IsAtEnd() && !outIt.IsAtEnd())
191 outIt.Set(inIt.Get());
199 template <
class TImage>
203 const ImageType* tile = this->GetInput(tileIndex);
205 unsigned int tileYIndex = tileIndex / m_Layout[0];
206 unsigned int tileXIndex = tileIndex % m_Layout[0];
209 typename RegionType::IndexType regionIndex = out2inRegion.GetIndex();
212 for (
unsigned int i = 0; i < tileXIndex; ++i)
214 regionIndex[0] -= m_ColumnsSizes.at(i);
217 for (
unsigned int i = 0; i < tileYIndex; ++i)
219 regionIndex[1] -= m_RowsSizes.at(i);
222 out2inRegion.SetIndex(regionIndex);
226 if (!out2inRegion.Crop(tile->GetLargestPossibleRegion()))
228 typename RegionType::IndexType nullIndex;
230 out2inRegion.SetIndex(nullIndex);
234 out2inRegion.SetSize(nullSize);
240 template <
class TImage>
246 unsigned int tileYIndex = tileIndex / m_Layout[0];
247 unsigned int tileXIndex = tileIndex % m_Layout[0];
250 typename RegionType::IndexType regionIndex = out2inRegion.GetIndex();
254 for (
unsigned int i = 0; i < tileXIndex; ++i)
256 regionIndex[0] += m_ColumnsSizes.at(i);
259 for (
unsigned int i = 0; i < tileYIndex; ++i)
261 regionIndex[1] += m_RowsSizes.at(i);
264 out2inRegion.SetIndex(regionIndex);
267 if (!out2inRegion.Crop(this->GetOutput()->GetLargestPossibleRegion()))
271 out2inRegion.SetSize(nullSize);