YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
depthimage_to_rgbTest.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <yarp/os/Network.h>
8#include <yarp/os/LogStream.h>
9#include <yarp/os/Time.h>
11#include <yarp/sig/Image.h>
12
14
15#include <array>
16#include <thread>
17#include <cmath>
18
19#include <catch2/catch_amalgamated.hpp>
20#include <harness.h>
21
22TEST_CASE("pm::depthimage_to_rgbTest", "[yarp::pm]")
23{
24 YARP_REQUIRE_PLUGIN("depthimage_to_rgb", "portmonitor")
25
27
30
31 struct TestCase {
32 std::string carrier;
33 bool should_convert;
34 };
35
36 auto tc = GENERATE(
37 TestCase {"fast_tcp", false},
38 TestCase {"fast_tcp+recv.portmonitor+file.depthimage_to_rgb+type.dll", true}
39 );
40
41 SECTION("Test depth image to RGB heatmap conversion with portmonitor: " + tc.carrier)
42 {
45
46 sender.open("/send");
47 receiver.open("/recv");
48
49 bool b = yarp::os::Network::connect("/send", "/recv", tc.carrier);
50 REQUIRE(b);
51
52 // Create a test depth image (16x16 pixels)
53 constexpr int width = 16;
54 constexpr int height = 16;
55
57 depthImg.resize(width, height);
58
59 // Fill with test data - values between 0.2 and 10.0 meters
60 for (int y = 0; y < height; y++)
61 {
62 for (int x = 0; x < width; x++)
63 {
64 float depth_value = 0.2f + (static_cast<float>(x + y * width) / (width * height - 1)) * 9.8f;
65 depthImg.pixel(x, y) = depth_value;
66 }
67 }
68
69 sender.write();
70
72
73 yarp::sig::FlexImage* receivedImg = receiver.read();
74 REQUIRE(receivedImg != nullptr);
75
76 // Verify dimensions
77 CHECK(receivedImg->width() == width);
78 CHECK(receivedImg->height() == height);
79
80 if (tc.should_convert)
81 {
82 // Verify it's converted to RGB heatmap
83 CHECK(receivedImg->getPixelCode() == VOCAB_PIXEL_RGB);
84 CHECK(receivedImg->getPixelSize() == 3);
85
86 // Verify some pixels have valid RGB values
87 unsigned char* pixels = receivedImg->getRawImage();
88 bool hasNonZeroPixel = false;
89 for (int i = 0; i < width * height * 3; i++)
90 {
91 CHECK(pixels[i] <= 255);
92 if (pixels[i] > 0)
93 {
94 hasNonZeroPixel = true;
95 }
96 }
97 // At least some pixels should have color (not all black)
98 CHECK(hasNonZeroPixel);
99 }
100 else
101 {
102 // Without conversion, should still be float
103 CHECK(receivedImg->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT);
104 }
105
106 receiver.close();
107 sender.close();
108 }
109
111}
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_RGB
Definition Image.h:44
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:751
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1118
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:718
Image class with user control of representation details.
Definition Image.h:361
Typed image class.
Definition Image.h:603
T & pixel(size_t x, size_t y)
Definition Image.h:619
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:462
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition Image.cpp:370
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition Image.cpp:387
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:375
TEST_CASE("pm::depthimage_to_rgbTest", "[yarp::pm]")
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16