I have a 256x256 array of float
s that represents a heightmap.
I would like to export it to a 16bit per pixel RAW image. What would be the correct way of converting float
to uint16_t
. (I'm aware of the precision loss)
My quick and dirty code for testing:
void ExportHeightmap(const Vector<float>& rHeights)
{
std::vector<uint16_t> output(256 * 256);
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::min();
for (size_t i = 0; i < output.size(); ++i)
{
float f = rHeights[i];
if (min > f) min = f;
if (max < f) max = f;
output[i] = static_cast<uint16_t>(rHeights[i]);
}
std::cout << " Min: " << min << std::endl; // -49.77
std::cout << " Max: " << max << std::endl; // 357.84
std::fstream file("Heightmap.raw", std::ios::out | std::ios::binary);
file.write((char*)output.data(), output.size() * sizeof(uint16_t));
}
EDIT: My goal is to export heightmap made in the application to an image.