23 #include "image_widget.h" 25 #include <core/exceptions/software.h> 26 #include <core/threading/mutex.h> 27 #include <fvutils/color/conversions.h> 28 #include <fvutils/color/yuv.h> 29 #include <fvutils/scalers/lossy.h> 30 #include <fvcams/camera.h> 55 __cam_enabled =
false;
57 __refresh_thread = NULL;
79 __cam_has_buffer =
false;
86 __cam_has_timestamp =
true;
89 __cam_has_timestamp =
false;
92 __refresh_thread =
new RefThread(
this, refresh_delay);
93 __refresh_thread->start();
94 __refresh_thread->refresh_cam();
109 : Gtk::Image(cobject)
112 __cam_enabled =
false;
114 __refresh_thread = NULL;
131 : Gtk::Image( cobject )
134 __cam_enabled =
false;
136 __refresh_thread = NULL;
147 if (__refresh_thread) __refresh_thread->stop();
164 __cam_enabled =
true;
165 __cam_has_buffer =
false;
172 __cam_has_timestamp =
true;
175 __cam_has_timestamp =
false;
178 if ( __refresh_thread ) {
179 __refresh_thread->set_delay(refresh_delay);
181 __refresh_thread =
new RefThread(
this, refresh_delay);
182 __refresh_thread->start();
185 __refresh_thread->refresh_cam();
197 if ( !enable && __cam_enabled ) {
198 __refresh_thread->stop();
199 }
else if ( __refresh_thread && enable && !__cam_enabled ) {
200 __refresh_thread->start();
203 __cam_enabled = enable;
219 if (!width || ! height) {
229 if (!__pixbuf || __width != width || __height != height) {
233 #if GLIBMM_MAJOR_VERSION > 2 || ( GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION >= 14 ) 239 __pixbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB,
false, 8, __width, __height);
241 set_size_request(__width, __height);
268 Glib::RefPtr<Gdk::Pixbuf>
302 RGB_t * target = RGB_PIXEL_AT(__pixbuf->get_pixels(), __width, x, y);
320 ImageWidget::show(colorspace_t colorspace,
unsigned char *buffer,
unsigned int width,
unsigned int height)
323 if (!width || !height || (width == __width && height == __height)) {
324 convert(colorspace, RGB, buffer, __pixbuf->get_pixels(), __width, __height);
327 unsigned char *scaled_buffer = (
unsigned char *)malloc(colorspace_buffer_size(colorspace, __width, __height));
337 convert(colorspace, RGB, scaled_buffer, __pixbuf->get_pixels(), __width, __height);
344 printf(
"ImageWidget::show(): %s\n", e.
what());
350 __signal_show.emit(colorspace, buffer, width, height);
354 printf(
"ImageWidget::show(): Could not set the new image (%s)\n", e.
what());
370 sigc::signal<void, colorspace_t, unsigned char *, unsigned int, unsigned int> &
373 return __signal_show;
385 __refresh_thread->set_delay(refresh_delay);
395 if ( __cam_enabled ) {
396 __refresh_thread->refresh_cam();
405 ImageWidget::set_cam()
407 if ( !__cam_enabled ) {
return; }
411 if (__cam_has_buffer) {
414 __cam_has_buffer =
false;
436 __pixbuf->save(filename, type);
440 catch (Glib::Exception &e) {
442 printf(
"save failed: %s\n", e.what().c_str());
459 __refresh_thread->save_on_refresh(enable, path, type, img_num);
469 return __refresh_thread->get_img_num();
478 ImageWidget::RefThread::RefThread(
ImageWidget *widget,
unsigned int refresh_delay)
479 : Thread(
"ImageWidget refresh thread")
481 set_delete_on_exit(
true);
485 __do_refresh =
false;
490 __dispatcher.connect( sigc::mem_fun( *widget , &ImageWidget::set_cam ) );
492 set_delay(refresh_delay);
501 ImageWidget::RefThread::set_delay(
unsigned int refresh_delay)
503 __refresh_delay = refresh_delay;
511 ImageWidget::RefThread::refresh_cam()
520 ImageWidget::RefThread::perform_refresh()
522 if (!__widget->__cam) {
527 if (__widget->__cam_mutex->try_lock()) {
528 __widget->__cam->dispose_buffer();
529 __widget->__cam->capture();
531 __widget->__cam_has_buffer =
true;
532 __widget->__cam_mutex->unlock();
534 if (__widget->__cam->ready()) {
539 if (__widget->__cam_has_timestamp) {
542 if (asprintf(&ctmp,
"%s/%06u.%ld.%s", __save_path.c_str(), ++__save_num, ts->
in_msec(), __save_type.c_str()) != -1) {
543 Glib::ustring fn = ctmp;
544 __widget->save_image(fn, __save_type);
547 printf(
"Cannot save image, asprintf() ran out of memory\n");
552 printf(
"Cannot save image (%s)\n", e.
what());
556 if (asprintf(&ctmp,
"%s/%06u.%s", __save_path.c_str(), ++__save_num, __save_type.c_str()) != -1) {
557 Glib::ustring fn = ctmp;
558 __widget->save_image(fn, __save_type);
561 printf(
"Cannot save image, asprintf() ran out of memory\n");
570 printf(
"Could not capture the image (%s)\n", e.
what());
576 ImageWidget::RefThread::loop()
581 if (__refresh_delay && !(__loop_cnt % __refresh_delay)) {
583 __do_refresh =
false;
589 __do_refresh =
false;
602 ImageWidget::RefThread::stop()
615 ImageWidget::RefThread::save_on_refresh(
bool enabled, std::string path, Glib::ustring type,
unsigned int img_num)
617 __save_imgs = enabled;
622 __save_num = img_num;
630 ImageWidget::RefThread::get_img_num()
Structure defining an RGB pixel (in R-G-B byte ordering).
virtual void set_scaled_buffer(unsigned char *buffer)
Set scaled image buffer.
virtual void set_original_buffer(unsigned char *buffer)
Set original image buffer.
Camera interface for image aquiring devices in FireVision.
void unlock()
Unlock the mutex.
virtual unsigned int pixel_width()=0
Width of image in pixels.
A class for handling time.
virtual const char * what() const
Get primary string.
A NULL pointer was supplied where not allowed.
virtual colorspace_t colorspace()=0
Colorspace of returned image.
long in_msec() const
Convert the stored time into milli-seconds.
Base class for exceptions in Fawkes.
virtual fawkes::Time * capture_time()
Get the Time of the last successfully captured image.
virtual void flush()=0
Flush image queue.
virtual void set_original_dimensions(unsigned int width, unsigned int height)
Set original image dimensions.
virtual void set_scaled_dimensions(unsigned int width, unsigned int height)
Set dimenins of scaled image buffer.
virtual unsigned char * buffer()=0
Get access to current image buffer.
virtual unsigned int pixel_height()=0
Height of image in pixels.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
virtual void scale()
Scale image.
Expected parameter is missing.