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>
37 using namespace fawkes;
39 namespace firevision {
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,
96 unsigned int width,
unsigned int height,
99 _image_id = strdup(image_id);
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.");
560 _colorspace = CS_UNKNOWN;
566 _orig_image_id = NULL;
567 _orig_frame_id = NULL;
578 colorspace_t colorspace,
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;
615 _header = h->_header;
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);
652 if (_header == NULL) {
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;
826 if ( _header)
return (colorspace_t)_header->
colorspace;
827 else return _colorspace;
837 if ( _header)
return _header->
width;
848 if ( _header)
return _header->
height;
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,
964 const char *colorspace = colorspace_to_string(h->
colorspace());
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" :
"")