22 #include "image_widget.h"
24 #include <core/exceptions/software.h>
25 #include <core/threading/mutex.h>
26 #include <fvcams/camera.h>
27 #include <fvutils/color/conversions.h>
28 #include <fvutils/color/yuv.h>
29 #include <fvutils/scalers/lossy.h>
33 namespace firevision {
52 refresh_thread_ = NULL;
68 unsigned int refresh_delay,
78 cam_has_buffer_ =
false;
85 cam_has_timestamp_ =
true;
87 cam_has_timestamp_ =
false;
90 refresh_thread_ =
new RefThread(
this, refresh_delay);
91 refresh_thread_->start();
92 refresh_thread_->refresh_cam();
107 : Gtk::Image(cobject)
110 cam_enabled_ =
false;
112 refresh_thread_ = NULL;
129 : Gtk::Image(cobject)
132 cam_enabled_ =
false;
134 refresh_thread_ = NULL;
146 refresh_thread_->stop();
164 cam_has_buffer_ =
false;
171 cam_has_timestamp_ =
true;
173 cam_has_timestamp_ =
false;
176 if (refresh_thread_) {
177 refresh_thread_->set_delay(refresh_delay);
179 refresh_thread_ =
new RefThread(
this, refresh_delay);
180 refresh_thread_->start();
183 refresh_thread_->refresh_cam();
195 if (!enable && cam_enabled_) {
196 refresh_thread_->stop();
197 }
else if (refresh_thread_ && enable && !cam_enabled_) {
198 refresh_thread_->start();
201 cam_enabled_ = enable;
217 if (!width || !height) {
223 "ImageWidget::set_size(): width and/or height may not be 0 if no Camera is set");
227 if (!pixbuf_ || width_ != width || height_ != height) {
231 #if GLIBMM_MAJOR_VERSION > 2 || (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION >= 14)
237 pixbuf_ = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB,
false, 8, width_, height_);
239 set_size_request(width_, height_);
266 Glib::RefPtr<Gdk::Pixbuf>
306 RGB_t *target = RGB_PIXEL_AT(pixbuf_->get_pixels(), width_, x, y);
325 unsigned char *buffer,
330 if (!width || !height || (width == width_ && height == height_)) {
331 convert(colorspace, RGB, buffer, pixbuf_->get_pixels(), width_, height_);
333 unsigned char *scaled_buffer =
334 (
unsigned char *)malloc(colorspace_buffer_size(colorspace, width_, height_));
344 convert(colorspace, RGB, scaled_buffer, pixbuf_->get_pixels(), width_, height_);
350 printf(
"ImageWidget::show(): %s\n", e.
what());
356 signal_show_.emit(colorspace, buffer, width, height);
359 printf(
"ImageWidget::show(): Could not set the new image (%s)\n", e.
what());
374 sigc::signal<void, colorspace_t, unsigned char *, unsigned int, unsigned int> &
388 refresh_thread_->set_delay(refresh_delay);
398 refresh_thread_->refresh_cam();
407 ImageWidget::set_cam()
415 if (cam_has_buffer_) {
418 cam_has_buffer_ =
false;
440 pixbuf_->save(filename, type);
441 cam_mutex_->unlock();
443 }
catch (Glib::Exception &e) {
444 cam_mutex_->unlock();
445 printf(
"save failed: %s\n", e.what().c_str());
463 unsigned int img_num)
465 refresh_thread_->save_on_refresh(enable, path, type, img_num);
475 return refresh_thread_->get_img_num();
484 ImageWidget::RefThread::RefThread(
ImageWidget *widget,
unsigned int refresh_delay)
485 : Thread(
"ImageWidget refresh thread")
487 set_delete_on_exit(
true);
496 dispatcher_.connect(sigc::mem_fun(*widget, &ImageWidget::set_cam));
498 set_delay(refresh_delay);
507 ImageWidget::RefThread::set_delay(
unsigned int refresh_delay)
509 refresh_delay_ = refresh_delay;
517 ImageWidget::RefThread::refresh_cam()
526 ImageWidget::RefThread::perform_refresh()
528 if (!widget_->cam_) {
533 if (widget_->cam_mutex_->try_lock()) {
534 widget_->cam_->dispose_buffer();
535 widget_->cam_->capture();
537 widget_->cam_has_buffer_ =
true;
538 widget_->cam_mutex_->unlock();
540 if (widget_->cam_->ready()) {
545 if (widget_->cam_has_timestamp_) {
555 Glib::ustring fn = ctmp;
556 widget_->save_image(fn, save_type_);
559 printf(
"Cannot save image, asprintf() ran out of memory\n");
563 printf(
"Cannot save image (%s)\n", e.
what());
566 if (asprintf(&ctmp,
"%s/%06u.%s", save_path_.c_str(), ++save_num_, save_type_.c_str())
568 Glib::ustring fn = ctmp;
569 widget_->save_image(fn, save_type_);
572 printf(
"Cannot save image, asprintf() ran out of memory\n");
580 printf(
"Could not capture the image (%s)\n", e.
what());
585 ImageWidget::RefThread::loop()
590 if (refresh_delay_ && !(loop_cnt_ % refresh_delay_)) {
611 ImageWidget::RefThread::stop()
623 ImageWidget::RefThread::save_on_refresh(
bool enabled,
626 unsigned int img_num)
628 save_imgs_ = enabled;
641 ImageWidget::RefThread::get_img_num()
Base class for exceptions in Fawkes.
virtual const char * what() const
Get primary string.
Expected parameter is missing.
Mutex mutual exclusion lock.
void lock()
Lock this mutex.
void unlock()
Unlock the mutex.
A NULL pointer was supplied where not allowed.
A class for handling time.
long in_msec() const
Convert the stored time into milli-seconds.
Camera interface for image aquiring devices in FireVision.
virtual fawkes::Time * capture_time()
Get the Time of the last successfully captured image.
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual void flush()=0
Flush image queue.
virtual unsigned int pixel_width()=0
Width of image in pixels.
virtual colorspace_t colorspace()=0
Colorspace of returned image.
virtual unsigned char * buffer()=0
Get access to current image buffer.
virtual void scale()
Scale image.
virtual void set_scaled_dimensions(unsigned int width, unsigned int height)
Set dimenins of scaled image buffer.
virtual void set_original_dimensions(unsigned int width, unsigned int height)
Set original image dimensions.
virtual void set_original_buffer(unsigned char *buffer)
Set original image buffer.
virtual void set_scaled_buffer(unsigned char *buffer)
Set scaled image buffer.
Structure defining an RGB pixel (in R-G-B byte ordering).