core/vgui/vgui_image_tableau.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_image_tableau.cxx
00002 #include "vgui_image_tableau.h"
00003 //:
00004 // \file
00005 // \brief  See vgui_image_tableau.h for a description of this file.
00006 // \author fsm
00007 //
00008 // \verbatim
00009 //  Modifications
00010 //   15-AUG-2000 Marko Bacic,Oxford RRG -- Removed legacy ROI
00011 //   16-FEB-2007 Andrey Khropov -- consistent choice between vil1_image/vil_image_view
00012 // \endverbatim
00013 
00014 
00015 #include <vcl_string.h>
00016 
00017 #include <vil/vil_image_view_base.h>
00018 #include <vil1/vil1_load.h>
00019 #include <vil/vil_load.h>
00020 #include <vil/vil_new.h>
00021 
00022 #include <vgui/vgui_image_renderer.h>
00023 #include <vgui/vgui_vil_image_renderer.h>
00024 #include <vgui/vgui_event.h>
00025 #include <vgui/vgui_menu.h>
00026 #include <vgui/vgui_command.h>
00027 #include <vgui/vgui_dialog.h>
00028 #include <vgui/vgui_gl.h>
00029 #include <vgui/vgui_glu.h>
00030 #include <vgui/vgui_range_map_params.h>
00031 
00032 //-----------------------------------------------------------------------------
00033 
00034 vgui_image_tableau::
00035 vgui_image_tableau()
00036   : vgui_tableau(),
00037     pixels_centered_( true ),
00038     rmp_( 0 ),
00039     renderer_( 0 ),
00040     vil_renderer_( 0 )
00041 {
00042 }
00043 
00044 //-----------------------------------------------------------------------------
00045 
00046 vgui_image_tableau::
00047 vgui_image_tableau( vil1_image const &I,
00048                     vgui_range_map_params_sptr const& mp)
00049   : vgui_tableau(),
00050     pixels_centered_( true ),
00051     renderer_( 0 ),
00052     vil_renderer_( 0 )
00053 {
00054   set_image( I, mp );
00055 }
00056 
00057 //-----------------------------------------------------------------------------
00058 
00059 vgui_image_tableau::
00060 vgui_image_tableau( vil_image_view_base const& I,
00061                     vgui_range_map_params_sptr const& mp )
00062   : vgui_tableau(),
00063     pixels_centered_( true ),
00064     renderer_( 0 ),
00065     vil_renderer_( 0 )
00066 {
00067   set_image_view( I, mp );
00068 }
00069 
00070 //-----------------------------------------------------------------------------
00071 
00072 vgui_image_tableau::
00073 vgui_image_tableau( vil_image_resource_sptr const& I,
00074                     vgui_range_map_params_sptr const& mp )
00075   : vgui_tableau(),
00076     pixels_centered_( true ),
00077     renderer_( 0 ),
00078     vil_renderer_( 0 )
00079 {
00080   set_image_resource( I, mp);
00081 }
00082 
00083 //-----------------------------------------------------------------------------
00084 
00085 vgui_image_tableau::
00086 vgui_image_tableau(char const *f,
00087                    vgui_range_map_params_sptr const& mp)
00088   : vgui_tableau(),
00089     name_( f ),
00090     pixels_centered_( true ),
00091     renderer_( 0 ),
00092     vil_renderer_( 0 )
00093 {
00094   set_image( f, mp );
00095 }
00096 
00097 //-----------------------------------------------------------------------------
00098 
00099 vgui_image_tableau::
00100 ~vgui_image_tableau()
00101 {
00102   delete renderer_;
00103   delete vil_renderer_;
00104   renderer_ = 0;
00105   vil_renderer_ = 0;
00106 }
00107 
00108 //-----------------------------------------------------------------------------
00109 
00110 vcl_string
00111 vgui_image_tableau::
00112 type_name() const
00113 {
00114   return "vgui_image_tableau";
00115 }
00116 
00117 
00118 //-----------------------------------------------------------------------------
00119 
00120 vcl_string
00121 vgui_image_tableau::
00122 file_name() const
00123 {
00124   return name_;
00125 }
00126 
00127 //-----------------------------------------------------------------------------
00128 
00129 vcl_string
00130 vgui_image_tableau::
00131 pretty_name() const
00132 {
00133   return type_name() + "[" + name_ + "]";
00134 }
00135 
00136 //-----------------------------------------------------------------------------
00137 
00138 vil1_image
00139 vgui_image_tableau::
00140 get_image() const
00141 {
00142   if (renderer_)
00143     return renderer_->get_image();
00144   else
00145     return 0;
00146 }
00147 
00148 //-----------------------------------------------------------------------------
00149 
00150 vil_image_view_base_sptr
00151 vgui_image_tableau::
00152 get_image_view() const
00153 {
00154   if (vil_renderer_)
00155     return vil_renderer_->get_image_resource()->get_view();
00156   else
00157     return 0;
00158 }
00159 
00160 //-----------------------------------------------------------------------------
00161 
00162 vil_image_resource_sptr
00163 vgui_image_tableau::
00164 get_image_resource() const
00165 {
00166   if (vil_renderer_)
00167     return vil_renderer_->get_image_resource();
00168   else
00169     return 0;
00170 }
00171 
00172 //-----------------------------------------------------------------------------
00173 
00174 void
00175 vgui_image_tableau::
00176 set_image_view( char const* f, vgui_range_map_params_sptr const& mp)
00177 {
00178   name_ = f;
00179   vil_image_view_base_sptr img = vil_load( f );
00180   if ( img )
00181     set_image_view( *img, mp );
00182 }
00183 
00184 //-----------------------------------------------------------------------------
00185 
00186 void
00187 vgui_image_tableau::
00188 set_image_view( vil_image_view_base const& I,
00189                 vgui_range_map_params_sptr const& mp)
00190 {
00191   set_image_view(I);
00192   rmp_ = mp;
00193 }
00194 
00195 //-----------------------------------------------------------------------------
00196 
00197 void
00198 vgui_image_tableau::
00199 set_image_resource( vil_image_resource_sptr const& I,
00200                     vgui_range_map_params_sptr const& mp )
00201 {
00202   set_image_resource(I);
00203   rmp_ = mp;
00204 }
00205 
00206 //-----------------------------------------------------------------------------
00207 
00208 void
00209 vgui_image_tableau::
00210 set_image( vil1_image const& I,
00211            vgui_range_map_params_sptr const& mp )
00212 {
00213   set_image(I);
00214   rmp_ = mp;
00215 }
00216 
00217 //-----------------------------------------------------------------------------
00218 
00219 void
00220 vgui_image_tableau::
00221 set_image_view( vil_image_view_base const& I)
00222 {
00223   set_image_resource( vil_new_image_resource_of_view( I ) );
00224 }
00225 
00226 //-----------------------------------------------------------------------------
00227 
00228 void
00229 vgui_image_tableau::
00230 set_image_resource( vil_image_resource_sptr const& I)
00231 {
00232   if ( !vil_renderer_ )
00233     vil_renderer_ = new vgui_vil_image_renderer;
00234 
00235   // use the name of the image as the name of the tableau :
00236   vil_renderer_->set_image_resource( I );
00237 
00238   if ( renderer_ )
00239   {
00240     delete renderer_;
00241     renderer_ = 0;
00242   }
00243 }
00244 
00245 //-----------------------------------------------------------------------------
00246 
00247 void
00248 vgui_image_tableau::
00249 set_image( vil1_image const& I)
00250 {
00251   if ( !renderer_ )
00252     renderer_ = new vgui_image_renderer;
00253 
00254   // use the name of the image as the name of the tableau :
00255   renderer_->set_image( I );
00256 
00257   if ( vil_renderer_ )
00258   {
00259     delete vil_renderer_;
00260     vil_renderer_ = 0;
00261   }
00262 }
00263 
00264 //-----------------------------------------------------------------------------
00265 
00266 void
00267 vgui_image_tableau::
00268 set_image(char const *f,
00269           vgui_range_map_params_sptr const& mp)
00270 {
00271   name_ = f;
00272   vil1_image img = vil1_load( f );
00273   if ( img )
00274     set_image( img, mp );
00275 }
00276 
00277 //-----------------------------------------------------------------------------
00278 
00279 void
00280 vgui_image_tableau::
00281 reread_image()
00282 {
00283   if ( renderer_ )           renderer_->reread_image();
00284   else if ( vil_renderer_ )  vil_renderer_->reread_image();
00285 }
00286 
00287 //-----------------------------------------------------------------------------
00288 
00289 unsigned
00290 vgui_image_tableau::
00291 width() const
00292 {
00293   if ( renderer_ )
00294     return renderer_->get_image().width();
00295   else if ( vil_renderer_ )
00296     return vil_renderer_->get_image_resource()->ni();
00297   else
00298     return 0;
00299 }
00300 
00301 //-----------------------------------------------------------------------------
00302 
00303 unsigned
00304 vgui_image_tableau::
00305 height() const
00306 {
00307   if ( renderer_ )
00308     return renderer_->get_image().height();
00309   else if ( vil_renderer_ )
00310     return vil_renderer_->get_image_resource()->nj();
00311   else
00312     return 0;
00313 }
00314 
00315 //-----------------------------------------------------------------------------
00316 
00317 bool
00318 vgui_image_tableau::
00319 get_bounding_box(float low[3], float high[3]) const
00320 {
00321   low[0] = 0; high[0] = width();
00322   low[1] = 0; high[1] = height();
00323 
00324   low[2] = 0; high[2] = 0;
00325   return true;
00326 }
00327 
00328 //-----------------------------------------------------------------------------
00329 
00330 bool
00331 vgui_image_tableau::
00332 handle(vgui_event const &e)
00333 {
00334   if (e.type == vgui_DRAW)
00335   {
00336     // If blending is turned on, there is a severe performance penalty
00337     // when rendering an image. So, we turn off blending before calling
00338     // the renderer. In cases where two images are to be blended, a
00339     // special tableau should be written, eg. vgui_blender_tableau.
00340     // fsm
00341     GLboolean blend_on;
00342     glGetBooleanv(GL_BLEND, &blend_on);
00343     if (blend_on)
00344       glDisable(GL_BLEND);
00345 
00346     if (pixels_centered_)
00347       glTranslated(-0.5, -0.5, 0);
00348     //If rmp_ is not null then the rendering will be mapped according to the
00349     //specified mapping parameters
00350     if ( renderer_ )          renderer_->render(rmp_);
00351     else if ( vil_renderer_ ) vil_renderer_->render(rmp_);
00352 
00353     if (pixels_centered_)
00354       glTranslated(+0.5, +0.5, 0);
00355 
00356     if (blend_on)
00357       glEnable(GL_BLEND);
00358 
00359     return true;
00360   }
00361   else
00362     return false;
00363 }
00364 
00365 void vgui_image_tableau::set_mapping(vgui_range_map_params_sptr const& rmp)
00366 {
00367   rmp_ = rmp;
00368   this->post_redraw();
00369 }
00370 
00371 
00372 //: A vgui command to set the rangemap parameters
00373 class vgui_set_rangemap_command : public vgui_command
00374 {
00375  public:
00376   vgui_set_rangemap_command(const vgui_image_tableau_sptr& tab,
00377                             unsigned int nc=1)
00378    : tab_(tab), nc_(nc) {}
00379 
00380   void execute()
00381   {
00382     vgui_range_map_params_sptr old_rmp = tab_->map_params();
00383     vgui_range_map_params_sptr rmp = NULL;
00384     if (!old_rmp || old_rmp->n_components_ != nc_) {
00385       rmp = new vgui_range_map_params(0.0, 1.0);
00386       rmp->n_components_ = nc_;
00387     }
00388     else
00389       rmp = new vgui_range_map_params(*old_rmp);
00390 
00391     // use this array because vgui_dialog does not support long double fields
00392     double ranges[8];
00393     int choice = rmp->band_map_;
00394     vgui_dialog rmp_dialog("Set Range Mapping Parameters");
00395     if (nc_ == 1) {
00396       ranges[0] = double(rmp->min_L_);
00397       ranges[1] = double(rmp->max_L_);
00398       rmp_dialog.field("Luminance Min ",ranges[0]);
00399       rmp_dialog.field("Luminance Max ",ranges[1]);
00400       rmp_dialog.field("Luminance Gamma ",rmp->gamma_L_);
00401     }
00402     if (nc_ == 3 || nc_ == 4) {
00403       ranges[0] = double(rmp->min_R_);
00404       ranges[1] = double(rmp->max_R_);
00405       rmp_dialog.field("R Min ",ranges[0]);
00406       rmp_dialog.field("R Max ",ranges[1]);
00407       rmp_dialog.field("R Gamma ",rmp->gamma_R_);
00408       ranges[2] = double(rmp->min_G_);
00409       ranges[3] = double(rmp->max_G_);
00410       rmp_dialog.field("G Min ",ranges[2]);
00411       rmp_dialog.field("G Max ",ranges[3]);
00412       rmp_dialog.field("G Gamma ",rmp->gamma_G_);
00413       ranges[4] = double(rmp->min_B_);
00414       ranges[5] = double(rmp->max_B_);
00415       rmp_dialog.field("B Min ",ranges[4]);
00416       rmp_dialog.field("B Max ",ranges[5]);
00417       rmp_dialog.field("B Gamma ",rmp->gamma_B_);
00418     }
00419     if (nc_==4) {
00420       ranges[6] = double(rmp->min_X_);
00421       ranges[7] = double(rmp->max_X_);
00422       rmp_dialog.field("X Min ",ranges[6]);
00423       rmp_dialog.field("X Max ",ranges[7]);
00424       rmp_dialog.field("X Gamma ",rmp->gamma_X_);
00425       vcl_vector<vcl_string> choices;
00426       for (unsigned c = 0; c<vgui_range_map_params::END_m; ++c)
00427         choices.push_back(vgui_range_map_params::bmap[c]);
00428       rmp_dialog.choice("Band Map", choices, choice);
00429     }
00430 
00431     rmp_dialog.checkbox("Invert ",rmp->invert_);
00432 
00433     rmp_dialog.checkbox("Use glPixelMap ",rmp->use_glPixelMap_);
00434     rmp_dialog.checkbox("Cache Map ",rmp->cache_mapped_pix_);
00435 
00436     if (!rmp_dialog.ask())
00437       return;
00438 
00439     rmp->band_map_ = choice;
00440     rmp->min_L_ = ranges[0];
00441     rmp->max_L_ = ranges[1];
00442     rmp->min_R_ = ranges[0];
00443     rmp->max_R_ = ranges[1];
00444     rmp->min_G_ = ranges[2];
00445     rmp->max_G_ = ranges[3];
00446     rmp->min_B_ = ranges[4];
00447     rmp->max_B_ = ranges[5];
00448     rmp->min_X_ = ranges[6];
00449     rmp->max_X_ = ranges[7];
00450 
00451     //Setting the map with a new instance forces redraw
00452     tab_->set_mapping(rmp);
00453   }
00454 
00455   vgui_image_tableau_sptr tab_;
00456   unsigned int nc_;
00457 };
00458 
00459 //----------------------------------------------------------------------------
00460 //: Builds a popup menu for the user to set range mapping parameters
00461 void vgui_image_tableau::get_popup(const vgui_popup_params& /*params*/,
00462                                    vgui_menu &menu)
00463 {
00464   vgui_menu submenu;
00465 
00466   unsigned int nc = 0;
00467   if ( renderer_ )
00468     nc = renderer_->get_image().components();
00469   else if ( vil_renderer_ )
00470     nc = vil_renderer_->get_image_resource()->nplanes();
00471 
00472   submenu.add("Range Mapping",new vgui_set_rangemap_command(this,nc));
00473 
00474   menu.add(type_name(), submenu);
00475 }