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