]> Creatis software - clitk.git/blob - common/vvImageWriter.txx
Prompt transform backup when saving image in vv.
[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 == "double") {
42     UpdateWithDimAndOutputPixelType<double,VImageDimension>();
43   } else if (OutputPixelType == "float") {
44     UpdateWithDimAndOutputPixelType<float,VImageDimension>();
45   } else {
46     std::cerr << "Error, output pixel type : \"" << OutputPixelType << "\" unknown !" << std::endl;
47   }
48 }
49 //====================================================================
50
51 //====================================================================
52 template<class OutputPixelType, unsigned int VImageDimension>
53 void vvImageWriter::UpdateWithDimAndOutputPixelType()
54 {
55   // The ITK image
56   typedef itk::Image< OutputPixelType, VImageDimension > OutputImageType;
57   typename OutputImageType::ConstPointer itkimg = vvImageToITK<OutputImageType>(mImage);
58
59   //Create the writer
60   typedef itk::ImageFileWriter<OutputImageType> WriterType;
61   typename WriterType::Pointer writer = WriterType::New();
62   writer->SetFileName(mOutputFilename);
63
64   //Change information if it must transformation must be saved
65   typedef itk::ChangeInformationImageFilter<OutputImageType> ChangeInfoType;
66   typename ChangeInfoType::Pointer info = ChangeInfoType::New();
67   if(mSaveTransform) {
68     // Set pipeline
69     info->SetInput(itkimg);
70     writer->SetInput(info->GetOutput());
71
72     // Inverse vv matrix
73     itk::Matrix<double, 4, 4> trans;
74     for(int i=0; i<4; i++)
75       for(int j=0; j<4; j++)
76         trans[i][j] = mImage->GetTransform()->GetMatrix()->GetElement(i,j);
77     trans = trans.GetInverse();
78
79     // Direction
80     typename ChangeInfoType::DirectionType direction;
81     for(unsigned int i=0; i<VImageDimension; i++)
82       for(unsigned int j=0; j<VImageDimension; j++)
83         direction[i][j] = trans[i][j];
84     info->SetOutputDirection(direction);
85     info->ChangeDirectionOn();
86
87     // Origin
88     typename ChangeInfoType::PointType origin = itkimg->GetOrigin();
89     origin = direction * origin;
90     for(unsigned int i=0; i<VImageDimension; i++)
91       origin[i] += trans[i][3];
92     info->SetOutputOrigin(origin);
93     info->ChangeOriginOn();
94   }
95   else
96     writer->SetInput(itkimg);
97
98
99   if (mUseAnObserver) {
100     writer->AddObserver(itk::ProgressEvent(), mObserver);
101   }
102   try {
103     writer->Update();
104   } catch ( itk::ExceptionObject & err ) {
105     std::cerr << "Error while reading " << mOutputFilename.c_str()
106               << " " << err << std::endl;
107     std::stringstream error;
108     error << err;
109     mLastError = error.str();
110     return;
111   }
112 }
113 //====================================================================
114
115 #endif /* end #define vvImageWriter_TXX */
116