Fawkes API  Fawkes Development Version
colormap_viewer_widget.cpp
1 
2 /***************************************************************************
3  * colormap_viewer_widget.cpp - Viewer widget for colormaps
4  *
5  * Created: Thu Mar 20 19:08:04 2008
6  * Copyright 2008 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "colormap_viewer_widget.h"
24 #include <fvutils/colormap/colormap.h>
25 #include <fvutils/scalers/lossy.h>
26 #include <fvutils/color/conversions.h>
27 
28 using namespace firevision;
29 
30 /** @class ColormapViewerWidget "colormap_viewer_widget.h"
31  * Select a layer from a colormap and render it to a Gtk::Image.
32  * @author Daniel Beck
33  */
34 
35 /** Constructor. */
37 {
38  m_cm = 0;
39  m_img_colormap = 0;
40  m_scl_layer_selector = 0;
41  m_colormap_img_buf = 0;
42 }
43 
44 /** Destructor. */
46 {
47  free(m_colormap_img_buf);
48 }
49 
50 /** Set the colormap to display.
51  * @param cm colormap
52  */
53 void
55 {
56  m_cm = cm;
57 
58  if (m_scl_layer_selector)
59  {
60  double max = m_cm->deepness();
61  m_scl_layer_selector->set_range(0.0, max);
62  m_scl_layer_selector->set_increments(1.0, 1.0);
63  m_scl_layer_selector->set_value(0.0);
64  }
65 }
66 
67 /** Set the image to render into.
68  * @param img the Image
69  */
70 void
72 {
73  m_img_colormap = img;
74 }
75 
76 /** Set the selector widget to choose the layer of the colormap which gets rendered.
77  * @param scl a Gtk::Scale
78  */
79 void
81 {
82  m_scl_layer_selector = scl;
83 
84  double max;
85  if (m_cm)
86  { max = m_cm->deepness(); }
87  else
88  { max = 256.0; }
89  m_scl_layer_selector->set_range(0.0, max);
90  m_scl_layer_selector->set_increments(1.0, 1.0);
91  m_scl_layer_selector->set_value(0.0);
92 
93  m_scl_layer_selector->signal_change_value().connect( sigc::mem_fun(*this, &ColormapViewerWidget::on_layer_selected) );
94 }
95 
96 bool
97 ColormapViewerWidget::on_layer_selected(Gtk::ScrollType scroll, double value)
98 {
99  unsigned int layer = (unsigned int) rint(value);
100  draw(layer);
101 
102  return true;
103 }
104 
105 /** Draw the colormap.
106  * @param layer the plane in the third dimension of the colormap to be drawn
107  */
108 void
109 ColormapViewerWidget::draw(unsigned int layer)
110 {
111  if (m_cm == 0 || m_img_colormap == 0)
112  { return; }
113 
114  if (layer >= m_cm->deepness() )
115  {
116  if (!m_scl_layer_selector) return;
117  else layer = (unsigned int) rint(m_scl_layer_selector->get_value());
118  }
119 
120  unsigned int cm_layer = (layer * m_cm->depth()) / m_cm->deepness();
121 
122  unsigned char* colormap_buffer = (unsigned char*) malloc( colorspace_buffer_size(YUV422_PLANAR, m_cm->image_width(), m_cm->image_height()) );
123  m_cm->to_image(colormap_buffer, cm_layer);
124 
125  unsigned int img_width = (unsigned int) m_img_colormap->get_width();
126  unsigned int img_height = (unsigned int) m_img_colormap->get_height();
127 
128  img_width = (img_width < img_height) ? img_width : img_height;
129  img_height = (img_width < img_height) ? img_width : img_height;
130 
131  // scale
132  LossyScaler scaler;
133  scaler.set_original_buffer(colormap_buffer);
134  scaler.set_original_dimensions(m_cm->image_width(), m_cm->image_height());
135  scaler.set_scaled_dimensions(img_width, img_height);
136  //unsigned int scaled_width = scaler.needed_scaled_width();
137  //unsigned int scaled_height = scaler.needed_scaled_height();
138  unsigned char* scaled_colormap_buffer = (unsigned char*) malloc( colorspace_buffer_size(YUV422_PLANAR, img_width, img_height) );
139  scaler.set_scaled_buffer(scaled_colormap_buffer);
140  scaler.scale();
141 
142  free(m_colormap_img_buf);
143  m_colormap_img_buf = (unsigned char*) malloc( colorspace_buffer_size(RGB, img_width, img_height) );
144  convert(YUV422_PLANAR, RGB, scaled_colormap_buffer, m_colormap_img_buf, img_width, img_height);
145 
146  Glib::RefPtr<Gdk::Pixbuf> colormap_image =
147  Gdk::Pixbuf::create_from_data( m_colormap_img_buf,
148  Gdk::COLORSPACE_RGB,
149  false,
150  8,
151  img_width, img_height,
152  3 * img_width);
153  m_img_colormap->set(colormap_image);
154 
155  free(colormap_buffer);
156  free(scaled_colormap_buffer);
157 }
virtual void set_scaled_buffer(unsigned char *buffer)
Set scaled image buffer.
Definition: lossy.cpp:126
virtual void set_original_buffer(unsigned char *buffer)
Set original image buffer.
Definition: lossy.cpp:119
Colormap interface.
Definition: colormap.h:38
void set_colormap_img(Gtk::Image *img)
Set the image to render into.
Lossy image scaler.
Definition: lossy.h:35
void set_layer_selector(Gtk::Scale *scl)
Set the selector widget to choose the layer of the colormap which gets rendered.
void draw(unsigned int layer=0)
Draw the colormap.
virtual void set_original_dimensions(unsigned int width, unsigned int height)
Set original image dimensions.
Definition: lossy.cpp:83
virtual void set_scaled_dimensions(unsigned int width, unsigned int height)
Set dimenins of scaled image buffer.
Definition: lossy.cpp:92
void set_colormap(firevision::Colormap *cm)
Set the colormap to display.
virtual unsigned int deepness() const =0
Get deepness of colormap.
virtual void scale()
Scale image.
Definition: lossy.cpp:153