File: itkTileImageFilter.hxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (425 lines) | stat: -rw-r--r-- 13,733 bytes parent folder | download | duplicates (3)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*=========================================================================
 *
 *  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"
#include "itkDefaultConvertPixelTraits.h"

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

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 typename TInputImage::template Rebind<InputPixelType, OutputImageDimension>::Type TempImageType;

  OutputPixelType defaultPixelValue = m_DefaultPixelValue;
  if (NumericTraits<OutputPixelType>::GetLength(defaultPixelValue) == 0)
    {
    const OutputPixelComponentType zeroComponent = NumericTraits<OutputPixelComponentType>::ZeroValue();
    const unsigned int nComponents = output->GetNumberOfComponentsPerPixel();
    NumericTraits<OutputPixelType>::SetLength(defaultPixelValue, nComponents );
    for (unsigned int n=0; n<nComponents; n++)
      {
      DefaultConvertPixelTraits<OutputPixelType>::SetNthComponent( n,
                                                                   defaultPixelValue,
                                                                   zeroComponent );
      }
    }

  // Allocate the output and initialize to default value
  this->AllocateOutputs();
  output->FillBuffer(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 typename TempImageType::PixelContainer 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()
{

  // Do not call the superclass's GenerateOutptuInformation method.
  // The input images are likely a different dimension than the input,
  // so the superclass's implementation is not compatible.

  // 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.
  TOutputImage * outputPtr = this->GetOutput();
  TInputImage *  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).
  SpacePrecisionType spacing[OutputImageDimension];
  SpacePrecisionType 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] = ( static_cast<SizeValueType>( 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);

  // Support VectorImages by setting number of components on output.
  const unsigned int numComponents = inputPtr->GetNumberOfComponentsPerPixel();
  if ( numComponents != outputPtr->GetNumberOfComponentsPerPixel() )
    {
    outputPtr->SetNumberOfComponentsPerPixel( numComponents );
    }
}

template< typename TInputImage, typename TOutputImage >
void
TileImageFilter< TInputImage, TOutputImage >
::VerifyInputInformation()
{

  // Do not call superclass's VerifyInputInformation method.
  // Intentionally do not verify input's spacing, direction or origin
  // since the images are going to be tiled in some way which likely
  // has no physical location meaning.

  // Verify that all input have the same number of components

  typename TInputImage::ConstPointer image = this->GetInput();

  if( image.IsNull() )
    {
    itkExceptionMacro( << "Input not set as expected!" );
    }

  const unsigned int numComponents = image->GetNumberOfComponentsPerPixel();

  typename Superclass::DataObjectPointerArraySizeType idx = 1;

  for(; idx < this->GetNumberOfIndexedInputs(); ++idx )
    {
    image = this->GetInput(idx);

    // If the input was not set it could still be null
    if( image.IsNull() )
      {
      // An invalid requested region exception will be generated later.
      continue;
      }


    if( numComponents != image->GetNumberOfComponentsPerPixel() )
      {
      itkExceptionMacro( << "Primary input has " << numComponents << " numberOfComponents "
                         << "but input " << idx << " has "
                         << image->GetNumberOfComponentsPerPixel() << "!" );
      }
    }
}

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

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