|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024-2024 Istituto Italiano di Tecnologia (IIT) |
| 3 | + * SPDX-License-Identifier: BSD-3-Clause |
| 4 | + */ |
| 5 | + |
| 6 | +#include <yarp/os/NetType.h> |
| 7 | +#include <yarp/os/impl/BufferedConnectionWriter.h> |
| 8 | +#include <yarp/sig/LayeredImage.h> |
| 9 | +#include <yarp/os/Network.h> |
| 10 | +#include <yarp/os/PortReaderBuffer.h> |
| 11 | +#include <yarp/os/Port.h> |
| 12 | +#include <yarp/os/Bottle.h> |
| 13 | +#include <yarp/os/Time.h> |
| 14 | +#include <yarp/os/Log.h> |
| 15 | +#include <yarp/os/PeriodicThread.h> |
| 16 | + |
| 17 | +#include <catch2/catch_amalgamated.hpp> |
| 18 | +#include <harness.h> |
| 19 | + |
| 20 | +using namespace yarp::os::impl; |
| 21 | +using namespace yarp::sig; |
| 22 | +using namespace yarp::os; |
| 23 | + |
| 24 | +TEST_CASE("sig::LayeredImageTest", "[yarp::sig]") |
| 25 | +{ |
| 26 | + NetworkBase::setLocalMode(true); |
| 27 | + |
| 28 | + SECTION("test serialization of layered images composed by a background + 2 layers") |
| 29 | + { |
| 30 | + FlexImage imageback; |
| 31 | + imageback.setPixelCode(VOCAB_PIXEL_RGB); |
| 32 | + imageback.resize(4, 8); |
| 33 | + |
| 34 | + FlexImage lay0; |
| 35 | + lay0.setPixelCode(VOCAB_PIXEL_RGB); |
| 36 | + lay0.resize(4, 8); |
| 37 | + |
| 38 | + FlexImage lay1; |
| 39 | + lay1.setPixelCode(VOCAB_PIXEL_RGB); |
| 40 | + lay1.resize(4, 8); |
| 41 | + |
| 42 | + LayeredImage multiLayerImageIn; |
| 43 | + multiLayerImageIn.background = imageback; |
| 44 | + multiLayerImageIn.layers.push_back(lay0); |
| 45 | + multiLayerImageIn.layers.push_back(lay1); |
| 46 | + |
| 47 | + LayeredImage multiLayerImageOut; |
| 48 | + |
| 49 | + yarp::os::Bottle output_bot; |
| 50 | + bool b1 = Property::copyPortable(multiLayerImageIn, output_bot); |
| 51 | + CHECK(b1); |
| 52 | + |
| 53 | + yarp::os::Bottle input_bot = output_bot; |
| 54 | + size_t i1 = output_bot.size(); |
| 55 | + size_t i2 = input_bot.size(); |
| 56 | + std::string s1 = output_bot.toString(); |
| 57 | + std::string s2 = input_bot.toString(); |
| 58 | + CHECK((int)i1 == 23); |
| 59 | + CHECK((int)i2 == 23); |
| 60 | + std::cout << "s1: " << s1 << std::endl; |
| 61 | + std::cout << "s2: " << s2 << std::endl; |
| 62 | + bool b2 = Property::copyPortable(input_bot, multiLayerImageOut); |
| 63 | + CHECK(b2); |
| 64 | + |
| 65 | + bool b3 = (multiLayerImageIn == multiLayerImageOut); |
| 66 | + CHECK(b3); |
| 67 | + } |
| 68 | + |
| 69 | + SECTION("test serialization of layered images composed by a background image only") |
| 70 | + { |
| 71 | + FlexImage imageback; |
| 72 | + imageback.setPixelCode(VOCAB_PIXEL_RGB); |
| 73 | + imageback.resize(16, 8); |
| 74 | + |
| 75 | + LayeredImage multiLayerImageIn; |
| 76 | + multiLayerImageIn.background = imageback; |
| 77 | + |
| 78 | + LayeredImage multiLayerImageOut; |
| 79 | + |
| 80 | + yarp::os::Bottle output_bot; |
| 81 | + bool b1 = Property::copyPortable(multiLayerImageIn, output_bot); |
| 82 | + CHECK(b1); |
| 83 | + |
| 84 | + yarp::os::Bottle input_bot = output_bot; |
| 85 | + size_t i1 = output_bot.size(); |
| 86 | + size_t i2 = input_bot.size(); |
| 87 | + std::string s1 = output_bot.toString(); |
| 88 | + std::string s2 = input_bot.toString(); |
| 89 | + CHECK((int)i1 == 3); |
| 90 | + CHECK((int)i2 == 3); |
| 91 | + bool b2 = Property::copyPortable(input_bot, multiLayerImageOut); |
| 92 | + CHECK(b2); |
| 93 | + |
| 94 | + bool b3 = (multiLayerImageIn == multiLayerImageOut); |
| 95 | + CHECK(b3); |
| 96 | + } |
| 97 | + |
| 98 | + SECTION("test alpha layered image") |
| 99 | + { |
| 100 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> imageback; |
| 101 | + imageback.resize(16, 8); |
| 102 | + for (size_t iy = 0; iy < imageback.height(); iy++) |
| 103 | + for (size_t ix = 0; ix < imageback.width(); ix++) { |
| 104 | + imageback.pixel(ix, iy).r = 10; |
| 105 | + imageback.pixel(ix, iy).g = 10; |
| 106 | + imageback.pixel(ix, iy).b = 10; |
| 107 | + } |
| 108 | + |
| 109 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> lay0; |
| 110 | + lay0.resize(16, 8); |
| 111 | + for (size_t iy = 0; iy < lay0.height(); iy++) |
| 112 | + for (size_t ix = 0; ix < lay0.width(); ix++) { |
| 113 | + lay0.pixel(ix, iy).r = 8; |
| 114 | + lay0.pixel(ix, iy).g = 10; |
| 115 | + lay0.pixel(ix, iy).b = 12; |
| 116 | + } |
| 117 | + |
| 118 | + LayeredImage multiLayerImageIn; |
| 119 | + multiLayerImageIn.background = *(reinterpret_cast<yarp::sig::FlexImage*>(&imageback)); |
| 120 | + multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay0))); |
| 121 | + multiLayerImageIn.layers[0].alpha.value = 0.5; |
| 122 | + |
| 123 | + FlexImage flat_img = multiLayerImageIn; |
| 124 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> flat_rgb_img = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&flat_img)); |
| 125 | + for (size_t iy = 0; iy < flat_rgb_img.height() / 2; iy++) |
| 126 | + for (size_t ix = 0; ix < flat_rgb_img.width() / 2; ix++) { |
| 127 | + CHECK(flat_rgb_img.pixel(ix, iy).r == 9); |
| 128 | + CHECK(flat_rgb_img.pixel(ix, iy).g == 10); |
| 129 | + CHECK(flat_rgb_img.pixel(ix, iy).b == 11); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + SECTION("test offset layered image") |
| 134 | + { |
| 135 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> imageback; |
| 136 | + imageback.resize(4, 4); |
| 137 | + for (size_t iy = 0; iy < imageback.height(); iy++) |
| 138 | + for (size_t ix = 0; ix < imageback.width(); ix++) { |
| 139 | + imageback.pixel(ix, iy).r = 10; |
| 140 | + imageback.pixel(ix, iy).g = 10; |
| 141 | + imageback.pixel(ix, iy).b = 10; |
| 142 | + } |
| 143 | + |
| 144 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> lay0; |
| 145 | + lay0.resize(2, 2); |
| 146 | + for (size_t iy = 0; iy < lay0.height(); iy++) |
| 147 | + for (size_t ix = 0; ix < lay0.width(); ix++) { |
| 148 | + lay0.pixel(ix, iy).r = 20; |
| 149 | + lay0.pixel(ix, iy).g = 20; |
| 150 | + lay0.pixel(ix, iy).b = 20; |
| 151 | + } |
| 152 | + |
| 153 | + LayeredImage multiLayerImageIn; |
| 154 | + multiLayerImageIn.background = *(reinterpret_cast<yarp::sig::FlexImage*>(&imageback)); |
| 155 | + multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay0))); |
| 156 | + multiLayerImageIn.layers[0].offset_x = 2; |
| 157 | + multiLayerImageIn.layers[0].offset_y = 1; |
| 158 | + |
| 159 | + FlexImage flat_img = multiLayerImageIn; |
| 160 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> flat_rgb_img = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&flat_img)); |
| 161 | + CHECK(flat_rgb_img.pixel(0, 0).r == 10); |
| 162 | + CHECK(flat_rgb_img.pixel(1, 0).r == 10); |
| 163 | + CHECK(flat_rgb_img.pixel(2, 0).r == 10); |
| 164 | + CHECK(flat_rgb_img.pixel(3, 0).r == 10); |
| 165 | + CHECK(flat_rgb_img.pixel(0, 1).r == 10); |
| 166 | + CHECK(flat_rgb_img.pixel(1, 1).r == 10); |
| 167 | + CHECK(flat_rgb_img.pixel(2, 1).r == 20); |
| 168 | + CHECK(flat_rgb_img.pixel(3, 1).r == 20); |
| 169 | + CHECK(flat_rgb_img.pixel(0, 2).r == 10); |
| 170 | + CHECK(flat_rgb_img.pixel(1, 2).r == 10); |
| 171 | + CHECK(flat_rgb_img.pixel(2, 2).r == 20); |
| 172 | + CHECK(flat_rgb_img.pixel(3, 2).r == 20); |
| 173 | + CHECK(flat_rgb_img.pixel(0, 3).r == 10); |
| 174 | + CHECK(flat_rgb_img.pixel(1, 3).r == 10); |
| 175 | + CHECK(flat_rgb_img.pixel(2, 3).r == 10); |
| 176 | + CHECK(flat_rgb_img.pixel(3, 3).r == 10); |
| 177 | + } |
| 178 | + |
| 179 | + SECTION("test colorkey layered image") |
| 180 | + { |
| 181 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> imageback; |
| 182 | + imageback.resize(4, 4); |
| 183 | + for (size_t iy = 0; iy < imageback.height(); iy++) |
| 184 | + for (size_t ix = 0; ix < imageback.width(); ix++) { |
| 185 | + imageback.pixel(ix, iy).r = 10; |
| 186 | + imageback.pixel(ix, iy).g = 10; |
| 187 | + imageback.pixel(ix, iy).b = 10; |
| 188 | + } |
| 189 | + |
| 190 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> lay0; |
| 191 | + lay0.resize(4, 4); |
| 192 | + lay0.zero(); |
| 193 | + imageback.pixel(0, 0).r = 20; |
| 194 | + imageback.pixel(1, 0).r = 20; |
| 195 | + imageback.pixel(2, 0).r = 20; |
| 196 | + imageback.pixel(3, 0).r = 20; |
| 197 | + imageback.pixel(0, 1).r = 20; |
| 198 | + imageback.pixel(1, 1).r = 50; |
| 199 | + imageback.pixel(2, 1).r = 50; |
| 200 | + imageback.pixel(3, 1).r = 20; |
| 201 | + imageback.pixel(0, 2).r = 20; |
| 202 | + imageback.pixel(1, 2).r = 50; |
| 203 | + imageback.pixel(2, 2).r = 50; |
| 204 | + imageback.pixel(3, 2).r = 20; |
| 205 | + imageback.pixel(0, 3).r = 20; |
| 206 | + imageback.pixel(1, 3).r = 50; |
| 207 | + imageback.pixel(2, 3).r = 20; |
| 208 | + imageback.pixel(3, 3).r = 20; |
| 209 | + |
| 210 | + LayeredImage multiLayerImageIn; |
| 211 | + multiLayerImageIn.background = *(reinterpret_cast<yarp::sig::FlexImage*>(&imageback)); |
| 212 | + multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay0))); |
| 213 | + |
| 214 | + FlexImage flat_img = multiLayerImageIn; |
| 215 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> flat_rgb_img = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&flat_img)); |
| 216 | + |
| 217 | + CHECK(flat_rgb_img.pixel(0, 0).r == 10); |
| 218 | + CHECK(flat_rgb_img.pixel(1, 0).r == 10); |
| 219 | + CHECK(flat_rgb_img.pixel(2, 0).r == 10); |
| 220 | + CHECK(flat_rgb_img.pixel(3, 0).r == 10); |
| 221 | + CHECK(flat_rgb_img.pixel(0, 1).r == 10); |
| 222 | + CHECK(flat_rgb_img.pixel(1, 1).r == 50); |
| 223 | + CHECK(flat_rgb_img.pixel(2, 1).r == 50); |
| 224 | + CHECK(flat_rgb_img.pixel(3, 1).r == 10); |
| 225 | + CHECK(flat_rgb_img.pixel(0, 2).r == 10); |
| 226 | + CHECK(flat_rgb_img.pixel(1, 2).r == 50); |
| 227 | + CHECK(flat_rgb_img.pixel(2, 2).r == 50); |
| 228 | + CHECK(flat_rgb_img.pixel(3, 2).r == 10); |
| 229 | + CHECK(flat_rgb_img.pixel(0, 3).r == 10); |
| 230 | + CHECK(flat_rgb_img.pixel(1, 3).r == 50); |
| 231 | + CHECK(flat_rgb_img.pixel(2, 3).r == 10); |
| 232 | + CHECK(flat_rgb_img.pixel(3, 3).r == 10); |
| 233 | + } |
| 234 | + |
| 235 | + SECTION("test flattening multi layered image") |
| 236 | + { |
| 237 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> imageback; |
| 238 | + imageback.resize(8, 8); |
| 239 | + for (size_t iy = 0; iy < imageback.height(); iy++) |
| 240 | + for (size_t ix = 0; ix < imageback.width(); ix++) |
| 241 | + { |
| 242 | + imageback.pixel(ix, iy).r = 10; |
| 243 | + imageback.pixel(ix, iy).g = 10; |
| 244 | + imageback.pixel(ix, iy).b = 10; |
| 245 | + } |
| 246 | + |
| 247 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> lay0; |
| 248 | + lay0.resize(8, 8); |
| 249 | + for (size_t iy = 0; iy < lay0.height(); iy++) |
| 250 | + for (size_t ix = 0; ix < lay0.width(); ix++) { |
| 251 | + lay0.pixel(ix, iy).r = 100; |
| 252 | + lay0.pixel(ix, iy).g = 100; |
| 253 | + lay0.pixel(ix, iy).b = 100; |
| 254 | + } |
| 255 | + |
| 256 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> lay1; |
| 257 | + lay1.resize(8, 8); |
| 258 | + for (size_t iy = 0; iy < lay1.height(); iy++) |
| 259 | + for (size_t ix = 0; ix < lay1.width(); ix++) { |
| 260 | + lay1.pixel(ix, iy).r = 101; |
| 261 | + lay1.pixel(ix, iy).g = 101; |
| 262 | + lay1.pixel(ix, iy).b = 101; |
| 263 | + } |
| 264 | + |
| 265 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> lay2; |
| 266 | + lay2.resize(8, 8); |
| 267 | + for (size_t iy = 0; iy < lay2.height(); iy++) |
| 268 | + for (size_t ix = 0; ix < lay2.width(); ix++) { |
| 269 | + lay2.pixel(ix, iy).r = 102; |
| 270 | + lay2.pixel(ix, iy).g = 102; |
| 271 | + lay2.pixel(ix, iy).b = 102; |
| 272 | + } |
| 273 | + |
| 274 | + LayeredImage multiLayerImageIn; |
| 275 | + multiLayerImageIn.background = *(reinterpret_cast<yarp::sig::FlexImage*>(&imageback)); |
| 276 | + multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay0))); |
| 277 | + multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay1))); |
| 278 | + multiLayerImageIn.layers[1].enable = false; |
| 279 | + |
| 280 | + FlexImage flat_img = multiLayerImageIn; |
| 281 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> flat_rgb_img = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&flat_img)); |
| 282 | + for (size_t iy = 0; iy < flat_img.height() ; iy++) |
| 283 | + for (size_t ix = 0; ix < flat_img.width() ; ix++) |
| 284 | + { |
| 285 | + CHECK(flat_rgb_img.pixel(ix, iy).r == 100); |
| 286 | + } |
| 287 | + |
| 288 | + multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay2))); |
| 289 | + FlexImage flat_img2 = multiLayerImageIn; |
| 290 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> flat_rgb_img2 = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&flat_img2)); |
| 291 | + for (size_t iy = 0; iy < flat_img2.height() ; iy++) |
| 292 | + for (size_t ix = 0; ix < flat_img2.width() ; ix++) { |
| 293 | + CHECK(flat_rgb_img2.pixel(ix, iy).r == 102); |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + NetworkBase::setLocalMode(false); |
| 298 | +} |
0 commit comments