shm_image.h

00001
00002 /***************************************************************************
00003  *  shm_image.h - shared memory image buffer
00004  *
00005  *  Created: Thu Jan 12 13:12:24 2006
00006  *  Copyright  2005-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #ifndef __FIREVISION_FVUTILS_IPC_SHM_IMAGE_H_
00025 #define __FIREVISION_FVUTILS_IPC_SHM_IMAGE_H_
00026 
00027 #include <utils/ipc/shm.h>
00028 #include <utils/ipc/shm_lister.h>
00029 #include <utils/time/time.h>
00030
00031 #include <fvutils/ipc/defs.h>
00032 #include <fvutils/color/colorspaces.h>
00033
00034
00035 // Magic token to identify FireVision shared memory images
00036 #define FIREVISION_SHM_IMAGE_MAGIC_TOKEN "FireVision Image"
00037 
00038 // Not that there is a relation to ITPimage_packet_header_t
00039 /** Shared memory header struct for FireVision images. */
00040 typedef struct {
00041   char          image_id[IMAGE_ID_MAX_LENGTH];/**< image ID */
00042   unsigned int  colorspace;             /**< color space */
00043   unsigned int  width;                  /**< width */
00044   unsigned int  height;                 /**< height */
00045   unsigned int  roi_x;                  /**< ROI start x */
00046   unsigned int  roi_y;                  /**< ROI start y */
00047   unsigned int  roi_width;              /**< ROI width */
00048   unsigned int  roi_height;             /**< ROI height */
00049   // Circle relative to ROI
00050   int           circle_x;               /**< ROI circle center x */
00051   int           circle_y;               /**< ROI circle center y */
00052   unsigned int  circle_radius;          /**< ROI circle radius */
00053   long int      capture_time_sec;       /**< Time in seconds since the epoch when
00054                                          * the image was captured. */
00055   long int      capture_time_usec;      /**< Addendum to capture_time_sec in
00056                                          * micro seconds. */
00057   unsigned int  flag_circle_found :  1; /**< 1 if circle found */
00058   unsigned int  flag_image_ready  :  1; /**< 1 if image ready */
00059   unsigned int  flag_reserved     : 30; /**< reserved for future use */
00060 } SharedMemoryImageBuffer_header_t;
00061
00062
00063 class SharedMemoryImageBufferHeader
00064 : public fawkes::SharedMemoryHeader
00065 {
00066  public:
00067   SharedMemoryImageBufferHeader();
00068   SharedMemoryImageBufferHeader(const char *image_id,
00069                                 colorspace_t colorspace,
00070                                 unsigned int width,
00071                                 unsigned int height);
00072   SharedMemoryImageBufferHeader(const SharedMemoryImageBufferHeader *h);
00073   virtual ~SharedMemoryImageBufferHeader();
00074
00075   virtual fawkes::SharedMemoryHeader *  clone() const;
00076   virtual bool         matches(void *memptr);
00077   virtual size_t       size();
00078   virtual void         print_info();
00079   virtual bool         create();
00080   virtual void         initialize(void *memptr);
00081   virtual void         set(void *memptr);
00082   virtual void         reset();
00083   virtual size_t       data_size();
00084   virtual bool         operator==(const fawkes::SharedMemoryHeader & s) const;
00085
00086   void                 set_image_id(const char *image_id);
00087   colorspace_t         colorspace() const;
00088   unsigned int         width() const;
00089   unsigned int         height() const;
00090   const char *         image_id() const;
00091
00092   SharedMemoryImageBuffer_header_t * raw_header();
00093
00094  private:
00095   char          *_image_id;
00096   colorspace_t   _colorspace;
00097   unsigned int   _width;
00098   unsigned int   _height;
00099
00100   char          *_orig_image_id;
00101   colorspace_t   _orig_colorspace;
00102   unsigned int   _orig_width;
00103   unsigned int   _orig_height;
00104
00105   SharedMemoryImageBuffer_header_t *_header;
00106 };
00107
00108 class SharedMemoryImageBufferLister
00109 : public fawkes::SharedMemoryLister
00110 {
00111  public:
00112   SharedMemoryImageBufferLister();
00113   virtual ~SharedMemoryImageBufferLister();
00114
00115   virtual void print_header();
00116   virtual void print_footer();
00117   virtual void print_no_segments();
00118   virtual void print_no_orphaned_segments();
00119   virtual void print_info(const fawkes::SharedMemoryHeader *header,
00120                           int shm_id, int semaphore,
00121                           unsigned int mem_size,
00122                           const void *memptr);
00123 };
00124
00125
00126 class SharedMemoryImageBuffer : public fawkes::SharedMemory
00127 {
00128
00129  public:
00130   SharedMemoryImageBuffer(const char *image_id,
00131                           colorspace_t cspace,
00132                           unsigned int width, unsigned int height);
00133   SharedMemoryImageBuffer(const char *image_id, bool is_read_only = true);
00134   ~SharedMemoryImageBuffer();
00135
00136   const char *     image_id() const;
00137   unsigned char *  buffer() const;
00138   colorspace_t     colorspace() const;
00139   unsigned int     width() const;
00140   unsigned int     height() const;
00141   unsigned int     roi_x() const;
00142   unsigned int     roi_y() const;
00143   unsigned int     roi_width() const;
00144   unsigned int     roi_height() const;
00145   int              circle_x() const;
00146   int              circle_y() const;
00147   unsigned int     circle_radius() const;
00148   bool             circle_found() const;
00149   void             set_roi_x(unsigned int roi_x);
00150   void             set_roi_y(unsigned int roi_y);
00151   void             set_roi_width(unsigned int roi_w);
00152   void             set_roi_height(unsigned int roi_h);
00153   void             set_roi(unsigned int roi_x, unsigned int roi_y,
00154                            unsigned int roi_w, unsigned int roi_h);
00155   void             set_circle_x(int circle_x);
00156   void             set_circle_y(int circle_y);
00157   void             set_circle_radius(unsigned int circle_radius);
00158   void             set_circle(int x, int y, unsigned int r);
00159   void             set_circle_found(bool found);
00160   bool             set_image_id(const char *image_id);
00161
00162   fawkes::Time     capture_time() const;
00163   void             capture_time(long int *sec, long int *usec) const;
00164   void             set_capture_time(fawkes::Time *time);
00165   void             set_capture_time(long int sec, long int usec);
00166
00167   static void      list();
00168   static void      cleanup(bool use_lister = true);
00169   static bool      exists(const char *image_id);
00170   static void      wipe(const char *image_id);
00171
00172  private:
00173   void constructor(const char *image_id, colorspace_t cspace,
00174                    unsigned int width, unsigned int height,
00175                    bool is_read_only);
00176
00177   SharedMemoryImageBufferHeader    *priv_header;
00178   SharedMemoryImageBuffer_header_t *raw_header;
00179
00180   char *         _image_id;
00181   colorspace_t   _colorspace;
00182   unsigned int   _width;
00183   unsigned int   _height;
00184
00185
00186 };
00187
00188
00189 #endif