Values passed to the put function don't have any effect

Hello. I have this code :
tgaManager.fileWriter.put((unsigned char)min(blue*255.0f,255.0f)).put((unsigned char)min(green*255.0f, 255.0f)).put((unsigned char)min(red*255.0f, 255.0f));

that should print the value decided by the min function in a tga image file. When I open the file, all I see it is a huge black screen. You may be thinking that the values of blue,green and red are all zero but it is not the case.

With this code:
if (x==50 && y==50) {
            cout << "Min BGR: " << endl;
            cout << min (blue*255.0f,255.0f) << ' ' << min (green*255.0f,255.0f) << ' ' << min (red*255.0f,255.0f) << ' ' << x << ' ' << y << endl;
}

I get
Min BGR:
9.54167 29.9188 47.8701 50 50
with it is anything but pure black and also it is the value that I expected for this pixel.

If I change
tgaManager.fileWriter.put((unsigned char)min(blue*255.0f,255.0f)).put((unsigned char)min(green*255.0f, 255.0f)).put((unsigned char)min(red*255.0f, 255.0f));

to this for instance:
tgaManager.fileWriter.put((unsigned char)min(255.0f,255.0f)).put((unsigned char)min(0.0f, 0.0f)).put((unsigned char)min(0.0f, 0.0f));

I receive the expected blue image.

So the three variables blue,green and red are being evaluated as 0.0f, even though they aren't, they are 0.x, 0.y, 0.z, and multiplied by 255 they end being greater than zero as I showed in the above code.

Another weird thing is that if I interrupt the program earlier, skipping lot of calculations and sending some different values of blue,green and red, 0.177, 0.555 and 0.888 respectively, it works

In some moment doing some tests I received a warning message saying something like: "left side assignment has no effect" but then It didn't show up again.

So, anyone has a clue about what might be happening ?

Many thanks.

Comments

  • Do you really mean to be multiplying by 255.0f then truncating the result down to a byte? Aside from getting completely different results on a big endian system, Im not sure what this accomplishes.
    Min BGR:
    9.54167 29.9188 47.8701 50 50

    You've stated yourself that the values of red/green/blue in testing are all greater than 1.
    min(blue*255.0f,255.0f)
    
    if blue is always > 1, blue * 255.0f will always be larger than 255.0f. This makes your minimums return 255.0f every time. 255f casted to unsigned char is 0 on little endian systems. That is why you're getting black for everything.
  • No, the values calculated are in the range 0.0 - 1.0f, then I multiply by 255.0f so to get an correspondent integer. For instance, these
    9.54167 29.9188 47.8701
    are the result of multiplying
    0.0374183 0.117329 0.187726
    by 255f, so I guess your idea of 255.0f being casted to 0 is not happening.
Sign In or Register to comment.