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