colormap_viewer_widget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <tools/firestation/colormap_viewer_widget.h>
00024 #include <fvutils/colormap/colormap.h>
00025 #include <fvutils/scalers/lossy.h>
00026 #include <fvutils/color/conversions.h>
00027
00028 using namespace firevision;
00029
00030
00031
00032
00033
00034
00035
00036 ColormapViewerWidget::ColormapViewerWidget()
00037 {
00038 m_cm = 0;
00039 m_img_colormap = 0;
00040 m_scl_layer_selector = 0;
00041 m_colormap_img_buf = 0;
00042 }
00043
00044
00045 ColormapViewerWidget::~ColormapViewerWidget()
00046 {
00047 free(m_colormap_img_buf);
00048 }
00049
00050
00051
00052
00053 void
00054 ColormapViewerWidget::set_colormap(Colormap *cm)
00055 {
00056 m_cm = cm;
00057
00058 if (m_scl_layer_selector)
00059 {
00060 double max = m_cm->deepness();
00061 m_scl_layer_selector->set_range(0.0, max);
00062 m_scl_layer_selector->set_increments(1.0, 1.0);
00063 m_scl_layer_selector->set_value(0.0);
00064 }
00065 }
00066
00067
00068
00069
00070 void
00071 ColormapViewerWidget::set_colormap_img(Gtk::Image* img)
00072 {
00073 m_img_colormap = img;
00074 }
00075
00076
00077
00078
00079 void
00080 ColormapViewerWidget::set_layer_selector(Gtk::Scale* scl)
00081 {
00082 m_scl_layer_selector = scl;
00083
00084 double max;
00085 if (m_cm)
00086 { max = m_cm->deepness(); }
00087 else
00088 { max = 256.0; }
00089 m_scl_layer_selector->set_range(0.0, max);
00090 m_scl_layer_selector->set_increments(1.0, 1.0);
00091 m_scl_layer_selector->set_value(0.0);
00092
00093 m_scl_layer_selector->signal_change_value().connect( sigc::mem_fun(*this, &ColormapViewerWidget::on_layer_selected) );
00094 }
00095
00096 bool
00097 ColormapViewerWidget::on_layer_selected(Gtk::ScrollType scroll, double value)
00098 {
00099 unsigned int layer = (unsigned int) rint(value);
00100 draw(layer);
00101
00102 return true;
00103 }
00104
00105
00106
00107
00108 void
00109 ColormapViewerWidget::draw(unsigned int layer)
00110 {
00111 if (m_cm == 0 || m_img_colormap == 0)
00112 { return; }
00113
00114 if (layer >= m_cm->deepness() )
00115 {
00116 if (!m_scl_layer_selector) return;
00117 else layer = (unsigned int) rint(m_scl_layer_selector->get_value());
00118 }
00119
00120 unsigned int cm_layer = (layer * m_cm->depth()) / m_cm->deepness();
00121
00122 unsigned char* colormap_buffer = (unsigned char*) malloc( colorspace_buffer_size(YUV422_PLANAR, m_cm->image_width(), m_cm->image_height()) );
00123 m_cm->to_image(colormap_buffer, cm_layer);
00124
00125 unsigned int img_width = (unsigned int) m_img_colormap->get_width();
00126 unsigned int img_height = (unsigned int) m_img_colormap->get_height();
00127
00128 img_width = (img_width < img_height) ? img_width : img_height;
00129 img_height = (img_width < img_height) ? img_width : img_height;
00130
00131
00132 LossyScaler scaler;
00133 scaler.set_original_buffer(colormap_buffer);
00134 scaler.set_original_dimensions(m_cm->image_width(), m_cm->image_height());
00135 scaler.set_scaled_dimensions(img_width, img_height);
00136
00137
00138 unsigned char* scaled_colormap_buffer = (unsigned char*) malloc( colorspace_buffer_size(YUV422_PLANAR, img_width, img_height) );
00139 scaler.set_scaled_buffer(scaled_colormap_buffer);
00140 scaler.scale();
00141
00142 free(m_colormap_img_buf);
00143 m_colormap_img_buf = (unsigned char*) malloc( colorspace_buffer_size(RGB, img_width, img_height) );
00144 convert(YUV422_PLANAR, RGB, scaled_colormap_buffer, m_colormap_img_buf, img_width, img_height);
00145
00146 Glib::RefPtr<Gdk::Pixbuf> colormap_image =
00147 Gdk::Pixbuf::create_from_data( m_colormap_img_buf,
00148 Gdk::COLORSPACE_RGB,
00149 false,
00150 8,
00151 img_width, img_height,
00152 3 * img_width);
00153 m_img_colormap->set(colormap_image);
00154
00155 free(colormap_buffer);
00156 free(scaled_colormap_buffer);
00157 }