]> Creatis software - clitk.git/blob - common/vvImageWriter.txx
Add pixel type options for clitkMergeSequence (double and unsigned char)
[clitk.git] / common / vvImageWriter.txx
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 vvImageWriter_TXX
19 #define vvImageWriter_TXX
20
21 #include <itkImageFileWriter.h>
22 #include <itkChangeInformationImageFilter.h>
23 #include "vvToITK.h"
24
25 //====================================================================
26 template<unsigned int VImageDimension>
27 void vvImageWriter::UpdateWithDim(std::string OutputPixelType)
28 {
29   if (OutputPixelType == "short") {
30     UpdateWithDimAndOutputPixelType<short,VImageDimension>();
31   } else if (OutputPixelType == "unsigned short") {
32     UpdateWithDimAndOutputPixelType<unsigned short,VImageDimension>();
33   } else if (OutputPixelType == "unsigned_short") {
34     UpdateWithDimAndOutputPixelType<unsigned short,VImageDimension>();
35   } else if (OutputPixelType == "char") {
36     UpdateWithDimAndOutputPixelType<char,VImageDimension>();
37   } else if (OutputPixelType == "unsigned_char") {
38     UpdateWithDimAndOutputPixelType<unsigned char,VImageDimension>();
39   } else if (OutputPixelType == "int") {
40     UpdateWithDimAndOutputPixelType<int,VImageDimension>();
41   } else if (OutputPixelType == "unsigned_int") {
42     UpdateWithDimAndOutputPixelType<unsigned int,VImageDimension>();
43   } else if (OutputPixelType == "double") {
44     UpdateWithDimAndOutputPixelType<double,VImageDimension>();
45   } else if (OutputPixelType == "float") {
46     UpdateWithDimAndOutputPixelType<float,VImageDimension>();
47   } else {
48     std::cerr << "Error, output pixel type : \"" << OutputPixelType << "\" unknown !" << std::endl;
49   }
50 }
51 //====================================================================
52
53 //====================================================================
54 template<class OutputPixelType, unsigned int VImageDimension>
55 void vvImageWriter::UpdateWithDimAndOutputPixelType()
56 {
57   // The ITK image
58   typedef itk::Image< OutputPixelType, VImageDimension > OutputImageType;
59   typename OutputImageType::ConstPointer itkimg = vvImageToITK<OutputImageType>(mImage);
60
61   //Create the writer
62   typedef itk::ImageFileWriter<OutputImageType> WriterType;
63   typename WriterType::Pointer writer = WriterType::New();
64   writer->SetFileName(mOutputFilename);
65
66   //Change information if it must transformation must be saved
67   typedef itk::ChangeInformationImageFilter<OutputImageType> ChangeInfoType;
68   typename ChangeInfoType::Pointer info = ChangeInfoType::New();
69   if(mSaveTransform) {
70     // Set pipeline
71     info->SetInput(itkimg);
72     writer->SetInput(info->GetOutput());
73
74     // Inverse vv matrix
75     itk::Matrix<double, 4, 4> trans;
76     for(int i=0; i<4; i++)
77       for(int j=0; j<4; j++)
78         // TODO SR and BP: check on the list of transforms and not the first only
79         trans[i][j] = mImage->GetTransform()[0]->GetMatrix()->GetElement(i,j);
80     trans = trans.GetInverse();
81
82     // Direction
83     typename ChangeInfoType::DirectionType direction;
84     for(unsigned int i=0; i<VImageDimension; i++)
85       for(unsigned int j=0; j<VImageDimension; j++)
86         direction[i][j] = trans[i][j];
87     info->SetOutputDirection(direction);
88     info->ChangeDirectionOn();
89
90     // Origin
91     typename ChangeInfoType::PointType origin = itkimg->GetOrigin();
92     origin = direction * origin;
93     for(unsigned int i=0; i<VImageDimension; i++)
94       origin[i] += trans[i][3];
95     info->SetOutputOrigin(origin);
96     info->ChangeOriginOn();
97   }
98   else
99     writer->SetInput(itkimg);
100
101
102   if (mUseAnObserver) {
103     writer->AddObserver(itk::ProgressEvent(), mObserver);
104   }
105   try {
106     writer->Update();
107   } catch ( itk::ExceptionObject & err ) {
108     std::cerr << "Error while reading " << mOutputFilename.c_str()
109               << " " << err << std::endl;
110     std::stringstream error;
111     error << err;
112     mLastError = error.str();
113     return;
114   }
115 }
116 //====================================================================
117
118 #endif /* end #define vvImageWriter_TXX */
119