]> Creatis software - clitk.git/blob - tools/clitkDicom2Image.cxx
First Modification for Qt5 & VTK6
[clitk.git] / tools / clitkDicom2Image.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://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
19 // clitk includes
20 #include "clitkDicom2Image_ggo.h"
21 #include "clitkCommon.h"
22 #include "clitkImageCommon.h"
23 #include "vvImageReader.h"
24 #include "vvImageWriter.h"
25 #include <gdcmFile.h>
26 #include <vtkVersion.h>
27 #include <vtkImageChangeInformation.h>
28 #if GDCM_MAJOR_VERSION == 2
29   #include <gdcmImageHelper.h>
30   #include <gdcmAttribute.h>
31   #include <gdcmReader.h>
32 #endif
33
34 #include <set>
35
36 //====================================================================
37 int main(int argc, char * argv[])
38 {
39   // init command line
40   GGO(clitkDicom2Image, args_info);
41   std::vector<std::string> input_files;
42   ///if std_input is given, read the input files from stdin
43   if (args_info.std_input_given) {
44     while (true) {
45       std::string tmp;
46       std::cin >> tmp;
47       if(std::cin.good())
48         input_files.push_back(tmp);
49       else break;
50     }
51     args_info.inputs_num=input_files.size();
52   } else for (unsigned int i=0; i<args_info.inputs_num; i++)
53       input_files.push_back(args_info.inputs[i]);
54
55
56   //===========================================
57   /// Get slices locations ...
58   int series_number = -1;
59   std::set<int> series_numbers;
60   std::map< int, std::vector<double> > theorigin;
61   std::map< int, std::vector<double> > sliceLocations;
62   std::map< int, std::vector<std::string> > seriesFiles;
63   for(unsigned int i=0; i<args_info.inputs_num; i++) {
64     //std::cout << "Reading <" << input_files[i] << std::endl;
65 #if GDCM_MAJOR_VERSION == 2
66     if (args_info.verbose_flag)
67       std::cout << "Using GDCM-2.x" << std::endl;
68     gdcm::Reader hreader;
69     hreader.SetFileName(input_files[i].c_str());
70     hreader.Read();
71     gdcm::DataSet& ds = hreader.GetFile().GetDataSet();
72
73     if (args_info.extract_series_flag) {
74       gdcm::Attribute<0x20,0x11> series_number_att;
75       series_number_att.SetFromDataSet(hreader.GetFile().GetDataSet());
76       series_number = series_number_att.GetValue();
77     }
78     
79     series_numbers.insert(series_number);
80     theorigin[series_number] = gdcm::ImageHelper::GetOriginValue(hreader.GetFile());
81     sliceLocations[series_number].push_back(theorigin[series_number][2]);
82     seriesFiles[series_number].push_back(input_files[i]);
83     
84     gdcm::Attribute<0x28, 0x100> pixel_size;
85     pixel_size.SetFromDataSet(ds);
86     /* if (pixel_size.GetValue() != 16)
87        {
88        std::cerr << "Pixel type not 2 bytes ! " << std::endl;
89        std::cerr << "In file " << input_files[i] << std::endl;
90        exit(0);
91        }
92     */
93 #else
94     if (args_info.verbose_flag)
95       std::cout << "Not using GDCM-2.x" << std::endl;
96   gdcm::File *header = new gdcm::File();
97   header->SetFileName(input_files[i]);
98   header->SetMaxSizeLoadEntry(16384); // required ?
99   header->Load();
100
101   if (args_info.extract_series_flag) {
102     series_number = atoi(header->GetEntryValue(0x20,0x11).c_str());
103   }
104   
105   series_numbers.insert(series_number);
106   theorigin[series_number].resize(3);
107   theorigin[series_number][0] = header->GetXOrigin();
108   theorigin[series_number][1] = header->GetYOrigin();
109   theorigin[series_number][2] = header->GetZOrigin();
110   sliceLocations[series_number].push_back(theorigin[series_number][2]);
111   seriesFiles[series_number].push_back(input_files[i]);
112   /*if (header->GetPixelSize() != 2) {
113     std::cerr << "Pixel type 2 bytes ! " << std::endl;
114     std::cerr << "In file " << input_files[i] << std::endl;
115     exit(0);
116   }
117   */
118 #endif
119   }
120
121   //===========================================
122   // Sort slices locations ...
123   std::set<int>::iterator sn = series_numbers.begin();
124   while ( sn != series_numbers.end() ) {
125     std::vector<double> locs = sliceLocations[*sn];
126     std::vector<double> origin = theorigin[*sn];
127     std::vector<std::string> files = seriesFiles[*sn];
128     std::vector<int> sliceIndex;
129     clitk::GetSortedIndex(locs, sliceIndex);
130     if (args_info.verboseSliceLocation_flag) {
131       std::cout << locs[sliceIndex[0]] << " -> "
132                 << sliceIndex[0] << " / " << 0 << " => "
133                 << "0 mm "
134                 << files[sliceIndex[0]]
135                 << std::endl;
136       for(unsigned int i=1; i<sliceIndex.size(); i++) {
137         std::cout << locs[sliceIndex[i]] << " -> "
138                   << sliceIndex[i] << " / " << i << " => "
139                   << locs[sliceIndex[i]] - locs[sliceIndex[i-1]]
140                   << "mm "
141                   << files[sliceIndex[i]]
142                   << std::endl;
143       }
144     }
145
146     //===========================================
147     // Analyze slices locations ...
148     double currentDist;
149     double dist=0;
150     double tolerance = args_info.tolerance_arg;
151     double previous = locs[sliceIndex[0]];
152     for(unsigned int i=1; i<sliceIndex.size(); i++) {
153       currentDist = locs[sliceIndex[i]]-previous;
154       if (i!=1) {
155         if (fabs(dist-currentDist) > tolerance) {
156           std::cout << "ERROR : " << std::endl
157                     << "Current slice pos is  = " << locs[sliceIndex[i]] << std::endl
158                     << "Previous slice pos is = " << previous << std::endl
159                     << "Current file is       = " << files[sliceIndex[i]] << std::endl
160                     << "Current index is      = " << i << std::endl
161                     << "Current sortindex is  = " << sliceIndex[i] << std::endl
162                     << "Current slice diff    = " << dist << std::endl
163                     << "Current error         = " << fabs(dist-currentDist) << std::endl;
164           exit(1);
165         }
166       } else dist = currentDist;
167       previous = locs[sliceIndex[i]];
168     }
169
170     //===========================================
171     // Create ordered vector of filenames
172     std::vector<std::string> sorted_files;
173     sorted_files.resize(sliceIndex.size());
174     for(unsigned int i=0; i<sliceIndex.size(); i++)
175       sorted_files[i] = files[ sliceIndex[i] ];
176
177     //===========================================
178     // Read write serie
179     vvImageReader::Pointer reader = vvImageReader::New();
180     reader->SetInputFilenames(sorted_files);
181     reader->Update(vvImageReader::DICOM);
182     if (reader->GetLastError().size() != 0) {
183       std::cerr << reader->GetLastError() << std::endl;
184       return 1;
185     }
186     
187     vvImage::Pointer image = reader->GetOutput();
188     vtkImageData* vtk_image = image->GetFirstVTKImageData();
189     vtkImageChangeInformation* modifier = vtkImageChangeInformation::New();
190     if  (args_info.focal_origin_given) {
191       std::vector<double> spacing = image->GetSpacing();
192       std::vector<int> size = image->GetSize();
193       origin[0] = -spacing[0]*size[0]/2.0;
194       origin[1] = -spacing[1]*size[1]/2.0;
195 #if VTK_MAJOR_VERSION <= 5
196       modifier->SetInput(vtk_image);
197 #else
198       modifier->SetInputData(vtk_image);
199 #endif
200       modifier->SetOutputOrigin(origin[0], origin[1], locs[sliceIndex[0]]);
201       modifier->Update();
202       vvImage::Pointer focal_image = vvImage::New();
203       focal_image->AddVtkImage(modifier->GetOutput());
204       image = focal_image;
205     }
206
207     std::string outfile;
208     if (series_numbers.size() == 1)
209       outfile = args_info.output_arg;
210     else {
211       std::ostringstream name;
212       name << *sn << "_" << args_info.output_arg;
213       outfile = name.str();
214     }
215     vvImageWriter::Pointer writer = vvImageWriter::New();
216     writer->SetInput(image);
217     writer->SetOutputFileName(outfile);
218     writer->Update();
219
220     modifier->Delete();
221     
222     sn++;
223   }
224   
225   return 0;
226 }