File: otbTileImageFilter.txx

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (292 lines) | stat: -rw-r--r-- 7,605 bytes parent folder | download
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef otbTileImageFilter_txx
#define otbTileImageFilter_txx

#include "otbTileImageFilter.h"
#include "itkImageRegionIterator.h"

namespace otb
{
template <class TImage>
TileImageFilter<TImage>
::TileImageFilter()
{}

template <class TImage>
TileImageFilter<TImage>
::~TileImageFilter()
{}

template <class TImage>
void
TileImageFilter<TImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  Superclass::PrintSelf(os,indent);
  os<<indent<<"Layout: "<<m_Layout<<std::endl;
}

template <class TImage>
void
TileImageFilter<TImage>
::GenerateOutputInformation()
{
  // First, call superclass implementation
  Superclass::GenerateOutputInformation();

  // Get the number of images
  unsigned int numberOfImages = m_Layout[0] * m_Layout[1];

  // Check we have enough inputs
  if(numberOfImages != this->GetNumberOfInputs())
    {
    itkExceptionMacro(<<"Layout has "<<numberOfImages<<" tiles, but only "<<this->GetNumberOfInputs()<<" inputs are found.");
    }

  typename ImageType::SpacingType spacing = this->GetInput()->GetSpacing();
  unsigned int nbComp = this->GetInput()->GetNumberOfComponentsPerPixel();

  m_ColumnsSizes.clear();
  m_RowsSizes.clear();

  // Loop on the layout
  for(unsigned int col = 0; col < m_Layout[0]; ++col)
    {
    for(unsigned int row = 0; row < m_Layout[1]; ++row)
      {
      // First, get current tile
      const ImageType * currentTile = this->GetInput(col + row * m_Layout[0]);
      typename ImageType::SizeType currentSize = currentTile->GetLargestPossibleRegion().GetSize();

      // Retrieve row and column sizes
      if(col == 0)
        {
        m_RowsSizes.push_back(currentSize[1]);
        }
      if(row == 0)
        {
        m_ColumnsSizes.push_back(currentSize[0]);
        }

      // Check for consistent layout
      if(currentSize[1] != m_RowsSizes[row]
         || currentSize[0] != m_ColumnsSizes[col])
        {
        itkExceptionMacro(<<"Inconsistent sizes in layout detected!");
        }

      if(spacing != currentTile->GetSpacing())
        {
        itkExceptionMacro(<<"Inconsistent spacings in layout detected!");
        }

      if(nbComp != currentTile->GetNumberOfComponentsPerPixel())
        {
        itkExceptionMacro(<<"Inconsistent number of components in layout detected!");
        }
      }
    }

  // Compute total size
  typename ImageType::SizeType totalSize;
  totalSize.Fill(0);

  for(unsigned int i = 0; i < m_Layout[0]; ++i)
    {
    totalSize[0]+=m_ColumnsSizes[i];
    }

  for(unsigned int i = 0; i < m_Layout[1]; ++i)
    {
    totalSize[1]+=m_RowsSizes[i];
    }

  // Fill out output image settings
  typename ImageType::RegionType outRegion;
  outRegion.SetSize(totalSize);

  //std::cout<<"Output largest region: "<<outRegion<<std::endl;

  ImageType * outputPtr = this->GetOutput();

  // Copy information from first tile
  outputPtr->CopyInformation(this->GetInput());

  // Set region
  outputPtr->SetLargestPossibleRegion(outRegion);
}

template <class TImage>
void
TileImageFilter<TImage>
::GenerateInputRequestedRegion()
{
  // Get output requested region
  RegionType outRegion = this->GetOutput()->GetRequestedRegion();

  // Loop on al input images
  unsigned int numberOfImages = m_Layout[0] * m_Layout[1];

  //std::cout<<"Number of input images: "<<this->GetNumberOfInputs()<<std::endl;

  //std::cout<<"Out region: "<<outRegion<<std::endl;

  for(unsigned int i = 0; i < numberOfImages; ++i)
    {
    ImageType * inputTile = const_cast<ImageType *>(this->GetInput(i));

    RegionType inRegion = OutputRegionToInputRegion(i,outRegion);

    //std::cout<<"In region: "<<i<<", "<<inRegion<<std::endl;
    //std::cout<<"tile pointer: "<<inputTile<<std::endl;

    inputTile->SetRequestedRegion(inRegion);
    }
}

template <class TImage>
void
TileImageFilter<TImage>
::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType itkNotUsed(threadId))
{
  // Retrieve output image pointer
  ImageType * outputPtr = this->GetOutput();

  // Loop on all input tiles
  unsigned int numberOfImages = m_Layout[0] * m_Layout[1];

  for(unsigned int i = 0; i < numberOfImages; ++i)
    {
    const ImageType * inputTile = this->GetInput(i);

    RegionType inRegion = OutputRegionToInputRegion(i,outputRegionForThread);
    RegionType outRegion = InputRegionToOutputRegion(i,inRegion);

    //std::cout<<"[thread] outRegion: "<<outRegion<<std::endl;

    if(inRegion.GetNumberOfPixels() > 0)
      {
      //std::cout<<"[thread] "<<i<<" inRegion: "<<inRegion<<std::endl;

      //std::cout<<"[thread] "<<i<< inputTile->GetBufferedRegion()<<std::endl;

      itk::ImageRegionConstIterator<ImageType> inIt(inputTile,inRegion);
      itk::ImageRegionIterator<ImageType>      outIt(outputPtr,outRegion);

      inIt.GoToBegin();
      outIt.GoToBegin();

      while(!inIt.IsAtEnd() && !outIt.IsAtEnd())
        {
        outIt.Set(inIt.Get());
        ++inIt;
        ++outIt;
        }
      }
    }
}

template <class TImage>
typename TileImageFilter<TImage>
::RegionType
TileImageFilter<TImage>
::OutputRegionToInputRegion(unsigned int tileIndex, const RegionType & requestedRegion)
{
  // Retrieve the nth tile pointer
  const ImageType * tile = this->GetInput(tileIndex);

  unsigned int tileYIndex = tileIndex / m_Layout[0];
  unsigned int tileXIndex = tileIndex % m_Layout[0];

  RegionType out2inRegion = requestedRegion;
  typename RegionType::IndexType regionIndex = out2inRegion.GetIndex();

  // Compute tile offsets
  for(unsigned int i = 0; i < tileXIndex; ++i)
    {
    regionIndex[0]-=m_ColumnsSizes.at(i);
    }

  for(unsigned int i = 0; i < tileYIndex; ++i)
    {
    regionIndex[1]-=m_RowsSizes.at(i);
    }

  out2inRegion.SetIndex(regionIndex);


  // Crop input region
  if(!out2inRegion.Crop(tile->GetLargestPossibleRegion()))
    {
    typename RegionType::IndexType nullIndex;
    nullIndex.Fill(0);
    out2inRegion.SetIndex(nullIndex);

    SizeType nullSize;
    nullSize.Fill(0);
    out2inRegion.SetSize(nullSize);
    }

  return out2inRegion;
}

template <class TImage>
typename TileImageFilter<TImage>
::RegionType
TileImageFilter<TImage>
::InputRegionToOutputRegion(unsigned int tileIndex, const RegionType & requestedRegion)
{
  // Retrieve the nth tile pointer
//  const ImageType * tile = this->GetInput(tileIndex);

  unsigned int tileYIndex = tileIndex / m_Layout[0];
  unsigned int tileXIndex = tileIndex % m_Layout[0];

  RegionType out2inRegion = requestedRegion;
  typename RegionType::IndexType regionIndex = out2inRegion.GetIndex();


  // Compute tile offsets
  for(unsigned int i = 0; i < tileXIndex; ++i)
    {
    regionIndex[0]+=m_ColumnsSizes.at(i);
    }

  for(unsigned int i = 0; i < tileYIndex; ++i)
    {
    regionIndex[1]+=m_RowsSizes.at(i);
    }

  out2inRegion.SetIndex(regionIndex);

  // Crop input region
  if(!out2inRegion.Crop(this->GetOutput()->GetLargestPossibleRegion()))
    {
    SizeType nullSize;
    nullSize.Fill(0);
    out2inRegion.SetSize(nullSize);
    }

  return out2inRegion;
}


} // end namepsace otb

#endif