This file is indexed.

/usr/include/ITK-4.5/itkTxtTransformIO.hxx is in libinsighttoolkit4-dev 4.5.0-3.

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

#include "itkTxtTransformIO.h"
#include "itksys/SystemTools.hxx"
#include "vnl/vnl_matlab_read.h"
#include "vnl/vnl_matlab_write.h"
#include "itkTransformFileReader.h"
#include "itkCompositeTransform.h"
#include "itkCompositeTransformIOHelper.h"
#include "itkNumberToString.h"
namespace itk
{
template<typename ParametersValueType>
TxtTransformIOTemplate<ParametersValueType>
::TxtTransformIOTemplate()
{}

template<typename ParametersValueType>
TxtTransformIOTemplate<ParametersValueType>
::~TxtTransformIOTemplate()
{}

template<typename ParametersValueType>
bool
TxtTransformIOTemplate<ParametersValueType>
::CanReadFile(const char *fileName)
{
  bool recognizedExtension = false;

  recognizedExtension |= ( itksys::SystemTools::GetFilenameLastExtension(fileName) == ".txt" );
  recognizedExtension |= ( itksys::SystemTools::GetFilenameLastExtension(fileName) == ".tfm" );
  return recognizedExtension;
}

template<typename ParametersValueType>
bool
TxtTransformIOTemplate<ParametersValueType>
::CanWriteFile(const char *fileName)
{
  bool recognizedExtension = false;

  recognizedExtension |= ( itksys::SystemTools::GetFilenameLastExtension(fileName) == ".txt" );
  recognizedExtension |= ( itksys::SystemTools::GetFilenameLastExtension(fileName) == ".tfm" );
  return recognizedExtension;
}

template<typename ParametersValueType>
std::string
TxtTransformIOTemplate<ParametersValueType>
::trim(std::string const & source, char const *delims)
{
  std::string            result(source);
  std::string::size_type index = result.find_last_not_of(delims);

  if ( index != std::string::npos )
    {
    result.erase(++index);
    }

  index = result.find_first_not_of(delims);
  if ( index != std::string::npos )
    {
    result.erase(0, index);
    }
  else
    {
    result.erase();
    }
  return result;
}

template<typename ParametersValueType>
void
TxtTransformIOTemplate<ParametersValueType>
::ReadComponentFile( std::string Value )
{
  /* Used for reading component files listed in a composite transform
   * file, which should be read by CompositeTransformReader for assembly
   * into a CompositeTransform.
   * The component filenames are listed w/out paths, and are expected
   * to be in the same path as the master file. */
  std::string filePath =
    itksys::SystemTools::GetFilenamePath( this->GetFileName() ) + "/";

  /* Use TransformFileReader to read each component file. */
  typename TransformFileReaderTemplate<ParametersValueType>::Pointer reader =
      TransformFileReaderTemplate<ParametersValueType>::New();
  std::string componentFullPath = filePath + Value;
  reader->SetFileName( componentFullPath );
  try
    {
    reader->Update();
    }
  catch (itk::ExceptionObject &ex)
    {
    itkExceptionMacro("Error reading component file: " << Value
                      << std::endl << ex );
    }
  TransformPointer transform = reader->GetTransformList()->front().GetPointer();
  this->GetReadTransformList().push_back (transform);
}

template<typename ParametersValueType>
void
TxtTransformIOTemplate<ParametersValueType>
::Read()
{
  TransformPointer transform;
  std::ifstream    in;

  in.open (this->GetFileName(), std::ios::in | std::ios::binary);
  if ( in.fail() )
    {
    in.close();
    itkExceptionMacro ("The file could not be opened for read access "
                       << std::endl << "Filename: \"" << this->GetFileName() << "\"");
    }

  std::ostringstream InData;

  // in.get ( InData );
  std::filebuf *pbuf;
  pbuf = in.rdbuf();

  // get file size using buffer's members
  int size = pbuf->pubseekoff (0, std::ios::end, std::ios::in);
  pbuf->pubseekpos (0, std::ios::in);

  // allocate memory to contain file data
  char *buffer = new char[size + 1];

  // get file data
  pbuf->sgetn (buffer, size);
  buffer[size] = '\0';
  itkDebugMacro ("Read file transform Data");
  InData << buffer;

  delete[] buffer;
  std::string data = InData.str();
  in.close();

  // Read line by line
  typename TransformType::ParametersType   VectorBuffer;
  std::string::size_type position = 0;

  typename TransformType::ParametersType TmpParameterArray;
  typename TransformType::ParametersType TmpFixedParameterArray;
  TmpParameterArray.clear();
  TmpFixedParameterArray.clear();
  bool haveFixedParameters = false;
  bool haveParameters = false;

  //
  // check for line end convention
  std::string line_end("\n");

  if ( data.find('\n') == std::string::npos )
    {
    if ( data.find('\r') == std::string::npos )
      {
      itkExceptionMacro ("No line ending character found, not a valid ITK Transform TXT file");
      }
    line_end = "\r";
    }
  while ( position != std::string::npos && position < data.size() )
    {
    // Find the next string
    std::string::size_type end = data.find (line_end, position);

    if( end == std::string::npos )
      {
      itkExceptionMacro("Couldn't find end of line in " << data );
      }

    std::string            line = trim ( data.substr (position, end - position) );
    position = end + 1;
    itkDebugMacro ("Found line: \"" << line << "\"");

    if ( line.length() == 0 )
      {
      continue;
      }
    if ( line[0] == '#' || std::string::npos == line.find_first_not_of (" \t") )
      {
      // Skip lines beginning with #, or blank lines
      continue;
      }

    // Get the name
    end = line.find (":");
    if ( end == std::string::npos )
      {
      // Throw an error
      itkExceptionMacro ("Tags must be delimited by :");
      }
    std::string Name = trim ( line.substr (0, end) );
    std::string Value = trim ( line.substr ( end + 1, line.length() ) );
    // Push back
    itkDebugMacro ("Name: \"" << Name << "\"");
    itkDebugMacro ("Value: \"" << Value << "\"");
    itksys_ios::istringstream parse (Value);
    VectorBuffer.clear();
    if ( Name == "Transform" )
      {
      // Transform name should be modified to have the output precision type.
      Superclass::CorrectTransformPrecisionType( Value );

      this->CreateTransform(transform, Value);
      this->GetReadTransformList().push_back (transform);
      }
    else if ( Name == "ComponentTransformFile" )
      {
      /* Used by CompositeTransform file */
      ReadComponentFile( Value );
      }
    else if ( Name == "Parameters" || Name == "FixedParameters" )
      {
      VectorBuffer.clear();

      // Read them
      parse >> VectorBuffer;
      itkDebugMacro ("Parsed: " << VectorBuffer);
      if ( Name == "Parameters" )
        {
        TmpParameterArray = VectorBuffer;
        itkDebugMacro ("Setting Parameters: " << TmpParameterArray);
        if ( haveFixedParameters )
          {
          transform->SetFixedParameters (TmpFixedParameterArray);
          itkDebugMacro ("Set Transform Fixed Parameters");
          transform->SetParametersByValue (TmpParameterArray);
          itkDebugMacro ("Set Transform Parameters");
          TmpParameterArray.clear();
          TmpFixedParameterArray.clear();
          haveFixedParameters = false;
          haveParameters = false;
          }
        else
          {
          haveParameters = true;
          }
        }
      else if ( Name == "FixedParameters" )
        {
        TmpFixedParameterArray = VectorBuffer;
        itkDebugMacro ("Setting Fixed Parameters: " << TmpFixedParameterArray);
        if ( !transform )
          {
          itkExceptionMacro ("Please set the transform before parameters"
                             "or fixed parameters");
          }
        if ( haveParameters )
          {
          transform->SetFixedParameters (TmpFixedParameterArray);
          itkDebugMacro ("Set Transform Fixed Parameters");
          transform->SetParametersByValue (TmpParameterArray);
          itkDebugMacro ("Set Transform Parameters");
          TmpParameterArray.clear();
          TmpFixedParameterArray.clear();
          haveFixedParameters = false;
          haveParameters = false;
          }
        else
          {
          haveFixedParameters = true;
          }
        }
      }
    }
}

namespace {
template<typename ParametersValueType>
void print_vector(std::ofstream& s, vnl_vector<ParametersValueType> const &v)
{
  NumberToString<ParametersValueType> convert;
  for (unsigned i = 0; i+1 < v.size(); ++i)
    {
    s << convert(v[i]) << ' ';
    }
  if (v.size() > 0)
    {
    s << convert(v[v.size()-1]);
    }
}
}

template<typename ParametersValueType>
void
TxtTransformIOTemplate<ParametersValueType>
::Write()
{
  ConstTransformListType &transformList =
    this->GetWriteTransformList();

  std::ofstream        out;
  this->OpenStream(out, false);

  out << "#Insight Transform File V1.0" << std::endl;

  const std::string CompositeTransformTypeName =
    transformList.front()->GetTransformTypeAsString();
  //
  // if the first transform in the list is a
  // composite transform, use its internal list
  // instead of the IO
  CompositeTransformIOHelperTemplate<ParametersValueType> helper;
  if(CompositeTransformTypeName.find("CompositeTransform") != std::string::npos)
    {
    transformList = helper.GetTransformList(transformList.front().GetPointer());
    }
  vnl_vector< ParametersValueType > TempArray;
  int count = 0;

  typename ConstTransformListType::const_iterator end = transformList.end();

  for (typename ConstTransformListType::const_iterator it = transformList.begin();
       it != end; ++it,++count )
    {
    const std::string TransformTypeName = ( *it )->GetTransformTypeAsString();
    out << "#Transform " << count << std::endl;
    out << "Transform: " << ( *it )->GetTransformTypeAsString() << std::endl;
    //
    // Composite Transforms are not written out with parameters;
    // their parameters are the union of all their component
    // transforms' parameters.
    if(TransformTypeName.find("CompositeTransform") != std::string::npos)
      {
      if(count > 0)
        {
        itkExceptionMacro(<< "Composite Transform can only be 1st transform in a file");
        }
      }
    else
      {
      TempArray = ( *it )->GetParameters();
      out << "Parameters: ";// << TempArray << std::endl;
      print_vector(out,TempArray);
      out << std::endl;
      TempArray = ( *it )->GetFixedParameters();
      out << "FixedParameters: ";// << TempArray << std::endl;
      print_vector(out,TempArray);
      out << std::endl;
      }
    }
  out.close();
}
}

#endif