11#include " FlatBufferClient.h"
2+ #include < utils/PixelFormat.h>
23
34// qt
45#include < QTcpSocket>
@@ -15,6 +16,8 @@ FlatBufferClient::FlatBufferClient(QTcpSocket* socket, int timeout, QObject *par
1516 , _timeout(timeout * 1000 )
1617 , _priority()
1718{
19+ _imageResampler.setPixelDecimation (1 );
20+
1821 // timer setup
1922 _timeoutTimer->setSingleShot (true );
2023 _timeoutTimer->setInterval (_timeout);
@@ -25,6 +28,11 @@ FlatBufferClient::FlatBufferClient(QTcpSocket* socket, int timeout, QObject *par
2528 connect (_socket, &QTcpSocket::disconnected, this , &FlatBufferClient::disconnected);
2629}
2730
31+ void FlatBufferClient::setPixelDecimation (int decimator)
32+ {
33+ _imageResampler.setPixelDecimation (decimator);
34+ }
35+
2836void FlatBufferClient::readyRead ()
2937{
3038 _timeoutTimer->start ();
@@ -141,55 +149,71 @@ void FlatBufferClient::handleRegisterCommand(const hyperionnet::Register *regReq
141149
142150void FlatBufferClient::handleImageCommand (const hyperionnet::Image *image)
143151{
152+ Image<ColorRgb> imageRGB;
153+
144154 // extract parameters
145155 int duration = image->duration ();
146-
147156 const void * reqPtr;
148157 if ((reqPtr = image->data_as_RawImage ()) != nullptr )
149158 {
150- const auto * img = static_cast <const hyperionnet::RawImage*>(reqPtr);
151- const auto & imageData = img-> data ();
152- const int width = img-> width () ;
153- const int height = img->height ( );
159+ const auto * img = static_cast <const hyperionnet::RawImage*>(reqPtr);
160+
161+ hyperionnet::RawImageT rawImageNative ;
162+ img->UnPackTo (&rawImageNative );
154163
155- if (width <= 0 || height <= 0 )
164+ const int width = rawImageNative.width ;
165+ const int height = rawImageNative.height ;
166+
167+ if (width <= 0 || height <= 0 || rawImageNative.data .empty ())
156168 {
157- sendErrorReply (" Size of image data does not match with the width and height " );
169+ sendErrorReply (" Invalid width and/or height or no raw image data provided " );
158170 return ;
159171 }
160172
161173 // check consistency of the size of the received data
162- int channelCount = ( int )imageData-> size ()/ (width* height);
163- if (channelCount != 3 && channelCount != 4 )
174+ int bytesPerPixel = rawImageNative. data . size () / (width * height);
175+ if (bytesPerPixel != 3 && bytesPerPixel != 4 )
164176 {
165177 sendErrorReply (" Size of image data does not match with the width and height" );
166178 return ;
167179 }
168180
169- // create ImageRgb
170- Image<ColorRgb> imageRGB (width, height );
171- if (channelCount == 3 )
172- {
173- memmove (imageRGB. memptr (), imageData-> data (), imageData-> size ());
174- }
181+ imageRGB. resize (width, height);
182+ processRawImage (rawImageNative, bytesPerPixel, _imageResampler, imageRGB );
183+ }
184+ else if ((reqPtr = image-> data_as_NV12Image ()) != nullptr )
185+ {
186+ const auto * img = static_cast < const hyperionnet::NV12Image*>(reqPtr);
175187
176- if (channelCount == 4 )
188+ hyperionnet::NV12ImageT nv12ImageNative;
189+ img->UnPackTo (&nv12ImageNative);
190+
191+ const int width = nv12ImageNative.width ;
192+ const int height = nv12ImageNative.height ;
193+
194+ if (width <= 0 || height <= 0 || nv12ImageNative.data_y .empty () || nv12ImageNative.data_uv .empty ())
177195 {
178- for (int source=0 , destination=0 ; source < width * height * static_cast <int >(sizeof (ColorRgb)); source+=sizeof (ColorRgb), destination+=sizeof (ColorRgba))
179- {
180- memmove ((uint8_t *)imageRGB.memptr () + source, imageData->data () + destination, sizeof (ColorRgb));
181- }
196+ sendErrorReply (" Invalid width and/or height or no complete NV12 image data provided" );
197+ return ;
182198 }
183199
184- emit setGlobalInputImage (_priority, imageRGB, duration);
185- emit setBufferImage (" FlatBuffer" , imageRGB);
200+ imageRGB.resize (width, height);
201+ processNV12Image (nv12ImageNative, _imageResampler, imageRGB);
202+
203+ }
204+ else
205+ {
206+ sendErrorReply (" No or unknown image data provided" );
207+ return ;
186208 }
187209
210+ emit setGlobalInputImage (_priority, imageRGB, duration);
211+ emit setBufferImage (" FlatBuffer" , imageRGB);
212+
188213 // send reply
189214 sendSuccessReply ();
190215}
191216
192-
193217void FlatBufferClient::handleClearCommand (const hyperionnet::Clear *clear)
194218{
195219 // extract parameters
@@ -242,3 +266,50 @@ void FlatBufferClient::sendErrorReply(const std::string &error)
242266
243267 _builder.Clear ();
244268}
269+
270+ inline void FlatBufferClient::processRawImage (const hyperionnet::RawImageT& raw_image, int bytesPerPixel, ImageResampler& resampler, Image<ColorRgb>& outputImage) {
271+
272+ int width = raw_image.width ;
273+ int height = raw_image.height ;
274+
275+ int lineLength = width * bytesPerPixel;
276+ PixelFormat pixelFormat = (bytesPerPixel == 4 ) ? PixelFormat::RGB32 : PixelFormat::RGB24;
277+
278+ // Process the image
279+ resampler.processImage (
280+ raw_image.data .data (), // Raw RGB/RGBA buffer
281+ width, // Image width
282+ height, // Image height
283+ lineLength, // Line length
284+ pixelFormat, // Pixel format (RGB24/RGB32)
285+ outputImage // Output image
286+ );
287+ }
288+
289+ inline void FlatBufferClient::processNV12Image (const hyperionnet::NV12ImageT& nv12_image, ImageResampler& resampler, Image<ColorRgb>& outputImage) {
290+ // Combine data_y and data_uv into a single buffer
291+ int width = nv12_image.width ;
292+ int height = nv12_image.height ;
293+
294+ size_t y_size = nv12_image.data_y .size ();
295+ size_t uv_size = nv12_image.data_uv .size ();
296+ std::vector<uint8_t > combined_buffer (y_size + uv_size);
297+
298+ std::memcpy (combined_buffer.data (), nv12_image.data_y .data (), y_size);
299+ std::memcpy (combined_buffer.data () + y_size, nv12_image.data_uv .data (), uv_size);
300+
301+ // Determine line length (stride_y)
302+ int lineLength = nv12_image.stride_y > 0 ? nv12_image.stride_y : width;
303+
304+ PixelFormat pixelFormat = PixelFormat::NV12;
305+
306+ // Process the image
307+ resampler.processImage (
308+ combined_buffer.data (), // Combined NV12 buffer
309+ width, // Image width
310+ height, // Image height
311+ lineLength, // Line length for Y plane
312+ pixelFormat, // Pixel format (NV12)
313+ outputImage // Output image
314+ );
315+ }
0 commit comments