]> Creatis software - clitk.git/blob - vv/vv.cxx
dicom structure in cmd line
[clitk.git] / vv / vv.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #include <utility>
19 #include <cassert>
20 #include <ctime>
21 #include <string>
22 #include <ostream>
23 #include <sstream>
24 #include <QApplication>
25 #include <QPixmap>
26 #include <QSplashScreen>
27 #include <QTimer>
28 #include <QDesktopWidget>
29 #include <QDir>
30
31 #include "clitkIO.h"
32 #include "vvMainWindow.h"
33 #include "vvReadState.h"
34 #include "vvToolsList.h"
35 #include <vtkFileOutputWindow.h>
36 #include <vtkSmartPointer.h>
37 #include <itkFileOutputWindow.h>
38 #include <itkSmartPointer.h>
39 #include <itksys/SystemTools.hxx>
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44
45 void load_image_first_error()
46 {
47   std::cerr << "You need to load an image before adding an overlay!" << std::endl;
48   exit(1);
49 }
50
51 std::string create_timed_string()
52 {
53   time_t t;
54   time(&t);
55
56   struct tm* pt = localtime(&t);
57
58   const int size = 64;
59   char st[size];
60   strftime(st, size, "%Y%m%d-%H%M%S", pt);
61
62   return st;
63 }
64
65 //------------------------------------------------------------------------------
66 #ifdef _WIN32
67 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
68 {
69   int argc = __argc;
70   char **argv = __argv;
71 #else
72 int main( int argc, char** argv )
73 {
74 #endif
75
76   CLITK_INIT;
77
78   QApplication app( argc, argv );
79   Q_INIT_RESOURCE(vvIcons);
80   
81   // 
82   // ATTENTION: Rômulo Pinho - 05/08/2011
83   // Forcing the locale of the application is necessary
84   // because QT initialization changes it to the locale
85   // of the language of the system. This can cause 
86   // inconsistencies when, e.g., reading float values
87   // from DICOM fields with gdcm, since the decimal
88   // point may be changed for a comma (as in French).
89   // In practice, functions such as scanf and its
90   // variations are directly affected.
91   // https://bugreports.qt.nokia.com//browse/QTBUG-15247?page=com.atlassian.jira.plugin.system.issuetabpanels%253Achangehistory-tabpanel
92   //
93 #ifndef _WIN32
94   std::string old_locale = setlocale(LC_NUMERIC, NULL);
95   setlocale(LC_NUMERIC, "POSIX");
96 #endif
97
98   vvMainWindow window;
99
100   //Try to give the window a sensible default size
101   int width=QApplication::desktop()->width()*0.8;
102   int height=QApplication::desktop()->height()*0.9;
103   if (width> 1.5*height)
104     width=1.5*height;
105   window.resize(width,height);
106
107   window.show();
108
109   std::vector<std::string> sequence_filenames;
110   enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL};
111   int parse_mode=P_NORMAL;
112   int n_image_loaded=0;
113   std::string win(""), lev("");
114
115         bool link_images = false;
116   if (argc >1) {
117     for (int i = 1; i < argc; i++) {
118       std::string current = argv[i];
119       if (!current.compare(0,2,"--")) { //We are parsing an option
120         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
121           window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
122           sequence_filenames.clear();
123           parse_mode=P_NORMAL;
124         }
125         if (current=="--vf") {
126           if (!n_image_loaded) load_image_first_error();
127           window.AddField(argv[i+1],n_image_loaded-1);
128           i++; //skip vf name
129         } else if (current=="--overlay") {
130           if (!n_image_loaded) load_image_first_error();
131           window.AddOverlayImage(n_image_loaded-1,argv[i+1]);
132           i++; //skip overlay name
133         } else if (current=="--roi") {
134           if (!n_image_loaded) load_image_first_error();
135           window.AddROI(n_image_loaded-1,argv[i+1]);
136           i++; //skip roi name
137         } else if (current=="--contour") {
138           if (!n_image_loaded) load_image_first_error();
139           window.AddDCStructContour(n_image_loaded-1,argv[i+1]);
140           i++; //skip roi name
141         } else if (current=="--fusion") {
142           if (!n_image_loaded) load_image_first_error();
143           window.AddFusionImage(n_image_loaded-1,argv[i+1]);
144           i++; //skip fusion name
145         } else if (current == "--sequence") {
146           n_image_loaded++; //count only one for the sequence
147           parse_mode=P_SEQUENCE;
148         } else if (current == "--window") {
149           parse_mode=P_WINDOW;
150         } else if (current == "--level") {
151           parse_mode=P_LEVEL;
152         } else if (current == "--linkall") {
153                                         link_images = true;
154                                 }
155                                 else if (current == "--log") {
156           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
157
158           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
159               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
160             std::cerr << "Error creating log directory, file exists and is not a directory." << std::endl;
161             exit(1);
162           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
163             std::cerr << "Error creating log directory." << std::endl;
164             exit(1);
165           }
166
167           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
168
169           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
170           itk_log->SetFileName(log_file.c_str());
171           itk_log->FlushOn();
172           itk_log->AppendOn();
173           itk::OutputWindow::SetInstance(itk_log);
174
175           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
176           vtk_log->SetFileName(log_file.c_str());
177           vtk_log->FlushOn();
178           vtk_log->AppendOn();
179           vtkOutputWindow::SetInstance(vtk_log);
180         } else if (current == "--state") {
181           //window.ReadSavedStateFile(argv[i+1]);
182           vvReadState read_state;
183           read_state.Run(&window, argv[i+1]);
184           n_image_loaded += read_state.GetNumberOfImages();
185           i++;
186         }
187         
188       } else if (parse_mode == P_SEQUENCE) {
189         sequence_filenames.push_back(current);
190       } else if (parse_mode == P_WINDOW) {
191         win=current;
192         parse_mode=P_NORMAL;
193       } else if (parse_mode == P_LEVEL) {
194         lev=current;
195         parse_mode=P_NORMAL;
196       } else {
197         std::vector<std::string> image;
198         image.push_back(current);
199         window.LoadImages(image, vvImageReader::IMAGE);
200         n_image_loaded++;
201       }
202     }
203     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
204       window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
205       sequence_filenames.clear();
206       parse_mode=P_NORMAL;
207     }
208   }
209
210   if(win!="" && lev!="") {
211     window.SetWindowLevel(atof(win.c_str()), atof(lev.c_str()));
212     window.ApplyWindowLevelToAllImages();
213   }
214
215         if (link_images)
216                 window.LinkAllImages();
217
218   int ret = app.exec();
219   
220 #ifndef _WIN32
221   // restoring the locale, just to be clean...
222   setlocale(LC_NUMERIC, old_locale.c_str());
223 #endif
224
225   return ret;
226 }