1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
24 #include <QApplication>
26 #include <QSplashScreen>
28 #include <QDesktopWidget>
31 #include <vtkAutoInit.h>
32 VTK_MODULE_INIT(vtkInteractionStyle);
33 VTK_MODULE_INIT(vtkRenderingOpenGL);
34 VTK_MODULE_INIT(vtkRenderingFreeType);
35 #define vtkRenderingContext2D_AUTOINIT 1(vtkRenderingContextOpenGL)
39 #include "vvMainWindow.h"
40 #include "vvReadState.h"
41 #include "vvToolsList.h"
42 #include "vvConfiguration.h"
44 #include <vtkFileOutputWindow.h>
45 #include <vtkSmartPointer.h>
47 #include <itkFileOutputWindow.h>
48 #include <itkSmartPointer.h>
49 #include <itksys/SystemTools.hxx>
51 #include <sys/types.h>
55 typedef enum {O_BASE,O_OVERLAY,O_FUSION,O_VF,O_CONTOUR,O_LANDMARKS} OpenModeType;
56 typedef enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL} ParseModeType;
58 void load_image_first_error()
60 std::cerr << "You need to load an image before adding an overlay!" << std::endl;
64 std::string create_timed_string()
69 struct tm* pt = localtime(&t);
73 strftime(st, size, "%Y%m%d-%H%M%S", pt);
78 void open_sequence(vvMainWindow &window,
79 OpenModeType &open_mode,
80 ParseModeType &parse_mode,
81 std::vector<std::string> &sequence_filenames,
84 const std::string open_mode_names[] = {"base", "overlay", "fusion", "vf", "contour", "fusionSequence"};
86 window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
87 else if (open_mode==O_OVERLAY && window.CheckAddedImage(n_image_loaded-1, "overlay"))
88 window.AddOverlayImage(n_image_loaded-1,sequence_filenames,vvImageReader::MERGEDWITHTIME);
89 else if (open_mode==O_LANDMARKS)
90 window.AddLandmarks(n_image_loaded-1,sequence_filenames);
91 else if (open_mode==O_FUSION && window.CheckAddedImage(n_image_loaded-1, "fusion") && window.CheckAddedImage(n_image_loaded-1, "fusionSequence"))
92 window.AddFusionImage(n_image_loaded-1,sequence_filenames,vvImageReader::MERGEDWITHTIME);
94 std::cerr << "Sequences are not managed for opening " << open_mode_names[open_mode] << std::endl;
99 sequence_filenames.clear();
104 //------------------------------------------------------------------------------
106 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
109 char **argv = __argv;
111 int main( int argc, char** argv )
117 QApplication app( argc, argv );
118 Q_INIT_RESOURCE(vvIcons);
121 // ATTENTION: Rômulo Pinho - 05/08/2011
122 // Forcing the locale of the application is necessary
123 // because QT initialization changes it to the locale
124 // of the language of the system. This can cause
125 // inconsistencies when, e.g., reading float values
126 // from DICOM fields with gdcm, since the decimal
127 // point may be changed for a comma (as in French).
128 // In practice, functions such as scanf and its
129 // variations are directly affected.
130 // https://bugreports.qt.nokia.com//browse/QTBUG-15247?page=com.atlassian.jira.plugin.system.issuetabpanels%253Achangehistory-tabpanel
133 std::string old_locale = setlocale(LC_NUMERIC, NULL);
134 setlocale(LC_NUMERIC, "POSIX");
139 //Try to give the window a sensible default size
140 int width=QApplication::desktop()->width()*0.8;
141 int height=QApplication::desktop()->height()*0.9;
142 if (width> 1.5*height)
144 window.resize(width,height);
148 std::vector<std::string> sequence_filenames;
149 ParseModeType parse_mode = P_NORMAL;
150 OpenModeType open_mode = O_BASE;
151 int n_image_loaded=0;
152 std::string win(""), lev("");
154 int first_of_wl_set = -1;
155 bool new_wl_set = false;
156 bool link_images = false;
158 for (int i = 1; i < argc; i++) {
159 std::string current = argv[i];
160 if (!current.compare(0,1,"-")) { // && !current.compare(0,2,"--")) { //We are parsing an option
161 if (parse_mode == P_SEQUENCE) {//First finish the current sequence
162 open_sequence(window, open_mode, parse_mode, sequence_filenames, n_image_loaded);
164 else if (parse_mode == P_WINDOW) { // handle negative window values
166 window.ApplyWindowToSetOfImages(atof(win.c_str()), first_of_wl_set, n_image_loaded-1);
170 } else if (parse_mode == P_LEVEL) { // handle negative level values
172 window.ApplyLevelToSetOfImages(atof(lev.c_str()), first_of_wl_set, n_image_loaded-1);
177 if ((current=="--help") || (current=="-h")) {
178 std::cout << "vv " << VV_VERSION << ", the 2D, 2D+t, 3D and 3D+t (or 4D) image viewer" << std::endl << std::endl
179 << "Synopsis: vv file(s) [OPTIONS] file(s)" << std::endl << std::endl
180 << "Open file(s) for visualization." << std::endl << std::endl
181 << "OPTIONS may be:" << std::endl
182 << "--help \t Print command line help and exit." << std::endl
183 << "--window number\t Gray scale window width for set of images appearing before in the cmd line (may appear more than once)." << std::endl
184 << "--level number \t Gray scale window level for set of images appearing before in the cmd line (may appear more than once)." << std::endl
185 << "--linkall \t Link pan, zoom and spatial position of crosshair in images." << std::endl
186 << "--log \t Log output messages in vv-log directory." << std::endl
187 << "--state file \t Read display parameters from file." << std::endl
188 << "--sequence file\t Read all file(s) until next option in a single temporal sequence." << std::endl
190 << "These last options must follow a file name since they overlay something on an image:" << std::endl
191 << "--vf file \t Overlay the vector field in file." << std::endl
192 << "--overlay [--sequence] file(s) \t Overlay the image in file with complementary colors." << std::endl
193 << "--fusion [--sequence] file(s) \t Overlay the image in file with alpha blending and colormap." << std::endl
194 //<< "--roi file \t Overlay binary mask images. Option may be repeated on a single base image." << std::endl
195 << "--contour file \t Overlay DICOM RT-STRUCT contours." << std::endl
196 << "--landmarks [--sequence] file(s) \t Overlay the landmarks in file(s) (.txt or .pts)." << std::endl;
198 } else if (current=="--vf") {
199 if (!n_image_loaded) load_image_first_error();
201 } else if (current=="--overlay") {
202 if (!n_image_loaded) load_image_first_error();
203 open_mode = O_OVERLAY;
204 } else if (current=="--contour") {
205 if (!n_image_loaded) load_image_first_error();
206 open_mode = O_CONTOUR;
207 } else if (current=="--fusion") {
208 if (!n_image_loaded) load_image_first_error();
209 open_mode = O_FUSION;
210 } else if (current=="--landmarks") {
211 if (!n_image_loaded) load_image_first_error();
212 open_mode = O_LANDMARKS;
213 } else if (current == "--sequence") {
214 if(open_mode==O_BASE) n_image_loaded++; //count only one for the whole sequence
215 parse_mode=P_SEQUENCE;
218 first_of_wl_set = n_image_loaded-1;
220 } else if (current == "--window") {
222 } else if (current == "--level") {
224 } else if (current == "--linkall") {
227 else if (current == "--log") {
228 std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
230 if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
231 !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
232 std::cerr << "Error creating log directory, file exists and is not a directory." << std::endl;
234 } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
235 std::cerr << "Error creating log directory." << std::endl;
239 std::string log_file = log_dir + "/" + create_timed_string() + ".log";
241 itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
242 itk_log->SetFileName(log_file.c_str());
245 itk::OutputWindow::SetInstance(itk_log);
247 vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
248 vtk_log->SetFileName(log_file.c_str());
251 vtkOutputWindow::SetInstance(vtk_log);
252 } else if (current == "--state") {
253 //window.ReadSavedStateFile(argv[i+1]);
254 vvReadState read_state;
255 read_state.Run(&window, argv[i+1]);
256 n_image_loaded += read_state.GetNumberOfImages();
260 } else if (parse_mode == P_SEQUENCE) {
261 sequence_filenames.push_back(current);
262 } else if (parse_mode == P_WINDOW) {
264 window.ApplyWindowToSetOfImages(atof(win.c_str()), first_of_wl_set, n_image_loaded-1);
267 } else if (parse_mode == P_LEVEL) {
269 window.ApplyLevelToSetOfImages(atof(lev.c_str()), first_of_wl_set, n_image_loaded-1);
273 std::vector<std::string> image;
274 image.push_back(current);
275 if(open_mode==O_BASE) {
276 window.LoadImages(image, vvImageReader::IMAGE);
280 first_of_wl_set = n_image_loaded-1;
283 else if (open_mode==O_VF && window.CheckAddedImage(n_image_loaded-1, "vector"))
284 window.AddField(current.c_str(), n_image_loaded-1);
285 else if (open_mode==O_OVERLAY && window.CheckAddedImage(n_image_loaded-1, "overlay"))
286 window.AddOverlayImage(n_image_loaded-1,image,vvImageReader::IMAGE);
287 else if (open_mode==O_CONTOUR)
288 window.AddDCStructContour(n_image_loaded-1,current.c_str());
289 else if (open_mode==O_FUSION && window.CheckAddedImage(n_image_loaded-1, "fusion") && window.CheckAddedImage(n_image_loaded-1, "fusionSequence"))
290 window.AddFusionImage(n_image_loaded-1,image,vvImageReader::IMAGE);
291 else if (open_mode==O_LANDMARKS)
292 window.AddLandmarks(n_image_loaded-1,image);
296 if (parse_mode == P_SEQUENCE) { //Finish any current sequence
297 open_sequence(window, open_mode, parse_mode, sequence_filenames, n_image_loaded);
301 // if(win!="" && lev!="") {
302 // window.SetWindowLevel(atof(win.c_str()), atof(lev.c_str()));
303 // window.ApplyWindowLevelToAllImages();
307 window.LinkAllImages();
309 int ret = app.exec();
312 // restoring the locale, just to be clean...
313 setlocale(LC_NUMERIC, old_locale.c_str());