23 #include "color_train_widget.h"
24 #include "colormap_viewer_widget.h"
25 #include <fvutils/color/yuv.h>
26 #include <fvutils/color/zauberstab.h>
27 #include <fvutils/color/colorspaces.h>
28 #include <fvutils/color/conversions.h>
29 #include <fvutils/draw/drawer.h>
30 #include <fvutils/scalers/lossy.h>
31 #include <fvutils/colormap/bayes/bayes_generator.h>
32 #include <fvutils/colormap/yuvcm.h>
33 #include <fvutils/colormap/cmfile.h>
35 #include <fvutils/writers/jpeg.h>
37 #include <fvutils/color/color_object_map.h>
39 #include <core/exceptions/software.h>
61 m_wnd_parent = parent;
62 m_btn_reset_selection = 0;
63 m_btn_add_to_colormap = 0;
64 m_btn_reset_colormap = 0;
65 m_btn_load_histos = 0;
66 m_btn_save_histos = 0;
67 m_btn_load_colormap = 0;
68 m_btn_save_colormap = 0;
71 m_spbtn_cm_height = 0;
72 m_img_segmentation = 0;
75 m_fcd_filechooser = 0;
92 m_fg_object = fg_object;
102 unsigned int img_width,
unsigned int img_height)
104 m_img_width = img_width;
105 m_img_height = img_height;
106 m_src_buffer = yuv422_buffer;
107 m_img_cs = YUV422_PLANAR;
108 m_img_size = colorspace_buffer_size( m_img_cs, m_img_width, m_img_height );
112 m_zauberstab->deleteRegion();
113 m_zauberstab->setBuffer(m_src_buffer, m_img_width, m_img_height);
114 m_zauberstab->setThreshold(10);
118 m_img_segmentation->clear();
119 m_img_segmentation->set(
"gtk-missing-image");
131 m_draw_buffer = buffer;
142 if (m_src_buffer == 0 || m_draw_buffer == 0)
145 if ( m_zauberstab->isEmptyRegion() )
147 if (button == MOUSE_BUTTON_LEFT)
149 m_zauberstab->findRegion(x, y);
154 if (button == MOUSE_BUTTON_LEFT)
156 m_zauberstab->addRegion(x, y);
159 if (button == MOUSE_BUTTON_RIGHT)
161 m_zauberstab->deleteRegion(x, y);
165 memcpy(m_draw_buffer, m_src_buffer, m_img_size);
167 ZRegion *region = m_zauberstab->getRegion();
169 d->
set_buffer( m_draw_buffer, m_img_width, m_img_height );
171 for (
unsigned int s = 0; s < region->
slices->size(); s++)
175 region->
slices->at(s)->rightX - region->
slices->at(s)->leftX,
181 m_signal_update_image();
189 { m_zauberstab->deleteRegion(); }
191 if( m_src_buffer && m_draw_buffer )
192 { memcpy(m_draw_buffer, m_src_buffer, m_img_size); }
194 m_signal_update_image();
203 m_btn_reset_selection = btn;
213 m_btn_add_to_colormap = btn;
223 m_btn_reset_colormap = btn;
233 m_btn_load_histos = btn;
243 m_btn_save_histos = btn;
253 m_btn_load_colormap = btn;
263 m_btn_save_colormap = btn;
273 m_cvw->set_colormap_img(img);
282 m_img_segmentation = img;
283 m_seg_img_max_width = m_img_segmentation->get_width();
284 m_seg_img_max_height = m_img_segmentation->get_height();
285 m_img_segmentation->signal_size_allocate().connect( sigc::mem_fun( *
this, &ColorTrainWidget::resize_seg_image) );
289 ColorTrainWidget::resize_seg_image(Gtk::Allocation& allocation)
291 unsigned int new_width = (
unsigned int) allocation.get_width();
292 unsigned int new_height = (
unsigned int) allocation.get_height();
294 if (new_width != m_seg_img_max_width || new_height != m_seg_img_max_height)
296 m_seg_img_max_width = new_width;
297 m_seg_img_max_height = new_height;
298 draw_segmentation_result();
308 m_scl_threshold = scl;
309 m_scl_threshold->signal_change_value().connect( sigc::mem_fun(*
this, &ColorTrainWidget::set_threshold) );
318 m_scl_min_prob = scl;
319 m_scl_min_prob->signal_change_value().connect( sigc::mem_fun(*
this, &ColorTrainWidget::set_min_prob) );
328 m_fcd_filechooser = dlg;
337 m_cvw->set_layer_selector(scl);
348 m_spbtn_cm_depth = depth;
349 m_spbtn_cm_width = width;
350 m_spbtn_cm_height = height;
359 return m_signal_update_image;
368 return m_signal_colormap_updated;
375 if ( !m_fcd_filechooser )
378 m_fcd_filechooser->set_title(
"Load histograms");
379 m_fcd_filechooser->set_action(Gtk::FILE_CHOOSER_ACTION_OPEN);
381 m_fcd_filechooser->set_transient_for(*m_wnd_parent);
383 int result = m_fcd_filechooser->run();
387 case (Gtk::RESPONSE_OK):
389 std::string filename = m_fcd_filechooser->get_filename();
392 m_generator->load_histograms( filename.c_str() );
394 m_signal_colormap_updated();
397 if (m_spbtn_cm_depth) m_spbtn_cm_depth->set_value(log(cur->
depth()) / log(2));
398 if (m_spbtn_cm_width) m_spbtn_cm_width->set_value(log(cur->
width()) / log(2));
399 if (m_spbtn_cm_height) m_spbtn_cm_height->set_value(log(cur->
height()) / log(2));
401 m_cvw->set_colormap(cur);
403 draw_segmentation_result();
407 case (Gtk::RESPONSE_CANCEL):
414 m_fcd_filechooser->hide();
421 if ( !m_fcd_filechooser )
424 m_fcd_filechooser->set_title(
"Save histograms");
425 m_fcd_filechooser->set_action(Gtk::FILE_CHOOSER_ACTION_SAVE);
427 m_fcd_filechooser->set_transient_for(*m_wnd_parent);
429 int result = m_fcd_filechooser->run();
433 case (Gtk::RESPONSE_OK):
435 std::string filename = m_fcd_filechooser->get_filename();
436 m_generator->save_histograms( filename.c_str() );
440 case (Gtk::RESPONSE_CANCEL):
447 m_fcd_filechooser->hide();
457 unsigned int cm_depth;
458 if (m_spbtn_cm_depth)
459 { cm_depth = (
unsigned int) rint( pow(2.0, m_spbtn_cm_depth->get_value()) ); }
463 unsigned int cm_width;
464 if (m_spbtn_cm_width)
465 { cm_width = (
unsigned int) rint( pow(2.0, m_spbtn_cm_width->get_value()) ); }
469 unsigned int cm_height;
470 if (m_spbtn_cm_height)
471 { cm_height = (
unsigned int) rint( pow(2.0, m_spbtn_cm_height->get_value()) ); }
476 || cm_depth != m_generator->get_current()->depth()
477 || cm_width != m_generator->get_current()->width()
478 || cm_height != m_generator->get_current()->height())
482 m_cvw->set_colormap( m_generator->get_current() );
485 if (m_fg_object == H_UNKNOWN)
487 printf(
"CTW::add_to_colormap(): no fg object set\n");
491 m_generator->set_fg_object(m_fg_object);
492 m_generator->reset_undo();
493 m_generator->set_buffer(m_src_buffer, m_img_width, m_img_height);
494 m_generator->set_selection( m_zauberstab->getSelection() );
495 m_generator->consider();
497 m_signal_colormap_updated();
503 draw_segmentation_result();
510 Gtk::MessageDialog dialog(*m_wnd_parent,
"Are you sure you want to reset the colormap?",
511 false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL);
513 int result = dialog.run();
515 if (result != Gtk::RESPONSE_OK)
return;
519 m_generator->reset();
520 m_signal_colormap_updated();
525 draw_segmentation_result();
533 if ( !m_fcd_filechooser )
536 m_fcd_filechooser->set_title(
"Load colormap colormap");
537 m_fcd_filechooser->set_action(Gtk::FILE_CHOOSER_ACTION_OPEN);
539 m_fcd_filechooser->set_transient_for(*m_wnd_parent);
541 int result = m_fcd_filechooser->run();
545 case (Gtk::RESPONSE_OK):
549 std::string filename = m_fcd_filechooser->get_filename();
551 cmf.
read(filename.c_str());
558 unsigned int cm_depth = tcm->
depth();
559 unsigned int cm_width = tcm->
width();
560 unsigned int cm_height = tcm->
height();
566 if (m_spbtn_cm_depth) m_spbtn_cm_depth->set_value(log(cm_depth) / log(2));
567 if (m_spbtn_cm_width) m_spbtn_cm_width->set_value(log(cm_width) / log(2));
568 if (m_spbtn_cm_height) m_spbtn_cm_height->set_value(log(cm_height) / log(2));
570 m_signal_colormap_updated();
571 m_cvw->set_colormap( m_generator->get_current() );
573 draw_segmentation_result();
577 case (Gtk::RESPONSE_CANCEL):
584 m_fcd_filechooser->hide();
591 if ( !m_fcd_filechooser )
594 m_fcd_filechooser->set_title(
"Save colormap colormap");
595 m_fcd_filechooser->set_action(Gtk::FILE_CHOOSER_ACTION_SAVE);
597 m_fcd_filechooser->set_transient_for(*m_wnd_parent);
599 int result = m_fcd_filechooser->run();
603 case(Gtk::RESPONSE_OK):
605 std::string filename = m_fcd_filechooser->get_filename();
609 cmf.write( filename.c_str() );
613 case(Gtk::RESPONSE_CANCEL):
620 m_fcd_filechooser->hide();
632 return m_generator->get_current();
636 ColorTrainWidget::set_threshold(Gtk::ScrollType scroll,
double value)
638 unsigned int threshold = (
unsigned int) rint(value);
639 m_zauberstab->setThreshold(threshold);
645 ColorTrainWidget::set_min_prob(Gtk::ScrollType scroll,
double value)
650 m_generator->set_min_probability(value);
656 ColorTrainWidget::reset_gui()
658 m_scl_min_prob->set_value(0.0);
667 if ( !m_src_buffer || !m_img_segmentation || !m_generator)
670 unsigned char* seg_buffer = (
unsigned char*) malloc(m_img_size);
671 bzero(seg_buffer, m_img_size);
674 d.
set_buffer(seg_buffer, m_img_width, m_img_height);
678 for (
unsigned int w = 0; w < m_img_width; ++w)
680 for (
unsigned int h = 0; h < m_img_height; ++h)
682 unsigned int y = YUV422_PLANAR_Y_AT(m_src_buffer, m_img_width, w, h);
683 unsigned int u = YUV422_PLANAR_U_AT(m_src_buffer, m_img_width, m_img_height, w, h);
684 unsigned int v = YUV422_PLANAR_V_AT(m_src_buffer, m_img_width, m_img_height, w, h);
698 unsigned char* scaled_buffer = (
unsigned char*) malloc( colorspace_buffer_size( m_img_cs,
704 unsigned char* rgb_buffer = (
unsigned char*) malloc( colorspace_buffer_size( RGB,
707 convert(m_img_cs, RGB, scaled_buffer, rgb_buffer, width, height);
709 Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_data( rgb_buffer,
716 Gdk::Pixbuf::SlotDestroyData(&free_rgb_buffer));
718 m_img_segmentation->set(image);
727 void ColorTrainWidget::free_rgb_buffer(
const guint8* rgb_buffer)
729 free(const_cast<guint8 *>(rgb_buffer));
virtual unsigned int width() const
Get width of colormap.
virtual unsigned int needed_scaled_height()
Minimum needed height of scaled image depending on factor and original image height.
void add_colormap(Colormap *colormap)
Add colormap.
virtual void set_scaled_buffer(unsigned char *buffer)
Set scaled image buffer.
virtual void set_original_buffer(unsigned char *buffer)
Set original image buffer.
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const
Determine color class for given YUV value.
virtual unsigned int height() const
Get height of colormap.
virtual unsigned int depth() const
Get depth of colormap.
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
void draw_rectangle_inverted(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
Draw inverted rectangle.
virtual void read(const char *file_name)
Read file.
virtual unsigned int width() const =0
Get width of colormap.
std::vector< ZSlice * > * slices
slices
void set_buffer(unsigned char *buffer, unsigned int width, unsigned int height)
Set the buffer to draw to.
virtual void set_original_dimensions(unsigned int width, unsigned int height)
Set original image dimensions.
a region is a stack of slices, together with the y-position of the slice at the top ...
Zaubertab selection utility.
virtual void set_scaled_dimensions(unsigned int width, unsigned int height)
Set dimenins of scaled image buffer.
Colormap Generator using Bayes method.
virtual unsigned int height() const =0
Get height of colormap.
virtual unsigned int needed_scaled_width()
Minimum needed width of scaled image depending on factor and original image width.
void color_point(unsigned int x, unsigned int y)
Color the given point.
virtual void scale()
Scale image.
static YUV_t get_color(color_t color)
YUV_t getter.
virtual unsigned int depth() const =0
Get depth of colormap.
void set_color(unsigned char y, unsigned char u, unsigned char v)
Set drawing color.