From 0493a007327c9f9ebd2ac95477b1ecbb48789235 Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Thu, 2 Jan 2025 11:53:07 +0100 Subject: [PATCH] fix --- src/libYARP_sig/src/yarp/sig/Image.cpp | 8 ++- src/libYARP_sig/src/yarp/sig/ImageUtils.cpp | 4 +- src/libYARP_sig/src/yarp/sig/ImageUtils.h | 6 +-- src/libYARP_sig/src/yarp/sig/LayeredImage.h | 39 +++++++++----- src/libYARP_sig/tests/LayeredImageTest.cpp | 57 +++++++++++++++------ 5 files changed, 76 insertions(+), 38 deletions(-) diff --git a/src/libYARP_sig/src/yarp/sig/Image.cpp b/src/libYARP_sig/src/yarp/sig/Image.cpp index 2c3c26eb413..fdaae2c1ba6 100644 --- a/src/libYARP_sig/src/yarp/sig/Image.cpp +++ b/src/libYARP_sig/src/yarp/sig/Image.cpp @@ -605,9 +605,13 @@ bool Image::operator==(const Image& alt) const //test byte per byte unsigned char* raw1 = getRawImage(); unsigned char* raw2 = alt.getRawImage(); - for (size_t i = 0; i < raw1size; i++, raw1++, raw2++) + if (raw1 == nullptr) { return false;} + if (raw2 == nullptr) { return false;} + for (size_t i = 0; i < raw1size; i++) { - if (*raw1 != *raw2) { return false; } + if (raw1[i] != raw2[i]) { + return false; + } } return true; } diff --git a/src/libYARP_sig/src/yarp/sig/ImageUtils.cpp b/src/libYARP_sig/src/yarp/sig/ImageUtils.cpp index b434c33ab3c..0dbf5dc2f2a 100644 --- a/src/libYARP_sig/src/yarp/sig/ImageUtils.cpp +++ b/src/libYARP_sig/src/yarp/sig/ImageUtils.cpp @@ -159,9 +159,7 @@ bool utils::sum(Image& OutImg, const Image& InImg, bool enable_colorkey, int col } yarp::sig::PixelRgb ColorkeyRGB; - ColorkeyRGB.r = colorkey; - ColorkeyRGB.g = colorkey; - ColorkeyRGB.b = colorkey; + ColorkeyRGB = *reinterpret_cast(&colorkey); size_t yis = InImg.height(); size_t xis = InImg.width(); diff --git a/src/libYARP_sig/src/yarp/sig/ImageUtils.h b/src/libYARP_sig/src/yarp/sig/ImageUtils.h index a088a0be53e..58a7fc05d35 100644 --- a/src/libYARP_sig/src/yarp/sig/ImageUtils.h +++ b/src/libYARP_sig/src/yarp/sig/ImageUtils.h @@ -69,10 +69,10 @@ bool YARP_sig_API cropRect(const yarp::sig::Image& inImg, yarp::sig::Image& outImg); /** - * @brief applies an image on the top over another image. - * @param[in/out] OutImg the output image. It must be a valid image on the top of which data will be summed. It may contain a backgroud or it can be zero. + * @brief applies an image on the top over another image. Currently it is implemented only for RGB Images + * @param[in/out] OutImg the output image. It must be a valid image on the top of which data will be summed. It may contain a background or it can be zero. * @param[in] InImg the layer to be applied - * @param[in] colorkey colorkey for the InImg image. If a pixel is == colorkey, then it will be made transparent and the backgroud will be visible. + * @param[in] colorkey colorkey for the InImg image. If a pixel is == colorkey, then it will be made transparent and the background will be visible. * @param[in] alpha to be applied to InImg. * @param[in] off_x horizontal offset applied to InImg. Excess will be cropped. * @param[in] off_y vertical offset applied to InImg. Excess will be cropped. diff --git a/src/libYARP_sig/src/yarp/sig/LayeredImage.h b/src/libYARP_sig/src/yarp/sig/LayeredImage.h index 222faa64590..fc082b6b4dc 100644 --- a/src/libYARP_sig/src/yarp/sig/LayeredImage.h +++ b/src/libYARP_sig/src/yarp/sig/LayeredImage.h @@ -25,29 +25,42 @@ class YARP_sig_API yarp::sig::ImageLayer public: yarp::sig::FlexImage layer; bool enable=true; - struct colorkey_s + class colorkey_s { - bool enable = true; - int value=0; + public: + int value; + bool enable; //default constructor - colorkey_s() = default; - } colorkey; - - struct alpha_s + colorkey_s() : enable(true), value(0) {} + + void setValueAsPixelRgb(yarp::sig::PixelRgb v) + { + value = *reinterpret_cast(&v); + } + yarp::sig::PixelRgb getValueAsPixelRgb() + { + return *reinterpret_cast(&value); + } + }; + colorkey_s colorkey; + + class alpha_s { - bool enable = true; - float value=1.0; + public: + bool enable; + float value; - //default constructor - alpha_s() = default; - } alpha; + // default constructor + alpha_s() : enable(true), value(1.0) {} + }; + alpha_s alpha; bool can_be_compressed = true; int offset_x=0; int offset_y=0; - ImageLayer(const yarp::sig::FlexImage& img, bool ena = true, colorkey_s ckey = colorkey_s {}, alpha_s alph = alpha_s {}, bool compress=true, int off_x = 0, int off_y = 0) + ImageLayer(const yarp::sig::FlexImage& img, bool ena = true, colorkey_s ckey = colorkey_s(), alpha_s alph = alpha_s(), bool compress = true, int off_x = 0, int off_y = 0) { layer = img; enable = ena; diff --git a/src/libYARP_sig/tests/LayeredImageTest.cpp b/src/libYARP_sig/tests/LayeredImageTest.cpp index 61d29f18687..8355ddd3aee 100644 --- a/src/libYARP_sig/tests/LayeredImageTest.cpp +++ b/src/libYARP_sig/tests/LayeredImageTest.cpp @@ -21,7 +21,23 @@ using namespace yarp::os::impl; using namespace yarp::sig; using namespace yarp::os; -TEST_CASE("sig::LayeredImageTest", "[yarp::sig]") +void fillTestImage(FlexImage& img) +{ +#if 1 + img.zero(); +#else + yarp::sig::ImageOf rgb_img = *(reinterpret_cast*>(&img)); + for (size_t iy = 0; iy < rgb_img.height(); iy++) + for (size_t ix = 0; ix < rgb_img.width(); ix++) + { + rgb_img.pixel(ix, iy).r = 1 + ix + iy*10; + rgb_img.pixel(ix, iy).g = 2 + ix + iy*10; + rgb_img.pixel(ix, iy).b = 3 + ix + iy*10; + } +#endif +} + + TEST_CASE("sig::LayeredImageTest", "[yarp::sig]") { NetworkBase::setLocalMode(true); @@ -30,14 +46,17 @@ TEST_CASE("sig::LayeredImageTest", "[yarp::sig]") FlexImage imageback; imageback.setPixelCode(VOCAB_PIXEL_RGB); imageback.resize(4, 8); + fillTestImage(imageback); FlexImage lay0; lay0.setPixelCode(VOCAB_PIXEL_RGB); lay0.resize(4, 8); + fillTestImage(lay0); FlexImage lay1; lay1.setPixelCode(VOCAB_PIXEL_RGB); lay1.resize(4, 8); + fillTestImage(lay1); LayeredImage multiLayerImageIn; multiLayerImageIn.background = imageback; @@ -71,6 +90,7 @@ TEST_CASE("sig::LayeredImageTest", "[yarp::sig]") FlexImage imageback; imageback.setPixelCode(VOCAB_PIXEL_RGB); imageback.resize(16, 8); + fillTestImage(imageback); LayeredImage multiLayerImageIn; multiLayerImageIn.background = imageback; @@ -190,26 +210,29 @@ TEST_CASE("sig::LayeredImageTest", "[yarp::sig]") yarp::sig::ImageOf lay0; lay0.resize(4, 4); lay0.zero(); - imageback.pixel(0, 0).r = 20; - imageback.pixel(1, 0).r = 20; - imageback.pixel(2, 0).r = 20; - imageback.pixel(3, 0).r = 20; - imageback.pixel(0, 1).r = 20; - imageback.pixel(1, 1).r = 50; - imageback.pixel(2, 1).r = 50; - imageback.pixel(3, 1).r = 20; - imageback.pixel(0, 2).r = 20; - imageback.pixel(1, 2).r = 50; - imageback.pixel(2, 2).r = 50; - imageback.pixel(3, 2).r = 20; - imageback.pixel(0, 3).r = 20; - imageback.pixel(1, 3).r = 50; - imageback.pixel(2, 3).r = 20; - imageback.pixel(3, 3).r = 20; + lay0.pixel(0, 0).r = 20; + lay0.pixel(1, 0).r = 20; + lay0.pixel(2, 0).r = 20; + lay0.pixel(3, 0).r = 20; + lay0.pixel(0, 1).r = 20; + lay0.pixel(1, 1).r = 50; + lay0.pixel(2, 1).r = 50; + lay0.pixel(3, 1).r = 20; + lay0.pixel(0, 2).r = 20; + lay0.pixel(1, 2).r = 50; + lay0.pixel(2, 2).r = 50; + lay0.pixel(3, 2).r = 20; + lay0.pixel(0, 3).r = 20; + lay0.pixel(1, 3).r = 50; + lay0.pixel(2, 3).r = 20; + lay0.pixel(3, 3).r = 20; LayeredImage multiLayerImageIn; multiLayerImageIn.background = *(reinterpret_cast(&imageback)); multiLayerImageIn.layers.push_back(*(reinterpret_cast(&lay0))); + yarp::sig::PixelRgb colorkeyRgb; + colorkeyRgb.r = 20; + multiLayerImageIn.layers[0].colorkey.setValueAsPixelRgb(colorkeyRgb); FlexImage flat_img = multiLayerImageIn; yarp::sig::ImageOf flat_rgb_img = *(reinterpret_cast*>(&flat_img));