]> Creatis software - clitk.git/blob - common/clitkChangeDicomTagGenericFilter.txx
Increase number of decimal for fusion and overlay
[clitk.git] / common / clitkChangeDicomTagGenericFilter.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 clitkChangeDicomTagGenericFilter_txx
19 #define clitkChangeDicomTagGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkChangeDicomTagGenericFilter.txx
23  * @author
24  * @date
25  *
26  * @brief
27  *
28  ===================================================*/
29
30 // clitk
31 #include "clitkResampleImageWithOptionsFilter.h"
32 #if GDCM_MAJOR_VERSION >= 2
33 #include "gdcmUIDGenerator.h"
34 #include <gdcmImageHelper.h>
35 #include <gdcmAttribute.h>
36 #include <gdcmReader.h>
37 #include <gdcmWriter.h>
38 #include <gdcmDataElement.h>
39 #include <gdcmTag.h>
40 #else
41 #include "gdcmFile.h"
42 #include "gdcmUtil.h"
43 #endif
44
45
46 namespace clitk
47 {
48
49
50 //-----------------------------------------------------------
51 // Constructor
52 //-----------------------------------------------------------
53 template<class args_info_type>
54 ChangeDicomTagGenericFilter<args_info_type>::ChangeDicomTagGenericFilter()
55 {
56   m_Verbose=false;
57 }
58
59
60 //-----------------------------------------------------------
61 // Update
62 //-----------------------------------------------------------
63 template<class args_info_type>
64 void ChangeDicomTagGenericFilter<args_info_type>::Update()
65 {
66   UpdateWithDimAndPixelType();
67 }
68
69 //-------------------------------------------------------------------
70 // Update with the number of dimensions and the pixeltype read from
71 // the dicom files. The MHD files may be resampled to match the
72 // dicom spacing (and number of slices). Rounding errors in resampling
73 // are handled by removing files when generating the output dicom
74 // series.
75 //-------------------------------------------------------------------
76 template<class args_info_type>
77 void
78 ChangeDicomTagGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
79 {
80   //Read the dicom file
81   gdcm::Reader reader;
82   reader.SetFileName( m_ArgsInfo.input_arg );
83   reader.Read();
84   gdcm::File &fileOutput = reader.GetFile();
85   gdcm::DataSet &dsOutput = fileOutput.GetDataSet();
86   const unsigned int ptr_len = 42;
87   char *ptr = new char[ptr_len];
88   memset(ptr,0,ptr_len);
89
90   unsigned int numberOfKeysGiven=m_ArgsInfo.key_given;
91   // For all input tag
92   for (unsigned int i = 0; i < numberOfKeysGiven; i++) {
93     // Split the tag element separated with '|' in 2 parts
94     char* copy = (char*)m_ArgsInfo.key_arg[i];
95     std::vector<char*> v;
96     char* chars_array = strtok(copy, "|");
97     while(chars_array) {
98       v.push_back(chars_array);
99       chars_array = strtok(NULL, "|");
100     }
101     std::stringstream str0(v[0]), str1(v[1]);
102     int first, second;
103     str0 >> first;
104     str1 >> second;
105
106     //Change the tag element by the corresponding value
107     gdcm::DataElement tagDE( gdcm::Tag(first, second) );
108     std::string value( m_ArgsInfo.tag_arg[i] );
109     tagDE.SetByteValue(value.c_str(), (uint32_t)strlen(value.c_str()));
110     dsOutput.Insert(tagDE);
111   }
112
113   //Write the output dicom file
114   gdcm::Writer w;
115   w.SetFile( fileOutput );
116   w.SetFileName( m_ArgsInfo.output_arg );
117   w.Write();
118 }
119
120 }//end clitk
121
122 #endif //#define clitkChangeDicomTagGenericFilter_txx