contrib/brl/vvid/vvid_vil_file_manager.cxx
Go to the documentation of this file.
00001 // This is brl/vvid/vvid_vil_file_manager.cxx
00002 #include "vvid_vil_file_manager.h"
00003 //:
00004 // \file
00005 // \author J.L. Mundy
00006 
00007 #include <vcl_vector.h>
00008 #include <vcl_iostream.h>
00009 #include <vil/vil_image_list.h>
00010 #include <vil/vil_image_resource.h>
00011 #include <vidl1/vidl1_movie.h>
00012 #include <vidl1/vidl1_io.h>
00013 #include <vgui/vgui.h>
00014 #include <vgui/vgui_error_dialog.h>
00015 #include <vgui/vgui_adaptor.h>
00016 #include <vgui/vgui_dialog.h>
00017 #include <vgui/vgui_shell_tableau.h>
00018 #include <vgui/vgui_range_map_params.h>
00019 #include <bgui/bgui_image_tableau.h>
00020 #include <bgui/bgui_vsol2D_tableau.h>
00021 #include <bgui/bgui_picker_tableau.h>
00022 #include <bgui/bgui_bargraph_clipon_tableau.h>
00023 #include <vgui/vgui_viewer2D_tableau.h>
00024 #include <vgui/vgui_grid_tableau.h>
00025 #include <vgui/vgui_image_tableau.h>
00026 #if 0 // currently unused
00027 #include <vsol/vsol_point_2d.h>
00028 #include <vsol/vsol_polygon_2d.h>
00029 #endif
00030 #include <vpro/vpro_vil_video_process.h>
00031 #include <vpro/vpro_roi_process.h>
00032 
00033 //static manager instance
00034 vvid_vil_file_manager *vvid_vil_file_manager::instance_ = 0;
00035 
00036 //The vvid_vil_file_manager is a singleton class
00037 vvid_vil_file_manager *vvid_vil_file_manager::instance()
00038 {
00039   if (!instance_)
00040   {
00041     instance_ = new vvid_vil_file_manager();
00042     instance_->init();
00043   }
00044   return vvid_vil_file_manager::instance_;
00045 }
00046 
00047 //======================================================================
00048 //: set up the tableaux at each grid cell
00049 //======================================================================
00050 void vvid_vil_file_manager::init()
00051 {
00052   grid_ = vgui_grid_tableau_new(1,1);
00053   grid_->set_grid_size_changeable(true);
00054 
00055   //  itab0_ = vgui_image_tableau_new();
00056   itab0_ = bgui_image_tableau_new();
00057   easy0_ = vgui_easy2D_tableau_new(itab0_);
00058   picktab0_ = bgui_picker_tableau_new(easy0_);
00059   v2D0_ = vgui_viewer2D_tableau_new(picktab0_);
00060   grid_->add_at(v2D0_, 0,0);
00061 #if 0
00062   itab1_ = bgui_image_tableau_new();
00063   easy1_ = bgui_vtol2D_tableau_new(itab1_);
00064   v2D1_ = vgui_viewer2D_tableau_new(easy1_);
00065   grid_->add_at(v2D1_, 1,0);
00066 #endif
00067   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00068   this->add_child(shell);
00069 
00070   display_frame_repeat_=1;
00071   start_frame_ = 0;
00072   end_frame_ = 0;
00073 }
00074 
00075 //-----------------------------------------------------------
00076 // constructors/destructor
00077 // start with a single pane
00078 vvid_vil_file_manager::vvid_vil_file_manager(): vgui_wrapper_tableau()
00079 {
00080   width_ = 512;
00081   height_ = 512;
00082 
00083   window_ = 0;
00084 
00085   my_movie_=(vidl1_movie*)0;
00086   win_ = 0;
00087   play_video_ = true;
00088   pause_video_ = false;
00089   next_frame_ = false;
00090   prev_frame_ = false;
00091   time_interval_ = 10.0;
00092   rmps_ = (vgui_range_map_params*)0;
00093 
00094   display_frame_repeat_=1;
00095 }
00096 
00097 vvid_vil_file_manager::~vvid_vil_file_manager()
00098 {
00099 }
00100 
00101 
00102 // make an event handler
00103 // note that we have to get an adaptor and set the tableau to receive events
00104 bool vvid_vil_file_manager::handle(const vgui_event &e)
00105 {
00106   return this->child.handle(e);
00107 }
00108 
00109 void vvid_vil_file_manager::quit()
00110 {
00111   vgui::quit();
00112 }
00113 
00114 //-----------------------------------------------------------------------------
00115 //: Loads a video file, e.g. avi into the viewer
00116 //-----------------------------------------------------------------------------
00117 void vvid_vil_file_manager::load_video_file()
00118 {
00119   play_video_ = true;
00120   pause_video_ = false;
00121   next_frame_ = false;
00122   prev_frame_ = false;
00123   float gamma = 1.0;
00124   bool invert = false;
00125   bool gl_map = true;
00126   bool cache = true;
00127   vgui_dialog load_video_dlg("Load video file");
00128   static vcl_string image_filename = "";
00129   static vcl_string ext = "";
00130   load_video_dlg.file("Filename:", ext, image_filename);
00131   load_video_dlg.checkbox("Cache Frames ", cache_frames_);
00132   if (!load_video_dlg.ask())
00133     return;
00134 
00135   my_movie_ = vidl1_io::load_movie(image_filename.c_str());
00136   if (!my_movie_) {
00137     vgui_error_dialog("Failed to load movie file");
00138     return;
00139   }
00140   tabs_.clear();
00141   int n_frames = my_movie_->length();
00142   start_frame_ = 0;
00143   end_frame_ = n_frames-1;
00144   vidl1_movie::frame_iterator pframe = my_movie_->first();
00145   vil_image_resource_sptr img = pframe->get_resource();
00146   if (!img)
00147     return;
00148   unsigned max = 255;
00149   if (img->pixel_format()==VIL_PIXEL_FORMAT_UINT_16)
00150     max =4095;
00151   rmps_=
00152     new vgui_range_map_params(0, max, gamma, invert,
00153                               gl_map, cache);
00154   //  vil1_image second = (++pframe)->get_image();
00155   height_ = img->ni();
00156   width_ = img->nj();
00157   vcl_cout << "Video Height " << height_ << vcl_endl
00158            << "Video Width  " << width_ << vcl_endl;
00159   unsigned display_width =2*width_, display_height = height_;
00160   if (display_width>1000)
00161     display_width = 1000;
00162   if (display_height>900)
00163     display_height = 900;
00164   if (win_)
00165     win_->reshape(display_width, display_height);
00166   itab0_->set_image_resource(img);
00167   itab0_->set_mapping(rmps_);
00168   grid_->post_redraw();
00169   vgui::run_till_idle();
00170 }
00171 
00172 //-----------------------------------------------------------------------------
00173 //: Loads a pyramid video
00174 //-----------------------------------------------------------------------------
00175 void vvid_vil_file_manager::load_pyramid_video()
00176 {
00177   play_video_ = true;
00178   pause_video_ = false;
00179   next_frame_ = false;
00180   prev_frame_ = false;
00181   float gamma = 1.0;
00182   bool invert = false;
00183   bool gl_map = true;
00184   bool cache = false;
00185   vgui_dialog load_video_dlg("Pyramid Video Dir");
00186   static vcl_string video_dir = "";
00187   static vcl_string ext = "";
00188   load_video_dlg.file("Filename:", ext, video_dir);
00189   if (!load_video_dlg.ask())
00190     return;
00191   vil_image_list il(video_dir.c_str());
00192   pyramid_movie_ = il.pyramids();
00193   if (pyramid_movie_.size()==0)
00194   {
00195     vgui_error_dialog("Failed to load pyramid movie");
00196     return;
00197   }
00198 
00199   unsigned n_frames = pyramid_movie_.size();
00200   start_frame_ = 0;
00201   end_frame_ = n_frames-1;
00202   vil_image_resource_sptr img = pyramid_movie_[0].ptr();
00203   if (!img)
00204     return;
00205   unsigned max = 255;
00206   rmps_=
00207     new vgui_range_map_params(0, max, gamma, invert,
00208                               gl_map, cache);
00209   if (img->pixel_format()==VIL_PIXEL_FORMAT_UINT_16)
00210     max =4095;
00211   //  vil1_image second = (++pframe)->get_image();
00212   height_ = img->ni();
00213   width_ = img->nj();
00214   vcl_cout << "Video Height " << height_ << vcl_endl
00215            << "Video Width  " << width_ << vcl_endl;
00216   unsigned display_width =2*width_, display_height = height_;
00217   if (display_width>1000)
00218     display_width = 1000;
00219   if (display_height>900)
00220     display_height = 900;
00221   if (win_)
00222     win_->reshape(display_width, display_height);
00223   itab0_->set_image_resource(img);
00224   itab0_->set_mapping(rmps_);
00225   grid_->post_redraw();
00226   vgui::run_till_idle();
00227 }
00228 
00229 //----------------------------------------------
00230 void vvid_vil_file_manager::un_cached_play()
00231 {
00232   if (!my_movie_)
00233   {
00234     vcl_cout << "No movie has been loaded\n";
00235     return;
00236   }
00237   if (video_process_)
00238     video_process_->set_n_frames(my_movie_->length());
00239   vidl1_movie::frame_iterator pframe = my_movie_->begin();
00240   vidl1_movie::frame_iterator lframe = pframe;
00241   pframe += start_frame_;
00242   lframe += end_frame_+1;
00243   for ( ; pframe!=my_movie_->end() && pframe!=lframe && play_video_; ++pframe)
00244   {
00245     int frame_index = pframe->get_real_frame_index();
00246     vgui::out << "frame["<< frame_index <<"]\n";
00247     vil_image_resource_sptr imgr = pframe->get_resource();
00248     if (!imgr)
00249       continue;
00250     itab0_->set_image_resource(imgr);
00251     itab0_->set_mapping(rmps_);
00252 
00253     // pause by repeating the same frame
00254     if (pause_video_)
00255     {
00256       if (next_frame_)
00257       {
00258         if (pframe!=my_movie_->last()) ++pframe;
00259         next_frame_ = false;
00260       }
00261       if (prev_frame_)
00262       {
00263         if (pframe!=my_movie_->first()) --pframe;
00264         prev_frame_ = false;
00265       }
00266       // repeat the same frame by undoing the subsequent ++pframe of the iteration
00267       --pframe;
00268     }
00269     else if (video_process_)
00270     {
00271       video_process_->set_frame_index(frame_index);
00272       video_process_->add_input_image(imgr);
00273       if (video_process_->execute())
00274       {
00275         vcl_cout << "Executing Frame " << frame_index << '\n';
00276       }
00277     }
00278     grid_->post_redraw();
00279     vgui::run_till_idle();
00280   }
00281   if (video_process_)
00282     video_process_->finish();
00283  }
00284 
00285 void vvid_vil_file_manager::pyramid_play()
00286 {
00287   unsigned n_frames = pyramid_movie_.size();
00288   if (n_frames == 0)
00289   {
00290     vcl_cout << "No pyramid movie has been loaded\n";
00291     return;
00292   }
00293   for (unsigned pframe = 0 ; pframe<n_frames && play_video_; ++pframe)
00294   {
00295     vgui::out << "frame["<< pframe <<"]\n";
00296     vil_image_resource_sptr imgr = pyramid_movie_[pframe];
00297     if (!imgr)
00298       continue;
00299     itab0_->set_image_resource(imgr);
00300     itab0_->set_mapping(rmps_);
00301 
00302     // pause by repeating the same frame
00303     if (pause_video_)
00304     {
00305       if (next_frame_)
00306       {
00307         if (pframe!=n_frames-1) ++pframe;
00308         next_frame_ = false;
00309       }
00310       if (prev_frame_)
00311       {
00312         if (pframe!=0) --pframe;
00313         prev_frame_ = false;
00314       }
00315       // repeat the same frame by undoing the subsequent ++pframe of the iteration
00316       --pframe;
00317     }
00318     grid_->post_redraw();
00319     vgui::run_till_idle();
00320   }
00321 }
00322 
00323 void vvid_vil_file_manager::play_video()
00324 {
00325   play_video_ = true;
00326   pause_video_ = false;
00327   time_interval_ = 10.0;
00328   easy0_->clear();
00329 
00330   //return the display to the first frame after the play is finished
00331   this->un_cached_play();
00332   if (!my_movie_)
00333     return;
00334   this->post_redraw();
00335 }
00336 
00337 void vvid_vil_file_manager::play_pyramid()
00338 {
00339   play_video_ = true;
00340   pause_video_ = false;
00341   time_interval_ = 10.0;
00342   easy0_->clear();
00343 
00344   //return the display to the first frame after the play is finished
00345   this->pyramid_play();
00346   this->post_redraw();
00347 }
00348 
00349 //Player control functions
00350 
00351 //Stop the video and return to the first frame
00352 void vvid_vil_file_manager::stop_video()
00353 {
00354   play_video_ = false;
00355 }
00356 
00357 //Cycle the play at the current frame
00358 void vvid_vil_file_manager::pause_video()
00359 {
00360   pause_video_ =!pause_video_;
00361   //make the time interval longer for paused state
00362   time_interval_ = 50.0;
00363 }
00364 
00365 void vvid_vil_file_manager::start_frame()
00366 {
00367   vgui_dialog frame_dlg("Start Frame");
00368   frame_dlg.field("Frame No.", start_frame_);
00369   if (!frame_dlg.ask())
00370     return;
00371 }
00372 
00373 void vvid_vil_file_manager::end_frame()
00374 {
00375   vgui_dialog frame_dlg("End Frame");
00376   frame_dlg.field("Frame No.", end_frame_);
00377   if (!frame_dlg.ask())
00378     return;
00379 }
00380 
00381 
00382 //While the video is paused go to the next frame
00383 void vvid_vil_file_manager::next_frame()
00384 {
00385   next_frame_ = true;
00386 }
00387 
00388 //While the video is paused go to the previous frame
00389 void vvid_vil_file_manager::prev_frame()
00390 {
00391   prev_frame_ = true;
00392 }
00393 
00394 void vvid_vil_file_manager::set_speed()
00395 {
00396   //not implemented yet
00397 }
00398 
00399 void vvid_vil_file_manager::set_range_params()
00400 {
00401   if (!itab0_||!itab0_->get_image_resource())
00402     return;
00403   static double min = 0, max = 255;
00404   static float gamma = 1.0;
00405   static bool invert = false;
00406   static bool gl_map = false;
00407   static bool cache = false;
00408   if (rmps_)
00409   {
00410     invert = rmps_->invert_;
00411     gl_map = rmps_->use_glPixelMap_;
00412     cache = rmps_->cache_mapped_pix_;
00413     if (rmps_->max_L_ > 0)
00414     {
00415       min = rmps_->min_L_;
00416       max = rmps_->max_L_;
00417       gamma = rmps_->gamma_L_;
00418     }
00419     else
00420     {
00421       min = rmps_->min_R_;
00422       max = rmps_->max_R_;
00423       gamma = rmps_->gamma_R_;
00424     }
00425   }
00426   vgui_dialog range_dlg("Set Range Map Params");
00427   range_dlg.field("Range min:", min);
00428   range_dlg.field("Range max:", max);
00429   range_dlg.field("Gamma:", gamma);
00430   range_dlg.checkbox("Invert:", invert);
00431   range_dlg.checkbox("Use GL Mapping", gl_map);
00432   range_dlg.checkbox("Cache Pixels", cache);
00433   if (!range_dlg.ask())
00434     return;
00435   vil_image_resource_sptr img = itab0_->get_image_resource();
00436   unsigned n_components = img->nplanes();
00437 
00438   if (n_components == 1)
00439   {
00440     rmps_=
00441       new vgui_range_map_params(min, max, gamma, invert,
00442                               gl_map, cache);
00443   }
00444   else if (n_components == 3)
00445      rmps_ =
00446       new vgui_range_map_params(min, max, min, max, min, max,
00447                                 gamma, gamma, gamma, invert,
00448                                 gl_map, cache);
00449 
00450   itab0_->set_mapping(rmps_);
00451 }
00452 
00453 void vvid_vil_file_manager::create_box()
00454 {
00455   float x1, y1, x2, y2;
00456   itab0_->unset_handle_motion();
00457   picktab0_->pick_box(&x1, &y1, &x2, &y2);
00458 #if 0
00459   vsol_point_2d_sptr p0 = new vsol_point_2d(x1, y1);
00460   vsol_point_2d_sptr p1 = new vsol_point_2d(x2, y1);
00461   vsol_point_2d_sptr p2 = new vsol_point_2d(x2, y2);
00462   vsol_point_2d_sptr p3 = new vsol_point_2d(x1, y2);
00463   vcl_vector<vsol_point_2d_sptr> pts;
00464   pts.push_back(p0);   pts.push_back(p1);
00465   pts.push_back(p2);   pts.push_back(p3);
00466   box_ = new vsol_polygon_2d(pts);
00467 #endif
00468   //: draw the box in easy2D tableau
00469   float x[4], y[4];
00470   x[0]=x[3] = x1; x[1]=x[2]=x2;
00471   y[0]=y[1] = y1; y[2]=y[3]=y2;
00472   easy0_->add_polygon(4, x, y);
00473   v2D0_->post_redraw();
00474   x0_=int(x1); y0_=int(y1);
00475   if (x2>=x1)
00476     xsize_ = (unsigned) (x2-x1 +1);
00477   else
00478     xsize_ = (unsigned) (x1-x2 +1);
00479   if (y2>y1)
00480    ysize_ = (unsigned) (y2-y1 +1);
00481   else
00482    ysize_ = (unsigned) (y1 -y2 +1);
00483   itab0_->set_handle_motion();
00484 }
00485 
00486 void vvid_vil_file_manager::save_roi()
00487 {
00488   vgui_dialog file_dialog("ROI Directory");
00489   static vcl_string video_file;
00490   static vcl_string ext = "";
00491   file_dialog.file("Video File:", ext, video_file);
00492   if (!file_dialog.ask())
00493     return;
00494   vpro_roi_process* rop = new vpro_roi_process(video_file, x0_, y0_, xsize_, ysize_);
00495   video_process_ = rop;
00496 }