24 #include <core/exception.h> 25 #include <fvutils/ipc/shm_image.h> 26 #include <fvutils/ipc/shm_exceptions.h> 27 #include <utils/system/console_colors.h> 28 #include <utils/ipc/shm_exceptions.h> 29 #include <utils/misc/strndup.h> 63 SharedMemoryImageBuffer::SharedMemoryImageBuffer(
const char *image_id,
72 constructor(image_id, cspace, width, height,
false);
88 :
SharedMemory(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, is_read_only, false, false)
90 constructor(image_id, CS_UNKNOWN, 0, 0, is_read_only);
95 SharedMemoryImageBuffer::constructor(
const char *
image_id, colorspace_t cspace,
102 _colorspace = cspace;
112 e.
append(
"SharedMemoryImageBuffer: could not attach to '%s'\n",
image_id);
140 _image_id = strdup(image_id);
155 strncpy(raw_header->
frame_id, frame_id, FRAME_ID_MAX_LENGTH);
207 throw Exception(
"Buffer is read-only. Not setting capture time.");
223 throw Exception(
"Buffer is read-only. Not setting capture time.");
236 return (
unsigned char *)
_memptr;
256 return raw_header->
width;
266 return raw_header->
height;
276 return raw_header->
roi_x;
286 return raw_header->
roi_y;
347 throw Exception(
"Buffer is read-only. Not setting ROI X.");
360 throw Exception(
"Buffer is read-only. Not setting ROI Y.");
373 throw Exception(
"Buffer is read-only. Not setting ROI width.");
386 throw Exception(
"Buffer is read-only. Not setting ROI height.");
400 unsigned int roi_w,
unsigned int roi_h)
403 throw Exception(
"Buffer is read-only. Not setting ROI X/Y.");
419 throw Exception(
"Buffer is read-only. Not setting circle X.");
432 throw Exception(
"Buffer is read-only. Not setting circle Y.");
445 throw Exception(
"Buffer is read-only. Not setting circle radius.");
460 throw Exception(
"Buffer is read-only. Not setting circle X/Y/radius.");
495 SharedMemory::list(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h, lister);
516 SharedMemory::erase_orphaned(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h, lister);
532 bool ex = SharedMemory::exists(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h);
547 SharedMemory::erase(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h, NULL);
560 _colorspace = CS_UNKNOWN;
566 _orig_image_id = NULL;
567 _orig_frame_id = NULL;
582 _image_id = strdup(image_id);
589 _orig_image_id = NULL;
590 _orig_frame_id = NULL;
593 _orig_colorspace = CS_UNKNOWN;
602 if ( h->_image_id != NULL ) {
603 _image_id = strdup(h->_image_id);
607 if ( h->_frame_id != NULL ) {
608 _frame_id = strdup(h->_frame_id);
612 _colorspace = h->_colorspace;
614 _height = h->_height;
617 _orig_image_id = NULL;
618 _orig_frame_id = NULL;
621 _orig_colorspace = CS_UNKNOWN;
628 if ( _image_id != NULL)
free(_image_id);
629 if ( _frame_id != NULL)
free(_frame_id);
630 if ( _orig_image_id != NULL)
free(_orig_image_id);
631 if ( _orig_frame_id != NULL)
free(_orig_frame_id);
653 return colorspace_buffer_size(_colorspace, _width, _height);
665 if (_image_id == NULL) {
668 }
else if (strncmp(h->
image_id, _image_id, IMAGE_ID_MAX_LENGTH) == 0) {
669 if ( (_colorspace == CS_UNKNOWN) ||
670 (((colorspace_t)h->
colorspace == _colorspace) &&
671 (h->
width == _width) &&
673 (! _frame_id || (strncmp(h->
frame_id, _frame_id, FRAME_ID_MAX_LENGTH) == 0))
702 return ( (strncmp(_image_id, h->_image_id, IMAGE_ID_MAX_LENGTH) == 0) &&
703 (! _frame_id || (strncmp(_frame_id, h->_frame_id, FRAME_ID_MAX_LENGTH) == 0)) &&
704 (_colorspace == h->_colorspace) &&
705 (_width == h->_width) &&
706 (_height == h->_height) );
714 if (_image_id == NULL) {
715 cout <<
"No image set" << endl;
718 cout <<
"SharedMemory Image Info: " << endl;
719 printf(
" address: %p\n",
_header);
720 cout <<
" image id: " << _image_id << endl
721 <<
" frame id: " << (_frame_id ? _frame_id :
"NOT SET") << endl
722 <<
" colorspace: " << _colorspace << endl
723 <<
" dimensions: " << _width <<
"x" << _height << endl;
741 return ( (_colorspace != CS_UNKNOWN) &&
753 strncpy(header->
image_id, _image_id, IMAGE_ID_MAX_LENGTH);
755 strncpy(header->
frame_id, _frame_id, FRAME_ID_MAX_LENGTH);
758 header->
width = _width;
769 if ( NULL != _orig_image_id )
free(_orig_image_id);
770 if ( NULL != _image_id ) {
771 _orig_image_id = strdup(_image_id);
774 _orig_image_id = NULL;
776 if ( NULL != _orig_frame_id )
free(_orig_frame_id);
777 if ( NULL != _frame_id ) {
778 _orig_frame_id = strdup(_frame_id);
781 _orig_frame_id = NULL;
783 _orig_width = _width;
784 _orig_height = _height;
785 _orig_colorspace = _colorspace;
788 _image_id = strndup(header->
image_id, IMAGE_ID_MAX_LENGTH);
789 _frame_id = strndup(header->
frame_id, FRAME_ID_MAX_LENGTH);
790 _width = header->
width;
792 _colorspace = (colorspace_t)header->
colorspace;
799 if ( NULL != _image_id ) {
803 if ( _orig_image_id != NULL ) {
804 _image_id = strdup(_orig_image_id);
806 if ( NULL != _frame_id ) {
810 if ( _orig_frame_id != NULL ) {
811 _frame_id = strdup(_orig_frame_id);
814 _height =_orig_height;
815 _colorspace =_orig_colorspace;
827 else return _colorspace;
879 if ( _image_id != NULL)
::free(_image_id);
880 _image_id = strdup(image_id);
890 if ( _frame_id != NULL)
::free(_frame_id);
891 _frame_id = strdup(frame_id);
924 cout << endl << cgreen <<
"FireVision Shared Memory Segments - Images" << cnormal << endl
925 <<
"========================================================================================" << endl
927 printf (
"%-20s %-20s %-10s %-10s %-9s %-16s %-5s %-5s %s\n",
928 "Image ID",
"Frame ID",
"ShmID",
"Semaphore",
"Bytes",
"Color Space",
"Width",
"Height",
931 <<
"----------------------------------------------------------------------------------------" << endl;
944 cout <<
"No FireVision shared memory segments found" << endl;
951 cout <<
"No orphaned FireVision shared memory segments found" << endl;
957 int shm_id,
int semaphore,
958 unsigned int mem_size,
966 printf(
"%-20s %-20s %-10d %-10d %-9u %-16s %-5u %-5u %s%s\n",
969 (SharedMemory::is_swapable(shm_id) ?
"S" :
""),
970 (SharedMemory::is_destroyed(shm_id) ?
"D" :
"")
void set_circle_found(bool found)
Set circle found.
virtual void print_no_orphaned_segments()
Print this if no matching orphaned segment was found.
int circle_x() const
Get circle X.
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
const char * frame_id() const
Get frame ID.
virtual ~SharedMemoryImageBufferLister()
Destructor.
Fawkes library namespace.
bool set_image_id(const char *image_id)
Set image number.
unsigned int height() const
Get image height.
void set_roi_height(unsigned int roi_h)
Set ROI height.
void set_roi_x(unsigned int roi_x)
Set ROI X.
virtual void print_no_segments()
Print this if no matching segment was found.
static void cleanup(bool use_lister=true)
Erase all shared memory segments that contain FireVision images.
A class for handling time.
void set_roi_y(unsigned int roi_y)
Set ROI Y.
Throw if an inconsistent image was found.
SharedMemoryImageBuffer(const char *image_id, colorspace_t cspace, unsigned int width, unsigned int height)
Write Constructor.
~SharedMemoryImageBuffer()
Destructor.
virtual void print_footer()
Print footer of the table.
bool is_read_only() const
Check for read-only mode.
void free()
Detach from and maybe destroy the shared memory segment.
Base class for exceptions in Fawkes.
void set_roi(unsigned int roi_x, unsigned int roi_y, unsigned int roi_w, unsigned int roi_h)
Set ROI data.
unsigned int circle_radius() const
Get circle radius.
void attach()
Attach to the shared memory segment.
void set_circle_radius(unsigned int circle_radius)
Set circle radius.
unsigned int roi_height() const
Get ROI height.
void set_frame_id(const char *frame_id)
Set frame ID.
SharedMemoryImageBufferLister()
Constructor.
unsigned char * buffer() const
Get image buffer.
bool circle_found() const
Check if circle was found .
unsigned int roi_x() const
Get ROI X.
void * _memptr
Pointer to the data segment.
void set_circle(int x, int y, unsigned int r)
Set circle data.
void set_circle_x(int circle_x)
Set circle X.
void add_semaphore()
Add semaphore to shared memory segment.
unsigned int width() const
Get image width.
Shared memory image buffer lister.
bool _is_read_only
Read-only.
SharedMemoryHeader * _header
Data-specific header.
const char * image_id() const
Get Image ID.
void * memptr() const
Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared me...
static void list()
List all shared memory segments that contain a FireVision image.
void set_roi_width(unsigned int roi_w)
Set ROI width.
void set_circle_y(int circle_y)
Set circle Y.
int circle_y() const
Get circle Y.
virtual void print_header()
Print header of the table.
static bool exists(const char *image_id)
Check image availability.
unsigned int roi_y() const
Get ROI Y.
void set_capture_time(fawkes::Time *time)
Set the capture time.
colorspace_t colorspace() const
Get color space.
static void wipe(const char *image_id)
Erase a specific shared memory segment that contains an image.
unsigned int roi_width() const
Get ROI width.
void append(const char *format,...)
Append messages to the message list.
virtual void print_info(const fawkes::SharedMemoryHeader *header, int shm_id, int semaphore, unsigned int mem_size, const void *memptr)
Print info about segment.
fawkes::Time capture_time() const
Get the time when the image was captured.