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
00029
00030
00031
00032
00033
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
00043 ColormapViewerWidget::~ColormapViewerWidget()
00044 {
00045 free(m_colormap_img_buf);
00046 }
00047
00048
00049
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
00066
00067
00068 void
00069 ColormapViewerWidget::set_colormap_img(Gtk::Image* img)
00070 {
00071 m_img_colormap = img;
00072 }
00073
00074
00075
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
00104
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
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 }