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