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