1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 ------------------------------------------------=
22 * @file clitkImageCreate.cxx
23 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24 * @date 14 Oct 2008 08:37:53
25 ------------------------------------------------=*/
28 #include "clitkImageCreate_ggo.h"
29 #include "clitkCommon.h"
30 #include "clitkImageCommon.h"
33 template<class ImageType>
34 void NewFilledImage(int * size, float * spacing, double * origin, double * direction,
35 double value,typename ImageType::Pointer output)
37 static const unsigned int Dim = ImageType::GetImageDimension();
39 typename ImageType::SizeType mSize;
41 for(unsigned int i=0; i<Dim; i++) mSize[i] = size[i];
43 typename ImageType::RegionType mRegion;
44 mRegion.SetSize(mSize);
46 typename ImageType::SpacingType mSpacing;
47 for(unsigned int i=0; i<Dim; i++) mSpacing[i] = spacing[i];
49 typename ImageType::DirectionType directionImage;
50 for(unsigned int i=0; i<Dim; i++)
51 for(unsigned int j=0; j<Dim; j++)
52 directionImage[i][j] = direction[i*Dim+j];
54 output->SetRegions(mRegion);
55 output->SetSpacing(mSpacing);
56 output->SetDirection(directionImage);
59 typename ImageType::PointType mOrigin;
60 for(unsigned int i=0; i<Dim; i++) mOrigin[i] = origin[i];
61 output->SetOrigin(mOrigin);
63 typedef typename ImageType::PixelType PixelType;
64 PixelType p = clitk::PixelTypeDownCast<double, PixelType>(value);
65 output->FillBuffer(p);
69 //--------------------------------------------------------------------
70 int main(int argc, char * argv[])
74 GGO(clitkImageCreate, args_info);
77 // Check --like option
79 if (args_info.like_given) {
80 itk::ImageIOBase::Pointer header = clitk::readImageHeader(args_info.like_arg);
81 dim = header->GetNumberOfDimensions();
82 //mPixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
83 //mNbOfComponents = header->GetNumberOfComponents();
84 args_info.size_given = dim;
85 args_info.size_arg = new int[dim];
86 args_info.spacing_given = dim;
87 args_info.spacing_arg = new float[dim];
88 args_info.origin_given = dim;
89 args_info.origin_arg = new double[dim];
90 args_info.transformMatrix_given = dim*dim;
91 args_info.transformMatrix_arg = new double[dim*dim];
93 for(int i=0; i<dim; i++) {
94 args_info.size_arg[i] = header->GetDimensions(i);
95 args_info.spacing_arg[i] = header->GetSpacing(i);
96 args_info.origin_arg[i]= header->GetOrigin(i);
97 for (int j=0; j<dim; ++j)
98 args_info.transformMatrix_arg[i*dim+j] = header->GetDirection(i)[j];
103 if ((args_info.size_given > 4) || (args_info.size_given <2)) {
104 std::cerr << "ERROR : only 2D/3D/4D image ! Please give 2 or 3 or 4 number to the --size option." << std::endl;
107 dim = args_info.size_given;
110 std::vector<double> origin;
112 for(int i=0; i<dim; i++) origin[i]=0.;
113 if (args_info.origin_given) {
114 if (args_info.origin_given==1)
115 for(int i=0; i<dim; i++) origin[i] = args_info.origin_arg[0];
117 if (args_info.origin_given != args_info.size_given) {
118 std::cerr << "ERROR : please give the same number of values for --origin and --size." << std::endl;
121 for(int i=0; i<dim; i++) origin[i] = args_info.origin_arg[i];
126 std::vector<float> spacing;
128 if (args_info.spacing_given == 1) {
129 for(int i=0; i<dim; i++) spacing[i] = args_info.spacing_arg[0];
131 if (args_info.spacing_given != args_info.size_given) {
132 std::cerr << "ERROR : please give the same number of values for --size and --spacing." << std::endl;
135 for(int i=0; i<dim; i++) spacing[i] = args_info.spacing_arg[i];
139 std::vector<double> direction;
140 direction.resize(dim*dim);
141 for(int i=0; i<dim; i++)
143 for(int j=0; j<dim; j++)
146 direction[i*dim+j] = 1;
148 direction[i*dim+j] = 0;
151 if (args_info.transformMatrix_given) {
152 if (args_info.transformMatrix_given != dim*dim) {
153 std::cerr << "ERROR : please give the same number of values for --transfomMatrix and --spacing." << std::endl;
156 for(int i=0; i<dim; i++)
157 for(int j=0; j<dim; j++)
158 direction[i*dim+j] = args_info.transformMatrix_arg[i*dim+j];
162 typedef float PixelType;
165 typedef itk::Image<PixelType, Dim> ImageType;
166 ImageType::Pointer output = ImageType::New();
167 NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], &direction[0], args_info.value_arg, output);
168 clitk::writeImage<ImageType>(output, args_info.output_arg);
172 typedef itk::Image<PixelType, Dim> ImageType;
173 ImageType::Pointer output = ImageType::New();
174 NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], &direction[0], args_info.value_arg, output);
175 clitk::writeImage<ImageType>(output, args_info.output_arg);
179 typedef itk::Image<PixelType, Dim> ImageType;
180 ImageType::Pointer output = ImageType::New();
181 NewFilledImage<ImageType>(args_info.size_arg, &spacing[0], &origin[0], &direction[0], args_info.value_arg, output);
182 clitk::writeImage<ImageType>(output, args_info.output_arg);
185 // this is the end my friend
189 #endif //define CLITKIMAGEARITHM_CXX