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 <vtkVersion.h>
27 #include <vtkImageChangeInformation.h>
28 #if GDCM_MAJOR_VERSION == 2
29 #include <gdcmImageHelper.h>
30 #include <gdcmAttribute.h>
31 #include <gdcmReader.h>
36 //====================================================================
37 int main(int argc, char * argv[])
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) {
48 input_files.push_back(tmp);
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]);
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;
69 hreader.SetFileName(input_files[i].c_str());
71 gdcm::DataSet& ds = hreader.GetFile().GetDataSet();
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();
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]);
84 gdcm::Attribute<0x28, 0x100> pixel_size;
85 pixel_size.SetFromDataSet(ds);
86 /* if (pixel_size.GetValue() != 16)
88 std::cerr << "Pixel type not 2 bytes ! " << std::endl;
89 std::cerr << "In file " << input_files[i] << std::endl;
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 ?
101 if (args_info.extract_series_flag) {
102 series_number = atoi(header->GetEntryValue(0x20,0x11).c_str());
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;
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 << " => "
134 << files[sliceIndex[0]]
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]]
141 << files[sliceIndex[i]]
146 //===========================================
147 // Analyze slices locations ...
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;
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;
166 } else dist = currentDist;
167 previous = locs[sliceIndex[i]];
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] ];
177 //===========================================
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;
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);
198 modifier->SetInputData(vtk_image);
200 modifier->SetOutputOrigin(origin[0], origin[1], locs[sliceIndex[0]]);
202 vvImage::Pointer focal_image = vvImage::New();
203 focal_image->AddVtkImage(modifier->GetOutput());
208 if (series_numbers.size() == 1)
209 outfile = args_info.output_arg;
211 std::ostringstream name;
212 name << *sn << "_" << args_info.output_arg;
213 outfile = name.str();
215 vvImageWriter::Pointer writer = vvImageWriter::New();
216 writer->SetInput(image);
217 writer->SetOutputFileName(outfile);