contrib/brl/bbas/bgui/bgui_histogram_tableau.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/bgui/bgui_histogram_tableau.cxx
00002 #include "bgui_histogram_tableau.h"
00003 //:
00004 // \file
00005 // \author  Matt Leotta
00006 
00007 #include <vil1/vil1_memory_image_of.h>
00008 #include <vil1/vil1_vil.h>
00009 #include <vil/vil_image_resource.h>
00010 #include <vil/algo/vil_histogram.h>
00011 #include <vgui/vgui.h>
00012 #include <vgui/vgui_gl.h>
00013 #include <vgui/vgui_easy2D_tableau.h>
00014 
00015 //========================================================================
00016 //: Constructor
00017 bgui_histogram_tableau::bgui_histogram_tableau()
00018   : left_offset_(10), top_offset_(10),
00019     graph_width_(256), graph_height_(200), plot_(0)
00020 {
00021   easy_ = vgui_easy2D_tableau_new();
00022 
00023   // Draw a square around the histogram
00024   vcl_vector<float> x_corners, y_corners;
00025   x_corners.push_back(left_offset_);
00026   x_corners.push_back(left_offset_+graph_width_);
00027   x_corners.push_back(left_offset_+graph_width_);
00028   x_corners.push_back(left_offset_);
00029   y_corners.push_back(top_offset_);
00030   y_corners.push_back(top_offset_);
00031   y_corners.push_back(top_offset_+graph_height_);
00032   y_corners.push_back(top_offset_+graph_height_);
00033   easy_->add_polygon(4, &x_corners[0], &y_corners[0]);
00034 }
00035 
00036 //========================================================================
00037 //: Destructor.
00038 bgui_histogram_tableau::~bgui_histogram_tableau()
00039 {
00040 }
00041 
00042 
00043 void bgui_histogram_tableau::update(vil1_memory_image_of< vil1_rgb<unsigned char> >& img)
00044 {
00045   vil_image_resource_sptr image = vil1_to_vil_image_resource(img);
00046   vil_image_view_base_sptr temp = image->get_view( 0 , image->ni() , 0 , image->nj() );
00047   vil_image_view<vxl_byte> img_view( temp );
00048   data_.clear();
00049   vil_histogram_byte( img_view , data_ );
00050 
00051   double max = data_[0];
00052   for (unsigned int i=1; i<data_.size(); ++i)
00053     if (max < data_[i]) max = data_[i];
00054 
00055   // scale and shift the data points
00056   vcl_vector<float> xscaled, yscaled;
00057   for (unsigned int i=0; i<data_.size(); ++i) {
00058     xscaled.push_back(float(left_offset_ + i));
00059     yscaled.push_back(float(top_offset_ + graph_height_ - data_[i]/max*graph_height_));
00060   }
00061 
00062   if (plot_)
00063   {
00064     // Update the plot points
00065     // This is a bit more efficient that deleting and reconstructing
00066     //   but not as "clean"
00067     for (unsigned int i=0; i<xscaled.size(); ++i) {
00068       plot_->x[i] = xscaled[i];
00069       plot_->y[i] = yscaled[i];
00070     }
00071   }
00072   else
00073     plot_ = easy_->add_linestrip(xscaled.size(), &xscaled[0], &yscaled[0]);
00074 
00075   post_redraw();
00076 }
00077 
00078 void bgui_histogram_tableau::update(vil_image_view< vxl_byte >& img_view)
00079 {
00080   data_.clear();
00081   vil_histogram_byte( img_view , data_ );
00082 
00083   double max = data_[0];
00084   for (unsigned int i=1; i<data_.size(); ++i)
00085     if (max < data_[i]) max = data_[i];
00086 
00087   // scale and shift the data points
00088   vcl_vector<float> xscaled, yscaled;
00089   for (unsigned int i=0; i<data_.size(); ++i) {
00090     xscaled.push_back(float(left_offset_ + i));
00091     yscaled.push_back(float(top_offset_ + graph_height_ - data_[i]/max*graph_height_));
00092   }
00093 
00094   if (plot_)
00095   {
00096     // Update the plot points
00097     // This is a bit more efficient that deleting and reconstructing
00098     //   but not as "clean"
00099     for (unsigned int i=0; i<xscaled.size(); ++i) {
00100       plot_->x[i] = xscaled[i];
00101       plot_->y[i] = yscaled[i];
00102     }
00103   }
00104   else
00105     plot_ = easy_->add_linestrip(xscaled.size(), &xscaled[0], &yscaled[0]);
00106 
00107   post_redraw();
00108 }
00109 
00110 //========================================================================
00111 //: Handles all events for this tableau.
00112 bool bgui_histogram_tableau::handle(const vgui_event& event)
00113 {
00114   // Pass events on down to the child tableaux:
00115   easy_->handle(event);
00116   return true;
00117 }
00118 
00119 void bgui_histogram_tableau::clear()
00120 {
00121   easy_->remove(plot_);
00122   delete plot_;
00123   plot_ = 0;
00124 
00125   post_redraw();
00126 }