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 ===========================================================================**/
21 #include "clitkDicom2Image_ggo.h"
22 #include "clitkCommon.h"
23 #include "clitkImageCommon.h"
24 #include "vvImageReader.h"
25 #include "vvImageWriter.h"
26 #include <itkGDCMImageIO.h>
27 #include <itkGDCMSeriesFileNames.h>
29 #include <vtkVersion.h>
30 #include <vtkImageChangeInformation.h>
31 #if GDCM_MAJOR_VERSION >= 2
32 #include <gdcmImageHelper.h>
33 #include <gdcmAttribute.h>
34 #include <gdcmReader.h>
39 //====================================================================
40 int main(int argc, char * argv[])
43 GGO(clitkDicom2Image, args_info);
46 std::vector<std::string> input_files;
47 ///if std_input is given, read the input files from stdin
48 if (args_info.std_input_given) {
53 input_files.push_back(tmp);
56 args_info.inputs_num=input_files.size();
57 } else for (unsigned int i=0; i<args_info.inputs_num; i++)
58 input_files.push_back(args_info.inputs[i]);
60 //Get GDCMSeriesFileNames order to sort filenames
61 typedef itk::GDCMSeriesFileNames NamesGeneratorType;
62 NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
63 nameGenerator->SetUseSeriesDetails(false);
64 std::string folderName=".";
65 const size_t last_slash_idx = input_files[0].rfind('/');
66 if (std::string::npos != last_slash_idx)
67 folderName = input_files[0].substr(0, last_slash_idx);
68 nameGenerator->SetInputDirectory(folderName);
70 //===========================================
71 /// Get slices locations ...
72 std::string series_UID = "";
73 std::set<std::string> series_UIDs;
74 std::map< std::string, std::vector<double> > theorigin;
75 std::map< std::string, std::vector<double> > theorientation;
76 std::map< std::string, std::vector<double> > sliceLocations;
77 std::map< std::string, std::vector<std::string> > seriesFiles;
78 #if GDCM_MAJOR_VERSION >= 2
79 if (args_info.verbose_flag)
80 std::cout << "Using GDCM-2.x" << std::endl;
82 if (args_info.verbose_flag) {
83 std::cout << "Not using GDCM-2.x" << std::endl;
84 std::cout<< "The image orientation is not supported with this version of GDCM" <<std::endl;
87 for(unsigned int i=0; i<args_info.inputs_num; i++) {
88 if (args_info.verbose_flag)
89 std::cout << "Reading <" << input_files[i] << std::endl;
90 #if GDCM_MAJOR_VERSION >= 2
92 hreader.SetFileName(input_files[i].c_str());
94 gdcm::DataSet& ds = hreader.GetFile().GetDataSet();
96 gdcm::Attribute<0x20,0x000e> series_UID_att;
97 series_UID_att.SetFromDataSet(ds);
98 series_UID = series_UID_att.GetValue().c_str();
100 series_UIDs.insert(series_UID);
101 theorigin[series_UID] = gdcm::ImageHelper::GetOriginValue(hreader.GetFile());
102 theorientation[series_UID] = gdcm::ImageHelper::GetDirectionCosinesValue(hreader.GetFile());
103 if (args_info.patientSystem_flag) {
104 double n1 = theorientation[series_UID][1]*theorientation[series_UID][5]-
105 theorientation[series_UID][2]*theorientation[series_UID][4];
106 double n2 = theorientation[series_UID][3]*theorientation[series_UID][2]-
107 theorientation[series_UID][5]*theorientation[series_UID][0];
108 double n3 = theorientation[series_UID][0]*theorientation[series_UID][4]-
109 theorientation[series_UID][1]*theorientation[series_UID][3];
110 double sloc = theorigin[series_UID][0]*n1+
111 theorigin[series_UID][1]*n2+
112 theorigin[series_UID][2]*n3;
113 sliceLocations[series_UID].push_back(sloc);
115 sliceLocations[series_UID].push_back(theorigin[series_UID][2]);
116 seriesFiles[series_UID].push_back(input_files[i]);
118 gdcm::Attribute<0x28, 0x100> pixel_size;
119 pixel_size.SetFromDataSet(ds);
120 /* if (pixel_size.GetValue() != 16)
122 std::cerr << "Pixel type not 2 bytes ! " << std::endl;
123 std::cerr << "In file " << input_files[i] << std::endl;
128 gdcm::File *header = new gdcm::File();
129 header->SetFileName(input_files[i]);
130 header->SetMaxSizeLoadEntry(16384); // required ?
133 series_UID = header->GetEntryValue(0x20,0x000e).c_str();
135 series_UIDs.insert(series_UID);
136 theorigin[series_UID].resize(3);
137 theorigin[series_UID][0] = header->GetXOrigin();
138 theorigin[series_UID][1] = header->GetYOrigin();
139 theorigin[series_UID][2] = header->GetZOrigin();
140 sliceLocations[series_UID].push_back(theorigin[series_UID][2]);
141 seriesFiles[series_UID].push_back(input_files[i]);
142 /*if (header->GetPixelSize() != 2) {
143 std::cerr << "Pixel type 2 bytes ! " << std::endl;
144 std::cerr << "In file " << input_files[i] << std::endl;
151 //===========================================
152 // Sort slices locations ...
153 std::set<std::string>::iterator sn = series_UIDs.begin();
154 while ( sn != series_UIDs.end() ) {
155 std::vector<double> locs = sliceLocations[*sn];
156 std::vector<double> origin = theorigin[*sn];
157 std::vector<std::string> files = seriesFiles[*sn];
158 std::vector<int> sliceIndex(files.size());
159 //clitk::GetSortedIndex(locs, sliceIndex);
160 //Look for files into GDCMSeriesFileNames, because it sorts files correctly and take the order
161 const std::vector<std::string> & temp = nameGenerator->GetFileNames(*sn);
162 for(unsigned int i=0; i<files.size(); i++) {
165 while (!found && j<temp.size()) {
166 const size_t last_slash_idx2 = temp[j].rfind('/');
167 std::string tempFilename(temp[j]);
168 if (temp[j][0] == '.' && temp[j][1] == '/')
169 tempFilename = temp[j].substr(2, temp[j].size()-1);
170 if (tempFilename == files[i]) {
177 if (sliceIndex.size() == 0) { //ie. sn is not a serie present in files
182 if (args_info.verbose_flag) {
183 std::cout << locs[sliceIndex[0]] << " -> "
184 << sliceIndex[0] << " / " << 0 << " => "
186 << files[sliceIndex[0]]
188 for(unsigned int i=1; i<sliceIndex.size(); i++) {
189 std::cout << locs[sliceIndex[i]] << " -> "
190 << sliceIndex[i] << " / " << i << " => "
191 << locs[sliceIndex[i]] - locs[sliceIndex[i-1]]
193 << files[sliceIndex[i]]
198 //===========================================
199 // Analyze slices locations ...
202 double tolerance = args_info.tolerance_arg;
203 double previous = locs[sliceIndex[0]];
204 for(unsigned int i=1; i<sliceIndex.size(); i++) {
205 currentDist = locs[sliceIndex[i]]-previous;
207 if (fabs(dist-currentDist) > tolerance) {
208 std::cout << "ERROR : " << std::endl
209 << "Current slice pos is = " << locs[sliceIndex[i]] << std::endl
210 << "Previous slice pos is = " << previous << std::endl
211 << "Current file is = " << files[sliceIndex[i]] << std::endl
212 << "Current index is = " << i << std::endl
213 << "Current sortindex is = " << sliceIndex[i] << std::endl
214 << "Current slice diff = " << dist << std::endl
215 << "Current error = " << fabs(dist-currentDist) << std::endl;
218 } else dist = currentDist;
219 previous = locs[sliceIndex[i]];
222 //===========================================
223 // Create ordered vector of filenames
224 std::vector<std::string> sorted_files;
225 sorted_files.resize(sliceIndex.size());
226 for(unsigned int i=0; i<sliceIndex.size(); i++)
227 sorted_files[i] = files[ sliceIndex[i] ];
229 //===========================================
231 vvImageReader::Pointer reader = vvImageReader::New();
232 reader->SetInputFilenames(sorted_files);
233 reader->SetPatientCoordinateSystem(args_info.patientSystem_flag);
234 reader->Update(vvImageReader::DICOM);
235 if (reader->GetLastError().size() != 0) {
236 std::cerr << reader->GetLastError() << std::endl;
240 vvImage::Pointer image = reader->GetOutput();
241 vtkImageData* vtk_image = image->GetFirstVTKImageData();
242 vtkImageChangeInformation* modifier = vtkImageChangeInformation::New();
243 if (args_info.focal_origin_given) {
244 std::vector<double> spacing = image->GetSpacing();
245 std::vector<int> size = image->GetSize();
246 origin[0] = -spacing[0]*size[0]/2.0;
247 origin[1] = -spacing[1]*size[1]/2.0;
248 #if VTK_MAJOR_VERSION <= 5
249 modifier->SetInput(vtk_image);
251 modifier->SetInputData(vtk_image);
253 modifier->SetOutputOrigin(origin[0], origin[1], locs[sliceIndex[0]]);
255 vvImage::Pointer focal_image = vvImage::New();
256 focal_image->AddVtkImage(modifier->GetOutput());
261 if (series_UIDs.size() == 1)
262 outfile = args_info.output_arg;
264 std::ostringstream name;
265 std::vector<std::string> directory = clitk::SplitFilename(args_info.output_arg);
266 if (directory.size() == 2)
267 name << directory[0] << "/" << *sn << "_" << directory[1];
269 name << *sn << "_" << args_info.output_arg;
270 outfile = name.str();
272 vvImageWriter::Pointer writer = vvImageWriter::New();
273 writer->SetInput(image);
274 if (args_info.patientSystem_flag && !image->GetTransform().empty())
275 writer->SetSaveTransform(true);
276 writer->SetOutputFileName(outfile);