I’m writing a C++ program which reads a texture image file then convert and save as .png.
When I met the ARGB half precision float pixel format.
I tried for each pixel ARGB channel
- read 16 bits
- convert to 32bit c primitive float
- multiply 255 or 1 (i’m not sure what’s the valid range of the half-float value)
- cast to a BYTE value
but I couldn’t get the visible image. maybe the convert method was incorrect.
What’s the formula to convert the half precision float to 8bit color intensity value?
The details of reading half floats are explained, including code, in a blog post here:
http://fpmurphy.blogspot.com/2008/12/half-precision-floating-point-format_14.html
After reading in your floats you will have to perform a tone mapping operation to get you values in the 0..255 ranged allowed for PNGs. This operations is called “tone mapping”, and you have various options to perform it. A very simple thing to do would be to find the maximum value in the image, M and scale all pixels intensities by 255 / M to get into the desired range. There are algorithms which are quite a bit more involved, though.
Depending on the source of your images you might also apply gamma correction before writing out the images. Because you have to work with float values anyway I’d suggest to do all calculations using floats and do the float->byte conversion as the last step to avoid image quality problems like banding.