Managing color space from metadata with libpng
Early on the project loaded images with FreeImage, but in practice it turned out not to preserve metadata fully, so the program could not determine an image’s color space correctly and the picture came out washed out and too bright. I later switched to parsing PNGs myself with libpng, choosing an sRGB texture (converted to linear by the hardware when sampled) or a UNORM texture based on the file’s sRGB and gAMA chunks; the offscreen color image stores linear values throughout, and the hardware does the gamma encoding when writing into the sRGB swapchain.
Pixels in the PNG
sRGB-encoded
Texture
sRGB format · converted to linear when sampledUNORM format · taken as linear values as is
Offscreen color image
R8G8B8A8_UNORM · stores linear values
Swapchain
B8G8R8A8_SRGB · encoded back to sRGB on write
The last ramp matches the first: what you see on screen is the image as it really is.The sRGB image is read as linear, one conversion is missing, and the last ramp comes out brighter and grayer than the original.

