Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Jan 7, 2025
1 parent e6242dd commit 0493a00
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
8 changes: 6 additions & 2 deletions src/libYARP_sig/src/yarp/sig/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions src/libYARP_sig/src/yarp/sig/ImageUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<yarp::sig::PixelRgb*>(&colorkey);

size_t yis = InImg.height();
size_t xis = InImg.width();
Expand Down
6 changes: 3 additions & 3 deletions src/libYARP_sig/src/yarp/sig/ImageUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 26 additions & 13 deletions src/libYARP_sig/src/yarp/sig/LayeredImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int*>(&v);
}
yarp::sig::PixelRgb getValueAsPixelRgb()
{
return *reinterpret_cast<yarp::sig::PixelRgb*>(&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;
Expand Down
57 changes: 40 additions & 17 deletions src/libYARP_sig/tests/LayeredImageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<yarp::sig::PixelRgb> rgb_img = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&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);

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -190,26 +210,29 @@ TEST_CASE("sig::LayeredImageTest", "[yarp::sig]")
yarp::sig::ImageOf<yarp::sig::PixelRgb> 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<yarp::sig::FlexImage*>(&imageback));
multiLayerImageIn.layers.push_back(*(reinterpret_cast<yarp::sig::FlexImage*>(&lay0)));
yarp::sig::PixelRgb colorkeyRgb;
colorkeyRgb.r = 20;
multiLayerImageIn.layers[0].colorkey.setValueAsPixelRgb(colorkeyRgb);

FlexImage flat_img = multiLayerImageIn;
yarp::sig::ImageOf<yarp::sig::PixelRgb> flat_rgb_img = *(reinterpret_cast<yarp::sig::ImageOf<yarp::sig::PixelRgb>*>(&flat_img));
Expand Down

0 comments on commit 0493a00

Please sign in to comment.