Archive 17/01/2023.

Weird results from Image::GetPixel() - can’t get the colour of a pixel at any coordinates

bremby

Hello,

I’m using code like this:

SharedPtr<Image> colouredMap_ = cache->GetResource<Image>("Maps/coloured_provinces.png");
log_->URHO3D_LOGDEBUGF("image: %d|%d|%d|%u", colouredMap_->GetWidth(), colouredMap_->GetHeight(), colouredMap_->GetDepth(), colouredMap_->GetComponents());    
colouredMap_ = colouredMap_->ConvertToRGBA();
Color c = colouredMap_->GetPixel(0,0);
log_->URHO3D_LOGDEBUGF("pixel colour: %x|%x|%x|%x", c.r_, c.g_, c.b_, c.a_);

The first and the last lines are the most important here. In this example I’m only checking the Pixel at <0,0>, but I’m also trying to use GetPixel() to get the colour of the pixel at X/Y coordinates under the mouse (I checked and the coordinates calculation is OK) and I’m only getting values that don’t make sense to me, like this:

[Sat Jul 15 20:26:38 2017] DEBUG: pixel colour: d1|249f0|0|7d0
[Sat Jul 15 20:26:38 2017] DEBUG: pixel colour: 7a|324b0|0|7d0
[Sat Jul 15 20:26:38 2017] DEBUG: pixel colour: be|36b00|0|7d0
[Sat Jul 15 20:26:38 2017] DEBUG: pixel colour: a8|42680|0|7d0

The red component has sane values, although given the image I’m supplying it still is incorrect. The green component doesn’t make sense at all, the blue is always zero and alpha is always 7d0. The picture has large areas of single colors, and not even that is represented in the data. The data just seems random, although apparently consistent between runs of the app.

What am I doing wrong? I tried PNG and BMP formats with the same result, I tried regenerating the image (in GIMP), I also tried a different image with similar results. I also tried GetPixelInt(), and even though I didn’t decode the unsigned int value, it behaves similarly.

1vanK

c.r_, c.g_, c.b_, c.a_ is float (0 - 1), why are you using %x?

bremby

Because I’m dumb and I simply assumed integer values between 0 and 255. Thanks.

This made me realize those values are floats, so multiplying by 256 and casting to int gives me reasonable (and probably right) values.

But for my usage this may not be absolutely correct. I need absolutely precise value of the colour and I’m afraid float operations may result in some rounding errors. Is it possible to avoid using floats completely and just get the 4 bytes (RGBA) instead?

Lumak
unsigned Color::ToUInt() const
1vanK

Image::GetPixelInt()

bremby

Thanks guys! :slight_smile:

SirNate0

You seem to have solved your problem, but I wanted to mention that it should be multiplying by 255 and casting to int, not multiplying by 256, as you want the 0-1 float mapped to a 0-255 integer (unsigned char).

bremby

Thanks, I know, it was an off-by-one error. I realized it just after posting the message. :slight_smile: