]> Creatis software - clitk.git/blob - vv/vv.cxx
Merge branch 'master' into PacsConnection
[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 "vvConfiguration.h"
36
37 #include <vtkFileOutputWindow.h>
38 #include <vtkSmartPointer.h>
39
40 #include <itkFileOutputWindow.h>
41 #include <itkSmartPointer.h>
42 #include <itksys/SystemTools.hxx>
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47 #pragma comment(lib, "ws2_32.lib")
48
49 typedef enum {O_BASE,O_OVERLAY,O_FUSION,O_VF,O_CONTOUR,O_LANDMARKS} OpenModeType;
50 typedef enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL} ParseModeType;
51
52 void load_image_first_error()
53 {
54   std::cerr << "You need to load an image before adding an overlay!" << std::endl;
55   exit(1);
56 }
57
58 std::string create_timed_string()
59 {
60   time_t t;
61   time(&t);
62
63   struct tm* pt = localtime(&t);
64
65   const int size = 64;
66   char st[size];
67   strftime(st, size, "%Y%m%d-%H%M%S", pt);
68
69   return st;
70 }
71
72 void open_sequence(vvMainWindow &window,
73                    OpenModeType &open_mode,
74                    ParseModeType &parse_mode,
75                    std::vector<std::string> &sequence_filenames,
76                    int n_image_loaded)
77 {
78   const std::string open_mode_names[] = {"base", "overlay", "fusion", "vf", "contour", "fusionSequence"};
79   if(open_mode==O_BASE)
80     window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
81   else if (open_mode==O_OVERLAY)
82     window.AddOverlayImage(n_image_loaded-1,sequence_filenames,vvImageReader::MERGEDWITHTIME);
83   else if (open_mode==O_LANDMARKS)
84     window.AddLandmarks(n_image_loaded-1,sequence_filenames);
85   else if (open_mode==O_FUSION)
86     window.AddFusionImage(n_image_loaded-1,sequence_filenames,vvImageReader::MERGEDWITHTIME);
87   else {
88     std::cerr << "Sequences are not managed for opening " << open_mode_names[open_mode] << std::endl;
89     exit(1);
90   }
91
92   // Reset
93   sequence_filenames.clear();
94   parse_mode=P_NORMAL;
95   open_mode=O_BASE;
96 }
97
98 //------------------------------------------------------------------------------
99 #ifdef _WIN32
100 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
101 {
102   int argc = __argc;
103   char **argv = __argv;
104 #else
105 int main( int argc, char** argv )
106 {
107 #endif
108
109   CLITK_INIT;
110
111   QApplication app( argc, argv );
112   Q_INIT_RESOURCE(vvIcons);
113   
114   // 
115   // ATTENTION: Rômulo Pinho - 05/08/2011
116   // Forcing the locale of the application is necessary
117   // because QT initialization changes it to the locale
118   // of the language of the system. This can cause 
119   // inconsistencies when, e.g., reading float values
120   // from DICOM fields with gdcm, since the decimal
121   // point may be changed for a comma (as in French).
122   // In practice, functions such as scanf and its
123   // variations are directly affected.
124   // https://bugreports.qt.nokia.com//browse/QTBUG-15247?page=com.atlassian.jira.plugin.system.issuetabpanels%253Achangehistory-tabpanel
125   //
126 #ifndef _WIN32
127   std::string old_locale = setlocale(LC_NUMERIC, NULL);
128   setlocale(LC_NUMERIC, "POSIX");
129 #endif
130
131   vvMainWindow window;
132
133   //Try to give the window a sensible default size
134   int width=QApplication::desktop()->width()*0.8;
135   int height=QApplication::desktop()->height()*0.9;
136   if (width> 1.5*height)
137     width=1.5*height;
138   window.resize(width,height);
139
140   window.show();
141
142   std::vector<std::string> sequence_filenames;
143   ParseModeType parse_mode = P_NORMAL;
144   OpenModeType open_mode = O_BASE;
145   int n_image_loaded=0;
146   std::string win(""), lev("");
147
148   int first_of_wl_set = -1;
149   bool new_wl_set = false;
150         bool link_images = false;
151   if (argc >1) {
152     for (int i = 1; i < argc; i++) {
153       std::string current = argv[i];
154       if (!current.compare(0,1,"-")) { // && !current.compare(0,2,"--")) { //We are parsing an option
155         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
156           open_sequence(window, open_mode, parse_mode, sequence_filenames, n_image_loaded);
157         } 
158         else if (parse_mode == P_WINDOW) { // handle negative window values
159           win=current;
160           window.ApplyWindowToSetOfImages(atof(win.c_str()), first_of_wl_set, n_image_loaded-1);
161           parse_mode=P_NORMAL;
162           new_wl_set = false;
163           continue;
164         } else if (parse_mode == P_LEVEL) { // handle negative level values
165           lev=current;
166           window.ApplyLevelToSetOfImages(atof(lev.c_str()), first_of_wl_set, n_image_loaded-1);
167           parse_mode=P_NORMAL;
168           new_wl_set = false;
169           continue;
170         }
171         if ((current=="--help") || (current=="-h")) {
172           std::cout << "vv " << VV_VERSION << ", the 2D, 2D+t, 3D and 3D+t (or 4D) image viewer" << std::endl << std::endl
173                     << "Synopsis: vv file(s) [OPTIONS] file(s)" << std::endl << std::endl
174                     << "Open file(s) for visualization." << std::endl << std::endl
175                     << "OPTIONS may be:" << std::endl
176                     << "--help         \t Print command line help and exit." << std::endl
177                     << "--window number\t Gray scale window width for set of images appearing before in the cmd line (may appear more than once)." << std::endl
178                     << "--level number \t Gray scale window level for set of images appearing before in the cmd line (may appear more than once)." << std::endl
179                     << "--linkall      \t Link pan, zoom and spatial position of crosshair in images." << std::endl
180                     << "--log          \t Log output messages in vv-log directory." << std::endl
181                     << "--state file   \t Read display parameters from file." << std::endl
182                     << "--sequence file\t Read all file(s) until next option in a single temporal sequence." << std::endl
183                     << std::endl
184                     << "These last options must follow a file name since they overlay something on an image:" << std::endl
185                     << "--vf file      \t Overlay the vector field in file." << std::endl
186                     << "--overlay file \t Overlay the image in file with complementary colors." << std::endl
187                     << "--fusion file  \t Overlay the image in file with alpha blending and colormap." << std::endl
188                     //<< "--roi file     \t Overlay binary mask images. Option may be repeated on a single base image." << std::endl
189                     << "--contour file \t Overlay DICOM RT-STRUCT contours." << std::endl
190                     << "--landmarks [--sequence] file(s)  \t Overlay the landmarks in file(s) (.txt or .pts)." << std::endl;
191           exit(0);
192         } else if (current=="--vf") {
193           if (!n_image_loaded) load_image_first_error();
194           open_mode = O_VF;
195         } else if (current=="--overlay") {
196           if (!n_image_loaded) load_image_first_error();
197           open_mode = O_OVERLAY;
198         } else if (current=="--contour") {
199           if (!n_image_loaded) load_image_first_error();
200           open_mode = O_CONTOUR;
201         } else if (current=="--fusion") {
202           if (!n_image_loaded) load_image_first_error();
203           open_mode = O_FUSION;
204         } else if (current=="--landmarks") {
205           if (!n_image_loaded) load_image_first_error();
206           open_mode = O_LANDMARKS;
207         } else if (current == "--sequence") {
208           if(open_mode==O_BASE) n_image_loaded++; //count only one for the whole sequence
209           parse_mode=P_SEQUENCE;
210           if (!new_wl_set) {
211             new_wl_set = true;
212             first_of_wl_set = n_image_loaded-1;
213           }
214         } else if (current == "--window") {
215           parse_mode=P_WINDOW;
216         } else if (current == "--level") {
217           parse_mode=P_LEVEL;
218         } else if (current == "--linkall") {
219           link_images = true;
220         }
221         else if (current == "--log") {
222           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
223
224           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
225               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
226             std::cerr << "Error creating log directory, file exists and is not a directory." << std::endl;
227             exit(1);
228           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
229             std::cerr << "Error creating log directory." << std::endl;
230             exit(1);
231           }
232
233           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
234
235           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
236           itk_log->SetFileName(log_file.c_str());
237           itk_log->FlushOn();
238           itk_log->AppendOn();
239           itk::OutputWindow::SetInstance(itk_log);
240
241           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
242           vtk_log->SetFileName(log_file.c_str());
243           vtk_log->FlushOn();
244           vtk_log->AppendOn();
245           vtkOutputWindow::SetInstance(vtk_log);
246         } else if (current == "--state") {
247           //window.ReadSavedStateFile(argv[i+1]);
248           vvReadState read_state;
249           read_state.Run(&window, argv[i+1]);
250           n_image_loaded += read_state.GetNumberOfImages();
251           i++;
252         }
253         
254       } else if (parse_mode == P_SEQUENCE) {
255         sequence_filenames.push_back(current);
256       } else if (parse_mode == P_WINDOW) {
257         win=current;
258         window.ApplyWindowToSetOfImages(atof(win.c_str()), first_of_wl_set, n_image_loaded-1);
259         parse_mode=P_NORMAL;
260         new_wl_set = false;
261       } else if (parse_mode == P_LEVEL) {
262         lev=current;
263         window.ApplyLevelToSetOfImages(atof(lev.c_str()), first_of_wl_set, n_image_loaded-1);
264         parse_mode=P_NORMAL;
265         new_wl_set = false;
266       } else {
267         std::vector<std::string> image;
268         image.push_back(current);
269         if(open_mode==O_BASE) {
270           window.LoadImages(image, vvImageReader::IMAGE);
271           n_image_loaded++;
272           if (!new_wl_set) {
273             new_wl_set = true;
274             first_of_wl_set = n_image_loaded-1;
275           }
276         }
277         else if (open_mode==O_VF)
278           window.AddField(current.c_str(), n_image_loaded-1);
279         else if (open_mode==O_OVERLAY)
280           window.AddOverlayImage(n_image_loaded-1,image,vvImageReader::IMAGE);
281         else if (open_mode==O_CONTOUR)
282           window.AddDCStructContour(n_image_loaded-1,current.c_str());
283         else if (open_mode==O_FUSION)
284           window.AddFusionImage(n_image_loaded-1,image,vvImageReader::IMAGE);
285         else if (open_mode==O_LANDMARKS)
286           window.AddLandmarks(n_image_loaded-1,image);
287         open_mode = O_BASE;
288       }
289     }
290     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
291       open_sequence(window, open_mode, parse_mode, sequence_filenames, n_image_loaded);
292     }
293   }
294
295 //   if(win!="" && lev!="") {
296 //     window.SetWindowLevel(atof(win.c_str()), atof(lev.c_str()));
297 //     window.ApplyWindowLevelToAllImages();
298 //   }
299
300   if (link_images)
301     window.LinkAllImages();
302
303   int ret = app.exec();
304   
305 #ifndef _WIN32
306   // restoring the locale, just to be clean...
307   setlocale(LC_NUMERIC, old_locale.c_str());
308 #endif
309
310   return ret;
311 }