File: itkTileImageFilter.hxx

package info (click to toggle)
insighttoolkit4 4.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 408,860 kB
  • ctags: 151,204
  • sloc: cpp: 633,356; ansic: 403,038; xml: 51,513; fortran: 34,250; python: 15,831; sh: 2,501; lisp: 2,070; tcl: 1,035; java: 710; makefile: 605; yacc: 323; perl: 200; csh: 195; lex: 177; pascal: 69; cs: 35; ruby: 10
file content (354 lines) | stat: -rw-r--r-- 11,078 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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
 *
 *         http://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 __itkTileImageFilter_hxx
#define __itkTileImageFilter_hxx
#include "itkTileImageFilter.h"

#include "itkMacro.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageRegionIterator.h"
#include "itkImageLinearConstIteratorWithIndex.h"
#include "itkPasteImageFilter.h"
#include "itkImportImageContainer.h"
#include "itkNumericTraitsRGBPixel.h"
#include "itkProgressAccumulator.h"

namespace itk
{
template< typename TInputImage, typename TOutputImage >
TileImageFilter< TInputImage, TOutputImage >
::TileImageFilter()
{
  m_Layout.Fill(0);
  m_DefaultPixelValue = NumericTraits< OutputPixelType >::ZeroValue();
}

template< typename TInputImage, typename TOutputImage >
void
TileImageFilter< TInputImage, TOutputImage >
::GenerateData()
{
  ProgressAccumulator::Pointer progress = ProgressAccumulator::New();
  progress->SetMiniPipelineFilter(this);

  typename TOutputImage::Pointer output = this->GetOutput();


  typedef Image< InputPixelType, OutputImageDimension > TempImageType;

  // Allocate the output and initialize to default value
  this->AllocateOutputs();
  output->FillBuffer(m_DefaultPixelValue);

  ImageRegionIterator< TileImageType > it( m_TileImage, m_TileImage->GetBufferedRegion() );
  it.GoToBegin();

  SizeValueType numPastes = 0;
  while ( !it.IsAtEnd() )
    {
    if ( it.Get().m_ImageNumber >= 0 )
      {
      ++numPastes;
      }
    ++it;
    }
  const float progressContrib = 1.0f/numPastes;

  it.GoToBegin();
  while ( !it.IsAtEnd() )
    {
    if ( it.Get().m_ImageNumber >= 0 )
      {
      typename PasteImageFilter< TOutputImage,
                                 TempImageType >::Pointer paste = PasteImageFilter< TOutputImage, TempImageType >::New();
      paste->SetDestinationImage(output);
      paste->InPlaceOn();

      progress->RegisterInternalFilter(paste, progressContrib);

      // Create a temporary image that has the same dimensions as the
      // output image. The additional dimensions are set to 1. The
      // temporary image will use the same container as the input
      // image. This way we avoid copying the data.
      typename TempImageType::Pointer tempImage = TempImageType::New();
      tempImage->CopyInformation( output );

      OutputSizeType  tempSize;
      OutputIndexType tempIndex;
      for ( unsigned int i = 0; i < InputImageDimension; i++ )
        {
        tempSize[i] = this->GetInput(it.Get().m_ImageNumber)->GetBufferedRegion().GetSize()[i];
        tempIndex[i] = this->GetInput(it.Get().m_ImageNumber)->GetBufferedRegion().GetIndex()[i];
        }
      for ( unsigned int i = InputImageDimension; i < OutputImageDimension; i++ )
        {
        tempSize[i] = 1;
        tempIndex[i] = 0;
        }
      OutputImageRegionType tempRegion(tempIndex, tempSize);
      tempImage->SetRegions( tempRegion );

      const TInputImage * inputImage = this->GetInput( it.Get().m_ImageNumber );

      typedef ImportImageContainer< SizeValueType, InputPixelType > PixelContainerType;

      tempImage->SetPixelContainer( const_cast< PixelContainerType * >( inputImage->GetPixelContainer() ) );

      paste->SetSourceImage(tempImage);
      paste->SetDestinationIndex( it.Get().m_Region.GetIndex() );
      paste->SetSourceRegion(tempRegion);
      paste->Update();
      output = paste->GetOutput();
      }
    ++it;
    }
  this->GraftOutput(output);
}

template< typename TInputImage, typename TOutputImage >
void
TileImageFilter< TInputImage, TOutputImage >
::GenerateInputRequestedRegion()
{
  for ( unsigned int i = 0; i < this->GetNumberOfIndexedInputs(); i++ )
    {
    TInputImage *input = const_cast< TInputImage * >( this->GetInput(i) );
    if ( input )
      {
      input->SetRequestedRegionToLargestPossibleRegion();
      }
    }
}

template< typename TInputImage, typename TOutputImage >
void
TileImageFilter< TInputImage, TOutputImage >
::GenerateOutputInformation()
{
  // Supply spacing, origin and LargestPossibleRegion for the output.
  // This method does most of the work for this filter. In addition to
  // the spacing, origin and region, this method computes the source
  // region and destination index for multiple invocations of the
  // PasteImageFilter.
  OutputImagePointer outputPtr = this->GetOutput();
  InputImagePointer  inputPtr = const_cast< TInputImage * >( this->GetInput() );

  if ( !outputPtr || !inputPtr )
    {
    return;
    }

  // Spacing(Origin): use the spacing(origin) of the first input for
  // all of the matching output dimensions. For remaining dimensions
  // use 1.0 (0.0).
  float spacing[OutputImageDimension];
  float origin[OutputImageDimension];

  for ( unsigned i = 0; i < OutputImageDimension; i++ )
    {
    if ( i < InputImageDimension )
      {
      spacing[i] = this->GetInput(0)->GetSpacing()[i];
      origin[i] = this->GetInput(0)->GetOrigin()[i];
      }
    else
      {
      spacing[i] = 1.0;
      origin[i] = 0.0;
      }
    }

  outputPtr->SetSpacing (spacing);
  outputPtr->SetOrigin (origin);

  // Create an image that has one element per tile. Each element of
  // this tile image will hold a class that defines the image number
  // for the tile, the size of the image and the destination index for
  // the image in the final composed image.
  typedef ImageRegion< OutputImageDimension > RegionType;
  m_TileImage = TileImageType::New();

  // Determine the last dimension for the tile image. This dimension will
  // be large enough to accommodate left-over images.
  OutputSizeType  outputSize; outputSize.Fill(1);
  OutputIndexType outputIndex; outputIndex.Fill(0);

  if ( m_Layout[OutputImageDimension - 1] == 0 )
    {
    int used = 1;
    for ( unsigned int d = 0; d < OutputImageDimension - 1; d++ )
      {
      used *= m_Layout[d];
      }
    outputSize[OutputImageDimension - 1] = ( this->GetNumberOfIndexedInputs() - 1 ) / used + 1;
    if ( outputSize[OutputImageDimension - 1] < 1 )
      {
      outputSize[OutputImageDimension - 1] = 1;
      }
    m_Layout[OutputImageDimension - 1] = outputSize[OutputImageDimension - 1];
    }

  OutputSizeType  tileSize; tileSize.Fill(1);
  OutputIndexType tileIndex; tileIndex.Fill(0);

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

  // Determine the size of the output. Each "row" size is determined
  // and the maximum size for each "row" will be the size for that
  // dimension.
  RegionType tileRegion(tileIndex, tileSize);
  m_TileImage->SetRegions(tileRegion);
  m_TileImage->Allocate();

  // Initialize the tile image with an image number for each tile. If
  // there is no corresponding input image for a tile, then set the
  // image number to -1.
  ImageRegionIteratorWithIndex< TileImageType > it( m_TileImage, m_TileImage->GetBufferedRegion() );

  it.GoToBegin();
  unsigned int input = 0;
  TileInfo     info;
  while ( !it.IsAtEnd() )
    {
    if ( input < this->GetNumberOfIndexedInputs() )
      {
      info.m_ImageNumber = input;
      it.Set (info);
      }
    else
      {
      info.m_ImageNumber = -1;
      it.Set (info);
      }
    ++input;
    ++it;
    }

  // Find the size of the largest cell for each "row" in each dimension.
  ImageLinearConstIteratorWithIndex< TileImageType > tit( m_TileImage, m_TileImage->GetRequestedRegion() );
  int                                                value;

  std::vector< std::vector< int > > sizes, offsets;

  sizes.resize(OutputImageDimension);
  offsets.resize(OutputImageDimension);
  for ( unsigned int i = 0; i < OutputImageDimension; i++ )
    {
    offsets[i].resize(m_Layout[i]);
    sizes[i].resize(m_Layout[i]);
    for ( unsigned int l = 0; l < m_Layout[i]; l++ )
      {
      sizes[i][l] = 1;
      }
    }
  for ( unsigned int i = 0; i < OutputImageDimension; i++ )
    {
    tit.SetDirection(i);
    tit.GoToBegin();
    while ( !tit.IsAtEnd() )
      {
      int dsize;
      int count = 0;
      while ( !tit.IsAtEndOfLine() )
        {
        value = tit.Get().m_ImageNumber;
        if ( value != -1 )
          {
          if ( i < InputImageDimension )
            {
            dsize = this->GetInput(value)->GetLargestPossibleRegion().GetSize()[i];
            if ( dsize > sizes[i][count] )
              {
              sizes[i][count] = dsize;
              }
            }
          }
        ++tit;
        ++count;
        }
      tit.NextLine();
      }
    }

  // Convert the sizes to offsets.
  for ( unsigned int i = 0; i < OutputImageDimension; i++ )
    {
    offsets[i][0] = 0;
    for ( unsigned int t = 0; t < m_Layout[i] - 1; t++ )
      {
      offsets[i][t + 1] = offsets[i][t] + sizes[i][t];
      }
    // The size for each dimension is the value of the last offset
    outputSize[i] = offsets[i][m_Layout[i] - 1] + sizes[i][m_Layout[i] - 1];
    }

  // Now create an region for each tile that has an image
  it.GoToBegin();
  while ( !it.IsAtEnd() )
    {
    value = it.Get().m_ImageNumber;
    if ( value >= 0 )
      {
      typename TileImageType::IndexType tileIndex2 = it.GetIndex();

      OutputSizeType  regionSize;
      OutputIndexType regionIndex;
      for ( unsigned int i = 0; i < OutputImageDimension; i++ )
        {
        regionIndex[i] = offsets[i][tileIndex2[i]];
        if ( i < InputImageDimension )
          {
          regionSize[i] = this->GetInput(value)->GetLargestPossibleRegion().GetSize()[i];
          }
        else
          {
          regionSize[i] = 1;
          }
        }
      OutputImageRegionType region(regionIndex, regionSize);
      info = it.Get();
      info.m_Region = region;
      it.Set(info);
      }
    ++it;
    }

  typename TOutputImage::RegionType outputLargestPossibleRegion;

  outputLargestPossibleRegion.SetSize(outputSize);
  outputLargestPossibleRegion.SetIndex(outputIndex);
  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
}

template< typename TInputImage, typename TOutputImage >
void
TileImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  std::cout << "DefaultPixelValue: "
            << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( m_DefaultPixelValue )
            << std::endl;
  std::cout << "Layout: " << m_Layout << std::endl;
}
} // end namespace itk
#endif