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 
25 #include <fvutils/color/conversions.h>
26 #include <fvutils/colormap/colormap.h>
27 #include <fvutils/scalers/lossy.h>
28 
29 using namespace firevision;
30 
31 /** @class ColormapViewerWidget "colormap_viewer_widget.h"
32  * Select a layer from a colormap and render it to a Gtk::Image.
33  * @author Daniel Beck
34  */
35 
36 /** Constructor. */
38 {
39  m_cm = 0;
40  m_img_colormap = 0;
41  m_scl_layer_selector = 0;
42  m_colormap_img_buf = 0;
43 }
44 
45 /** Destructor. */
47 {
48  free(m_colormap_img_buf);
49 }
50 
51 /** Set the colormap to display.
52  * @param cm colormap
53  */
54 void
56 {
57  m_cm = cm;
58 
59  if (m_scl_layer_selector) {
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  }
90  m_scl_layer_selector->set_range(0.0, max);
91  m_scl_layer_selector->set_increments(1.0, 1.0);
92  m_scl_layer_selector->set_value(0.0);
93 
94  m_scl_layer_selector->signal_change_value().connect(
95  sigc::mem_fun(*this, &ColormapViewerWidget::on_layer_selected));
96 }
97 
98 bool
99 ColormapViewerWidget::on_layer_selected(Gtk::ScrollType scroll, double value)
100 {
101  unsigned int layer = (unsigned int)rint(value);
102  draw(layer);
103 
104  return true;
105 }
106 
107 /** Draw the colormap.
108  * @param layer the plane in the third dimension of the colormap to be drawn
109  */
110 void
111 ColormapViewerWidget::draw(unsigned int layer)
112 {
113  if (m_cm == 0 || m_img_colormap == 0) {
114  return;
115  }
116 
117  if (layer >= m_cm->deepness()) {
118  if (!m_scl_layer_selector)
119  return;
120  else
121  layer = (unsigned int)rint(m_scl_layer_selector->get_value());
122  }
123 
124  unsigned int cm_layer = (layer * m_cm->depth()) / m_cm->deepness();
125 
126  unsigned char *colormap_buffer = (unsigned char *)malloc(
127  colorspace_buffer_size(YUV422_PLANAR, m_cm->image_width(), m_cm->image_height()));
128  m_cm->to_image(colormap_buffer, cm_layer);
129 
130  unsigned int img_width = (unsigned int)m_img_colormap->get_width();
131  unsigned int img_height = (unsigned int)m_img_colormap->get_height();
132 
133  img_width = (img_width < img_height) ? img_width : img_height;
134  img_height = (img_width < img_height) ? img_width : img_height;
135 
136  // scale
137  LossyScaler scaler;
138  scaler.set_original_buffer(colormap_buffer);
139  scaler.set_original_dimensions(m_cm->image_width(), m_cm->image_height());
140  scaler.set_scaled_dimensions(img_width, img_height);
141  //unsigned int scaled_width = scaler.needed_scaled_width();
142  //unsigned int scaled_height = scaler.needed_scaled_height();
143  unsigned char *scaled_colormap_buffer =
144  (unsigned char *)malloc(colorspace_buffer_size(YUV422_PLANAR, img_width, img_height));
145  scaler.set_scaled_buffer(scaled_colormap_buffer);
146  scaler.scale();
147 
148  free(m_colormap_img_buf);
149  m_colormap_img_buf = (unsigned char *)malloc(colorspace_buffer_size(RGB, img_width, img_height));
150  convert(YUV422_PLANAR, RGB, scaled_colormap_buffer, m_colormap_img_buf, img_width, img_height);
151 
152  Glib::RefPtr<Gdk::Pixbuf> colormap_image = Gdk::Pixbuf::create_from_data(
153  m_colormap_img_buf, Gdk::COLORSPACE_RGB, false, 8, img_width, img_height, 3 * img_width);
154  m_img_colormap->set(colormap_image);
155 
156  free(colormap_buffer);
157  free(scaled_colormap_buffer);
158 }
void draw(unsigned int layer=0)
Draw the colormap.
void set_colormap_img(Gtk::Image *img)
Set the image to render into.
void set_colormap(firevision::Colormap *cm)
Set the colormap to display.
void set_layer_selector(Gtk::Scale *scl)
Set the selector widget to choose the layer of the colormap which gets rendered.
Colormap interface.
Definition: colormap.h:37
virtual unsigned int deepness() const =0
Get deepness of colormap.
Lossy image scaler.
Definition: lossy.h:33
virtual void scale()
Scale image.
Definition: lossy.cpp:139
virtual void set_scaled_dimensions(unsigned int width, unsigned int height)
Set dimenins of scaled image buffer.
Definition: lossy.cpp:83
virtual void set_original_dimensions(unsigned int width, unsigned int height)
Set original image dimensions.
Definition: lossy.cpp:76
virtual void set_original_buffer(unsigned char *buffer)
Set original image buffer.
Definition: lossy.cpp:109
virtual void set_scaled_buffer(unsigned char *buffer)
Set scaled image buffer.
Definition: lossy.cpp:115