File: itkNiftiImageIOTest2.cxx

package info (click to toggle)
insighttoolkit 3.18.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 110,432 kB
  • ctags: 74,559
  • sloc: cpp: 412,627; ansic: 196,210; fortran: 28,000; python: 3,852; tcl: 2,005; sh: 1,186; java: 583; makefile: 458; csh: 220; perl: 193; xml: 20
file content (164 lines) | stat: -rw-r--r-- 4,549 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
/*=========================================================================

Program:   Insight Segmentation & Registration Toolkit
Module:    $RCSfile: itkNiftiImageIOTest2.cxx,v $
Language:  C++
Date:      $Date: 2009-10-30 21:32:45 $
Version:   $Revision: 1.7 $

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.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include "itkNiftiImageIOTest.h"
#include "itkNumericTraits.h"

template <class RGBPixelType>
int RGBTest(int ac, char *av[])
{
  if(ac > 2) 
    {
    char *testdir = *++av;
    itksys::SystemTools::ChangeDirectory(testdir);
    }
  else
    {
    return EXIT_FAILURE;
    }
  char * tmpImage = *++av;
  int success(EXIT_SUCCESS);
  typedef typename itk::Image<RGBPixelType,3> RGBImageType;
  typename RGBImageType::RegionType imageRegion;
  typename RGBImageType::SizeType size;
  typename RGBImageType::IndexType index;
  typename RGBImageType::SpacingType spacing;
  typename RGBImageType::PointType origin;
  typename RGBImageType::DirectionType myDirection;
  for(unsigned i = 0; i < 3; i++)
    {
    size[i] = 5;
    index[i] = 0;
    spacing[i] = 1.0;
    origin[i] = 0;
    }
  imageRegion.SetSize(size);
  imageRegion.SetIndex(index);
  typename RGBImageType::Pointer im;
  AllocateImageFromRegionAndSpacing(RGBImageType,im,imageRegion,spacing);
  vnl_random randgen(12345678);
  itk::ImageRegionIterator<RGBImageType> it(im,im->GetLargestPossibleRegion());
  for(it.GoToBegin(); !it.IsAtEnd(); ++it)
    {
    RGBPixelType pix;
    for(unsigned int i = 0; i < RGBPixelType::Dimension; i++)
      {
      pix[i] = randgen.lrand32(255);
      }
    it.Set(pix);
    }
  typename RGBImageType::Pointer im2;
  try
    {
    WriteImage<RGBImageType>(im,std::string(tmpImage));
    im2 = ReadImage<RGBImageType>(std::string(tmpImage));
    }
  catch(itk::ExceptionObject &err)
    {
    std::cout << "itkNiftiImageIOTest9" << std::endl 
              << "Exception Object caught: " << std::endl
              << err << std::endl;
    return EXIT_FAILURE;
    }
  itk::ImageRegionIterator<RGBImageType> it2(im2,im2->GetLargestPossibleRegion());
  for(it.GoToBegin(),it2.GoToBegin(); !it.IsAtEnd() && !it2.IsAtEnd(); ++it,++it2)
    {
    if(it.Value() != it2.Value())
      {
      std::cout << "Original Pixel (" << it.Value()
                << ") doesn't match read-in Pixel ("
                << it2.Value() << std::endl;
      success = EXIT_FAILURE;
      break;
      }
    }
  Remove(tmpImage);
  return success;
}

int itkNiftiImageIOTest9(int ac, char *av[])
{
  return RGBTest<itk::RGBPixel<unsigned char> >(ac,av);
}

int itkNiftiImageIOTest10(int ac, char *av[])
{
  return RGBTest<itk::RGBAPixel<unsigned char> >(ac,av);
}

int itkNiftiImageIOTest11(int ac, char *av[])
{
  std::string testfilename;
  if(ac > 1) 
    {
    char *testdir = *++av;
    itksys::SystemTools::ChangeDirectory(testdir);
    }
  else
    {
    return EXIT_FAILURE;
    }
  if(ac > 2)
    {
    testfilename = *++av;
    }
  else
    {
    return EXIT_FAILURE;
    }
  typedef  itk::Image<char,3> ImageType;
  ImageType::RegionType imageRegion;
  ImageType::SizeType size;
  ImageType::IndexType index;
  ImageType::SpacingType spacing;
  ImageType::PointType origin;
  ImageType::DirectionType myDirection;

  size[0] = static_cast<long int>(itk::NumericTraits<short>::max()) * 2;
  size[1] = 1;
  size[2] = 1;

  index.Fill(0);
  spacing.Fill(1.0);
  origin.Fill(0.0);

  imageRegion.SetSize(size);
  imageRegion.SetIndex(index);
  ImageType::Pointer im;
  AllocateImageFromRegionAndSpacing(ImageType,im,imageRegion,spacing);
  ImageType::DirectionType dir(CORDirCosines<ImageType>());
  std::cout << "itkNiftiImageIOTest11" << std::endl;
  std::cout << "Direction = " << dir << std::endl;
  im->SetDirection(dir);
  try
    {
    WriteImage<ImageType>(im,testfilename);
    std::cerr << "FAILED to catch expected exception" << std::endl;
    return EXIT_FAILURE;
    }
  catch (itk::ExceptionObject & e)
    {
    std::cout << "EXPECTED exception in file writer " << std::endl;
    std::cout << e.GetDescription() << std::endl;
    std::cout << e.GetLocation() << std::endl;
    }

  return EXIT_SUCCESS;
}