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