]> Creatis software - gdcm.git/blob - vtk/vtkWriteDicomExtended.cxx
BUG: On Win32 you cannot pass NULL os any ostream
[gdcm.git] / vtk / vtkWriteDicomExtended.cxx
1 // This example illustrates how the vtkGdcmWriter vtk class can be
2 // used in order to:
3 //
4 // Usage:
5 // 
6 //----------------------------------------------------------------------------
7 #include <iostream>
8
9 #include <vtkImageMapToColors.h>
10 #include <vtkLookupTable.h>
11 #include <vtkImageData.h>
12
13 #include "vtkGdcmReader.h"
14 #include "vtkGdcmWriter.h"
15
16 #ifndef vtkFloatingPointType
17 #define vtkFloatingPointType float
18 #endif
19
20 #include "gdcmArgMgr.h" // for Argument Manager functions
21 #include "gdcmFile.h"
22
23 //----------------------------------------------------------------------------
24 int main(int argc, char *argv[])
25 {
26    START_USAGE(usage)
27    " \n vtkWriteDicomExtended : \n",
28    " Reads a DICOM file and re writes it according to user's requierements.   ",
29    "                                                                          ",
30    " usage: vtkWriteDicomExtended filein=dicom file to read                   ",
31    "                           [filecontent = ] [2D]                          ",
32    "                           [noshadowseq][noshadow][noseq]                 ",
33    "                           [debug]                                        ",
34    "      filecontent = 1 : USER_OWN_IMAGE                                    ",
35    "                  = 2 : FILTERED_IMAGE                                    ",
36    "                  = 3 : CREATED_IMAGE                                     ",
37    "                  = 4 : UNMODIFIED_PIXELS_IMAGE                           ",
38    "      noshadowseq: user doesn't want to load Private Sequences            ",
39    "      noshadow   : user doesn't want to load Private groups (odd number)  ",
40    "      noseq      : user doesn't want to load Sequences                    ",
41    "      debug      : user wants to run the program in 'debug mode'          ",
42    FINISH_USAGE
43    
44    
45     // Initialize Arguments Manager   
46    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
47   
48    if (argc == 1 || am->ArgMgrDefined("usage") )
49    {
50       am->ArgMgrUsage(usage); // Display 'usage'
51       delete am;
52       return 0;
53    }
54    
55    int loadMode = gdcm::LD_ALL;
56    if ( am->ArgMgrDefined("noshadowseq") )
57       loadMode |= gdcm::LD_NOSHADOWSEQ;
58    else 
59    {
60       if ( am->ArgMgrDefined("noshadow") )
61          loadMode |= gdcm::LD_NOSHADOW;
62       if ( am->ArgMgrDefined("noseq") )
63          loadMode |= gdcm::LD_NOSEQ;
64    }
65    
66    int filecontent =  am->ArgMgrGetInt("filecontent", 1);
67    
68    char *filein = am->ArgMgrWantString("filein",usage);
69    char *fileout = (char *)(am->ArgMgrGetString("fileout","fileout"));
70    
71    if (am->ArgMgrDefined("debug"))
72       gdcm::Debug::DebugOn();
73       
74    bool deuxD = am->ArgMgrDefined("2D");
75
76    /* if unused Param we give up */
77    if ( am->ArgMgrPrintUnusedLabels() )
78    {
79       am->ArgMgrUsage(usage);
80       delete am;
81       return 0;
82    }
83    
84 // ------------------------------------------------------------  
85    std::vector<gdcm::File* > cfl;
86          
87    gdcm::File *f = gdcm::File::New();
88    f->SetFileName(filein);
89    f->Load();
90    cfl.push_back(f);
91   
92    vtkGdcmReader *reader = vtkGdcmReader::New();
93    reader->AllowLookupTableOff();
94    //reader->SetFileName( filein );
95    // in order not to parse twice the input file.
96    reader->SetCoherentFileList(&cfl);
97    reader->Update();
98
99    vtkImageData *output;
100    if( reader->GetLookupTable() )
101    {
102       //convert to color:
103       vtkImageMapToColors *map = vtkImageMapToColors::New ();
104       map->SetInput (reader->GetOutput());
105       map->SetLookupTable (reader->GetLookupTable());
106       map->SetOutputFormatToRGB();
107       output = map->GetOutput();
108       map->Delete();
109    }
110    else
111    {
112       output = reader->GetOutput();
113    }
114   
115    //print debug info:
116    output->Print(cout);
117
118    //////////////////////////////////////////////////////////
119    // WRITE...
120    //if you wish you can export dicom to a vtk file 
121    // this file will have the add of .tmp.dcm extention
122    
123    std::string fileName(filein);
124    vtkGdcmWriter *writer = vtkGdcmWriter::New();
125       
126    switch (filecontent)
127    {
128       case 1:
129          writer->SetContentTypeToUserOwnImage();
130          fileName = fileName + "_UserOwnImage.dcm";
131          break;
132  
133       case 2:
134          writer->SetContentTypeToFilteredImage();
135          writer->SetGdcmFile( f );
136          fileName = fileName + "_FilteredImage.dcm";
137          break;
138  
139       case 3:
140          writer->SetContentTypeToUserCreatedImage();
141          writer->SetGdcmFile( f );
142          fileName = fileName + "_UserCreatedImage.dcm";
143          break;
144  
145       case 4:
146          writer->SetContentTypeToUserCreatedImage();
147          writer->SetGdcmFile( f ); 
148          fileName = fileName + "_UnmodifiedPixelsImage.dcm";
149          break; 
150    }
151    
152 /// \todo : fix stupid generated image names (later : JPRx)
153
154    if(deuxD)
155    {
156          writer->SetFileDimensionality(2);
157          writer->SetFilePrefix(fileout);
158          writer->SetFilePattern("%s%d.dcm");
159    }
160    else
161    {
162       fileName += ".dcm";
163       // For 3D
164       writer->SetFileDimensionality(3);
165       writer->SetFileName(fileName.c_str());   
166    }
167
168    writer->SetInput(output);
169    writer->Write();
170    //////////////////////////////////////////////////////////
171
172    // Clean up
173    writer->Delete();
174    reader->Delete();
175
176    return 0;
177 }