]> Creatis software - clitk.git/blob - tools/clitkImageCreate.cxx
added the new headers
[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://oncora1.lyon.fnclcc.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   // Init command line
61   GGO(clitkImageCreate, args_info);
62   CLITK_INIT;
63
64   // Check --like option
65   int dim;
66   if (args_info.like_given) {
67     itk::ImageIOBase::Pointer header = clitk::readImageHeader(args_info.like_arg);
68      dim = header->GetNumberOfDimensions();
69      //mPixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());  
70      //mNbOfComponents = header->GetNumberOfComponents();
71      args_info.size_given = dim;
72      args_info.size_arg = new int[dim];
73      args_info.spacing_given = dim;
74      args_info.spacing_arg = new float[dim];
75      args_info.origin_given = dim;
76      args_info.origin_arg = new double[dim];
77      
78      for(int i=0; i<dim; i++) {
79        args_info.size_arg[i] = header->GetDimensions(i);
80        args_info.spacing_arg[i] = header->GetSpacing(i);
81        args_info.origin_arg[i]=   header->GetOrigin(i);
82      }
83   }
84
85   // Check dimension
86   if ((args_info.size_given > 4) || (args_info.size_given <2)) {
87     std::cerr << "ERROR : only 2D/3D/4D image ! Please give 2 or 3 or 4 number to the --size option." << std::endl;
88     exit(-1);
89   }
90   dim = args_info.size_given;
91   
92   // origin 
93   std::vector<double> origin;
94   origin.resize(dim);
95   for(int i=0; i<dim; i++) origin[i]=0.;
96   if (args_info.origin_given)
97     {
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         {
102           if (args_info.origin_given != args_info.size_given) {
103             std::cerr << "ERROR : please give the same number of values for --origin and --size." << std::endl;
104             exit(-1);
105           }
106           for(int i=0; i<dim; i++) origin[i] = args_info.origin_arg[i]; 
107         }
108     }
109
110   // spacing 
111   std::vector<float> spacing;
112   spacing.resize(dim);
113   if (args_info.spacing_given == 1) {
114     for(int i=0; i<dim; i++) spacing[i] = args_info.spacing_arg[0];
115   }
116   else {
117     if (args_info.spacing_given != args_info.size_given) {
118       std::cerr << "ERROR : please give the same number of values for --size and --spacing." << std::endl;
119       exit(-1);
120     }
121     for(int i=0; i<dim; i++) spacing[i] = args_info.spacing_arg[i];
122   }
123   
124   // Create new image
125   typedef float PixelType;
126   if (dim == 2) {
127     const int Dim=2;
128     typedef itk::Image<PixelType, Dim> ImageType; 
129     ImageType::Pointer output = ImageType::New();
130     NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], args_info.value_arg, output);
131     clitk::writeImage<ImageType>(output, args_info.output_arg);
132   }
133   if (dim == 3) {
134     const int Dim=3;
135     typedef itk::Image<PixelType, Dim> ImageType; 
136     ImageType::Pointer output = ImageType::New();
137     NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], args_info.value_arg, output);
138     clitk::writeImage<ImageType>(output, args_info.output_arg);
139   }
140   if (dim == 4) {
141     const int Dim=4;
142     typedef itk::Image<PixelType, Dim> ImageType; 
143     ImageType::Pointer output = ImageType::New();
144     NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], args_info.value_arg, output);
145     clitk::writeImage<ImageType>(output, args_info.output_arg);
146   }
147   
148   // this is the end my friend  
149   return 0;
150 } // end main
151
152 #endif //define CLITKIMAGEARITHM_CXX