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> 64 SharedMemoryImageBuffer::SharedMemoryImageBuffer(
const char *image_id,
73 constructor(image_id, cspace, width, height,
false);
89 :
SharedMemory(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, is_read_only, false, false)
91 constructor(image_id, CS_UNKNOWN, 0, 0, is_read_only);
96 SharedMemoryImageBuffer::constructor(
const char *
image_id, colorspace_t cspace,
103 _colorspace = cspace;
113 e.
append(
"SharedMemoryImageBuffer: could not attach to '%s'\n",
image_id);
141 _image_id = strdup(image_id);
156 strncpy(raw_header->
frame_id, frame_id, FRAME_ID_MAX_LENGTH);
208 throw Exception(
"Buffer is read-only. Not setting capture time.");
224 throw Exception(
"Buffer is read-only. Not setting capture time.");
237 return (
unsigned char *)
_memptr;
257 return raw_header->
width;
267 return raw_header->
height;
277 return raw_header->
roi_x;
287 return raw_header->
roi_y;
348 throw Exception(
"Buffer is read-only. Not setting ROI X.");
361 throw Exception(
"Buffer is read-only. Not setting ROI Y.");
374 throw Exception(
"Buffer is read-only. Not setting ROI width.");
387 throw Exception(
"Buffer is read-only. Not setting ROI height.");
401 unsigned int roi_w,
unsigned int roi_h)
404 throw Exception(
"Buffer is read-only. Not setting ROI X/Y.");
420 throw Exception(
"Buffer is read-only. Not setting circle X.");
433 throw Exception(
"Buffer is read-only. Not setting circle Y.");
446 throw Exception(
"Buffer is read-only. Not setting circle radius.");
461 throw Exception(
"Buffer is read-only. Not setting circle X/Y/radius.");
496 SharedMemory::list(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h, lister);
506 std::list<SharedMemoryImageBufferMetaData>
509 #if __cplusplus >= 201103L 510 std::unique_ptr<SharedMemoryImageBufferMetaDataCollector>
512 std::unique_ptr<SharedMemoryImageBufferHeader>
515 std::auto_ptr<SharedMemoryImageBufferMetaDataCollector>
517 std::auto_ptr<SharedMemoryImageBufferHeader>
521 SharedMemory::list(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h.get(), lister.get());
522 return lister->meta_data();
540 SharedMemory::erase_orphaned(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h, lister);
556 bool ex = SharedMemory::exists(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h);
571 SharedMemory::erase(FIREVISION_SHM_IMAGE_MAGIC_TOKEN, h, NULL);
584 _colorspace = CS_UNKNOWN;
590 _orig_image_id = NULL;
591 _orig_frame_id = NULL;
606 _image_id = strdup(image_id);
613 _orig_image_id = NULL;
614 _orig_frame_id = NULL;
617 _orig_colorspace = CS_UNKNOWN;
626 if ( h->_image_id != NULL ) {
627 _image_id = strdup(h->_image_id);
631 if ( h->_frame_id != NULL ) {
632 _frame_id = strdup(h->_frame_id);
636 _colorspace = h->_colorspace;
638 _height = h->_height;
641 _orig_image_id = NULL;
642 _orig_frame_id = NULL;
645 _orig_colorspace = CS_UNKNOWN;
652 if ( _image_id != NULL)
free(_image_id);
653 if ( _frame_id != NULL)
free(_frame_id);
654 if ( _orig_image_id != NULL)
free(_orig_image_id);
655 if ( _orig_frame_id != NULL)
free(_orig_frame_id);
677 return colorspace_buffer_size(_colorspace, _width, _height);
689 if (_image_id == NULL) {
692 }
else if (strncmp(h->
image_id, _image_id, IMAGE_ID_MAX_LENGTH) == 0) {
693 if ( (_colorspace == CS_UNKNOWN) ||
694 (((colorspace_t)h->
colorspace == _colorspace) &&
695 (h->
width == _width) &&
697 (! _frame_id || (strncmp(h->
frame_id, _frame_id, FRAME_ID_MAX_LENGTH) == 0))
726 return ( (strncmp(_image_id, h->_image_id, IMAGE_ID_MAX_LENGTH) == 0) &&
727 (! _frame_id || (strncmp(_frame_id, h->_frame_id, FRAME_ID_MAX_LENGTH) == 0)) &&
728 (_colorspace == h->_colorspace) &&
729 (_width == h->_width) &&
730 (_height == h->_height) );
738 if (_image_id == NULL) {
739 cout <<
"No image set" << endl;
742 cout <<
"SharedMemory Image Info: " << endl;
743 printf(
" address: %p\n",
_header);
744 cout <<
" image id: " << _image_id << endl
745 <<
" frame id: " << (_frame_id ? _frame_id :
"NOT SET") << endl
746 <<
" colorspace: " << _colorspace << endl
747 <<
" dimensions: " << _width <<
"x" << _height << endl;
765 return ( (_colorspace != CS_UNKNOWN) &&
777 strncpy(header->
image_id, _image_id, IMAGE_ID_MAX_LENGTH);
779 strncpy(header->
frame_id, _frame_id, FRAME_ID_MAX_LENGTH);
782 header->
width = _width;
793 if ( NULL != _orig_image_id )
free(_orig_image_id);
794 if ( NULL != _image_id ) {
795 _orig_image_id = strdup(_image_id);
798 _orig_image_id = NULL;
800 if ( NULL != _orig_frame_id )
free(_orig_frame_id);
801 if ( NULL != _frame_id ) {
802 _orig_frame_id = strdup(_frame_id);
805 _orig_frame_id = NULL;
807 _orig_width = _width;
808 _orig_height = _height;
809 _orig_colorspace = _colorspace;
812 _image_id = strndup(header->
image_id, IMAGE_ID_MAX_LENGTH);
813 _frame_id = strndup(header->
frame_id, FRAME_ID_MAX_LENGTH);
814 _width = header->
width;
816 _colorspace = (colorspace_t)header->
colorspace;
823 if ( NULL != _image_id ) {
827 if ( _orig_image_id != NULL ) {
828 _image_id = strdup(_orig_image_id);
830 if ( NULL != _frame_id ) {
834 if ( _orig_frame_id != NULL ) {
835 _frame_id = strdup(_orig_frame_id);
838 _height =_orig_height;
839 _colorspace =_orig_colorspace;
851 else return _colorspace;
903 if ( _image_id != NULL)
::free(_image_id);
904 _image_id = strdup(image_id);
914 if ( _frame_id != NULL)
::free(_frame_id);
915 _frame_id = strdup(frame_id);
948 cout << endl << cgreen <<
"FireVision Shared Memory Segments - Images" << cnormal << endl
949 <<
"========================================================================================" << endl
951 printf (
"%-20s %-20s %-10s %-10s %-9s %-16s %-5s %-5s %s\n",
952 "Image ID",
"Frame ID",
"ShmID",
"Semaphore",
"Bytes",
"Color Space",
"Width",
"Height",
955 <<
"----------------------------------------------------------------------------------------" << endl;
968 cout <<
"No FireVision shared memory segments found" << endl;
975 cout <<
"No orphaned FireVision shared memory segments found" << endl;
981 int shm_id,
int semaphore,
982 unsigned int mem_size,
990 printf(
"%-20s %-20s %-10d %-10d %-9u %-16s %-5u %-5u %s%s\n",
993 (SharedMemory::is_swapable(shm_id) ?
"S" :
""),
994 (SharedMemory::is_destroyed(shm_id) ?
"D" :
"")
1010 mem_swapable =
false;
1011 mem_destroyed =
false;
1028 bool mem_swapable,
bool mem_destroyed)
1033 this->width =
width;
1035 this->mem_size = mem_size;
1036 this->mem_swapable = mem_swapable;
1037 this->mem_destroyed = mem_destroyed;
1082 int shm_id,
int semaphore,
1083 unsigned int mem_size,
1090 SharedMemory::is_swapable(shm_id),
1091 SharedMemory::is_destroyed(shm_id)));
void set_circle_found(bool found)
Set circle found.
virtual void print_no_orphaned_segments()
Print this if no matching orphaned segment was found.
fawkes::Time capture_time() const
Get the time when the image was captured.
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
unsigned int roi_y() const
Get ROI Y.
virtual ~SharedMemoryImageBufferLister()
Destructor.
Fawkes library namespace.
const char * image_id() const
Get Image ID.
bool set_image_id(const char *image_id)
Set image number.
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.
unsigned char * buffer() const
Get image buffer.
static std::list< SharedMemoryImageBufferMetaData > list_meta_data()
Get meta data about image buffers.
virtual void print_footer()
Print footer of the table.
bool is_read_only() const
Check for read-only mode.
bool circle_found() const
Check if circle was found .
void free()
Detach from and maybe destroy the shared memory segment.
unsigned int roi_width() const
Get ROI width.
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.
void attach()
Attach to the shared memory segment.
void set_circle_radius(unsigned int circle_radius)
Set circle radius.
void set_frame_id(const char *frame_id)
Set frame ID.
SharedMemoryImageBufferLister()
Constructor.
void * memptr() const
Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared me...
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.
unsigned int width() const
Get image width.
void add_semaphore()
Add semaphore to shared memory segment.
unsigned int roi_x() const
Get ROI X.
Shared memory image buffer lister.
int circle_x() const
Get circle X.
bool _is_read_only
Read-only.
SharedMemoryHeader * _header
Data-specific header.
unsigned int circle_radius() const
Get circle radius.
static void list()
List all shared memory segments that contain a FireVision image.
const char * frame_id() const
Get frame ID.
void set_roi_width(unsigned int roi_w)
Set ROI width.
void set_circle_y(int circle_y)
Set circle Y.
virtual void print_header()
Print header of the table.
static bool exists(const char *image_id)
Check image availability.
void set_capture_time(fawkes::Time *time)
Set the capture time.
unsigned int roi_height() const
Get ROI height.
static void wipe(const char *image_id)
Erase a specific shared memory segment that contains an image.
unsigned int height() const
Get image height.
int circle_y() const
Get circle Y.
void append(const char *format,...)
Append messages to the message list.
colorspace_t colorspace() const
Get color space.
virtual void print_info(const fawkes::SharedMemoryHeader *header, int shm_id, int semaphore, unsigned int mem_size, const void *memptr)
Print info about segment.