Fawkes API Fawkes Development Version
fuse_image_content.cpp
1
2/***************************************************************************
3 * fuse_image_content.cpp - FUSE image content encapsulation
4 *
5 * Created: Thu Nov 15 15:55:51 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/color/conversions.h>
27#include <fvutils/compression/jpeg_decompressor.h>
28#include <fvutils/ipc/shm_image.h>
29#include <fvutils/net/fuse_image_content.h>
30#include <netinet/in.h>
31
32#include <cstdlib>
33#include <cstring>
34
35namespace firevision {
36
37/** @class FuseImageContent <fvutils/net/fuse_image_content.h>
38 * FUSE image content.
39 * @ingroup FUSE
40 * @ingroup FireVision
41 * @author Tim Niemueller
42 */
43
44/** Constructor.
45 * @param type message type
46 * @param payload payload
47 * @param payload_size size of payload
48 */
49FuseImageContent::FuseImageContent(uint32_t type, void *payload, size_t payload_size)
50{
51 if (type != FUSE_MT_IMAGE) {
52 throw fawkes::TypeMismatchException("Type %u != FUSE_MT_IMAGE (%u)", type, FUSE_MT_IMAGE);
53 }
54
57
59 buffer_ = (unsigned char *)_payload + sizeof(FUSE_image_message_header_t);
60 capture_time_ =
61 new fawkes::Time(ntohl(header_->capture_time_sec), ntohl(header_->capture_time_usec));
62
63 buffer_size_ = ntohl(header_->buffer_size);
64}
65
66/** Constructor.
67 * Copies data from given buffer.
68 * @param b shared memory image buffer to copy image from
69 */
71{
72 buffer_size_ = colorspace_buffer_size(b->colorspace(), b->width(), b->height());
73 _payload_size = buffer_size_ + sizeof(FUSE_image_message_header_t);
74 _payload = malloc(_payload_size);
75
76 if (_payload == NULL) {
77 throw fawkes::OutOfMemoryException("Cannot allocate FuseImageContent buffer");
78 }
79
81 buffer_ = (unsigned char *)_payload + sizeof(FUSE_image_message_header_t);
82
83 strncpy(header_->image_id, b->image_id(), IMAGE_ID_MAX_LENGTH - 1);
84 header_->format = FUSE_IF_RAW;
85 header_->colorspace = htons(b->colorspace());
86 header_->reserved = 0;
87 header_->width = htonl(b->width());
88 header_->height = htonl(b->height());
89 header_->buffer_size = htonl(buffer_size_);
90
91 long int cts = 0, ctus = 0;
92 b->capture_time(&cts, &ctus);
93 header_->capture_time_sec = htonl(cts);
94 header_->capture_time_usec = htonl(ctus);
95
96 capture_time_ = NULL;
97
98 b->lock_for_read();
99 memcpy(buffer_, b->buffer(), buffer_size_);
100 b->unlock();
101}
102
103/** Constructor.
104 * Copies data from given buffer.
105 * @param image_format image format
106 * @param image_id image ID
107 * @param buffer image buffer, encoded according to image_format
108 * @param buffer_size size of buffer in bytes
109 * @param colorspace color space
110 * @param width width of image in pixels
111 * @param height height of image in pixels
112 * @param capture_time_sec optional seconds part of the capture time
113 * @param capture_time_usec optional microseconds part of the capture time
114 */
115FuseImageContent::FuseImageContent(FUSE_image_format_t image_format,
116 const char * image_id,
117 unsigned char * buffer,
118 size_t buffer_size,
119 colorspace_t colorspace,
120 unsigned int width,
121 unsigned int height,
122 long int capture_time_sec,
123 long int capture_time_usec)
124{
125 buffer_size_ = buffer_size;
126 _payload_size = buffer_size_ + sizeof(FUSE_image_message_header_t);
127 _payload = malloc(_payload_size);
128
129 if (_payload == NULL) {
130 throw fawkes::OutOfMemoryException("Cannot allocate FuseImageContent buffer");
131 }
132
134 buffer_ = (unsigned char *)_payload + sizeof(FUSE_image_message_header_t);
135
136 strncpy(header_->image_id, image_id, IMAGE_ID_MAX_LENGTH - 1);
137 header_->format = image_format;
138 header_->colorspace = htons(colorspace);
139 header_->reserved = 0;
140 header_->width = htonl(width);
141 header_->height = htonl(height);
142 header_->buffer_size = htonl(buffer_size_);
143 header_->capture_time_sec = htonl(capture_time_sec);
144 header_->capture_time_usec = htonl(capture_time_usec);
145
146 capture_time_ = NULL;
147
148 memcpy(buffer_, buffer, buffer_size_);
149}
150
151/** Destructor. */
153{
154 delete capture_time_;
155}
156
157/** Image buffer.
158 * @return image buffer
159 */
160unsigned char *
162{
163 return buffer_;
164}
165
166/** Get size of buffer.
167 * @return size of buffer returned by buffer()
168 */
169size_t
171{
172 return buffer_size_;
173}
174
175/** Get image width.
176 * @return width of image in pixels
177 */
178unsigned int
180{
181 return ntohl(header_->width);
182}
183
184/** Get image height.
185 * @return height of image in pixels
186 */
187unsigned int
189{
190 return ntohl(header_->height);
191}
192
193/** Get colorspace.
194 * @return colorspace
195 */
196unsigned int
198{
199 return ntohs(header_->colorspace);
200}
201
202/** Get image format.
203 * @return format
204 */
205unsigned int
207{
208 return header_->format;
209}
210
211/** Get capture time.
212 * @return capture time
213 */
216{
217 if (!capture_time_) {
218 capture_time_ =
219 new fawkes::Time(ntohl(header_->capture_time_sec), ntohl(header_->capture_time_usec));
220 }
221 return capture_time_;
222}
223
224void
226{
227 // Nothing to do here
228}
229
230/** Decompress image data.
231 * This is a utility method which can be used on clients to decompress compressed
232 * image payload. Since every time a new decompressor is created and deleted
233 * this method can be slower compared to decompressing the data directly in your
234 * application so use with care.
235 * @param yuv422_planar_buffer an already allocated buffer where the decompressed image
236 * will be stored.
237 * @param buffer_size size of yuv422_planar_buffer in bytes. Must be big enough to store
238 * a YUV422_PLANAR image of the image dimensions of the compressed data.
239 */
240void
241FuseImageContent::decompress(unsigned char *yuv422_planar_buffer, size_t buffer_size)
242{
243 if (buffer_size
244 < colorspace_buffer_size(YUV422_PLANAR, ntohs(header_->width), ntohs(header_->height))) {
245 throw fawkes::IllegalArgumentException("Supplied buffer is too small\n");
246 }
247 if (header_->format != FUSE_IF_JPEG) {
248 JpegImageDecompressor *decompressor = new JpegImageDecompressor();
249 decompressor->set_compressed_buffer(buffer_, buffer_size_);
250 decompressor->set_decompressed_buffer(yuv422_planar_buffer, buffer_size);
251 decompressor->decompress();
252 delete decompressor;
253 } else {
254 convert((colorspace_t)ntohs(header_->colorspace),
255 YUV422_PLANAR,
256 buffer_,
257 yuv422_planar_buffer,
258 ntohs(header_->width),
259 ntohs(header_->height));
260 }
261}
262
263} // end namespace firevision
Expected parameter is missing.
Definition: software.h:80
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
void unlock()
Unlock memory.
Definition: shm.cpp:1025
void lock_for_read()
Lock shared memory segment for reading.
Definition: shm.cpp:909
A class for handling time.
Definition: time.h:93
unsigned int format() const
Get image format.
virtual void serialize()
Serialize message content.
size_t buffer_size() const
Get size of buffer.
void decompress(unsigned char *yuv422_planar_buffer, size_t buffer_size)
Decompress image data.
unsigned int colorspace() const
Get colorspace.
fawkes::Time * capture_time() const
Get capture time.
unsigned char * buffer() const
Image buffer.
unsigned int pixel_height() const
Get image height.
unsigned int pixel_width() const
Get image width.
FuseImageContent(SharedMemoryImageBuffer *b)
Constructor.
virtual void * payload() const
Return pointer to payload.
virtual size_t payload_size() const
Return payload size.
void * _payload
Pointer to payload.
virtual void set_decompressed_buffer(unsigned char *buf, unsigned int buf_size)
Set decompressed buffer.
virtual void set_compressed_buffer(unsigned char *buf, unsigned int buf_size)
Set compressed buffer.
Decompressor for JPEG images.
virtual void decompress()
Decompress image.
Shared memory image buffer.
Definition: shm_image.h:184
unsigned int height() const
Get image height.
Definition: shm_image.cpp:255
fawkes::Time capture_time() const
Get the time when the image was captured.
Definition: shm_image.cpp:189
colorspace_t colorspace() const
Get color space.
Definition: shm_image.cpp:237
const char * image_id() const
Get Image ID.
Definition: shm_image.cpp:160
unsigned int width() const
Get image width.
Definition: shm_image.cpp:246
unsigned char * buffer() const
Get image buffer.
Definition: shm_image.cpp:228
int64_t capture_time_sec
capture time seconds part
Definition: fuse.h:128
uint32_t format
Image format.
Definition: fuse.h:122
uint32_t buffer_size
size of following image buffer in bytes
Definition: fuse.h:127
int64_t capture_time_usec
capture time microseconds part
Definition: fuse.h:129
uint32_t height
height in pixels
Definition: fuse.h:126
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
Definition: fuse.h:121
uint32_t reserved
reserved for future use
Definition: fuse.h:124
uint32_t width
width in pixels
Definition: fuse.h:125
uint32_t colorspace
color space
Definition: fuse.h:123