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://www.centreleonberard.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 #include <vtkImageChangeInformation.h>
27 #if GDCM_MAJOR_VERSION == 2
28 #include <gdcmImageHelper.h>
29 #include <gdcmAttribute.h>
30 #include <gdcmReader.h>
33 //====================================================================
34 int main(int argc, char * argv[])
37 GGO(clitkDicom2Image, args_info);
38 std::vector<std::string> input_files;
39 ///if std_input is given, read the input files from stdin
40 if (args_info.std_input_given) {
45 input_files.push_back(tmp);
48 args_info.inputs_num=input_files.size();
49 } else for (unsigned int i=0; i<args_info.inputs_num; i++)
50 input_files.push_back(args_info.inputs[i]);
53 //===========================================
54 /// Get slices locations ...
55 std::vector<double> theorigin(3);
56 std::vector<double> sliceLocations;
57 for(unsigned int i=0; i<args_info.inputs_num; i++) {
58 //std::cout << "Reading <" << input_files[i] << std::endl;
59 #if GDCM_MAJOR_VERSION == 2
61 hreader.SetFileName(input_files[i].c_str());
63 theorigin = gdcm::ImageHelper::GetOriginValue(hreader.GetFile());
64 sliceLocations.push_back(theorigin[2]);
65 gdcm::Attribute<0x28, 0x100> pixel_size;
66 gdcm::DataSet& ds = hreader.GetFile().GetDataSet();
67 pixel_size.SetFromDataSet(ds);
68 if (pixel_size.GetValue() != 16)
70 std::cerr << "Pixel type 2 bytes ! " << std::endl;
71 std::cerr << "In file " << input_files[i] << std::endl;
75 gdcm::File *header = new gdcm::File();
76 header->SetFileName(input_files[i]);
77 header->SetMaxSizeLoadEntry(16384); // required ?
79 theorigin[0] = header->GetXOrigin();
80 theorigin[1] = header->GetYOrigin();
81 theorigin[2] = header->GetZOrigin();
82 sliceLocations.push_back(theorigin[2]);
83 if (header->GetPixelSize() != 2) {
84 std::cerr << "Pixel type 2 bytes ! " << std::endl;
85 std::cerr << "In file " << input_files[i] << std::endl;
91 //===========================================
92 // Sort slices locations ...
93 std::vector<int> sliceIndex;
94 clitk::GetSortedIndex(sliceLocations, sliceIndex);
95 if (args_info.verboseSliceLocation_flag) {
96 std::cout << sliceLocations[sliceIndex[0]] << " -> "
97 << sliceIndex[0] << " / " << 0 << " => "
99 << input_files[sliceIndex[0]]
101 for(unsigned int i=1; i<sliceIndex.size(); i++) {
102 std::cout << sliceLocations[sliceIndex[i]] << " -> "
103 << sliceIndex[i] << " / " << i << " => "
104 << sliceLocations[sliceIndex[i]] - sliceLocations[sliceIndex[i-1]]
106 << input_files[sliceIndex[i]]
111 //===========================================
112 // Analyze slices locations ...
115 double tolerance = args_info.tolerance_arg;
116 double previous = sliceLocations[sliceIndex[0]];
117 for(unsigned int i=1; i<sliceIndex.size(); i++) {
118 currentDist = sliceLocations[sliceIndex[i]]-previous;
120 if (fabs(dist-currentDist) > tolerance) {
121 std::cout << "ERROR : " << std::endl
122 << "Current slice pos is = " << sliceLocations[sliceIndex[i]] << std::endl
123 << "Previous slice pos is = " << previous << std::endl
124 << "Current file is = " << input_files[sliceIndex[i]] << std::endl
125 << "Current index is = " << i << std::endl
126 << "Current sortindex is = " << sliceIndex[i] << std::endl
127 << "Current slice diff = " << dist << std::endl
128 << "Current error = " << fabs(dist-currentDist) << std::endl;
131 } else dist = currentDist;
132 previous = sliceLocations[sliceIndex[i]];
135 //===========================================
136 // Create ordered vector of filenames
137 std::vector<std::string> sorted_files;
138 sorted_files.resize(sliceIndex.size());
139 for(unsigned int i=0; i<sliceIndex.size(); i++)
140 sorted_files[i] = input_files[ sliceIndex[i] ];
142 //===========================================
144 vvImageReader::Pointer reader = vvImageReader::New();
145 reader->SetInputFilenames(sorted_files);
146 reader->Update(vvImageReader::DICOM);
147 if (reader->GetLastError().size() != 0) {
148 std::cerr << reader->GetLastError() << std::endl;
152 vvImage::Pointer image = reader->GetOutput();
153 vtkImageData* vtk_image = image->GetFirstVTKImageData();
154 vtkImageChangeInformation* modifier = vtkImageChangeInformation::New();
155 if (args_info.focal_origin_given) {
156 std::vector<double> spacing = image->GetSpacing();
157 std::vector<int> size = image->GetSize();
158 theorigin[0] = -spacing[0]*size[0]/2.0;
159 theorigin[1] = -spacing[1]*size[1]/2.0;
160 modifier->SetInput(vtk_image);
161 modifier->SetOutputOrigin(theorigin[0], theorigin[1], sliceLocations[sliceIndex[0]]);
163 vvImage::Pointer focal_image = vvImage::New();
164 focal_image->AddVtkImage(modifier->GetOutput());
168 vvImageWriter::Pointer writer = vvImageWriter::New();
169 writer->SetInput(image);
170 writer->SetOutputFileName(args_info.output_arg);