]> Creatis software - clitk.git/blob - tools/clitkDicom2Image.cxx
changes in license header
[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
26 //====================================================================
27 int main(int argc, char * argv[])
28 {
29   // init command line
30   GGO(clitkDicom2Image, args_info);
31   std::vector<std::string> input_files;
32   ///if std_input is given, read the input files from stdin
33   if (args_info.std_input_given) {
34     while (true) {
35       std::string tmp;
36       std::cin >> tmp;
37       if(std::cin.good())
38         input_files.push_back(tmp);
39       else break;
40     }
41     args_info.inputs_num=input_files.size();
42   } else for (unsigned int i=0; i<args_info.inputs_num; i++)
43       input_files.push_back(args_info.inputs[i]);
44
45
46   //===========================================
47   /// Get slices locations ...
48   std::vector<double> sliceLocations;
49   for(unsigned int i=0; i<args_info.inputs_num; i++) {
50     //std::cout << "Reading <" << input_files[i] << std::endl;
51     gdcm::File * header = clitk::readDicomHeader(input_files[i]);
52     sliceLocations.push_back(header->GetZOrigin());
53     if (header->GetPixelSize() != 2) {
54       std::cerr << "Pixel type 2 bytes ! " << std::endl;
55       std::cerr << "In file " << input_files[i] << std::endl;
56       exit(0);
57     }
58   }
59
60   //===========================================
61   // Sort slices locations ...
62   std::vector<int> sliceIndex;
63   clitk::GetSortedIndex(sliceLocations, sliceIndex);
64   if (args_info.verboseSliceLocation_flag) {
65     std::cout << sliceLocations[sliceIndex[0]] << " -> "
66               << sliceIndex[0] << " / " << 0 << " => "
67               << "0 mm "
68               << input_files[sliceIndex[0]]
69               << std::endl;
70     for(unsigned int i=1; i<sliceIndex.size(); i++) {
71       std::cout << sliceLocations[sliceIndex[i]] << " -> "
72                 << sliceIndex[i] << " / " << i << " => "
73                 << sliceLocations[sliceIndex[i]] - sliceLocations[sliceIndex[i-1]]
74                 << "mm "
75                 << input_files[sliceIndex[i]]
76                 << std::endl;
77     }
78   }
79
80   //===========================================
81   // Analyze slices locations ...
82   double currentDist;
83   double dist=0;
84   double tolerance = args_info.tolerance_arg;
85   double previous = sliceLocations[sliceIndex[0]];
86   for(unsigned int i=1; i<sliceIndex.size(); i++) {
87     currentDist = sliceLocations[sliceIndex[i]]-previous;
88     if (i!=1) {
89       if (fabs(dist-currentDist) > tolerance) {
90         std::cout << "ERROR : " << std::endl
91                   << "Current slice pos is  = " << sliceLocations[sliceIndex[i]] << std::endl
92                   << "Previous slice pos is = " << previous << std::endl
93                   << "Current file is       = " << input_files[sliceIndex[i]] << std::endl
94                   << "Current index is      = " << i << std::endl
95                   << "Current sortindex is  = " << sliceIndex[i] << std::endl
96                   << "Current slice diff    = " << dist << std::endl
97                   << "Current error         = " << fabs(dist-currentDist) << std::endl;
98         exit(1);
99       }
100     } else dist = currentDist;
101     previous = sliceLocations[sliceIndex[i]];
102   }
103
104   //===========================================
105   // Create ordered vector of filenames
106   std::vector<std::string> sorted_files;
107   sorted_files.resize(sliceIndex.size());
108   for(unsigned int i=0; i<sliceIndex.size(); i++)
109     sorted_files[i] = input_files[ sliceIndex[i] ];
110
111   //===========================================
112   // Read write serie
113   vvImageReader::Pointer reader = vvImageReader::New();
114   reader->SetInputFilenames(sorted_files);
115   reader->Update(vvImageReader::DICOM);
116   if (reader->GetLastError().size() != 0) {
117     std::cerr << reader->GetLastError() << std::endl;
118     return 1;
119   }
120
121   vvImageWriter::Pointer writer = vvImageWriter::New();
122   writer->SetInput(reader->GetOutput());
123   writer->SetOutputFileName(args_info.output_arg);
124   writer->Update();
125
126   return 0;
127 }