1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include "clitkDicom2Image_ggo.h"
21 #include "clitkCommon.h"
22 #include "clitkImageCommon.h"
23 #include "vvImageReader.h"
24 #include "vvImageWriter.h"
26 //====================================================================
27 int main(int argc, char * argv[])
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) {
38 input_files.push_back(tmp);
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]);
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;
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 << " => "
68 << input_files[sliceIndex[0]]
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]]
75 << input_files[sliceIndex[i]]
80 //===========================================
81 // Analyze slices locations ...
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;
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;
100 } else dist = currentDist;
101 previous = sliceLocations[sliceIndex[i]];
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] ];
111 //===========================================
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;
121 vvImageWriter::Pointer writer = vvImageWriter::New();
122 writer->SetInput(reader->GetOutput());
123 writer->SetOutputFileName(args_info.output_arg);