This file is indexed.

/usr/include/ITK-4.9/itkSharedMorphologyUtilities.hxx is in libinsighttoolkit4-dev 4.9.0-4ubuntu1.

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
/*=========================================================================
 *
 *  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 itkSharedMorphologyUtilities_hxx
#define itkSharedMorphologyUtilities_hxx

#include "itkSharedMorphologyUtilities.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkImageRegionConstIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include <list>

namespace itk
{
/**
 * \class SharedMorphUtilities
 * \brief functionality in common for anchor and VanHerkGilWerman openings/closings and
 * erosions/dilation
 *
 */

template< typename TRegion, typename TLine >
bool NeedToDoFace(const TRegion AllImage,
                  const TRegion face,
                  const TLine line)
{
  // can't use the continuous IsInside (even if I could get it to
  // work) because on the edge doesn't count as inside for this test

  // If the component of the vector orthogonal to the face doesn't go
  // inside the image then we can ignore the face

  // find the small dimension of the face - should only be one
  typename TRegion::IndexType ISt = AllImage.GetIndex();

  typename TRegion::SizeType FSz = face.GetSize();
  typename TRegion::IndexType FSt = face.GetIndex();

  unsigned smallDim = 0;
  for ( unsigned i = 0; i < AllImage.GetImageDimension(); i++ )
    {
    if ( FSz[i] == 1 )
      {
      smallDim = i;
      break;
      }
    }
  IndexValueType startI = ISt[smallDim];
  IndexValueType facePos = FSt[smallDim] + FSz[smallDim] - 1;
  if ( facePos == startI )
    {
    // at the start of dimension - vector must be positive
    if ( line[smallDim] > 0.000001 ) { return true; }
    // some small angle that we consider to be zero - should be more rigorous
    }
  else
    {
    // at the end of dimension - vector must be positive
    if ( line[smallDim] < -0.000001 ) { return true; }
    }
  return ( false );
}

template< typename TImage, typename TBres, typename TLine >
int ComputeStartEnd(const typename TImage::IndexType StartIndex,
                    const TLine line,
                    const float tol,
                    const typename TBres::OffsetArray LineOffsets,
                    const typename TImage::RegionType AllImage,
                    unsigned & start,
                    unsigned & end)
{
  // compute intersection between ray and box
  typename TImage::IndexType ImStart = AllImage.GetIndex();
  typename TImage::SizeType ImSize = AllImage.GetSize();
  float    Tfar = NumericTraits< float >::max();
  float    Tnear  = NumericTraits< float >::NonpositiveMin();
  float    domdir  = NumericTraits< float >::NonpositiveMin();
  int      sPos, ePos;
  unsigned perpdir = 0;
  for ( unsigned i = 0; i < TImage::RegionType::ImageDimension; i++ )
    {
    if ( std::fabs(line[i]) > domdir )
      {
      domdir = std::fabs(line[i]);
      perpdir = i;
      }
    if ( std::fabs(line[i]) > tol )
      {
      int   P1 = ImStart[i] - StartIndex[i];
      int   P2 = ImStart[i] + ImSize[i] - 1 - StartIndex[i];
      float T1 = ( (float)( P1 ) ) / line[i];
      float T2 = ( (float)( P2 ) ) / line[i];

      if ( T1 > T2 )
        {
        // T1 is meant to be the near face
        std::swap(T1, T2);
        }
      // want the farthest Tnear and nearest TFar
      if ( T1 > Tnear )
        {
        Tnear = T1;
        }
      if ( T2 < Tfar )
        {
        Tfar = T2;
        }
      }
    else
      {
      // parallel to an axis - check for intersection at all
      if ( ( StartIndex[i] < ImStart[i] ) || ( StartIndex[i] > ImStart[i] + (int)ImSize[i] - 1 ) )
        {
        // no intersection
        start = end = 0;
        return ( 0 );
        }
      }
    }
  sPos = (int)( Tnear * std::fabs(line[perpdir]) + 0.5 );
  ePos = (int)( Tfar * std::fabs(line[perpdir]) + 0.5 );

  //std::cout << Tnear << " " << Tfar << std::endl;
  if ( Tfar < Tnear ) // seems to need some margin
    {
    // in theory, no intersection, but search between them
    bool     intersection = false;
    unsigned int inside = 0; // initialize to avoid warning
    if ( Tnear - Tfar < 10 )
      {
//      std::cout << "Searching " << Tnear << " " << Tfar << std::endl;
      itkAssertInDebugAndIgnoreInReleaseMacro(ePos >= 0);
      itkAssertInDebugAndIgnoreInReleaseMacro( sPos < (int)LineOffsets.size() );
      for ( int i = ePos; i <= sPos; i++ )
        {
        if ( AllImage.IsInside(StartIndex + LineOffsets[i]) )
          {
          inside = i;
          intersection = true;
          break;
          }
        }
      }
    if ( intersection )
      {
//      std::cout << "Found intersection after all :: " << inside << std::endl;
      sPos = ePos = inside;
      itkAssertInDebugAndIgnoreInReleaseMacro(ePos + 1 >= 0);
      itkAssertInDebugAndIgnoreInReleaseMacro( ePos + 1 < (int)LineOffsets.size() );
      while ( AllImage.IsInside(StartIndex + LineOffsets[ePos + 1]) )
        {
        ++ePos;
        itkAssertInDebugAndIgnoreInReleaseMacro(ePos + 1 >= 0);
        itkAssertInDebugAndIgnoreInReleaseMacro( ePos + 1 < (int)LineOffsets.size() );
        }
      itkAssertInDebugAndIgnoreInReleaseMacro(sPos - 1 >= 0);
      itkAssertInDebugAndIgnoreInReleaseMacro( sPos - 1 < (int)LineOffsets.size() );
      while ( AllImage.IsInside(StartIndex + LineOffsets[sPos - 1]) )
        {
        --sPos;
        itkAssertInDebugAndIgnoreInReleaseMacro(sPos - 1 >= 0);
        itkAssertInDebugAndIgnoreInReleaseMacro( sPos - 1 < (int)LineOffsets.size() );
        }
      start = sPos;
      end = ePos;
      }
    else
      {
//      std::cout << StartIndex << "No intersection" << std::endl;
      start = end = 0;
      return ( 0 );
      }
    }
  else
    {
    itkAssertInDebugAndIgnoreInReleaseMacro(sPos >= 0);
    itkAssertInDebugAndIgnoreInReleaseMacro( sPos < (int)LineOffsets.size() );
    if ( AllImage.IsInside(StartIndex + LineOffsets[sPos]) )
      {
      for (; sPos > 0; )
        {
        itkAssertInDebugAndIgnoreInReleaseMacro(sPos - 1 >= 0);
        itkAssertInDebugAndIgnoreInReleaseMacro( sPos - 1 < (int)LineOffsets.size() );
        if ( !AllImage.IsInside(StartIndex + LineOffsets[sPos - 1]) ) { break; }
        else { --sPos; }
        }
      }
    else
      {
      for (; sPos < (int)LineOffsets.size(); )
        {
        itkAssertInDebugAndIgnoreInReleaseMacro(sPos >= 0);
        itkAssertInDebugAndIgnoreInReleaseMacro( sPos < (int)LineOffsets.size() );
        ++sPos;
        if ( !AllImage.IsInside(StartIndex + LineOffsets[sPos]) ) { ++sPos; }
        else { break; }
        }
      }
    if ( AllImage.IsInside(StartIndex + LineOffsets[ePos]) )
      {
      for (; ePos < (int)LineOffsets.size(); )
        {
        itkAssertInDebugAndIgnoreInReleaseMacro(ePos + 1 >= 0);
        itkAssertInDebugAndIgnoreInReleaseMacro( ePos + 1 < (int)LineOffsets.size() );
        if ( !AllImage.IsInside(StartIndex + LineOffsets[ePos + 1]) ) { break; }
        else { ++ePos; }
        }
      }
    else
      {
      for (; ePos > 0; )
        {
        --ePos;
        itkAssertInDebugAndIgnoreInReleaseMacro(ePos >= 0);
        itkAssertInDebugAndIgnoreInReleaseMacro( ePos < (int)LineOffsets.size() );
        if ( !AllImage.IsInside(StartIndex + LineOffsets[ePos]) ) { --ePos; }
        else { break; }
        }
      }
    }
  start = sPos;
  end = ePos;
  return ( 1 );
}

template< typename TImage, typename TBres >
void CopyLineToImage(const typename TImage::Pointer output,
                     const typename TImage::IndexType StartIndex,
                     const typename TBres::OffsetArray LineOffsets,
                     std::vector<typename TImage::PixelType> & outbuffer,
                     const unsigned start,
                     const unsigned end)
{
  unsigned size = end - start + 1;

  for ( unsigned i = 0; i < size; i++ )
    {
    // itkAssertInDebugAndIgnoreInReleaseMacro(start + i >= 0);
    itkAssertInDebugAndIgnoreInReleaseMacro( start + i < LineOffsets.size() );
    output->SetPixel(StartIndex + LineOffsets[start + i], outbuffer[i + 1]);  //compat
    }
}

template< typename TInputImage, typename TLine >
typename TInputImage::RegionType
MakeEnlargedFace(const typename TInputImage::ConstPointer itkNotUsed(input),
                 const typename TInputImage::RegionType AllImage,
                 const TLine line)
{
  // the face list calculator strategy fails in multithreaded mode
  // with 1D kernels
  // because it doesn't return faces of the sub-blocks if they don't
  // fall along the edge of the image
  typedef typename TInputImage::RegionType RegionType;
  typedef typename TInputImage::SizeType   SizeType;
  typedef typename TInputImage::IndexType  IndexType;
  typedef std::list< RegionType >          FaceListType;
  FaceListType faceList;

  for ( unsigned i = 0; i < TInputImage::ImageDimension; i++ )
    {
    RegionType R1, R2;
    SizeType   S1 = AllImage.GetSize();
    IndexType  I2 = AllImage.GetIndex();

    S1[i] = 1;
    R1 = AllImage;
    R2 = AllImage;

    // the first face will have the same starting index and one
    // dimension removed
    R1.SetSize(S1);

    I2[i] = I2[i] + AllImage.GetSize()[i] - 1;
    R2.SetSize(S1);
    R2.SetIndex(I2);
    faceList.push_back(R1);
    faceList.push_back(R2);
//    std::cout << R1 << R2 << std::endl;
    }
  typename FaceListType::iterator fit;
  fit = faceList.begin();

  typename TInputImage::RegionType RelevantRegion;
  bool     foundFace = false;
  float    MaxComp = NumericTraits< float >::NonpositiveMin();
  unsigned DomDir = 0;
  //std::cout << "------------" << std::endl;
  // figure out the dominant direction of the line
  for ( unsigned i = 0; i < TInputImage::RegionType::ImageDimension; i++ )
    {
    if ( std::fabs(line[i]) > MaxComp )
      {
      MaxComp = std::fabs(line[i]);
      DomDir = i;
      }
    }

  for (; fit != faceList.end(); ++fit )
    {
    // check whether this face is suitable for parallel sweeping - i.e
    // whether the line is within 45 degrees of the perpendicular
    // Figure out the perpendicular using the region size
    unsigned FaceDir = 0;
    //    std::cout << "Face " << *fit << std::endl;
    for ( unsigned i = 0; i < TInputImage::RegionType::ImageDimension; i++ )
      {
      if ( fit->GetSize()[i] == 1 ) { FaceDir = i; }
      }
    if ( FaceDir == DomDir ) // within 1 degree
      {
      // now check whether the line goes inside the image from this face
      if ( NeedToDoFace< typename TInputImage::RegionType, TLine >(AllImage, *fit, line) )
        {
//        std::cout << "Using face: " << *fit << line << std::endl;
        RelevantRegion = *fit;
        foundFace = true;
        break;
        }
      }
    }
  if ( foundFace )
    {
    // enlarge the region so that sweeping the line across it will
    // cause all pixels to be visited.
    // find the dimension not within the face
    unsigned NonFaceDim = 0;

    for ( unsigned i = 0; i < TInputImage::RegionType::ImageDimension; i++ )
      {
      if ( RelevantRegion.GetSize()[i] == 1 )
        {
        NonFaceDim = i;
        break;
        }
      }

    // figure out how much extra each other dimension needs to be extended
    typename TInputImage::SizeType NewSize = RelevantRegion.GetSize();
    typename TInputImage::IndexType NewStart = RelevantRegion.GetIndex();
    unsigned NonFaceLen = AllImage.GetSize()[NonFaceDim];
    for ( unsigned i = 0; i < TInputImage::RegionType::ImageDimension; i++ )
      {
      if ( i != NonFaceDim )
        {
        int Pad = Math::Ceil< int >( (float)( NonFaceLen ) * line[i] / std::fabs(line[NonFaceDim]) );
        if ( Pad < 0 )
          {
          // just increase the size - no need to change the start
          NewSize[i] += abs(Pad) + 1;
          }
        else
          {
          // change the size and index
          NewSize[i] += Pad + 1;
          NewStart[i] -= Pad + 1;
          }
        }
      }
    RelevantRegion.SetSize(NewSize);
    RelevantRegion.SetIndex(NewStart);
    }
  else
    {
    std::cout << "Line " << line << " doesn't correspond to a face" << std::endl;
    }

  return RelevantRegion;
}

template< typename TImage, typename TBres, typename TLine >
int FillLineBuffer(typename TImage::ConstPointer input,
                   const typename TImage::IndexType StartIndex,
                   const TLine line,  // unit vector
                   const float tol,
                   const typename TBres::OffsetArray LineOffsets,
                   const typename TImage::RegionType AllImage,
                   std::vector<typename TImage::PixelType> & inbuffer,
                   unsigned int & start,
                   unsigned int & end)
{
  int status = ComputeStartEnd< TImage, TBres, TLine >(StartIndex, line, tol, LineOffsets, AllImage,
                                                       start, end);
  if ( !status ) { return ( status ); }
  unsigned size = end - start + 1;
  // compat
  for ( unsigned i = 0; i < size; i++ )
    {
    itkAssertInDebugAndIgnoreInReleaseMacro( start + i < LineOffsets.size() );
    inbuffer[i + 1] = input->GetPixel(StartIndex + LineOffsets[start + i]);
    }
  return ( 1 );
}

template< typename TLine >
unsigned int GetLinePixels(const TLine line)
{
  float N = line.GetNorm();
  float correction = 0.0;

  for ( unsigned int i = 0; i < TLine::Dimension; i++ )
    {
    float tt = std::fabs(line[i] / N);
    if ( tt > correction ) { correction = tt; }
    }

  N *= correction;
  return (int)( N + 0.5 );
}
} // namespace itk

#endif