When I load an image as float using STB_Image the values seem to be off. I created an image to test it . (The used RGB code here is [127, 255, 32])
When I load this image as unsigned char
using stbi_load()
I get the correct values. But when I load it as float
using stbi_loadf()
I get wrong values which don't really make sense to me.
This is the code I used for testing:
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <sstream>
#include <iomanip>
#include <iostream>
#include <string>
struct ColorF {
float r;
float g;
float b;
float a;
std::string toString() {
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << "[" << this->r << ", " << this->g << ", " << this->b << ", " << this->a << "]";
return stream.str();
}
};
struct ColorUC {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
std::string toString() {
std::stringstream stream;
stream << "[" << (float) this->r / 255.0f << ", " << (float) this->g / 255.0f << ", " << (float) this->b / 255.0f << ", " << (float) this->a / 255.0f << "]";
return stream.str();
}
};
int main() {
int width, height, channels;
float* image = stbi_loadf("test.png", &width, &height, &channels, STBI_rgb_alpha);
// print content of first pixel of the image
std::cout << ((ColorF*) image)->toString() << std::endl;
unsigned char* jpeg = stbi_load("test.png", &width, &height, &channels, STBI_rgb_alpha);
// print content of first pixel of the image
std::cout << ((ColorUC*) jpeg)->toString() << std::endl;
stbi_image_free(image);
stbi_image_free(jpeg);
return 0;
}
The test output I get is this:
[0.22, 1.00, 0.01, 1.00]
[0.50, 1.00, 0.13, 1.00]
In theory this should print out the same values, the bottom one being the correct one, on both lines but for some reason it doesn't.
Now i could of course use the unsigned char values and write myself a function that converts everything to the proper float values but I feel like there should be a way to do this just using STB_Image itself.