Fawkes API  Fawkes Development Version
fuse_lut_content.cpp
1 
2 /***************************************************************************
3  * fuse_lut_content.cpp - FUSE LUT content encapsulation
4  *
5  * Created: Wed Nov 21 16:49:18 2007
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvutils/net/fuse_lut_content.h>
25 #include <fvutils/ipc/shm_lut.h>
26 
27 #include <core/exceptions/system.h>
28 #include <core/exceptions/software.h>
29 
30 #include <cstdlib>
31 #include <netinet/in.h>
32 #include <cstring>
33 
34 namespace firevision {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 /** @class FuseLutContent <fvutils/net/fuse_lut_content.h>
40  * FUSE lookup table content.
41  * @ingroup FUSE
42  * @ingroup FireVision
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor.
47  * @param type content type, must be FUSE_MT_LUT
48  * @param payload payload
49  * @param payload_size size of payload
50  * @exception TypeMismatchException thrown if type does not equal FUSE_MT_LUT
51  */
53  void *payload, size_t payload_size)
54 {
55  if ( (type != FUSE_MT_LUT) && (type != FUSE_MT_SET_LUT) ) {
56  throw fawkes::TypeMismatchException("Type %u != FUSE_MT_LUT/FUSE_MT_SET_LUT (%u/%u)",
57  type, FUSE_MT_LUT, FUSE_MT_SET_LUT);
58  }
59 
61  _payload = payload;
62 
63 
64  __header = (FUSE_lut_message_header_t *)_payload;
65  __buffer = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
66 
67  __lut_id = (char *)malloc(LUT_ID_MAX_LENGTH + 1);
68  __lut_id[LUT_ID_MAX_LENGTH] = 0;
69  strncpy(__lut_id, __header->lut_id, LUT_ID_MAX_LENGTH);
70 
71  __buffer_size = ntohl(__header->width) * ntohl(__header->height) *
72  ntohl(__header->depth) * ntohl(__header->bytes_per_cell);
73 }
74 
75 
76 /** Constructor.
77  * @param b lookup table to copy data from
78  */
80 {
81  __buffer_size = b->width() * b->height() * b->depth() * b->bytes_per_cell();
82  _payload_size = __buffer_size + sizeof(FUSE_lut_message_header_t);
83 
84  _payload = malloc(_payload_size);
85  if ( _payload == NULL ) {
86  throw fawkes::OutOfMemoryException("Cannot allocate FuseLutContent buffer");
87  }
88 
89  __header = (FUSE_lut_message_header_t *)_payload;
90  __buffer = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
91 
92  strncpy(__header->lut_id, b->lut_id(), LUT_ID_MAX_LENGTH);
93  __header->width = htonl(b->width());
94  __header->height = htonl(b->height());
95  __header->depth = htonl(b->depth());
96  __header->bytes_per_cell = htonl(b->bytes_per_cell());
97  __lut_id = strdup(b->lut_id());
98 
99  // b->lock_for_read();
100  memcpy(__buffer, b->buffer(), __buffer_size);
101  // b->unlock();
102 }
103 
104 
105 /** Constructor.
106  * Create a brand new FuseLutContent from a raw buffer.
107  * @param lut_id LUT ID
108  * @param buffer buffer that holds the LUT data
109  * @param width LUT width
110  * @param height LUT height
111  * @param depth LUT depth
112  * @param bpc LUT bytes per cell
113  */
115  unsigned int width, unsigned int height,
116  unsigned int depth, unsigned int bpc)
117 {
118  __buffer_size = width * height * depth * bpc;
119  _payload_size = __buffer_size + sizeof(FUSE_lut_message_header_t);
120 
121  _payload = malloc(_payload_size);
122  if ( _payload == NULL ) {
123  throw fawkes::OutOfMemoryException("Cannot allocate FuseLutContent buffer");
124  }
125 
126  __header = (FUSE_lut_message_header_t *)_payload;
127  __buffer = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
128 
129  strncpy(__header->lut_id, lut_id, LUT_ID_MAX_LENGTH);
130  __header->width = htonl(width);
131  __header->height = htonl(height);
132  __header->depth = htonl(depth);
133  __header->bytes_per_cell = htonl(bpc);
134  __lut_id = strdup(lut_id);
135 
136  memcpy(__buffer, buffer, __buffer_size);
137 }
138 
139 
140 FuseLutContent::~FuseLutContent()
141 {
142  free(__lut_id);
143 }
144 
145 
146 /** Get LUT ID.
147  * @return LUT ID
148  */
149 const char *
151 {
152  return __lut_id;
153 }
154 
155 /** Get buffer.
156  * @return buffer
157  */
158 unsigned char *
160 {
161  return __buffer;
162 }
163 
164 
165 /** Get buffer size.
166  * @return size of buffer returned by buffer()
167  */
168 size_t
170 {
171  return __buffer_size;
172 }
173 
174 
175 /** Width of LUT.
176  * @return width of LUT
177  */
178 unsigned int
180 {
181  return ntohl(__header->width);
182 }
183 
184 
185 /** Height of LUT.
186  * @return height of LUT
187  */
188 unsigned int
190 {
191  return ntohl(__header->height);
192 }
193 
194 /** Depth of LUT.
195  * @return depth of LUT
196  */
197 unsigned int
199 {
200  return ntohl(__header->depth);
201 }
202 
203 
204 /** Bytes per cell in LUT.
205  * @return Bytes per cell in LUT
206  */
207 unsigned int
209 {
210  return ntohl(__header->bytes_per_cell);
211 }
212 
213 
214 void
216 {
217  // Nothing to do here
218 }
219 
220 } // end namespace firevision
uint32_t depth
depth of LUT
Definition: fuse.h:110
unsigned int depth() const
Depth of LUT.
const char * lut_id() const
Get LUT ID.
Definition: shm_lut.cpp:122
char lut_id[LUT_ID_MAX_LENGTH]
LUT ID.
Definition: fuse.h:107
unsigned int width() const
Width of LUT.
size_t buffer_size() const
Get buffer size.
unsigned char * buffer() const
Get buffer.
FuseLutContent(const char *lut_id, void *buffer, unsigned int width, unsigned int height, unsigned int depth, unsigned int bpc)
Constructor.
virtual void * payload() const
Return pointer to payload.
unsigned int width() const
Get LUT width.
Definition: shm_lut.cpp:158
unsigned int bytes_per_cell() const
Get bytes per cell.
Definition: shm_lut.cpp:188
const char * lut_id() const
Get LUT ID.
uint32_t width
width of LUT
Definition: fuse.h:108
unsigned int height() const
Height of LUT.
uint32_t bytes_per_cell
bytes per cell
Definition: fuse.h:111
void * _payload
Pointer to payload.
uint32_t height
height of LUT
Definition: fuse.h:109
unsigned int height() const
Get LUT height.
Definition: shm_lut.cpp:168
unsigned int bytes_per_cell() const
Bytes per cell in LUT.
virtual size_t payload_size() const
Return payload size.
unsigned int depth() const
Get LUT depth.
Definition: shm_lut.cpp:178
Lookup table packet header.
Definition: fuse.h:106
unsigned char * buffer() const
Get LUT buffer.
Definition: shm_lut.cpp:148
virtual void serialize()
Serialize message content.
Shared memory lookup table.
Definition: shm_lut.h:113
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32