]> Creatis software - clitk.git/blob - tools/clitkDicomRTDose2Image.cxx
Rolled back to middle slice on first load
[clitk.git] / tools / clitkDicomRTDose2Image.cxx
1 // clitk includes
2 #include "clitkDicomRTDose2Image_ggo.h"
3 #include "clitkCommon.h"
4 #include "clitkImageCommon.h"
5 #include "itkImageFileReader.h"
6 #include "itkImageFileWriter.h"
7 #include <gdcmFile.h>
8 #if GDCM_MAJOR_VERSION == 2
9   #include <gdcmImageHelper.h>
10   #include <gdcmAttribute.h>
11   #include <gdcmReader.h>
12 #endif
13
14 //====================================================================
15 int main(int argc, char * argv[])
16 {
17   // init command line
18   GGO(clitkDicomRTDose2Image, args_info);
19   //===========================================
20   // Read write serie
21
22   const unsigned int Dim = 3;
23   typedef unsigned int ImageValueType;
24   typedef itk::Image<ImageValueType, Dim> ImageType;
25   typedef itk::ImageFileReader<ImageType> ReaderType;
26   
27   ReaderType::Pointer reader = ReaderType::New();
28   reader->SetFileName(args_info.input_arg);
29   reader->Update();
30   
31   ImageType::Pointer image = reader->GetOutput();
32
33   typedef float DoseValueType;
34   typedef itk::Image<DoseValueType, Dim> DoseImageType;
35   DoseImageType::Pointer output_image = DoseImageType::New();
36   output_image->SetOrigin(image->GetOrigin());
37   output_image->SetSpacing(image->GetSpacing());
38   output_image->SetDirection(image->GetDirection());
39   output_image->SetRegions(image->GetLargestPossibleRegion());
40   output_image->Allocate();
41   
42   typedef itk::ImageRegionConstIterator<ImageType> InputIteratorType;
43   InputIteratorType it_in(image, image->GetLargestPossibleRegion());
44   it_in.GoToBegin();
45
46   typedef itk::ImageRegionIterator<DoseImageType> OutputIteratorType;
47   OutputIteratorType it_out(output_image, output_image->GetLargestPossibleRegion());
48   it_out.GoToBegin();
49   
50   double dose_scale = 1;
51   std::string modality;
52 #if GDCM_MAJOR_VERSION == 2
53     if (args_info.verbose_flag)
54       std::cout << "Using GDCM-2.x" << std::endl;
55     gdcm::Reader hreader;
56     hreader.SetFileName(args_info.input_arg);
57     hreader.Read();
58     gdcm::DataSet& ds = hreader.GetFile().GetDataSet();
59     gdcm::Attribute<0x0008, 0x0060> attr_modality;
60     attr_modality.SetFromDataSet(ds);
61     modality = attr_modality.GetValue();
62     if (modality != "RTDOSE") {
63       FATAL("Dicom modality (0x0008, 0x0060) must be RTDOSE.\n");
64     }
65     gdcm::Attribute<0x3004, 0x000e> attr_dose_grid_scaling;
66     attr_dose_grid_scaling.SetFromDataSet(ds);
67     dose_scale = attr_dose_grid_scaling.GetValue();
68 #else
69     if (args_info.verbose_flag)
70       std::cout << "Not using GDCM-2.x" << std::endl;
71   gdcm::File *header = new gdcm::File();
72   header->SetFileName(input_files[i]);
73   header->SetMaxSizeLoadEntry(16384); // required ?
74   header->Load();
75   modality = atof(header->GetEntryValue(0x0008, 0x0060).c_str());
76   if (modality != "RTDOSE") {
77     FATAL("Dicom modality (0x0008, 0x0060) must be RTDOSE.\n");
78   }
79   dose_scale = atof(header->GetEntryValue(0x3004,0x000e).c_str());
80 #endif
81
82   if (args_info.verbose_flag) {
83     std::cout << "Dose Grid Scaling is " << dose_scale << std::endl;
84   }
85   
86   while (!it_in.IsAtEnd()) {
87     it_out.Set(it_in.Get() * dose_scale);
88     ++it_in;
89     ++it_out;
90   }
91   
92   typedef itk::ImageFileWriter<DoseImageType> WriterType;
93   WriterType::Pointer writer = WriterType::New();
94   writer->SetInput(output_image);
95   writer->SetFileName(args_info.output_arg);
96   writer->Update();
97   
98   return 0;
99 }