fuse_lut_content.cpp

00001 
00002 /***************************************************************************
00003  *  fuse_lut_content.cpp - FUSE LUT content encapsulation
00004  *
00005  *  Created: Wed Nov 21 16:49:18 2007
00006  *  Copyright  2005-2007  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 #include <fvutils/net/fuse_lut_content.h>
00025 #include <fvutils/ipc/shm_lut.h>
00026 
00027 #include <core/exceptions/system.h>
00028 #include <core/exceptions/software.h>
00029 
00030 #include <cstdlib>
00031 #include <netinet/in.h>
00032 #include <cstring>
00033 
00034 namespace firevision {
00035 #if 0 /* just to make Emacs auto-indent happy */
00036 }
00037 #endif
00038 
00039 /** @class FuseLutContent <fvutils/net/fuse_lut_content.h>
00040  * FUSE lookup table content.
00041  * @ingroup FUSE
00042  * @ingroup FireVision
00043  * @author Tim Niemueller
00044  */
00045 
00046 /** Constructor.
00047  * @param type content type, must be FUSE_MT_LUT
00048  * @param payload payload
00049  * @param payload_size size of payload
00050  * @exception TypeMismatchException thrown if type does not equal FUSE_MT_LUT
00051  */
00052 FuseLutContent::FuseLutContent(uint32_t type,
00053                                void *payload, size_t payload_size)
00054 {
00055   if ( (type != FUSE_MT_LUT) && (type != FUSE_MT_SET_LUT) ) {
00056     throw fawkes::TypeMismatchException("Type %u != FUSE_MT_LUT/FUSE_MT_SET_LUT (%u/%u)",
00057                                         type, FUSE_MT_LUT, FUSE_MT_SET_LUT);
00058   }
00059 
00060   _payload_size = payload_size;
00061   _payload = payload;
00062   
00063 
00064   __header = (FUSE_lut_message_header_t *)_payload;
00065   __buffer = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
00066 
00067   __lut_id = (char *)malloc(LUT_ID_MAX_LENGTH + 1);
00068   __lut_id[LUT_ID_MAX_LENGTH] = 0;
00069   strncpy(__lut_id, __header->lut_id, LUT_ID_MAX_LENGTH);
00070 
00071   __buffer_size = ntohl(__header->width) * ntohl(__header->height) *
00072                   ntohl(__header->depth) * ntohl(__header->bytes_per_cell);
00073 }
00074 
00075 
00076 /** Constructor.
00077  * @param b lookup table to copy data from
00078  */
00079 FuseLutContent::FuseLutContent(SharedMemoryLookupTable *b)
00080 {
00081   __buffer_size  = b->width() * b->height() * b->depth() * b->bytes_per_cell();
00082   _payload_size = __buffer_size + sizeof(FUSE_lut_message_header_t);
00083 
00084   _payload = malloc(_payload_size);
00085   if ( _payload == NULL ) {
00086     throw fawkes::OutOfMemoryException("Cannot allocate FuseLutContent buffer");
00087   }
00088 
00089   __header = (FUSE_lut_message_header_t *)_payload;
00090   __buffer = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
00091 
00092   strncpy(__header->lut_id, b->lut_id(), LUT_ID_MAX_LENGTH);
00093   __header->width  = htonl(b->width());
00094   __header->height = htonl(b->height());
00095   __header->depth  = htonl(b->depth());
00096   __header->bytes_per_cell = htonl(b->bytes_per_cell());
00097   __lut_id = strdup(b->lut_id());
00098 
00099   // b->lock_for_read(); 
00100   memcpy(__buffer, b->buffer(), __buffer_size);
00101   // b->unlock();
00102 }
00103 
00104 
00105 /** Constructor.
00106  * Create a brand new FuseLutContent from a raw buffer.
00107  * @param lut_id LUT ID
00108  * @param buffer buffer that holds the LUT data
00109  * @param width LUT width
00110  * @param height LUT height
00111  * @param depth LUT depth
00112  * @param bpc LUT bytes per cell
00113  */
00114 FuseLutContent::FuseLutContent(const char *lut_id, void *buffer,
00115                                unsigned int width, unsigned int height,
00116                                unsigned int depth, unsigned int bpc)
00117 {
00118   __buffer_size  = width * height * depth * bpc;
00119   _payload_size = __buffer_size + sizeof(FUSE_lut_message_header_t);
00120 
00121   _payload = malloc(_payload_size);
00122   if ( _payload == NULL ) {
00123     throw fawkes::OutOfMemoryException("Cannot allocate FuseLutContent buffer");
00124   }
00125 
00126   __header = (FUSE_lut_message_header_t *)_payload;
00127   __buffer = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
00128 
00129   strncpy(__header->lut_id, lut_id, LUT_ID_MAX_LENGTH);
00130   __header->width  = htonl(width);
00131   __header->height = htonl(height);
00132   __header->depth  = htonl(depth);
00133   __header->bytes_per_cell = htonl(bpc);
00134   __lut_id = strdup(lut_id);
00135 
00136   memcpy(__buffer, buffer, __buffer_size);
00137 }
00138 
00139 
00140 FuseLutContent::~FuseLutContent()
00141 {
00142   free(__lut_id);
00143 }
00144 
00145 
00146 /** Get LUT ID.
00147  * @return LUT ID
00148  */
00149 const char *
00150 FuseLutContent::lut_id() const
00151 {
00152   return __lut_id;
00153 }
00154 
00155 /** Get buffer.
00156  * @return buffer
00157  */
00158 unsigned char *
00159 FuseLutContent::buffer() const
00160 {
00161   return __buffer;
00162 }
00163 
00164 
00165 /** Get buffer size.
00166  * @return size of buffer returned by buffer()
00167  */
00168 size_t
00169 FuseLutContent::buffer_size() const
00170 {
00171   return __buffer_size;
00172 }
00173 
00174 
00175 /** Width of LUT.
00176  * @return width of LUT
00177  */
00178 unsigned int
00179 FuseLutContent::width() const
00180 {
00181   return ntohl(__header->width);
00182 }
00183 
00184 
00185 /** Height of LUT.
00186  * @return height of LUT
00187  */
00188 unsigned int
00189 FuseLutContent::height() const
00190 {
00191   return ntohl(__header->height);
00192 }
00193 
00194 /** Depth of LUT.
00195  * @return depth of LUT
00196  */
00197 unsigned int
00198 FuseLutContent::depth() const
00199 {
00200   return ntohl(__header->depth);
00201 }
00202 
00203 
00204 /** Bytes per cell in LUT.
00205  * @return Bytes per cell in LUT
00206  */
00207 unsigned int
00208 FuseLutContent::bytes_per_cell() const
00209 {
00210   return ntohl(__header->bytes_per_cell);
00211 }
00212 
00213 
00214 void
00215 FuseLutContent::serialize()
00216 {
00217   // Nothing to do here
00218 }
00219 
00220 } // end namespace firevision

Generated on Tue Feb 22 13:32:16 2011 for Fawkes API by  doxygen 1.4.7