colormap_viewer_widget.cpp

00001 
00002 /***************************************************************************
00003  *  colormap_viewer_widget.cpp - Viewer widget for colormaps
00004  *
00005  *  Created: Thu Mar 20 19:08:04 2008
00006  *  Copyright  2008  Daniel Beck
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
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 /** @class ColormapViewerWidget "colormap_viewer_widget.h"
00031  * Select a layer from a colormap and render it to a Gtk::Image.
00032  * @author Daniel Beck
00033  */
00034 
00035 /** Constructor. */
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 /** Destructor. */
00045 ColormapViewerWidget::~ColormapViewerWidget()
00046 {
00047   free(m_colormap_img_buf);
00048 }
00049 
00050 /** Set the colormap to display.
00051  * @param cm colormap
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 /** Set the image to render into.
00068  * @param img the Image
00069  */
00070 void
00071 ColormapViewerWidget::set_colormap_img(Gtk::Image* img)
00072 {
00073   m_img_colormap = img;
00074 }
00075 
00076 /** Set the selector widget to choose the layer of the colormap which gets rendered.
00077  * @param scl a Gtk::Scale
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 /** Draw the colormap.
00106  * @param layer the plane in the third dimension of the colormap to be drawn
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   // scale
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   //unsigned int scaled_width  = scaler.needed_scaled_width();
00137   //unsigned int scaled_height = scaler.needed_scaled_height();
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 }

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1