]> Creatis software - clitk.git/blob - tools/clitkImageCreate.cxx
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[clitk.git] / tools / clitkImageCreate.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef CLITKIMAGECREATE_CXX
19 #define CLITKIMAGECREATE_CXX
20 /**
21    ------------------------------------------------=
22    * @file   clitkImageCreate.cxx
23    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24    * @date   14 Oct 2008 08:37:53
25    ------------------------------------------------=*/
26
27 // clitk include
28 #include "clitkImageCreate_ggo.h"
29 #include "clitkCommon.h"
30 #include "clitkImageCommon.h"
31 #include "clitkIO.h"
32
33 template<class ImageType>
34 void NewFilledImage(int * size, float * spacing, double * origin,
35                     double value,typename ImageType::Pointer output)
36 {
37   static const unsigned int Dim = ImageType::GetImageDimension();
38   typename ImageType::SizeType mSize;
39   mSize.Fill (0);
40   for(unsigned int i=0; i<Dim; i++) mSize[i] = size[i];
41   typename ImageType::RegionType mRegion;
42   mRegion.SetSize(mSize);
43   typename ImageType::SpacingType mSpacing;
44   for(unsigned int i=0; i<Dim; i++) mSpacing[i] = spacing[i];
45   output->SetRegions(mRegion);
46   output->SetSpacing(mSpacing);
47   output->Allocate();
48   typename ImageType::PointType mOrigin;
49   for(unsigned int i=0; i<Dim; i++) mOrigin[i] = origin[i];
50   output->SetOrigin(mOrigin);
51   typedef typename ImageType::PixelType PixelType;
52   PixelType p = clitk::PixelTypeDownCast<double, PixelType>(value);
53   output->FillBuffer(p);
54 }
55
56
57 //--------------------------------------------------------------------
58 int main(int argc, char * argv[])
59 {
60
61   // Init command line
62   GGO(clitkImageCreate, args_info);
63   CLITK_INIT;
64
65   // Check --like option
66   int dim;
67   if (args_info.like_given) {
68     itk::ImageIOBase::Pointer header = clitk::readImageHeader(args_info.like_arg);
69     dim = header->GetNumberOfDimensions();
70     //mPixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
71     //mNbOfComponents = header->GetNumberOfComponents();
72     args_info.size_given = dim;
73     args_info.size_arg = new int[dim];
74     args_info.spacing_given = dim;
75     args_info.spacing_arg = new float[dim];
76     args_info.origin_given = dim;
77     args_info.origin_arg = new double[dim];
78
79     for(int i=0; i<dim; i++) {
80       args_info.size_arg[i] = header->GetDimensions(i);
81       args_info.spacing_arg[i] = header->GetSpacing(i);
82       args_info.origin_arg[i]=   header->GetOrigin(i);
83     }
84   }
85
86   // Check dimension
87   if ((args_info.size_given > 4) || (args_info.size_given <2)) {
88     std::cerr << "ERROR : only 2D/3D/4D image ! Please give 2 or 3 or 4 number to the --size option." << std::endl;
89     exit(-1);
90   }
91   dim = args_info.size_given;
92
93   // origin
94   std::vector<double> origin;
95   origin.resize(dim);
96   for(int i=0; i<dim; i++) origin[i]=0.;
97   if (args_info.origin_given) {
98     if (args_info.origin_given==1)
99       for(int i=0; i<dim; i++) origin[i] = args_info.origin_arg[0];
100     else {
101       if (args_info.origin_given != args_info.size_given) {
102         std::cerr << "ERROR : please give the same number of values for --origin and --size." << std::endl;
103         exit(-1);
104       }
105       for(int i=0; i<dim; i++) origin[i] = args_info.origin_arg[i];
106     }
107   }
108
109   // spacing
110   std::vector<float> spacing;
111   spacing.resize(dim);
112   if (args_info.spacing_given == 1) {
113     for(int i=0; i<dim; i++) spacing[i] = args_info.spacing_arg[0];
114   } else {
115     if (args_info.spacing_given != args_info.size_given) {
116       std::cerr << "ERROR : please give the same number of values for --size and --spacing." << std::endl;
117       exit(-1);
118     }
119     for(int i=0; i<dim; i++) spacing[i] = args_info.spacing_arg[i];
120   }
121
122   // Create new image
123   typedef float PixelType;
124   if (dim == 2) {
125     const int Dim=2;
126     typedef itk::Image<PixelType, Dim> ImageType;
127     ImageType::Pointer output = ImageType::New();
128     NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], args_info.value_arg, output);
129     clitk::writeImage<ImageType>(output, args_info.output_arg);
130   }
131   if (dim == 3) {
132     const int Dim=3;
133     typedef itk::Image<PixelType, Dim> ImageType;
134     ImageType::Pointer output = ImageType::New();
135     NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], args_info.value_arg, output);
136     clitk::writeImage<ImageType>(output, args_info.output_arg);
137   }
138   if (dim == 4) {
139     const int Dim=4;
140     typedef itk::Image<PixelType, Dim> ImageType;
141     ImageType::Pointer output = ImageType::New();
142     NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], args_info.value_arg, output);
143     clitk::writeImage<ImageType>(output, args_info.output_arg);
144   }
145
146   // this is the end my friend
147   return 0;
148 } // end main
149
150 #endif //define CLITKIMAGEARITHM_CXX