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 /** @class ColormapViewerWidget <tools/firestation/colormap_viewer_widget.h>
00029  * Select a layer from a colormap and render it to a Gtk::Image.
00030  * @author Daniel Beck
00031  */
00032 
00033 /** Constructor. */
00034 ColormapViewerWidget::ColormapViewerWidget()
00035 {
00036   m_cm = 0;
00037   m_img_colormap = 0;
00038   m_scl_layer_selector = 0;
00039   m_colormap_img_buf = 0;
00040 }
00041 
00042 /** Destructor. */
00043 ColormapViewerWidget::~ColormapViewerWidget()
00044 {
00045   free(m_colormap_img_buf);
00046 }
00047 
00048 /** Set the colormap to display.
00049  * @param cm colormap
00050  */
00051 void
00052 ColormapViewerWidget::set_colormap(Colormap *cm)
00053 {
00054   m_cm = cm;
00055
00056   if (m_scl_layer_selector)
00057     {
00058       double max = m_cm->deepness();
00059       m_scl_layer_selector->set_range(0.0, max);
00060       m_scl_layer_selector->set_increments(1.0, 1.0);
00061       m_scl_layer_selector->set_value(0.0);
00062     }
00063 }
00064 
00065 /** Set the image to render into.
00066  * @param img the Image
00067  */
00068 void
00069 ColormapViewerWidget::set_colormap_img(Gtk::Image* img)
00070 {
00071   m_img_colormap = img;
00072 }
00073 
00074 /** Set the selector widget to choose the layer of the colormap which gets rendered.
00075  * @param scl a Gtk::Scale
00076  */
00077 void
00078 ColormapViewerWidget::set_layer_selector(Gtk::Scale* scl)
00079 {
00080   m_scl_layer_selector = scl;
00081
00082   double max;
00083   if (m_cm)
00084     { max = m_cm->deepness(); }
00085   else
00086     { max = 256.0; }
00087   m_scl_layer_selector->set_range(0.0, max);
00088   m_scl_layer_selector->set_increments(1.0, 1.0);
00089   m_scl_layer_selector->set_value(0.0);
00090
00091   m_scl_layer_selector->signal_change_value().connect( sigc::mem_fun(*this, &ColormapViewerWidget::on_layer_selected) );
00092 }
00093
00094 bool
00095 ColormapViewerWidget::on_layer_selected(Gtk::ScrollType scroll, double value)
00096 {
00097   unsigned int layer = (unsigned int) rint(value);
00098   draw(layer);
00099
00100   return true;
00101 }
00102 
00103 /** Draw the colormap.
00104  * @param layer the plane in the third dimension of the colormap to be drawn
00105  */
00106 void
00107 ColormapViewerWidget::draw(unsigned int layer)
00108 {
00109   if (m_cm == 0 || m_img_colormap == 0)
00110     { return; }
00111
00112   if (layer >= m_cm->deepness() )
00113     {
00114       if (!m_scl_layer_selector) return;
00115       else layer = (unsigned int) rint(m_scl_layer_selector->get_value());
00116     }
00117
00118   unsigned int cm_layer = (layer * m_cm->depth()) / m_cm->deepness();
00119
00120   unsigned char* colormap_buffer = (unsigned char*) malloc( colorspace_buffer_size(YUV422_PLANAR, m_cm->width() * 2, m_cm->height() * 2) );
00121   m_cm->to_image(colormap_buffer, cm_layer);
00122
00123   unsigned int img_width  = (unsigned int) m_img_colormap->get_width();
00124   unsigned int img_height = (unsigned int) m_img_colormap->get_height();
00125
00126   img_width  = (img_width < img_height) ? img_width : img_height;
00127   img_height = (img_width < img_height) ? img_width : img_height;
00128
00129   // scale
00130   LossyScaler scaler;
00131   scaler.set_original_buffer(colormap_buffer);
00132   scaler.set_original_dimensions(m_cm->width() * 2, m_cm->height() * 2);
00133   scaler.set_scaled_dimensions(img_width, img_height);
00134   unsigned char* scaled_colormap_buffer = (unsigned char*) malloc( colorspace_buffer_size(YUV422_PLANAR, img_width, img_height) );
00135   scaler.set_scaled_buffer(scaled_colormap_buffer);
00136   scaler.scale();
00137
00138   free(m_colormap_img_buf);
00139   m_colormap_img_buf = (unsigned char*) malloc( colorspace_buffer_size(RGB, img_width, img_height) );
00140   convert(YUV422_PLANAR, RGB, scaled_colormap_buffer, m_colormap_img_buf, img_width, img_height);
00141
00142   Glib::RefPtr<Gdk::Pixbuf> colormap_image = Gdk::Pixbuf::create_from_data( m_colormap_img_buf,
00143                                                                             Gdk::COLORSPACE_RGB,
00144                                                                             false,
00145                                                                             8,
00146                                                                             img_width, img_height,
00147                                                                             3 * img_width);
00148   m_img_colormap->set(colormap_image);
00149
00150   free(colormap_buffer);
00151   free(scaled_colormap_buffer);
00152 }