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
33namespace 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 */
48FuseLutContent::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
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 */
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 */
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
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
136FuseLutContent::~FuseLutContent()
137{
138 free(lut_id_);
139}
140
141/** Get LUT ID.
142 * @return LUT ID
143 */
144const char *
146{
147 return lut_id_;
148}
149
150/** Get buffer.
151 * @return buffer
152 */
153unsigned char *
155{
156 return buffer_;
157}
158
159/** Get buffer size.
160 * @return size of buffer returned by buffer()
161 */
162size_t
164{
165 return buffer_size_;
166}
167
168/** Width of LUT.
169 * @return width of LUT
170 */
171unsigned int
173{
174 return ntohl(header_->width);
175}
176
177/** Height of LUT.
178 * @return height of LUT
179 */
180unsigned int
182{
183 return ntohl(header_->height);
184}
185
186/** Depth of LUT.
187 * @return depth of LUT
188 */
189unsigned int
191{
192 return ntohl(header_->depth);
193}
194
195/** Bytes per cell in LUT.
196 * @return Bytes per cell in LUT
197 */
198unsigned int
200{
201 return ntohl(header_->bytes_per_cell);
202}
203
204void
206{
207 // Nothing to do here
208}
209
210} // end namespace firevision
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
virtual void serialize()
Serialize message content.
unsigned char * buffer() const
Get buffer.
size_t buffer_size() const
Get buffer size.
unsigned int height() const
Height of LUT.
FuseLutContent(const char *lut_id, void *buffer, unsigned int width, unsigned int height, unsigned int depth, unsigned int bpc)
Constructor.
unsigned int bytes_per_cell() const
Bytes per cell in LUT.
unsigned int depth() const
Depth of LUT.
unsigned int width() const
Width of LUT.
const char * lut_id() const
Get LUT ID.
virtual void * payload() const
Return pointer to payload.
virtual size_t payload_size() const
Return payload size.
void * _payload
Pointer to payload.
Shared memory lookup table.
Definition: shm_lut.h:113
unsigned int bytes_per_cell() const
Get bytes per cell.
Definition: shm_lut.cpp:177
unsigned int width() const
Get LUT width.
Definition: shm_lut.cpp:150
unsigned int depth() const
Get LUT depth.
Definition: shm_lut.cpp:168
unsigned char * buffer() const
Get LUT buffer.
Definition: shm_lut.cpp:141
const char * lut_id() const
Get LUT ID.
Definition: shm_lut.cpp:117
unsigned int height() const
Get LUT height.
Definition: shm_lut.cpp:159
Lookup table packet header.
Definition: fuse.h:107
uint32_t width
width of LUT
Definition: fuse.h:109
uint32_t bytes_per_cell
bytes per cell
Definition: fuse.h:112
uint32_t height
height of LUT
Definition: fuse.h:110
char lut_id[LUT_ID_MAX_LENGTH]
LUT ID.
Definition: fuse.h:108
uint32_t depth
depth of LUT
Definition: fuse.h:111