This file is indexed.

/usr/include/InsightToolkit/IO/itkImageSeriesReader.txx is in libinsighttoolkit3-dev 3.20.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
426
427
428
429
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkImageSeriesReader.txx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkImageSeriesReader_txx
#define __itkImageSeriesReader_txx

#include "itkImageSeriesReader.h"

#include "itkImageRegion.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkExceptionObject.h"
#include "itkArray.h"
#include "vnl/vnl_math.h"
#include "itkProgressReporter.h"
#include "itkMetaDataObject.h"

namespace itk
{

// Destructor
template <class TOutputImage>
ImageSeriesReader<TOutputImage>
::~ImageSeriesReader()
{
  // Clear the eventual previous content of the MetaDictionary array
  if( m_MetaDataDictionaryArray.size() )
    {
    for(unsigned int i=0; i<m_MetaDataDictionaryArray.size(); i++)
      {
      // each element is a raw pointer, delete them.
      delete m_MetaDataDictionaryArray[i];
      }
    }
  m_MetaDataDictionaryArray.clear();
}


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

  os << indent << "ReverseOrder: " << m_ReverseOrder << std::endl;
  os << indent << "UseStreaming: " << m_UseStreaming << std::endl;

  if (m_ImageIO)
    {
    os << indent << "ImageIO: \n";
    m_ImageIO->Print(os, indent.GetNextIndent());
    }
  else
    {
    os << indent << "ImageIO: (null)" << "\n";
    }

  os << indent << "MetaDataDictionaryArrayMTime: " <<  m_MetaDataDictionaryArrayMTime  << std::endl;
  os << indent << "MetaDataDictionaryArrayUpdate: " << m_MetaDataDictionaryArrayUpdate << std::endl;
}


template <class TOutputImage>
int ImageSeriesReader<TOutputImage>
::ComputeMovingDimensionIndex( ReaderType * reader )
{
  // This method computes the the diminesion index which we are going
  // to be moving in for slices

  int movingDimension = reader->GetImageIO()->GetNumberOfDimensions();

  if (movingDimension > TOutputImage::ImageDimension - 1)
    {
    movingDimension = TOutputImage::ImageDimension - 1;
    }

  SizeType dimSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();

  // collapse the number of dimensions in image if any of the last
  // dimensions are one
  while (movingDimension > 0 && dimSize[movingDimension-1] == 1)
    {
    --movingDimension;
    }

  return movingDimension;
}


template <class TOutputImage>
void ImageSeriesReader<TOutputImage>
::GenerateOutputInformation(void)
{
  typename TOutputImage::Pointer output = this->GetOutput();

  Array<float> position1(TOutputImage::ImageDimension); position1.Fill(0.0f);
  Array<float> position2(TOutputImage::ImageDimension); position2.Fill(0.0f);

  ImageRegionType largestRegion;
  typename TOutputImage::SpacingType   spacing;
  typename TOutputImage::PointType     origin;
  typename TOutputImage::DirectionType direction;

  origin.Fill(0.0);

  std::string key("ITK_ImageOrigin");

  // Clear the previous content of the MetaDictionary array
  if( m_MetaDataDictionaryArray.size() )
    {
    for(unsigned int i=0; i<m_MetaDataDictionaryArray.size(); i++)
      {
      // each element is a raw pointer, delete them.
      delete m_MetaDataDictionaryArray[i];
      }
    }
  m_MetaDataDictionaryArray.clear();

  if (m_FileNames.size() == 0)
    {
    itkExceptionMacro(<< "At least one filename is required." );
    }


  const int numberOfFiles = static_cast<int>(m_FileNames.size());
  for ( int i = 0; i < 2 && i < numberOfFiles; ++i )
    {
    const int iFileName = ( m_ReverseOrder ? numberOfFiles - i - 1: i );

    typename ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName( m_FileNames[iFileName].c_str() );
    if ( m_ImageIO )
      {
      reader->SetImageIO( m_ImageIO );
      }

    // update the MetaDataDictionary and output information
    reader->UpdateOutputInformation();

    if (m_FileNames.size() == 1)
      {
      // ----------------------------
      // there is only one file need to copy all of it's meta data
      spacing = reader->GetOutput()->GetSpacing();
      origin = reader->GetOutput()->GetOrigin();
      direction = reader->GetOutput()->GetDirection();
      largestRegion = reader->GetOutput()->GetLargestPossibleRegion();
      // the slice moving direction for a single image can be the
      // output image dimensions, since this will indicate that we can
      // not move in the slice moving direction
      this->m_NumberOfDimensionsInImage = reader->GetImageIO()->GetNumberOfDimensions();
      if ( this->m_NumberOfDimensionsInImage > TOutputImage::ImageDimension )
        {
        this->m_NumberOfDimensionsInImage = TOutputImage::ImageDimension;
        }

      }
    else if (i == 0) 
      {
      // ----------------------------
      // first of multiple slices
      
      spacing = reader->GetOutput()->GetSpacing();
      direction = reader->GetOutput()->GetDirection(); 
      
      SizeType dimSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();

      // compute the moving dimensions index, or the number of image
      // dimensions we are going to use
      this->m_NumberOfDimensionsInImage = ComputeMovingDimensionIndex( reader );

      dimSize[this->m_NumberOfDimensionsInImage] = m_FileNames.size();
      
      IndexType start;
      start.Fill(0);
      largestRegion.SetSize(dimSize);
      largestRegion.SetIndex(start);

      // Initialize the position to the origin returned by the reader
      unsigned int j;
      for (j = 0; j < TOutputImage::ImageDimension; j++)
        {
        position1[j] = static_cast<float>(reader->GetOutput()->GetOrigin()[j]);
        }
      // Override the position if there is an ITK_ImageOrigin 
      ExposeMetaData< Array<float> > ( reader->GetImageIO()->GetMetaDataDictionary(), key, position1 );
    

      for (j = 0; j < TOutputImage::ImageDimension; j++)
        {
        if (j < position1.size())
          {
          origin[j] = position1[j];
          }
        else
          {
          origin[j] = static_cast<float>(reader->GetOutput()->GetOrigin()[j]);
          }
        }
      }
    else if (i == 1) 
      {
      // ----------------------------
      // second of multiple slices

      // Initialize the position to the origin returned by the reader
      unsigned int j;
      for (j = 0; j < TOutputImage::ImageDimension; j++)
        {
        position2[j] = static_cast<float>(reader->GetOutput()->GetOrigin()[j]);
        }
      // Override the position if there is an ITK_ImageOrigin 
      ExposeMetaData< Array<float> > ( reader->GetImageIO()->GetMetaDataDictionary(), key, position2 );
    

      // Compute the inter slice spacing by computing the distance
      // between two consective slices
      float interSliceSpacing = 0.0f;
      for (j = 0; j < position1.size(); ++j) 
        {
        interSliceSpacing += vnl_math_sqr(position2[j] - position1[j]);
        }
      interSliceSpacing = static_cast<float>(vcl_sqrt(interSliceSpacing));
          
      if (interSliceSpacing == 0.0f)
        {
        interSliceSpacing = 1.0f;
        }
        
      // set interslice spacing
      spacing[this->m_NumberOfDimensionsInImage] = interSliceSpacing;
        
      }

    }
    

  output->SetOrigin( origin );     // Set the image origin
  output->SetSpacing( spacing );   // Set the image spacing
  output->SetDirection( direction );  // Set the image direction
  output->SetLargestPossibleRegion( largestRegion );
 
}


template <class TOutputImage>
void
ImageSeriesReader<TOutputImage>
::EnlargeOutputRequestedRegion(DataObject *output)
{
  typename TOutputImage::Pointer out = dynamic_cast<TOutputImage*>(output);
  ImageRegionType requestedRegion = out->GetRequestedRegion();
  ImageRegionType largestRegion = out->GetLargestPossibleRegion();

  if (m_UseStreaming) 
    {   
    out->SetRequestedRegion( requestedRegion );
    } 
  else
    {
    out->SetRequestedRegion( largestRegion );
    }
}


template <class TOutputImage>
void ImageSeriesReader<TOutputImage>
::GenerateData()
{
  TOutputImage * output = this->GetOutput();
  
  ImageRegionType requestedRegion = output->GetRequestedRegion();
  ImageRegionType largestRegion = output->GetLargestPossibleRegion();
  ImageRegionType sliceRegionToRequest = output->GetRequestedRegion();

  // Each file must have the same size.
  SizeType validSize = largestRegion.GetSize();

  // If more than one file is being read, then the input dimension
  // will be less than the output dimension.  In this case, set
  // the last dimension that is other than 1 of validSize to 1.  However, if the
  // input and output have the same number of dimensions, this should
  // not be done because it will lower the dimension of the output image.
  if (TOutputImage::ImageDimension != this->m_NumberOfDimensionsInImage)
    {
    validSize[this->m_NumberOfDimensionsInImage] = 1;
    sliceRegionToRequest.SetSize( this->m_NumberOfDimensionsInImage, 1 );
    sliceRegionToRequest.SetIndex( this->m_NumberOfDimensionsInImage, 0 );
    }

  // Allocate the output buffer
  output->SetBufferedRegion( requestedRegion );
  output->Allocate();

  ProgressReporter progress( this, 0, 
                            requestedRegion.GetNumberOfPixels(),
                            100 );

  // We utilize the modified time of the output information to
  // know when the meta array needs to be updated, when the output
  // information is updated so should the meta array.
  // Each file can not be read in the UpdateOutputInformation methods
  // due to the poor performance of reading each file a second time there.
  bool needToUpdateMetaDataDictionaryArray = 
    this->m_OutputInformationMTime > this->m_MetaDataDictionaryArrayMTime &&
     m_MetaDataDictionaryArrayUpdate;


  ImageRegionIterator<TOutputImage> ot (output, requestedRegion );
  IndexType sliceStartIndex = requestedRegion.GetIndex();
  const int numberOfFiles = static_cast<int>(m_FileNames.size());
  for ( int i = 0; i != numberOfFiles; ++i )
    {
    
    if (TOutputImage::ImageDimension != this->m_NumberOfDimensionsInImage)
      {
      sliceStartIndex[this->m_NumberOfDimensionsInImage] = i;
      }

    const bool insideRequestedRegion = requestedRegion.IsInside(sliceStartIndex);
    const int iFileName = ( m_ReverseOrder ? numberOfFiles - i - 1: i );

    // check if we need this slice
    if( !insideRequestedRegion && !needToUpdateMetaDataDictionaryArray ) 
      {
      continue;
      }
    

    // configure reader 
    typename ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName( m_FileNames[iFileName].c_str() );
    if ( m_ImageIO )
      {
      reader->SetImageIO( m_ImageIO );
      }
    reader->SetUseStreaming( m_UseStreaming );
    reader->GetOutput()->SetRequestedRegion( sliceRegionToRequest );

    // update the data or info
    if( !insideRequestedRegion ) 
      {
      reader->UpdateOutputInformation();
      }
    else
      {
      reader->Update();
      }

    // Deep copy the MetaDataDictionary into the array
    if ( reader->GetImageIO() &&  needToUpdateMetaDataDictionaryArray )
      {
      DictionaryRawPointer newDictionary = new DictionaryType;
      *newDictionary = reader->GetImageIO()->GetMetaDataDictionary();
      m_MetaDataDictionaryArray.push_back( newDictionary );
      }

    // if we only needed the info continue to next slice
    if ( !insideRequestedRegion )
      {
      continue;
      }
    
    if ( reader->GetOutput()->GetLargestPossibleRegion().GetSize() != validSize )
      {
      itkExceptionMacro(<< "Size mismatch! The size of  " 
                        << m_FileNames[iFileName].c_str()
                        << " is " 
                        << reader->GetOutput()->GetLargestPossibleRegion().GetSize()
                        << " and does not match the required size "
                        << validSize
                        << " from file " 
                        << m_FileNames[m_ReverseOrder ? m_FileNames.size()-1 : 0].c_str());
        }
    
    
    // set the iterator for this slice
    ot.SetIndex( sliceStartIndex );
    
    ImageRegionConstIterator<TOutputImage> it (reader->GetOutput(),
                                               sliceRegionToRequest);
    while (!it.IsAtEnd())
      {
      ot.Set(it.Get());
      ++it;
      ++ot;
      progress.CompletedPixel();
      }
    }

  // update the time if we modified the meta array
  if ( needToUpdateMetaDataDictionaryArray )
    {
    m_MetaDataDictionaryArrayMTime.Modified();
    }
}


template <class TOutputImage>
typename 
ImageSeriesReader<TOutputImage>::DictionaryArrayRawPointer 
ImageSeriesReader<TOutputImage>
::GetMetaDataDictionaryArray() const
{
  // this warning has been introduced in 3.17 due to a change in
  // behavior. It may be removed in the future.
  if (this->m_OutputInformationMTime > this->m_MetaDataDictionaryArrayMTime )
    {
    itkWarningMacro( "The MetaDataDictionaryArray is not up to date. This is no longer updated in the UpdateOutputInformation method but in GenerateData.") 
    }
  return & m_MetaDataDictionaryArray;
}

} //namespace ITK

#endif