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 <core/exceptions/software.h>
25 #include <core/exceptions/system.h>
26 #include <fvutils/ipc/shm_lut.h>
27 #include <fvutils/net/fuse_lut_content.h>
28 #include <netinet/in.h>
29 
30 #include <cstdlib>
31 #include <cstring>
32 
33 namespace firevision {
34 
35 /** @class FuseLutContent <fvutils/net/fuse_lut_content.h>
36  * FUSE lookup table content.
37  * @ingroup FUSE
38  * @ingroup FireVision
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * @param type content type, must be FUSE_MT_LUT
44  * @param payload payload
45  * @param payload_size size of payload
46  * @exception TypeMismatchException thrown if type does not equal FUSE_MT_LUT
47  */
48 FuseLutContent::FuseLutContent(uint32_t type, void *payload, size_t payload_size)
49 {
50  if ((type != FUSE_MT_LUT) && (type != FUSE_MT_SET_LUT)) {
51  throw fawkes::TypeMismatchException("Type %u != FUSE_MT_LUT/FUSE_MT_SET_LUT (%u/%u)",
52  type,
53  FUSE_MT_LUT,
54  FUSE_MT_SET_LUT);
55  }
56 
58  _payload = payload;
59 
61  buffer_ = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
62 
63  lut_id_ = (char *)malloc(LUT_ID_MAX_LENGTH + 1);
64  lut_id_[LUT_ID_MAX_LENGTH] = 0;
65  strncpy(lut_id_, header_->lut_id, LUT_ID_MAX_LENGTH);
66 
67  buffer_size_ = (size_t)ntohl(header_->width) * ntohl(header_->height)
68  * (size_t)ntohl(header_->depth) * ntohl(header_->bytes_per_cell);
69 }
70 
71 /** Constructor.
72  * @param b lookup table to copy data from
73  */
74 FuseLutContent::FuseLutContent(SharedMemoryLookupTable *b)
75 {
76  buffer_size_ = (size_t)b->width() * b->height() * b->depth() * b->bytes_per_cell();
77  _payload_size = buffer_size_ + sizeof(FUSE_lut_message_header_t);
78 
79  _payload = malloc(_payload_size);
80  if (_payload == NULL) {
81  throw fawkes::OutOfMemoryException("Cannot allocate FuseLutContent buffer");
82  }
83 
85  buffer_ = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
86 
87  strncpy(header_->lut_id, b->lut_id(), LUT_ID_MAX_LENGTH - 1);
88  header_->width = htonl(b->width());
89  header_->height = htonl(b->height());
90  header_->depth = htonl(b->depth());
91  header_->bytes_per_cell = htonl(b->bytes_per_cell());
92  lut_id_ = strdup(b->lut_id());
93 
94  // b->lock_for_read();
95  memcpy(buffer_, b->buffer(), buffer_size_);
96  // b->unlock();
97 }
98 
99 /** Constructor.
100  * Create a brand new FuseLutContent from a raw buffer.
101  * @param lut_id LUT ID
102  * @param buffer buffer that holds the LUT data
103  * @param width LUT width
104  * @param height LUT height
105  * @param depth LUT depth
106  * @param bpc LUT bytes per cell
107  */
108 FuseLutContent::FuseLutContent(const char * lut_id,
109  void * buffer,
110  unsigned int width,
111  unsigned int height,
112  unsigned int depth,
113  unsigned int bpc)
114 {
115  buffer_size_ = (size_t)width * height * depth * bpc;
116  _payload_size = buffer_size_ + sizeof(FUSE_lut_message_header_t);
117 
118  _payload = malloc(_payload_size);
119  if (_payload == NULL) {
120  throw fawkes::OutOfMemoryException("Cannot allocate FuseLutContent buffer");
121  }
122 
123  header_ = (FUSE_lut_message_header_t *)_payload;
124  buffer_ = (unsigned char *)_payload + sizeof(FUSE_lut_message_header_t);
125 
126  strncpy(header_->lut_id, lut_id, LUT_ID_MAX_LENGTH - 1);
127  header_->width = htonl(width);
128  header_->height = htonl(height);
129  header_->depth = htonl(depth);
130  header_->bytes_per_cell = htonl(bpc);
131  lut_id_ = strdup(lut_id);
132 
133  memcpy(buffer_, buffer, buffer_size_);
134 }
135 
136 FuseLutContent::~FuseLutContent()
137 {
138  free(lut_id_);
139 }
140 
141 /** Get LUT ID.
142  * @return LUT ID
143  */
144 const char *
146 {
147  return lut_id_;
148 }
149 
150 /** Get buffer.
151  * @return buffer
152  */
153 unsigned char *
155 {
156  return buffer_;
157 }
158 
159 /** Get buffer size.
160  * @return size of buffer returned by buffer()
161  */
162 size_t
164 {
165  return buffer_size_;
166 }
167 
168 /** Width of LUT.
169  * @return width of LUT
170  */
171 unsigned int
172 FuseLutContent::width() const
173 {
174  return ntohl(header_->width);
175 }
176 
177 /** Height of LUT.
178  * @return height of LUT
179  */
180 unsigned int
182 {
183  return ntohl(header_->height);
184 }
185 
186 /** Depth of LUT.
187  * @return depth of LUT
188  */
189 unsigned int
190 FuseLutContent::depth() const
191 {
192  return ntohl(header_->depth);
193 }
194 
195 /** Bytes per cell in LUT.
196  * @return Bytes per cell in LUT
197  */
198 unsigned int
200 {
201  return ntohl(header_->bytes_per_cell);
202 }
203 
204 void
206 {
207  // Nothing to do here
208 }
209 
210 } // end namespace firevision
firevision::FUSE_lut_message_header_t::depth
uint32_t depth
depth of LUT
Definition: fuse.h:115
firevision::FuseLutContent::bytes_per_cell
unsigned int bytes_per_cell() const
Bytes per cell in LUT.
Definition: fuse_lut_content.cpp:203
firevision::FuseLutContent::height
unsigned int height() const
Height of LUT.
Definition: fuse_lut_content.cpp:185
firevision::FUSE_lut_message_header_t::width
uint32_t width
width of LUT
Definition: fuse.h:113
firevision::FuseLutContent::serialize
virtual void serialize()
Definition: fuse_lut_content.cpp:209
firevision::FuseMessageContent::payload_size
virtual size_t payload_size() const
Return payload size.
Definition: fuse_message_content.cpp:90
firevision::FUSE_lut_message_header_t::height
uint32_t height
height of LUT
Definition: fuse.h:114
firevision::FuseMessageContent::_payload
void * _payload
Pointer to payload.
Definition: fuse_message_content.h:57
fawkes::TypeMismatchException
Definition: software.h:47
firevision::FUSE_lut_message_header_t
Lookup table packet header.
Definition: fuse.h:110
firevision::FuseLutContent::depth
unsigned int depth() const
Depth of LUT.
Definition: fuse_lut_content.cpp:194
firevision::FUSE_lut_message_header_t::bytes_per_cell
uint32_t bytes_per_cell
bytes per cell
Definition: fuse.h:116
firevision::FuseMessageContent::payload
virtual void * payload() const
Return pointer to payload.
Definition: fuse_message_content.cpp:76
firevision::FUSE_lut_message_header_t::lut_id
char lut_id[LUT_ID_MAX_LENGTH]
LUT ID.
Definition: fuse.h:112
firevision::FuseLutContent::width
unsigned int width() const
Width of LUT.
Definition: fuse_lut_content.cpp:176
firevision::FuseLutContent::buffer_size
size_t buffer_size() const
Get buffer size.
Definition: fuse_lut_content.cpp:167
firevision::FuseMessageContent::_payload_size
size_t _payload_size
Payloda size.
Definition: fuse_message_content.h:59
firevision::FuseLutContent::FuseLutContent
FuseLutContent(const char *lut_id, void *buffer, unsigned int width, unsigned int height, unsigned int depth, unsigned int bpc)
Constructor.
Definition: fuse_lut_content.cpp:112
firevision::FuseLutContent::lut_id
const char * lut_id() const
Get LUT ID.
Definition: fuse_lut_content.cpp:149
fawkes::OutOfMemoryException
Definition: system.h:35
firevision::FuseLutContent::buffer
unsigned char * buffer() const
Get buffer.
Definition: fuse_lut_content.cpp:158