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